diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-06-10 00:14:50 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-06-10 00:14:50 +0200 |
commit | 52db01104aa7bb2500e46d77f246aa321650d231 (patch) | |
tree | 02e11ed1aab391779f74e619f0c16f70468f3dfa /prototypes | |
parent | d70ef80e63a0f84798071d31abbf80747c1fe639 (diff) |
self adjusting DeBruijn variables
Diffstat (limited to 'prototypes')
-rw-r--r-- | prototypes/FreeVars.hs | 120 | ||||
-rw-r--r-- | prototypes/LamMachine.hs | 26 | ||||
-rw-r--r-- | prototypes/SplayList.hs | 16 |
3 files changed, 77 insertions, 85 deletions
diff --git a/prototypes/FreeVars.hs b/prototypes/FreeVars.hs index 71c0a99c..f0fb9a4b 100644 --- a/prototypes/FreeVars.hs +++ b/prototypes/FreeVars.hs | |||
@@ -21,10 +21,13 @@ module FreeVars | |||
21 | ( SFV(..), FV, Nat(..), pattern Nat | 21 | ( SFV(..), FV, Nat(..), pattern Nat |
22 | , HasFV (..), Lens | 22 | , HasFV (..), Lens |
23 | , up, down, usedVar, appLens, lComp, lConst | 23 | , up, down, usedVar, appLens, lComp, lConst |
24 | , FVCache(..), LamFV(..), VarFV(..) | 24 | , Pair, pattern Pair |
25 | , pattern CacheFV | 25 | , List, pattern List |
26 | , LamFV, pattern LamFV | ||
27 | , VarFV(..) | ||
26 | 28 | ||
27 | , expand, fv, primContract | 29 | , ChangeFV(..) |
30 | , expand'', expand''' | ||
28 | ) | 31 | ) |
29 | where | 32 | where |
30 | 33 | ||
@@ -78,14 +81,6 @@ instance Show Nat where show = ppShow | |||
78 | 81 | ||
79 | ---------------------------------------------------------------------------------- | 82 | ---------------------------------------------------------------------------------- |
80 | 83 | ||
81 | -- Expand monoid | ||
82 | newtype Expand = Expand {getExpand :: FV} | ||
83 | deriving (Eq, Show, Arbitrary) | ||
84 | |||
85 | instance Monoid Expand where | ||
86 | mempty = Expand $ fO 0 | ||
87 | Expand a `mappend` Expand b = Expand (a `expand` b) | ||
88 | |||
89 | {- | 84 | {- |
90 | diffTest u = u == uncurry expandT (diffT u) | 85 | diffTest u = u == uncurry expandT (diffT u) |
91 | 86 | ||
@@ -157,6 +152,8 @@ fromBools [] = FE | |||
157 | fromBools (True: bs) = fv 0 1 $ fromBools bs | 152 | fromBools (True: bs) = fv 0 1 $ fromBools bs |
158 | fromBools (False: bs) = fv 1 0 $ fromBools bs | 153 | fromBools (False: bs) = fv 1 0 $ fromBools bs |
159 | 154 | ||
155 | fromStr = fromBools . map (=='1') | ||
156 | |||
160 | instance Monoid FV where | 157 | instance Monoid FV where |
161 | mempty = FE | 158 | mempty = FE |
162 | 159 | ||
@@ -173,34 +170,6 @@ instance Monoid FV where | |||
173 | prop_monoid_FV = prop_Monoid (T :: T FV) | 170 | prop_monoid_FV = prop_Monoid (T :: T FV) |
174 | prop_mappend_normal_FV (a :: FV) b = testNormalFV (a <> b) | 171 | prop_mappend_normal_FV (a :: FV) b = testNormalFV (a <> b) |
175 | 172 | ||
176 | expand :: FV -> FV -> FV | ||
177 | expand _ FE = FE | ||
178 | expand FE _ = FE | ||
179 | expand (FV a b xs) (FV c d ys) | ||
180 | | c + d <= b = fv (a + c) d $ expand (FV 0 (b - (c + d)) xs) ys | ||
181 | | c <= b = fv (a + c) (b - c) $ expand xs (FV 0 (d - (b - c)) ys) | ||
182 | | otherwise = fv (a + b) 0 $ expand xs (FV (c - b) d ys) | ||
183 | |||
184 | primContract :: FV -> FV -> FV | ||
185 | primContract x FE = FE | ||
186 | primContract (FV a' b' us') (FV a b us) | ||
187 | | a' + b' <= a = fv b' 0 $ primContract us' (FV (a - (a' + b')) b us) | ||
188 | | otherwise = fv (a - a') b $ primContract (FV 0 ((a' + b') - (a + b)) us') us | ||
189 | |||
190 | contract :: FV -> FV -> FV | ||
191 | contract a b = primContract (a <> b) a | ||
192 | |||
193 | -- selfContract a = primContract a a | ||
194 | selfContract :: FV -> FV | ||
195 | selfContract xs = fv 0 (altersum xs) FE | ||
196 | where | ||
197 | altersum FE = 0 | ||
198 | altersum (FV _ a cs) = a + altersum cs | ||
199 | |||
200 | prop_expand_normal_FV (a :: FV) b = testNormalFV (expand a b) | ||
201 | prop_contract_normal_FV (a :: FV) b = testNormalFV (contract a b) | ||
202 | |||
203 | prop_monoid_Expand_FV = prop_Monoid (T :: T Expand) | ||
204 | {- | 173 | {- |
205 | --prop_diffTestFV1 = diffTest :: D1 FV -> Bool | 174 | --prop_diffTestFV1 = diffTest :: D1 FV -> Bool |
206 | --prop_diffTestFV2 = diffTest :: D2 FV -> Bool | 175 | --prop_diffTestFV2 = diffTest :: D2 FV -> Bool |
@@ -210,6 +179,32 @@ prop_monoid_Expand_FV = prop_Monoid (T :: T Expand) | |||
210 | --prop_assocTestR_FV = assocTestR :: D3 FV -> Bool | 179 | --prop_assocTestR_FV = assocTestR :: D3 FV -> Bool |
211 | -} | 180 | -} |
212 | 181 | ||
182 | propagateChange :: FV -> FV -> FV -> FV | ||
183 | propagateChange _ _ FE = FE | ||
184 | propagateChange new@(FV nn ln un) old@(FV no lo uo) x | ||
185 | = fv nn 0 $ onTail (propagateChange (fv 0 (ln - l) un) (fv 0 (lo - l) uo)) l $ sDrop no x | ||
186 | where | ||
187 | l = min ln lo | ||
188 | |||
189 | {- | ||
190 | prop_propagate a b = do | ||
191 | let ab = a <> b | ||
192 | FV _ n _ = selfContract ab | ||
193 | gs <- replicateM n arbitrary | ||
194 | let ab' = foldr (\n -> fv n 1) FE gs | ||
195 | return $ | ||
196 | -} | ||
197 | |||
198 | data ChangeFV | ||
199 | = Same !FV | ||
200 | | Changed !FV !FV -- new old | ||
201 | deriving (Eq, Show) | ||
202 | |||
203 | instance HasFV ChangeFV where | ||
204 | fvLens = \case | ||
205 | Same u -> (u, \s -> if s == u then Same u else Changed s u) | ||
206 | Changed n o -> (n, \s -> if s == o then Same o else Changed s o) | ||
207 | |||
213 | sIndex :: FV -> Nat -> Bool | 208 | sIndex :: FV -> Nat -> Bool |
214 | sIndex FE i = False | 209 | sIndex FE i = False |
215 | sIndex (FV n l us) i = i >= n && (i < l || sIndex us (i - l - n)) | 210 | sIndex (FV n l us) i = i >= n && (i < l || sIndex us (i - l - n)) |
@@ -281,42 +276,44 @@ usedVar i x = sIndex (fst $ fvLens x :: FV) i | |||
281 | instance HasFV FV where | 276 | instance HasFV FV where |
282 | fvLens = lId | 277 | fvLens = lId |
283 | 278 | ||
284 | instance (HasFV a, HasFV b) => HasFV (a, b) where | 279 | data Pair a b = Pair_ !ChangeFV !a !b |
285 | fvLens (fvLens -> (ux, x'), fvLens -> (uy, y')) = (s, \s' -> (x' $ s' `expand` primContract s ux, y' $ s' `expand` primContract s uy)) where | 280 | deriving (Eq, Show) |
286 | s = ux <> uy | 281 | |
282 | instance (HasFV a, HasFV b) => HasFV (Pair a b) where | ||
283 | fvLens (Pair_ (fvLens -> ~(s, f)) a b) = (s, \s -> Pair_ (f s) a b) | ||
284 | |||
285 | pattern Pair a b <- Pair_ s (expand'' s -> a) (expand'' s -> b) | ||
286 | where Pair a@(fvLens -> ~(ux, _)) b@(fvLens -> ~(uy, _)) = Pair_ (Same $ ux <> uy) a b | ||
287 | 287 | ||
288 | --prop_HasFV_D2 (a :: FV) b = contractFV (D2 a b) == diffT (D2 a b) | 288 | --prop_HasFV_D2 (a :: FV) b = contractFV (D2 a b) == diffT (D2 a b) |
289 | --prop_HasFV_D2' (s :: FV) a b = expandFV s (D2 a b) == expandT s (D2 a b) | 289 | --prop_HasFV_D2' (s :: FV) a b = expandFV s (D2 a b) == expandT s (D2 a b) |
290 | 290 | ||
291 | newtype LamFV u = LamFV {getLamFV :: u} | 291 | data LamFV u = LamFV_ !ChangeFV u |
292 | deriving (Eq, Show) | 292 | deriving (Eq, Show) |
293 | 293 | ||
294 | instance HasFV a => HasFV (LamFV a) where | 294 | instance HasFV a => HasFV (LamFV a) where |
295 | fvLens (LamFV (fvLens -> x@(ux, fx))) = (sDrop 1 ux, \s -> LamFV $ lApp' (onTail (const s) 1) x) | 295 | fvLens (LamFV_ (fvLens -> (s, f)) x) = (s, \s -> LamFV_ (f s) x) |
296 | |||
297 | pattern LamFV a <- LamFV_ s (expand''' 1 s -> a) | ||
298 | where LamFV a@(fvLens -> ~(ux, _)) = LamFV_ (Same $ sDrop 1 ux) a | ||
296 | 299 | ||
297 | --prop_LamFV (u :: FV) = contractFV (LamFV u) == (sDrop 1 u, LamFV $ onTail selfContract 1 u) | 300 | --prop_LamFV (u :: FV) = contractFV (LamFV u) == (sDrop 1 u, LamFV $ onTail selfContract 1 u) |
298 | --prop_LamFV' s (u :: FV) = expandFV s (LamFV u) == LamFV (fv 0 1 s `expand` u) | 301 | --prop_LamFV' s (u :: FV) = expandFV s (LamFV u) == LamFV (fv 0 1 s `expand` u) |
299 | 302 | ||
300 | data FVCache a b = FVCache !a !b | 303 | expand'' Same{} x = x |
301 | deriving (Eq, Show) | 304 | expand'' (Changed new old) (fvLens -> (x, f)) = f $ propagateChange new old x |
302 | 305 | ||
303 | instance Functor (FVCache a) where | 306 | expand''' _ Same{} x = x |
304 | fmap f (FVCache a b) = FVCache a (f b) | 307 | expand''' n (Changed new old) (fvLens -> (x, f)) = f $ onTail (propagateChange new old) n x |
305 | 308 | ||
306 | instance HasFV (FVCache FV x) where | 309 | data List a = List_ !ChangeFV [a] |
307 | fvLens (FVCache u x) = (u, \u -> FVCache u x) | 310 | deriving (Eq, Show) |
308 | 311 | ||
309 | pattern CacheFV a <- FVCache u (lApp' (expand u) . fvLens -> a) | 312 | instance HasFV a => HasFV (List a) where |
310 | where CacheFV (fvLens -> x@(ua, fa)) = FVCache ua $ lApp' selfContract x | 313 | fvLens (List_ (fvLens -> (s, f)) xs) = (s, \s -> List_ (f s) xs) |
311 | 314 | ||
312 | instance HasFV a => HasFV [a] where | 315 | pattern List a <- List_ s (map (expand'' s) -> a) |
313 | fvLens [] = lConst [] | 316 | where List a = List_ (Same $ mconcat $ map (fst . fvLens) a) a |
314 | fvLens [fvLens -> (u, fx)] = (u, \s -> [fx s]) | ||
315 | fvLens [fvLens -> (u, fx), fvLens -> (u', fy)] = (s, \s' -> [fx $ s' `expand` primContract s u, fy $ s' `expand` primContract s u' ]) | ||
316 | where | ||
317 | s = u <> u' | ||
318 | fvLens (unzip . map fvLens -> (us, fs)) = let | ||
319 | s = mconcat us in (s, \s -> zipWith (\f u -> f $ s `expand` primContract s u) fs us) | ||
320 | 317 | ||
321 | newtype VarFV = VarFV Nat | 318 | newtype VarFV = VarFV Nat |
322 | deriving (Eq, Show) | 319 | deriving (Eq, Show) |
@@ -341,9 +338,10 @@ instance Monoid SFV where | |||
341 | SFV m b `mappend` SFV n a = SFV (n + m) $ sDrop n b <> a | 338 | SFV m b `mappend` SFV n a = SFV (n + m) $ sDrop n b <> a |
342 | 339 | ||
343 | prop_monoid_SFV = prop_Monoid (T :: T SFV) | 340 | prop_monoid_SFV = prop_Monoid (T :: T SFV) |
344 | 341 | {- | |
345 | instance HasFV SFV where | 342 | instance HasFV SFV where |
346 | fvLens (SFV n u) = (u, SFV n) | 343 | fvLens (SFV n u) = (u, SFV n) |
344 | -} | ||
347 | {- | 345 | {- |
348 | --prop_diffTest1_SFV = diffTest :: D1 SFV -> Bool | 346 | --prop_diffTest1_SFV = diffTest :: D1 SFV -> Bool |
349 | --prop_diffTest2_SFV = diffTest :: D2 SFV -> Bool | 347 | --prop_diffTest2_SFV = diffTest :: D2 SFV -> Bool |
diff --git a/prototypes/LamMachine.hs b/prototypes/LamMachine.hs index f5375d7f..19928c3a 100644 --- a/prototypes/LamMachine.hs +++ b/prototypes/LamMachine.hs | |||
@@ -40,13 +40,13 @@ data Lit | |||
40 | 40 | ||
41 | data Exp | 41 | data Exp |
42 | = Var_ !VarFV | 42 | = Var_ !VarFV |
43 | | Lam_ VarInfo !(FVCache FV (LamFV Exp)) | 43 | | Lam_ VarInfo !(LamFV Exp) |
44 | | Let_ VarInfo !(FVCache FV (Exp, LamFV Exp)) | 44 | | Let_ VarInfo !(Pair Exp (LamFV Exp)) |
45 | | Con_ ConInfo !Int (FVCache FV [Exp]) | 45 | | Con_ ConInfo !Int (List Exp) |
46 | | Case_ CaseInfo !(FVCache FV (Exp, [Exp])) | 46 | | Case_ CaseInfo !(Pair Exp (List Exp)) |
47 | | Lit !Lit | 47 | | Lit !Lit |
48 | | Op1 !Op1 !Exp | 48 | | Op1 !Op1 !Exp |
49 | | Op2_ !Op2 !(FVCache FV (Exp, Exp)) | 49 | | Op2_ !Op2 !(Pair Exp Exp) |
50 | deriving (Eq, Show) | 50 | deriving (Eq, Show) |
51 | 51 | ||
52 | data Op1 = HNFMark | YOp | Round | 52 | data Op1 = HNFMark | YOp | Round |
@@ -61,27 +61,27 @@ pattern Seq a b = Op2 SeqOp a b | |||
61 | pattern Int i = Lit (LInt i) | 61 | pattern Int i = Lit (LInt i) |
62 | 62 | ||
63 | pattern Var i = Var_ (VarFV i) | 63 | pattern Var i = Var_ (VarFV i) |
64 | pattern Lam n i = Lam_ n (CacheFV (LamFV i)) | 64 | pattern Lam n i = Lam_ n (LamFV i) |
65 | pattern Let n i x <- Let_ n (CacheFV (i, LamFV x)) | 65 | pattern Let n i x <- Let_ n (Pair i (LamFV x)) |
66 | where Let _ _ (down 0 -> Just x) = x | 66 | where Let _ _ (down 0 -> Just x) = x |
67 | Let n i x = Let_ n (CacheFV (i, LamFV x)) | 67 | Let n i x = Let_ n (Pair i (LamFV x)) |
68 | pattern Con a b i = Con_ a b (CacheFV i) | 68 | pattern Con a b i = Con_ a b (List i) |
69 | pattern Case a b c = Case_ a (CacheFV (b, c)) | 69 | pattern Case a b c = Case_ a (Pair b (List c)) |
70 | pattern Op2 op a b = Op2_ op (CacheFV (a, b)) | 70 | pattern Op2 op a b = Op2_ op (Pair a b) |
71 | 71 | ||
72 | infixl 4 `App` | 72 | infixl 4 `App` |
73 | 73 | ||
74 | data EnvPiece | 74 | data EnvPiece |
75 | = ELet_ VarInfo !Exp | 75 | = ELet_ VarInfo !Exp |
76 | | EDLet1_ VarInfo !(LamFV DExp) | 76 | | EDLet1_ VarInfo !(LamFV DExp) |
77 | | ECase_ CaseInfo !(FVCache FV [Exp]) | 77 | | ECase_ CaseInfo !(List Exp) |
78 | | EOp2_1_ !Op2 !Exp | 78 | | EOp2_1_ !Op2 !Exp |
79 | | EOp2_2_ !Op2 !Exp | 79 | | EOp2_2_ !Op2 !Exp |
80 | deriving (Eq, Show) | 80 | deriving (Eq, Show) |
81 | 81 | ||
82 | pattern ELet i x = ELet_ i x | 82 | pattern ELet i x = ELet_ i x |
83 | pattern EDLet1 i x = EDLet1_ i (LamFV x) | 83 | pattern EDLet1 i x = EDLet1_ i (LamFV x) |
84 | pattern ECase op b = ECase_ op (CacheFV b) | 84 | pattern ECase op b = ECase_ op (List b) |
85 | pattern EOp2_1 i x = EOp2_1_ i x | 85 | pattern EOp2_1 i x = EOp2_1_ i x |
86 | pattern EOp2_2 i x = EOp2_2_ i x | 86 | pattern EOp2_2 i x = EOp2_2_ i x |
87 | 87 | ||
diff --git a/prototypes/SplayList.hs b/prototypes/SplayList.hs index fee995ed..2cee0699 100644 --- a/prototypes/SplayList.hs +++ b/prototypes/SplayList.hs | |||
@@ -36,7 +36,7 @@ import FreeVars | |||
36 | data SplayList a | 36 | data SplayList a |
37 | = Nil | 37 | = Nil |
38 | | Singleton a | 38 | | Singleton a |
39 | | Append_ !(Measure a) !FV (SplayList a) (SplayList a) | 39 | | Append_ !(Measure a) !ChangeFV (SplayList a) (SplayList a) |
40 | deriving (Typeable) | 40 | deriving (Typeable) |
41 | 41 | ||
42 | deriving instance (Show a, Show (Measure a)) => Show (SplayList a) | 42 | deriving instance (Show a, Show (Measure a)) => Show (SplayList a) |
@@ -66,7 +66,7 @@ instance (Measured a, HasFV a) => HasFV (SplayList a) where | |||
66 | fvLens = \case | 66 | fvLens = \case |
67 | Nil -> lConst Nil | 67 | Nil -> lConst Nil |
68 | Singleton (fvLens -> (s, f)) -> (s, Singleton . f) | 68 | Singleton (fvLens -> (s, f)) -> (s, Singleton . f) |
69 | Append_ n s l r -> (s, \s -> Append_ n s l r) | 69 | Append_ n (fvLens -> (s, f)) l r -> (s, \s -> Append_ n (f s) l r) |
70 | 70 | ||
71 | instance (Measured a) => Measured (SplayList a) where | 71 | instance (Measured a) => Measured (SplayList a) where |
72 | type Measure (SplayList a) = Measure a | 72 | type Measure (SplayList a) = Measure a |
@@ -76,20 +76,14 @@ instance (Measured a) => Measured (SplayList a) where | |||
76 | 76 | ||
77 | -------------------------------------------- | 77 | -------------------------------------------- |
78 | 78 | ||
79 | getAppend (Append_ _ s x@(fvLens -> ~(ux, x')) z@(fvLens -> ~(uz, z'))) | 79 | getAppend (Append_ _ s x z) = Just (expand''' (measure z) s x, expand'' s z) |
80 | = Just (x' $ fv 0 (measure z) s `expand` ux, z' $ s `expand` uz) | ||
81 | getAppend _ = Nothing | 80 | getAppend _ = Nothing |
82 | 81 | ||
83 | pattern Append :: () => Measured a => SplayList a -> SplayList a -> SplayList a | 82 | pattern Append :: () => Measured a => SplayList a -> SplayList a -> SplayList a |
84 | pattern Append l r <- (getAppend -> Just (l, r)) | 83 | pattern Append l r <- (getAppend -> Just (l, r)) |
85 | where Append x@(fvLens -> (ux, x')) z@(fvLens -> (uz, z')) = Append_ us s (x' ux') (z' uz') | 84 | where Append x@(fvLens -> (ux, _)) z@(fvLens -> (uz, _)) = Append_ us (Same s) x z |
86 | where | 85 | where |
87 | SFV us s = SFV (measure x) ux <> SFV mz uz | 86 | SFV us s = SFV (measure x) ux <> SFV (measure z) uz |
88 | |||
89 | mz = measure z | ||
90 | |||
91 | ux' = fv 0 mz s `primContract` ux | ||
92 | uz' = s `primContract` uz | ||
93 | 87 | ||
94 | pattern Cons a as <- (viewl -> Just (a, ascendL -> as)) | 88 | pattern Cons a as <- (viewl -> Just (a, ascendL -> as)) |
95 | where Cons a as = Singleton a <> as | 89 | where Cons a as = Singleton a <> as |