-
Notifications
You must be signed in to change notification settings - Fork 247
/
NormNum.lean
159 lines (131 loc) · 6.35 KB
/
NormNum.lean
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
/-
Copyright (c) 2021 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import Lean.Elab.Tactic.Basic
import Mathlib.Algebra.Ring.Basic
import Mathlib.Tactic.Core
namespace Lean
/--
Return true if `e` is one of the following
- A nat literal (numeral)
- `Nat.zero`
- `Nat.succ x` where `isNumeral x`
- `OfNat.ofNat _ x _` where `isNumeral x` -/
partial def Expr.numeral? (e : Expr) : Option Nat :=
if let some n := e.natLit? then n
else
let f := e.getAppFn
if !f.isConst then none
else
let fName := f.constName!
if fName == ``Nat.succ && e.getAppNumArgs == 1 then (numeral? e.appArg!).map Nat.succ
else if fName == ``OfNat.ofNat && e.getAppNumArgs == 3 then numeral? (e.getArg! 1)
else if fName == ``Nat.zero && e.getAppNumArgs == 0 then some 0
else none
namespace Meta
def mkOfNatLit (u : Level) (α sα n : Expr) : Expr :=
let inst := mkApp3 (mkConst ``Numeric.OfNat [u]) α n sα
mkApp3 (mkConst ``OfNat.ofNat [u]) α n inst
namespace NormNum
def isNat [Semiring α] (a : α) (n : ℕ) := a = OfNat.ofNat n
class LawfulOfNat (α) [Semiring α] (n) [OfNat α n] : Prop where
isNat_ofNat : isNat (OfNat.ofNat n : α) n
instance (α) [Semiring α] : LawfulOfNat α n := ⟨rfl⟩
instance (α) [Semiring α] : LawfulOfNat α (nat_lit 0) := ⟨Semiring.ofNat_zero.symm⟩
instance (α) [Semiring α] : LawfulOfNat α (nat_lit 1) := ⟨Semiring.ofNat_one.symm⟩
instance : LawfulOfNat Nat n := ⟨rfl⟩
instance : LawfulOfNat Int n := ⟨rfl⟩
theorem isNat_rawNat (n : ℕ) : isNat n n := rfl
class LawfulZero (α) [Semiring α] [Zero α] : Prop where
isNat_zero : isNat (Zero.zero : α) (nat_lit 0)
instance (α) [Semiring α] : LawfulZero α := ⟨Semiring.ofNat_zero.symm⟩
class LawfulOne (α) [Semiring α] [One α] : Prop where
isNat_one : isNat (One.one : α) (nat_lit 1)
instance (α) [Semiring α] : LawfulOne α := ⟨Semiring.ofNat_one.symm⟩
theorem isNat_add {α} [Semiring α] : (a b : α) → (a' b' c : Nat) →
isNat a a' → isNat b b' → Nat.add a' b' = c → isNat (a + b) c
| _, _, _, _, _, rfl, rfl, rfl => (Semiring.ofNat_add _ _).symm
theorem isNat_mul {α} [Semiring α] : (a b : α) → (a' b' c : Nat) →
isNat a a' → isNat b b' → Nat.mul a' b' = c → isNat (a * b) c
| _, _, _, _, _, rfl, rfl, rfl => (Semiring.ofNat_mul _ _).symm
theorem isNat_pow {α} [Semiring α] : (a : α) → (b a' b' c : Nat) →
isNat a a' → isNat b b' → Nat.pow a' b' = c → isNat (a ^ b) c
| _, _, _, _, _, rfl, rfl, rfl => (Semiring.ofNat_pow _ _).symm
def instSemiringNat : Semiring Nat := inferInstance
partial def evalIsNat (u : Level) (α sα e : Expr) : MetaM (Expr × Expr) := do
let (n, p) ← match e.getAppFnArgs with
| (``HAdd.hAdd, #[_, _, _, _, a, b]) => evalBinOp ``NormNum.isNat_add (·+·) a b
| (``HMul.hMul, #[_, _, _, _, a, b]) => evalBinOp ``NormNum.isNat_mul (·*·) a b
| (``HPow.hPow, #[_, _, _, _, a, b]) => evalPow ``NormNum.isNat_pow (·^·) a b
| (``OfNat.ofNat, #[_, ln, inst]) =>
let some n ← ln.natLit? | throwError "fail"
let lawful ← synthInstance (mkApp4 (mkConst ``LawfulOfNat [u]) α sα ln inst)
(ln, mkApp5 (mkConst ``LawfulOfNat.isNat_ofNat [u]) α sα ln inst lawful)
| (``Zero.zero, #[_, inst]) =>
let lawful ← synthInstance (mkApp3 (mkConst ``LawfulZero [u]) α sα inst)
(mkNatLit 0, mkApp4 (mkConst ``LawfulZero.isNat_zero [u]) α sα inst lawful)
| (``One.one, #[_, inst]) =>
let lawful ← synthInstance (mkApp3 (mkConst ``LawfulOne [u]) α sα inst)
(mkNatLit 1, mkApp4 (mkConst ``LawfulOne.isNat_one [u]) α sα inst lawful)
| _ =>
if e.isNatLit then (e, mkApp (mkConst ``isNat_rawNat) e)
else throwError "fail"
(n, mkApp2 (mkConst ``id [levelZero]) (mkApp4 (mkConst ``isNat [u]) α sα e n) p)
where
evalBinOp (name : Name) (f : Nat → Nat → Nat) (a b : Expr) : MetaM (Expr × Expr) := do
let (la, pa) ← evalIsNat u α sα a
let (lb, pb) ← evalIsNat u α sα b
let a' := la.natLit!
let b' := lb.natLit!
let c' := f a' b'
let lc := mkRawNatLit c'
(lc, mkApp10 (mkConst name [u]) α sα a b la lb lc pa pb (← mkEqRefl lc))
evalPow (name : Name) (f : Nat → Nat → Nat) (a b : Expr) : MetaM (Expr × Expr) := do
let (la, pa) ← evalIsNat u α sα a
let (lb, pb) ← evalIsNat levelZero (mkConst ``Nat) (mkConst ``instSemiringNat) b
let a' := la.natLit!
let b' := lb.natLit!
let c' := f a' b'
let lc := mkRawNatLit c'
(lc, mkApp10 (mkConst name [u]) α sα a b la lb lc pa pb (← mkEqRefl lc))
theorem eval_of_isNat {α} [Semiring α] (n) [OfNat α n] [LawfulOfNat α n] :
(a : α) → isNat a n → a = OfNat.ofNat n
| _, rfl => LawfulOfNat.isNat_ofNat.symm
def eval (e : Expr) : MetaM (Expr × Expr) := do
let α ← inferType e
let Level.succ u _ ← getLevel α | throwError "fail"
let sα ← synthInstance (mkApp (mkConst ``Semiring [u]) α)
let (ln, p) ← evalIsNat u α sα e
let ofNatInst ← synthInstance (mkApp2 (mkConst ``OfNat [u]) α ln)
let lawfulInst ← synthInstance (mkApp4 (mkConst ``LawfulOfNat [u]) α sα ln ofNatInst)
(mkApp3 (mkConst ``OfNat.ofNat [u]) α ln ofNatInst,
mkApp7 (mkConst ``eval_of_isNat [u]) α sα ln ofNatInst lawfulInst e p)
theorem eval_eq_of_isNat {α} [Semiring α] :
(a b : α) → (n : ℕ) → isNat a n → isNat b n → a = b
| _, _, _, rfl, rfl => rfl
def evalEq (α a b : Expr) : MetaM Expr := do
let Level.succ u _ ← getLevel α | throwError "fail"
let sα ← synthInstance (mkApp (mkConst ``Semiring [u]) α)
let (ln, pa) ← evalIsNat u α sα a
let (ln', pb) ← evalIsNat u α sα b
guard (ln.natLit! == ln'.natLit!)
mkApp7 (mkConst ``eval_eq_of_isNat [u]) α sα a b ln pa pb
end NormNum
end Meta
syntax (name := Parser.Tactic.normNum) "normNum" : tactic
open Meta Elab Tactic
@[tactic normNum] def Tactic.evalNormNum : Tactic := fun stx =>
liftMetaTactic fun g => do
let some (α, lhs, rhs) ← matchEq? (← getMVarType g) | throwError "fail"
let p ← NormNum.evalEq α lhs rhs
assignExprMVar g p
pure []
end Lean
variable (α) [Semiring α]
example : (1 + 0 : α) = (0 + 1 : α) := by normNum
example : (0 + (2 + 3) + 1 : α) = 6 := by normNum
example : (70 * (33 + 2) : α) = 2450 := by normNum
example : (8 + 2 ^ 2 * 3 : α) = 20 := by normNum
example : ((2 * 1 + 1) ^ 2 : α) = (3 * 3 : α) := by normNum