diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-05-25 09:48:09 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-05-25 09:48:09 +0200 |
commit | 6b4235b3ec5af4b60a89383ecda5d35269f9d9a0 (patch) | |
tree | 3f7d6382f34615d4bd3f74e962f11750fd4ee8ff /prototypes | |
parent | e2d67444c5b4153fc7c588f67d7bf01fd4e675cf (diff) |
refactoring
Diffstat (limited to 'prototypes')
-rw-r--r-- | prototypes/Inspector.hs | 2 | ||||
-rw-r--r-- | prototypes/LamMachine.hs | 141 |
2 files changed, 71 insertions, 72 deletions
diff --git a/prototypes/Inspector.hs b/prototypes/Inspector.hs index b725294d..b6be9562 100644 --- a/prototypes/Inspector.hs +++ b/prototypes/Inspector.hs | |||
@@ -92,7 +92,7 @@ main = do | |||
92 | (LeftArrow, st@(_, _:_:_)) -> cycle' $ iterate goLeft st !! 100 | 92 | (LeftArrow, st@(_, _:_:_)) -> cycle' $ iterate goLeft st !! 100 |
93 | (RightArrow, st@(_:_, _)) -> cycle' $ iterate goRight st !! 100 | 93 | (RightArrow, st@(_:_, _)) -> cycle' $ iterate goRight st !! 100 |
94 | (IntArg n, _) -> cycle' ([], stepList $ t' n) | 94 | (IntArg n, _) -> cycle' ([], stepList $ t' n) |
95 | (ProgramChange, _) -> cycle' ([], stepList $ test) --t'' 0) | 95 | (ProgramChange, _) -> cycle' ([], stepList $ t'' 100) |
96 | _ -> cycle False st | 96 | _ -> cycle False st |
97 | 97 | ||
98 | cycle' st@(h, (_, x): _) = do | 98 | cycle' st@(h, (_, x): _) = do |
diff --git a/prototypes/LamMachine.hs b/prototypes/LamMachine.hs index 2fe663d1..7cf997a5 100644 --- a/prototypes/LamMachine.hs +++ b/prototypes/LamMachine.hs | |||
@@ -218,61 +218,65 @@ steps lev nostep {-ready-} bind cont dt@(MSt e vs) = case e of | |||
218 | f i (x: xs) | simple x = (up 0 lz x, []): f i xs | 218 | f i (x: xs) | simple x = (up 0 lz x, []): f i xs |
219 | | otherwise = (Var' i, [up 0 (lz - i - 1) x]): f (i+1) xs | 219 | | otherwise = (Var' i, [up 0 (lz - i - 1) x]): f (i+1) xs |
220 | 220 | ||
221 | Var i -> lookupHNF_ nostep "var" const i dt | 221 | Var i -> lookupHNF i dt |
222 | 222 | ||
223 | Seq a b -> case a of | 223 | Seq a b -> hnf "seq hnf" focus $ MSt a $ EOp2_1 lev SeqOp b: vs |
224 | Int{} -> step "seq" $ MSt b vs | 224 | where |
225 | Lam{} -> step "seq" $ tryRemoves (fvs a) $ MSt b vs | 225 | focus (MSt a xs) = case a of |
226 | Con{} -> step "seq" $ tryRemoves (fvs a) $ MSt b vs | 226 | Int{} -> step "seq" $ MSt b vs |
227 | Var i -> lookupHNF' "seqvar" (\e (Seq _ b) -> b) i dt | 227 | Lam{} -> step "seq" $ tryRemoves (fvs a) $ MSt b vs |
228 | _ -> step "seqexp" $ MSt (Seq (Var 0) $ up 0 1 b) $ ELet a: vs | 228 | Con{} -> step "seq" $ tryRemoves (fvs a) $ MSt b vs |
229 | _ -> nostep $ MSt (Seq a b) vs | ||
230 | where | ||
231 | (b, vs) = f xs | ||
232 | f (EOp2_1 _ SeqOp c: xs) = (c, xs) | ||
233 | f (ELet x: (f -> (c, xs))) = (up 0 1 c, ELet x: xs) | ||
229 | 234 | ||
230 | -- TODO: handle recursive constants | 235 | -- TODO: handle recursive constants |
231 | Y (Lam x) -> step "Y" $ MSt x $ ELet e: vs | 236 | Y (Lam x) -> step "Y" $ MSt x $ ELet e: vs |
232 | 237 | ||
233 | App a b -> case a of | 238 | App a b -> hnf "app hnf" focus $ MSt a $ EApp1 lev b: vs |
234 | Lam x | usedVar 0 x -> step "app" $ MSt x $ ELet b: vs | ||
235 | | otherwise -> step "appdel" $ tryRemoves (fvs b) $ MSt x vs | ||
236 | _ -> bind "app1" (hnf "app1 hnf" (step "appexp" . focus)) $ MSt a $ EApp1 lev b: vs | ||
237 | where | 239 | where |
238 | focus (MSt b xs) = MSt (App b c) xs' | 240 | focus (MSt a xs) = case a of |
241 | Lam x | usedVar 0 x -> step "app" $ MSt x $ ELet b: vs | ||
242 | | otherwise -> step "appdel" $ tryRemoves (fvs b) $ MSt x vs | ||
243 | _ -> nostep $ MSt (App a b) vs | ||
239 | where | 244 | where |
240 | (c, xs') = f xs | 245 | (b, vs) = f xs |
241 | f (EApp1 _ c: xs) = (c, xs) | 246 | f (EApp1 _ c: xs) = (c, xs) |
242 | f (ELet x: (f -> (c, xs))) = (up 0 1 c, ELet x: xs) | 247 | f (ELet x: (f -> (c, xs))) = (up 0 1 c, ELet x: xs) |
243 | 248 | ||
244 | Case cns a cs -> case a of | 249 | Case cns a cs -> hnf "case hnf" focus $ MSt a $ ECase lev cns cs: vs |
245 | Con cn i es -> step "case" $ tryRemoves (nub $ foldMap fvs $ delElem i cs) $ MSt (foldl App (cs !! i) es) vs | ||
246 | _ -> bind "case1" (hnf "case hnf" (step "caseexp" . focus)) $ MSt a $ ECase lev cns cs: vs | ||
247 | where | 250 | where |
248 | focus (MSt b xs) = MSt (Case cns b c) xs' | 251 | focus (MSt a xs) = case a of |
252 | Con cn i es -> step "case" $ tryRemoves (nub $ foldMap fvs $ delElem i cs) $ MSt (foldl App (cs !! i) es) vs | ||
253 | _ -> nostep $ MSt (Case cns a cs) vs | ||
249 | where | 254 | where |
250 | (c, xs') = f xs | 255 | ((cns, cs), vs) = f xs |
251 | f (ECase _ _ c: xs) = (c, xs) | 256 | f (ECase _ cns cs: xs) = ((cns, cs), xs) |
252 | f (ELet x: (f -> (c, xs))) = (up 0 1 <$> c, ELet x: xs) | 257 | f (ELet x: (f -> (c, xs))) = (second (up 0 1 <$>) c, ELet x: xs) |
253 | 258 | ||
254 | Op2 op x y -> case (x, y) of | 259 | Op2 op x y -> hnf "op2_1 hnf" focus1 $ MSt x $ EOp2_1 lev op y: vs |
255 | (Int e, Int f) -> step "op-done" $ MSt (int op e f) vs | ||
256 | where | ||
257 | int Add a b = Int $ a + b | ||
258 | int Sub a b = Int $ a - b | ||
259 | int Mod a b = Int $ a `mod` b | ||
260 | int LessEq a b = if a <= b then T else F | ||
261 | int EqInt a b = if a == b then T else F | ||
262 | (Int{}, _) -> bind "op2_2 ready" (hnf "op2_2 hnf" (step "op2_2" . focus2)) $ MSt y $ EOp2_2 lev op x: vs | ||
263 | _ -> bind "op2_1 ready" (hnf "op2_1 hnf" (step "op2_1" . focus1)) $ MSt x $ EOp2_1 lev op y: vs | ||
264 | where | 260 | where |
265 | focus1 (MSt b xs) = MSt (Op2 op b c) xs' | 261 | focus1 (MSt x xs) = hnf "op2_2 hnf" focus2 $ MSt y $ EOp2_2 lev op x: vs |
266 | where | 262 | where |
267 | (c, xs') = f xs | 263 | ((op, y), vs) = f xs |
268 | f (EOp2_1 _ _ c: xs) = (c, xs) | 264 | f (EOp2_1 _ op y: xs) = ((op, y), xs) |
269 | f (ELet x: (f -> (c, xs))) = (up 0 1 c, ELet x: xs) | 265 | f (ELet x: (f -> (c, xs))) = (second (up 0 1) c, ELet x: xs) |
270 | 266 | ||
271 | focus2 (MSt b xs) = MSt (Op2 op c b) xs' | 267 | focus2 (MSt y xs) = case (x, y) of |
268 | (Int e, Int f) -> step "op-done" $ MSt (int op e f) vs | ||
269 | where | ||
270 | int Add a b = Int $ a + b | ||
271 | int Sub a b = Int $ a - b | ||
272 | int Mod a b = Int $ a `mod` b | ||
273 | int LessEq a b = if a <= b then T else F | ||
274 | int EqInt a b = if a == b then T else F | ||
275 | _ -> nostep $ MSt (Op2 op x y) vs | ||
272 | where | 276 | where |
273 | (c, xs') = f xs | 277 | ((op, x), vs) = f xs |
274 | f (EOp2_2 _ _ c: xs) = (c, xs) | 278 | f (EOp2_2 _ op x: xs) = ((op, x), xs) |
275 | f (ELet x: (f -> (c, xs))) = (up 0 1 c, ELet x: xs) | 279 | f (ELet x: (f -> (c, xs))) = (second (up 0 1) c, ELet x: xs) |
276 | 280 | ||
277 | where | 281 | where |
278 | rec i = steps i nostep bind cont | 282 | rec i = steps i nostep bind cont |
@@ -286,45 +290,40 @@ steps lev nostep {-ready-} bind cont dt@(MSt e vs) = case e of | |||
286 | hnfTag (MSt b c) = MSt (HNF lev b) c | 290 | hnfTag (MSt b c) = MSt (HNF lev b) c |
287 | 291 | ||
288 | -- lookup var in head normal form | 292 | -- lookup var in head normal form |
289 | lookupHNF_ :: (MSt -> e) -> StepTag -> (Exp -> Exp -> Exp) -> Nat -> MSt -> e | 293 | lookupHNF :: Nat -> MSt -> e |
290 | lookupHNF_ end msg f i@(Nat i') dt = bind msg (hnf "lookup" shiftLookup) dt' | 294 | lookupHNF i@(Nat i') dt = hnf "var lookup" shiftLookup dt' |
291 | where | 295 | where |
292 | (path, dt') = shiftL [] i' $ hnfTag dt | 296 | (path, dt') = shiftL [] i' $ hnfTag dt |
293 | 297 | ||
294 | shiftLookup st | 298 | shiftLookup st |
295 | = case boot (shiftR path . pakol') st of | 299 | = case boot (shiftR path . pakol') st of |
296 | (q, MSt (HNF lev z) es) -> bind "shiftR" (tryRemove i) $ MSt (f (up 0 1 q) z) es | 300 | (q, MSt HNF{} es) -> bind "remove" nostep $ tryRemoves [i] $ MSt (up 0 1 q) es |
297 | st -> error $ "sl: " ++ ppShow st | 301 | st -> error $ "sl: " ++ ppShow st |
298 | 302 | ||
299 | tryRemove i st = {-maybe (end st)-} (bind "remove" end) $ tryRemoves [i] st | 303 | boot c (MSt e (ELet x: xs)) = boot (c . pakol) (MSt (Let x e) xs) |
300 | 304 | boot c st = c st | |
301 | -- lookup & step | 305 | |
302 | lookupHNF' :: StepTag -> (Exp -> Exp -> Exp) -> Nat -> MSt -> e | 306 | pakol (MSt (Let x e) (ELet1 y: xs)) = MSt e (ELet1 (up 1 1 y): ELet x: xs) |
303 | lookupHNF' msg f i dt = lookupHNF_ (rec lev) msg f i dt | 307 | |
304 | 308 | pakol' (MSt x (ELet1 y: xs)) = (x, MSt y (ELet x: xs)) | |
305 | shiftL path 0 (MSt x (ELet e: es)) = (path, MSt e $ ELet1 x: es) | 309 | |
306 | shiftL path n (MSt x (ELet e: es)) = shiftL (TELet: path) (n-1) $ MSt (Let e x) es | 310 | shiftL path 0 (MSt x (ELet e: es)) = (path, MSt e $ ELet1 x: es) |
307 | shiftL path n (MSt x (EApp1 i e: es)) = shiftL (TEApp1: path) n $ MSt (HNF i $ App x e) es | 311 | shiftL path n (MSt x (ELet e: es)) = shiftL (TELet: path) (n-1) $ MSt (Let e x) es |
308 | shiftL path n (MSt x (ECase i cns e: es)) = shiftL (TECase: path) n $ MSt (HNF i $ Case cns x e) es | 312 | shiftL path n (MSt x (EApp1 i e: es)) = shiftL (TEApp1: path) n $ MSt (HNF i $ App 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 | 313 | shiftL path n (MSt x (ECase i cns e: es)) = shiftL (TECase: path) n $ MSt (HNF i $ Case cns 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 | 314 | shiftL path n (MSt x (EOp2_1 i op e: es)) = shiftL (TEOp2_1: path) n $ MSt (HNF i $ Op2 op x e) es |
311 | shiftL path n (MSt x (ELet1 e: es)) = shiftL (TELet1: path) n $ MSt (Let x e) es | 315 | shiftL path n (MSt x (EOp2_2 i op e: es)) = shiftL (TEOp2_2: path) n $ MSt (HNF i $ Op2 op e x) es |
312 | shiftL path n st = error $ "shiftL: " ++ show (path, n) ++ "\n" ++ ppShow st | 316 | shiftL path n (MSt x (ELet1 e: es)) = shiftL (TELet1: path) n $ MSt (Let x e) es |
313 | 317 | shiftL path n st = error $ "shiftL: " ++ show (path, n) ++ "\n" ++ ppShow st | |
314 | boot c (MSt e (ELet x: xs)) = boot (c . pakol) (MSt (Let x e) xs) | 318 | |
315 | boot c st = c st | 319 | shiftR [] st = st |
316 | 320 | shiftR (TELet: n) (y, MSt (Let e x) es) = shiftR n (up 0 1 y, MSt x $ ELet e: es) | |
317 | pakol (MSt (Let x e) (ELet1 y: xs)) = MSt e (ELet1 (up 1 1 y): ELet x: xs) | 321 | shiftR (TEApp1: n) (y, MSt (HNF l (App x e)) es) = shiftR n (y, MSt x $ EApp1 l e: es) |
318 | pakol' (MSt x (ELet1 y: xs)) = (x, MSt y (ELet x: xs)) | 322 | shiftR (TECase: n) (y, MSt (HNF l (Case cns x e)) es) = shiftR n (y, MSt x $ ECase l cns e: es) |
319 | 323 | shiftR (TEOp2_1: n) (y, MSt (HNF l (Op2 op x e)) es) = shiftR n (y, MSt x $ EOp2_1 l op e: es) | |
320 | shiftR [] st = st | 324 | shiftR (TEOp2_2: n) (y, MSt (HNF l (Op2 op e x)) es) = shiftR n (y, MSt x $ EOp2_2 l op e: es) |
321 | shiftR (TELet: n) (y, MSt (Let e x) es) = shiftR n (up 0 1 y, MSt x $ ELet e: es) | 325 | shiftR (TELet1: n) (y, MSt (Let x e) es) = shiftR n (y, MSt x $ ELet1 e: es) |
322 | shiftR (TEApp1: n) (y, MSt (HNF l (App x e)) es) = shiftR n (y, MSt x $ EApp1 l e: es) | 326 | shiftR path x = error $ "shiftR: " ++ show path ++ "\n" ++ ppShow x |
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) | ||
326 | shiftR (TELet1: n) (y, MSt (Let x e) es) = shiftR n (y, MSt x $ ELet1 e: es) | ||
327 | shiftR path x = error $ "shiftR: " ++ show path ++ "\n" ++ ppShow x | ||
328 | 327 | ||
329 | simple = \case | 328 | simple = \case |
330 | Var{} -> True | 329 | Var{} -> True |