module Ring

/*
  Ring normalization for the `algebra` tactic (#1190).

  `algebra` proves an equation `a = b` whose sides are equal as
  polynomials. It writes each side as an expression (`RExpr`) over
  atoms, the maximal subterms that are not sums, products, constants,
  powers with a literal exponent, or (in a ring) differences and
  negations, and instantiates
  `semiring_prove` or `ring_prove` at the goal's type. The checker
  evaluates the normal forms of both expressions and the denotation of
  each, so the premise becomes `true` and the conclusion the goal as the
  user wrote it; nothing about the tactic is trusted.

  A normal form is a list of monomials `mono(c, es)`: the coefficient
  `c`, a UInt, times the product of the atoms raised to the UInt
  exponents `es` (atom 0 first), so the checker computes with them in
  binary. The list is sorted by `ring_mono_less`
  and has no zero coefficients, so equal polynomials have equal normal
  forms. In a ring, an expression is normalized to two such polynomials
  `P` and `N`, its value being `P - N`, and `a = b` holds when
  `P(a) + N(b)` and `P(b) + N(a)` have the same normal form.

  In a field (#1322), an expression with inverses, quotients, and
  fraction constants is brought to a fraction `n / d` of two ring
  expressions (`field_frac`), and `a = b` holds when `n(a) * d(b)` and
  `n(b) * d(a)` are equal in the ring, provided each factor that is
  inverted is nonzero (`field_conds`). The checker passes those factors
  to `field_prove` and proves that they are nonzero.
*/

// The tactic builds expressions from lists and Nat indices, so their
// names must be in scope wherever this is.
public import UInt
public import Nat
public import Int
import Base
public import List
public import Algebra

union RExpr {
  rvar(Nat)
  rzero
  rone
  rconst(UInt)
  radd(RExpr, RExpr)
  rsub(RExpr, RExpr)
  rneg(RExpr)
  rmul(RExpr, RExpr)
  rpow(RExpr, UInt)
  rinv(RExpr)
  rdiv(RExpr, RExpr)
  rfrac(UInt, UInt)
}

recursive ring_get<T> where { CommSemiring<T> } (List<T>, Nat) -> T {
  ring_get(empty, i) = zero
  ring_get(node(x, xs), i) =
    switch i { case zero { x } case suc(j) { ring_get(xs, j) } }
}

fun ring_tail<T>(xs : List<T>) {
  switch xs { case empty { xs } case node(y, ys) { ys } }
}

// In a semiring, differences and negations are not normalized, and in
// a semiring or ring, inverses and quotients; the tactic leaves them
// in atoms.
recursive ring_sdenote<T> where { CommSemiring<T> } (RExpr, List<T>) -> T {
  ring_sdenote(rvar(i), env) = ring_get(env, i)
  ring_sdenote(rzero, env) = zero
  ring_sdenote(rone, env) = one
  ring_sdenote(rconst(n), env) = from_uint(n)
  ring_sdenote(radd(a, b), env) = ring_sdenote(a, env) + ring_sdenote(b, env)
  ring_sdenote(rsub(a, b), env) = zero
  ring_sdenote(rneg(a), env) = zero
  ring_sdenote(rmul(a, b), env) = ring_sdenote(a, env) * ring_sdenote(b, env)
  ring_sdenote(rpow(a, k), env) = ring_sdenote(a, env) ^ k
  ring_sdenote(rinv(a), env) = zero
  ring_sdenote(rdiv(a, b), env) = zero
  ring_sdenote(rfrac(n, d), env) = zero
}

recursive ring_denote<T> where { CommRing<T> } (RExpr, List<T>) -> T {
  ring_denote(rvar(i), env) = ring_get(env, i)
  ring_denote(rzero, env) = zero
  ring_denote(rone, env) = one
  ring_denote(rconst(n), env) = from_uint(n)
  ring_denote(radd(a, b), env) = ring_denote(a, env) + ring_denote(b, env)
  ring_denote(rsub(a, b), env) = ring_denote(a, env) - ring_denote(b, env)
  ring_denote(rneg(a), env) = - ring_denote(a, env)
  ring_denote(rmul(a, b), env) = ring_denote(a, env) * ring_denote(b, env)
  ring_denote(rpow(a, k), env) = ring_denote(a, env) ^ k
  ring_denote(rinv(a), env) = zero
  ring_denote(rdiv(a, b), env) = zero
  ring_denote(rfrac(n, d), env) = zero
}

/********************** Normal forms **********************/

union Mono { mono(UInt, List<UInt>) }

// The product of two monomials adds their exponents.
recursive ring_vadd(List<UInt>, List<UInt>) -> List<UInt> {
  ring_vadd(empty, ys) = ys
  ring_vadd(node(x, xs), ys) =
    switch ys {
      case empty { node(x, xs) }
      case node(y, ys2) { node(x + y, ring_vadd(xs, ys2)) }
    }
}

recursive ring_vunit(Nat) -> List<UInt> {
  ring_vunit(zero) = [1]
  ring_vunit(suc(i)) = node(0, ring_vunit(i))
}

recursive ring_mono_less(List<UInt>, List<UInt>) -> bool {
  ring_mono_less(empty, ys) = switch ys { case empty { false } case node(y, ys2) { true } }
  ring_mono_less(node(x, xs), ys) =
    switch ys {
      case empty { false }
      case node(y, ys2) { if x = y then ring_mono_less(xs, ys2) else x < y }
    }
}

// Add the monomial `c` times `m`.
recursive ring_insert(List<Mono>, UInt, List<UInt>) -> List<Mono> {
  ring_insert(empty, c, m) = [mono(c, m)]
  ring_insert(node(t, ps), c, m) =
    switch t {
      case mono(d, m2) {
        if m = m2 then node(mono(c + d, m), ps)
        else if ring_mono_less(m, m2) then node(mono(c, m), node(t, ps))
        else node(t, ring_insert(ps, c, m))
      }
    }
}

recursive ring_add(List<Mono>, List<Mono>) -> List<Mono> {
  ring_add(empty, qs) = qs
  ring_add(node(t, ps), qs) =
    switch t { case mono(c, m) { ring_insert(ring_add(ps, qs), c, m) } }
}

// Multiply by the monomial `c` times `m`.
recursive ring_scale(List<Mono>, UInt, List<UInt>) -> List<Mono> {
  ring_scale(empty, c, m) = empty
  ring_scale(node(t, ps), c, m) =
    switch t {
      case mono(d, m2) { node(mono(c * d, ring_vadd(m, m2)), ring_scale(ps, c, m)) }
    }
}

recursive ring_mul(List<Mono>, List<Mono>) -> List<Mono> {
  ring_mul(empty, qs) = empty
  ring_mul(node(t, ps), qs) =
    switch t { case mono(c, m) { ring_add(ring_scale(qs, c, m), ring_mul(ps, qs)) } }
}

recursive ring_npow(Nat, List<Mono>) -> List<Mono> {
  ring_npow(zero, ps) = [mono(1, [])]
  ring_npow(suc(n), ps) = ring_mul(ps, ring_npow(n, ps))
}

recursive ring_norm(RExpr) -> List<Mono> {
  ring_norm(rvar(i)) = [mono(1, ring_vunit(i))]
  ring_norm(rzero) = []
  ring_norm(rone) = [mono(1, [])]
  ring_norm(rconst(n)) = if n = 0 then [] else [mono(n, [])]
  ring_norm(radd(a, b)) = ring_add(ring_norm(a), ring_norm(b))
  ring_norm(rsub(a, b)) = []
  ring_norm(rneg(a)) = []
  ring_norm(rmul(a, b)) = ring_mul(ring_norm(a), ring_norm(b))
  ring_norm(rpow(a, k)) = ring_npow(toNat(k), ring_norm(a))
  ring_norm(rinv(a)) = []
  ring_norm(rdiv(a, b)) = []
  ring_norm(rfrac(n, d)) = []
}

recursive ring_pow<T> where { CommSemiring<T> } (Nat, T) -> T {
  ring_pow(zero, x) = one
  ring_pow(suc(n), x) = x * ring_pow(n, x)
}

recursive ring_mdenote<T> where { CommSemiring<T> } (List<UInt>, List<T>) -> T {
  ring_mdenote(empty, env) = one
  ring_mdenote(node(e, es), env) =
    ring_get(env, zero) ^ e * ring_mdenote(es, ring_tail(env))
}

recursive ring_pdenote<T> where { CommSemiring<T> } (List<Mono>, List<T>) -> T {
  ring_pdenote(empty, env) = zero
  ring_pdenote(node(t, ps), env) =
    switch t {
      case mono(c, m) { from_uint(c) * ring_mdenote(m, env) + ring_pdenote(ps, env) }
    }
}

/********************** Semiring lemmas **********************/

lemma ring_zero_add: all T:type where { CommSemiring<T> }. all x:T. zero + x = x
proof
  arbitrary T:type where { CommSemiring<T> }
  arbitrary x:T
  replace add_commute[zero, x]
  add_zero[x]
end

lemma ring_one_mult: all T:type where { CommSemiring<T> }. all x:T. one * x = x
proof
  arbitrary T:type where { CommSemiring<T> }
  arbitrary x:T
  replace mult_commute[one, x]
  mult_one[x]
end

lemma ring_zero_mult: all T:type where { CommSemiring<T> }. all x:T. zero * x = zero
proof
  arbitrary T:type where { CommSemiring<T> }
  arbitrary x:T
  replace mult_commute[zero, x]
  mult_zero[x]
end

lemma ring_dist_right: all T:type where { CommSemiring<T> }.
  all x:T, y:T, z:T. (x + y) * z = x * z + y * z
proof
  arbitrary T:type where { CommSemiring<T> }
  arbitrary x:T, y:T, z:T
  replace mult_commute[x + y, z] | dist_mult_add[z, x, y]
        | mult_commute[z, x] | mult_commute[z, y].
end

lemma ring_add_left_comm: all T:type where { CommSemiring<T> }.
  all x:T, y:T, z:T. x + (y + z) = y + (x + z)
proof
  arbitrary T:type where { CommSemiring<T> }
  arbitrary x:T, y:T, z:T
  replace symmetric add_assoc[x, y, z] | add_commute[x, y] | add_assoc[y, x, z].
end

lemma ring_mult_shuffle: all T:type where { CommSemiring<T> }.
  all a:T, b:T, c:T, d:T. (a * b) * (c * d) = (a * c) * (b * d)
proof
  arbitrary T:type where { CommSemiring<T> }
  arbitrary a:T, b:T, c:T, d:T
  equations
        (a * b) * (c * d) = a * (b * (c * d))   by mult_assoc[a, b, c * d]
      ... = a * ((b * c) * d)   by replace symmetric mult_assoc[b, c, d].
      ... = a * ((c * b) * d)   by replace mult_commute[b, c].
      ... = a * (c * (b * d))   by replace mult_assoc[c, b, d].
      ... = (a * c) * (b * d)   by symmetric mult_assoc[a, c, b * d]
end

/********************** Soundness in a semiring **********************/

lemma ring_pow_add: all T:type where { CommSemiring<T> }.
  all a:Nat. all b:Nat, x:T.
  ring_pow(a + b, x) = ring_pow(a, x) * ring_pow(b, x)
proof
  arbitrary T:type where { CommSemiring<T> }
  induction Nat
  case zero {
    arbitrary b:Nat, x:T
    expand ring_pow
    symmetric ring_one_mult<T>
  }
  case suc(a) assume IH {
    arbitrary b:Nat, x:T
    expand operator+ | ring_pow
    replace IH[b, x]
    symmetric mult_assoc
  }
end

// The semiring's `^` is `ring_pow` on the exponent as a Nat.
lemma ring_pow_uint: all T:type where { CommSemiring<T> }.
  all x:T, k:UInt. x ^ k = ring_pow(toNat(k), x)
proof
  arbitrary T:type where { CommSemiring<T> }
  arbitrary x:T
  define P = fun k:UInt { x ^ k = ring_pow(toNat(k), x) }
  have base: P(0) by {
    expand P
    have z: toNat(fromNat(â„•0)) = zero by uint_toNat_fromNat[â„•0]
    replace pow_zero | z
    expand ring_pow.
  }
  have step: all m:UInt. if P(m) then P(1 + m) by {
    arbitrary m:UInt
    assume IH
    expand P
    have toNat_one: toNat(fromNat(â„•1)) = â„•1 by uint_toNat_fromNat[â„•1]
    have nat_eq: toNat((1:UInt) + m) = suc(toNat(m)) by {
      replace toNat_add | toNat_one
      evaluate
    }
    replace pow_one_add | nat_eq | (expand P in IH)
    expand ring_pow.
  }
  arbitrary k:UInt
  have all_P: all n:UInt. P(n) by apply uint_induction[P] to base, step
  expand P in all_P[k]
end

lemma ring_upow_add: all T:type where { CommSemiring<T> }.
  all a:UInt, b:UInt, x:T. x ^ (a + b) = x ^ a * x ^ b
proof
  arbitrary T:type where { CommSemiring<T> }
  arbitrary a:UInt, b:UInt, x:T
  replace ring_pow_uint<T>[x, a + b] | ring_pow_uint<T>[x, a]
        | ring_pow_uint<T>[x, b] | toNat_add
  ring_pow_add<T>
end

lemma ring_upow_one: all T:type where { CommSemiring<T> }. all x:T. x ^ (1:UInt) = x
proof
  arbitrary T:type where { CommSemiring<T> }
  arbitrary x:T
  equations
        x ^ (1:UInt) = x * x ^ (0:UInt)   by pow_one_add[x, 0]
      ... = x                             by replace pow_zero | mult_one.
end

lemma ring_mdenote_vadd: all T:type where { CommSemiring<T> }.
  all m1:List<UInt>. all m2:List<UInt>, env:List<T>.
  ring_mdenote(ring_vadd(m1, m2), env)
  = ring_mdenote(m1, env) * ring_mdenote(m2, env)
proof
  arbitrary T:type where { CommSemiring<T> }
  induction List<UInt>
  case empty {
    arbitrary m2:List<UInt>, env:List<T>
    expand ring_vadd | ring_mdenote
    symmetric ring_one_mult<T>
  }
  case node(e, es) assume IH {
    arbitrary m2:List<UInt>, env:List<T>
    switch m2 {
      case empty {
        expand ring_vadd | ring_mdenote
        symmetric mult_one
      }
      case node(f, fs) {
        expand ring_vadd | ring_mdenote
        replace ring_upow_add<T> | IH[fs, ring_tail(env)]
        ring_mult_shuffle<T>
      }
    }
  }
end

lemma ring_mdenote_vunit: all T:type where { CommSemiring<T> }.
  all i:Nat. all env:List<T>.
  ring_mdenote(ring_vunit(i), env) = ring_get(env, i)
proof
  arbitrary T:type where { CommSemiring<T> }
  induction Nat
  case zero {
    arbitrary env:List<T>
    expand ring_vunit | 2 * ring_mdenote
    replace ring_upow_one<T> | mult_one.
  }
  case suc(j) assume IH {
    arbitrary env:List<T>
    expand ring_vunit | ring_mdenote
    replace pow_zero | IH[ring_tail(env)] | ring_one_mult<T>
    switch env {
      case empty { evaluate }
      case node(x, xs) { evaluate }
    }
  }
end

lemma ring_pdenote_insert: all T:type where { CommSemiring<T> }.
  all ps:List<Mono>. all c:UInt, m:List<UInt>, env:List<T>.
  ring_pdenote(ring_insert(ps, c, m), env)
  = from_uint(c) * ring_mdenote(m, env) + ring_pdenote(ps, env)
proof
  arbitrary T:type where { CommSemiring<T> }
  induction List<Mono>
  case empty {
    arbitrary c:UInt, m:List<UInt>, env:List<T>
    expand ring_insert | 2*ring_pdenote.
  }
  case node(t, ps) assume IH {
    arbitrary c:UInt, m:List<UInt>, env:List<T>
    switch t {
      case mono(d, m2) {
        expand ring_insert
        switch m = m2 {
          case true assume e {
            replace e
            expand ring_pdenote
            replace from_uint_add[c, d] | ring_dist_right<T> | add_assoc.
          }
          case false {
            switch ring_mono_less(m, m2) {
              case true {
                expand 2*ring_pdenote.
              }
              case false {
                expand ring_pdenote
                replace IH[c, m, env]
                ring_add_left_comm<T>
              }
            }
          }
        }
      }
    }
  }
end

lemma ring_pdenote_add: all T:type where { CommSemiring<T> }.
  all ps:List<Mono>. all qs:List<Mono>, env:List<T>.
  ring_pdenote(ring_add(ps, qs), env)
  = ring_pdenote(ps, env) + ring_pdenote(qs, env)
proof
  arbitrary T:type where { CommSemiring<T> }
  induction List<Mono>
  case empty {
    arbitrary qs:List<Mono>, env:List<T>
    expand ring_add | ring_pdenote
    symmetric ring_zero_add<T>
  }
  case node(t, ps) assume IH {
    arbitrary qs:List<Mono>, env:List<T>
    switch t {
      case mono(c, m) {
        expand ring_add | ring_pdenote
        replace ring_pdenote_insert<T> | IH[qs, env]
        symmetric add_assoc
      }
    }
  }
end

lemma ring_pdenote_scale: all T:type where { CommSemiring<T> }.
  all ps:List<Mono>. all c:UInt, m:List<UInt>, env:List<T>.
  ring_pdenote(ring_scale(ps, c, m), env)
  = (from_uint(c) * ring_mdenote(m, env)) * ring_pdenote(ps, env)
proof
  arbitrary T:type where { CommSemiring<T> }
  induction List<Mono>
  case empty {
    arbitrary c:UInt, m:List<UInt>, env:List<T>
    expand ring_scale | ring_pdenote
    symmetric mult_zero
  }
  case node(t, ps) assume IH {
    arbitrary c:UInt, m:List<UInt>, env:List<T>
    switch t {
      case mono(d, m2) {
        expand ring_scale | ring_pdenote
        replace IH[c, m, env] | from_uint_mult[c, d] | ring_mdenote_vadd<T>
              | dist_mult_add
              | ring_mult_shuffle<T>[from_uint(c), from_uint(d), ring_mdenote(m, env),
                                     ring_mdenote(m2, env)].
      }
    }
  }
end

lemma ring_pdenote_mul: all T:type where { CommSemiring<T> }.
  all ps:List<Mono>. all qs:List<Mono>, env:List<T>.
  ring_pdenote(ring_mul(ps, qs), env)
  = ring_pdenote(ps, env) * ring_pdenote(qs, env)
proof
  arbitrary T:type where { CommSemiring<T> }
  induction List<Mono>
  case empty {
    arbitrary qs:List<Mono>, env:List<T>
    expand ring_mul | ring_pdenote
    symmetric ring_zero_mult<T>
  }
  case node(t, ps) assume IH {
    arbitrary qs:List<Mono>, env:List<T>
    switch t {
      case mono(c, m) {
        expand ring_mul | ring_pdenote
        replace ring_pdenote_add<T> | ring_pdenote_scale<T> | IH[qs, env]
        symmetric ring_dist_right<T>
      }
    }
  }
end

lemma ring_pdenote_npow: all T:type where { CommSemiring<T> }.
  all n:Nat. all ps:List<Mono>, env:List<T>.
  ring_pdenote(ring_npow(n, ps), env) = ring_pow(n, ring_pdenote(ps, env))
proof
  arbitrary T:type where { CommSemiring<T> }
  induction Nat
  case zero {
    arbitrary ps:List<Mono>, env:List<T>
    expand ring_npow | ring_pow | 2*ring_pdenote | ring_mdenote
    replace from_uint_one | mult_one | add_zero.
  }
  case suc(n) assume IH {
    arbitrary ps:List<Mono>, env:List<T>
    expand ring_npow | ring_pow
    replace ring_pdenote_mul<T> | IH[ps, env].
  }
end

// The normal form denotes the expression.
lemma ring_norm_sound: all T:type where { CommSemiring<T> }.
  all e:RExpr. all env:List<T>.
  ring_pdenote(ring_norm(e), env) = ring_sdenote(e, env)
proof
  arbitrary T:type where { CommSemiring<T> }
  induction RExpr
  case rvar(i) {
    arbitrary env:List<T>
    expand ring_norm | ring_sdenote | 2*ring_pdenote
    replace ring_mdenote_vunit<T> | from_uint_one | ring_one_mult<T> | add_zero.
  }
  case rzero {
    arbitrary env:List<T>
    expand ring_norm | ring_sdenote | ring_pdenote.
  }
  case rone {
    arbitrary env:List<T>
    expand ring_norm | ring_sdenote | 2*ring_pdenote | ring_mdenote
    replace from_uint_one | mult_one | add_zero.
  }
  case rconst(n) {
    arbitrary env:List<T>
    expand ring_norm | ring_sdenote
    switch n = 0 {
      case true assume n0 {
        replace n0
        expand ring_pdenote
        symmetric from_uint_zero
      }
      case false {
        expand 2*ring_pdenote | ring_mdenote
        replace mult_one | add_zero.
      }
    }
  }
  case radd(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    expand ring_norm | ring_sdenote
    replace ring_pdenote_add<T> | IHa[env] | IHb[env].
  }
  case rsub(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    expand ring_norm | ring_sdenote | ring_pdenote.
  }
  case rneg(a) assume IHa {
    arbitrary env:List<T>
    expand ring_norm | ring_sdenote | ring_pdenote.
  }
  case rmul(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    expand ring_norm | ring_sdenote
    replace ring_pdenote_mul<T> | IHa[env] | IHb[env].
  }
  case rpow(a, k) assume IHa {
    arbitrary env:List<T>
    expand ring_norm | ring_sdenote
    replace ring_pdenote_npow<T> | IHa[env] | ring_pow_uint<T>[ring_sdenote(a, env), k].
  }
  case rinv(a) assume IHa {
    arbitrary env:List<T>
    expand ring_norm | ring_sdenote | ring_pdenote.
  }
  case rdiv(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    expand ring_norm | ring_sdenote | ring_pdenote.
  }
  case rfrac(n, d) {
    arbitrary env:List<T>
    expand ring_norm | ring_sdenote | ring_pdenote.
  }
end

theorem semiring_prove: all T:type where { CommSemiring<T> }.
  all env:List<T>, a:RExpr, b:RExpr.
  if ring_norm(a) = ring_norm(b)
  then ring_sdenote(a, env) = ring_sdenote(b, env)
proof
  arbitrary T:type where { CommSemiring<T> }
  arbitrary env:List<T>, a:RExpr, b:RExpr
  assume eq
  replace symmetric ring_norm_sound<T>[a, env] | symmetric ring_norm_sound<T>[b, env]
        | eq.
end

/********************** Soundness in a ring **********************/

// The difference of two polynomials, the value of a ring expression.
union PN { pn(List<Mono>, List<Mono>) }

fun pn_pos(x : PN) { switch x { case pn(p, n) { p } } }
fun pn_neg(x : PN) { switch x { case pn(p, n) { n } } }

fun pn_add(x : PN, y : PN) {
  pn(ring_add(pn_pos(x), pn_pos(y)), ring_add(pn_neg(x), pn_neg(y)))
}

fun pn_swap(x : PN) { pn(pn_neg(x), pn_pos(x)) }

fun pn_mul(x : PN, y : PN) {
  pn(ring_add(ring_mul(pn_pos(x), pn_pos(y)), ring_mul(pn_neg(x), pn_neg(y))),
     ring_add(ring_mul(pn_pos(x), pn_neg(y)), ring_mul(pn_neg(x), pn_pos(y))))
}

recursive pn_npow(Nat, PN) -> PN {
  pn_npow(zero, x) = pn(ring_norm(rone), [])
  pn_npow(suc(n), x) = pn_mul(x, pn_npow(n, x))
}

recursive ring_pn(RExpr) -> PN {
  ring_pn(rvar(i)) = pn(ring_norm(rvar(i)), [])
  ring_pn(rzero) = pn([], [])
  ring_pn(rone) = pn(ring_norm(rone), [])
  ring_pn(rconst(n)) = pn(ring_norm(rconst(n)), [])
  ring_pn(radd(a, b)) = pn_add(ring_pn(a), ring_pn(b))
  ring_pn(rsub(a, b)) = pn_add(ring_pn(a), pn_swap(ring_pn(b)))
  ring_pn(rneg(a)) = pn_swap(ring_pn(a))
  ring_pn(rmul(a, b)) = pn_mul(ring_pn(a), ring_pn(b))
  ring_pn(rpow(a, k)) = pn_npow(toNat(k), ring_pn(a))
  ring_pn(rinv(a)) = pn([], [])
  ring_pn(rdiv(a, b)) = pn([], [])
  ring_pn(rfrac(n, d)) = pn([], [])
}

// `a - b` is `(P(a) - N(a)) - (P(b) - N(b))`, which is zero when
// `P(a) + N(b) = P(b) + N(a)`.
fun ring_pn_eq(a : RExpr, b : RExpr) {
  ring_add(pn_pos(ring_pn(a)), pn_neg(ring_pn(b)))
  = ring_add(pn_pos(ring_pn(b)), pn_neg(ring_pn(a)))
}

// If x = p - n and y = q - m, and p - n = q - m, then x = y.
lemma ring_pn_cancel: all T:type where { CommRing<T> }.
  all x:T, y:T, p:T, q:T, n:T, m:T.
  if x + n = p and y + m = q and p + m = q + n then x = y
proof
  arbitrary T:type where { CommRing<T> }
  arbitrary x:T, y:T, p:T, q:T, n:T, m:T
  assume h
  have xnm: x + (n + m) = y + (n + m) by
    equations
          x + (n + m) = (x + n) + m     by algebra
        ... = p + m                     by replace conjunct 0 of h.
        ... = q + n                     by conjunct 2 of h
        ... = (y + m) + n               by replace symmetric conjunct 1 of h.
        ... = y + (n + m)               by algebra
  equations
        x = #x + ((n + m) + - (n + m))#     by replace add_inverse | add_zero.
      ... = (x + (n + m)) + - (n + m)       by algebra
      ... = (y + (n + m)) + - (n + m)       by replace xnm.
      ... = y + ((n + m) + - (n + m))       by algebra
      ... = y                               by replace add_inverse | add_zero.
end

lemma ring_pn_sub: all T:type where { CommRing<T> }.
  all x:T, y:T, n:T, m:T. (x - y) + (n + (y + m)) = (x + n) + m
proof
  arbitrary T:type where { CommRing<T> }
  arbitrary x:T, y:T, n:T, m:T
  replace sub_def
  equations
        (x + - y) + (n + (y + m)) = ((x + n) + m) + (y + - y)   by algebra
      ... = (x + n) + m                                        by replace add_inverse | add_zero.
end

lemma ring_pn_neg: all T:type where { CommRing<T> }.
  all x:T, n:T. - x + (x + n) = n
proof
  arbitrary T:type where { CommRing<T> }
  arbitrary x:T, n:T
  equations
        - x + (x + n) = n + (x + - x)   by algebra
      ... = n                           by replace add_inverse | add_zero.
end

lemma pn_pos_pn: all p:List<Mono>, n:List<Mono>. pn_pos(pn(p, n)) = p
proof
  arbitrary p:List<Mono>, n:List<Mono>
  expand pn_pos.
end

lemma pn_neg_pn: all p:List<Mono>, n:List<Mono>. pn_neg(pn(p, n)) = n
proof
  arbitrary p:List<Mono>, n:List<Mono>
  expand pn_neg.
end

// If x = P(X) - N(X) and y = P(Y) - N(Y), then x * y is the value of
// their product.
lemma pn_mul_sound: all T:type where { CommRing<T> }.
  all x:T, y:T, X:PN, Y:PN, env:List<T>.
  if x + ring_pdenote(pn_neg(X), env) = ring_pdenote(pn_pos(X), env)
     and y + ring_pdenote(pn_neg(Y), env) = ring_pdenote(pn_pos(Y), env)
  then x * y + ring_pdenote(pn_neg(pn_mul(X, Y)), env)
     = ring_pdenote(pn_pos(pn_mul(X, Y)), env)
proof
  arbitrary T:type where { CommRing<T> }
  arbitrary x:T, y:T, X:PN, Y:PN, env:List<T>
  assume h
  expand pn_mul
  replace pn_pos_pn | pn_neg_pn | ring_pdenote_add<T> | ring_pdenote_mul<T>
        | symmetric conjunct 0 of h | symmetric conjunct 1 of h
  algebra
end

lemma pn_npow_sound: all T:type where { CommRing<T> }.
  all n:Nat. all x:T, X:PN, env:List<T>.
  if x + ring_pdenote(pn_neg(X), env) = ring_pdenote(pn_pos(X), env)
  then ring_pow(n, x) + ring_pdenote(pn_neg(pn_npow(n, X)), env)
     = ring_pdenote(pn_pos(pn_npow(n, X)), env)
proof
  arbitrary T:type where { CommRing<T> }
  induction Nat
  case zero {
    arbitrary x:T, X:PN, env:List<T>
    assume h
    expand pn_npow | ring_pow
    replace pn_pos_pn | pn_neg_pn | ring_norm_sound<T>
    expand ring_sdenote | ring_pdenote
    add_zero
  }
  case suc(n) assume IH {
    arbitrary x:T, X:PN, env:List<T>
    assume h
    expand pn_npow | ring_pow
    apply pn_mul_sound<T>[x, ring_pow(n, x), X, pn_npow(n, X), env]
      to h, (apply IH[x, X, env] to h)
  }
end

// The value of an expression is P - N, stated without subtraction.
lemma ring_pn_sound: all T:type where { CommRing<T> }.
  all e:RExpr. all env:List<T>.
  ring_denote(e, env) + ring_pdenote(pn_neg(ring_pn(e)), env)
  = ring_pdenote(pn_pos(ring_pn(e)), env)
proof
  arbitrary T:type where { CommRing<T> }
  induction RExpr
  case rvar(i) {
    arbitrary env:List<T>
    expand ring_pn
    replace pn_pos_pn | pn_neg_pn | ring_norm_sound<T>
    expand ring_denote | ring_sdenote | ring_pdenote
    add_zero
  }
  case rzero {
    arbitrary env:List<T>
    expand ring_pn
    replace pn_pos_pn | pn_neg_pn
    expand ring_denote | ring_pdenote
    add_zero
  }
  case rone {
    arbitrary env:List<T>
    expand ring_pn
    replace pn_pos_pn | pn_neg_pn | ring_norm_sound<T>
    expand ring_denote | ring_sdenote | ring_pdenote
    add_zero
  }
  case rconst(n) {
    arbitrary env:List<T>
    expand ring_pn
    replace pn_pos_pn | pn_neg_pn | ring_norm_sound<T>
    expand ring_denote | ring_sdenote | ring_pdenote
    add_zero
  }
  case radd(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    expand ring_pn | pn_add | ring_denote
    replace pn_pos_pn | pn_neg_pn | ring_pdenote_add<T> | symmetric IHa[env] | symmetric IHb[env]
    algebra
  }
  case rsub(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    expand ring_pn | pn_swap | pn_add | ring_denote
    replace pn_pos_pn | pn_neg_pn | pn_pos_pn | pn_neg_pn | ring_pdenote_add<T> | symmetric IHa[env] | symmetric IHb[env]
    ring_pn_sub<T>
  }
  case rneg(a) assume IHa {
    arbitrary env:List<T>
    expand ring_pn | pn_swap | ring_denote
    replace pn_pos_pn | pn_neg_pn | symmetric IHa[env]
    ring_pn_neg<T>
  }
  case rmul(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    expand ring_pn | ring_denote
    apply pn_mul_sound<T> to IHa[env], IHb[env]
  }
  case rpow(a, k) assume IHa {
    arbitrary env:List<T>
    expand ring_pn | ring_denote
    replace ring_pow_uint<T>[ring_denote(a, env), k]
    apply pn_npow_sound<T>[toNat(k)][ring_denote(a, env), ring_pn(a), env] to IHa[env]
  }
  case rinv(a) assume IHa {
    arbitrary env:List<T>
    expand ring_pn
    replace pn_pos_pn | pn_neg_pn
    expand ring_denote | ring_pdenote
    add_zero
  }
  case rdiv(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    expand ring_pn
    replace pn_pos_pn | pn_neg_pn
    expand ring_denote | ring_pdenote
    add_zero
  }
  case rfrac(n, d) {
    arbitrary env:List<T>
    expand ring_pn
    replace pn_pos_pn | pn_neg_pn
    expand ring_denote | ring_pdenote
    add_zero
  }
end

theorem ring_prove: all T:type where { CommRing<T> }.
  all env:List<T>, a:RExpr, b:RExpr.
  if ring_pn_eq(a, b)
  then ring_denote(a, env) = ring_denote(b, env)
proof
  arbitrary T:type where { CommRing<T> }
  arbitrary env:List<T>, a:RExpr, b:RExpr
  assume h
  have e: ring_pdenote(pn_pos(ring_pn(a)), env) + ring_pdenote(pn_neg(ring_pn(b)), env)
        = ring_pdenote(pn_pos(ring_pn(b)), env) + ring_pdenote(pn_neg(ring_pn(a)), env)
    by replace symmetric ring_pdenote_add<T> | (expand ring_pn_eq in h).
  apply ring_pn_cancel<T> to ring_pn_sound<T>[a, env], ring_pn_sound<T>[b, env], e
end

/********************** Soundness in a field **********************/

recursive field_denote<T> where { Field<T> } (RExpr, List<T>) -> T {
  field_denote(rvar(i), env) = ring_get(env, i)
  field_denote(rzero, env) = zero
  field_denote(rone, env) = one
  field_denote(rconst(n), env) = from_uint(n)
  field_denote(radd(a, b), env) = field_denote(a, env) + field_denote(b, env)
  field_denote(rsub(a, b), env) = field_denote(a, env) - field_denote(b, env)
  field_denote(rneg(a), env) = - field_denote(a, env)
  field_denote(rmul(a, b), env) = field_denote(a, env) * field_denote(b, env)
  field_denote(rpow(a, k), env) = field_denote(a, env) ^ k
  field_denote(rinv(a), env) = inv(field_denote(a, env))
  field_denote(rdiv(a, b), env) = field_denote(a, env) / field_denote(b, env)
  field_denote(rfrac(n, d), env) = from_frac(n, d)
}

// A fraction: a numerator and a denominator, both ring expressions.
union FP { fp(RExpr, RExpr) }

fun fp_num(x : FP) { switch x { case fp(n, d) { n } } }
fun fp_den(x : FP) { switch x { case fp(n, d) { d } } }

// The product, without the factor `rone`, so that side conditions read
// as the user wrote them.
fun field_mul(a : RExpr, b : RExpr) {
  if a = rone then b else if b = rone then a else rmul(a, b)
}

fun fp_add(x : FP, y : FP) {
  fp(radd(field_mul(fp_num(x), fp_den(y)), field_mul(fp_num(y), fp_den(x))),
     field_mul(fp_den(x), fp_den(y)))
}

fun fp_sub(x : FP, y : FP) {
  fp(rsub(field_mul(fp_num(x), fp_den(y)), field_mul(fp_num(y), fp_den(x))),
     field_mul(fp_den(x), fp_den(y)))
}

fun fp_neg(x : FP) { fp(rneg(fp_num(x)), fp_den(x)) }

fun fp_mul(x : FP, y : FP) {
  fp(field_mul(fp_num(x), fp_num(y)), field_mul(fp_den(x), fp_den(y)))
}

fun fp_pow(x : FP, k : UInt) { fp(rpow(fp_num(x), k), rpow(fp_den(x), k)) }

fun fp_inv(x : FP) { fp(fp_den(x), fp_num(x)) }

// An expression as a single fraction.
recursive field_frac(RExpr) -> FP {
  field_frac(rvar(i)) = fp(rvar(i), rone)
  field_frac(rzero) = fp(rzero, rone)
  field_frac(rone) = fp(rone, rone)
  field_frac(rconst(n)) = fp(rconst(n), rone)
  field_frac(radd(a, b)) = fp_add(field_frac(a), field_frac(b))
  field_frac(rsub(a, b)) = fp_sub(field_frac(a), field_frac(b))
  field_frac(rneg(a)) = fp_neg(field_frac(a))
  field_frac(rmul(a, b)) = fp_mul(field_frac(a), field_frac(b))
  field_frac(rpow(a, k)) = fp_pow(field_frac(a), k)
  field_frac(rinv(a)) = fp_inv(field_frac(a))
  field_frac(rdiv(a, b)) = fp_mul(field_frac(a), fp_inv(field_frac(b)))
  field_frac(rfrac(n, d)) = fp(rconst(n), rconst(d))
}

// The factors of a product, each of which must be nonzero for the
// product to be.
recursive field_factors(RExpr) -> List<RExpr> {
  field_factors(rvar(i)) = [rvar(i)]
  field_factors(rzero) = [rzero]
  field_factors(rone) = []
  field_factors(rconst(n)) = [rconst(n)]
  field_factors(radd(a, b)) = [radd(a, b)]
  field_factors(rsub(a, b)) = [rsub(a, b)]
  field_factors(rneg(a)) = field_factors(a)
  field_factors(rmul(a, b)) = field_factors(a) ++ field_factors(b)
  field_factors(rpow(a, k)) = if k = 0 then [] else field_factors(a)
  field_factors(rinv(a)) = [rinv(a)]
  field_factors(rdiv(a, b)) = [rdiv(a, b)]
  field_factors(rfrac(n, d)) = [rfrac(n, d)]
}

// The side conditions: the factors of each inverted numerator.
recursive field_conds(RExpr) -> List<RExpr> {
  field_conds(rvar(i)) = []
  field_conds(rzero) = []
  field_conds(rone) = []
  field_conds(rconst(n)) = []
  field_conds(radd(a, b)) = field_conds(a) ++ field_conds(b)
  field_conds(rsub(a, b)) = field_conds(a) ++ field_conds(b)
  field_conds(rneg(a)) = field_conds(a)
  field_conds(rmul(a, b)) = field_conds(a) ++ field_conds(b)
  field_conds(rpow(a, k)) = field_conds(a)
  field_conds(rinv(a)) = field_factors(fp_num(field_frac(a))) ++ field_conds(a)
  field_conds(rdiv(a, b)) =
    field_factors(fp_num(field_frac(b))) ++ (field_conds(a) ++ field_conds(b))
  field_conds(rfrac(n, d)) = [rconst(d)]
}

recursive field_nonzero<T> where { Field<T> } (List<RExpr>, List<T>) -> bool {
  field_nonzero(empty, env) = true
  field_nonzero(node(c, cs), env) =
    not (ring_denote(c, env) = zero) and field_nonzero(cs, env)
}

recursive field_in(List<RExpr>, RExpr) -> bool {
  field_in(empty, c) = false
  field_in(node(d, ds), c) = if c = d then true else field_in(ds, c)
}

recursive field_covers(List<RExpr>, List<RExpr>) -> bool {
  field_covers(empty, cs) = true
  field_covers(node(x, xs), cs) = field_in(cs, x) and field_covers(xs, cs)
}

// The cross products of the two fractions are equal.
fun field_frac_eq(a : RExpr, b : RExpr) {
  ring_pn_eq(rmul(fp_num(field_frac(a)), fp_den(field_frac(b))),
             rmul(fp_num(field_frac(b)), fp_den(field_frac(a))))
}

// `x` is the fraction `X`, whose denominator is nonzero.
fun fp_holds<T> where { Field<T> } (x : T, X : FP, env : List<T>) {
  x * ring_denote(fp_den(X), env) = ring_denote(fp_num(X), env)
  and not (ring_denote(fp_den(X), env) = zero)
}

lemma fp_num_fp: all n:RExpr, d:RExpr. fp_num(fp(n, d)) = n
proof
  arbitrary n:RExpr, d:RExpr
  expand fp_num.
end

lemma fp_den_fp: all n:RExpr, d:RExpr. fp_den(fp(n, d)) = d
proof
  arbitrary n:RExpr, d:RExpr
  expand fp_den.
end

lemma field_mult_cancel: all T:type where { Field<T> }.
  all x:T, y:T, z:T. if not (z = zero) and x * z = y * z then x = y
proof
  arbitrary T:type where { Field<T> }
  arbitrary x:T, y:T, z:T
  assume h
  have zi: z * inv(z) = one by apply mult_inv to conjunct 0 of h
  equations
        x = #x * (z * inv(z))#   by replace zi | mult_one.
      ... = (x * z) * inv(z)     by symmetric mult_assoc
      ... = (y * z) * inv(z)     by replace conjunct 1 of h.
      ... = y * (z * inv(z))     by mult_assoc
      ... = y                    by replace zi | mult_one.
end

lemma field_mult_nonzero: all T:type where { Field<T> }.
  all x:T, y:T. if not (x = zero) and not (y = zero) then not (x * y = zero)
proof
  arbitrary T:type where { Field<T> }
  arbitrary x:T, y:T
  assume h
  assume xy
  have xy2: x * y = zero * y by replace xy | ring_zero_mult<T>.
  have x0: x = zero by apply field_mult_cancel<T>[x, zero, y] to (conjunct 1 of h), xy2
  apply (conjunct 0 of h) to x0
end

lemma field_neg_nonzero: all T:type where { Field<T> }.
  all x:T. if not (x = zero) then not (- x = zero)
proof
  arbitrary T:type where { Field<T> }
  arbitrary x:T
  assume h
  assume nx
  have x0: x = zero by
    equations
          x = #x + zero#  by replace add_zero.
        ... = #x + - x#   by replace nx.
        ... = zero        by add_inverse
  apply h to x0
end

lemma field_ring_pow_nonzero: all T:type where { Field<T> }.
  all n:Nat. all x:T. if not (x = zero) then not (ring_pow(n, x) = zero)
proof
  arbitrary T:type where { Field<T> }
  induction Nat
  case zero {
    arbitrary x:T
    assume h
    expand ring_pow
    one_nonzero
  }
  case suc(n) assume IH {
    arbitrary x:T
    assume h
    expand ring_pow
    apply field_mult_nonzero<T> to h, (apply IH[x] to h)
  }
end

lemma field_pow_nonzero: all T:type where { Field<T> }.
  all x:T, k:UInt. if not (x = zero) then not (x ^ k = zero)
proof
  arbitrary T:type where { Field<T> }
  arbitrary x:T, k:UInt
  assume h
  replace ring_pow_uint<T>[x, k]
  apply field_ring_pow_nonzero<T>[toNat(k)] to h
end

lemma ring_pow_mult: all T:type where { CommSemiring<T> }.
  all n:Nat. all x:T, y:T. ring_pow(n, x * y) = ring_pow(n, x) * ring_pow(n, y)
proof
  arbitrary T:type where { CommSemiring<T> }
  induction Nat
  case zero {
    arbitrary x:T, y:T
    expand ring_pow
    symmetric mult_one
  }
  case suc(n) assume IH {
    arbitrary x:T, y:T
    expand ring_pow
    replace IH[x, y]
    ring_mult_shuffle<T>
  }
end

lemma field_nonzero_append: all T:type where { Field<T> }.
  all xs:List<RExpr>. all ys:List<RExpr>, env:List<T>.
  if field_nonzero(xs ++ ys, env)
  then field_nonzero(xs, env) and field_nonzero(ys, env)
proof
  arbitrary T:type where { Field<T> }
  induction List<RExpr>
  case empty {
    arbitrary ys:List<RExpr>, env:List<T>
    assume h
    expand field_nonzero
    expand operator++ in h
  }
  case node(x, xs) assume IH {
    arbitrary ys:List<RExpr>, env:List<T>
    assume h
    have h2: not (ring_denote(x, env) = zero) and field_nonzero(xs ++ ys, env)
      by expand operator++ | field_nonzero in h
    have rest: field_nonzero(xs, env) and field_nonzero(ys, env)
      by apply IH[ys, env] to conjunct 1 of h2
    expand field_nonzero
    (conjunct 0 of h2), (conjunct 0 of rest), (conjunct 1 of rest)
  }
end

lemma field_in_nonzero: all T:type where { Field<T> }.
  all cs:List<RExpr>. all c:RExpr, env:List<T>.
  if field_in(cs, c) and field_nonzero(cs, env)
  then not (ring_denote(c, env) = zero)
proof
  arbitrary T:type where { Field<T> }
  induction List<RExpr>
  case empty {
    arbitrary c:RExpr, env:List<T>
    assume h
    conclude false by expand field_in in conjunct 0 of h
  }
  case node(d, ds) assume IH {
    arbitrary c:RExpr, env:List<T>
    assume h
    have nz: not (ring_denote(d, env) = zero) and field_nonzero(ds, env)
      by expand field_nonzero in conjunct 1 of h
    switch c = d {
      case true assume cd {
        replace cd
        conjunct 0 of nz
      }
      case false assume cd {
        have rest: field_in(ds, c) by
          simplify with cd in (expand field_in in (conjunct 0 of h))
        apply IH[c, env] to rest, (conjunct 1 of nz)
      }
    }
  }
end

lemma field_covers_nonzero: all T:type where { Field<T> }.
  all xs:List<RExpr>. all cs:List<RExpr>, env:List<T>.
  if field_covers(xs, cs) and field_nonzero(cs, env)
  then field_nonzero(xs, env)
proof
  arbitrary T:type where { Field<T> }
  induction List<RExpr>
  case empty {
    arbitrary cs:List<RExpr>, env:List<T>
    assume h
    expand field_nonzero.
  }
  case node(x, xs) assume IH {
    arbitrary cs:List<RExpr>, env:List<T>
    assume h
    have cov: field_in(cs, x) and field_covers(xs, cs)
      by expand field_covers in conjunct 0 of h
    expand field_nonzero
    (apply field_in_nonzero<T>[cs, x, env] to (conjunct 0 of cov), (conjunct 1 of h)),
    (apply IH[cs, env] to (conjunct 1 of cov), (conjunct 1 of h))
  }
end

// A product is nonzero when its factors are.
lemma field_factors_nonzero: all T:type where { Field<T> }.
  all e:RExpr. all env:List<T>.
  if field_nonzero(field_factors(e), env) then not (ring_denote(e, env) = zero)
proof
  arbitrary T:type where { Field<T> }
  induction RExpr
  case rvar(i) {
    arbitrary env:List<T>
    assume h
    conjunct 0 of (expand field_factors | field_nonzero in h)
  }
  case rzero {
    arbitrary env:List<T>
    assume h
    conjunct 0 of (expand field_factors | field_nonzero in h)
  }
  case rone {
    arbitrary env:List<T>
    assume h
    expand ring_denote
    one_nonzero
  }
  case rconst(n) {
    arbitrary env:List<T>
    assume h
    conjunct 0 of (expand field_factors | field_nonzero in h)
  }
  case radd(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    assume h
    conjunct 0 of (expand field_factors | field_nonzero in h)
  }
  case rsub(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    assume h
    conjunct 0 of (expand field_factors | field_nonzero in h)
  }
  case rneg(a) assume IHa {
    arbitrary env:List<T>
    assume h
    expand ring_denote
    apply field_neg_nonzero<T>[ring_denote(a, env)] to (apply IHa[env] to (expand field_factors in h))
  }
  case rmul(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    assume h
    have both: field_nonzero(field_factors(a), env) and field_nonzero(field_factors(b), env)
      by apply field_nonzero_append<T> to (expand field_factors in h)
    expand ring_denote
    apply field_mult_nonzero<T> to (apply IHa[env] to conjunct 0 of both),
                                   (apply IHb[env] to (conjunct 1 of both))
  }
  case rpow(a, k) assume IHa {
    arbitrary env:List<T>
    assume h
    expand ring_denote
    switch k = 0 {
      case true assume k0 {
        replace k0 | pow_zero
        one_nonzero
      }
      case false assume kn0 {
        have ha: field_nonzero(field_factors(a), env) by simplify with kn0 in expand field_factors in h
        apply field_pow_nonzero<T>[ring_denote(a, env), k] to (apply IHa[env] to ha)
      }
    }
  }
  case rinv(a) assume IHa {
    arbitrary env:List<T>
    assume h
    conjunct 0 of (expand field_factors | field_nonzero in h)
  }
  case rdiv(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    assume h
    conjunct 0 of (expand field_factors | field_nonzero in h)
  }
  case rfrac(n, d) {
    arbitrary env:List<T>
    assume h
    conjunct 0 of (expand field_factors | field_nonzero in h)
  }
end

lemma field_mul_denote: all T:type where { Field<T> }.
  all a:RExpr, b:RExpr, env:List<T>.
  ring_denote(field_mul(a, b), env) = ring_denote(a, env) * ring_denote(b, env)
proof
  arbitrary T:type where { Field<T> }
  arbitrary a:RExpr, b:RExpr, env:List<T>
  expand field_mul
  switch a = rone {
    case true assume ea {
      replace ea
      expand ring_denote
      symmetric ring_one_mult<T>
    }
    case false {
      switch b = rone {
        case true assume eb {
          replace eb
          expand ring_denote
          symmetric mult_one
        }
        case false {
          expand ring_denote.
        }
      }
    }
  }
end

lemma fp_add_sound: all T:type where { Field<T> }.
  all x:T, y:T, X:FP, Y:FP, env:List<T>.
  if fp_holds(x, X, env) and fp_holds(y, Y, env)
  then fp_holds(x + y, fp_add(X, Y), env)
proof
  arbitrary T:type where { Field<T> }
  arbitrary x:T, y:T, X:FP, Y:FP, env:List<T>
  assume h
  have hx: x * ring_denote(fp_den(X), env) = ring_denote(fp_num(X), env)
           and not (ring_denote(fp_den(X), env) = zero)
    by expand fp_holds in conjunct 0 of h
  have hy: y * ring_denote(fp_den(Y), env) = ring_denote(fp_num(Y), env)
           and not (ring_denote(fp_den(Y), env) = zero)
    by expand fp_holds in conjunct 1 of h
  expand fp_holds | fp_add
  replace fp_num_fp | fp_den_fp
  expand ring_denote
  replace field_mul_denote<T>
        | symmetric conjunct 0 of hx | symmetric conjunct 0 of hy
  have eq: (x + y) * (ring_denote(fp_den(X), env) * ring_denote(fp_den(Y), env))
         = (x * ring_denote(fp_den(X), env)) * ring_denote(fp_den(Y), env)
           + (y * ring_denote(fp_den(Y), env)) * ring_denote(fp_den(X), env)
    by algebra
  eq, (apply field_mult_nonzero<T> to (conjunct 1 of hx), (conjunct 1 of hy))
end

lemma fp_sub_sound: all T:type where { Field<T> }.
  all x:T, y:T, X:FP, Y:FP, env:List<T>.
  if fp_holds(x, X, env) and fp_holds(y, Y, env)
  then fp_holds(x - y, fp_sub(X, Y), env)
proof
  arbitrary T:type where { Field<T> }
  arbitrary x:T, y:T, X:FP, Y:FP, env:List<T>
  assume h
  have hx: x * ring_denote(fp_den(X), env) = ring_denote(fp_num(X), env)
           and not (ring_denote(fp_den(X), env) = zero)
    by expand fp_holds in conjunct 0 of h
  have hy: y * ring_denote(fp_den(Y), env) = ring_denote(fp_num(Y), env)
           and not (ring_denote(fp_den(Y), env) = zero)
    by expand fp_holds in conjunct 1 of h
  expand fp_holds | fp_sub
  replace fp_num_fp | fp_den_fp
  expand ring_denote
  replace field_mul_denote<T>
        | symmetric conjunct 0 of hx | symmetric conjunct 0 of hy
  have eq: (x - y) * (ring_denote(fp_den(X), env) * ring_denote(fp_den(Y), env))
         = (x * ring_denote(fp_den(X), env)) * ring_denote(fp_den(Y), env)
           - (y * ring_denote(fp_den(Y), env)) * ring_denote(fp_den(X), env)
    by algebra
  eq, (apply field_mult_nonzero<T> to (conjunct 1 of hx), (conjunct 1 of hy))
end

lemma fp_neg_sound: all T:type where { Field<T> }.
  all x:T, X:FP, env:List<T>.
  if fp_holds(x, X, env) then fp_holds(- x, fp_neg(X), env)
proof
  arbitrary T:type where { Field<T> }
  arbitrary x:T, X:FP, env:List<T>
  assume h
  have hx: x * ring_denote(fp_den(X), env) = ring_denote(fp_num(X), env)
           and not (ring_denote(fp_den(X), env) = zero)
    by expand fp_holds in h
  expand fp_holds | fp_neg
  replace fp_num_fp | fp_den_fp
  expand ring_denote
  replace symmetric conjunct 0 of hx
  have eq: (- x) * ring_denote(fp_den(X), env) = - (x * ring_denote(fp_den(X), env))
    by algebra
  eq, (conjunct 1 of hx)
end

lemma fp_mul_sound: all T:type where { Field<T> }.
  all x:T, y:T, X:FP, Y:FP, env:List<T>.
  if fp_holds(x, X, env) and fp_holds(y, Y, env)
  then fp_holds(x * y, fp_mul(X, Y), env)
proof
  arbitrary T:type where { Field<T> }
  arbitrary x:T, y:T, X:FP, Y:FP, env:List<T>
  assume h
  have hx: x * ring_denote(fp_den(X), env) = ring_denote(fp_num(X), env)
           and not (ring_denote(fp_den(X), env) = zero)
    by expand fp_holds in conjunct 0 of h
  have hy: y * ring_denote(fp_den(Y), env) = ring_denote(fp_num(Y), env)
           and not (ring_denote(fp_den(Y), env) = zero)
    by expand fp_holds in conjunct 1 of h
  expand fp_holds | fp_mul
  replace fp_num_fp | fp_den_fp | field_mul_denote<T>
        | symmetric conjunct 0 of hx | symmetric conjunct 0 of hy
  have eq: (x * y) * (ring_denote(fp_den(X), env) * ring_denote(fp_den(Y), env))
         = (x * ring_denote(fp_den(X), env)) * (y * ring_denote(fp_den(Y), env))
    by algebra
  eq, (apply field_mult_nonzero<T> to (conjunct 1 of hx), (conjunct 1 of hy))
end

lemma fp_pow_sound: all T:type where { Field<T> }.
  all x:T, k:UInt, X:FP, env:List<T>.
  if fp_holds(x, X, env) then fp_holds(x ^ k, fp_pow(X, k), env)
proof
  arbitrary T:type where { Field<T> }
  arbitrary x:T, k:UInt, X:FP, env:List<T>
  assume h
  have hx: x * ring_denote(fp_den(X), env) = ring_denote(fp_num(X), env)
           and not (ring_denote(fp_den(X), env) = zero)
    by expand fp_holds in h
  have nz: not (ring_denote(fp_den(X), env) ^ k = zero)
    by apply field_pow_nonzero<T>[ring_denote(fp_den(X), env), k] to (conjunct 1 of hx)
  expand fp_holds | fp_pow
  replace fp_num_fp | fp_den_fp
  expand ring_denote
  replace symmetric conjunct 0 of hx
  have eq: x ^ k * ring_denote(fp_den(X), env) ^ k
         = (x * ring_denote(fp_den(X), env)) ^ k
    by replace ring_pow_uint<T> | ring_pow_mult<T>.
  eq, nz
end

lemma fp_inv_sound: all T:type where { Field<T> }.
  all x:T, X:FP, env:List<T>.
  if fp_holds(x, X, env) and not (ring_denote(fp_num(X), env) = zero)
  then fp_holds(inv(x), fp_inv(X), env)
proof
  arbitrary T:type where { Field<T> }
  arbitrary x:T, X:FP, env:List<T>
  assume h
  have hx: x * ring_denote(fp_den(X), env) = ring_denote(fp_num(X), env)
           and not (ring_denote(fp_den(X), env) = zero)
    by expand fp_holds in conjunct 0 of h
  have xnz: not (x = zero) by {
    assume x0
    have n0: ring_denote(fp_num(X), env) = zero by
      replace symmetric (conjunct 0 of hx) | x0 | ring_zero_mult<T>.
    apply (conjunct 1 of h) to n0
  }
  have xi: x * inv(x) = one by apply mult_inv to xnz
  expand fp_holds | fp_inv
  replace fp_num_fp | fp_den_fp
  have eq: inv(x) * ring_denote(fp_num(X), env) = ring_denote(fp_den(X), env) by
    equations
          inv(x) * ring_denote(fp_num(X), env)
        = inv(x) * (x * ring_denote(fp_den(X), env))   by replace symmetric conjunct 0 of hx.
      ... = (x * inv(x)) * ring_denote(fp_den(X), env)
            by replace symmetric mult_assoc[inv(x), x, ring_denote(fp_den(X), env)]
                     | mult_commute[inv(x), x].
      ... = ring_denote(fp_den(X), env)                by replace xi | ring_one_mult<T>.
  eq, (conjunct 1 of h)
end

lemma fp_leaf: all T:type where { Field<T> }.
  all e:RExpr, env:List<T>. fp_holds(ring_denote(e, env), fp(e, rone), env)
proof
  arbitrary T:type where { Field<T> }
  arbitrary e:RExpr, env:List<T>
  expand fp_holds
  replace fp_num_fp | fp_den_fp
  expand ring_denote
  replace mult_one
  one_nonzero
end

// Under the side conditions, an expression is its fraction.
lemma field_frac_sound: all T:type where { Field<T> }.
  all e:RExpr. all env:List<T>.
  if field_nonzero(field_conds(e), env)
  then fp_holds(field_denote(e, env), field_frac(e), env)
proof
  arbitrary T:type where { Field<T> }
  induction RExpr
  case rvar(i) {
    arbitrary env:List<T>
    assume h
    expand field_frac | field_denote
    expand ring_denote in fp_leaf<T>[rvar(i), env]
  }
  case rzero {
    arbitrary env:List<T>
    assume h
    expand field_frac | field_denote
    expand ring_denote in fp_leaf<T>[rzero, env]
  }
  case rone {
    arbitrary env:List<T>
    assume h
    expand field_frac | field_denote
    expand ring_denote in fp_leaf<T>[rone, env]
  }
  case rconst(n) {
    arbitrary env:List<T>
    assume h
    expand field_frac | field_denote
    expand ring_denote in fp_leaf<T>[rconst(n), env]
  }
  case radd(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    assume h
    have both: field_nonzero(field_conds(a), env) and field_nonzero(field_conds(b), env)
      by apply field_nonzero_append<T> to (expand field_conds in h)
    expand field_frac | field_denote
    apply fp_add_sound<T> to (apply IHa[env] to (conjunct 0 of both)),
                             (apply IHb[env] to (conjunct 1 of both))
  }
  case rsub(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    assume h
    have both: field_nonzero(field_conds(a), env) and field_nonzero(field_conds(b), env)
      by apply field_nonzero_append<T> to (expand field_conds in h)
    expand field_frac | field_denote
    apply fp_sub_sound<T> to (apply IHa[env] to (conjunct 0 of both)),
                             (apply IHb[env] to (conjunct 1 of both))
  }
  case rneg(a) assume IHa {
    arbitrary env:List<T>
    assume h
    expand field_frac | field_denote
    apply fp_neg_sound<T> to (apply IHa[env] to (expand field_conds in h))
  }
  case rmul(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    assume h
    have both: field_nonzero(field_conds(a), env) and field_nonzero(field_conds(b), env)
      by apply field_nonzero_append<T> to (expand field_conds in h)
    expand field_frac | field_denote
    apply fp_mul_sound<T> to (apply IHa[env] to (conjunct 0 of both)),
                             (apply IHb[env] to (conjunct 1 of both))
  }
  case rpow(a, k) assume IHa {
    arbitrary env:List<T>
    assume h
    expand field_frac | field_denote
    apply fp_pow_sound<T>[field_denote(a, env), k, field_frac(a), env]
      to (apply IHa[env] to (expand field_conds in h))
  }
  case rinv(a) assume IHa {
    arbitrary env:List<T>
    assume h
    have both: field_nonzero(field_factors(fp_num(field_frac(a))), env)
               and field_nonzero(field_conds(a), env)
      by apply field_nonzero_append<T> to (expand field_conds in h)
    expand field_frac | field_denote
    apply fp_inv_sound<T> to (apply IHa[env] to (conjunct 1 of both)),
      (apply field_factors_nonzero<T> to (conjunct 0 of both))
  }
  case rdiv(a, b) assume IHa, IHb {
    arbitrary env:List<T>
    assume h
    have h1: field_nonzero(field_factors(fp_num(field_frac(b))), env)
             and field_nonzero(field_conds(a) ++ field_conds(b), env)
      by apply field_nonzero_append<T> to (expand field_conds in h)
    have both: field_nonzero(field_conds(a), env) and field_nonzero(field_conds(b), env)
      by apply field_nonzero_append<T> to (conjunct 1 of h1)
    have hb: fp_holds(inv(field_denote(b, env)), fp_inv(field_frac(b)), env)
      by apply fp_inv_sound<T> to (apply IHb[env] to (conjunct 1 of both)),
           (apply field_factors_nonzero<T> to (conjunct 0 of h1))
    expand field_frac | field_denote
    replace div_def
    apply fp_mul_sound<T> to (apply IHa[env] to (conjunct 0 of both)), hb
  }
  case rfrac(n, d) {
    arbitrary env:List<T>
    assume h
    have dnz: not (from_uint(d) = zero)
      by conjunct 0 of (expand field_conds | field_nonzero | ring_denote in h)
    expand field_frac | field_denote | fp_holds
    replace fp_num_fp | fp_den_fp
    expand ring_denote
    (apply from_frac_mult[n, d] to dnz), dnz
  }
end

// If x = nx / dx and y = ny / dy, and the cross products are equal,
// then x = y.
lemma field_cross_cancel: all T:type where { Field<T> }.
  all x:T, y:T, nx:T, ny:T, dx:T, dy:T.
  if x * dx = nx and y * dy = ny and nx * dy = ny * dx
     and not (dx = zero) and not (dy = zero)
  then x = y
proof
  arbitrary T:type where { Field<T> }
  arbitrary x:T, y:T, nx:T, ny:T, dx:T, dy:T
  assume h
  have e: x * (dx * dy) = y * (dx * dy) by
    equations
          x * (dx * dy) = (x * dx) * dy   by algebra
        ... = nx * dy                     by replace conjunct 0 of h.
        ... = ny * dx                     by conjunct 2 of h
        ... = (y * dy) * dx               by replace symmetric conjunct 1 of h.
        ... = y * (dx * dy)               by algebra
  apply field_mult_cancel<T>[x, y, dx * dy]
    to (apply field_mult_nonzero<T> to (conjunct 3 of h), (conjunct 4 of h)), e
end

theorem field_prove: all T:type where { Field<T> }.
  all env:List<T>, a:RExpr, b:RExpr, cs:List<RExpr>.
  if field_frac_eq(a, b) and field_covers(field_conds(a) ++ field_conds(b), cs)
  then if field_nonzero(cs, env)
  then field_denote(a, env) = field_denote(b, env)
proof
  arbitrary T:type where { Field<T> }
  arbitrary env:List<T>, a:RExpr, b:RExpr, cs:List<RExpr>
  assume h
  assume nz
  have conds: field_nonzero(field_conds(a), env) and field_nonzero(field_conds(b), env)
    by apply field_nonzero_append<T>
         to (apply field_covers_nonzero<T> to (conjunct 1 of h), nz)
  have ha: field_denote(a, env) * ring_denote(fp_den(field_frac(a)), env)
           = ring_denote(fp_num(field_frac(a)), env)
           and not (ring_denote(fp_den(field_frac(a)), env) = zero)
    by expand fp_holds in (apply field_frac_sound<T> to (conjunct 0 of conds))
  have hb: field_denote(b, env) * ring_denote(fp_den(field_frac(b)), env)
           = ring_denote(fp_num(field_frac(b)), env)
           and not (ring_denote(fp_den(field_frac(b)), env) = zero)
    by expand fp_holds in (apply field_frac_sound<T> to (conjunct 1 of conds))
  have cross: ring_denote(fp_num(field_frac(a)), env) * ring_denote(fp_den(field_frac(b)), env)
            = ring_denote(fp_num(field_frac(b)), env) * ring_denote(fp_den(field_frac(a)), env)
    by expand ring_denote in
         (apply ring_prove<T>[env, rmul(fp_num(field_frac(a)), fp_den(field_frac(b))),
                                   rmul(fp_num(field_frac(b)), fp_den(field_frac(a)))]
          to (expand field_frac_eq in (conjunct 0 of h)))
  apply field_cross_cancel<T> to (conjunct 0 of ha), (conjunct 0 of hb), cross,
                                 (conjunct 1 of ha), (conjunct 1 of hb)
end