import Option

import Base

union Nat {
  zero
  suc(Nat)
}

recursive operator +(Nat,Nat) -> Nat{
  operator +(zero, m) = m
  operator +(suc(n), m) = suc(n + m)
}

recursive operator *(Nat,Nat) -> Nat{
  operator *(zero, m) = zero
  operator *(suc(n), m) = m + n * m
}

recursive expt(Nat,Nat) -> Nat{
  expt(zero, n) = suc(zero)
  expt(suc(p), n) = n * expt(p, n)
}

fun operator ^(a:Nat, b:Nat) {
  expt(b, a)
}

recursive max(Nat,Nat) -> Nat{
  max(zero, n) = n
  max(suc(m'), n) = 
    switch n {
      case zero {
        suc(m')
      }
      case suc(n') {
        suc(max(m', n'))
      }
    }
}

recursive min(Nat,Nat) -> Nat{
  min(zero, n) = zero
  min(suc(m'), n) = 
    switch n {
      case zero {
        zero
      }
      case suc(n') {
        suc(min(m', n'))
      }
    }
}

fun pred(n:Nat) {
  switch n {
    case zero {
      zero
    }
    case suc(n') {
      n'
    }
  }
}

recursive operator ∸(Nat,Nat) -> Nat{
  operator ∸(zero, m) = zero
  operator ∸(suc(n), m) = 
    switch m {
      case zero {
        suc(n)
      }
      case suc(m') {
        n  m'
      }
    }
}

recursive operator ≤(Nat,Nat) -> bool{
  operator ≤(zero, m) = true
  operator ≤(suc(n'), m) = 
    switch m {
      case zero {
        false
      }
      case suc(m') {
        n'  m'
      }
    }
}

fun operator <(x:Nat, y:Nat) {
  suc(x)  y
}

fun operator >(x:Nat, y:Nat) {
  y < x
}

fun operator ≥(x:Nat, y:Nat) {
  y  x
}

recursive summation(Nat,Nat,(fn Nat -> Nat)) -> Nat{
  summation(zero, begin, f) = zero
  summation(suc(k), begin, f) = f(begin) + summation(k, suc(begin), f)
}

recursive iterate<T>(Nat,T,(fn T -> T)) -> T{
  iterate(zero, init, f) = init
  iterate(suc(n), init, f) = f(@iterate<T>(n, init, f))
}

recursive pow2(Nat) -> Nat{
  pow2(zero) = suc(zero)
  pow2(suc(n')) = suc(suc(zero)) * pow2(n')
}

recursive equal(Nat,Nat) -> bool{
  equal(zero, n) = 
    switch n {
      case zero {
        true
      }
      case suc(n') {
        false
      }
    }
  equal(suc(m'), n) = 
    switch n {
      case zero {
        false
      }
      case suc(n') {
        equal(m', n')
      }
    }
}

recursive dist(Nat,Nat) -> Nat{
  dist(zero, n) = n
  dist(suc(m), n) = 
    switch n {
      case zero {
        suc(m)
      }
      case suc(n') {
        dist(m, n')
      }
    }
}

fun log(n) {
  find_log(n, zero, zero)
}

add_commute: (all n:Nat. (all m:Nat. n + m = m + n))

add_assoc: (all m:Nat, n:Nat, o:Nat. (m + n) + o = m + (n + o))

assoc_add: (all m:Nat, n:Nat, o:Nat. m + (n + o) = (m + n) + o)

add_both_sides_of_equal: (all x:Nat. (all y:Nat, z:Nat. ((x + y = x + z)  (y = z))))

left_cancel: (all x:Nat. (all y:Nat, z:Nat. (if x + y = x + z then y = z)))

add_both_monus: (all z:Nat, y:Nat, x:Nat. (z + y)  (z + x) = y  x)

add_monus_identity: (all m:Nat. (all n:Nat. (m + n)  m = n))

monus_monus_eq_monus_add: (all x:Nat. (all y:Nat. (all z:Nat. (x  y)  z = x  (y + z))))

monus_order: (all x:Nat, y:Nat, z:Nat. (x  y)  z = (x  z)  y)

mult_commute: (all m:Nat. (all n:Nat. m * n = n * m))

dist_mult_add: (all a:Nat, x:Nat, y:Nat. a * (x + y) = a * x + a * y)

dist_mult_add_right: (all x:Nat, y:Nat, a:Nat. (x + y) * a = x * a + y * a)

mult_assoc: (all m:Nat. (all n:Nat, o:Nat. (m * n) * o = m * (n * o)))

less_equal_implies_less_or_equal: (all x:Nat. (all y:Nat. (if x  y then (x < y or x = y))))

less_equal_not_equal_implies_less: (all x:Nat, y:Nat. (if (x  y and not (x = y)) then x < y))

less_implies_less_equal: (all x:Nat. (all y:Nat. (if x < y then x  y)))

less_equal_refl: (all n:Nat. n  n)

equal_implies_less_equal: (all x:Nat, y:Nat. (if x = y then x  y))

less_equal_antisymmetric: (all x:Nat. (all y:Nat. (if (x  y and y  x) then x = y)))

less_equal_trans: (all m:Nat. (all n:Nat, o:Nat. (if (m  n and n  o) then m  o)))

not_less_implies_less_equal: (all x:Nat. (all y:Nat. (if not (x < y) then y  x)))

less_irreflexive: (all x:Nat. not (x < x))

less_not_equal: (all x:Nat, y:Nat. (if x < y then not (x = y)))

greater_not_equal: (all x:Nat, y:Nat. (if x > y then not (x = y)))

trichotomy: (all x:Nat. (all y:Nat. (x < y or x = y or y < x)))

trichotomy2: (all y:Nat, x:Nat. (if (not (x = y) and not (x < y)) then y < x))

dichotomy: (all x:Nat, y:Nat. (x  y or y < x))

not_less_equal_iff_greater: (all x:Nat, y:Nat. (not (x  y)  (y < x)))

less_implies_not_greater: (all x:Nat. (all y:Nat. (if x < y then not (y < x))))

not_less_equal_less_equal: (all x:Nat, y:Nat. (if not (x  y) then y  x))

less_equal_add: (all x:Nat. (all y:Nat. x  x + y))

less_equal_add_left: (all x:Nat, y:Nat. y  x + y)

add_mono: (all a:Nat, b:Nat, c:Nat, d:Nat. (if (a  c and b  d) then a + b  c + d))

add_mono_less: (all a:Nat, b:Nat, c:Nat, d:Nat. (if (a < c and b < d) then a + b < c + d))

add_both_sides_of_less_equal: (all x:Nat. (all y:Nat, z:Nat. ((x + y  x + z)  (y  z))))

add_both_sides_of_less: (all x:Nat, y:Nat, z:Nat. ((x + y < x + z)  (y < z)))

mult_mono_le: (all n:Nat. (all x:Nat, y:Nat. (if x  y then n * x  n * y)))

mult_mono_le_r: (all n:Nat. (all x:Nat, y:Nat. (if x  y then x * n  y * n)))

mult_cancel_right_less: (all x:Nat, y:Nat, z:Nat. (if y * x < z * x then y < z))

mult_cancel_left_less: (all x:Nat, y:Nat, z:Nat. (if x * y < x * z then y < z))

monus_add_assoc: (all n:Nat. (all l:Nat, m:Nat. (if m  n then l + (n  m) = (l + n)  m)))

monus_add_identity: (all n:Nat. (all m:Nat. (if m  n then m + (n  m) = n)))

monus_right_cancel: (all x:Nat, y:Nat, a:Nat. (if (x  a = y  a and a  x and a  y) then x = y))

less_equal_add_monus: (all m:Nat. (all n:Nat, o:Nat. (if (n  m and m  n + o) then m  n  o)))

le_exists_monus: (all n:Nat, m:Nat. (if n  m then some x:Nat. m = n + x))

less_trans: (all m:Nat, n:Nat, o:Nat. (if (m < n and n < o) then m < o))

dist_mult_monus: (all x:Nat. (all y:Nat, z:Nat. x * (y  z) = x * y  x * z))

dist_mult_monus_one: (all x:Nat, y:Nat. x * (y  suc(zero)) = x * y  x)

n_le_nn: (all n:Nat. n  n * n)

max_greater_right: (all y:Nat, x:Nat. y  max(x, y))

max_greater_left: (all x:Nat, y:Nat. x  max(x, y))

max_is_left_or_right: (all x:Nat, y:Nat. (max(x, y) = x or max(x, y) = y))

max_symmetric: (all x:Nat, y:Nat. max(x, y) = max(y, x))

max_assoc: (all x:Nat, y:Nat, z:Nat. max(max(x, y), z) = max(x, max(y, z)))

max_equal_greater_right: (all x:Nat, y:Nat. (if x  y then max(x, y) = y))

max_equal_greater_left: (all x:Nat, y:Nat. (if y  x then max(x, y) = x))

max_less_equal: (all x:Nat, y:Nat, z:Nat. (if (x  z and y  z) then max(x, y)  z))

recursive parity(Nat,bool) -> bool{
  parity(zero, b) = b
  parity(suc(n'), b) = parity(n', not b)
}

fun is_even(n:Nat) {
  parity(n, true)
}

fun is_odd(n:Nat) {
  parity(n, false)
}

fun EvenNat(n:Nat) {
  some m:Nat. n = suc(suc(zero)) * m
}

fun OddNat(n:Nat) {
  some m:Nat. n = suc(suc(suc(zero)) * m)
}

even_or_odd: (all n:Nat. (is_even(n) or is_odd(n)))

addition_of_evens: (all x:Nat, y:Nat. (if (EvenNat(x) and EvenNat(y)) then EvenNat(x + y)))

is_even_odd: (all n:Nat. ((if is_even(n) then EvenNat(n)) and (if is_odd(n) then OddNat(n))))

Even_or_Odd: (all n:Nat. (EvenNat(n) or OddNat(n)))

Even_not_Odd: (all n:Nat. (EvenNat(n)  not OddNat(n)))

summation_cong: (all k:Nat. (all f:(fn Nat -> Nat), g:(fn Nat -> Nat), s:Nat, t:Nat. (if (all i:Nat. (if i < k then f(s + i) = g(t + i))) then summation(k, s, f) = summation(k, t, g))))

summation_add: (all a:Nat. (all b:Nat, s:Nat, t:Nat, f:(fn Nat -> Nat), g:(fn Nat -> Nat), h:(fn Nat -> Nat). (if ((all i:Nat. (if i < a then g(s + i) = f(s + i))) and (all i:Nat. (if i < b then h(t + i) = f((s + a) + i)))) then summation(a + b, s, f) = summation(a, s, g) + summation(b, t, h))))

equal_refl: (all n:Nat. equal(n, n))

equal_complete_sound: (all m:Nat. (all n:Nat. ((m = n)  equal(m, n))))

not_equal_not_eq: (all m:Nat, n:Nat. (if not equal(m, n) then not (m = n)))

recfun operator /(n:Nat, m:Nat) -> Nat
measure	n  {
  if n < m then
    zero
  else
    if m = zero then
      zero
    else
      suc(zero) + (n  m) / m
}

fun operator %(n:Nat, m:Nat) {
  n  (n / m) * m
}

strong_induction: (all P:(fn Nat -> bool), n:Nat. (if (all j:Nat. (if (all i:Nat. (if i < j then P(i))) then P(j))) then P(n)))

fun divides(a:Nat, b:Nat) {
  some k:Nat. a * k = b
}

pow_add_r: (all n:Nat, m:Nat, o:Nat. m ^ (n + o) = m ^ n * m ^ o)

pow_mul_l: (all o:Nat, m:Nat, n:Nat. (m * n) ^ o = m ^ o * n ^ o)

pow_mul_r: (all o:Nat, m:Nat, n:Nat. (m ^ n) ^ o = m ^ (n * o))

pow_le_mono_l: (all c:Nat, a:Nat, b:Nat. (if a  b then a ^ c  b ^ c))

recfun gcd(a:Nat, b:Nat) -> Nat
measure	b  {
  if b = zero then
    a
  else
    gcd(b, a % b)
}

gcd_divides: (all b:Nat, a:Nat. (divides(gcd(a, b), a) and divides(gcd(a, b), b)))

less_equal_pow_log: (all n:Nat. n  pow2(log(n)))

fun lit(a:Nat) {
  a
}

nat_zero_add: (all y:Nat. ℕ0 + lit(y) = lit(y))

lit_suc_add: (all x:Nat, y:Nat. lit(suc(x)) + lit(y) = lit(suc(lit(x) + lit(y))))

suc_lit: (all n:Nat. suc(lit(n)) = lit(suc(n)))

lit_idem: (all x:Nat. lit(lit(x)) = lit(x))

nat_zero_mult: (all y:Nat. ℕ0 * lit(y) = ℕ0)

nat_one_mult: (all n:Nat. ℕ1 * n = n)

nat_mult_one: (all n:Nat. n * ℕ1 = n)

lit_suc_mult: (all m:Nat, n:Nat. lit(suc(m)) * lit(n) = lit(n) + lit(m) * lit(n))

lit_mult_lit_suc: (all m:Nat, n:Nat. lit(m) * lit(suc(n)) = lit(m) + lit(m) * lit(n))

lit_mult_suc: (all m:Nat, n:Nat. lit(m) * suc(n) = lit(m) + lit(m) * n)

nat_zero_monus: (all m:Nat. ℕ0  lit(m) = ℕ0)

nat_monus_zero: (all n:Nat. n  ℕ0 = n)

lit_suc_monus_suc: (all n:Nat, m:Nat. lit(suc(n))  lit(suc(m)) = lit(n)  lit(m))

lit_dist_mult_add: (all a:Nat, x:Nat, y:Nat. lit(a) * (x + y) = lit(a) * x + lit(a) * y)

lit_dist_mult_add_right: (all x:Nat, y:Nat, a:Nat. (x + y) * lit(a) = x * lit(a) + y * lit(a))

mult_two: (all n:Nat. n + n = ℕ2 * n)

lit_suc_add2: (all x:Nat, y:Nat. suc(lit(x) + y) = lit(suc(x)) + y)

nat_suc_one_add: (all n:Nat. suc(n) = ℕ1 + n)

lit_add_suc: (all n:Nat, m:Nat. lit(n) + suc(m) = lit(suc(n)) + m)

lit_mult_left_cancel: (all m:Nat, a:Nat, b:Nat. (if lit(suc(m)) * a = lit(suc(m)) * b then a = b))

sum_n: (all n:Nat. ℕ2 * summation(n, ℕ0, fun x { x }) = n * (n  ℕ1))

sum_n': (all n:Nat. ℕ2 * summation(suc(n), ℕ0, fun x { x }) = n * (n + ℕ1))

pos_mult_left_cancel: (all m:Nat, a:Nat, b:Nat. (if (ℕ0 < m and m * a = m * b) then a = b))

pos_mult_right_cancel_less: (all c:Nat, a:Nat, b:Nat. (if (ℕ0 < c and a * c < b * c) then a < b))

pos_mult_left_cancel_less_equal: (all n:Nat, x:Nat, y:Nat. (if (ℕ0 < n and n * x  n * y) then x  y))

pos_mult_both_sides_of_less: (all n:Nat, x:Nat, y:Nat. (if (ℕ0 < n and x < y) then n * x < n * y))

nat_zero_less_one_add: (all n:Nat. ℕ0 < ℕ1 + n)

nat_add_to_zero: (all n:Nat, m:Nat. (if n + m = ℕ0 then (n = ℕ0 and m = ℕ0)))

nat_less_add_pos: (all x:Nat, y:Nat. (if ℕ0 < y then x < x + y))

nat_monus_zero_iff_less_eq: (all x:Nat, y:Nat. ((x  y)  (x  y = ℕ0)))

nat_monus_one_pred: (all x:Nat. x  ℕ1 = pred(x))

nat_monus_cancel: (all n:Nat. n  n = ℕ0)

nat_zero_or_positive: (all x:Nat. (x = ℕ0 or ℕ0 < x))

nat_not_one_add_zero: (all n:Nat. not (ℕ1 + n = ℕ0))

nat_positive_suc: (all n:Nat. (if ℕ0 < n then some n':Nat. n = ℕ1 + n'))

nat_zero_le_zero: (all x:Nat. (if x  ℕ0 then x = ℕ0))

summation_next: (all n:Nat, s:Nat, f:(fn Nat -> Nat). summation(ℕ1 + n, s, f) = summation(n, s, f) + f(s + n))

less_zero_false: (all x:Nat. (x < zero) = false)

zero_less_equal_true: (all x:Nat. (zero  x) = true)