summaryrefslogtreecommitdiff
path: root/prototypes
diff options
context:
space:
mode:
authorPéter Diviánszky <divipp@gmail.com>2016-05-16 15:46:08 +0200
committerPéter Diviánszky <divipp@gmail.com>2016-05-16 15:46:08 +0200
commitf0180a74480031783dddbae6f000c87be43edda2 (patch)
tree9e0bd65798923956c9b9129030f29adae74dca76 /prototypes
parentb3308f8ee9c4fd26ef0306d25c3c83d915ec4786 (diff)
refactoring
Diffstat (limited to 'prototypes')
-rw-r--r--prototypes/LamMachine.hs179
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
45data Exp = Exp {dbUps :: [Up], maxFreeVars :: Int, expexp :: Exp_ } 45data Exp = Exp {dbUps :: [Up], _maxFreeVars :: Int, expexp :: Exp_ }
46 46
47-- state of the machine 47-- state of the machine
48data MSt = MSt Exp -- TODO: use finger tree instead of lets? 48data 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
93maxFreeVars' (Exp xs s _) = foldl' f s xs 93maxFreeVars (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
120insertUp (Up l 0) us = us
121insertUp u [] = [u]
122insertUp 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
128fvs (Exp us fv _) = gen 0 $ foldr f [fv] us where 120fvs (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
150upss u (Exp _ i e) = Exp u i e 142upss u (Exp _ i e) = Exp u i e
151 143
152dup2 f ax bx = Exp s (maxFreeVars' az `max` maxFreeVars' bz) $ f az bz where 144dup2 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
158dup1 f (Exp a b x) = Exp a b $ f $ Exp [] b x 150dup1 f (Exp a b x) = Exp a b $ f $ Exp [] b x
159 151
160dupCon f [] = Exp [] 0 $ f [] 152dupCon f [] = Exp [] 0 $ f []
161dupCon f bx = Exp s (maximum $ maxFreeVars' <$> bz) $ f bz where 153dupCon 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
165dupCase f ax (unzip -> (ss, bx)) 157dupCase 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
172dupLam f e@(Exp a fv ax) = Exp (ff a) (max 0 $ fv - 1) $ f $ Exp (gg a) fv ax 164dupLam 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
180pattern Int i <- Exp _ _ (Int_ i) 174pattern Int i <- Exp _ _ (Int_ i)
@@ -213,64 +207,55 @@ incUp t (Up l n) = Up (l+t) n
213showUps us n = foldr f (replicate n True) us where 207showUps 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
218sect [] xs = xs
219sect xs [] = xs
220sect (x:xs) (y:ys) = (x || y): sect xs ys
221
222{- TODO
223sectUps _ u _ [] = []
224sectUps _ [] _ u = []
225sectUps 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
236diffUps [] u = u
237diffUps [] [] = []
238diffUps (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
241diffUps a b = diffUps' 0 (back a) (back b)
242
243diffUps' n u [] = (+(-n)) <$> u
244diffUps' n [] _ = []
245diffUps' n (x: xs) (y: ys)
246 | x < y = (x - n): diffUps' n xs (y: ys)
247 | x == y = diffUps' (n+1) xs ys
248
249back = map fst . filter (not . snd) . zip [0..]
250
251mkUps = f 0
252 where
253 f i [] = []
254 f i (x: xs) = insertUp (Up (x-i) 1) $ f (i+1) xs
255
256deltaUps = deltaUps_ . map crk 210deltaUps = deltaUps_ . map crk
257
258deltaUps_ (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
262crk (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
264joinUps a b = foldr insertUp b a 256 negL [] = []
257 negL xs = init $ add1 0 xs
265 258
266diffUpsTest xs | and $ zipWith (\a (b, _) -> s `joinUps` a == b) ys xs = show (s, ys)
267 where
268 (s, ys) = deltaUps_ xs
269
270diffUpsTest' = 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
275getLets (Let x y) = x: getLets y 260getLets (Let x y) = x: getLets y
276getLets x = [x] 261getLets x = [x]
@@ -494,3 +479,57 @@ primes = 2:3: filter (\n -> and $ map (\p -> n `mod` p /= 0) (takeWhile (\x -> x
494 479
495main = primes !! 3000 480main = primes !! 3000
496-} 481-}
482
483
484
485-------------------------------------------------------------
486
487{- alternative presentation
488
489sect [] xs = xs
490sect xs [] = xs
491sect (x:xs) (y:ys) = (x || y): sect xs ys
492
493diffUps a b = diffUps' 0 (back a) (back b)
494
495diffUps' n u [] = (+(-n)) <$> u
496diffUps' n [] _ = []
497diffUps' n (x: xs) (y: ys)
498 | x < y = (x - n): diffUps' n xs (y: ys)
499 | x == y = diffUps' (n+1) xs ys
500
501back = map fst . filter (not . snd) . zip [0..]
502
503mkUps = f 0
504 where
505 f i [] = []
506 f i (x: xs) = insertUp (Up (x-i) 1) $ f (i+1) xs
507
508deltaUps_ (map $ uncurry showUps -> us) = (mkUps $ back s, [mkUps $ u `diffUps` s | u <- us])
509 where
510 s = foldr1 sect $ us
511
512joinUps a b = foldr insertUp b a
513
514diffUpsTest 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
521diffUpsTest' = diffUpsTest [x,y] --diffUpsTest x y
522
523x = ([Up 1 2, Up 3 4, Up 8 2], 20)
524y = ([Up 2 2, Up 5 1, Up 6 2, Up 7 2], 18)
525
526-- TODO: remove
527insertUp (Up l 0) us = us
528insertUp u [] = [u]
529insertUp 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