A simplified datatype representing an expression. This can be used to inspect the structure of a Expr, which is hidden.
Constructors
SEBin BinaryOp (SimpleExpr v n) (SimpleExpr v n)SEUn UnaryOp (SimpleExpr v n)Var vConst n
:: a typeCtrl KGHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05
Modulemfsolve-0.3.2.2Haskell2010
This module implements an equation solver that solves and evaluates expressions on the fly. It is based on Prof. D.E.Knuth's metafont. The goal of mfsolve is to make the solver useful in an interactive program, by enhancing the bidirectionality of the solver. Like metafont, it can solve linear equations, and evaluate nonlinear expressions. In addition to metafont, it also solves for angles, and makes the solution independend of the order of the equations.
The Expr datatype allows for calculations with constants and unknown variables. The Dependencies datatype contains all dependencies and known equations.
Let's define some variables. The SimpleVar type is a simple wrapper around String to provide nice output, since the Show instance for String outputs quotation marks.
let [x, y, t, a] = map (makeVariable . SimpleVar) ["x", "y", "t", "a"]Solve linear equations:
showVars $ flip execSolver noDeps $ do
2*x + y === 5
x - y === 1x = 2.0
y = 1.0Solve for angle (pi/4):
showVars $ flip execSolver noDeps $ sin(t) === 1/sqrt(2)t = 0.7853981633974484Solve for angle (pi/3) and amplitude:
showVars $ flip execSolver noDeps $ do
a*sin(x) === sqrt 3
a*cos(x) === 1x = 1.0471975511965979
a = 2.0Allow nonlinear expression with unknown variables:
showVars $ flip execSolver noDeps $ do
sin(sqrt(x)) === y
x === 2x = 2.0
y = 0.9877659459927355Find the angle and amplitude when using a rotation matrix:
showVars $ flip execSolver noDeps $ do
a*cos t*x - a*sin t*y === 30
a*sin t*x + a*cos t*y === 40
x === 10
y === 10x = 10.0
y = 10.0
t = 0.14189705460416402
a = 3.5355339059327373A simplified datatype representing an expression. This can be used to inspect the structure of a Expr, which is hidden.
SEBin BinaryOp (SimpleExpr v n) (SimpleExpr v n)SEUn UnaryOp (SimpleExpr v n)Var vConst nA mathematical expression of several variables. Several Numeric instances (Num, Floating and Fractional) are provided, so doing calculations over Expr is more convenient.
(Eq n, Eq v) => Eq (Expr v n)Defined in mfsolve-0.3.2.2 · Math.MFSolve(Floating n, Ord n, Ord v) => Floating (Expr v n)Defined in mfsolve-0.3.2.2 · Math.MFSolve(Floating n, Ord n, Ord v) => Fractional (Expr v n)Defined in mfsolve-0.3.2.2 · Math.MFSolve(Floating n, Ord n, Ord v) => Num (Expr v n)Defined in mfsolve-0.3.2.2 · Math.MFSolve(Ord n, Num n, Eq n, Show v, Show n) => Show (Expr v n)Defined in mfsolve-0.3.2.2 · Math.MFSolveGeneric (Expr v n)Defined in mfsolve-0.3.2.2 · Math.MFSolve(Hashable v, Hashable n) => Hashable (Expr v n)Defined in mfsolve-0.3.2.2 · Math.MFSolvetype Rep (Expr v n) = D1 ('MetaData "Expr"
"Math.MFSolve"
"mfsolve-0.3.2.2-4P6nUllz1M08u0ErqGO98f"
'False) (C1 ('MetaCons "Expr"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (LinExpr v n)) :*: (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [TrigTerm v n]) :*: S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [NonLinExpr v n]))))Defined in mfsolve-0.3.2.2 · Math.MFSolveA linear expression of several variables.
For example: 2*a + 3*b + 2 would be represented as
LinExpr 2 [(a, 2), (b, 3)].
LinExpr n [(v, n)](Eq n, Eq v) => Eq (LinExpr v n)Defined in mfsolve-0.3.2.2 · Math.MFSolve(Show n, Show v) => Show (LinExpr v n)Defined in mfsolve-0.3.2.2 · Math.MFSolveGeneric (LinExpr v n)Defined in mfsolve-0.3.2.2 · Math.MFSolve(Hashable v, Hashable n) => Hashable (LinExpr v n)Defined in mfsolve-0.3.2.2 · Math.MFSolvetype Rep (LinExpr v n) = D1 ('MetaData "LinExpr"
"Math.MFSolve"
"mfsolve-0.3.2.2-4P6nUllz1M08u0ErqGO98f"
'False) (C1 ('MetaCons "LinExpr"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 n) :*: S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [(v, n)])))Defined in mfsolve-0.3.2.2 · Math.MFSolveSinsine
Coscosine
Absabsolute value
Recipreciprocal (1/x)
Signumsign
Expnatural exponential (e^x)
Lognatural logarithm (log x)
Coshhyperbolic cosine
Atanhinverse hyperbolic tangent
Tantangent
Tanhhyperbolic tangent
Sinhhyperbolic sine
Asininverse sine
Acosinverse cosine
Asinhinverse hyperbolic sine
Acoshinverse hyperbolic cosine
Ataninverse tangent
Eq UnaryOpDefined in mfsolve-0.3.2.2 · Math.MFSolveShow UnaryOpDefined in mfsolve-0.3.2.2 · Math.MFSolveGeneric UnaryOpDefined in mfsolve-0.3.2.2 · Math.MFSolveHashable UnaryOpDefined in mfsolve-0.3.2.2 · Math.MFSolvetype Rep UnaryOp = D1 ('MetaData "UnaryOp"
"Math.MFSolve"
"mfsolve-0.3.2.2-4P6nUllz1M08u0ErqGO98f"
'False) ((((C1 ('MetaCons "Sin"
'PrefixI 'False) U1 :+: C1 ('MetaCons "Cos"
'PrefixI 'False) U1) :+: (C1 ('MetaCons "Abs"
'PrefixI 'False) U1 :+: C1 ('MetaCons "Recip"
'PrefixI 'False) U1)) :+: ((C1 ('MetaCons "Signum"
'PrefixI 'False) U1 :+: C1 ('MetaCons "Exp"
'PrefixI 'False) U1) :+: (C1 ('MetaCons "Log"
'PrefixI 'False) U1 :+: C1 ('MetaCons "Cosh"
'PrefixI 'False) U1))) :+: (((C1 ('MetaCons "Atanh"
'PrefixI 'False) U1 :+: C1 ('MetaCons "Tan"
'PrefixI 'False) U1) :+: (C1 ('MetaCons "Tanh"
'PrefixI 'False) U1 :+: C1 ('MetaCons "Sinh"
'PrefixI 'False) U1)) :+: ((C1 ('MetaCons "Asin"
'PrefixI 'False) U1 :+: C1 ('MetaCons "Acos"
'PrefixI 'False) U1) :+: (C1 ('MetaCons "Asinh"
'PrefixI 'False) U1 :+: (C1 ('MetaCons "Acosh"
'PrefixI 'False) U1 :+: C1 ('MetaCons "Atan"
'PrefixI 'False) U1)))))Defined in mfsolve-0.3.2.2 · Math.MFSolveEq SimpleVarDefined in mfsolve-0.3.2.2 · Math.MFSolveOrd SimpleVarDefined in mfsolve-0.3.2.2 · Math.MFSolveShow SimpleVarDefined in mfsolve-0.3.2.2 · Math.MFSolveA simple String wrapper, which will print formulas more cleanly.
Generic SimpleVarDefined in mfsolve-0.3.2.2 · Math.MFSolveHashable SimpleVarDefined in mfsolve-0.3.2.2 · Math.MFSolvetype Rep SimpleVar = D1 ('MetaData "SimpleVar"
"Math.MFSolve"
"mfsolve-0.3.2.2-4P6nUllz1M08u0ErqGO98f"
'True) (C1 ('MetaCons "SimpleVar"
'PrefixI 'False) (S1 ('MetaSel 'Nothing 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 String)))Defined in mfsolve-0.3.2.2 · Math.MFSolveCreate an expression from a variable
Create an expression from a constant
Evaluate the expression given a variable substitution.
Make a expression from a simple expression.
Convert an Expr to a SimpleExpr.
evaluate a simple expression using the given substitution.
The expression contains the given variable.
map a simple expression using the given substitution.
map an expression using the given substitution.
This hidden datatype represents a system of equations. It contains linear dependencies on variables as well as nonlinear equations. The following terminology is used from metafont:
known variable: A variable who's dependency is just a number.
dependend variable: A variable which depends linearly on other variables.
independend variable: any other variable.
A dependend variable can only depend on other independend variables. Nonlinear equations will be simplified by substituting and evaluating known variables, or by reducing some trigonometric equations to linear equations.
(Show n, Floating n, Ord n, Ord v, Show v) => Show (Dependencies v n)Defined in mfsolve-0.3.2.2 · Math.MFSolveMonad m => MonadState (Dependencies v n) (MFSolverT v n m)Defined in mfsolve-0.3.2.2 · Math.MFSolveAn error type for ===, =&= and addEquation:
UndefinedVar vThe variable is not defined.
UnknownVar v nThe variable is defined but dependend an other variables.
InconsistentEq n (Expr v n)The equation was reduced to the impossible equation `a == 0` for nonzero a, which means the equation is inconsistent with previous equations.
RedundantEq (Expr v n)The equation was reduced to the redundant equation `0 == 0`, which means it doesn't add any information.
(Num n, Ord n, Show n, Show v) => Show (DepError v n)Defined in mfsolve-0.3.2.2 · Math.MFSolve(Ord n, Num n, Show v, Show n, Typeable v, Typeable n) => Exception (DepError v n)Defined in mfsolve-0.3.2.2 · Math.MFSolveMonad m => MonadError (DepError v n) (MFSolverT v n m)Defined in mfsolve-0.3.2.2 · Math.MFSolveAn empty system of equations.
addEquation d e: Add the equation e = 0 to the system d.
Eliminate an variable from the equations. Returns the eliminated equations. Before elimination it performs substitution to minimize the number of eliminated equations.
Important: this function is still experimental and mostly untested.
Return the value of the variable, or a list of variables it depends on. Only linear dependencies are shown.
Return all known variables.
Return True if the variable is known or dependend.
Return all nonlinear equations e_i, where e_i = 0.
Return all dependend variables with their dependencies.
Make the expressions on both sides equal
Make the pairs of expressions on both sides equal. No error is
signaled if the equation for one of the sides is Redundant for
example in (x, 0) == (y, 0).
Get the dependencies from a state monad. Specialized version of get.
Return the value of the variable or throw an error.
Monadic version of getKnown.
Monadic version of varDefined.
Monadic version of eliminate.
Succeed even when trowing a RedundantEq error.
A monad for solving equations. Basicly just a state and exception monad over Dependencies and DepError.
run the solver.
Return the result of solving the equations or an error.
Run the solver and return the dependencies or an error.
Return the result of solving the equations, or throw the error as an exception.
Show all variables and equations. Useful in combination with execSolver.
A monad transformer for solving equations. Basicly just a state and exception monad transformer over Dependencies and DepError.
MonadReader s m => MonadReader s (MFSolverT v n m)Defined in mfsolve-0.3.2.2 · Math.MFSolveMonadWriter s m => MonadWriter s (MFSolverT v n m)Defined in mfsolve-0.3.2.2 · Math.MFSolveMonadTrans (MFSolverT v n)Defined in mfsolve-0.3.2.2 · Math.MFSolveMonad m => MonadError (DepError v n) (MFSolverT v n m)Defined in mfsolve-0.3.2.2 · Math.MFSolveMonad m => MonadState (Dependencies v n) (MFSolverT v n m)Defined in mfsolve-0.3.2.2 · Math.MFSolveMonad m => Monad (MFSolverT v n m)Defined in mfsolve-0.3.2.2 · Math.MFSolveFunctor m => Functor (MFSolverT v n m)Defined in mfsolve-0.3.2.2 · Math.MFSolveMonad m => Applicative (MFSolverT v n m)Defined in mfsolve-0.3.2.2 · Math.MFSolveMonadIO m => MonadIO (MFSolverT v n m)Defined in mfsolve-0.3.2.2 · Math.MFSolveMonadCont m => MonadCont (MFSolverT v n m)Defined in mfsolve-0.3.2.2 · Math.MFSolveReturn the result of solving the equations or an error. Monadic version.
Run the solver and return the dependencies or an error. Monadic version.
Return the result of solving the equations, or throw the error as an exception. Monadic version.