Changeset 23 for sizechecking
- Timestamp:
- May 3, 2014, 5:01:55 PM (11 years ago)
- Location:
- sizechecking/branches/macs
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
sizechecking/branches/macs/Exp.hs
r22 r23 1 1 module Exp where 2 2 3 import Size 3 import Lambda 4 import Ops 5 import Data.Supply as S 4 6 5 class Exp l where7 class LOps l => Exp l where 6 8 nil :: l [a] 7 9 cons :: l a -> l [a] -> l [a] 8 match :: l [a] -> l b -> l (a -> [a] -> b) -> [b] 10 cons = infixopr ":" 5 (:) 11 match :: l [a] -> l b -> (l a -> l [a] -> l b) -> l b 12 cond :: l Bool -> l a -> l a -> l a 13 undefined :: l a 14 15 instance Exp Q where 16 nil = Q [] 17 cons x xs = Q ( unQ x : unQ xs ) 18 match l nbranch cbranch = case unQ l of 19 [] -> nbranch 20 (x:xs) -> cbranch (Q x) (Q xs) 21 cond c tbranch fbranch = if unQ c then tbranch else fbranch 22 undefined = Prelude.undefined 23 instance Exp S where 24 nil = S $ \_ _ -> showString "[]" 25 undefined = S $ \_ _ -> showString "undefined" 26 match list nbranch cbranch = S $ \s p -> 27 let (s1, s2, ss) = S.split3 s 28 (s3, s4, s5) = S.split3 ss 29 v1 = S.supplyValue s4 30 showV1 = S $ \_ _ -> showVar v1 31 v2 = S.supplyValue s5 32 showV2 = S $ \_ _ -> showVar v2 33 in showParen (p>0) $ 34 showString "case ". 35 unS list s1 0 . 36 showString " of [] => ". 37 unS nbranch s2 0 . 38 showString "; (" . showVar v1 . showChar ':' . showVar v2 . showString ") => " . 39 unS (cbranch showV1 showV2) s3 0 -
sizechecking/branches/macs/Lambda.hs
r20 r23 12 12 app :: l (a -> b) -> l a -> l b 13 13 const :: Int -> l Int 14 15 14 16 15 {- -
sizechecking/branches/macs/Ops.hs
r21 r23 10 10 class (Lambda l) => LOps l where 11 11 infixop :: String -> Int -> (a -> b -> c) -> l a -> l b -> l c 12 infixopr :: String -> Int -> (a -> b -> c) -> l a -> l b -> l c 13 infixopl :: String -> Int -> (a -> b -> c) -> l a -> l b -> l c 12 14 fun :: String -> a -> l a 13 15 14 16 (+) :: l Int -> l Int -> l Int 15 (+) = infixop "+" 4(Prelude.+)17 (+) = infixopl "+" 6 (Prelude.+) 16 18 17 19 (-) :: l Int -> l Int -> l Int 18 (-) = infixop "-" 4(Prelude.-)20 (-) = infixopl "-" 6 (Prelude.-) 19 21 20 22 (*) :: l Int -> l Int -> l Int 21 (*) = infixop "*" 5(Prelude.*)23 (*) = infixopl "*" 7 (Prelude.*) 22 24 23 25 24 26 instance LOps Q where 25 infixop _ _ f lhs rhs = Q (eval lhs `f` eval rhs) 27 infixopl _ _ f lhs rhs = Q (eval lhs `f` eval rhs) 28 infixop _ _ f lhs rhs = Q (eval lhs `f` eval rhs) 29 infixopr _ _ f lhs rhs = Q (eval lhs `f` eval rhs) 26 30 fun _ = Q 27 31 … … 29 33 fun name _ = S (\_ p -> Prelude.showsPrec p name) 30 34 31 infixop name prec _ lhs rhs = S(\s p ->35 infixopl name prec _ lhs rhs = S(\s p -> 32 36 let (s1, s2) = S.split2 s 33 37 in Prelude.showParen (p Prelude.> prec) $ … … 36 40 unS rhs s2 (Prelude.succ prec) 37 41 ) 42 infixop name prec _ lhs rhs = S(\s p -> 43 let (s1, s2) = S.split2 s 44 in Prelude.showParen (p Prelude.> prec) $ 45 unS lhs s1 (Prelude.succ prec) . 46 Prelude.showString name . 47 unS rhs s2 (Prelude.succ prec) 48 ) 49 infixopr name prec _ lhs rhs = S(\s p -> 50 let (s1, s2) = S.split2 s 51 in Prelude.showParen (p Prelude.> prec) $ 52 unS lhs s1 (Prelude.succ prec) . 53 Prelude.showString name . 54 unS rhs s2 prec 55 ) -
sizechecking/branches/macs/SizedExp.hs
r22 r23 1 1 import Ops 2 2 3 :e -
sizechecking/branches/macs/tests/ExpTest.hs
r22 r23 5 5 import Lambda 6 6 import Exp 7 import Ops8 7 import Prelude ( ($), Int, (==), return, sequence, (>>=), and, (.), IO, Bool ) 8 import qualified Control.Monad 9 9 10 10 testNil :: Exp e => e [a] 11 11 testNil = nil 12 12 13 addOne :: Exp e => e [Int] 13 testAddOne :: Exp e => e ([Int] -> [Int]) 14 testAddOne = lam $ \l -> cons (const 1) l 15 16 testHead :: Exp e => e ([a] -> a) 17 testHead = lam $ \l -> match l undefined $ \x _ -> x 18 19 testTail :: Exp e => e ([a] -> [a]) 20 testTail = lam $ \l -> match l undefined $ \_ xs -> xs 21 22 testConcat :: Exp e => e ([a] -> [a] -> [a]) 23 testConcat = lam $ \l1 -> lam $ \l2 -> match l1 l2 24 $ \x xs -> cons x (testConcat `app` xs `app` l2) 25 26 testEvalNil :: IO Bool 27 testEvalNil = return $ ([]::[Int]) == eval testNil 28 29 testEvalAddOne :: IO Bool 30 testEvalAddOne = return $ [1::Int] == (eval $ testAddOne `app` nil) 31 32 testEvalTail :: IO Bool 33 testEvalTail = return $ [2..6::Int] == eval testTail [1..6] 34 35 testEvalConcat :: IO Bool 36 testEvalConcat = return $ [1..6::Int] == eval testConcat [1,2,3] [4,5,6] 37 38 tests :: [IO Bool] 39 40 tests = [ testEvalNil 41 , testEvalAddOne 42 , testEvalTail 43 , testEvalConcat 44 ] 45 46 runTests :: IO Bool 47 runTests = Control.Monad.liftM and $ sequence tests
Note: See TracChangeset
for help on using the changeset viewer.