1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use std::sync::Arc;
use std::fmt;
use super::Expr;

/// Contains symbols and operators on symbols.
#[derive(Clone, PartialEq, Debug)]
pub enum Symbol {
    /// The wildcard symbol `_`.
    Any,
    /// A variable bound from context.
    ///
    /// This can be anything.
    Var(Arc<String>),
    /// A function variable with specified arity (number of arguments).
    ArityVar(Arc<String>, usize),
    /// A list variable.
    ListVar(Arc<String>),
    /// A list variable of length 1.
    ///
    /// Lifts the value out of the list at binding.
    Singleton(Arc<String>),
    /// A head-tail pattern match on a tuple.
    ///
    /// This requires the tuple to have at least length 2.
    /// It is to avoid cycles between reductions.
    HeadTailTup(Box<Expr>, Box<Expr>),
    /// A head-tail pattern match on a list.
    ///
    /// This requires the list to have at least length 2.
    /// It is to avoid cycles between reductions.
    HeadTailList(Box<Expr>, Box<Expr>),
    /// A value variable.
    ///
    /// This requires the expression to be `Ret` variant.
    /// It is used in special rules such as `(\k)(x) => \k`.
    RetVar(Arc<String>),
    /// A value variable that is an integer.
    RetIntVar(Arc<String>),
    /// A value variable that is positive or zero.
    RetPosVar(Arc<String>),
    /// A value variable that is negative and non-zero.
    ///
    /// Binds to its positive value.
    RetNegVar(Arc<String>),
    /// A variable that is not a value variable.
    NotRetVar(Arc<String>),
    /// Compute a binary function.
    ///
    /// This is used when the right side of the rule computes something from two left side expressions.
    BinopRetVar(Arc<String>, Arc<String>, Box<Symbol>),
    /// Compute a ternary function.
    ///
    /// This is used when the right side of the rule computes something from three left side expressions.
    TernopRetVar(Arc<String>, Arc<String>, Arc<String>, Box<Symbol>),
    /// Compute a unary function.
    ///
    /// This is used when the right side of the rule computes something from a left side expression.
    UnopRetVar(Arc<String>, Box<Symbol>),
    /// A function without domain constraints.
    NoConstrVar(Arc<String>),
    /// `\false` for one argument.
    False1,
    /// `not`.
    Not,
    /// `id` for booleans.
    Idb,
    /// `\true` for one argument.
    True1,
    /// `\false` for two arguments.
    False2,
    /// `\true` for two arguments.
    True2,
    /// `and`.
    And,
    /// `or`.
    Or,
    /// `eq` for booleans.
    Eqb,
    /// `xor`.
    Xor,
    /// `nand`.
    Nand,
    /// `nor`.
    Nor,
    /// `exc`.
    Exc,
    /// `imply`.
    Imply,
    /// `fst` for booleans.
    Fstb,
    /// `snd` for booleans.
    Sndb,
    /// `even`.
    Even,
    /// `odd`.
    Odd,
    /// `abs`.
    Abs,
    /// `lt`.
    Lt,
    /// `(< _)(_)`.
    Rlt,
    /// `le`.
    Le,
    /// `(<= _)(_)`.
    Rle,
    /// `gt`.
    Gt,
    /// `(> _)(_)`.
    Rgt,
    /// `ge`.
    Ge,
    /// `(>= _)(_)`.
    Rge,
    /// `neg`.
    Neg,
    /// `reci` (reciprocal).
    Reci,
    /// `conj` (complex conjugate).
    Conj,
    /// `norm` (vector norm).
    Norm,
    /// `sqnorm` (vector square norm).
    Sqnorm,
    /// `add`.
    Add,
    /// `sub`.
    Sub,
    /// `mul`.
    Mul,
    /// `mulc` (complex multiplication).
    Mulc,
    /// `div`.
    Div,
    /// `rem`.
    Rem,
    /// `pow`.
    Pow,
    /// `rpow`.
    Rpow,
    /// `sqrt`.
    Sqrt,
    /// `ln`.
    Ln,
    /// `log2`.
    Log2,
    /// `log10`.
    Log10,
    /// `exp`.
    Exp,
    /// `len`.
    Len,
    /// `concat`.
    Concat,
    /// `sum`.
    Sum,
    /// `min2`.
    Min2,
    /// `max2`.
    Max2,
    /// `min`.
    Min,
    /// `max`.
    Max,
    /// `range` [a, b].
    Range,
    /// `rangel` [a, b).
    Rangel,
    /// `ranger` (a, b].
    Ranger,
    /// `rangem` (a, b).
    Rangem,
    /// `prob` [0, 1].
    Prob,
    /// `probl` [0, 1).
    Probl,
    /// `probr` (0, 1].
    Probr,
    /// `probm` (0, 1).
    Probm,
    /// `mul_mat`.
    MulMat,
    /// `det`.
    Det,
    /// `dim`.
    Dim,
    /// `is_square_mat`,
    IsSquareMat,
    /// `base` (len, index).
    ///
    /// Constructs a list with zeroes of specified length,
    /// but with `1` at the index.
    Base,
    /// `fst`.
    Fst,
    /// `snd`.
    Snd,
    /// `sin`.
    Sin,
    /// `asin`.
    Asin,
    /// `cos`.
    Cos,
    /// `acos`.
    Acos,
    /// `tan`.
    Tan,
    /// `atan`.
    Atan,
    /// `atan2`.
    Atan2,
    /// `dot`.
    Dot,
    /// `push`.
    Push,
    /// `push_front`.
    PushFront,
    /// `item` (index, list).
    Item,
    /// `el`.
    El,
    /// `re`.
    Re,
    /// `im`.
    Im,
    /// Generic `id`.
    Id,
    /// Generic `eq`.
    Eq,
    /// Generic `neq`.
    Neq,
    /// Derivative.
    D,
    /// Integral.
    Integ,
    /// `if`.
    ///
    /// This is used in Boolean functions.
    If,
    /// Existential path `∃`.
    Ex,
    /// Trivial path `∀`.
    Triv,
    /// `\`, the type of `\x`.
    RetType,
    /// The type of lists.
    VecType,
    /// The judgement `(: a)(b)`.
    Rty,
    /// `vec_op`.
    ///
    /// Applies a binary function component-wise to lists.
    VecOp,
    /// `vec_uop`.
    ///
    /// Applies a unary function component-wise to lists.
    VecUop,
    /// `arity`.
    Arity,
    /// `pi` or `π`.
    Pi,
    /// `tau` or `τ`.
    Tau,
    /// `eps` or `ε`.
    Eps,
    /// `imag` (complex imaginary base).
    Imag,
    /// `imag2` (second complex imaginary base for quaternions).
    Imag2,
    /// `imag3` (third complex imaginary base for quaternions).
    Imag3,
    /// `type_of`.
    TypeOf,
    /// `bool`.
    BoolType,
    /// `f64`.
    F64Type,
    /// `quat` (type of quaternions).
    QuatType,
    /// `inf` (infinity).
    Inf,
}

impl fmt::Display for Symbol {
    fn fmt(&self, w: &mut fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> {
        use Symbol::*;

        match self {
            False1 => write!(w, "false1")?,
            Not => write!(w, "not")?,
            Idb => write!(w, "idb")?,
            True1 => write!(w, "true1")?,
            False2 => write!(w, "false2")?,
            True2 => write!(w, "true2")?,
            And => write!(w, "and")?,
            Or => write!(w, "or")?,
            Eqb => write!(w, "eqb")?,
            Xor => write!(w, "xor")?,
            Nand => write!(w, "nand")?,
            Nor => write!(w, "nor")?,
            Exc => write!(w, "exc")?,
            Imply => write!(w, "imply")?,
            Fstb => write!(w, "fstb")?,
            Sndb => write!(w, "sndb")?,
            Even => write!(w, "even")?,
            Odd => write!(w, "odd")?,
            Abs => write!(w, "abs")?,
            Lt => write!(w, "lt")?,
            Rlt => write!(w, "rlt")?,
            Le => write!(w, "le")?,
            Rle => write!(w, "rle")?,
            Gt => write!(w, "gt")?,
            Rgt => write!(w, "rgt")?,
            Ge => write!(w, "ge")?,
            Rge => write!(w, "rge")?,
            Neg => write!(w, "neg")?,
            Reci => write!(w, "reci")?,
            Conj => write!(w, "conj")?,
            Norm => write!(w, "norm")?,
            Sqnorm => write!(w, "sqnorm")?,
            Add => write!(w, "add")?,
            Sub => write!(w, "sub")?,
            Mul => write!(w, "mul")?,
            Mulc => write!(w, "mulc")?,
            Div => write!(w, "div")?,
            Rem => write!(w, "rem")?,
            Pow => write!(w, "pow")?,
            Rpow => write!(w, "rpow")?,
            Sqrt => write!(w, "sqrt")?,
            Ln => write!(w, "ln")?,
            Log2 => write!(w, "log2")?,
            Log10 => write!(w, "log10")?,
            Exp => write!(w, "exp")?,
            Len => write!(w, "len")?,
            Concat => write!(w, "concat")?,
            Sum => write!(w, "sum")?,
            Min2 => write!(w, "min2")?,
            Max2 => write!(w, "max2")?,
            Min => write!(w, "min")?,
            Max => write!(w, "max")?,
            Range => write!(w, "range")?,
            Rangel => write!(w, "rangel")?,
            Ranger => write!(w, "ranger")?,
            Rangem => write!(w, "rangem")?,
            Prob => write!(w, "prob")?,
            Probl => write!(w, "probl")?,
            Probr => write!(w, "probr")?,
            Probm => write!(w, "probm")?,
            MulMat => write!(w, "mul_mat")?,
            Det => write!(w, "det")?,
            Dim => write!(w, "dim")?,
            IsSquareMat => write!(w, "is_square_mat")?,
            Base => write!(w, "base")?,
            Fst => write!(w, "fst")?,
            Snd => write!(w, "snd")?,
            Sin => write!(w, "sin")?,
            Asin => write!(w, "asin")?,
            Cos => write!(w, "cos")?,
            Acos => write!(w, "acos")?,
            Tan => write!(w, "tan")?,
            Atan => write!(w, "atan")?,
            Atan2 => write!(w, "atan2")?,
            Dot => write!(w, "dot")?,
            Push => write!(w, "push")?,
            PushFront => write!(w, "push_front")?,
            Item => write!(w, "item")?,
            El => write!(w, "el")?,
            Re => write!(w, "re")?,
            Im => write!(w, "im")?,
            Id => write!(w, "id")?,
            Eq => write!(w, "eq")?,
            Neq => write!(w, "neq")?,
            If => write!(w, "if")?,
            Any => write!(w, "_")?,
            Ex => write!(w, "∃")?,
            Triv => write!(w, "∀")?,
            RetType => write!(w, "\\")?,
            VecType => write!(w, "vec")?,
            Rty => write!(w, "rty")?,
            VecOp => write!(w, "vec_op")?,
            VecUop => write!(w, "vec_uop")?,
            Arity => write!(w, "arity")?,
            D => write!(w, "d")?,
            Integ => write!(w, "∫")?,
            Pi => write!(w, "π")?,
            Tau => write!(w, "τ")?,
            Eps => write!(w, "ε")?,
            Imag => write!(w, "𝐢")?,
            Imag2 => write!(w, "𝐢₂")?,
            Imag3 => write!(w, "𝐢₃")?,
            TypeOf => write!(w, "type_of")?,
            BoolType => write!(w, "bool")?,
            F64Type => write!(w, "f64")?,
            QuatType => write!(w, "quat")?,
            Inf => write!(w, "∞")?,
            Var(x) | NoConstrVar(x) => write!(w, "{}", x)?,
            ArityVar(x, _) => write!(w, "{}", x)?,
            RetVar(x) => write!(w, "\\{}", x)?,
            RetIntVar(x) => write!(w, "\\{}:int", x)?,
            RetPosVar(x) => write!(w, "\\{}:(>= 0)", x)?,
            RetNegVar(x) => write!(w, "\\{}:(< 0)", x)?,
            NotRetVar(x) => write!(w, "!\\{}", x)?,
            ListVar(x) => write!(w, "[{}..]", x)?,
            Singleton(x) => write!(w, "[{}]", x)?,
            HeadTailTup(x, y) => write!(w, "({}, {}..)", x, y)?,
            HeadTailList(x, y) => write!(w, "[{}, {}..]", x, y)?,
            BinopRetVar(x, y, f) => {
                match **f {
                    Lt => write!(w, "compute::lt({}, {})", x, y)?,
                    Le => write!(w, "compute::le({}, {})", x, y)?,
                    Gt => write!(w, "compute::gt({}, {})", x, y)?,
                    Ge => write!(w, "compute::ge({}, {})", x, y)?,
                    Add => write!(w, "compute::add({}, {})", x, y)?,
                    Sub => write!(w, "compute::sub({}, {})", x, y)?,
                    Mul => write!(w, "compute::mul({}, {})", x, y)?,
                    Div => write!(w, "compute::div({}, {})", x, y)?,
                    Pow => write!(w, "compute::pow({}, {})", x, y)?,
                    Rem => write!(w, "compute::rem({}, {})", x, y)?,
                    Eq => write!(w, "compute::eq({}, {})", x, y)?,
                    Concat => write!(w, "compute::concat({}, {})", x, y)?,
                    Push => write!(w, "compute::push({}, {})", x, y)?,
                    PushFront => write!(w, "compute::push_front({}, {})", x, y)?,
                    Max2 => write!(w, "compute::max2({}, {})", x, y)?,
                    Min2 => write!(w, "compute::min2({}, {})", x, y)?,
                    Item => write!(w, "compute::item({}, {})", x, y)?,
                    Base => write!(w, "compute::base({}, {})", x, y)?,
                    Atan2 => write!(w, "compute::atan2({}, {})", x, y)?,
                    _ => write!(w, "{:?}", self)?,
                }
            }
            TernopRetVar(x, y, z, f) => {
                match **f {
                    Range => write!(w, "compute::range({}, {}, {})", x, y, z)?,
                    Rangel => write!(w, "compute::rangel({}, {}, {})", x, y, z)?,
                    Ranger => write!(w, "compute::ranger({}, {}, {})", x, y, z)?,
                    Rangem => write!(w, "compute::rangem({}, {}, {})", x, y, z)?,
                    _ => write!(w, "{:?}", self)?,
                }
            }
            UnopRetVar(x, f) => {
                match **f {
                    Neg => write!(w, "compute::neg({})", x)?,
                    Reci => write!(w, "compute::reci({})", x)?,
                    Abs => write!(w, "compute::abs({})", x)?,
                    Len => write!(w, "compute::len({})", x)?,
                    Prob => write!(w, "compute::prob({})", x)?,
                    Probl => write!(w, "compute::probl({})", x)?,
                    Probr => write!(w, "compute::probr({})", x)?,
                    Probm => write!(w, "compute::probm({})", x)?,
                    Sqrt => write!(w, "compute::sqrt({})", x)?,
                    Ln => write!(w, "compute::ln({})", x)?,
                    Log2 => write!(w, "compute::log2({})", x)?,
                    Log10 => write!(w, "compute::log10({})", x)?,
                    Exp => write!(w, "compute::exp({})", x)?,
                    Sin => write!(w, "compute::sin({})", x)?,
                    Asin => write!(w, "compute::asin({})", x)?,
                    Cos => write!(w, "compute::cos({})", x)?,
                    Acos => write!(w, "compute::acos({})", x)?,
                    Tan => write!(w, "compute::tan({})", x)?,
                    Atan => write!(w, "compute::atan({})", x)?,
                    IsSquareMat => write!(w, "compute::is_square_mat({})", x)?,
                    Arity => write!(w, "compute::arity({})", x)?,
                    TypeOf => write!(w, "compute::type_of({})", x)?,
                    _ => write!(w, "{:?}", self)?,
                }
            }
            // _ => write!(w, "{:?}", self)?,
        }
        Ok(())
    }
}