calculator-0.1.3.0: src/Calculator/Prim/Expr.hs
module Calculator.Prim.Expr where
--------------------------------------------------------------------------------
import Calculator.Prim.Base (Number)
--------------------------------------------------------------------------------
type Bindings = [(String, Number)]
data Expr = Function String Expr
| Constant Number
| UnOp Operator Expr
| BinOp (Expr, [(Operator, Expr)])
| Variable String
| InvalidError String
data Operator = UnaryOp (Number -> Number)
| BinaryOp (Number -> Number -> Number)
--------------------------------------------------------------------------------
binaryOps :: [(Char, Operator)]
binaryOps = [ ('+', BinaryOp (+))
, ('-', BinaryOp (-))
, ('*', BinaryOp (*))
, ('/', BinaryOp (/))
, ('^', BinaryOp (**))
]
dispatch :: (RealFrac a, Floating a) => [(String, a -> a)]
dispatch = [ ("sin", sin)
, ("cos", cos)
, ("tan", tan)
, ("asin", asin)
, ("acos", acos)
, ("atan", atan)
, ("sinh", sinh)
, ("cosh", cosh)
, ("tanh", tanh)
, ("exp", exp)
, ("log", log)
, ("log10", logBase 10)
, ("sqrt", sqrt)
, ("ceil", fromInteger . ceiling)
, ("floor", fromInteger . floor)
, ("abs", abs)
]
--------------------------------------------------------------------------------
constEq :: Expr -> Expr -> Bool
constEq (Constant x) (Constant y) = x == y
constEq _ _ = False
--------------------------------------------------------------------------------
def_vars :: Bindings
def_vars = [ ("pi", pi) ]
--------------------------------------------------------------------------------
fromConst :: Expr -> Number
fromConst (Constant v) = v
fromConst (Function _ _) = error "fromConst Function"
fromConst (UnOp _ _) = error "fromConst UnOp"
fromConst (BinOp _) = error "fromConst BinOp"
fromConst (Variable _) = error "fromConst Variable"
fromConst (InvalidError _) = error "fromConst InvalidError"
--------------------------------------------------------------------------------
isConst :: Expr -> Bool
isConst (Constant _) = True
isConst _ = False
--------------------------------------------------------------------------------