-- Interpretation of Symantics that compiles to a recursive data type -- with stripped types. module SymExpr (Expr,expr) where import Sym -- We compile literals and operations to String, stripping types and -- representations. Note that t is a phantom. data Expr t = Expr {expr :: Term} deriving Eq instance Show (Expr t) where show = show . expr -- We need an extra level of indirection to represent the phantom -- type because of types like (Expr Tfloat -> Expr Tint) data Term = Lit String | Op String [Term] deriving Eq instance Show Term where show (Lit l) = l show (Op opc as) = opc ++ show as lit x = Expr $ Lit $ show x op1 opc (Expr x) = Expr $ Op opc $ [x] op2 opc (Expr x) (Expr y) = Expr $ Op opc $ [x,y] instance Symantics Expr where iVal = lit fVal = lit bVal = lit isign = op1 "isign" iabs = op1 "iabs" iadd = op2 "iadd" isub = op2 "isub" imul = op2 "imul" idiv = op2 "idiv" imod = op2 "imod" fsign = op1 "fsign" fabs = op1 "fabs" fadd = op2 "fadd" fsub = op2 "fsub" fmul = op2 "fmul" fdiv = op2 "fdiv" fsin = op1 "fsin" fcos = op1 "fcos" fexp = op1 "fexp" i2f = op1 "i2f" f2i = op1 "f2i" -- Can't use `es' from above because the terms are not the same (phantom) type. if_ (Expr c) (Expr y) (Expr n) = Expr $ Op "if_" [c,y,n]