Changeset 23 for sizechecking


Ignore:
Timestamp:
May 3, 2014, 5:01:55 PM (11 years ago)
Author:
gobi
Message:

expression

Location:
sizechecking/branches/macs
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • sizechecking/branches/macs/Exp.hs

    r22 r23  
    11module Exp where 
    22 
    3 import Size 
     3import Lambda 
     4import Ops 
     5import Data.Supply as S 
    46 
    5 class Exp l where 
     7class LOps l => Exp l where 
    68    nil :: l [a] 
    79    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 
     15instance 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  
     23instance 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  
    1212    app   :: l (a -> b) -> l a -> l b 
    1313    const :: Int -> l Int 
    14  
    1514 
    1615{- 
  • sizechecking/branches/macs/Ops.hs

    r21 r23  
    1010class (Lambda l) => LOps l where 
    1111    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 
    1214    fun :: String -> a -> l a 
    1315 
    1416    (+) :: l Int -> l Int -> l Int 
    15     (+) = infixop "+" 4 (Prelude.+) 
     17    (+) = infixopl "+" 6 (Prelude.+) 
    1618 
    1719    (-) :: l Int -> l Int -> l Int 
    18     (-) = infixop "-" 4 (Prelude.-) 
     20    (-) = infixopl "-" 6 (Prelude.-) 
    1921 
    2022    (*) :: l Int -> l Int -> l Int 
    21     (*) = infixop "*" 5 (Prelude.*) 
     23    (*) = infixopl "*" 7 (Prelude.*) 
    2224 
    2325 
    2426instance 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) 
    2630    fun _ = Q 
    2731 
     
    2933    fun name _ = S (\_ p -> Prelude.showsPrec p name) 
    3034 
    31     infixop name prec _ lhs rhs = S(\s p -> 
     35    infixopl name prec _ lhs rhs = S(\s p -> 
    3236        let (s1, s2) = S.split2 s 
    3337        in Prelude.showParen (p Prelude.> prec) $ 
     
    3640            unS rhs s2 (Prelude.succ prec) 
    3741        ) 
     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  
    11import Ops 
    22 
    3 :e 
  • sizechecking/branches/macs/tests/ExpTest.hs

    r22 r23  
    55import Lambda 
    66import Exp 
    7 import Ops 
    87import Prelude ( ($), Int, (==), return, sequence, (>>=), and, (.), IO, Bool ) 
     8import qualified Control.Monad 
    99 
    1010testNil :: Exp e => e [a] 
    1111testNil = nil 
    1212 
    13 addOne :: Exp e => e [Int] 
     13testAddOne :: Exp e => e ([Int] -> [Int]) 
     14testAddOne = lam $ \l -> cons (const 1) l 
     15 
     16testHead :: Exp e => e ([a] -> a) 
     17testHead = lam $ \l -> match l undefined $ \x _ -> x 
     18 
     19testTail :: Exp e => e ([a] -> [a]) 
     20testTail = lam $ \l -> match l undefined $ \_ xs -> xs 
     21 
     22testConcat :: Exp e => e ([a] -> [a] -> [a]) 
     23testConcat = lam $ \l1 -> lam $ \l2 -> match l1 l2 
     24    $ \x xs -> cons x (testConcat `app` xs `app` l2) 
     25 
     26testEvalNil :: IO Bool 
     27testEvalNil = return $ ([]::[Int]) == eval testNil 
     28 
     29testEvalAddOne :: IO Bool 
     30testEvalAddOne = return $ [1::Int] == (eval $ testAddOne `app` nil) 
     31 
     32testEvalTail :: IO Bool 
     33testEvalTail = return $ [2..6::Int] == eval testTail [1..6] 
     34 
     35testEvalConcat :: IO Bool 
     36testEvalConcat = return $ [1..6::Int] == eval testConcat [1,2,3] [4,5,6]  
     37 
     38tests :: [IO Bool] 
     39 
     40tests = [ testEvalNil 
     41        , testEvalAddOne 
     42        , testEvalTail 
     43        , testEvalConcat 
     44        ] 
     45 
     46runTests :: IO Bool 
     47runTests = Control.Monad.liftM and $ sequence tests 
Note: See TracChangeset for help on using the changeset viewer.