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

Safe HaskellNone
LanguageHaskell2010

Stg.Marshal

Description

Convert Haskell values to STG values and back.

This module is what users should be using - it reexports only the safe classes.

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 Source

Arguments

:: Var

Name of the binding

-> value 
-> Program 

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
(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
(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
(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

class FromStg value where Source

Look up the value of a global variable.

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

Minimal complete definition

fromStgAddr

Methods

fromStg Source

Arguments

:: StgState 
-> Var

Name of the global, e.g. main

-> Either FromStgError value 

Retrieve the value of a global variable.

Instances

FromStg Bool Source 
FromStg Integer Source

Boxed (Int# 1#) or unboxed (1#)

FromStg () Source 
FromStg a => FromStg [a] Source 
FromStg a => FromStg (Maybe a) Source 
(FromStg a, FromStg b) => FromStg (Either a b) Source 
(FromStg a, FromStg b) => FromStg (a, b) Source 
(FromStg a, FromStg b, FromStg c) => FromStg (a, b, c) Source 
(FromStg a, FromStg b, FromStg c, FromStg d) => FromStg (a, b, c, d) Source 
(FromStg a, FromStg b, FromStg c, FromStg d, FromStg e) => FromStg (a, b, c, d, e) Source 

data FromStgError Source

Constructors

TypeMismatch

e.g. asking for an Int# at an address that contains a Cons

IsWrongLambdaType LambdaType

Tried retrieving a non-constructor

IsBlackhole

Tried retrieving a black hole

BadArity

e.g. Cons x y z

NotFound NotInScope

An unsuccessful variable lookup

AddrNotOnHeap 
NoConstructorMatch

None of the given alternatives matched the given constructor, e.g. when trying to receive a Left as a Just