stgi-1.1: Educational implementation of the STG (Spineless Tagless G-machine)

Safe HaskellNone
LanguageHaskell2010

Stg.Marshal.ToStg

Description

Convert Haskell values to STG values.

Synopsis

Documentation

class ToStg value where Source #

Convert a Haskell value to an STG binding.

Instances of this class should have a corresponding FromStg instance to retrieve a value fom the program, with the two being inverse to each other (up to forcing the generated thunks).

This class contains a helper function, toStgWithGlobals, this is hidden from the outside. If you want to write your own instance, have a look at the source for documentation.

Minimal complete definition

toStg | toStgWithGlobals

Methods

toStg :: Var -> value -> Program Source #

toStgWithGlobals :: Var -> value -> Writer Program Program Source #

Some definitions, such as the one for lists, require certain global values to be present (such as nil). In order to avoid duplicate definitions, this function allows defining top-level elements using Writers tell function.

Instances

ToStg Bool Source #
>>> ppr (toStg "bool" True)
bool = \ -> True
ToStg Int Source #

Same as the Integer instance, but makes for shorter type annotations

ToStg Integer Source #
>>> ppr (toStg "int" (1 :: Integer))
int = \ -> Int# 1#
ToStg () Source #
>>> ppr (toStg "unit" ())
unit = \ -> Unit
ToStg a => ToStg [a] Source #
>>> ppr (toStg "list" ([] :: [Int]))
list = \ => nil;
nil = \ -> Nil
>>> ppr (toStg "list" [1, 2, 3 :: Int])
list = \ =>
    letrec __0_value = \ -> Int# 1#;
           __1_cons = \(__1_value __2_cons) -> Cons __1_value __2_cons;
           __1_value = \ -> Int# 2#;
           __2_cons = \(__2_value) -> Cons __2_value nil;
           __2_value = \ -> Int# 3#
    in Cons __0_value __1_cons;
nil = \ -> Nil
ToStg a => ToStg (Maybe a) Source #
>>> ppr (toStg "maybe" (Nothing :: Maybe Int))
maybe = \ => nothing;
nothing = \ -> Nothing
>>> ppr (toStg "maybe" (Just 1 :: Maybe Int))
maybe = \ =>
    let __justVal = \ -> Int# 1#
    in Just __justVal
(ToStg a, ToStg b) => ToStg (Either a b) Source #
>>> ppr (toStg "either" (Left 1 :: Either Int [Int]))
either = \ =>
    let __leftval = \ -> Int# 1#
    in Left __leftval
>>> ppr (toStg "either" (Right 2 :: Either [Int] Int))
either = \ =>
    let __rightval = \ -> Int# 2#
    in Right __rightval
(ToStg a, ToStg b) => ToStg (a, b) Source #
>>> ppr (toStg "pair" ((1,2) :: (Int,Int)))
pair = \ =>
    let __fst = \ -> Int# 1#;
        __snd = \ -> Int# 2#
    in Pair __fst __snd

Methods

toStg :: Var -> (a, b) -> Program Source #

toStgWithGlobals :: Var -> (a, b) -> Writer Program Program Source #

(ToStg a, ToStg b, ToStg c) => ToStg (a, b, c) Source #
>>> ppr (toStg "triple" ((1,2,3) :: (Int,Int,Int)))
triple = \ =>
    let __x = \ -> Int# 1#;
        __y = \ -> Int# 2#;
        __z = \ -> Int# 3#
    in Triple __x __y __z

Methods

toStg :: Var -> (a, b, c) -> Program Source #

toStgWithGlobals :: Var -> (a, b, c) -> Writer Program Program Source #

(ToStg a, ToStg b, ToStg c, ToStg d) => ToStg (a, b, c, d) Source #
>>> ppr (toStg "quadruple" ((1,2,3,4) :: (Int,Int,Int,Int)))
quadruple = \ =>
    let __w = \ -> Int# 1#;
        __x = \ -> Int# 2#;
        __y = \ -> Int# 3#;
        __z = \ -> Int# 4#
    in Quadruple __w __x __y __z

Methods

toStg :: Var -> (a, b, c, d) -> Program Source #

toStgWithGlobals :: Var -> (a, b, c, d) -> Writer Program Program Source #

(ToStg a, ToStg b, ToStg c, ToStg d, ToStg e) => ToStg (a, b, c, d, e) Source #
>>> ppr (toStg "quintuple" ((1,2,3,4,5) :: (Int,Int,Int,Int,Int)))
quintuple = \ =>
    let __v = \ -> Int# 1#;
        __w = \ -> Int# 2#;
        __x = \ -> Int# 3#;
        __y = \ -> Int# 4#;
        __z = \ -> Int# 5#
    in Quintuple __v __w __x __y __z

Methods

toStg :: Var -> (a, b, c, d, e) -> Program Source #

toStgWithGlobals :: Var -> (a, b, c, d, e) -> Writer Program Program Source #