{-# LANGUAGE NoMonomorphismRestriction #-} import Sym import SymEval import SymExpr import SymAsm -- Tests. These expressions are polymorphic Symantics or Num class -- expressions, and can be interpreted by placing them in the right -- type context, i.e. try to apply them to "eval" or "code". -- expr1 :: (Symantics repr) => repr Tfloat expr1 = fmul (fVal 2) (fVal 3) -- Tagging the numbers isn't necessary; inferred through fadd's type. -- expr2 :: (Symantics repr) => repr Tfloat expr2 = fadd 5 6 -- Using Num class operations. The literal constructors are necessary here. -- expr3 :: (Symantics repr) => repr Tint expr3 = (iVal 1) + (iVal 2) -- All previous expressions infer correctly. Alternatively, a type -- declaration can be used to fix ambiguous expressions: expr4 = 1 + 2 :: (Symantics repr) => repr Tfloat -- :: (Num t) => t -- which means the syntax is lost: this is just a number. expr5 = 1 + 2 -- Variable bindings are implemented using Haskel's name binding. expr6 = let_ (iVal 3) (\x -> x * x) expr7 = let_ (iVal 3) (\x -> let_ (x*x) (\xx -> (xx * xx))) expr8 = let_ (iVal 3) (\x -> let_ (x*x) (\xx -> (x * xx))) expr9 = let_ (iVal 1) (\x -> (x * x)) expr10 = let_ (iVal 2) (\x -> (x * x)) -- These are generic num class operations which are also supported. -- :: (Num a) => a -> a -> a f1 a b = (a - b) * (a + b) f2 x = let_ (x * x) $ \xx -> xx * xx exprTests = (expr $ expr3, expr $ expr1, expr $ expr1 * expr2, expr $ f1 expr1 expr2, expr $ expr4) evalTests = (eval $ expr1, eval $ expr2, eval $ expr3) asmTests = (asm $ expr3, asm $ expr1, asm $ expr1 * expr2, asm $ f1 expr1 expr2, asm $ expr4) f3 x = let xx = x * x in xx * xx -- Doesn't share properly. f4 x = let x1 = x * x in let x2 = x1 * x1 in let x3 = x2 * x2 in x3 * x3 -- This shares properly. f5 x = let_ (x * x) $ \x1 -> let_ (x1 * x1) $ \x2 -> let_ (x2 * x2) $ \x3 -> x3 * x3 f6 x = do x1 <- x * x x2 <- x1 * x1 x3 <- x2 * x2 x3 * x3 v = return f7 x = do x1 <- v $ x * x x2 <- v $ x1 * x1 x3 <- v $ x2 * x2 v $ x3 * x3 f8 a b = if_ (a `ieq` b) 1 2 share x = let_ x id e11 = x3 where x1 = share 123 x2 = share $ x1 * x1 x3 = share $ x2 * x2 e12 = x3 where x1 = 123 x2 = x1 * x1 x3 = x2 * x2 e13 = x3 where x1 = share 123 x2 = share $ x1 * x1 x3 = share $ x2 * x2 -- (.==) = (==)