diff options
author | Péter Diviánszky <divipp@gmail.com> | 2016-05-16 15:46:08 +0200 |
---|---|---|
committer | Péter Diviánszky <divipp@gmail.com> | 2016-05-16 15:46:08 +0200 |
commit | f0180a74480031783dddbae6f000c87be43edda2 (patch) | |
tree | 9e0bd65798923956c9b9129030f29adae74dca76 /prototypes | |
parent | b3308f8ee9c4fd26ef0306d25c3c83d915ec4786 (diff) |
refactoring
Diffstat (limited to 'prototypes')
-rw-r--r-- | prototypes/LamMachine.hs | 179 |
1 files changed, 109 insertions, 70 deletions
diff --git a/prototypes/LamMachine.hs b/prototypes/LamMachine.hs index 5dce0829..f2f48b11 100644 --- a/prototypes/LamMachine.hs +++ b/prototypes/LamMachine.hs | |||
@@ -42,12 +42,12 @@ data Op2 = Add | Sub | Mod | LessEq | EqInt | |||
42 | deriving (Eq, Show) | 42 | deriving (Eq, Show) |
43 | 43 | ||
44 | -- cached free variables set | 44 | -- cached free variables set |
45 | data Exp = Exp {dbUps :: [Up], maxFreeVars :: Int, expexp :: Exp_ } | 45 | data Exp = Exp {dbUps :: [Up], _maxFreeVars :: Int, expexp :: Exp_ } |
46 | 46 | ||
47 | -- state of the machine | 47 | -- state of the machine |
48 | data MSt = MSt Exp -- TODO: use finger tree instead of lets? | 48 | data MSt = MSt Exp |
49 | Exp | 49 | Exp |
50 | [Exp] -- TODO: use finger tree instead of list? | 50 | [Exp] |
51 | 51 | ||
52 | --------------------------------------------------------------------- toolbox: pretty print | 52 | --------------------------------------------------------------------- toolbox: pretty print |
53 | 53 | ||
@@ -90,7 +90,7 @@ showLet x y = DLet' x y | |||
90 | 90 | ||
91 | --------------------------------------------------------------------- toolbox: free variables | 91 | --------------------------------------------------------------------- toolbox: free variables |
92 | 92 | ||
93 | maxFreeVars' (Exp xs s _) = foldl' f s xs | 93 | maxFreeVars (Exp xs s _) = foldl' f s xs |
94 | where | 94 | where |
95 | f m (Up l n) = n + m | 95 | f m (Up l n) = n + m |
96 | 96 | ||
@@ -116,14 +116,6 @@ insertUp_ s u@(Up l n) us_@(u'@(Up l' n'): us) | |||
116 | | l >= l' && l <= l' + n' = Up l' (n' + n): us | 116 | | l >= l' && l <= l' + n' = Up l' (n' + n): us |
117 | | otherwise = u': insertUp_ s (Up (l-n') n) us | 117 | | otherwise = u': insertUp_ s (Up (l-n') n) us |
118 | 118 | ||
119 | -- TODO: remove | ||
120 | insertUp (Up l 0) us = us | ||
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 | ||
126 | |||
127 | -- TODO: remove if possible | 119 | -- TODO: remove if possible |
128 | fvs (Exp us fv _) = gen 0 $ foldr f [fv] us where | 120 | fvs (Exp us fv _) = gen 0 $ foldr f [fv] us where |
129 | f (Up l n) xs = l: l+n: map (+n) xs | 121 | f (Up l n) xs = l: l+n: map (+n) xs |
@@ -149,7 +141,7 @@ down i0 e0@(Exp us fv e) = f i0 us where | |||
149 | 141 | ||
150 | upss u (Exp _ i e) = Exp u i e | 142 | upss u (Exp _ i e) = Exp u i e |
151 | 143 | ||
152 | dup2 f ax bx = Exp s (maxFreeVars' az `max` maxFreeVars' bz) $ f az bz where | 144 | dup2 f ax bx = Exp s (maxFreeVars az `max` maxFreeVars bz) $ f az bz where |
153 | (s, [a', b']) = deltaUps [ax, bx] | 145 | (s, [a', b']) = deltaUps [ax, bx] |
154 | az = upss a' ax | 146 | az = upss a' ax |
155 | bz = upss b' bx | 147 | bz = upss b' bx |
@@ -158,23 +150,25 @@ dup1 :: (Exp -> Exp_) -> Exp -> Exp | |||
158 | dup1 f (Exp a b x) = Exp a b $ f $ Exp [] b x | 150 | dup1 f (Exp a b x) = Exp a b $ f $ Exp [] b x |
159 | 151 | ||
160 | dupCon f [] = Exp [] 0 $ f [] | 152 | dupCon f [] = Exp [] 0 $ f [] |
161 | dupCon f bx = Exp s (maximum $ maxFreeVars' <$> bz) $ f bz where | 153 | dupCon f bx = Exp s (maximum $ maxFreeVars <$> bz) $ f bz where |
162 | (s, b') = deltaUps bx | 154 | (s, b') = deltaUps bx |
163 | bz = zipWith upss b' bx | 155 | bz = zipWith upss b' bx |
164 | 156 | ||
165 | dupCase f ax (unzip -> (ss, bx)) | 157 | dupCase f ax (unzip -> (ss, bx)) |
166 | = Exp s (maxFreeVars' az `max` maximum (maxFreeVars' <$> bz)) $ f az $ zip ss bz | 158 | = Exp s (maxFreeVars az `max` maximum (maxFreeVars <$> bz)) $ f az $ zip ss bz |
167 | where | 159 | where |
168 | (s, a': b') = deltaUps $ ax: bx | 160 | (s, a': b') = deltaUps $ ax: bx |
169 | az = upss a' ax | 161 | az = upss a' ax |
170 | bz = zipWith upss b' bx | 162 | bz = zipWith upss b' bx |
171 | 163 | ||
172 | dupLam f e@(Exp a fv ax) = Exp (ff a) (max 0 $ fv - 1) $ f $ Exp (gg a) fv ax | 164 | dupLam f e@(Exp a fv ax) = Exp (ff a) fv' $ f $ Exp (gg a) fv ax |
173 | where | 165 | where |
166 | fv' = max 0 $ fv - 1 | ||
167 | |||
174 | gg (Up 0 n: _) = [Up 0 1] | 168 | gg (Up 0 n: _) = [Up 0 1] |
175 | gg _ = [] | 169 | gg _ = [] |
176 | 170 | ||
177 | ff (Up 0 n: us) = insertUp (Up 0 $ n - 1) $ incUp (-1) <$> us | 171 | ff (Up 0 n: us) = insertUp_ fv' (Up 0 $ n - 1) $ incUp (-1) <$> us |
178 | ff us = incUp (-1) <$> us | 172 | ff us = incUp (-1) <$> us |
179 | 173 | ||
180 | pattern Int i <- Exp _ _ (Int_ i) | 174 | pattern Int i <- Exp _ _ (Int_ i) |
@@ -213,64 +207,55 @@ incUp t (Up l n) = Up (l+t) n | |||
213 | showUps us n = foldr f (replicate n True) us where | 207 | showUps us n = foldr f (replicate n True) us where |
214 | f (Up l n) is = take l is ++ replicate n False ++ drop l is | 208 | f (Up l n) is = take l is ++ replicate n False ++ drop l is |
215 | 209 | ||
216 | --sectUps' a b = sect (showUps a) (showUps b) -- sectUps 0 a 0 b | ||
217 | |||
218 | sect [] xs = xs | ||
219 | sect xs [] = xs | ||
220 | sect (x:xs) (y:ys) = (x || y): sect xs ys | ||
221 | |||
222 | {- TODO | ||
223 | sectUps _ u _ [] = [] | ||
224 | sectUps _ [] _ u = [] | ||
225 | sectUps k us_@(Up l n: us) k' us_'@(Up l' n': us') | ||
226 | | k + l + n <= k' + l' = sectUps (k + n) us k' us_' | ||
227 | | k' + l' + n' <= k + l = sectUps k us_ (k' + n') us' | ||
228 | | otherwise = insertUp (Up l'' n'') $ sectUps (k + n - c) (Up b c: us) (k' + n' - c') (Up b c': us') | ||
229 | where | ||
230 | l'' = max l l' | ||
231 | b = min (l + n) (l' + n') | ||
232 | n'' = b - l'' | ||
233 | c = l + n - b | ||
234 | c' = l' + n' - b | ||
235 | |||
236 | diffUps [] u = u | ||
237 | diffUps [] [] = [] | ||
238 | diffUps (Up l n: us) (Up l' n': us') = insertUp (Up l' (l - l')) $ diffUps us (Up (l + n) (l' + n' - l - n): us') | ||
239 | -} | ||
240 | |||
241 | diffUps a b = diffUps' 0 (back a) (back b) | ||
242 | |||
243 | diffUps' n u [] = (+(-n)) <$> u | ||
244 | diffUps' n [] _ = [] | ||
245 | diffUps' n (x: xs) (y: ys) | ||
246 | | x < y = (x - n): diffUps' n xs (y: ys) | ||
247 | | x == y = diffUps' (n+1) xs ys | ||
248 | |||
249 | back = map fst . filter (not . snd) . zip [0..] | ||
250 | |||
251 | mkUps = f 0 | ||
252 | where | ||
253 | f i [] = [] | ||
254 | f i (x: xs) = insertUp (Up (x-i) 1) $ f (i+1) xs | ||
255 | |||
256 | deltaUps = deltaUps_ . map crk | 210 | deltaUps = deltaUps_ . map crk |
257 | |||
258 | deltaUps_ (map $ uncurry showUps -> us) = (mkUps $ back s, [mkUps $ u `diffUps` s | u <- us]) | ||
259 | where | 211 | where |
260 | s = foldr1 sect $ us | 212 | crk (Exp u e _) = (u, e) |
261 | 213 | ||
262 | crk (Exp u e _) = (u, e) | 214 | deltaUps_ (map toLadder -> xs) = (fromLadder $ negL s, [fromLadder $ dLadders 0 (negL u) (negL s) | u <- xs]) |
215 | where | ||
216 | s = foldr1 iLadders xs | ||
217 | |||
218 | toLadder (us, k) = add1 0 $ f 0 us where | ||
219 | f s (Up l n: us) = (l+s): (l+s+n): f (s+n) us | ||
220 | f s [] = k+s: [] | ||
221 | |||
222 | iLadders :: [Int] -> [Int] -> [Int] | ||
223 | iLadders x [] = x | ||
224 | iLadders [] x = x | ||
225 | iLadders x@(a: b: us) x'@(a': b': us') | ||
226 | | b <= a' = addL a b $ iLadders us x' | ||
227 | | b' <= a = addL a' b' $ iLadders x us' | ||
228 | | otherwise = addL (min a a') c $ iLadders (addL c b us) (addL c b' us') | ||
229 | where | ||
230 | c = min b b' | ||
231 | |||
232 | addL a b cs | a == b = cs | ||
233 | addL a b [] = a: b: [] | ||
234 | addL a b (c: cs) | b == c = a: cs | ||
235 | | otherwise = a: b: c: cs | ||
236 | |||
237 | fromLadder :: [Int] -> [Up] | ||
238 | fromLadder = f 0 where | ||
239 | f s (a: b: cs) = Up (a-s) (b-a): f (s+b-a) cs | ||
240 | f s [] = [] | ||
241 | |||
242 | add1 a (b: cs) | a == b = cs | ||
243 | | otherwise = a: b: cs | ||
244 | |||
245 | dLadders :: Int -> [Int] -> [Int] -> [Int] | ||
246 | dLadders s x [] = map (+(-s)) x | ||
247 | dLadders s [] x = [] -- impossible? | ||
248 | dLadders s x@(a: b: us) x'@(a': b': us') | ||
249 | | a' >= b = addL (a - s) (b - s) $ dLadders s us x' | ||
250 | | a' < a || b' < a' || b < a = error "dLadders" | ||
251 | | otherwise = addL (a - s) (a' - s) $ dLadders (s + sd) (addL c b us) (addL c b' us') | ||
252 | where | ||
253 | c = min b b' | ||
254 | sd = c - a' | ||
263 | 255 | ||
264 | joinUps a b = foldr insertUp b a | 256 | negL [] = [] |
257 | negL xs = init $ add1 0 xs | ||
265 | 258 | ||
266 | diffUpsTest xs | and $ zipWith (\a (b, _) -> s `joinUps` a == b) ys xs = show (s, ys) | ||
267 | where | ||
268 | (s, ys) = deltaUps_ xs | ||
269 | |||
270 | diffUpsTest' = diffUpsTest [x,y] --diffUpsTest x y | ||
271 | where | ||
272 | x = ([Up 1 2, Up 3 4, Up 8 2], 20) | ||
273 | y = ([Up 2 2, Up 5 1, Up 6 2, Up 7 2], 18) | ||
274 | 259 | ||
275 | getLets (Let x y) = x: getLets y | 260 | getLets (Let x y) = x: getLets y |
276 | getLets x = [x] | 261 | getLets x = [x] |
@@ -494,3 +479,57 @@ primes = 2:3: filter (\n -> and $ map (\p -> n `mod` p /= 0) (takeWhile (\x -> x | |||
494 | 479 | ||
495 | main = primes !! 3000 | 480 | main = primes !! 3000 |
496 | -} | 481 | -} |
482 | |||
483 | |||
484 | |||
485 | ------------------------------------------------------------- | ||
486 | |||
487 | {- alternative presentation | ||
488 | |||
489 | sect [] xs = xs | ||
490 | sect xs [] = xs | ||
491 | sect (x:xs) (y:ys) = (x || y): sect xs ys | ||
492 | |||
493 | diffUps a b = diffUps' 0 (back a) (back b) | ||
494 | |||
495 | diffUps' n u [] = (+(-n)) <$> u | ||
496 | diffUps' n [] _ = [] | ||
497 | diffUps' n (x: xs) (y: ys) | ||
498 | | x < y = (x - n): diffUps' n xs (y: ys) | ||
499 | | x == y = diffUps' (n+1) xs ys | ||
500 | |||
501 | back = map fst . filter (not . snd) . zip [0..] | ||
502 | |||
503 | mkUps = f 0 | ||
504 | where | ||
505 | f i [] = [] | ||
506 | f i (x: xs) = insertUp (Up (x-i) 1) $ f (i+1) xs | ||
507 | |||
508 | deltaUps_ (map $ uncurry showUps -> us) = (mkUps $ back s, [mkUps $ u `diffUps` s | u <- us]) | ||
509 | where | ||
510 | s = foldr1 sect $ us | ||
511 | |||
512 | joinUps a b = foldr insertUp b a | ||
513 | |||
514 | diffUpsTest xs | ||
515 | | and $ zipWith (\a (b, _) -> s `joinUps` a == b) ys xs = show (s, ys) | ||
516 | | otherwise = error $ unlines $ map (show . toLadder) xs ++ "----": map show xs ++ "-----": show s: show s_: "-----": map show ys ++ "------": map (show . joinUps s) ys | ||
517 | where | ||
518 | (s, ys) = deltaUps_ xs | ||
519 | s_ = foldr1 iLadders $ toLadder <$> xs | ||
520 | |||
521 | diffUpsTest' = diffUpsTest [x,y] --diffUpsTest x y | ||
522 | |||
523 | x = ([Up 1 2, Up 3 4, Up 8 2], 20) | ||
524 | y = ([Up 2 2, Up 5 1, Up 6 2, Up 7 2], 18) | ||
525 | |||
526 | -- TODO: remove | ||
527 | insertUp (Up l 0) us = us | ||
528 | insertUp u [] = [u] | ||
529 | insertUp u@(Up l n) us_@(u'@(Up l' n'): us) | ||
530 | | l < l' = u: us_ | ||
531 | | l >= l' && l <= l' + n' = Up l' (n' + n): us | ||
532 | | otherwise = u': insertUp (Up (l-n') n) us | ||
533 | |||
534 | -} | ||
535 | |||