diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-05-16 14:19:07 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-05-16 14:19:07 +0200 |
commit | b3308f8ee9c4fd26ef0306d25c3c83d915ec4786 (patch) | |
tree | cc7a62f1a10fa6b82fe5d8a4b36e2db26b2b3497 /prototypes | |
parent | c3bf9728ba74fbb363dcd00941419602568d645b (diff) |
refactoring
Diffstat (limited to 'prototypes')
-rw-r--r-- | prototypes/LamMachine.hs | 208 |
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 | |||
46 | data Exp = Exp {dbUps :: [Up], maxFreeVars :: Int, expexp :: Exp_ } | 45 | data Exp = Exp {dbUps :: [Up], maxFreeVars :: Int, expexp :: Exp_ } |
47 | 46 | ||
48 | -- state of the machine | 47 | -- state of the machine |
49 | data MSt = MSt [(FreeVars, Exp)] -- TODO: use finger tree instead of list | 48 | data 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 | ||
55 | instance PShow Exp where | 54 | instance 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 | |||
71 | instance 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 | ||
71 | shUps a b = DPreOp (-20) (SimpleAtom $ show a) b | 75 | shUps a b = DPreOp (-20) (SimpleAtom $ show a) b |
72 | shUps' a x b = DPreOp (-20) (SimpleAtom $ show a ++ show x) b | 76 | shUps' a x b = DPreOp (-20) (SimpleAtom $ show a ++ show x) b |
73 | 77 | ||
74 | shLam_ usedVar b = DFreshName usedVar $ showLam (DVar 0) b | 78 | shLam b = DFreshName True $ showLam (DVar 0) b |
75 | 79 | ||
76 | showLam x (DFreshName u d) = DFreshName u $ showLam (DUp 0 x) d | 80 | showLam x (DFreshName u d) = DFreshName u $ showLam (DUp 0 x) d |
77 | showLam x (DLam xs y) = DLam (DSep (InfixR 11) x xs) y | 81 | showLam x (DLam xs y) = DLam (DSep (InfixR 11) x xs) y |
78 | showLam x y = DLam x y | 82 | showLam x y = DLam x y |
79 | 83 | ||
80 | shLet i a b = DLet' (DLet "=" (blue $ shVar i) $ DUp i a) (DUp i b) | 84 | shLet a b = DFreshName True $ showLet (DLet "=" (shVar 0) $ DUp 0 a) b |
81 | 85 | ||
82 | showLet x (DFreshName u d) = DFreshName u $ showLet (DUp 0 x) d | 86 | showLet x (DFreshName u d) = DFreshName u $ showLet (DUp 0 x) d |
83 | showLet x (DLet' xs y) = DLet' (DSemi x xs) y | 87 | showLet x (DLet' xs y) = DLet' (DSemi x xs) y |
84 | showLet x y = DLet' x y | 88 | showLet x y = DLet' x y |
85 | 89 | ||
86 | shLet_ a b = DFreshName True $ showLet (DLet "=" (shVar 0) $ DUp 0 a) b | ||
87 | |||
88 | instance 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 | ||
95 | maxFreeVars' (Exp u k _) = upsFreeVars' u k | 93 | maxFreeVars' (Exp xs s _) = foldl' f s xs |
96 | |||
97 | upsFreeVars xs s = foldr rearr s xs | ||
98 | |||
99 | upsFreeVars' 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 | ||
105 | upVarIndex xs s = foldr rearr' s xs | 97 | upVarIndex 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 | ||
103 | upp u x = foldr up x u | ||
104 | |||
105 | up' l n = up $ Up l n | ||
106 | |||
107 | up :: Up -> Exp -> Exp | ||
108 | up (Up i 0) e = e | ||
109 | up u@(Up i num) e@(Exp us s x) = Exp (insertUp_ s u us) s x | ||
110 | |||
111 | insertUp_ s u@(Up i _) [] | ||
112 | | i >= s = [] | ||
113 | | otherwise = [u] | ||
114 | insertUp_ 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 |
113 | instance HasFreeVars Exp where | 120 | insertUp (Up l 0) us = us |
114 | getFreeVars (Exp us fv e) = upsFreeVars us $ FreeVars $ 2^fv - 1 | 121 | insertUp u [] = [u] |
122 | insertUp 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 | ||
116 | fvs (Exp us fv _) = gen 0 $ foldr f [fv] us where | 128 | fvs (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 | ||
134 | usedVar' 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 | |||
141 | down 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 | |||
122 | upss u (Exp _ i e) = Exp u i e | 150 | upss u (Exp _ i e) = Exp u i e |
123 | 151 | ||
124 | dup2 f ax bx = Exp s (maxFreeVars' az `max` maxFreeVars' bz) $ f az bz where | 152 | dup2 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 | ||
153 | dupLet 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 | |||
157 | pattern Int i <- Exp _ _ (Int_ i) | 180 | pattern Int i <- Exp _ _ (Int_ i) |
158 | where Int i = Exp [] 0 $ Int_ i | 181 | where Int i = Exp [] 0 $ Int_ i |
159 | pattern App a b <- Exp u _ (App_ (upp u -> a) (upp u -> b)) | 182 | pattern 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 |
170 | pattern Lam i <- Exp u _ (Lam_ (upp (incUp 1 <$> u) -> i)) | 193 | pattern Lam i <- Exp u _ (Lam_ (upp (incUp 1 <$> u) -> i)) |
171 | where Lam i = dupLam Lam_ i | 194 | where Lam i = dupLam Lam_ i |
172 | pattern Let i x <- Exp u _ (Let_ (upp u -> i) (upp (incUp 1 <$> u) -> x)) | ||
173 | where Let i x = dupLet Let_ i x | ||
174 | pattern Y i <- Exp u _ (Y_ (upp u -> i)) | 195 | pattern Y i <- Exp u _ (Y_ (upp u -> i)) |
175 | where Y i = dup1 Y_ i | 196 | where Y i = dup1 Y_ i |
176 | pattern Con a b i <- Exp u _ (Con_ a b (map (upp u) -> i)) | 197 | pattern 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 |
178 | pattern Case a b <- Exp u _ (Case_ (upp u -> a) (map (second $ upp u) -> b)) | 199 | pattern 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 | |||
183 | upp u x = foldr up x u | ||
184 | |||
185 | up :: Up -> Exp -> Exp | ||
186 | up u@(Up i num) e@(Exp us s x) | ||
187 | | dbGE'' i e = e | ||
188 | | otherwise = addUp u e | ||
189 | 201 | ||
202 | pattern Let i x = App (Lam x) i | ||
190 | 203 | ||
191 | infixl 4 `App` | 204 | infixl 4 `App` |
192 | 205 | ||
@@ -197,8 +210,6 @@ data Up = Up !Int{-level-} !Int{-num-} | |||
197 | 210 | ||
198 | incUp t (Up l n) = Up (l+t) n | 211 | incUp t (Up l n) = Up (l+t) n |
199 | 212 | ||
200 | rearr (Up l n) = rearrangeFreeVars (RFUp n) l | ||
201 | |||
202 | showUps us n = foldr f (replicate n True) us where | 213 | showUps 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 | ||
264 | insertUp u@(Up l 0) us = us | 275 | getLets (Let x y) = x: getLets y |
265 | insertUp u@(Up l n) [] = [u] | 276 | getLets x = [x] |
266 | insertUp 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 | |||
271 | addUp u (Exp us k e) = Exp (insertUp u us) k e -- TODO: remove excess ups? | ||
272 | |||
273 | ups' a b = ups $ Up a b | ||
274 | |||
275 | ups :: Up -> [(FreeVars, Exp)] -> [(FreeVars, Exp)] | ||
276 | ups l [] = [] | ||
277 | ups l@(Up i _) xs@((s, e): es) | ||
278 | | dbGE i s = xs | ||
279 | | otherwise = eP (up l e) $ ups (incUp 1 l) es | ||
280 | |||
281 | eP x [] = [(getFreeVars x, x)] | ||
282 | eP x xs@((s, _):_) = (getFreeVars x <> lowerFreeVars s, x): xs | ||
283 | |||
284 | pattern EPP x <- (_, x) | ||
285 | 277 | ||
286 | --------------------------- | 278 | --------------------------- |
287 | 279 | ||
288 | down i x | usedVar i x = Nothing | ||
289 | down i x = Just $ down_ i x | ||
290 | |||
291 | dbGE' l m = l >= m | ||
292 | |||
293 | dbGE'' l m = dbGE' l $ maxFreeVars' m | ||
294 | |||
295 | down_ i e | ||
296 | | dbGE'' i e = e | ||
297 | down_ 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 | |||
304 | tryRemoves xs = tryRemoves_ (Var <$> xs) | 280 | tryRemoves xs = tryRemoves_ (Var <$> xs) |
305 | tryRemoves_ [] dt = dt | 281 | tryRemoves_ [] dt = dt |
306 | tryRemoves_ (Var i: vs) dt | 282 | tryRemoves_ (Var i: vs) dt |
@@ -312,30 +288,16 @@ tryRemove i x = fromMaybe x $ tryRemove_ i x | |||
312 | 288 | ||
313 | tryRemove_ i dt@(MSt xs e es) | 289 | tryRemove_ 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 | ||
321 | downEP i ( x) = down i x | ||
322 | downEP_ i (s, x) = (delVar i s, down_ i x) | ||
323 | |||
324 | downUp i [] = Just [] | ||
325 | downUp i x@((s, _): _) | ||
326 | | usedVar (i+1) s = Nothing | ||
327 | | otherwise = Just $ downUp_ i x | ||
328 | |||
329 | downUp_ i [] = [] | ||
330 | downUp_ i (x: xs) | ||
331 | | x' <- downEP_ (i+1) x | ||
332 | , xs' <- downUp_ (i+1) xs | ||
333 | = x': xs' | ||
334 | |||
335 | downDown i [] = Just [] | 297 | downDown i [] = Just [] |
336 | downDown 0 (_: xs) = Just xs | 298 | downDown 0 (_: xs) = Just xs |
337 | downDown i (x: xs) | 299 | downDown 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 | ||
432 | stepMsg :: MachineMonad m => String -> MSt -> m MSt | 394 | stepMsg :: MachineMonad m => String -> MSt -> m MSt |
433 | stepMsg msg dt = do | 395 | stepMsg 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 | ||
438 | hnf e = stepMsg "hnf" $ MSt [] e [] | 400 | hnf e = stepMsg "hnf" $ MSt (Var 0) e [] |
439 | 401 | ||
440 | size (MSt xs _ ys) = length xs + length ys | 402 | size (MSt xs _ ys) = length (getLets xs) + length ys |
441 | 403 | ||
442 | shiftL (MSt xs x (e: es)) = MSt (eP x xs) e es | 404 | shiftL (MSt xs x (e: es)) = MSt (Let x xs) e es |
443 | shiftR (MSt (EPP x: xs) e es) = MSt xs x $ e: es | 405 | shiftR (MSt (Let x xs) e es) = MSt xs x $ e: es |
444 | 406 | ||
445 | shiftLookup f n dt@(MSt _ e _) = repl $ iterate shiftR dt !! (n+1) where | 407 | shiftLookup 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 |
449 | lookupHNF msg f i dt = tryRemove i . shiftLookup f i <$> stepMsg msg (iterate shiftL dt !! (i+1)) | 411 | lookupHNF 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 | ||
479 | test' = runMachinePure (id_ `App` (id_ `App` Con "()" 0 [])) | 441 | test' = runMachinePure (id_ `App` (id_ `App` Con "()" 0 [])) |
480 | 442 | ||
481 | foldr_ 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)) | 443 | foldr_ 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 | ||
483 | filter_ p = foldr_ (Lam $ Lam $ if_ (p `App` Var 1) (Cons (Var 1) (Var 0)) (Var 0)) Nil | 445 | filter_ p = foldr_ (Lam $ Lam $ if_ (p `App` Var 1) (Cons (Var 1) (Var 0)) (Var 0)) Nil |
484 | 446 | ||