summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-05-24 19:33:36 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-05-24 19:33:36 +0200
commit76e8df767f58b5f7e89853d74254679cfbb63fd9 (patch)
treebfd6c6a8ba5dada139b13e040bbd721d94d1e667
parentac41cbea6ca348e662cf8996d2b7127066825af5 (diff)
refactoring: next part of env implementation
-rw-r--r--prototypes/LamMachine.hs24
1 files changed, 16 insertions, 8 deletions
diff --git a/prototypes/LamMachine.hs b/prototypes/LamMachine.hs
index cbb0d479..0d176d8f 100644
--- a/prototypes/LamMachine.hs
+++ b/prototypes/LamMachine.hs
@@ -54,6 +54,7 @@ data Env
54 = ELet Exp 54 = ELet Exp
55 | ELet1 Exp 55 | ELet1 Exp
56 | EApp1 !Int Exp 56 | EApp1 !Int Exp
57 | ECase !Int [String] [Exp]
57 deriving Eq 58 deriving Eq
58 59
59--------------------------------------------------------------------- toolbox: pretty print 60--------------------------------------------------------------------- toolbox: pretty print
@@ -84,6 +85,7 @@ instance PShow MSt where
84 ELet x -> Let x y 85 ELet x -> Let x y
85 ELet1 x -> Let y x 86 ELet1 x -> Let y x
86 EApp1 i x -> HNF i $ App y x 87 EApp1 i x -> HNF i $ App y x
88 ECase i cns xs -> HNF i $ Case cns y xs
87 89
88shUps a b = DPreOp (-20) (SimpleAtom $ show a) b 90shUps a b = DPreOp (-20) (SimpleAtom $ show a) b
89shUps' a x b = DPreOp (-20) (SimpleAtom $ show a ++ show x) b 91shUps' a x b = DPreOp (-20) (SimpleAtom $ show a ++ show x) b
@@ -174,6 +176,7 @@ tryRemoves_ (Var' i: vs) dt = maybe (tryRemoves_ vs dt) (\(is, st) -> tryRemoves
174 downDown i (ELet x: xs) = (\x (is, xs) -> (up 0 1 <$> is, ELet x: xs)) <$> down (i-1) x <*> downDown (i-1) xs 176 downDown i (ELet x: xs) = (\x (is, xs) -> (up 0 1 <$> is, ELet x: xs)) <$> down (i-1) x <*> downDown (i-1) xs
175 downDown i (ELet1 x: xs) = (\x (is, xs) -> (is, ELet1 x: xs)) <$> down (i+1) x <*> downDown i xs 177 downDown i (ELet1 x: xs) = (\x (is, xs) -> (is, ELet1 x: xs)) <$> down (i+1) x <*> downDown i xs
176 downDown i (EApp1 j x: xs) = (\x (is, xs) -> (is, EApp1 j x: xs)) <$> down i x <*> downDown i xs 178 downDown i (EApp1 j x: xs) = (\x (is, xs) -> (is, EApp1 j x: xs)) <$> down i x <*> downDown i xs
179 downDown i (ECase j cns x: xs) = (\x (is, xs) -> (is, ECase j cns x: xs)) <$> traverse (down i) x <*> downDown i xs
177 180
178----------------------------------------------------------- machine code begins here 181----------------------------------------------------------- machine code begins here
179 182
@@ -222,10 +225,8 @@ steps lev nostep {-ready-} bind cont dt@(MSt e vs) = case e of
222 Y (Lam x) -> step "Y" $ MSt x $ ELet e: vs 225 Y (Lam x) -> step "Y" $ MSt x $ ELet e: vs
223 226
224 App a b -> case a of 227 App a b -> case a of
225 Lam x | usedVar 0 x 228 Lam x | usedVar 0 x -> step "app" $ MSt x $ ELet b: vs
226 -> step "app" $ MSt x $ ELet b: vs 229 | otherwise -> step "appdel" $ tryRemoves (fvs b) $ MSt x vs
227 Lam x -> step "appdel" $ tryRemoves (fvs b) $ MSt x vs
228-- Var i -> lookupHNF' "appvar" (\e (App _ y) -> App e y) i dt
229 _ -> bind "app1" (hnf "app1 hnf" (step "appexp" . focus)) $ MSt a $ EApp1 lev b: vs 230 _ -> bind "app1" (hnf "app1 hnf" (step "appexp" . focus)) $ MSt a $ EApp1 lev b: vs
230 where 231 where
231 focus (MSt b xs) = MSt (App b c) xs' 232 focus (MSt b xs) = MSt (App b c) xs'
@@ -234,10 +235,15 @@ steps lev nostep {-ready-} bind cont dt@(MSt e vs) = case e of
234 f (EApp1 _ c: xs) = (c, xs) 235 f (EApp1 _ c: xs) = (c, xs)
235 f (ELet x: (f -> (c, xs))) = (up 0 1 c, ELet x: xs) 236 f (ELet x: (f -> (c, xs))) = (up 0 1 c, ELet x: xs)
236 237
237 Case cn a cs -> case a of 238 Case cns a cs -> case a of
238 Con cn i es -> step "case" $ tryRemoves (nub $ foldMap fvs $ delElem i cs) $ MSt (foldl App (cs !! i) es) vs 239 Con cn i es -> step "case" $ tryRemoves (nub $ foldMap fvs $ delElem i cs) $ MSt (foldl App (cs !! i) es) vs
239 Var i -> lookupHNF' "casevar" (\e (Case cn _ cs) -> Case cn e cs) i dt 240 _ -> bind "case1" (hnf "case hnf" (step "caseexp" . focus)) $ MSt a $ ECase lev cns cs: vs
240 _ -> step "caseexp" $ MSt (Case cn (Var 0) $ up 0 1 <$> cs) $ ELet a: vs 241 where
242 focus (MSt b xs) = MSt (Case cns b c) xs'
243 where
244 (c, xs') = f xs
245 f (ECase _ _ c: xs) = (c, xs)
246 f (ELet x: (f -> (c, xs))) = (up 0 1 <$> c, ELet x: xs)
241 247
242 Op2 op x y -> case (x, y) of 248 Op2 op x y -> case (x, y) of
243 (Int e, Int f) -> step "op-done" $ MSt (int op e f) vs 249 (Int e, Int f) -> step "op-done" $ MSt (int op e f) vs
@@ -283,6 +289,7 @@ steps lev nostep {-ready-} bind cont dt@(MSt e vs) = case e of
283 shiftL path 0 (MSt x (ELet e: es)) = (path, MSt e $ ELet1 x: es) 289 shiftL path 0 (MSt x (ELet e: es)) = (path, MSt e $ ELet1 x: es)
284 shiftL path n (MSt x (ELet e: es)) = shiftL (TELet: path) (n-1) $ MSt (Let e x) es 290 shiftL path n (MSt x (ELet e: es)) = shiftL (TELet: path) (n-1) $ MSt (Let e x) es
285 shiftL path n (MSt x (EApp1 i e: es)) = shiftL (TEApp1: path) n $ MSt (HNF i $ App x e) es 291 shiftL path n (MSt x (EApp1 i e: es)) = shiftL (TEApp1: path) n $ MSt (HNF i $ App x e) es
292 shiftL path n (MSt x (ECase i cns e: es)) = shiftL (TECase: path) n $ MSt (HNF i $ Case cns x e) es
286 shiftL path n (MSt x (ELet1 e: es)) = shiftL (TELet1: path) n $ MSt (Let x e) es 293 shiftL path n (MSt x (ELet1 e: es)) = shiftL (TELet1: path) n $ MSt (Let x e) es
287 shiftL path n st = error $ "shiftL: " ++ show (path, n) ++ "\n" ++ ppShow st 294 shiftL path n st = error $ "shiftL: " ++ show (path, n) ++ "\n" ++ ppShow st
288 295
@@ -295,6 +302,7 @@ steps lev nostep {-ready-} bind cont dt@(MSt e vs) = case e of
295 shiftR [] st = st 302 shiftR [] st = st
296 shiftR (TELet: n) (y, MSt (Let e x) es) = shiftR n (up 0 1 y, MSt x $ ELet e: es) 303 shiftR (TELet: n) (y, MSt (Let e x) es) = shiftR n (up 0 1 y, MSt x $ ELet e: es)
297 shiftR (TEApp1: n) (y, MSt (HNF l (App x e)) es) = shiftR n (y, MSt x $ EApp1 l e: es) 304 shiftR (TEApp1: n) (y, MSt (HNF l (App x e)) es) = shiftR n (y, MSt x $ EApp1 l e: es)
305 shiftR (TECase: n) (y, MSt (HNF l (Case cns x e)) es) = shiftR n (y, MSt x $ ECase l cns e: es)
298 shiftR (TELet1: n) (y, MSt (Let x e) es) = shiftR n (y, MSt x $ ELet1 e: es) 306 shiftR (TELet1: n) (y, MSt (Let x e) es) = shiftR n (y, MSt x $ ELet1 e: es)
299 shiftR path x = error $ "shiftR: " ++ show path ++ "\n" ++ ppShow x 307 shiftR path x = error $ "shiftR: " ++ show path ++ "\n" ++ ppShow x
300 308
@@ -305,7 +313,7 @@ steps lev nostep {-ready-} bind cont dt@(MSt e vs) = case e of
305 313
306 delElem i xs = take i xs ++ drop (i+1) xs 314 delElem i xs = take i xs ++ drop (i+1) xs
307 315
308data TE = TELet | TELet1 | TEApp1 316data TE = TELet | TELet1 | TEApp1 | TECase
309 deriving Show 317 deriving Show
310 318
311---------------------------------------------------------------------------------------- examples 319---------------------------------------------------------------------------------------- examples