summaryrefslogtreecommitdiff
path: root/prototypes
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-05-16 14:19:07 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-05-16 14:19:07 +0200
commitb3308f8ee9c4fd26ef0306d25c3c83d915ec4786 (patch)
treecc7a62f1a10fa6b82fe5d8a4b36e2db26b2b3497 /prototypes
parentc3bf9728ba74fbb363dcd00941419602568d645b (diff)
refactoring
Diffstat (limited to 'prototypes')
-rw-r--r--prototypes/LamMachine.hs208
1 files changed, 85 insertions, 123 deletions
diff --git a/prototypes/LamMachine.hs b/prototypes/LamMachine.hs
index b869285e..5dce0829 100644
--- a/prototypes/LamMachine.hs
+++ b/prototypes/LamMachine.hs
@@ -27,7 +27,6 @@ data Exp_
27 | Y_ Exp 27 | Y_ Exp
28 | Int_ Int 28 | Int_ Int
29 | Lam_ Exp 29 | Lam_ Exp
30 | Let_ Exp Exp
31 30
32 | App_ Exp Exp 31 | App_ Exp Exp
33 | Seq_ Exp Exp 32 | Seq_ Exp Exp
@@ -46,79 +45,108 @@ data Op2 = Add | Sub | Mod | LessEq | EqInt
46data Exp = Exp {dbUps :: [Up], maxFreeVars :: Int, expexp :: Exp_ } 45data Exp = Exp {dbUps :: [Up], maxFreeVars :: Int, expexp :: Exp_ }
47 46
48-- state of the machine 47-- state of the machine
49data MSt = MSt [(FreeVars, Exp)] -- TODO: use finger tree instead of list 48data MSt = MSt Exp -- TODO: use finger tree instead of lets?
50 Exp 49 Exp
51 [Exp] -- TODO: use finger tree instead of list 50 [Exp] -- TODO: use finger tree instead of list?
52 51
53--------------------------------------------------------------------- toolbox: pretty print 52--------------------------------------------------------------------- toolbox: pretty print
54 53
55instance PShow Exp where 54instance PShow Exp where
56 pShow e@(Exp u t x) = case e of -- shUps' u t $ case Exp [] t x of 55 pShow e@(Exp u t x) = case e of -- shUps' u t $ case Exp [] t x of
57 Var i -> DVar i 56 Var i -> DVar i
58 App a b -> DApp (pShow a) (pShow b) 57 Let a b -> shLet (pShow a) $ pShow b
59 Seq a b -> DOp "`seq`" (Infix 1) (pShow a) (pShow b) 58 App a b -> DApp (pShow a) (pShow b)
60 Lam e -> shLam_ True $ pShow e 59 Seq a b -> DOp "`seq`" (Infix 1) (pShow a) (pShow b)
61 Let a b -> shLet_ (pShow a) $ pShow b 60 Lam e -> shLam $ pShow e
62 Con s i xs -> foldl DApp (text s) $ pShow <$> xs 61 Con s i xs -> foldl DApp (text s) $ pShow <$> xs
63 Int i -> pShow i 62 Int i -> pShow i
64 Op1 o x -> text (show o) `DApp` pShow x 63 Op1 o x -> text (show o) `DApp` pShow x
65 Op2 EqInt x y -> DOp "==" (Infix 4) (pShow x) (pShow y) 64 Op2 EqInt x y -> DOp "==" (Infix 4) (pShow x) (pShow y)
66 Op2 Add x y -> DOp "+" (InfixL 6) (pShow x) (pShow y) 65 Op2 Add x y -> DOp "+" (InfixL 6) (pShow x) (pShow y)
67 Op2 o x y -> text (show o) `DApp` pShow x `DApp` pShow y 66 Op2 o x y -> text (show o) `DApp` pShow x `DApp` pShow y
68 Y e -> "Y" `DApp` pShow e 67 Y e -> "Y" `DApp` pShow e
69 Case e xs -> DPreOp (-20) (ComplexAtom "case" (-10) (pShow e) (SimpleAtom "of")) $ foldr1 DSemi [DArr_ "->" (text a) (pShow b) | (a, b) <- xs] 68 Case e xs -> DPreOp (-20) (ComplexAtom "case" (-10) (pShow e) (SimpleAtom "of"))
69 $ foldr1 DSemi [DArr_ "->" (text a) (pShow b) | (a, b) <- xs]
70
71instance PShow MSt where
72 pShow (MSt as b bs) = case foldl (flip (:)) (DBrace (pShow b): [pShow x | x <- bs]) $ [pShow as] of
73 x: xs -> foldl (flip shLet) x xs
70 74
71shUps a b = DPreOp (-20) (SimpleAtom $ show a) b 75shUps a b = DPreOp (-20) (SimpleAtom $ show a) b
72shUps' a x b = DPreOp (-20) (SimpleAtom $ show a ++ show x) b 76shUps' a x b = DPreOp (-20) (SimpleAtom $ show a ++ show x) b
73 77
74shLam_ usedVar b = DFreshName usedVar $ showLam (DVar 0) b 78shLam b = DFreshName True $ showLam (DVar 0) b
75 79
76showLam x (DFreshName u d) = DFreshName u $ showLam (DUp 0 x) d 80showLam x (DFreshName u d) = DFreshName u $ showLam (DUp 0 x) d
77showLam x (DLam xs y) = DLam (DSep (InfixR 11) x xs) y 81showLam x (DLam xs y) = DLam (DSep (InfixR 11) x xs) y
78showLam x y = DLam x y 82showLam x y = DLam x y
79 83
80shLet i a b = DLet' (DLet "=" (blue $ shVar i) $ DUp i a) (DUp i b) 84shLet a b = DFreshName True $ showLet (DLet "=" (shVar 0) $ DUp 0 a) b
81 85
82showLet x (DFreshName u d) = DFreshName u $ showLet (DUp 0 x) d 86showLet x (DFreshName u d) = DFreshName u $ showLet (DUp 0 x) d
83showLet x (DLet' xs y) = DLet' (DSemi x xs) y 87showLet x (DLet' xs y) = DLet' (DSemi x xs) y
84showLet x y = DLet' x y 88showLet x y = DLet' x y
85 89
86shLet_ a b = DFreshName True $ showLet (DLet "=" (shVar 0) $ DUp 0 a) b
87
88instance PShow MSt where
89 pShow (MSt as b bs) = case foldl (flip (:)) (DBrace (pShow b): [pShow x | x <- bs]) [pShow x | EPP x <- as] of
90 x: xs -> foldl (flip shLet_) x xs
91
92 90
93--------------------------------------------------------------------- toolbox: free variables 91--------------------------------------------------------------------- toolbox: free variables
94 92
95maxFreeVars' (Exp u k _) = upsFreeVars' u k 93maxFreeVars' (Exp xs s _) = foldl' f s xs
96
97upsFreeVars xs s = foldr rearr s xs
98
99upsFreeVars' xs s = foldr rearr' s xs
100 where 94 where
101 rearr' (Up l n) m 95 f m (Up l n) = n + m
102 | l >= m = m -- TODO: remove
103 | otherwise = n + m
104 96
105upVarIndex xs s = foldr rearr' s xs 97upVarIndex xs s = foldr f s xs
106 where 98 where
107 rearr' (Up l n) i 99 f (Up l n) i
108 | l > i = i 100 | l > i = i
109 | otherwise = n + i 101 | otherwise = n + i
110 102
103upp u x = foldr up x u
104
105up' l n = up $ Up l n
106
107up :: Up -> Exp -> Exp
108up (Up i 0) e = e
109up u@(Up i num) e@(Exp us s x) = Exp (insertUp_ s u us) s x
110
111insertUp_ s u@(Up i _) []
112 | i >= s = []
113 | otherwise = [u]
114insertUp_ s u@(Up l n) us_@(u'@(Up l' n'): us)
115 | l < l' = u: us_
116 | l >= l' && l <= l' + n' = Up l' (n' + n): us
117 | otherwise = u': insertUp_ s (Up (l-n') n) us
111 118
112-- TODO: remove 119-- TODO: remove
113instance HasFreeVars Exp where 120insertUp (Up l 0) us = us
114 getFreeVars (Exp us fv e) = upsFreeVars us $ FreeVars $ 2^fv - 1 121insertUp u [] = [u]
122insertUp u@(Up l n) us_@(u'@(Up l' n'): us)
123 | l < l' = u: us_
124 | l >= l' && l <= l' + n' = Up l' (n' + n): us
125 | otherwise = u': insertUp (Up (l-n') n) us
115 126
127-- TODO: remove if possible
116fvs (Exp us fv _) = gen 0 $ foldr f [fv] us where 128fvs (Exp us fv _) = gen 0 $ foldr f [fv] us where
117 f (Up l n) xs = l: l+n: map (+n) xs 129 f (Up l n) xs = l: l+n: map (+n) xs
118 gen i (a: xs) = [i..a-1] ++ gen' xs 130 gen i (a: xs) = [i..a-1] ++ gen' xs
119 gen' [] = [] 131 gen' [] = []
120 gen' (a: xs) = gen a xs 132 gen' (a: xs) = gen a xs
121 133
134usedVar' i (Exp us fv _) = f i us where
135 f i [] = i < fv
136 f i (Up l n: us)
137 | l > i = f i us
138 | i < l + n = False
139 | otherwise = f (n + i) us
140
141down i0 e0@(Exp us fv e) = f i0 us where
142 f i []
143 | i < fv = Nothing
144 | otherwise = Just $ Exp [] fv e
145 f i (u@(Up j n): us)
146 | i < j = up' (j-1) n <$> f i us
147 | i >= j + n = up u <$> f (i-n) us
148 | otherwise = Just $ up' j (n-1) $ Exp us fv e
149
122upss u (Exp _ i e) = Exp u i e 150upss u (Exp _ i e) = Exp u i e
123 151
124dup2 f ax bx = Exp s (maxFreeVars' az `max` maxFreeVars' bz) $ f az bz where 152dup2 f ax bx = Exp s (maxFreeVars' az `max` maxFreeVars' bz) $ f az bz where
@@ -149,11 +177,6 @@ dupLam f e@(Exp a fv ax) = Exp (ff a) (max 0 $ fv - 1) $ f $ Exp (gg a) fv ax
149 ff (Up 0 n: us) = insertUp (Up 0 $ n - 1) $ incUp (-1) <$> us 177 ff (Up 0 n: us) = insertUp (Up 0 $ n - 1) $ incUp (-1) <$> us
150 ff us = incUp (-1) <$> us 178 ff us = incUp (-1) <$> us
151 179
152-- (\x -> e) f x = f in e
153dupLet f z w = case Lam w `App` z of
154 Exp a fv (App_ (Exp b fv' (Lam_ c)) d) -> Exp a fv $ Let_ d c
155
156
157pattern Int i <- Exp _ _ (Int_ i) 180pattern Int i <- Exp _ _ (Int_ i)
158 where Int i = Exp [] 0 $ Int_ i 181 where Int i = Exp [] 0 $ Int_ i
159pattern App a b <- Exp u _ (App_ (upp u -> a) (upp u -> b)) 182pattern App a b <- Exp u _ (App_ (upp u -> a) (upp u -> b))
@@ -169,24 +192,14 @@ pattern Var i <- Exp u _ (Var_ (upVarIndex u -> i))
169 Var i = Exp [Up 0 i] 1 $ Var_ 0 192 Var i = Exp [Up 0 i] 1 $ Var_ 0
170pattern Lam i <- Exp u _ (Lam_ (upp (incUp 1 <$> u) -> i)) 193pattern Lam i <- Exp u _ (Lam_ (upp (incUp 1 <$> u) -> i))
171 where Lam i = dupLam Lam_ i 194 where Lam i = dupLam Lam_ i
172pattern Let i x <- Exp u _ (Let_ (upp u -> i) (upp (incUp 1 <$> u) -> x))
173 where Let i x = dupLet Let_ i x
174pattern Y i <- Exp u _ (Y_ (upp u -> i)) 195pattern Y i <- Exp u _ (Y_ (upp u -> i))
175 where Y i = dup1 Y_ i 196 where Y i = dup1 Y_ i
176pattern Con a b i <- Exp u _ (Con_ a b (map (upp u) -> i)) 197pattern Con a b i <- Exp u _ (Con_ a b (map (upp u) -> i))
177 where Con a b i = dupCon (Con_ a b) i 198 where Con a b i = dupCon (Con_ a b) i
178pattern Case a b <- Exp u _ (Case_ (upp u -> a) (map (second $ upp u) -> b)) 199pattern Case a b <- Exp u _ (Case_ (upp u -> a) (map (second $ upp u) -> b))
179 where Case a b = dupCase Case_ a b 200 where Case a b = dupCase Case_ a b
180--pattern Ups op a <- Exp _ (Ups_ op a)
181-- where Ups op a = Exp (
182
183upp u x = foldr up x u
184
185up :: Up -> Exp -> Exp
186up u@(Up i num) e@(Exp us s x)
187 | dbGE'' i e = e
188 | otherwise = addUp u e
189 201
202pattern Let i x = App (Lam x) i
190 203
191infixl 4 `App` 204infixl 4 `App`
192 205
@@ -197,8 +210,6 @@ data Up = Up !Int{-level-} !Int{-num-}
197 210
198incUp t (Up l n) = Up (l+t) n 211incUp t (Up l n) = Up (l+t) n
199 212
200rearr (Up l n) = rearrangeFreeVars (RFUp n) l
201
202showUps us n = foldr f (replicate n True) us where 213showUps us n = foldr f (replicate n True) us where
203 f (Up l n) is = take l is ++ replicate n False ++ drop l is 214 f (Up l n) is = take l is ++ replicate n False ++ drop l is
204 215
@@ -261,46 +272,11 @@ diffUpsTest' = diffUpsTest [x,y] --diffUpsTest x y
261 x = ([Up 1 2, Up 3 4, Up 8 2], 20) 272 x = ([Up 1 2, Up 3 4, Up 8 2], 20)
262 y = ([Up 2 2, Up 5 1, Up 6 2, Up 7 2], 18) 273 y = ([Up 2 2, Up 5 1, Up 6 2, Up 7 2], 18)
263 274
264insertUp u@(Up l 0) us = us 275getLets (Let x y) = x: getLets y
265insertUp u@(Up l n) [] = [u] 276getLets x = [x]
266insertUp u@(Up l n) us_@(u'@(Up l' n'): us)
267 | l < l' = u: us_
268 | l >= l' && l <= l' + n' = Up l' (n' + n): us
269 | otherwise = u': insertUp (Up (l-n') n) us
270
271addUp u (Exp us k e) = Exp (insertUp u us) k e -- TODO: remove excess ups?
272
273ups' a b = ups $ Up a b
274
275ups :: Up -> [(FreeVars, Exp)] -> [(FreeVars, Exp)]
276ups l [] = []
277ups l@(Up i _) xs@((s, e): es)
278 | dbGE i s = xs
279 | otherwise = eP (up l e) $ ups (incUp 1 l) es
280
281eP x [] = [(getFreeVars x, x)]
282eP x xs@((s, _):_) = (getFreeVars x <> lowerFreeVars s, x): xs
283
284pattern EPP x <- (_, x)
285 277
286--------------------------- 278---------------------------
287 279
288down i x | usedVar i x = Nothing
289down i x = Just $ down_ i x
290
291dbGE' l m = l >= m
292
293dbGE'' l m = dbGE' l $ maxFreeVars' m
294
295down_ i e
296 | dbGE'' i e = e
297down_ i0 e0@(Exp us ii e) = f i0 us e where
298 f i [] e = error $ "-- - - -- " ++ show (i0, i) ++ " " ++ ppShow e0 --(pushUps e) --"show down_ i e
299 f i (u@(Up j n): us) e
300 | i < j = addUp (Up (j-1) n) $ f i us e
301 | i >= j + n = addUp u $ f (i-n) us e
302 | otherwise = addUp (Up j $ n-1) $ Exp us ii e
303
304tryRemoves xs = tryRemoves_ (Var <$> xs) 280tryRemoves xs = tryRemoves_ (Var <$> xs)
305tryRemoves_ [] dt = dt 281tryRemoves_ [] dt = dt
306tryRemoves_ (Var i: vs) dt 282tryRemoves_ (Var i: vs) dt
@@ -312,30 +288,16 @@ tryRemove i x = fromMaybe x $ tryRemove_ i x
312 288
313tryRemove_ i dt@(MSt xs e es) 289tryRemove_ i dt@(MSt xs e es)
314 | Just e' <- down i e 290 | Just e' <- down i e
315 , Just xs' <- downUp i xs 291 , Just xs' <- down (i+1) xs
316 , Just es' <- downDown i es 292 , Just es' <- downDown i es
317 = Just $ MSt xs' e' es' 293 = Just $ MSt xs' e' es'
318 | otherwise 294 | otherwise
319 = Nothing 295 = Nothing
320 296
321downEP i ( x) = down i x
322downEP_ i (s, x) = (delVar i s, down_ i x)
323
324downUp i [] = Just []
325downUp i x@((s, _): _)
326 | usedVar (i+1) s = Nothing
327 | otherwise = Just $ downUp_ i x
328
329downUp_ i [] = []
330downUp_ i (x: xs)
331 | x' <- downEP_ (i+1) x
332 , xs' <- downUp_ (i+1) xs
333 = x': xs'
334
335downDown i [] = Just [] 297downDown i [] = Just []
336downDown 0 (_: xs) = Just xs 298downDown 0 (_: xs) = Just xs
337downDown i (x: xs) 299downDown i (x: xs)
338 | Just x' <- downEP (i-1) x 300 | Just x' <- down (i-1) x
339 , Just xs' <- downDown (i-1) xs 301 , Just xs' <- downDown (i-1) xs
340 = Just $ x': xs' 302 = Just $ x': xs'
341 | otherwise = Nothing 303 | otherwise = Nothing
@@ -382,14 +344,14 @@ step dt@(MSt t e vs) = case e of
382 Lam{} -> return dt 344 Lam{} -> return dt
383 345
384 Con cn i xs 346 Con cn i xs
385 | lz /= 0 -> return $ MSt (ups' 1 lz t) (Con cn i xs') $ zs ++ vs -- share complex constructor arguments 347 | lz /= 0 -> return $ MSt (up' 1 lz t) (Con cn i xs') $ zs ++ vs -- share complex constructor arguments
386 | otherwise -> return dt 348 | otherwise -> return dt
387 where 349 where
388 lz = length zs 350 lz = length zs
389 (xs', concat -> zs) = unzip $ f 0 xs 351 (xs', concat -> zs) = unzip $ f 0 xs
390 f i [] = [] 352 f i [] = []
391 f i (x: xs) | simple x = (up (Up 0 lz) x, []): f i xs 353 f i (x: xs) | simple x = (up' 0 lz x, []): f i xs
392 | otherwise = (Var i, [up (Up 0 (lz - i - 1)) x]): f (i+1) xs 354 | otherwise = (Var i, [up' 0 (lz - i - 1) x]): f (i+1) xs
393 355
394 Var i -> lookupHNF "var" (\e _ -> e) i dt 356 Var i -> lookupHNF "var" (\e _ -> e) i dt
395 357
@@ -398,22 +360,22 @@ step dt@(MSt t e vs) = case e of
398 Lam{} -> stepMsg "seq" $ tryRemoves (fvs a) $ MSt t b vs 360 Lam{} -> stepMsg "seq" $ tryRemoves (fvs a) $ MSt t b vs
399 Con{} -> stepMsg "seq" $ tryRemoves (fvs a) $ MSt t b vs 361 Con{} -> stepMsg "seq" $ tryRemoves (fvs a) $ MSt t b vs
400 Var i -> lookupHNF' "seqvar" (\e (Seq _ b) -> b) i dt 362 Var i -> lookupHNF' "seqvar" (\e (Seq _ b) -> b) i dt
401 _ -> stepMsg "seqexp" $ MSt (ups' 1 1 t) (Seq (Var 0) $ up (Up 0 1) b) $ a: vs 363 _ -> stepMsg "seqexp" $ MSt (up' 1 1 t) (Seq (Var 0) $ up' 0 1 b) $ a: vs
402 364
403 -- TODO: handle recursive constants 365 -- TODO: handle recursive constants
404 Y (Lam x) -> stepMsg "Y" $ MSt (ups' 1 1 t) x $ e: vs 366 Y (Lam x) -> stepMsg "Y" $ MSt (up' 1 1 t) x $ e: vs
405 367
406 App a b -> case a of 368 App a b -> case a of
407 Lam x | usedVar 0 x 369 Lam x | usedVar' 0 x
408 -> stepMsg "app" $ MSt (ups' 1 1 t) x $ b: vs 370 -> stepMsg "app" $ MSt (up' 1 1 t) x $ b: vs
409 Lam x -> stepMsg "appdel" $ tryRemoves (fvs b) $ MSt t x vs 371 Lam x -> stepMsg "appdel" $ tryRemoves (fvs b) $ MSt t x vs
410 Var i -> lookupHNF' "appvar" (\e (App _ y) -> App e y) i dt 372 Var i -> lookupHNF' "appvar" (\e (App _ y) -> App e y) i dt
411 _ -> stepMsg "appexp" $ MSt (ups' 1 1 t) (App (Var 0) $ up (Up 0 1) b) $ a: vs 373 _ -> stepMsg "appexp" $ MSt (up' 1 1 t) (App (Var 0) $ up' 0 1 b) $ a: vs
412 374
413 Case a cs -> case a of 375 Case a cs -> case a of
414 Con cn i es -> stepMsg "case" $ tryRemoves (nub $ foldMap (fvs . snd) $ delElem i cs) $ (MSt t (foldl App (snd $ cs !! i) es) vs) 376 Con cn i es -> stepMsg "case" $ tryRemoves (nub $ foldMap (fvs . snd) $ delElem i cs) $ (MSt t (foldl App (snd $ cs !! i) es) vs)
415 Var i -> lookupHNF' "casevar" (\e (Case _ cs) -> Case e cs) i dt 377 Var i -> lookupHNF' "casevar" (\e (Case _ cs) -> Case e cs) i dt
416 _ -> stepMsg "caseexp" $ MSt (ups' 1 1 t) (Case (Var 0) $ second (up (Up 0 1)) <$> cs) $ a: vs 378 _ -> stepMsg "caseexp" $ MSt (up' 1 1 t) (Case (Var 0) $ second (up' 0 1) <$> cs) $ a: vs
417 379
418 Op2 op x y -> case (x, y) of 380 Op2 op x y -> case (x, y) of
419 (Int e, Int f) -> return $ MSt t (int op e f) vs 381 (Int e, Int f) -> return $ MSt t (int op e f) vs
@@ -425,9 +387,9 @@ step dt@(MSt t e vs) = case e of
425 int EqInt a b = if a == b then T else F 387 int EqInt a b = if a == b then T else F
426 (Var i, _) -> lookupHNF' "op2-1var" (\e (Op2 op _ y) -> Op2 op e y) i dt 388 (Var i, _) -> lookupHNF' "op2-1var" (\e (Op2 op _ y) -> Op2 op e y) i dt
427 (_, Var i) -> lookupHNF' "op2-2var" (\e (Op2 op x _) -> Op2 op x e) i dt 389 (_, Var i) -> lookupHNF' "op2-2var" (\e (Op2 op x _) -> Op2 op x e) i dt
428 (Int{}, _) -> stepMsg "op2" $ MSt (ups' 1 1 t) (Op2 op x (Var 0)) $ y: vs 390 (Int{}, _) -> stepMsg "op2" $ MSt (up' 1 1 t) (Op2 op x (Var 0)) $ y: vs
429 (_, Int{}) -> stepMsg "op2" $ MSt (ups' 1 1 t) (Op2 op (Var 0) y) $ x: vs 391 (_, Int{}) -> stepMsg "op2" $ MSt (up' 1 1 t) (Op2 op (Var 0) y) $ x: vs
430 _ -> stepMsg "op2" $ MSt (ups' 1 2 t) (Op2 op (Var 0) (Var 1)) $ x: y: vs 392 _ -> stepMsg "op2" $ MSt (up' 1 2 t) (Op2 op (Var 0) (Var 1)) $ x: y: vs
431 393
432stepMsg :: MachineMonad m => String -> MSt -> m MSt 394stepMsg :: MachineMonad m => String -> MSt -> m MSt
433stepMsg msg dt = do 395stepMsg msg dt = do
@@ -435,15 +397,15 @@ stepMsg msg dt = do
435 collectSizeStat $ size dt 397 collectSizeStat $ size dt
436 step dt 398 step dt
437 399
438hnf e = stepMsg "hnf" $ MSt [] e [] 400hnf e = stepMsg "hnf" $ MSt (Var 0) e []
439 401
440size (MSt xs _ ys) = length xs + length ys 402size (MSt xs _ ys) = length (getLets xs) + length ys
441 403
442shiftL (MSt xs x (e: es)) = MSt (eP x xs) e es 404shiftL (MSt xs x (e: es)) = MSt (Let x xs) e es
443shiftR (MSt (EPP x: xs) e es) = MSt xs x $ e: es 405shiftR (MSt (Let x xs) e es) = MSt xs x $ e: es
444 406
445shiftLookup f n dt@(MSt _ e _) = repl $ iterate shiftR dt !! (n+1) where 407shiftLookup f n dt@(MSt _ e _) = repl $ iterate shiftR dt !! (n+1) where
446 repl (MSt xs z es) = MSt xs (f (up (Up 0 (n+1)) e) z) es 408 repl (MSt xs z es) = MSt xs (f (up' 0 (n+1) e) z) es
447 409
448-- lookup var in head normal form 410-- lookup var in head normal form
449lookupHNF msg f i dt = tryRemove i . shiftLookup f i <$> stepMsg msg (iterate shiftL dt !! (i+1)) 411lookupHNF msg f i dt = tryRemove i . shiftLookup f i <$> stepMsg msg (iterate shiftL dt !! (i+1))
@@ -478,7 +440,7 @@ test = runMachinePure (id_ `App` id_ `App` id_ `App` id_ `App` Con "()" 0 [])
478 440
479test' = runMachinePure (id_ `App` (id_ `App` Con "()" 0 [])) 441test' = runMachinePure (id_ `App` (id_ `App` Con "()" 0 []))
480 442
481foldr_ f e = Y $ Lam $ Lam $ caseList (Var 0) (up (Up 0 2) e) (Lam $ Lam $ up (Up 0 4) f `App` Var 1 `App` (Var 3 `App` Var 0)) 443foldr_ f e = Y $ Lam $ Lam $ caseList (Var 0) (up' 0 2 e) (Lam $ Lam $ up' 0 4 f `App` Var 1 `App` (Var 3 `App` Var 0))
482 444
483filter_ p = foldr_ (Lam $ Lam $ if_ (p `App` Var 1) (Cons (Var 1) (Var 0)) (Var 0)) Nil 445filter_ p = foldr_ (Lam $ Lam $ if_ (p `App` Var 1) (Cons (Var 1) (Var 0)) (Var 0)) Nil
484 446