HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulemfsolve-0.3.2.2Haskell2010

Math.MFSolve

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.

Examples:

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   === 1
x = 2.0
y = 1.0

Solve for angle (pi/4):

showVars $ flip execSolver noDeps $ sin(t) === 1/sqrt(2)
t = 0.7853981633974484

Solve for angle (pi/3) and amplitude:

showVars $ flip execSolver noDeps $ do
  a*sin(x) === sqrt 3
  a*cos(x) === 1
x = 1.0471975511965979
a = 2.0

Allow nonlinear expression with unknown variables:

showVars $ flip execSolver noDeps $ do
  sin(sqrt(x)) === y
  x === 2
x = 2.0
y = 0.9877659459927355

Find 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 === 10
x = 10.0
y = 10.0
t = 0.14189705460416402
a = 3.5355339059327373
  • 10 types
  • 34 values
  • Packagemfsolve-0.3.2.2
  • Exports44
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceMFSolve.hs

Expressions

15 declarations
datadata Expr v n
#

A mathematical expression of several variables. Several Numeric instances (Num, Floating and Fractional) are provided, so doing calculations over Expr is more convenient.

Instances8Eq, Floating, Fractional, Num, Show, Generic, …
datadata LinExpr v n
#

A linear expression of several variables. For example: 2*a + 3*b + 2 would be represented as LinExpr 2 [(a, 2), (b, 3)].

Constructors

Instances5Eq, Show, Generic, Hashable, Rep
datadata UnaryOp
#

Constructors

Instances5Eq, Show, Generic, Hashable, Rep
datadata BinaryOp
#

Constructors

Instances2Eq, Show
newtypenewtype SimpleVar
#

Constructors

Instances6Eq, Ord, Show, Generic, Hashable, Rep

Dependencies

10 declarations
datadata Dependencies v n
#

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.

Instances2Show, MonadState
datadata DepError v n
#

An error type for ===, =&= and addEquation:

Constructors

  • UndefinedVar v

    The variable is not defined.

  • UnknownVar v n

    The 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.

Instances3Show, Exception, MonadError
valuegetKnown :: (Eq v, Hashable v) => v -> Dependencies v n -> Either [v] n
#

Return the value of the variable, or a list of variables it depends on. Only linear dependencies are shown.

Monadic Interface

8 declarations

MFSolver monad

6 declarations

MFSolverT monad transformer

5 declarations
newtypenewtype MFSolverT v n (m :: Type -> Type) a
#

A monad transformer for solving equations. Basicly just a state and exception monad transformer over Dependencies and DepError.

Instances10MonadReader, MonadWriter, MonadTrans, MonadError, MonadState, Monad, …