finally fix dispatch scope

This commit is contained in:
crumbtoo
2023-12-06 11:23:44 -07:00
parent 1b51ee0c64
commit 87d3aac1fb

101
src/GM.hs
View File

@@ -50,6 +50,7 @@ data Key = NameKey Name
data Instr = Unwind data Instr = Unwind
| PushGlobal Name | PushGlobal Name
| PushConstr Tag Int
| PushInt Int | PushInt Int
| Push Int | Push Int
| MkAp | MkAp
@@ -142,29 +143,30 @@ isFinal :: GmState -> Bool
isFinal st = null $ st ^. gmCode isFinal st = null $ st ^. gmCode
step :: GmState -> GmState step :: GmState -> GmState
step state = case head (state ^. gmCode) of step st = case head (st ^. gmCode) of
-- TODO: clean this up. let transition functions use the 'state' parameter -- TODO: clean this up. let transition functions use the 'state' parameter
-- instead of passing it to them. -- instead of passing it to them.
Unwind -> unwindI state Unwind -> unwindI
PushGlobal n -> pushGlobalI n state PushGlobal n -> pushGlobalI n
PushInt n -> pushIntI n state PushConstr t n -> pushConstrI t n
Push n -> pushI n state PushInt n -> pushIntI n
MkAp -> mkApI state Push n -> pushI n
Slide n -> slideI n state MkAp -> mkApI
Pop n -> popI n state Slide n -> slideI n
Update n -> updateI n state Pop n -> popI n
Alloc n -> allocI n state Update n -> updateI n
Eval -> evalI state Alloc n -> allocI n
Neg -> negI state Eval -> evalI
Add -> addI state Neg -> negI
Sub -> subI state Add -> addI
Mul -> mulI state Sub -> subI
Div -> divI state Mul -> mulI
Split n -> splitI n state Div -> divI
Split n -> splitI n
where where
pushGlobalI :: Name -> GmState -> GmState pushGlobalI :: Name -> GmState
pushGlobalI k st = st pushGlobalI k = st
& advanceCode & advanceCode
& gmStack .~ s' & gmStack .~ s'
where where
@@ -175,9 +177,12 @@ step state = case head (state ^. gmCode) of
a = lookupN k m a = lookupN k m
& fromMaybe (error $ "undefined var: " <> show k) & fromMaybe (error $ "undefined var: " <> show k)
pushConstrI :: Tag -> Int -> GmState
pushConstrI = undefined
-- Extension Rules 1,2 (sharing) -- Extension Rules 1,2 (sharing)
pushIntI :: Int -> GmState -> GmState pushIntI :: Int -> GmState
pushIntI n st = case lookupN n' m of pushIntI n = case lookupN n' m of
Just a -> st Just a -> st
& advanceCode & advanceCode
& gmStack .~ s' & gmStack .~ s'
@@ -201,8 +206,8 @@ step state = case head (state ^. gmCode) of
n' = show n n' = show n
-- Core Rule 2. (no sharing) -- Core Rule 2. (no sharing)
-- pushIntI :: Int -> GmState -> GmState -- pushIntI :: Int -> GmState
-- pushIntI n st = st -- pushIntI n = st
-- & advanceCode -- & advanceCode
-- & gmStack .~ s' -- & gmStack .~ s'
-- & gmHeap .~ h' -- & gmHeap .~ h'
@@ -214,8 +219,8 @@ step state = case head (state ^. gmCode) of
-- s' = a : s -- s' = a : s
-- (h',a) = alloc h (NNum n) -- (h',a) = alloc h (NNum n)
mkApI :: GmState -> GmState mkApI :: GmState
mkApI st = st mkApI = st
& advanceCode & advanceCode
& gmStack .~ s' & gmStack .~ s'
& gmHeap .~ h' & gmHeap .~ h'
@@ -230,8 +235,8 @@ step state = case head (state ^. gmCode) of
-- a `Push n` instruction pushes the address of (n+1)-th argument onto -- a `Push n` instruction pushes the address of (n+1)-th argument onto
-- the stack. -- the stack.
pushI :: Int -> GmState -> GmState pushI :: Int -> GmState
pushI n st = st pushI n = st
& advanceCode & advanceCode
& gmStack %~ (a:) & gmStack %~ (a:)
where where
@@ -246,16 +251,16 @@ step state = case head (state ^. gmCode) of
-- 1: f 1: f x y -- 1: f 1: f x y
-- 2: f x -- 2: f x
-- 3: f x y -- 3: f x y
slideI :: Int -> GmState -> GmState slideI :: Int -> GmState
slideI n st = st slideI n = st
& advanceCode & advanceCode
& gmStack .~ s' & gmStack .~ s'
where where
(a:s) = st ^. gmStack (a:s) = st ^. gmStack
s' = a : drop n s s' = a : drop n s
updateI :: Int -> GmState -> GmState updateI :: Int -> GmState
updateI n st = st updateI n = st
& advanceCode & advanceCode
& gmStack .~ s & gmStack .~ s
& gmHeap .~ h' & gmHeap .~ h'
@@ -265,13 +270,13 @@ step state = case head (state ^. gmCode) of
h' = st ^. gmHeap h' = st ^. gmHeap
& update an (NInd e) & update an (NInd e)
popI :: Int -> GmState -> GmState popI :: Int -> GmState
popI n st = st popI n = st
& advanceCode & advanceCode
& gmStack %~ drop n & gmStack %~ drop n
allocI :: Int -> GmState -> GmState allocI :: Int -> GmState
allocI n st = st allocI n = st
& advanceCode & advanceCode
& gmStack .~ s' & gmStack .~ s'
& gmHeap .~ h' & gmHeap .~ h'
@@ -286,8 +291,8 @@ step state = case head (state ^. gmCode) of
allocNode k g = allocNode (k-1) g' & _2 %~ (a:) allocNode k g = allocNode (k-1) g' & _2 %~ (a:)
where (g',a) = alloc g NUninitialised where (g',a) = alloc g NUninitialised
evalI :: GmState -> GmState evalI :: GmState
evalI st = st evalI = st
-- Unwind performs the actual evaluation; we just set the stage -- Unwind performs the actual evaluation; we just set the stage
-- so Unwind knows what to do -- so Unwind knows what to do
& gmCode .~ [Unwind] & gmCode .~ [Unwind]
@@ -299,17 +304,17 @@ step state = case head (state ^. gmCode) of
(_:i) = st ^. gmCode (_:i) = st ^. gmCode
(a:s) = st ^. gmStack (a:s) = st ^. gmStack
negI :: GmState -> GmState negI :: GmState
negI = primitive1 boxInt unboxInt negate negI = primitive1 boxInt unboxInt negate st
addI, subI, mulI, divI :: GmState -> GmState addI, subI, mulI, divI :: GmState
addI = primitive2 boxInt unboxInt (+) addI = primitive2 boxInt unboxInt (+) st
subI = primitive2 boxInt unboxInt (-) subI = primitive2 boxInt unboxInt (-) st
mulI = primitive2 boxInt unboxInt (*) mulI = primitive2 boxInt unboxInt (*) st
divI = primitive2 boxInt unboxInt div divI = primitive2 boxInt unboxInt div st
splitI :: Int -> GmState -> GmState splitI :: Int -> GmState
splitI n st = st splitI n = st
& advanceCode & advanceCode
& gmStack .~ s' & gmStack .~ s'
where where
@@ -319,8 +324,8 @@ step state = case head (state ^. gmCode) of
NConstr _ components = hLookupUnsafe a h NConstr _ components = hLookupUnsafe a h
-- the complex heart of the G-machine -- the complex heart of the G-machine
unwindI :: GmState -> GmState unwindI :: GmState
unwindI st = case hLookupUnsafe a h of unwindI = case hLookupUnsafe a h of
NNum _ -> st NNum _ -> st
& gmCode .~ i' & gmCode .~ i'
& gmStack .~ s' & gmStack .~ s'