haskellator-0.1.0.0: SI-Units supporting calculator
Safe HaskellSafe-Inferred
LanguageHaskell2010

Math.Haskellator.Internal.Expr

Description

Models an expression tree

Examples:

>>> show $ BinOp (Val $ Value 1.0 $ meter 1) Plus (BinOp (Val $ Value 2 $ multiplier 1) Mult (Val $ Value 3.0 $ meter 1))
"(1.0 m + (2.0 * 3.0 m))"
>>> show $ BinOp (BinOp (Val $ Value 1.0 $ meter 1) Plus (Val $ Value 2.0 $ meter 1)) Mult (Val $ Value 3.0 $ multiplier 1)
"((1.0 m + 2.0 m) * 3.0)"
Synopsis

Documentation

type AstFold a b = ExceptT Error (State (Stack (Map String (Thunk a)))) b Source #

Encapsulates the result b of folding an expression tree and holds the current state of variable bindings to values of type a

type AstValue = Value Dimension Source #

The specific Value type used in the expression tree

type Bindings a = [(String, a)] Source #

A list of variable bindings, mapping a name to an arbitrary value

data Expr Source #

The expression tree

Constructors

Val AstValue

a literal value

BinOp Expr Op Expr

a binary expression (like +, -, *, /, ^)

UnaryOp Op Expr

a unary expression (like -)

Conversion Expr Dimension

a conversion (1m [km]). If present, this node is the root of the tree.

VarBindings (Bindings Expr) Expr

a variable binding expression

Var String 

Instances

Instances details
Show Expr Source # 
Instance details

Defined in Math.Haskellator.Internal.Expr

Methods

showsPrec :: Int -> Expr -> ShowS #

show :: Expr -> String #

showList :: [Expr] -> ShowS #

Eq Expr Source # 
Instance details

Defined in Math.Haskellator.Internal.Expr

Methods

(==) :: Expr -> Expr -> Bool #

(/=) :: Expr -> Expr -> Bool #

type SimpleAstFold a = AstFold a a Source #

Simplified version of AstFold that returns the same type as it binds to variables

data Thunk a Source #

A Value wrapped in a Thunk to allow for lazy evaluation

Constructors

Expr Expr

The unevaluated expression

Result a 

data Value u Source #

A simple representation of a value with a unit. The unit's type is parameterized, since the unit can be a simple Unit or a Dimension.

Constructors

Value 

Fields

Instances

Instances details
Show u => Show (Value u) Source # 
Instance details

Defined in Math.Haskellator.Internal.TH.UnitGeneration

Methods

showsPrec :: Int -> Value u -> ShowS #

show :: Value u -> String #

showList :: [Value u] -> ShowS #

bindVar :: String -> Thunk a -> AstFold a () Source #

Binds a Thunk to a variable name

bindVars :: Bindings (Thunk a) -> AstFold a () Source #

Binds multiple variable names

foldExpr Source #

Arguments

:: (AstValue -> a)

function that folds a value

-> (a -> Op -> a -> a)

function that folds a binary expression

-> (Op -> a -> a)

function that folds a unary expression

-> (a -> Dimension -> a)

function that folds a conversion expression

-> (Bindings a -> a -> a)

function that folds variable bindings

-> (String -> a)

function that folds a variable

-> Expr

the Expr to fold over

-> a

the resulting value

Folds an expression tree

getVarBinding Source #

Arguments

:: String

the variable name

-> AstFold a (Thunk a)

the Thunk bound to the variable

Retrieves the Thunk bound to a variable name

partiallyFoldExprM :: (AstValue -> SimpleAstFold a) -> (a -> Op -> a -> SimpleAstFold a) -> (Op -> a -> SimpleAstFold a) -> (a -> Dimension -> SimpleAstFold a) -> (Bindings Expr -> Expr -> SimpleAstFold a) -> (String -> SimpleAstFold a) -> Expr -> SimpleAstFold a Source #

Like foldExpr, but does not fold into variable bindings and returns a monadic result

runAstFold Source #

Arguments

:: SimpleAstFold a

the computation to run

-> Either Error a

the computation's result or an error

Runs an SimpleAstFold computation

runInNewScope Source #

Arguments

:: SimpleAstFold a

the computation to run

-> SimpleAstFold a

the computation's result

Evaluates a SimpleAstFold inside a new (and empty) scope