diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-05-24 19:43:42 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-05-24 19:43:42 +0200 |
commit | e2d67444c5b4153fc7c588f67d7bf01fd4e675cf (patch) | |
tree | ae6c8333ea0ee28d844d815921a2a0f06f3ad4ad /prototypes | |
parent | 76e8df767f58b5f7e89853d74254679cfbb63fd9 (diff) |
next part of env implementation
Diffstat (limited to 'prototypes')
-rw-r--r-- | prototypes/LamMachine.hs | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/prototypes/LamMachine.hs b/prototypes/LamMachine.hs index 0d176d8f..2fe663d1 100644 --- a/prototypes/LamMachine.hs +++ b/prototypes/LamMachine.hs | |||
@@ -55,6 +55,8 @@ data Env | |||
55 | | ELet1 Exp | 55 | | ELet1 Exp |
56 | | EApp1 !Int Exp | 56 | | EApp1 !Int Exp |
57 | | ECase !Int [String] [Exp] | 57 | | ECase !Int [String] [Exp] |
58 | | EOp2_1 !Int Op2 Exp | ||
59 | | EOp2_2 !Int Op2 Exp | ||
58 | deriving Eq | 60 | deriving Eq |
59 | 61 | ||
60 | --------------------------------------------------------------------- toolbox: pretty print | 62 | --------------------------------------------------------------------- toolbox: pretty print |
@@ -86,6 +88,8 @@ instance PShow MSt where | |||
86 | ELet1 x -> Let y x | 88 | ELet1 x -> Let y x |
87 | EApp1 i x -> HNF i $ App y x | 89 | EApp1 i x -> HNF i $ App y x |
88 | ECase i cns xs -> HNF i $ Case cns y xs | 90 | ECase i cns xs -> HNF i $ Case cns y xs |
91 | EOp2_1 i op x -> HNF i $ Op2 op y x | ||
92 | EOp2_2 i op x -> HNF i $ Op2 op x y | ||
89 | 93 | ||
90 | shUps a b = DPreOp (-20) (SimpleAtom $ show a) b | 94 | shUps a b = DPreOp (-20) (SimpleAtom $ show a) b |
91 | shUps' a x b = DPreOp (-20) (SimpleAtom $ show a ++ show x) b | 95 | shUps' a x b = DPreOp (-20) (SimpleAtom $ show a ++ show x) b |
@@ -177,6 +181,8 @@ tryRemoves_ (Var' i: vs) dt = maybe (tryRemoves_ vs dt) (\(is, st) -> tryRemoves | |||
177 | downDown i (ELet1 x: xs) = (\x (is, xs) -> (is, ELet1 x: xs)) <$> down (i+1) x <*> downDown i xs | 181 | downDown i (ELet1 x: xs) = (\x (is, xs) -> (is, ELet1 x: xs)) <$> down (i+1) 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 | 182 | 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 | 183 | downDown i (ECase j cns x: xs) = (\x (is, xs) -> (is, ECase j cns x: xs)) <$> traverse (down i) x <*> downDown i xs |
184 | downDown i (EOp2_1 j op x: xs) = (\x (is, xs) -> (is, EOp2_1 j op x: xs)) <$> down i x <*> downDown i xs | ||
185 | downDown i (EOp2_2 j op x: xs) = (\x (is, xs) -> (is, EOp2_2 j op x: xs)) <$> down i x <*> downDown i xs | ||
180 | 186 | ||
181 | ----------------------------------------------------------- machine code begins here | 187 | ----------------------------------------------------------- machine code begins here |
182 | 188 | ||
@@ -253,11 +259,21 @@ steps lev nostep {-ready-} bind cont dt@(MSt e vs) = case e of | |||
253 | int Mod a b = Int $ a `mod` b | 259 | int Mod a b = Int $ a `mod` b |
254 | int LessEq a b = if a <= b then T else F | 260 | int LessEq a b = if a <= b then T else F |
255 | int EqInt a b = if a == b then T else F | 261 | int EqInt a b = if a == b then T else F |
256 | (Var i, _) -> lookupHNF' "op2-1var" (\e (Op2 op _ y) -> Op2 op e y) i dt | 262 | (Int{}, _) -> bind "op2_2 ready" (hnf "op2_2 hnf" (step "op2_2" . focus2)) $ MSt y $ EOp2_2 lev op x: vs |
257 | (_, Var i) -> lookupHNF' "op2-2var" (\e (Op2 op x _) -> Op2 op x e) i dt | 263 | _ -> bind "op2_1 ready" (hnf "op2_1 hnf" (step "op2_1" . focus1)) $ MSt x $ EOp2_1 lev op y: vs |
258 | (Int{}, _) -> step "op2" $ MSt (Op2 op x (Var 0)) $ ELet y: vs | 264 | where |
259 | (_, Int{}) -> step "op2" $ MSt (Op2 op (Var 0) y) $ ELet x: vs | 265 | focus1 (MSt b xs) = MSt (Op2 op b c) xs' |
260 | _ -> step "op2" $ MSt (Op2 op (Var 0) (Var 1)) $ ELet x: ELet y: vs | 266 | where |
267 | (c, xs') = f xs | ||
268 | f (EOp2_1 _ _ c: xs) = (c, xs) | ||
269 | f (ELet x: (f -> (c, xs))) = (up 0 1 c, ELet x: xs) | ||
270 | |||
271 | focus2 (MSt b xs) = MSt (Op2 op c b) xs' | ||
272 | where | ||
273 | (c, xs') = f xs | ||
274 | f (EOp2_2 _ _ c: xs) = (c, xs) | ||
275 | f (ELet x: (f -> (c, xs))) = (up 0 1 c, ELet x: xs) | ||
276 | |||
261 | where | 277 | where |
262 | rec i = steps i nostep bind cont | 278 | rec i = steps i nostep bind cont |
263 | 279 | ||
@@ -290,6 +306,8 @@ steps lev nostep {-ready-} bind cont dt@(MSt e vs) = case e of | |||
290 | shiftL path n (MSt x (ELet e: es)) = shiftL (TELet: path) (n-1) $ MSt (Let e x) es | 306 | shiftL path n (MSt x (ELet e: es)) = shiftL (TELet: path) (n-1) $ MSt (Let e x) es |
291 | shiftL path n (MSt x (EApp1 i e: es)) = shiftL (TEApp1: path) n $ MSt (HNF i $ App x e) es | 307 | 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 | 308 | shiftL path n (MSt x (ECase i cns e: es)) = shiftL (TECase: path) n $ MSt (HNF i $ Case cns x e) es |
309 | shiftL path n (MSt x (EOp2_1 i op e: es)) = shiftL (TEOp2_1: path) n $ MSt (HNF i $ Op2 op x e) es | ||
310 | shiftL path n (MSt x (EOp2_2 i op e: es)) = shiftL (TEOp2_2: path) n $ MSt (HNF i $ Op2 op e x) es | ||
293 | shiftL path n (MSt x (ELet1 e: es)) = shiftL (TELet1: path) n $ MSt (Let x e) es | 311 | shiftL path n (MSt x (ELet1 e: es)) = shiftL (TELet1: path) n $ MSt (Let x e) es |
294 | shiftL path n st = error $ "shiftL: " ++ show (path, n) ++ "\n" ++ ppShow st | 312 | shiftL path n st = error $ "shiftL: " ++ show (path, n) ++ "\n" ++ ppShow st |
295 | 313 | ||
@@ -303,6 +321,8 @@ steps lev nostep {-ready-} bind cont dt@(MSt e vs) = case e of | |||
303 | shiftR (TELet: n) (y, MSt (Let e x) es) = shiftR n (up 0 1 y, MSt x $ ELet e: es) | 321 | shiftR (TELet: n) (y, MSt (Let e x) es) = shiftR n (up 0 1 y, MSt x $ ELet e: es) |
304 | shiftR (TEApp1: n) (y, MSt (HNF l (App x e)) es) = shiftR n (y, MSt x $ EApp1 l e: es) | 322 | 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) | 323 | shiftR (TECase: n) (y, MSt (HNF l (Case cns x e)) es) = shiftR n (y, MSt x $ ECase l cns e: es) |
324 | shiftR (TEOp2_1: n) (y, MSt (HNF l (Op2 op x e)) es) = shiftR n (y, MSt x $ EOp2_1 l op e: es) | ||
325 | shiftR (TEOp2_2: n) (y, MSt (HNF l (Op2 op e x)) es) = shiftR n (y, MSt x $ EOp2_2 l op e: es) | ||
306 | shiftR (TELet1: n) (y, MSt (Let x e) es) = shiftR n (y, MSt x $ ELet1 e: es) | 326 | shiftR (TELet1: n) (y, MSt (Let x e) es) = shiftR n (y, MSt x $ ELet1 e: es) |
307 | shiftR path x = error $ "shiftR: " ++ show path ++ "\n" ++ ppShow x | 327 | shiftR path x = error $ "shiftR: " ++ show path ++ "\n" ++ ppShow x |
308 | 328 | ||
@@ -313,7 +333,7 @@ steps lev nostep {-ready-} bind cont dt@(MSt e vs) = case e of | |||
313 | 333 | ||
314 | delElem i xs = take i xs ++ drop (i+1) xs | 334 | delElem i xs = take i xs ++ drop (i+1) xs |
315 | 335 | ||
316 | data TE = TELet | TELet1 | TEApp1 | TECase | 336 | data TE = TELet | TELet1 | TEApp1 | TECase | TEOp2_1 | TEOp2_2 |
317 | deriving Show | 337 | deriving Show |
318 | 338 | ||
319 | ---------------------------------------------------------------------------------------- examples | 339 | ---------------------------------------------------------------------------------------- examples |