module Algebra

/*
  Algebraic concepts (#1264) and their models for UInt, Int, Rat, and
  Real.

  OrderedQVectorSpace is the interface of the linear-arithmetic tactic
  (#1189): an ordered abelian group with scaling by rationals. Rat and
  Real model it, and so will angle measures.

  A theorem proved under `where { OrderedQVectorSpace }` holds for
  every model: use it at a type with `thm` or `thm`.
*/

import Nat
import UInt
import Int
import Base
public import Rat
public import Real

concept AddCommMonoid<T> {
  fun operator+ : fn (T, T) -> T
  fun zero : T
  theorem add_commute: all x:T, y:T. x + y = y + x
  theorem add_assoc: all x:T, y:T, z:T. (x + y) + z = x + (y + z)
  theorem add_zero: all x:T. x + zero = x
}

concept AddCommGroup<T> {
  refines AddCommMonoid<T>
  fun operator- : fn T -> T
  fun operator- : fn (T, T) -> T
  theorem add_inverse: all x:T. x + - x = zero
  theorem sub_def: all x:T, y:T. x - y = x + - y
}

concept OrderedAddCommGroup<T> {
  refines AddCommGroup<T>
  fun operator≤ : fn (T, T) -> bool
  fun operator< : fn (T, T) -> bool
  theorem less_equal_trans: all x:T, y:T, z:T. if x ≤ y and y ≤ z then x ≤ z
  theorem less_equal_antisymmetric: all x:T, y:T. if x ≤ y and y ≤ x then x = y
  theorem less_equal_total: all x:T, y:T. x ≤ y or y ≤ x
  theorem add_le_right_mono: all x:T, y:T, z:T. if x ≤ y then x + z ≤ y + z
  theorem less_def: all x:T, y:T. (x < y) = (x ≤ y and not (x = y))
}

concept OrderedQVectorSpace<T> {
  refines OrderedAddCommGroup<T>
  fun scale : fn (Rat, T) -> T
  theorem scale_one: all x:T. scale(rat(+1), x) = x
  theorem scale_assoc: all p:Rat, q:Rat, x:T. scale(p * q, x) = scale(p, scale(q, x))
  theorem scale_add_left: all p:Rat, q:Rat, x:T.
    scale(p + q, x) = scale(p, x) + scale(q, x)
  theorem scale_add_right: all q:Rat, x:T, y:T.
    scale(q, x + y) = scale(q, x) + scale(q, y)
  theorem scale_pos: all q:Rat, x:T.
    if rat(+0) < q and zero < x then zero < scale(q, x)
}

// The interfaces of ring normalization in the `algebra` tactic (#1190,
// lib/Ring.pf). `from_uint` reads constants in the semiring: the UInt
// literal inside a literal such as `2`, `+2`, `rat(+2)`, or `real(+2)`.
// `^` is the power with a UInt exponent (#1324).
concept CommSemiring<T> {
  refines AddCommMonoid<T>
  fun operator* : fn (T, T) -> T
  fun one : T
  fun from_uint : fn UInt -> T
  fun operator^ : fn (T, UInt) -> T
  theorem mult_commute: all x:T, y:T. x * y = y * x
  theorem mult_assoc: all x:T, y:T, z:T. (x * y) * z = x * (y * z)
  theorem mult_one: all x:T. x * one = x
  theorem mult_zero: all x:T. x * zero = zero
  theorem dist_mult_add: all x:T, y:T, z:T. x * (y + z) = x * y + x * z
  theorem from_uint_zero: from_uint(0) = zero
  theorem from_uint_one: from_uint(1) = one
  theorem from_uint_add: all m:UInt, n:UInt. from_uint(m + n) = from_uint(m) + from_uint(n)
  theorem from_uint_mult: all m:UInt, n:UInt. from_uint(m * n) = from_uint(m) * from_uint(n)
  theorem pow_zero: all x:T. x ^ (0:UInt) = one
  theorem pow_one_add: all x:T, k:UInt. x ^ (1 + k) = x * x ^ k
}

concept CommRing<T> {
  refines CommSemiring<T>
  refines AddCommGroup<T>
}

// The interface of the field mode of `algebra` (#1322). `from_frac`
// reads fraction constants: the UInt numerator and denominator of a
// literal such as `frac(+1, 2)` or `real(frac(+1, 2))`.
concept Field<T> {
  refines CommRing<T>
  fun inv : fn T -> T
  fun operator/ : fn (T, T) -> T
  fun from_frac : fn (UInt, UInt) -> T
  theorem one_nonzero: not (one = zero)
  theorem mult_inv: all x:T. if not (x = zero) then x * inv(x) = one
  theorem div_def: all x:T, y:T. x / y = x * inv(y)
  theorem from_frac_mult: all n:UInt, d:UInt.
    if not (from_uint(d) = zero) then from_frac(n, d) * from_uint(d) = from_uint(n)
}

// UInt

model AddCommMonoid<UInt> {
  fun zero = 0
  theorem add_commute = uint_add_commute
  theorem add_assoc = uint_add_assoc
  theorem add_zero = uint_add_zero
}

model CommSemiring<UInt> {
  fun one = 1
  fun from_uint = fun n:UInt { n }
  theorem mult_commute = uint_mult_commute
  theorem mult_assoc = uint_mult_assoc
  theorem mult_one = uint_mult_one
  theorem mult_zero = uint_mult_zero
  theorem dist_mult_add = uint_dist_mult_add
  theorem from_uint_zero = evaluate
  theorem from_uint_one = evaluate
  theorem from_uint_add = { arbitrary m:UInt, n:UInt . }
  theorem from_uint_mult = { arbitrary m:UInt, n:UInt . }
  theorem pow_zero = uint_expt_zero
  theorem pow_one_add = {
    arbitrary x:UInt, k:UInt
    replace uint_pow_add_r[x, 1, k].
  }
}

// Int

model AddCommMonoid<Int> {
  fun zero = +0
  theorem add_commute = int_add_commute
  theorem add_assoc = int_add_assoc
  theorem add_zero = int_add_zero
}

model AddCommGroup<Int> {
  theorem add_inverse = int_add_inverse
  theorem sub_def = {
    arbitrary x:Int, y:Int
    expand operator-.
  }
}

model CommSemiring<Int> {
  fun one = +1
  fun from_uint = fun n:UInt { pos(n) }
  theorem mult_commute = int_mult_commute
  theorem mult_assoc = int_mult_assoc
  theorem mult_one = int_mult_one
  theorem mult_zero = int_mult_zero
  theorem dist_mult_add = int_dist_mult_add
  theorem from_uint_zero = evaluate
  theorem from_uint_one = evaluate
  theorem from_uint_add = {
    arbitrary m:UInt, n:UInt
    replace add_pos_pos.
  }
  theorem from_uint_mult = {
    arbitrary m:UInt, n:UInt
    replace mult_pos_pos.
  }
  theorem pow_zero = int_pow_zero
  theorem pow_one_add = int_pow_one_add
}

model CommRing<Int> { }

// Rat

model AddCommMonoid<Rat> {
  fun zero = rat(+0)
  theorem add_commute = rat_add_commute
  theorem add_assoc = rat_add_assoc
  theorem add_zero = rat_add_zero
}

model AddCommGroup<Rat> {
  theorem add_inverse = rat_add_inverse
  theorem sub_def = rat_sub_def
}

model OrderedAddCommGroup<Rat> {
  theorem less_equal_trans = rat_less_equal_trans
  theorem less_equal_antisymmetric = rat_less_equal_antisymmetric
  theorem less_equal_total = rat_less_equal_total
  theorem add_le_right_mono = rat_add_le_right_mono
  theorem less_def = rat_less_iff_le_not_equal
}

model OrderedQVectorSpace<Rat> {
  fun scale = operator*
  theorem scale_one = rat_one_mult
  theorem scale_assoc = rat_mult_assoc
  theorem scale_add_left = {
    arbitrary p:Rat, q:Rat, x:Rat
    rat_dist_mult_add_right[x, p, q]
  }
  theorem scale_add_right = rat_dist_mult_add
  theorem scale_pos = rat_mult_pos
}

model CommSemiring<Rat> {
  fun one = rat(+1)
  fun from_uint = fun n:UInt { rat(pos(n)) }
  theorem mult_commute = rat_mult_commute
  theorem mult_assoc = rat_mult_assoc
  theorem mult_one = rat_mult_one
  theorem mult_zero = rat_mult_zero
  theorem dist_mult_add = rat_dist_mult_add
  theorem from_uint_zero = evaluate
  theorem from_uint_one = evaluate
  theorem from_uint_add = {
    arbitrary m:UInt, n:UInt
    replace add_pos_pos.
  }
  theorem from_uint_mult = {
    arbitrary m:UInt, n:UInt
    replace mult_pos_pos.
  }
  theorem pow_zero = rat_pow_zero
  theorem pow_one_add = rat_pow_one_add
}

model CommRing<Rat> { }

model Field<Rat> {
  fun from_frac = fun n:UInt, d:UInt { frac(pos(n), d) }
  theorem one_nonzero = rat_one_not_zero
  theorem mult_inv = rat_mult_inv
  theorem div_def = rat_div_def
  theorem from_frac_mult = {
    arbitrary n:UInt, d:UInt
    assume h
    have h2: not (rat(pos(d)) = rat(+0)) by h
    replace rat_frac_div
    apply rat_div_mult_cancel[rat(pos(n)), rat(pos(d))] to h2
  }
}

// Real

model AddCommMonoid<Real> {
  fun zero = real(+0)
  theorem add_commute = real_add_commute
  theorem add_assoc = real_add_assoc
  theorem add_zero = real_add_zero
}

model AddCommGroup<Real> {
  theorem add_inverse = real_add_inverse
  theorem sub_def = real_sub_def
}

model OrderedAddCommGroup<Real> {
  theorem less_equal_trans = real_less_equal_trans
  theorem less_equal_antisymmetric = real_less_equal_antisymmetric
  theorem less_equal_total = real_less_equal_total
  theorem add_le_right_mono = real_add_le_right_mono
  theorem less_def = real_less_def
}

// Scaling a real by a rational.
fun real_scale(q:Rat, x:Real) { real(q) * x }

theorem real_scale_one: all x:Real. real_scale(rat(+1), x) = x
proof
  arbitrary x:Real
  expand real_scale
  real_one_mult[x]
end

theorem real_scale_assoc: all p:Rat, q:Rat, x:Real.
  real_scale(p * q, x) = real_scale(p, real_scale(q, x))
proof
  arbitrary p:Rat, q:Rat, x:Real
  expand real_scale
  real_mult_assoc[real(p), real(q), x]
end

theorem real_scale_add_left: all p:Rat, q:Rat, x:Real.
  real_scale(p + q, x) = real_scale(p, x) + real_scale(q, x)
proof
  arbitrary p:Rat, q:Rat, x:Real
  expand real_scale
  real_dist_mult_add_right[x, real(p), real(q)]
end

theorem real_scale_add_right: all q:Rat, x:Real, y:Real.
  real_scale(q, x + y) = real_scale(q, x) + real_scale(q, y)
proof
  arbitrary q:Rat, x:Real, y:Real
  expand real_scale
  real_dist_mult_add[real(q), x, y]
end

theorem real_scale_pos: all q:Rat, x:Real.
  if rat(+0) < q and real(+0) < x then real(+0) < real_scale(q, x)
proof
  arbitrary q:Rat, x:Real
  assume prem
  have q_pos: real(+0) < real(q) by conjunct 0 of prem
  expand real_scale
  apply real_mult_pos[real(q), x] to q_pos, conjunct 1 of prem
end

model OrderedQVectorSpace<Real> {
  fun scale = real_scale
  theorem scale_one = real_scale_one
  theorem scale_assoc = real_scale_assoc
  theorem scale_add_left = real_scale_add_left
  theorem scale_add_right = real_scale_add_right
  theorem scale_pos = real_scale_pos
}

model CommSemiring<Real> {
  fun one = real(+1)
  fun from_uint = fun n:UInt { real(pos(n)) }
  theorem mult_commute = real_mult_commute
  theorem mult_assoc = real_mult_assoc
  theorem mult_one = real_mult_one
  theorem mult_zero = real_mult_zero
  theorem dist_mult_add = real_dist_mult_add
  theorem from_uint_zero = evaluate
  theorem from_uint_one = evaluate
  theorem from_uint_add = {
    arbitrary m:UInt, n:UInt
    replace add_pos_pos.
  }
  theorem from_uint_mult = {
    arbitrary m:UInt, n:UInt
    replace mult_pos_pos.
  }
  theorem pow_zero = real_pow_zero
  theorem pow_one_add = real_pow_one_add
}

model CommRing<Real> { }

model Field<Real> {
  fun from_frac = fun n:UInt, d:UInt { real(frac(pos(n), d)) }
  theorem one_nonzero = real_one_not_zero
  theorem mult_inv = real_mult_inv
  theorem div_def = real_div_def
  theorem from_frac_mult = {
    arbitrary n:UInt, d:UInt
    assume h
    have h2: not (real(pos(d)) = real(+0)) by h
    replace rat_frac_div
    apply real_div_mult_cancel[real(pos(n)), real(pos(d))] to h2
  }
}