@@ -11,6 +11,7 @@ import Init.Data.Hashable
1111import all Init.Data.Ord
1212import Init.Data.RArray
1313import Init.Grind.CommRing.Basic
14+ import Init.Grind.Ordered.Ring
1415
1516namespace Lean.Grind
1617namespace CommRing
@@ -35,13 +36,13 @@ abbrev Context (α : Type u) := RArray α
3536def Var.denote {α} (ctx : Context α) (v : Var) : α :=
3637 ctx.get v
3738
38- def denoteInt {α} [CommRing α] (k : Int) : α :=
39+ def denoteInt {α} [Ring α] (k : Int) : α :=
3940 bif k < 0 then
4041 - OfNat.ofNat (α := α) k.natAbs
4142 else
4243 OfNat.ofNat (α := α) k.natAbs
4344
44- def Expr.denote {α} [CommRing α] (ctx : Context α) : Expr → α
45+ def Expr.denote {α} [Ring α] (ctx : Context α) : Expr → α
4546 | .add a b => denote ctx a + denote ctx b
4647 | .sub a b => denote ctx a - denote ctx b
4748 | .mul a b => denote ctx a * denote ctx b
@@ -1139,5 +1140,72 @@ theorem imp_keqC {α c} [CommRing α] [IsCharP α c] (ctx : Context α) [NoNatZe
11391140
11401141end Stepwise
11411142
1143+ /-! IntModule interface -/
1144+
1145+ def Poly.denoteAsIntModule [CommRing α] (ctx : Context α) (p : Poly) : α :=
1146+ match p with
1147+ | .num k => Int.cast k * 1
1148+ | .add k m p => Int.cast k * m.denote ctx + denote ctx p
1149+
1150+ theorem Poly.denoteAsIntModule_eq_denote {α} [CommRing α] (ctx : Context α) (p : Poly) : p.denoteAsIntModule ctx = p.denote ctx := by
1151+ induction p <;> simp [*, denoteAsIntModule, denote, mul_one]
1152+
1153+ open Stepwise
1154+
1155+ theorem eq_norm {α} [CommRing α] (ctx : Context α) (lhs rhs : Expr) (p : Poly)
1156+ : core_cert lhs rhs p → lhs.denote ctx = rhs.denote ctx → p.denoteAsIntModule ctx = 0 := by
1157+ rw [Poly.denoteAsIntModule_eq_denote]; apply core
1158+
1159+ theorem diseq_norm {α} [CommRing α] (ctx : Context α) (lhs rhs : Expr) (p : Poly)
1160+ : core_cert lhs rhs p → lhs.denote ctx ≠ rhs.denote ctx → p.denoteAsIntModule ctx ≠ 0 := by
1161+ simp [core_cert, Poly.denoteAsIntModule_eq_denote]; intro _ h; subst p; simp [Expr.denote_toPoly, Expr.denote]
1162+ intro h; rw [sub_eq_zero_iff] at h; contradiction
1163+
1164+ open IntModule.IsOrdered
1165+
1166+ theorem le_norm {α} [CommRing α] [Preorder α] [Ring.IsOrdered α] (ctx : Context α) (lhs rhs : Expr) (p : Poly)
1167+ : core_cert lhs rhs p → lhs.denote ctx ≤ rhs.denote ctx → p.denoteAsIntModule ctx ≤ 0 := by
1168+ simp [core_cert, Poly.denoteAsIntModule_eq_denote]; intro _ h; subst p; simp [Expr.denote_toPoly, Expr.denote]
1169+ replace h := add_le_left h ((-1 ) * rhs.denote ctx)
1170+ rw [neg_mul, ← sub_eq_add_neg, one_mul, ← sub_eq_add_neg, sub_self] at h
1171+ assumption
1172+
1173+ theorem lt_norm {α} [CommRing α] [Preorder α] [Ring.IsOrdered α] (ctx : Context α) (lhs rhs : Expr) (p : Poly)
1174+ : core_cert lhs rhs p → lhs.denote ctx < rhs.denote ctx → p.denoteAsIntModule ctx < 0 := by
1175+ simp [core_cert, Poly.denoteAsIntModule_eq_denote]; intro _ h; subst p; simp [Expr.denote_toPoly, Expr.denote]
1176+ replace h := add_lt_left h ((-1 ) * rhs.denote ctx)
1177+ rw [neg_mul, ← sub_eq_add_neg, one_mul, ← sub_eq_add_neg, sub_self] at h
1178+ assumption
1179+
1180+ theorem not_le_norm {α} [CommRing α] [LinearOrder α] [Ring.IsOrdered α] (ctx : Context α) (lhs rhs : Expr) (p : Poly)
1181+ : core_cert rhs lhs p → ¬ lhs.denote ctx ≤ rhs.denote ctx → p.denote ctx < 0 := by
1182+ simp [core_cert]; intro _ h₁; subst p; simp [Expr.denote_toPoly, Expr.denote]
1183+ replace h₁ := LinearOrder.lt_of_not_le h₁
1184+ replace h₁ := add_lt_left h₁ (-lhs.denote ctx)
1185+ simp [← sub_eq_add_neg, sub_self] at h₁
1186+ assumption
1187+
1188+ theorem not_lt_norm {α} [CommRing α] [LinearOrder α] [Ring.IsOrdered α] (ctx : Context α) (lhs rhs : Expr) (p : Poly)
1189+ : core_cert rhs lhs p → ¬ lhs.denote ctx < rhs.denote ctx → p.denote ctx ≤ 0 := by
1190+ simp [core_cert]; intro _ h₁; subst p; simp [Expr.denote_toPoly, Expr.denote]
1191+ replace h₁ := LinearOrder.le_of_not_lt h₁
1192+ replace h₁ := add_le_left h₁ (-lhs.denote ctx)
1193+ simp [← sub_eq_add_neg, sub_self] at h₁
1194+ assumption
1195+
1196+ theorem not_le_norm' {α} [CommRing α] [Preorder α] [Ring.IsOrdered α] (ctx : Context α) (lhs rhs : Expr) (p : Poly)
1197+ : core_cert lhs rhs p → ¬ lhs.denote ctx ≤ rhs.denote ctx → ¬ p.denote ctx ≤ 0 := by
1198+ simp [core_cert]; intro _ h₁; subst p; simp [Expr.denote_toPoly, Expr.denote]; intro h
1199+ replace h := add_le_right (rhs.denote ctx) h
1200+ rw [sub_eq_add_neg, add_left_comm, ← sub_eq_add_neg, sub_self] at h; simp [add_zero] at h
1201+ contradiction
1202+
1203+ theorem not_lt_norm' {α} [CommRing α] [Preorder α] [Ring.IsOrdered α] (ctx : Context α) (lhs rhs : Expr) (p : Poly)
1204+ : core_cert lhs rhs p → ¬ lhs.denote ctx < rhs.denote ctx → ¬ p.denote ctx < 0 := by
1205+ simp [core_cert]; intro _ h₁; subst p; simp [Expr.denote_toPoly, Expr.denote]; intro h
1206+ replace h := add_lt_right (rhs.denote ctx) h
1207+ rw [sub_eq_add_neg, add_left_comm, ← sub_eq_add_neg, sub_self] at h; simp [add_zero] at h
1208+ contradiction
1209+
11421210end CommRing
11431211end Lean.Grind
0 commit comments