8 | --------------------------------------------------------------------------------
9 | -- Natural Numbers
10 | --------------------------------------------------------------------------------
12 | ||| Multiplies a value `n` times with itself. In case of `n`
13 | ||| being zero, this returns `1`.
19 | --------------------------------------------------------------------------------
20 | -- Expression
21 | --------------------------------------------------------------------------------
23 | ||| Data type representing expressions in a commutative semiring.
24 | |||
25 | ||| This is used to at compile time compare different forms of
26 | ||| the same expression and proof that they evaluate to
27 | ||| the same result.
28 | |||
29 | ||| An example:
30 | |||
31 | ||| ```idris example
32 | ||| 0 binom1 : {x,y : Bits8} -> (x + y) * (x + y) === x * x + 2 * x * y + y * y
33 | ||| binom1 = solve [x,y]
34 | ||| ((x .+. y) * (x .+. y))
35 | ||| (x .*. x + 2 *. x *. y + y .*. y)
36 | ||| ```
37 | |||
38 | ||| @ a the type used in the arithmetic expression
39 | ||| @ as list of variables which don't reduce at compile time
40 | |||
41 | ||| In the example above, `x` and `y` are variables, while `2`
42 | ||| is a literal known at compile time. To make working with
43 | ||| variables more convenient (the have to be wrapped in data
44 | ||| constructor `Var`, by using function `var` for instance),
45 | ||| additional operators for addition, multiplication, and
46 | ||| subtraction are provided.
47 | |||
48 | ||| In order to proof that two expressions evaluate to the
49 | ||| same results, the following steps are taken at compile
50 | ||| time:
51 | |||
52 | ||| 1. Both expressions are converted to a normal form via
53 | ||| `Algebra.Solver.Semiring.Sum.normalize`.
54 | ||| 2. The normal forms are compared for being identical.
55 | ||| 3. Since in `Algebra.Solver.Semiring.Sum` there is a proof that
56 | ||| converting an expression to its normal form does not
57 | ||| affect the result when evaluating it, if the normal
58 | ||| forms are identical, the two expressions must evaluate
59 | ||| to the same result.
62 | ||| A literal. This should be a value known at compile time
63 | ||| so that it reduces during normalization.
66 | ||| A variabl. This is is for values not known at compile
67 | ||| time. In order to compare and merge variables, we need an
68 | ||| `Elem x as` proof.
71 | ||| Addition of two expressions.
74 | ||| Multiplication of two expressions.
77 | ||| While this allows you to use the usual operators
78 | ||| for addition and multiplication, it is often convenient
79 | ||| to use related operators `.*.`, `.+.`, and similar when
80 | ||| working with variables.
87 | ||| Like `Var` but takes the `Elem x as` as an auto implicit
88 | ||| argument.
93 | --------------------------------------------------------------------------------
94 | -- Syntax
95 | --------------------------------------------------------------------------------
101 | ||| Addition of variables. This is an alias for
102 | ||| `var x + var y`.
112 | ||| Addition of variables. This is an alias for
113 | ||| `x + var y`.
123 | ||| Addition of variables. This is an alias for
124 | ||| `var x + y`.
134 | ||| Multiplication of variables. This is an alias for
135 | ||| `var x * var y`.
145 | ||| Multiplication of variables. This is an alias for
146 | ||| `var x * y`.
156 | ||| Multiplication of variables. This is an alias for
157 | ||| `x * var y`.
167 | --------------------------------------------------------------------------------
168 | -- Evaluation
169 | --------------------------------------------------------------------------------
171 | ||| Evaluation of expressions. This keeps the exact
172 | ||| structure of the expression tree. For instance
173 | ||| `eval $ x .*. (y .+. z)` evaluates to `x * (y + z)`.
181 | --------------------------------------------------------------------------------
182 | -- Proofs
183 | --------------------------------------------------------------------------------
185 | ||| Proof that addition of exponents is equivalent to multiplcation
186 | ||| of the two terms.
187 | export