diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-06-03 11:24:33 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-06-03 11:24:33 +0200 |
commit | 19dc22401cc599cd6d76e2710ad40cec9fbb3cb8 (patch) | |
tree | 263c0cb96973cee7caafddec2c96b29c46e2bdbb /prototypes | |
parent | d68e8b537283ac97c9c0c6fda14e9de2fe36f980 (diff) |
cleanup-refactoring
Diffstat (limited to 'prototypes')
-rw-r--r-- | prototypes/LamMachine.hs | 131 |
1 files changed, 48 insertions, 83 deletions
diff --git a/prototypes/LamMachine.hs b/prototypes/LamMachine.hs index efb0ffda..6fbd3f73 100644 --- a/prototypes/LamMachine.hs +++ b/prototypes/LamMachine.hs | |||
@@ -88,7 +88,6 @@ pattern DExpCons a b <- (getDExpCons -> (a, b)) | |||
88 | where DExpCons (EnvPiece ux a) DExpNil = DExpCons_ (fromSFV s) (EnvPiece ux' a) DExpNil | 88 | where DExpCons (EnvPiece ux a) DExpNil = DExpCons_ (fromSFV s) (EnvPiece ux' a) DExpNil |
89 | where (s, D1 ux') = diffT $ D1 ux | 89 | where (s, D1 ux') = diffT $ D1 ux |
90 | DExpCons (ELet a) (dDown 0 -> Just d) = d | 90 | DExpCons (ELet a) (dDown 0 -> Just d) = d |
91 | -- DExpCons (EDLet1 (dDown 0 -> Just a)) x = error "? ?" | ||
92 | DExpCons (EnvPiece ux a) (DExpCons_ u x y) = DExpCons_ (fromSFV s) (EnvPiece ux' a) (DExpCons_ u' x y) | 91 | DExpCons (EnvPiece ux a) (DExpCons_ u x y) = DExpCons_ (fromSFV s) (EnvPiece ux' a) (DExpCons_ u' x y) |
93 | where (s, D2 (SFV 0 u') ux') = diffT $ D2 (SFV 0 u) ux | 92 | where (s, D2 (SFV 0 u') ux') = diffT $ D2 (SFV 0 u) ux |
94 | 93 | ||
@@ -100,7 +99,7 @@ upP i j = uppEP $ mkUp i j | |||
100 | 99 | ||
101 | incFV' (SFV n u) = SFV (n + 1) $ incFV u | 100 | incFV' (SFV n u) = SFV (n + 1) $ incFV u |
102 | 101 | ||
103 | --uppDE :: FV -> DExp -> DExp | 102 | uppDE :: FV -> Nat -> DExp -> DExp |
104 | uppDE a _ DExpNil = DExpNil | 103 | uppDE a _ DExpNil = DExpNil |
105 | uppDE a n (DExpCons_ u x y) = DExpCons_ (onTail (expand a) n u) x y | 104 | uppDE a n (DExpCons_ u x y) = DExpCons_ (onTail (expand a) n u) x y |
106 | 105 | ||
@@ -115,26 +114,9 @@ data MSt = MSt DEnv Exp | |||
115 | 114 | ||
116 | infixr 4 `DEnvCons`, `DExpCons` | 115 | infixr 4 `DEnvCons`, `DExpCons` |
117 | 116 | ||
118 | mSt es e = MSt ({-gc u-} es) e | ||
119 | |||
120 | gc :: FV -> DEnv -> DEnv | ||
121 | gc _ DEnvNil = DEnvNil | ||
122 | gc u (e@(ELet x) `DEnvCons` es) | ||
123 | | sIndex u 0 = e `DEnvCons` gc (sDrop 1 u) es | ||
124 | | otherwise = ELet (Int 111) `DEnvCons` gc (sDrop 1 u) es | ||
125 | --gc u (EDLet1 `DEnvCons` es) = e `DEnvCons` gc u es | ||
126 | gc u (e `DEnvCons` es) = e `DEnvCons` gc u es | ||
127 | |||
128 | dDown i DExpNil = Just DExpNil | 117 | dDown i DExpNil = Just DExpNil |
129 | dDown i (DExpCons_ u a b) = DExpCons_ <$> downFV i u <*> pure a <*> pure b | 118 | dDown i (DExpCons_ u a b) = DExpCons_ <$> downFV i u <*> pure a <*> pure b |
130 | 119 | ||
131 | gc' :: Exp -> Exp | ||
132 | gc' (Let a b) = mkLet a $ gc' b | ||
133 | where | ||
134 | mkLet i (down 0 -> Just x) = x | ||
135 | mkLet i x = Let i x | ||
136 | gc' x = x | ||
137 | |||
138 | --------------------------------------------------------------------- toolbox: pretty print | 120 | --------------------------------------------------------------------- toolbox: pretty print |
139 | 121 | ||
140 | class ViewShow a where | 122 | class ViewShow a where |
@@ -231,7 +213,7 @@ pattern Case a b c <- Exp_ u (Case_ a (upp u -> b) (map (upp u) -> c)) | |||
231 | where Case cn a b = Exp_ s $ Case_ cn az bz where (s, az: bz) = deltas $ a: b | 213 | where Case cn a b = Exp_ s $ Case_ cn az bz where (s, az: bz) = deltas $ a: b |
232 | 214 | ||
233 | pattern Let i x <- App (Lam x) i | 215 | pattern Let i x <- App (Lam x) i |
234 | where --Let i (down 0 -> Just x) = x -- exception with pakol | 216 | where Let i (down 0 -> Just x) = x |
235 | Let i x = App (Lam x) i | 217 | Let i x = App (Lam x) i |
236 | pattern Y a = Op1 YOp a | 218 | pattern Y a = Op1 YOp a |
237 | pattern InHNF a = Op1 HNF_ a | 219 | pattern InHNF a = Op1 HNF_ a |
@@ -303,17 +285,14 @@ focusNotUsed (MSt (EDLet1 x `DEnvCons` _) _) = not $ usedVar' 0 x | |||
303 | focusNotUsed _ = True | 285 | focusNotUsed _ = True |
304 | 286 | ||
305 | steps :: forall e . Int -> GenSteps e | 287 | steps :: forall e . Int -> GenSteps e |
306 | steps lev nostep bind cont st@(MSt DEnvNil (InHNF e)) = nostep st | 288 | steps lev nostep bind cont (MSt vs e) = case e of |
307 | steps lev nostep bind cont dt@(MSt vs e) = case e of | ||
308 | |||
309 | InHNF{} -> goUp dt | ||
310 | 289 | ||
311 | Int{} -> ready "int" dt | 290 | Int{} -> step "int hnf" $ MSt vs $ InHNF e |
312 | Lam{} -> ready "lam" dt | 291 | Lam{} -> step "lam hnf" $ MSt vs $ InHNF e |
313 | 292 | ||
314 | Con cn i xs | 293 | Con cn i xs |
315 | | lz == 0 || focusNotUsed dt -> ready "con" dt | 294 | | lz == 0 || focusNotUsed (MSt vs e) -> step "con hnf" $ MSt vs $ InHNF e |
316 | | otherwise -> ready "copy con" $ MSt (foldr DEnvCons vs $ ELet <$> zs) $ Con cn i xs' -- share complex constructor arguments | 295 | | otherwise -> step "copy con" $ MSt (foldr DEnvCons vs $ ELet <$> zs) $ InHNF $ Con cn i xs' -- share complex constructor arguments |
317 | where | 296 | where |
318 | lz = Nat $ length zs | 297 | lz = Nat $ length zs |
319 | (xs', concat -> zs) = unzip $ f 0 xs | 298 | (xs', concat -> zs) = unzip $ f 0 xs |
@@ -322,80 +301,66 @@ steps lev nostep bind cont dt@(MSt vs e) = case e of | |||
322 | | otherwise = (Var' i, [up 0 (lz - i - 1) x]): f (i+1) xs | 301 | | otherwise = (Var' i, [up 0 (lz - i - 1) x]): f (i+1) xs |
323 | 302 | ||
324 | -- TODO: handle recursive constants | 303 | -- TODO: handle recursive constants |
325 | Y x -> step "Y" $ MSt vs $ App x (Y x) | 304 | Y x -> step "Y" $ MSt vs $ App x (Y x) |
326 | -- Y (Lam x) -> step "Y" $ MSt (ELet e `DEnvCons` vs) x | ||
327 | 305 | ||
328 | Var i -> step "var" $ zipDown i DExpNil vs | 306 | Var i -> step "var" $ zipDown i DExpNil vs |
329 | Seq a b -> step "seq" $ MSt (EOp2_1 SeqOp b `DEnvCons` vs) a | 307 | Seq a b -> step "seq" $ MSt (EOp2_1 SeqOp b `DEnvCons` vs) a |
330 | Case cns a cs -> step "case" $ MSt (ECase cns cs `DEnvCons` vs) a | 308 | Case cns a cs -> step "case" $ MSt (ECase cns cs `DEnvCons` vs) a |
331 | Op2 op a b -> step "op2_1" $ MSt (EOp2_1 op b `DEnvCons` vs) a | 309 | Op2 op a b -> step "op2_1" $ MSt (EOp2_1 op b `DEnvCons` vs) a |
332 | 310 | ||
333 | where | 311 | InHNF a -> case vs of |
334 | bind' = bind -- _ f x = f x | 312 | |
335 | 313 | DEnvNil -> bind "whnf" nostep $ MSt DEnvNil $ InHNF a | |
336 | ready :: StepTag -> MSt -> e | 314 | |
337 | ready t (MSt vs e) = step (t ++ " ready") $ MSt vs $ inHNF e | 315 | ELet x `DEnvCons` vs -> step "goUp" $ MSt vs $ inHNF $ Let x $ InHNF a |
338 | 316 | x `DEnvCons` vs | Let y e <- a -> step "pakol" $ MSt (upP 0 1 x `DEnvCons` ELet y `DEnvCons` vs) e | |
339 | rec i = steps i nostep bind cont | 317 | x `DEnvCons` vs -> case x of |
340 | 318 | EOp2_1 SeqOp b -> case a of | |
341 | step :: StepTag -> MSt -> e | 319 | Int{} -> step "seq" $ MSt vs b |
342 | step t = bind t (rec lev) | 320 | Lam{} -> step "seq" $ MSt vs b -- TODO: remove a |
321 | Con{} -> step "seq" $ MSt vs b -- TODO: remove a | ||
322 | _ -> step "seq hnf" $ MSt vs $ InHNF $ Seq (InHNF a) b | ||
323 | EOp2_1 AppOp b -> case a of | ||
324 | Lam x | usedVar 0 x -> step "app" $ MSt (ELet b `DEnvCons` vs) x | ||
325 | | otherwise -> step "appdel" $ MSt vs x -- TODO: remove b | ||
326 | _ -> step "app hnf" $ MSt vs $ InHNF $ App (InHNF a) b | ||
327 | ECase cns cs -> case a of | ||
328 | Con cn i es -> step "case" $ MSt vs $ foldl App (cs !! i) es -- TODO: remove unused cases | ||
329 | _ -> step "case hnf" $ MSt vs $ InHNF $ Case cns (InHNF a) cs | ||
330 | EOp2_1 op b -> step "op2_2 hnf" $ MSt (EOp2_2 op (InHNF a) `DEnvCons` vs) b | ||
331 | EOp2_2 op b -> case (b, a) of | ||
332 | (InHNF (Int e), Int f) -> step "op-done" $ MSt vs (int op e f) | ||
333 | where | ||
334 | int Add a b = Int $ a + b | ||
335 | int Sub a b = Int $ a - b | ||
336 | int Mod a b = Int $ a `mod` b | ||
337 | int LessEq a b = if a <= b then T else F | ||
338 | int EqInt a b = if a == b then T else F | ||
339 | _ -> step "op2 hnf" $ MSt vs $ InHNF $ Op2 op b (InHNF a) | ||
340 | EDLet1 (dDown 0 -> Just d) -> zipUp a vs d | ||
341 | EDLet1 d -> zipUp (up 0 1 a) (ELet (InHNF a) `DEnvCons` vs) d | ||
343 | 342 | ||
343 | where | ||
344 | zipDown 0 e (ELet z `DEnvCons` zs) = MSt (EDLet1 e `DEnvCons` zs) z | 344 | zipDown 0 e (ELet z `DEnvCons` zs) = MSt (EDLet1 e `DEnvCons` zs) z |
345 | zipDown j e (z@ELet{} `DEnvCons` zs) = zipDown (j-1) (z `DExpCons` e) zs | 345 | zipDown j e (z@ELet{} `DEnvCons` zs) = zipDown (j-1) (z `DExpCons` e) zs |
346 | zipDown j e (z `DEnvCons` zs) = zipDown j (z `DExpCons` e) zs | 346 | zipDown j e (z `DEnvCons` zs) = zipDown j (z `DExpCons` e) zs |
347 | 347 | ||
348 | goUp = boot "." 0 | 348 | zipUp x xs DExpNil = step "zipUp ready" $ MSt xs x |
349 | |||
350 | boot t n (MSt (ELet x `DEnvCons` xs) e) = boot t (n+1) (MSt xs (Let x e)) | ||
351 | boot t n st@(MSt DEnvNil e) = ready "whnf" $ MSt DEnvNil $ gc' e -- $ iterate pakol' st !! n | ||
352 | boot t n st = bind' "boot half" (pakol n) st | ||
353 | |||
354 | pakol 0 st = bind' "boot end" focus st | ||
355 | pakol n (MSt (y `DEnvCons` xs) (Let x e)) = bind' "pakol" (pakol (n-1)) $ MSt (upP 0 1 y `DEnvCons` ELet x `DEnvCons` xs) e | ||
356 | pakol n st = error $ "pakol: " ++ show st | ||
357 | |||
358 | pakol' (MSt xs (Let x e)) = MSt (ELet x `DEnvCons` xs) e | ||
359 | |||
360 | focus st@(MSt DEnvNil (InHNF a)) = ready "end" st -- TODO: a? | ||
361 | focus st@(MSt (DEnvCons x vs) (InHNF a)) = case x of | ||
362 | -- ELet{} -> focus $ MSt ( `DEnvCons` vs) e | ||
363 | EOp2_1 SeqOp b -> case a of | ||
364 | Int{} -> step "seq" $ MSt vs b | ||
365 | Lam{} -> step "seq" $ MSt vs b -- remove a! | ||
366 | Con{} -> step "seq" $ MSt vs b -- remove a! | ||
367 | _ -> ready "seq" $ MSt vs (Seq (InHNF a) b) | ||
368 | EOp2_1 AppOp b -> case a of | ||
369 | Lam x | usedVar 0 x -> step "app" $ MSt (ELet b `DEnvCons` vs) x | ||
370 | | otherwise -> step "appdel" $ MSt vs x -- remove b! | ||
371 | _ -> ready "app" $ MSt vs (App (InHNF a) b) | ||
372 | ECase cns cs -> case a of | ||
373 | Con cn i es -> step "case" $ MSt vs (foldl App (cs !! i) es) -- remove unused cases! | ||
374 | _ -> ready "case" $ MSt vs (Case cns (InHNF a) cs) | ||
375 | EOp2_1 op b -> step "op2_2 hnf" $ MSt (EOp2_2 op (InHNF a) `DEnvCons` vs) b | ||
376 | EOp2_2 op b -> case (b, a) of | ||
377 | (InHNF (Int e), Int f) -> step "op-done" $ MSt vs (int op e f) | ||
378 | where | ||
379 | int Add a b = Int $ a + b | ||
380 | int Sub a b = Int $ a - b | ||
381 | int Mod a b = Int $ a `mod` b | ||
382 | int LessEq a b = if a <= b then T else F | ||
383 | int EqInt a b = if a == b then T else F | ||
384 | _ -> ready "op2" $ MSt vs (Op2 op b (InHNF a)) | ||
385 | EDLet1 d | Just d' <- dDown 0 d -> zipUp a vs d' | ||
386 | EDLet1 d -> zipUp (up 0 1 a) (ELet (InHNF a) `DEnvCons` vs) d | ||
387 | |||
388 | zipUp x xs DExpNil = ready "lookup" $ mSt xs x | ||
389 | zipUp x xs (DExpCons a@ELet{} as) = zipUp (up 0 1 x) (a `DEnvCons` xs) as | 349 | zipUp x xs (DExpCons a@ELet{} as) = zipUp (up 0 1 x) (a `DEnvCons` xs) as |
390 | zipUp x xs (DExpCons a as) = zipUp x (a `DEnvCons` xs) as | 350 | zipUp x xs (DExpCons a as) = zipUp x (a `DEnvCons` xs) as |
351 | |||
352 | rec i = steps i nostep bind cont | ||
353 | |||
354 | step :: StepTag -> MSt -> e | ||
355 | step t = bind t (rec lev) | ||
391 | {- | 356 | {- |
392 | hnf :: StepTag -> (MSt -> e) -> MSt -> e | 357 | hnf :: StepTag -> (MSt -> e) -> MSt -> e |
393 | hnf t f = bind ("hnf " ++ t) $ cont t f (rec (1 + lev)) | 358 | hnf t f = bind ("hnf " ++ t) $ cont t f (rec (1 + lev)) |
394 | 359 | ||
395 | hnf' :: StepTag -> MSt -> e | 360 | hnf' :: StepTag -> MSt -> e |
396 | hnf' t = hnf t $ bind ("focus " ++ t) $ boot t 0 | 361 | hnf' t = hnf t $ bind ("focus " ++ t) $ goUp t 0 |
397 | |||
398 | -} | 362 | -} |
363 | |||
399 | simple = \case | 364 | simple = \case |
400 | Var{} -> True | 365 | Var{} -> True |
401 | Int{} -> True | 366 | Int{} -> True |