diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-06-09 17:14:31 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-06-09 17:14:31 +0200 |
commit | d70ef80e63a0f84798071d31abbf80747c1fe639 (patch) | |
tree | 54f3d5dc021ad2e61a553fe711b17352d3f72a81 /prototypes/LamMachine.hs | |
parent | 57fd497f487b83d6eab1e2ca05e6ff5722af6938 (diff) |
use splay lists
Diffstat (limited to 'prototypes/LamMachine.hs')
-rw-r--r-- | prototypes/LamMachine.hs | 268 |
1 files changed, 107 insertions, 161 deletions
diff --git a/prototypes/LamMachine.hs b/prototypes/LamMachine.hs index d878bcb4..f5375d7f 100644 --- a/prototypes/LamMachine.hs +++ b/prototypes/LamMachine.hs | |||
@@ -7,7 +7,10 @@ | |||
7 | {-# LANGUAGE TypeSynonymInstances #-} | 7 | {-# LANGUAGE TypeSynonymInstances #-} |
8 | {-# LANGUAGE FlexibleInstances #-} | 8 | {-# LANGUAGE FlexibleInstances #-} |
9 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | 9 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} |
10 | {-# LANGUAGE MultiParamTypeClasses #-} | ||
11 | {-# LANGUAGE FlexibleContexts #-} | ||
10 | {-# LANGUAGE NoMonomorphismRestriction #-} | 12 | {-# LANGUAGE NoMonomorphismRestriction #-} |
13 | {-# LANGUAGE TypeFamilies #-} | ||
11 | 14 | ||
12 | module LamMachine where | 15 | module LamMachine where |
13 | 16 | ||
@@ -16,6 +19,8 @@ import Data.Word | |||
16 | import Data.Int | 19 | import Data.Int |
17 | import Data.Monoid | 20 | import Data.Monoid |
18 | import Data.Maybe | 21 | import Data.Maybe |
22 | --import Data.Foldable (toList) | ||
23 | import qualified SplayList as F | ||
19 | import Control.Arrow hiding ((<+>)) | 24 | import Control.Arrow hiding ((<+>)) |
20 | import Control.Category hiding ((.), id) | 25 | import Control.Category hiding ((.), id) |
21 | import Control.Monad | 26 | import Control.Monad |
@@ -28,20 +33,20 @@ import FreeVars | |||
28 | --------------------------------------------------------------------- data structures | 33 | --------------------------------------------------------------------- data structures |
29 | 34 | ||
30 | data Lit | 35 | data Lit |
31 | = LInt !Int | 36 | = LInt !Int |
32 | | LChar !Char | 37 | | LChar !Char |
33 | | LFloat !Double | 38 | | LFloat !Double |
34 | deriving Eq | 39 | deriving Eq |
35 | 40 | ||
36 | data Exp_ | 41 | data Exp |
37 | = Var_ | 42 | = Var_ !VarFV |
38 | | Lam_ VarInfo !Exp | 43 | | Lam_ VarInfo !(FVCache FV (LamFV Exp)) |
39 | | Let_ VarInfo !Exp !Exp | 44 | | Let_ VarInfo !(FVCache FV (Exp, LamFV Exp)) |
40 | | Con_ ConInfo !Int [Exp] | 45 | | Con_ ConInfo !Int (FVCache FV [Exp]) |
41 | | Case_ CaseInfo !Exp ![Exp] | 46 | | Case_ CaseInfo !(FVCache FV (Exp, [Exp])) |
42 | | Lit_ !Lit | 47 | | Lit !Lit |
43 | | Op1_ !Op1 !Exp | 48 | | Op1 !Op1 !Exp |
44 | | Op2_ !Op2 !Exp !Exp | 49 | | Op2_ !Op2 !(FVCache FV (Exp, Exp)) |
45 | deriving (Eq, Show) | 50 | deriving (Eq, Show) |
46 | 51 | ||
47 | data Op1 = HNFMark | YOp | Round | 52 | data Op1 = HNFMark | YOp | Round |
@@ -55,29 +60,54 @@ pattern App a b = Op2 AppOp a b | |||
55 | pattern Seq a b = Op2 SeqOp a b | 60 | pattern Seq a b = Op2 SeqOp a b |
56 | pattern Int i = Lit (LInt i) | 61 | pattern Int i = Lit (LInt i) |
57 | 62 | ||
58 | infixl 4 `App` | 63 | pattern Var i = Var_ (VarFV i) |
64 | pattern Lam n i = Lam_ n (CacheFV (LamFV i)) | ||
65 | pattern Let n i x <- Let_ n (CacheFV (i, LamFV x)) | ||
66 | where Let _ _ (down 0 -> Just x) = x | ||
67 | Let n i x = Let_ n (CacheFV (i, LamFV x)) | ||
68 | pattern Con a b i = Con_ a b (CacheFV i) | ||
69 | pattern Case a b c = Case_ a (CacheFV (b, c)) | ||
70 | pattern Op2 op a b = Op2_ op (CacheFV (a, b)) | ||
59 | 71 | ||
60 | -- cached and compressed free variables set | 72 | infixl 4 `App` |
61 | data Exp = Exp !FV !Exp_ | ||
62 | deriving (Eq, Show) | ||
63 | 73 | ||
64 | data EnvPiece | 74 | data EnvPiece |
65 | = ELet VarInfo !Exp | 75 | = ELet_ VarInfo !Exp |
66 | | EDLet1 VarInfo !DExp | 76 | | EDLet1_ VarInfo !(LamFV DExp) |
67 | | ECase_ CaseInfo !FV ![Exp] | 77 | | ECase_ CaseInfo !(FVCache FV [Exp]) |
68 | | EOp2_1 !Op2 !Exp | 78 | | EOp2_1_ !Op2 !Exp |
69 | | EOp2_2 !Op2 !Exp | 79 | | EOp2_2_ !Op2 !Exp |
70 | deriving (Eq, Show) | 80 | deriving (Eq, Show) |
71 | 81 | ||
72 | data DExp | 82 | pattern ELet i x = ELet_ i x |
73 | = DExpNil -- variable | 83 | pattern EDLet1 i x = EDLet1_ i (LamFV x) |
74 | | DExpCons_ !FV !EnvPiece !DExp | 84 | pattern ECase op b = ECase_ op (CacheFV b) |
75 | deriving (Eq, Show) | 85 | pattern EOp2_1 i x = EOp2_1_ i x |
86 | pattern EOp2_2 i x = EOp2_2_ i x | ||
76 | 87 | ||
77 | data DEnv | 88 | type DEnv = F.SplayList EnvPiece |
78 | = DEnvNil | 89 | |
79 | | DEnvCons !EnvPiece !DEnv | 90 | pattern DEnvNil = F.Nil |
80 | deriving (Eq, Show) | 91 | |
92 | pattern DEnvCons :: EnvPiece -> DEnv -> DEnv | ||
93 | pattern DEnvCons p e = F.Cons p e | ||
94 | |||
95 | instance F.Measured EnvPiece where | ||
96 | type Measure EnvPiece = Nat | ||
97 | measure = \case | ||
98 | ELet_{} -> 1 | ||
99 | _ -> 0 | ||
100 | --instance HasSFV DExp where | ||
101 | |||
102 | |||
103 | type DExp = DEnv | ||
104 | |||
105 | pattern DExpNil = DEnvNil | ||
106 | |||
107 | pattern DExpCons :: EnvPiece -> DExp -> DExp | ||
108 | pattern DExpCons p e = F.Snoc e p | ||
109 | |||
110 | infixr 4 `DEnvCons`, `DExpCons` | ||
81 | 111 | ||
82 | -- state of the machine | 112 | -- state of the machine |
83 | data MSt = MSt !DEnv !Exp | 113 | data MSt = MSt !DEnv !Exp |
@@ -89,122 +119,40 @@ type VarInfo = String | |||
89 | type ConInfo = String | 119 | type ConInfo = String |
90 | type CaseInfo = [(String, [String])] | 120 | type CaseInfo = [(String, [String])] |
91 | 121 | ||
92 | --------------------------------------------------------------------- pattern synonyms | 122 | --------------------------------------------------------------------- instances |
93 | 123 | ||
94 | infixr 4 `DEnvCons`, `DExpCons` | 124 | instance HasFV Exp where |
125 | fvLens = \case | ||
126 | Op1 op e -> appLens (Op1 op) e | ||
127 | Op2_ op e -> appLens (Op2_ op) e | ||
128 | Con_ x op e -> appLens (Con_ x op) e | ||
129 | Case_ op e -> appLens (Case_ op) e | ||
130 | Let_ op e -> appLens (Let_ op) e | ||
131 | Lam_ op e -> appLens (Lam_ op) e | ||
132 | Var_ e -> appLens Var_ e | ||
133 | Lit l -> lConst $ Lit l | ||
134 | |||
135 | instance HasFV EnvPiece where | ||
136 | fvLens = \case | ||
137 | EOp2_1_ op e -> appLens (EOp2_1_ op) e | ||
138 | EOp2_2_ op e -> appLens (EOp2_2_ op) e | ||
139 | ECase_ op e -> appLens (ECase_ op) e | ||
140 | ELet_ op e -> appLens (ELet_ op) e | ||
141 | EDLet1_ op e -> appLens (EDLet1_ op) e | ||
95 | 142 | ||
96 | pattern ECase op e <- ECase_ op u (map (upp u) -> e) | 143 | --------------------------- |
97 | where ECase op b = ECase_ op u bz where (u, bz) = deltas b | ||
98 | |||
99 | pattern EnvPiece sfv p <- (getEnvPiece -> (sfv, p)) | ||
100 | where EnvPiece sfv@(SFV n u') = \case | ||
101 | EOp2_1 op e -> EOp2_1 op (uppS sfv e) | ||
102 | EOp2_2 op e -> EOp2_2 op (uppS sfv e) | ||
103 | ECase_ s u es -> ECase_ s (expand u' u) es | ||
104 | ELet n (Exp u e) -> ELet n (Exp (sDrop 1 u' `expand` u) e) | ||
105 | EDLet1 n z -> EDLet1 n $ uppDE u' 1 z | ||
106 | |||
107 | getEnvPiece = \case | ||
108 | EOp2_1 op (Exp u e) -> (SFV 0 u, EOp2_1 op (Exp (selfContract u) e)) | ||
109 | EOp2_2 op (Exp u e) -> (SFV 0 u, EOp2_2 op (Exp (selfContract u) e)) | ||
110 | ECase_ s u es -> (SFV 0 u, ECase_ s (selfContract u) es) | ||
111 | ELet n (Exp u e) -> (SFV 1 $ fv 1 0 u, ELet n $ Exp (selfContract u) e) | ||
112 | EDLet1 n DExpNil -> (mempty, EDLet1 n DExpNil) | ||
113 | EDLet1 n (DExpCons_ u x y) -> (SFV 0 $ sDrop 1 u, EDLet1 n $ DExpCons_ (onTail selfContract 1 u) x y) | ||
114 | |||
115 | uppS (SFV _ x) (Exp u a) = Exp (expand x u) a | ||
116 | |||
117 | pattern DExpCons :: EnvPiece -> DExp -> DExp | ||
118 | pattern DExpCons a b <- (getDExpCons -> (a, b)) | ||
119 | where DExpCons (EnvPiece ux a) DExpNil = DExpCons_ (fromSFV s) (EnvPiece ux' a) DExpNil | ||
120 | where (s, D1 ux') = diffT $ D1 ux | ||
121 | DExpCons (ELet _ a) (dDown 0 -> Just d) = d | ||
122 | DExpCons (EnvPiece ux a) (DExpCons_ u x y) = DExpCons_ (fromSFV s) (EnvPiece ux' a) (DExpCons_ u' x y) | ||
123 | where (s, D2 (SFV 0 u') ux') = diffT $ D2 (SFV 0 u) ux | ||
124 | |||
125 | getDExpCons (DExpCons_ u x@(EnvPiece (SFV n _) _) b) = (uppEP u x, uppDE u n b) | ||
126 | |||
127 | uppEP u (EnvPiece (SFV n x) y) = EnvPiece (SFV n $ onTail (u `expand`) n x) y | ||
128 | |||
129 | upP i j = uppEP $ mkUp i j | ||
130 | |||
131 | incFV' (SFV n u) = SFV (n + 1) $ incFV u | ||
132 | |||
133 | uppDE :: FV -> Nat -> DExp -> DExp | ||
134 | uppDE a _ DExpNil = DExpNil | ||
135 | uppDE a n (DExpCons_ u x y) = DExpCons_ (onTail (expand a) n u) x y | ||
136 | |||
137 | dDown i DExpNil = Just DExpNil | ||
138 | dDown i (DExpCons_ u a b) = DExpCons_ <$> downFV i u <*> pure a <*> pure b | ||
139 | |||
140 | --------------------------------------------------------------------- | ||
141 | |||
142 | pattern Lit i <- Exp _ (Lit_ i) | ||
143 | where Lit i = Exp mempty $ Lit_ i | ||
144 | pattern Op2 op a b <- Exp u (Op2_ op (upp u -> a) (upp u -> b)) | ||
145 | where Op2 op a b = Exp s $ Op2_ op az bz where (s, az, bz) = delta2 a b | ||
146 | pattern Op1 op a <- Exp u (Op1_ op (upp u -> a)) | ||
147 | where Op1 op (Exp ab x) = Exp ab $ Op1_ op $ Exp (selfContract ab) x | ||
148 | pattern Var' i = Exp (VarFV i) Var_ | ||
149 | pattern Var i = Var' i | ||
150 | pattern Lam n i <- Exp u (Lam_ n (upp (incFV u) -> i)) | ||
151 | where Lam n (Exp vs ax) = Exp (sDrop 1 vs) $ Lam_ n $ Exp (compact vs) ax | ||
152 | pattern Con a b i <- Exp u (Con_ a b (map (upp u) -> i)) | ||
153 | where Con a b x = Exp s $ Con_ a b bz where (s, bz) = deltas x | ||
154 | pattern Case a b c <- Exp u (Case_ a (upp u -> b) (map (upp u) -> c)) | ||
155 | where Case cn a b = Exp s $ Case_ cn az bz where (s, az: bz) = deltas $ a: b | ||
156 | |||
157 | pattern Let n i x <- Exp u (Let_ n (upp u -> i) (upp (incFV u) -> x)) | ||
158 | where Let _ _ (down 0 -> Just x) = x | ||
159 | Let n a b = Exp s (Let_ n a' b') where (s, a', Lam _ b') = delta2 a $ Lam n b | ||
160 | 144 | ||
161 | pattern InHNF a <- (getHNF -> Just a) | 145 | pattern InHNF a <- (getHNF -> Just a) |
162 | where InHNF a@Lit{} = a | 146 | where InHNF a@Lit{} = a |
163 | InHNF a@Lam{} = a | 147 | InHNF a@Lam{} = a |
164 | InHNF a@(Op1 HNFMark _) = a | 148 | InHNF a@(Op1 HNFMark _) = a |
165 | InHNF a = Op1 HNFMark a | 149 | InHNF a = Op1 HNFMark a |
166 | 150 | ||
167 | getHNF x = case x of | 151 | getHNF x = case x of |
168 | Lit{} -> Just x | 152 | Lit{} -> Just x |
169 | Lam{} -> Just x | 153 | Lam{} -> Just x |
170 | Op1 HNFMark a -> Just a | 154 | Op1 HNFMark a -> Just a |
171 | _ -> Nothing | 155 | _ -> Nothing |
172 | |||
173 | delta2 (Exp ua a) (Exp ub b) = (s, Exp ua' a, Exp ub' b) | ||
174 | where | ||
175 | (s, ua', ub') = delta2' ua ub | ||
176 | |||
177 | delta2' ua ub = (s, primContract s ua, primContract s ub) | ||
178 | where | ||
179 | s = ua <> ub | ||
180 | |||
181 | deltas [] = (mempty, []) | ||
182 | deltas [Exp x e] = (x, [Exp (selfContract x) e]) | ||
183 | deltas [Exp ua a, Exp ub b] = (s, [Exp (primContract s ua) a, Exp (primContract s ub) b]) | ||
184 | where | ||
185 | s = ua <> ub | ||
186 | deltas es = (s, [Exp (primContract s u) e | (u, Exp _ e) <- zip xs es]) | ||
187 | where | ||
188 | xs = [ue | Exp ue _ <- es] | ||
189 | |||
190 | s = mconcat xs | ||
191 | |||
192 | upp :: FV -> Exp -> Exp | ||
193 | upp a (Exp b e) = Exp (expand a b) e | ||
194 | |||
195 | up l n (Exp us x) = Exp (upFV l n us) x | ||
196 | |||
197 | -- free variables set | ||
198 | --fvs (Exp us _) = usedFVs us | ||
199 | |||
200 | usedVar i (Exp us _) = sIndex us i | ||
201 | |||
202 | usedVar' i DExpNil = False | ||
203 | usedVar' i (DExpCons_ u _ _) = sIndex u i | ||
204 | |||
205 | down i (Exp us e) = Exp <$> downFV i us <*> pure e | ||
206 | |||
207 | --------------------------- | ||
208 | 156 | ||
209 | initSt :: Exp -> MSt | 157 | initSt :: Exp -> MSt |
210 | initSt e = MSt DEnvNil e | 158 | initSt e = MSt DEnvNil e |
@@ -221,30 +169,27 @@ hnf = justResult . initSt | |||
221 | data StepTag = Begin String | End String | Step String | 169 | data StepTag = Begin String | End String | Step String |
222 | deriving Show | 170 | deriving Show |
223 | 171 | ||
224 | type GenSteps e | 172 | focusNotUsed (EDLet1 _ x `DEnvCons` _) = False --not $ usedVar 0 x |
225 | = e | ||
226 | -> (StepTag -> MSt -> e) | ||
227 | -> MSt -> e | ||
228 | |||
229 | focusNotUsed (MSt (EDLet1 _ x `DEnvCons` _) _) = not $ usedVar' 0 x | ||
230 | focusNotUsed _ = True | 173 | focusNotUsed _ = True |
231 | 174 | ||
232 | inHNF :: MSt -> Bool | 175 | inHNF :: MSt -> Bool |
233 | inHNF (MSt _ (InHNF _)) = True | 176 | inHNF (MSt _ (InHNF _)) = True |
234 | inHNF _ = False | 177 | inHNF _ = False |
235 | 178 | ||
236 | steps :: forall e . GenSteps e | 179 | steps :: forall e . e -> (StepTag -> MSt -> e) -> MSt -> e |
237 | steps nostep bind (MSt vs e) = case e of | 180 | steps nostep bind (MSt vs e) = case e of |
238 | 181 | ||
182 | e | EDLet1 _ (down 0 -> Just d) `DEnvCons` vs <- vs -> zipUp e vs d | ||
183 | |||
239 | Con cn i xs | 184 | Con cn i xs |
240 | | lz == 0 || focusNotUsed (MSt vs e) -> step (cn ++ " hnf") $ MSt vs $ InHNF e | 185 | | focusNotUsed vs || lz == 0 -> step (cn ++ " hnf") $ MSt vs $ InHNF e |
241 | | otherwise -> step (cn ++ " copy") $ MSt (foldr DEnvCons vs $ ELet "" <$> zs) $ InHNF $ Con cn i xs' -- share complex constructor arguments | 186 | | otherwise -> step (cn ++ " copy") $ MSt (foldr DEnvCons vs $ ELet "" <$> zs) $ InHNF $ Con cn i xs' -- share complex constructor arguments |
242 | where | 187 | where |
243 | lz = Nat $ length zs | 188 | lz = Nat $ length zs |
244 | (xs', concat -> zs) = unzip $ f 0 xs | 189 | (xs', concat -> zs) = unzip $ f 0 xs |
245 | f i [] = [] | 190 | f i [] = [] |
246 | f i (x: xs) | simple x = (up 0 lz x, []): f i xs | 191 | f i (x: xs) | simple x = (up 0 lz x, []): f i xs |
247 | | otherwise = (Var' i, [up 0 (lz - i - 1) x]): f (i+1) xs | 192 | | otherwise = (Var i, [up 0 (lz - i - 1) x]): f (i+1) xs |
248 | 193 | ||
249 | -- TODO: handle recursive constants | 194 | -- TODO: handle recursive constants |
250 | Y x -> step "Y" $ MSt vs $ App x (Y x) | 195 | Y x -> step "Y" $ MSt vs $ App x (Y x) |
@@ -257,14 +202,15 @@ steps nostep bind (MSt vs e) = case e of | |||
257 | 202 | ||
258 | DEnvNil -> nostep | 203 | DEnvNil -> nostep |
259 | 204 | ||
260 | ELet n x `DEnvCons` vs -> step "goUp" $ MSt vs $ InHNF $ Let n x $ InHNF a | 205 | ELet n x `DEnvCons` vs -> step "goUp" $ MSt vs $ InHNF $ Let n x $ InHNF a -- TODO: speed up? |
261 | x `DEnvCons` vs | Let n y e <- a -> step "pakol" $ MSt (upP 0 1 x `DEnvCons` ELet n y `DEnvCons` vs) e | 206 | x `DEnvCons` vs | Let n y e <- a -> step "pakol" $ MSt (up 0 1 x `DEnvCons` ELet n y `DEnvCons` vs) e -- TODO: speed up |
262 | x `DEnvCons` vs -> case x of | 207 | x `DEnvCons` vs -> case x of |
263 | ECase cns cs -> end "case" $ case a of | 208 | ECase cns cs -> end "case" $ case a of |
264 | Con cn i es -> MSt vs $ foldl App (cs !! i) es -- TODO: remove unused cases | 209 | Con cn i es -> MSt vs $ foldl App (cs !! i) es -- TODO: remove unused cases |
265 | _ -> MSt vs $ InHNF $ Case cns (InHNF a) cs | 210 | _ -> MSt vs $ InHNF $ Case cns (InHNF a) cs |
266 | EOp2_1 AppOp b -> end "AppOp" $ case a of | 211 | EOp2_1 AppOp b -> end "AppOp" $ case a of |
267 | Lam _ (down 0 -> Just x) -> MSt vs x -- TODO: remove b | 212 | Lam _ (down 0 -> Just x) -> MSt vs x -- TODO: remove b |
213 | -- TODO: optimize out Var 0 application | ||
268 | Lam n x -> MSt (ELet n b `DEnvCons` vs) x | 214 | Lam n x -> MSt (ELet n b `DEnvCons` vs) x |
269 | _ -> MSt vs $ InHNF $ App (InHNF a) b | 215 | _ -> MSt vs $ InHNF $ App (InHNF a) b |
270 | EOp2_1 SeqOp b -> end "SeqOp" $ case a of | 216 | EOp2_1 SeqOp b -> end "SeqOp" $ case a of |
@@ -282,17 +228,17 @@ steps nostep bind (MSt vs e) = case e of | |||
282 | int LessEq a b = if a <= b then T else F | 228 | int LessEq a b = if a <= b then T else F |
283 | int EqInt a b = if a == b then T else F | 229 | int EqInt a b = if a == b then T else F |
284 | _ -> MSt vs $ InHNF $ Op2 op b (InHNF a) | 230 | _ -> MSt vs $ InHNF $ Op2 op b (InHNF a) |
285 | EDLet1 _ (dDown 0 -> Just d) -> zipUp (InHNF a) vs d | ||
286 | EDLet1 n d -> zipUp (up 0 1 a) (ELet n (InHNF a) `DEnvCons` vs) d | 231 | EDLet1 n d -> zipUp (up 0 1 a) (ELet n (InHNF a) `DEnvCons` vs) d |
287 | 232 | ||
288 | where | 233 | where |
289 | zipDown 0 e (ELet n z `DEnvCons` zs) = MSt (EDLet1 n e `DEnvCons` zs) z | 234 | zipDown j e (F.split (> j) -> (t, ELet n z `DEnvCons` zs)) |
290 | zipDown j e (z@ELet{} `DEnvCons` zs) = zipDown (j-1) (z `DExpCons` e) zs | 235 | = MSt (EDLet1 n (f e t) `DEnvCons` zs) z |
291 | zipDown j e (z `DEnvCons` zs) = zipDown j (z `DExpCons` e) zs | 236 | where |
237 | f e t = t <> e | ||
292 | 238 | ||
293 | zipUp x xs DExpNil = end "var" $ MSt xs x | 239 | zipUp x xs e = end "var" $ MSt (e <> xs) $ up 0 n x |
294 | zipUp x xs (DExpCons a@ELet{} as) = zipUp (up 0 1 x) (a `DEnvCons` xs) as | 240 | where |
295 | zipUp x xs (DExpCons a as) = zipUp x (a `DEnvCons` xs) as | 241 | n = F.measure e |
296 | 242 | ||
297 | begin t = bind (Begin t) | 243 | begin t = bind (Begin t) |
298 | next t = bind (Step t) | 244 | next t = bind (Step t) |
@@ -318,7 +264,7 @@ class ViewShow a where | |||
318 | 264 | ||
319 | instance ViewShow Exp where | 265 | instance ViewShow Exp where |
320 | viewShow vi = pShow where | 266 | viewShow vi = pShow where |
321 | pShow e@(Exp fv x) = showFE green vi fv $ | 267 | pShow e = showFE green vi (fst $ fvLens e :: FV) $ |
322 | case {-if vi then Exp_ (selfContract fv) x else-} e of | 268 | case {-if vi then Exp_ (selfContract fv) x else-} e of |
323 | Var (Nat i) -> DVar i | 269 | Var (Nat i) -> DVar i |
324 | Let n a b -> shLet n (pShow a) $ pShow b | 270 | Let n a b -> shLet n (pShow a) $ pShow b |
@@ -366,7 +312,7 @@ instance ViewShow MSt where | |||
366 | EOp2_2 op x -> shOp2 op (pShow x) y | 312 | EOp2_2 op x -> shOp2 op (pShow x) y |
367 | 313 | ||
368 | h n DExpNil = onred $ white $ DVar n | 314 | h n DExpNil = onred $ white $ DVar n |
369 | h n z@(DExpCons p (h (adj p n) -> y)) = showFE blue vi (case p of EnvPiece f _ -> f) $ case p of | 315 | h n z@(DExpCons p (h (adj p n) -> y)) = showFE blue vi (fst $ fvLens p :: FV) $ case p of |
370 | EDLet1 n x -> shLet n y (h 0 x) | 316 | EDLet1 n x -> shLet n y (h 0 x) |
371 | ELet n x -> shLet n (pShow x) y | 317 | ELet n x -> shLet n (pShow x) y |
372 | ECase cns xs -> shCase cns y pShow xs | 318 | ECase cns xs -> shCase cns y pShow xs |
@@ -446,7 +392,7 @@ sum' = Y $ Lam "sum" $ Lam "xs" $ caseList (Var 0) (Int 0) $ Lam "y" $ Lam "ys" | |||
446 | 392 | ||
447 | infixl 4 `sApp` | 393 | infixl 4 `sApp` |
448 | 394 | ||
449 | sApp a b = Seq b (App a b) | 395 | sApp a b = Lam "s" (Seq (Var 0) (a `App` Var 0)) `App` b |
450 | 396 | ||
451 | {- | 397 | {- |
452 | accsum acc [] = acc | 398 | accsum acc [] = acc |
@@ -477,14 +423,14 @@ inc = Lam "n" $ Op2 Add (Var 0) (Int 1) | |||
477 | test'' = Lam "f" (Int 4) `App` Int 3 | 423 | test'' = Lam "f" (Int 4) `App` Int 3 |
478 | 424 | ||
479 | twiceTest n = (Lam "twice" $ (iterate (`App` Var 0) (Var 0) !! n) `App` inc `App` Int 0) `App` twice | 425 | twiceTest n = (Lam "twice" $ (iterate (`App` Var 0) (Var 0) !! n) `App` inc `App` Int 0) `App` twice |
480 | twiceTest2 = twice2 `App` twice2 `App` twice2 `App` twice2 `App` inc `App` Int 0 | 426 | twiceTest2 n = (Lam "twice" $ (iterate (`App` Var 0) (Var 0) !! n) `App` inc `App` Int 0) `App` twice2 |
481 | 427 | ||
482 | tests | 428 | tests |
483 | = hnf test == hnf (Int 13) | 429 | = hnf test == hnf (Int 13) |
484 | && hnf test' == hnf (Int 14) | 430 | && hnf test' == hnf (Int 14) |
485 | && hnf test'' == hnf (Int 4) | 431 | && hnf test'' == hnf (Int 4) |
486 | && hnf (t' 10) == hnf (Int 55) | 432 | && hnf (t' 10) == hnf (Int 55) |
487 | && hnf (t'' 10) == hnf (Int 55) | 433 | -- && hnf (t'' 10) == hnf (Int 55) |
488 | 434 | ||
489 | 435 | ||
490 | 436 | ||