Changeset 12
- Timestamp:
- Nov 15, 2012, 12:44:36 AM (13 years ago)
- Location:
- sizechecking
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
sizechecking/Examples.hs
r11 r12 4 4 import Lambda 5 5 import SizedExp 6 import Prelude (($)) 7 import Constraints 8 import Prelude ( (+), (-), Int, (==), (*), (<), (>), (<=), (>=), (/=) ) 6 import Constraints() 7 import Prelude ( ($), (+), (-), Int, (==), (*), (<), (>), (<=), (>=), (/=) ) 9 8 import qualified Prelude as P 10 9 import qualified Control.Monad as M 11 10 import qualified Data.List as List 12 import Debug.Trace13 11 14 12 head :: (SizedExp se ) => Size se ([l] -> l) 15 13 head = bind headc body 16 14 where 17 body l = match l true (\x xs -> x)15 body l = match l true P.const 18 16 19 17 tail :: (SizedExp se ) => Size se ([l] -> [l]) 20 18 tail = bind tailc body 21 where body l = match l true (\ x xs -> xs)19 where body l = match l true (\_ xs -> xs) 22 20 23 21 cons :: (SizedExp se) => Size se (x -> [x] -> [x]) … … 28 26 where body f x = f `app` (f `app` (f `app` x)) 29 27 30 nil :: (SizedExp se) => Size se ([x])28 nil :: (SizedExp se) => Size se [x] 31 29 nil = bind nils true 32 30 33 31 map :: (SizedExp se) => Size se ( (a->b) -> [a] -> [b] ) 34 32 map = bind smap body 35 where body f l = match l (nil)33 where body f l = match l nil 36 34 ( \x xs -> cons `app` (f `app` x) `app` (map `app` f `app` xs )) 37 35 … … 44 42 append :: (SizedExp se) => Size se ([a] -> [a] -> [a]) 45 43 append = bind appends body 46 where body l1 l2 = match l1 (l2)44 where body l1 l2 = match l1 l2 47 45 (\x xs -> cons `app` x `app` (append `app` xs `app` l2)) 48 46 … … 59 57 60 58 pam :: (SizedExp se) => Size se ([a -> b] -> a -> [b]) 61 pam = bind (AAbs 1 2 $ Abs 3 $ List (Var 1) (Abs 4 $ (App (App (Var 2) (Var 4)) (Var 3)))) $ \fl x -> match fl (nil)59 pam = bind (AAbs 1 2 $ Abs 3 $ List (Var 1) (Abs 4 $ App (App (Var 2) (Var 4)) (Var 3))) $ \fl x -> match fl nil 62 60 (\f fs -> cons `app` (f `app` x) `app` (pam `app` fs `app` x)) 63 61 64 62 reverse :: (SizedExp se) => Size se([a] -> [a]) 65 reverse = bind reverses $ \l -> match l (63 reverse = bind reverses $ \l -> match l 66 64 nil 67 )(65 ( 68 66 \x xs -> append `app` (reverse `app` xs) `app` (cons `app` x `app` nil) 69 67 ) … … 88 86 \f x -> t3 `app` (t3 `app` f) `app` x 89 87 90 add27s = (AAbs 0 1 $ List (Op (Var 0) '+' (Num 27)) (Abs 2 Unsized)) 88 add27s :: L 89 add27s = AAbs 0 1 $ List (Op (Var 0) '+' (Num 27)) (Abs 2 Unsized) 91 90 add27 :: (SizedExp se) => Size se ([P.Int] -> [P.Int]) 92 91 add27 = bind add27s $ \x -> t27 `app` addone `app` x 93 92 94 93 94 zipWiths :: L 95 95 zipWiths = let q = App (Var 4) $ Op (Op (Var 5) '+' (Var 3)) '-' (Var 1) in 96 96 (Abs 0 $ AAbs 1 2 $ AAbs 3 4 $ List (Var 1) (Abs 5 $ App (App (Var 0) (App (Var 2) (Var 5))) q )) 97 97 zipWith :: (SizedExp se) => Size se ((a2 -> a1 -> a) -> [a2] -> [a1] -> [a]) 98 98 zipWith = bind zipWiths $ \f l1 l2 -> 99 match l1 (99 match l1 100 100 nil 101 )(102 \x xs -> match l2 (101 ( 102 \x xs -> match l2 103 103 true 104 )(104 ( 105 105 \y ys -> cons `app` (f `app` x `app` y) `app` (zipWith `app` f `app` xs `app` ys) 106 106 ) 107 107 ) 108 108 appAll :: (SizedExp se) => Size se ( [a -> b] -> a -> [b] ) 109 appAll = bind (AAbs 0 1 $ Abs 2 $ List (Var 0) (Abs 3 $ Var 1 `App` Var 3 `App` Var 2 ) ) $ \fl x -> match fl (110 nil 111 ) (109 appAll = bind (AAbs 0 1 $ Abs 2 $ List (Var 0) (Abs 3 $ Var 1 `App` Var 3 `App` Var 2 ) ) $ \fl x -> match fl 110 nil 111 ( 112 112 \f fs -> cons `app` (f `app` x) `app` (appAll `app` fs `app` x) 113 113 ) … … 129 129 sqdiff = bind (let sq l = Op l '*' l in AAbs 0 1 $ AAbs 2 3 $ List (sq $ Op (Var 0) '-' (Var 2)) $ Abs 4 $ List (Num 2) $ Abs 5 Unsized) $ 130 130 \l1 l2 -> match l1 (cprod `app` l2 `app` l2) 131 (\ hd1tl1 -> match l2 (cprod `app` l1 `app` l1)132 (\ hd2tl2 -> sqdiff `app` tl1 `app` tl2))131 (\_ tl1 -> match l2 (cprod `app` l1 `app` l1) 132 (\_ tl2 -> sqdiff `app` tl1 `app` tl2)) 133 133 134 134 replace :: SizedExp se => Size se (Int -> [Int] -> [Int]) 135 135 replace = bind (Abs 0 $ AAbs 1 2 $ List (Var 1) (Abs 3 Unsized)) $ 136 \x l -> match l (nil)(\hd tl -> cons `app` (x+hd) `app` tl)137 138 scalar _prod :: (SizedExp se0) => Size se0 ([Int] -> [Int] -> [Int])139 scalar _prod = bind (AAbs 0 1 $ AAbs 2 3 $ List (Num 1) (Abs 4 Unsized)) $136 \x l -> match l nil (\hd tl -> cons `app` (x+hd) `app` tl) 137 138 scalarProd :: (SizedExp se0) => Size se0 ([Int] -> [Int] -> [Int]) 139 scalarProd = bind (AAbs 0 1 $ AAbs 2 3 $ List (Num 1) (Abs 4 Unsized)) $ 140 140 \l1 l2 -> match l1 ( 141 141 match l2 ( cons `app` 0 `app` nil ) 142 (\ hd2 tl2-> true)142 (\_ _ -> true) 143 143 ) ( \hd1 tl1 -> 144 match l2 (true)145 ( \hd2 tl2 -> replace `app` (hd1 * hd2) `app` (scalar _prod `app` tl1 `app` tl2) )144 match l2 true 145 ( \hd2 tl2 -> replace `app` (hd1 * hd2) `app` (scalarProd `app` tl1 `app` tl2) ) 146 146 ) 147 147 … … 157 157 158 158 take4 :: SizedExp se => Size se (([a] -> [a]) -> [[a]]) 159 take4 = bind (Abs 0 $ List (Num 1) (Abs 2 (Var 0 `App` (Var 0 `App` List (Num 0) (Abs 1 $Bottom))))) $159 take4 = bind (Abs 0 $ List (Num 1) (Abs 2 (Var 0 `App` (Var 0 `App` List (Num 0) (Abs 1 Bottom))))) $ 160 160 \f ->cons `app` (f `app` (f `app` nil) ) `app` nil 161 161 162 162 163 163 merge :: SizedExp se => Size se ([Int] -> [Int] -> [Int]) 164 merge = bind (AAbs 0 1 $ AAbs 2 3 $ List (Op (Var 0) '+' (Var 2)) (Abs 4 $Unsized))$164 merge = bind (AAbs 0 1 $ AAbs 2 3 $ List (Op (Var 0) '+' (Var 2)) (Abs 4 Unsized))$ 165 165 \l1 l2 -> match l1 l2 ( 166 166 \x xs -> match l2 l1 ( … … 171 171 172 172 split1 :: SizedExp se => Size se ([Int] -> [Int]) 173 split1 = bind (AAbs 0 1 $ List (Op (Op (Var 0) '+' (Num 1))'/' (Num 2)) (Abs 2 $Unsized)) $173 split1 = bind (AAbs 0 1 $ List (Op (Op (Var 0) '+' (Num 1))'/' (Num 2)) (Abs 2 Unsized)) $ 174 174 \z -> match z nil (\y ys -> cons `app` y `app` (split2 `app` ys)) 175 175 176 176 split2 :: SizedExp se => Size se ([Int] -> [Int]) 177 split2 = bind (AAbs 0 1 $ List (Op (Var 0) '/' (Num 2)) (Abs 2 $Unsized)) $177 split2 = bind (AAbs 0 1 $ List (Op (Var 0) '/' (Num 2)) (Abs 2 Unsized)) $ 178 178 \z -> match z nil (\y ys -> split1 `app` ys) 179 179 180 ms = (AAbs 0 1 $ List (Var 0) (Abs 4 $ Unsized))180 ms = AAbs 0 1 $ List (Var 0) (Abs 4 Unsized) 181 181 mergesort :: SizedExp se => Size se ([Int] -> [Int]) 182 182 mergesort = bind ms $ … … 194 194 195 195 fix :: SizedExp se => Size se ((a -> a) -> a) 196 fix = bind (Abs 2 $ App y (Var 2)) $196 fix = bind (Abs 2 $ App yComb (Var 2)) $ 197 197 \f -> f `app` (fix `app` f) 198 198 … … 202 202 \x xs -> cons `app` (cons `app` x `app` (heads `app` xss)) 203 203 `app` (transpose `app` (cons `app` xs `app` (tails `app` xss))) 204 transposec = (AAbs 18 5 $ List len fun )204 transposec = AAbs 18 5 $ List len fun 205 205 where 206 len = (AAbs 19 6 $ (Var 19)) `App` (Var 5 `App` Num 0)207 fun = Abs 8 $ List (Var 18) (Abs 9 $ (AAbs 19 6 $ Var 6 `App` Var 8 ) `App` (Var 5 `App` Var 9))208 209 comps = (Abs 2 $ Abs 3 $ Abs 4 $ App (Var 2) (App (Var 3) (Var 4)))206 len = AAbs 19 6 (Var 19) `App` (Var 5 `App` Num 0) 207 fun = Abs 8 $ List (Var 18) (Abs 9 $ AAbs 19 6 (Var 6 `App` Var 8) `App` (Var 5 `App` Var 9)) 208 209 comps = Abs 2 $ Abs 3 $ Abs 4 $ App (Var 2) (App (Var 3) (Var 4)) 210 210 comp :: (SizedExp se) => Size se ( (b->c) -> (a->b) -> a->c ) 211 211 comp = bind comps $ \f g x -> f `app` (g `app` x) 212 212 213 test1s = Abs 2 $ AAbs 19 6 $ (Var 2) `App` (List (Var 19) (Var 6))213 test1s = Abs 2 $ AAbs 19 6 $ Var 2 `App` List (Var 19) (Var 6) 214 214 test1 :: (SizedExp se) => Size se (([a] -> [b]) -> [a] -> [b]) 215 215 test1 = bind test1s $ \f l -> match (f `app` l) nil (\x xs -> f `app` l) 216 216 217 217 218 test2s = Abs 2 $ AAbs 18 5 $ appends `App` (Var 2 `App` (List (Var 18) (Var 5))) `App` (List (Var 18) (Var 5))218 test2s = Abs 2 $ AAbs 18 5 $ appends `App` (Var 2 `App` List (Var 18) (Var 5)) `App` List (Var 18) (Var 5) 219 219 test2 :: (SizedExp se) => Size se (([a] -> [a]) -> [a] -> [a]) 220 220 test2 = bind test2s $ \f l -> append `app` (f `app` l) `app` l … … 244 244 , TestCase "appAll" appAll 245 245 , TestCase "conspack" conspack 246 , TestCase "scalar _prod" scalar_prod246 , TestCase "scalarProd" scalarProd 247 247 , TestCase "sqdiff" sqdiff 248 248 , TestCase "mlist" mlist … … 255 255 runTests = do 256 256 failed <- M.forM tests $ \(TestCase name test) -> do 257 P.print $" +++++++++++++++++++++++++++++++"258 P.print $ " + Proving " P.++ name259 P.print $" +++++++++++++++++++++++++++++++"257 P.print " +++++++++++++++++++++++++++++++" 258 P.print $ " + Proving " P.++ name 259 P.print " +++++++++++++++++++++++++++++++" 260 260 261 261 s <- prove test 262 if P.not s then 263 M.return [name] 264 else 265 M.return [] 262 M.return [name | P.not s] 266 263 267 264 let f = P.concat failed … … 269 266 P.putStrLn "All ok." 270 267 else do 271 P.putStr $"\n\nFailed test cases: "268 P.putStr "\n\nFailed test cases: " 272 269 P.putStrLn $ List.intercalate ", " f 273 270 -
sizechecking/Lambda.hs
r6 r12 219 219 dupfst = AAbs 19 5 $ List (Op (Num 1) '+' (Var 19)) $ Shift (Var 5) (Op (Var 19) '-' (Num 1)) 220 220 $ Abs 8 $ App (Var 5) (Op (Var 19) '-' (Num 1)) 221 y = Abs 18 $ App (Abs 22 $ App (Var 18) (App (Var 22) (Var 22))) (Abs 22 $ App (Var 18) (App (Var 22) (Var 22)))221 yComb = Abs 18 $ App (Abs 22 $ App (Var 18) (App (Var 22) (Var 22))) (Abs 22 $ App (Var 18) (App (Var 22) (Var 22))) 222 222 -- Îs,g.List s (λi.(λx.Ît,f.List (1+t) (Shift f (t-1) x)) U (g i)) 223 223 reverses = AAbs 18 5 $ List (Var 18) (Abs 8 $ App (Var 5) $ Op (Op (Var 18) '-' (Num 1)) '-' (Var 8) ) -
sizechecking/SizedExp.hs
r11 r12 11 11 import Constraints 12 12 import Data.Supply 13 13 import Control.Arrow 14 import Control.Monad 14 15 15 16 class TS b => (Unify se a b) where … … 19 20 20 21 instance (SizedExp se, Unify se a b, se~se1, c1~c2, TS c2) => Unify se (Size se1 c1->a) (c2->b) where 21 unify f g x = f undefined x22 unify f g = f undefined 22 23 23 24 instance (SizedExp se, se~se1, c1~c2, TS c2) => Unify se (Size se1 c1) c2 where 24 unify f g x = g undefined x25 unify f g = g undefined 25 26 26 27 class SizedExp (se :: * -> *) where … … 77 78 (s1, s2, s3) = split3 supply 78 79 freshvars' :: (Unify Q a b) => (a -> (Size Q b, L -> L)) -> (Size Q c -> a) -> (Size Q (c -> b), L -> L) 79 freshvars' u exp = (QSynt $ sizeof $x, \l -> f $ App l fv)80 freshvars' u exp = (QSynt $ sizeof x, \l -> f $ App l fv) 80 81 where 81 82 (x, f) = u $ exp $ QSynt $ do 82 putStrLn $ "[Bind] var="++ (show fv)++" of type "++(show $tk q)83 putStrLn $ "[Bind] var="++ show fv ++" of type "++ show (tk q) 83 84 return [([], fv)] 84 85 fv = fresh (tk q) s3 85 86 86 87 addConstraint :: Constraint -> [SExp] -> [SExp] 87 addConstraint nc = map ( \(c,l) -> (nc:c, l))88 addConstraint nc = map (first ((:) nc)) 88 89 89 90 concatMapM :: (a -> IO [b]) -> [a] -> IO [b] 90 concatMapM f l = mapM f l >>= return.concat91 concatMapM f l = liftM concat $ mapM f l 91 92 92 93 type SExp = ([Constraint], L) … … 99 100 match1 (cond, ltype) = do 100 101 x <- sizeof l 101 putStrLn $ "ORIGINAL is " ++ (show x)102 putStrLn $ "[MATCH] ==> " ++ (show $ ltype)102 putStrLn $ "ORIGINAL is " ++ show x 103 putStrLn $ "[MATCH] ==> " ++ show ltype 103 104 let lt = rall$ App (AAbs 18 5 $ Var 18) ltype 104 putStrLn $ " --- NIL ---" ++ (show lt)105 putStrLn $ " --- NIL ---" ++ show lt 105 106 nils <- sizeof nil 106 p utStrLn $ shownils107 print nils 107 108 let nilc = foldr addConstraint nils $ Zero lt:cond 108 109 let tx = rall $ App (AAbs 18 5 $ App (Var 5) (Op (Var 18) '-' (Num 1))) ltype 109 110 let txs = rall $App (AAbs 18 5 $ List (Op (Var 18) '-' (Num 1)) (Var 5)) ltype 110 putStrLn $ " --- CONS --- "++ (show tx)++" <-> "++(show txs)111 putStrLn $ " --- CONS --- "++ show tx ++" <-> "++ show txs 111 112 conss <- sizeof $ cons (QSynt$return [([], tx)]) (QSynt$return [([], txs)]) 112 113 print conss … … 151 152 fromInteger = num 152 153 153 prove :: Size Q b -> IO (Bool)154 prove :: Size Q b -> IO Bool 154 155 prove (QProvable l x) = do 155 (s1,s2,s3) <- newSupply 30 (+1) >>= return.split3156 (s1,s2,s3) <- liftM split3 $ newSupply 30 (+1) 156 157 c <- x s1 157 158 putStrLn ""
Note: See TracChangeset
for help on using the changeset viewer.