packages feed

calculator-0.3.0.0: src/Calculator/Prim/Definitions.hs

module Calculator.Prim.Definitions 
    ( unaryOps
    , binaryOps
    , defVars
    , defFuns
    , defBinds
    ) where

--------------------------------------------------------------------------------

import           Calculator.Prim.Bindings (Bindings, mkBind)
import           Calculator.Prim.Expr     (Operator (..))
import           Calculator.Prim.Function (Function, oneArg)

--------------------------------------------------------------------------------

import           Control.Arrow            (second)

--------------------------------------------------------------------------------

unaryOps :: [(Char, Operator)]
unaryOps = [ ('-', UnaryOp negate) ]

--------------------------------------------------------------------------------

-- | Binary Operators
binaryOps :: [(Char, Operator)]
binaryOps = [ ('+', BinaryOp (+))
            , ('-', BinaryOp (-))
            , ('*', BinaryOp (*))
            , ('/', BinaryOp (/))
            , ('^', BinaryOp (**))
            ]

--------------------------------------------------------------------------------

-- | List of pre-defined @Functions@
defFuns :: [(String, Function)]
defFuns = map (second oneArg)
           [ ("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)
           ]

--------------------------------------------------------------------------------

-- | List of provided variables
defVars :: [(String, Double)]
defVars = [ ("pi", pi)
          , ("e", exp 1)
          ]

--------------------------------------------------------------------------------

-- | Default bindings, created using defVars and defFuns
defBinds :: Bindings
defBinds = mkBind defVars defFuns

--------------------------------------------------------------------------------