Ignore:
Timestamp:
May 2, 2014, 3:10:50 PM (11 years ago)
Author:
gobi
Message:

operators

Location:
sizechecking/branches/macs
Files:
2 added
2 edited

Legend:

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

    r20 r21  
     1{-# LANGUAGE FlexibleInstances #-} 
     2 
    13module Ops where 
    24 
     5import qualified Data.Supply as S 
     6import Prelude (String, Int, ($), (.)) 
     7import qualified Prelude 
    38import Lambda 
     9 
     10class (Lambda l) => LOps l where 
     11    infixop :: String -> Int -> (a -> b -> c) -> l a -> l b -> l c 
     12    fun :: String -> a -> l a 
     13 
     14    (+) :: l Int -> l Int -> l Int 
     15    (+) = infixop "+" 4 (Prelude.+) 
     16 
     17    (-) :: l Int -> l Int -> l Int 
     18    (-) = infixop "-" 4 (Prelude.-) 
     19 
     20    (*) :: l Int -> l Int -> l Int 
     21    (*) = infixop "*" 5 (Prelude.*) 
     22 
     23 
     24instance LOps Q where 
     25    infixop _ _ f lhs rhs = Q (eval lhs `f` eval rhs) 
     26    fun _ = Q 
     27 
     28instance LOps S where 
     29    fun name _ = S (\_ p -> Prelude.showsPrec p name) 
     30 
     31    infixop name prec _ lhs rhs = S(\s p -> 
     32        let (s1, s2) = S.split2 s 
     33        in Prelude.showParen (p Prelude.> prec) $ 
     34            unS lhs s1 prec . 
     35            Prelude.showString name . 
     36            unS rhs s2 (Prelude.succ prec) 
     37        ) 
  • sizechecking/branches/macs/tests/LambdaTest.hs

    r20 r21  
    11{-# LANGUAGE NoMonomorphismRestriction #-} 
    22 
     3module Tests.LambdaTest where 
     4 
    35import Lambda 
    4 import Ops 
    5 import Prelude ( ($), Int ) 
     6import Prelude ( ($), Int, (==), return, sequence, (>>=), and, (.), IO, Bool ) 
     7import qualified Control.Monad 
    68 
    79test1 :: (Lambda l) => l Int 
    810test1 = app (lam $ \_ -> const 3) (const 2) 
    911 
     12test1ast :: IO Bool 
     13test1ast = do 
     14    t <- ast test1 
     15    return $ t "" == "(λa.3) 2" 
    1016 
     17test1eval :: IO Bool 
     18test1eval = return $ eval test1 == 3 
     19 
     20tests :: [IO Bool] 
     21tests = [  
     22      test1ast 
     23    , test1eval 
     24    ] 
     25 
     26runTests :: IO Bool 
     27runTests = Control.Monad.liftM and $ sequence tests 
Note: See TracChangeset for help on using the changeset viewer.