This commit is contained in:
crumbtoo
2023-11-23 00:56:47 -07:00
parent ac6c0b7457
commit d84fe56fbb
5 changed files with 33 additions and 24 deletions

View File

@@ -277,7 +277,7 @@ Perform the reduction if the first argument is in normal form
\end{bmatrix} \end{bmatrix}
& g & g
} }
{ a_2 : s { a_1 : a_2 : s
& d & d
& h & h
\begin{bmatrix} \begin{bmatrix}

View File

@@ -12,13 +12,13 @@ letrecExample :: Program
letrecExample = [coreProg| letrecExample = [coreProg|
pair x y f = f x y pair x y f = f x y
fst p = p k fst' p = p k
snd p = p k1 snd' p = p k1
f x y = f x y =
letrec a = pair x b letrec a = pair x b
b = pair y a b = pair y a
in fst (snd (snd (snd a))); in fst' (snd' (snd' (snd' a)));
main = f 3 4; main = f 3 4;
|] |]

View File

@@ -49,11 +49,11 @@ alloc :: Heap a -> a -> (Heap a, Addr)
alloc (Heap (u:us) m) v = (Heap us (M.insert u v m), u) alloc (Heap (u:us) m) v = (Heap us (M.insert u v m), u)
alloc (Heap [] _) _ = error "STG heap model ran out of memory..." alloc (Heap [] _) _ = error "STG heap model ran out of memory..."
update :: Heap a -> Addr -> a -> Heap a update :: Addr -> a -> Heap a -> Heap a
update (Heap u m) k v = Heap u (M.adjust (const v) k m) update k v (Heap u m) = Heap u (M.adjust (const v) k m)
free :: Heap a -> Addr -> Heap a free :: Addr -> Heap a -> Heap a
free (Heap u m) k = Heap (k:u) (M.delete k m) free k (Heap u m) = Heap (k:u) (M.delete k m)
hLookup :: Addr -> Heap a -> Maybe a hLookup :: Addr -> Heap a -> Maybe a
hLookup k (Heap _ m) = m !? k hLookup k (Heap _ m) = m !? k

View File

@@ -145,18 +145,18 @@ instantiate _ _ _ = error "unimplemented"
-- instantiate and update -- instantiate and update
instantiateU :: Expr -> Addr -> TiHeap -> [(Name, Addr)] -> TiHeap instantiateU :: Expr -> Addr -> TiHeap -> [(Name, Addr)] -> TiHeap
instantiateU (App f x) root h g = update h'' root (NAp f' x') instantiateU (App f x) root h g = update root (NAp f' x') h''
where where
(h',f') = instantiate f h g (h',f') = instantiate f h g
(h'',x') = instantiate x h' g (h'',x') = instantiate x h' g
instantiateU (Case _ _) _ _ _ = error "cannot instantiate case expressions" instantiateU (Case _ _) _ _ _ = error "cannot instantiate case expressions"
instantiateU (Con t a) root h g = update h root c instantiateU (Con t a) root h g = update root c h
where where
c = NPrim "Pack" (ConP t a) c = NPrim "Pack" (ConP t a)
instantiateU (Var k) root h g = update h' root (NInd a) instantiateU (Var k) root h g = update root (NInd a) h'
where (h',a) = instantiate (Var k) h g where (h',a) = instantiate (Var k) h g
-- i don't really know if this is correct tbh i'm gonna cry -- i don't really know if this is correct tbh i'm gonna cry
@@ -170,7 +170,7 @@ instantiateU (Let NonRec bs e) root h g = h''
let (h',a) = instantiate v h g let (h',a) = instantiate v h g
in (h',(k,a)) in (h',(k,a))
instantiateU (IntE n) root h _ = update h root (NNum n) instantiateU (IntE n) root h _ = update root (NNum n) h
---------------------------------------------------------------------------------- ----------------------------------------------------------------------------------
@@ -211,7 +211,7 @@ step st =
NAp f (hViewUnsafe h -> NInd a) -> NAp f (hViewUnsafe h -> NInd a) ->
TiState (ap:s) d h' g sts' TiState (ap:s) d h' g sts'
where where
h' = (update h ap $ NAp f a) h' = update ap (NAp f a) h
sts' = sts & stsDereferences %~ succ sts' = sts & stsDereferences %~ succ
_ -> _ ->
@@ -240,7 +240,7 @@ step st =
case isDataNode arg of case isDataNode arg of
True -> TiState s'' d h' g sts True -> TiState s'' d h' g sts
where where
h' = update h rootAddr (NNum $ negate n) h' = update rootAddr (NNum $ negate n) h
s'' = rootAddr : s' s'' = rootAddr : s'
(_:rootAddr:s') = s (_:rootAddr:s') = s
NNum n = arg NNum n = arg
@@ -273,7 +273,7 @@ step st =
h' | needsEval cn = h h' | needsEval cn = h
| otherwise = | otherwise =
update h rootAddr (NInd $ if isTrue then t else f) update rootAddr (NInd $ if isTrue then t else f) h
[cn,tn,fn] = hViewUnsafe h <$> [c,t,f] [cn,tn,fn] = hViewUnsafe h <$> [c,t,f]
[c,t,f] = getArgs h s [c,t,f] = getArgs h s
@@ -287,18 +287,27 @@ step st =
primStep _ CasePairP (TiState s d h g sts) = primStep _ CasePairP (TiState s d h g sts) =
case needsEval pn of case needsEval pn of
True -> TiState s' d' h g sts True -> TiState s' d' h g sts
where s' = undefined; d' = undefined where
s' = [p]
d' = drop 1 s : d
False -> TiState s' d h' g sts False -> TiState s' d h' g sts
where s' = undefined; h' = undefined where
s' = drop 1 s
h' = h & update a1 (NAp f x)
& update a2 (NAp a1 y)
rootAddr = head s'
a1 = s' !! 0
a2 = s' !! 1
NData 0 [x,y] = pn
where where
[p,f] = getArgs h s [p,f] = getArgs h s
pn = undefined pn = hLookupUnsafe p h
primStep n (ConP t a) (TiState s d h g sts) = primStep n (ConP t a) (TiState s d h g sts) =
TiState s' d h' g sts TiState s' d h' g sts
where where
s' = drop a s s' = drop a s
h' = update h rootAddr $ NData t argAddrs h' = update rootAddr (NData t argAddrs) h
rootAddr = s !! a rootAddr = s !! a
argAddrs = getArgs h s argAddrs = getArgs h s
@@ -322,8 +331,8 @@ primArbitrary f (TiState s d h g sts) =
Nothing -> d Nothing -> d
h' = case unevaled of h' = case unevaled of
Just _ -> h Just _ -> h
Nothing -> update h rootAddr $ Nothing -> update rootAddr x h
onList f (fmap (\a -> hLookupUnsafe a h) argAddrs) where x = onList f (fmap (\a -> hLookupUnsafe a h) argAddrs)
unevaled = find (\ (_,a) -> needsEval $ hLookupUnsafe a h) ans unevaled = find (\ (_,a) -> needsEval $ hLookupUnsafe a h) ans
ans = [1..] `zip` argAddrs ans = [1..] `zip` argAddrs
@@ -356,7 +365,7 @@ primBinary f (TiState s d h g sts) =
h' | needsEval xarg = h h' | needsEval xarg = h
| needsEval yarg = h | needsEval yarg = h
| otherwise = update h rootAddr (xarg `f` yarg) | otherwise = update rootAddr (xarg `f` yarg) h
d' | needsEval xarg = drop 1 s : d d' | needsEval xarg = drop 1 s : d
| needsEval yarg = drop 2 s : d | needsEval yarg = drop 2 s : d