summaryrefslogtreecommitdiff
path: root/prototypes
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-06-03 13:27:35 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-06-03 13:27:35 +0200
commite2bd6c4f6a2a5fb3969692ea7ac6d9ef9a66ee7c (patch)
treeca8fb1e921d26923948329ad48c646bce9826cc2 /prototypes
parent19dc22401cc599cd6d76e2710ad40cec9fbb3cb8 (diff)
refactoring
Diffstat (limited to 'prototypes')
-rw-r--r--prototypes/LamMachine.hs240
1 files changed, 123 insertions, 117 deletions
diff --git a/prototypes/LamMachine.hs b/prototypes/LamMachine.hs
index 6fbd3f73..0a80ea2c 100644
--- a/prototypes/LamMachine.hs
+++ b/prototypes/LamMachine.hs
@@ -47,16 +47,32 @@ data Op2 = AppOp | SeqOp | Add | Sub | Mod | LessEq | EqInt
47data Exp = Exp_ !FV !Exp_ 47data Exp = Exp_ !FV !Exp_
48 deriving (Eq, Show) 48 deriving (Eq, Show)
49 49
50-------------------
51
52data EnvPiece 50data EnvPiece
53 = ELet Exp 51 = ELet !Exp
54 | EDLet1 DExp 52 | EDLet1 !DExp
55 | ECase_ FV [String] [Exp] 53 | ECase_ !FV ![String] ![Exp]
56 | EOp2_1 Op2 Exp 54 | EOp2_1 !Op2 !Exp
57 | EOp2_2 Op2 Exp 55 | EOp2_2 !Op2 !Exp
58 deriving (Eq, Show) 56 deriving (Eq, Show)
59 57
58data DExp
59 = DExpNil -- variable
60 | DExpCons_ !FV !EnvPiece !DExp
61 deriving (Eq, Show)
62
63data DEnv
64 = DEnvNil
65 | DEnvCons !EnvPiece !DEnv
66 deriving (Eq, Show)
67
68-- state of the machine
69data MSt = MSt !DEnv !Exp
70 deriving (Eq, Show)
71
72--------------------------------------------------------------------- pattern synonyms
73
74infixr 4 `DEnvCons`, `DExpCons`
75
60pattern ECase op e <- ECase_ u op (map (upp u) -> e) 76pattern ECase op e <- ECase_ u op (map (upp u) -> e)
61 where ECase op b = ECase_ u op bz where (u, bz) = deltas b 77 where ECase op b = ECase_ u op bz where (u, bz) = deltas b
62 78
@@ -78,11 +94,6 @@ getEnvPiece = \case
78 94
79uppS (SFV _ x) (Exp_ u a) = Exp_ (expand x u) a 95uppS (SFV _ x) (Exp_ u a) = Exp_ (expand x u) a
80 96
81data DExp
82 = DExpNil -- variable
83 | DExpCons_ FV EnvPiece DExp
84 deriving (Eq, Show)
85
86pattern DExpCons :: EnvPiece -> DExp -> DExp 97pattern DExpCons :: EnvPiece -> DExp -> DExp
87pattern DExpCons a b <- (getDExpCons -> (a, b)) 98pattern DExpCons a b <- (getDExpCons -> (a, b))
88 where DExpCons (EnvPiece ux a) DExpNil = DExpCons_ (fromSFV s) (EnvPiece ux' a) DExpNil 99 where DExpCons (EnvPiece ux a) DExpNil = DExpCons_ (fromSFV s) (EnvPiece ux' a) DExpNil
@@ -93,7 +104,7 @@ pattern DExpCons a b <- (getDExpCons -> (a, b))
93 104
94getDExpCons (DExpCons_ u x@(EnvPiece (SFV n _) _) b) = (uppEP u x, uppDE u n b) 105getDExpCons (DExpCons_ u x@(EnvPiece (SFV n _) _) b) = (uppEP u x, uppDE u n b)
95 106
96uppEP u (EnvPiece (SFV n x) y) = EnvPiece (SFV n $ onTail (u `expand`) n x) y -- ??? 107uppEP u (EnvPiece (SFV n x) y) = EnvPiece (SFV n $ onTail (u `expand`) n x) y
97 108
98upP i j = uppEP $ mkUp i j 109upP i j = uppEP $ mkUp i j
99 110
@@ -103,99 +114,10 @@ uppDE :: FV -> Nat -> DExp -> DExp
103uppDE a _ DExpNil = DExpNil 114uppDE a _ DExpNil = DExpNil
104uppDE a n (DExpCons_ u x y) = DExpCons_ (onTail (expand a) n u) x y 115uppDE a n (DExpCons_ u x y) = DExpCons_ (onTail (expand a) n u) x y
105 116
106data DEnv
107 = DEnvNil
108 | DEnvCons EnvPiece DEnv
109 deriving (Eq, Show)
110
111-- state of the machine
112data MSt = MSt DEnv Exp
113 deriving (Eq, Show)
114
115infixr 4 `DEnvCons`, `DExpCons`
116
117dDown i DExpNil = Just DExpNil 117dDown i DExpNil = Just DExpNil
118dDown i (DExpCons_ u a b) = DExpCons_ <$> downFV i u <*> pure a <*> pure b 118dDown i (DExpCons_ u a b) = DExpCons_ <$> downFV i u <*> pure a <*> pure b
119 119
120--------------------------------------------------------------------- toolbox: pretty print 120---------------------------------------------------------------------
121
122class ViewShow a where
123 viewShow :: Bool -> a -> Doc
124
125instance ViewShow Exp where
126 viewShow vi = pShow where
127 pShow e@(Exp_ fv x) = showFE green vi fv $
128 case {-if vi then Exp_ (selfContract fv) x else-} e of
129 Var (Nat i) -> DVar i
130 Let a b -> shLet (pShow a) $ pShow b
131 Seq a b -> DOp "`seq`" (Infix 1) (pShow a) (pShow b)
132 Lam e -> shLam $ pShow e
133 Con s i xs -> foldl DApp (text s) $ pShow <$> xs
134 Int i -> pShow' i
135 Y e -> "Y" `DApp` pShow e
136 InHNF x -> DBrace (pShow x)
137 Op1 o x -> text (show o) `DApp` pShow x
138 Op2 o x y -> shOp2 o (pShow x) (pShow y)
139 Case cn e xs -> shCase cn (pShow e) (pShow <$> xs)
140 Exp_ u Var_ -> onblue $ pShow' u
141 e -> error $ "pShow @Exp: " ++ show e
142
143glueTo = DGlue (InfixR 40)
144
145shCase cn e xs = DPreOp (-20) (ComplexAtom "case" (-10) e (SimpleAtom "of"))
146 $ foldr1 DSemi [DArr_ "->" (text a) b | (a, b) <- zip cn xs]
147
148shOp2 AppOp x y = DApp x y
149shOp2 EqInt x y = DOp "==" (Infix 4) x y
150shOp2 Add x y = DOp "+" (InfixL 6) x y
151shOp2 o x y = text (show o) `DApp` x `DApp` y
152
153showFE c True fv | fv /= mempty = DGlue (InfixL 40) (c $ pShow' fv)
154showFE _ _ _ = id
155
156pShow' = pShow
157
158instance ViewShow MSt where
159 viewShow vi (MSt env e) = g (onred $ white $ pShow e) env
160 where
161 pShow = viewShow vi
162
163 g y DEnvNil = y
164 g y z@(DEnvCons p env) = flip g env $ {-showFE red vi (case p of EnvPiece f _ -> f) $-} case p of
165 EDLet1 x -> shLet y (h x)
166 ELet x -> shLet (pShow x) y
167 ECase cns xs -> shCase cns y (pShow <$> xs)
168 EOp2_1 op x -> shOp2 op y (pShow x)
169 EOp2_2 op x -> shOp2 op (pShow x) y
170
171 h DExpNil = onred $ white "*" --TODO?
172 h z@(DExpCons p (h -> y)) = showFE blue vi (case p of EnvPiece f _ -> f) $ case p of
173 EDLet1 x -> shLet y (h x)
174 ELet x -> shLet (pShow x) y
175 ECase cns xs -> shCase cns y (pShow <$> xs)
176 EOp2_1 op x -> shOp2 op y (pShow x)
177 EOp2_2 op x -> shOp2 op (pShow x) y
178
179instance PShow MSt where pShow = viewShow False
180
181
182shUps a b = DPreOp (-20) (SimpleAtom $ show a) b
183shUps' a x b = DPreOp (-20) (SimpleAtom $ show a ++ show x) b
184
185shLam b = DFreshName True $ showLam (DVar 0) b
186
187showLam x (DFreshName u d) = DFreshName u $ showLam (DUp 0 x) d
188showLam x (DLam xs y) = DLam (DSep (InfixR 11) x xs) y
189showLam x y = DLam x y
190
191shLet a b = DFreshName True $ showLet (DLet "=" (shVar 0) $ DUp 0 a) b
192
193showLet x (DFreshName u d) = DFreshName u $ showLet (DUp 0 x) d
194showLet x (DLet' xs y) = DLet' (DSemi x xs) y
195showLet x y = DLet' x y
196
197
198--------------------------------------------------------------------- pattern synonyms
199 121
200pattern Int i <- Exp_ _ (Int_ i) 122pattern Int i <- Exp_ _ (Int_ i)
201 where Int i = Exp_ mempty $ Int_ i 123 where Int i = Exp_ mempty $ Int_ i
@@ -215,8 +137,20 @@ pattern Case a b c <- Exp_ u (Case_ a (upp u -> b) (map (upp u) -> c))
215pattern Let i x <- App (Lam x) i 137pattern Let i x <- App (Lam x) i
216 where Let i (down 0 -> Just x) = x 138 where Let i (down 0 -> Just x) = x
217 Let i x = App (Lam x) i 139 Let i x = App (Lam x) i
140
141pattern InHNF a <- (getHNF -> Just a)
142 where InHNF a@Int{} = a
143 InHNF a@Lam{} = a
144 InHNF a@(Op1 HNF_ _) = a
145 InHNF a = Op1 HNF_ a
146
147getHNF x = case x of
148 Int{} -> Just x
149 Lam{} -> Just x
150 Op1 HNF_ a -> Just a
151 _ -> Nothing
152
218pattern Y a = Op1 YOp a 153pattern Y a = Op1 YOp a
219pattern InHNF a = Op1 HNF_ a
220pattern App a b = Op2 AppOp a b 154pattern App a b = Op2 AppOp a b
221pattern Seq a b = Op2 SeqOp a b 155pattern Seq a b = Op2 SeqOp a b
222 156
@@ -270,9 +204,6 @@ hnf = justResult . initSt
270 204
271---------------- 205----------------
272 206
273inHNF (InHNF a) = InHNF a
274inHNF a = InHNF a
275
276type GenSteps e 207type GenSteps e
277 = (MSt -> e) 208 = (MSt -> e)
278 -> (StepTag -> (MSt -> e) -> MSt -> e) 209 -> (StepTag -> (MSt -> e) -> MSt -> e)
@@ -287,9 +218,6 @@ focusNotUsed _ = True
287steps :: forall e . Int -> GenSteps e 218steps :: forall e . Int -> GenSteps e
288steps lev nostep bind cont (MSt vs e) = case e of 219steps lev nostep bind cont (MSt vs e) = case e of
289 220
290 Int{} -> step "int hnf" $ MSt vs $ InHNF e
291 Lam{} -> step "lam hnf" $ MSt vs $ InHNF e
292
293 Con cn i xs 221 Con cn i xs
294 | lz == 0 || focusNotUsed (MSt vs e) -> step "con hnf" $ MSt vs $ InHNF e 222 | lz == 0 || focusNotUsed (MSt vs e) -> step "con hnf" $ MSt vs $ InHNF e
295 | otherwise -> step "copy con" $ MSt (foldr DEnvCons vs $ ELet <$> zs) $ InHNF $ Con cn i xs' -- share complex constructor arguments 223 | otherwise -> step "copy con" $ MSt (foldr DEnvCons vs $ ELet <$> zs) $ InHNF $ Con cn i xs' -- share complex constructor arguments
@@ -312,7 +240,7 @@ steps lev nostep bind cont (MSt vs e) = case e of
312 240
313 DEnvNil -> bind "whnf" nostep $ MSt DEnvNil $ InHNF a 241 DEnvNil -> bind "whnf" nostep $ MSt DEnvNil $ InHNF a
314 242
315 ELet x `DEnvCons` vs -> step "goUp" $ MSt vs $ inHNF $ Let x $ InHNF a 243 ELet x `DEnvCons` vs -> step "goUp" $ MSt vs $ InHNF $ Let x $ InHNF a
316 x `DEnvCons` vs | Let y e <- a -> step "pakol" $ MSt (upP 0 1 x `DEnvCons` ELet y `DEnvCons` vs) e 244 x `DEnvCons` vs | Let y e <- a -> step "pakol" $ MSt (upP 0 1 x `DEnvCons` ELet y `DEnvCons` vs) e
317 x `DEnvCons` vs -> case x of 245 x `DEnvCons` vs -> case x of
318 EOp2_1 SeqOp b -> case a of 246 EOp2_1 SeqOp b -> case a of
@@ -320,16 +248,16 @@ steps lev nostep bind cont (MSt vs e) = case e of
320 Lam{} -> step "seq" $ MSt vs b -- TODO: remove a 248 Lam{} -> step "seq" $ MSt vs b -- TODO: remove a
321 Con{} -> step "seq" $ MSt vs b -- TODO: remove a 249 Con{} -> step "seq" $ MSt vs b -- TODO: remove a
322 _ -> step "seq hnf" $ MSt vs $ InHNF $ Seq (InHNF a) b 250 _ -> 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 251 ECase cns cs -> case a of
328 Con cn i es -> step "case" $ MSt vs $ foldl App (cs !! i) es -- TODO: remove unused cases 252 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 253 _ -> step "case hnf" $ MSt vs $ InHNF $ Case cns (InHNF a) cs
254 EOp2_1 AppOp b -> case a of
255 Lam (down 0 -> Just x) -> step "appdel" $ MSt vs x -- TODO: remove b
256 Lam x -> step "app" $ MSt (ELet b `DEnvCons` vs) x
257 _ -> step "app hnf" $ MSt vs $ InHNF $ App (InHNF a) b
330 EOp2_1 op b -> step "op2_2 hnf" $ MSt (EOp2_2 op (InHNF a) `DEnvCons` vs) b 258 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 259 EOp2_2 op b -> case (b, a) of
332 (InHNF (Int e), Int f) -> step "op-done" $ MSt vs (int op e f) 260 (Int e, Int f) -> step "op-done" $ MSt vs (int op e f)
333 where 261 where
334 int Add a b = Int $ a + b 262 int Add a b = Int $ a + b
335 int Sub a b = Int $ a - b 263 int Sub a b = Int $ a - b
@@ -337,7 +265,7 @@ steps lev nostep bind cont (MSt vs e) = case e of
337 int LessEq a b = if a <= b then T else F 265 int LessEq a b = if a <= b then T else F
338 int EqInt a b = if a == b then T else F 266 int EqInt a b = if a == b then T else F
339 _ -> step "op2 hnf" $ MSt vs $ InHNF $ Op2 op b (InHNF a) 267 _ -> step "op2 hnf" $ MSt vs $ InHNF $ Op2 op b (InHNF a)
340 EDLet1 (dDown 0 -> Just d) -> zipUp a vs d 268 EDLet1 (dDown 0 -> Just d) -> zipUp (InHNF a) vs d
341 EDLet1 d -> zipUp (up 0 1 a) (ELet (InHNF a) `DEnvCons` vs) d 269 EDLet1 d -> zipUp (up 0 1 a) (ELet (InHNF a) `DEnvCons` vs) d
342 270
343 where 271 where
@@ -368,6 +296,84 @@ simple = \case
368 296
369delElem i xs = take i xs ++ drop (i+1) xs 297delElem i xs = take i xs ++ drop (i+1) xs
370 298
299--------------------------------------------------------------------- toolbox: pretty print
300
301class ViewShow a where
302 viewShow :: Bool -> a -> Doc
303
304instance ViewShow Exp where
305 viewShow vi = pShow where
306 pShow e@(Exp_ fv x) = showFE green vi fv $
307 case {-if vi then Exp_ (selfContract fv) x else-} e of
308 Var (Nat i) -> DVar i
309 Let a b -> shLet (pShow a) $ pShow b
310 Seq a b -> DOp "`seq`" (Infix 1) (pShow a) (pShow b)
311 Lam e -> shLam $ pShow e
312 Con s i xs -> foldl DApp (text s) $ pShow <$> xs
313 Int i -> pShow' i
314 Y e -> "Y" `DApp` pShow e
315 InHNF x -> DBrace (pShow x)
316 Op1 o x -> text (show o) `DApp` pShow x
317 Op2 o x y -> shOp2 o (pShow x) (pShow y)
318 Case cn e xs -> shCase cn (pShow e) (pShow <$> xs)
319 Exp_ u Var_ -> onblue $ pShow' u
320 e -> error $ "pShow @Exp: " ++ show e
321
322glueTo = DGlue (InfixR 40)
323
324shCase cn e xs = DPreOp (-20) (ComplexAtom "case" (-10) e (SimpleAtom "of"))
325 $ foldr1 DSemi [DArr_ "->" (text a) b | (a, b) <- zip cn xs]
326
327shOp2 AppOp x y = DApp x y
328shOp2 EqInt x y = DOp "==" (Infix 4) x y
329shOp2 Add x y = DOp "+" (InfixL 6) x y
330shOp2 o x y = text (show o) `DApp` x `DApp` y
331
332showFE c True fv | fv /= mempty = DGlue (InfixL 40) (c $ pShow' fv)
333showFE _ _ _ = id
334
335pShow' = pShow
336
337instance ViewShow MSt where
338 viewShow vi (MSt env e) = g (onred $ white $ pShow e) env
339 where
340 pShow = viewShow vi
341
342 g y DEnvNil = y
343 g y z@(DEnvCons p env) = flip g env $ {-showFE red vi (case p of EnvPiece f _ -> f) $-} case p of
344 EDLet1 x -> shLet y (h x)
345 ELet x -> shLet (pShow x) y
346 ECase cns xs -> shCase cns y (pShow <$> xs)
347 EOp2_1 op x -> shOp2 op y (pShow x)
348 EOp2_2 op x -> shOp2 op (pShow x) y
349
350 h DExpNil = onred $ white "*" --TODO?
351 h z@(DExpCons p (h -> y)) = showFE blue vi (case p of EnvPiece f _ -> f) $ case p of
352 EDLet1 x -> shLet y (h x)
353 ELet x -> shLet (pShow x) y
354 ECase cns xs -> shCase cns y (pShow <$> xs)
355 EOp2_1 op x -> shOp2 op y (pShow x)
356 EOp2_2 op x -> shOp2 op (pShow x) y
357
358instance PShow MSt where pShow = viewShow False
359
360
361shUps a b = DPreOp (-20) (SimpleAtom $ show a) b
362shUps' a x b = DPreOp (-20) (SimpleAtom $ show a ++ show x) b
363
364shLam b = DFreshName True $ showLam (DVar 0) b
365
366showLam x (DFreshName u d) = DFreshName u $ showLam (DUp 0 x) d
367showLam x (DLam xs y) = DLam (DSep (InfixR 11) x xs) y
368showLam x y = DLam x y
369
370shLet a b = DFreshName True $ showLet (DLet "=" (shVar 0) $ DUp 0 a) b
371
372showLet x (DFreshName u d) = DFreshName u $ showLet (DUp 0 x) d
373showLet x (DLet' xs y) = DLet' (DSemi x xs) y
374showLet x y = DLet' x y
375
376
371---------------------------------------------------------------------------------------- examples 377---------------------------------------------------------------------------------------- examples
372 378
373pPrint = putStrLn . ppShow 379pPrint = putStrLn . ppShow