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 ring.
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.Ring.Sum.normalize`.
54 | ||| 2. The normal forms are compared for being identical.
55 | ||| 3. Since in `Algebra.Solver.Ring.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.
60 | |||
61 | ||| You can find several examples of making use of this
62 | ||| in `Data.Prim.Integer.Extra`.
65 | ||| A literal. This should be a value known at compile time
66 | ||| so that it reduces during normalization.
69 | ||| A variabl. This is is for values not known at compile
70 | ||| time. In order to compare and merge variables, we need an
71 | ||| `Elem x as` proof.
74 | ||| Negates and expression.
77 | ||| Addition of two expressions.
80 | ||| Multiplication of two expressions.
83 | ||| Subtraction of two expressions.
86 | ||| While this allows you to use the usual operators
87 | ||| for addition and multiplication, it is often convenient
88 | ||| to use related operators `.*.`, `.+.`, and similar when
89 | ||| working with variables.
101 | ||| Like `Var` but takes the `Elem x as` as an auto implicit
102 | ||| argument.
107 | --------------------------------------------------------------------------------
108 | -- Syntax
109 | --------------------------------------------------------------------------------
117 | ||| Addition of variables. This is an alias for
118 | ||| `var x + var y`.
128 | ||| Addition of variables. This is an alias for
129 | ||| `x + var y`.
139 | ||| Addition of variables. This is an alias for
140 | ||| `var x + y`.
150 | ||| Subtraction of variables. This is an alias for
151 | ||| `var x - var y`.
161 | ||| Subtraction of variables. This is an alias for
162 | ||| `x - var y`.
172 | ||| Subtraction of variables. This is an alias for
173 | ||| `var x - y`.
183 | ||| Multiplication of variables. This is an alias for
184 | ||| `var x * var y`.
194 | ||| Multiplication of variables. This is an alias for
195 | ||| `var x * y`.
205 | ||| Multiplication of variables. This is an alias for
206 | ||| `x * var y`.
216 | --------------------------------------------------------------------------------
217 | -- Evaluation
218 | --------------------------------------------------------------------------------
220 | ||| Evaluation of expressions. This keeps the exact
221 | ||| structure of the expression tree. For instance
222 | ||| `eval $ x .*. (y .+. z)` evaluates to `x * (y + z)`.
232 | --------------------------------------------------------------------------------
233 | -- Proofs
234 | --------------------------------------------------------------------------------
236 | ||| Proof that addition of exponents is equivalent to multiplcation
237 | ||| of the two terms.
238 | export