Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Int.toNat simproc #3440

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/Int.lean
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,6 @@ builtin_simproc [simp, seval] reducePow ((_ : Int) ^ (_ : Nat)) := fun e => do
let some v₂ ← Nat.fromExpr? e.appArg! | return .continue
return .done { expr := toExpr (v₁ ^ v₂) }

builtin_simproc [simp, seval] reduceAbs (natAbs _) := fun e => do
unless e.isAppOfArity ``natAbs 1 do return .continue
let some v ← fromExpr? e.appArg! | return .continue
return .done { expr := mkNatLit (natAbs v) }

builtin_simproc [simp, seval] reduceLT (( _ : Int) < _) := reduceBinPred ``LT.lt 4 (. < .)
builtin_simproc [simp, seval] reduceLE (( _ : Int) ≤ _) := reduceBinPred ``LE.le 4 (. ≤ .)
builtin_simproc [simp, seval] reduceGT (( _ : Int) > _) := reduceBinPred ``GT.gt 4 (. > .)
Expand All @@ -105,4 +100,12 @@ builtin_simproc [simp, seval] reduceNe (( _ : Int) ≠ _) := reduceBinPred ``N
builtin_simproc [simp, seval] reduceBEq (( _ : Int) == _) := reduceBoolPred ``BEq.beq 4 (. == .)
builtin_simproc [simp, seval] reduceBNe (( _ : Int) != _) := reduceBoolPred ``bne 4 (. != .)

@[inline] def reduceNatCore (declName : Name) (op : Int → Nat) (e : Expr) : SimpM Step := do
unless e.isAppOfArity declName 1 do return .continue
let some v ← fromExpr? e.appArg! | return .continue
return .done { expr := mkNatLit (op v) }

builtin_simproc [simp, seval] reduceAbs (natAbs _) := reduceNatCore ``natAbs natAbs
builtin_simproc [simp, seval] reduceToNat (Int.toNat _) := reduceNatCore ``Int.toNat Int.toNat

end Int
10 changes: 10 additions & 0 deletions tests/lean/run/simproc2.lean
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ example : foo x = 8 + 2 := by
fail_if_success simp [-Nat.reduceAdd]
simp only [Nat.reduceAdd]
rw [foo]

example (h : 0 = x) : (-2).toNat = x := by
simp
guard_target =ₛ 0 = x
assumption

example (h : 1 = x) : (1 : Int).toNat = x := by
simp
guard_target =ₛ 1 = x
assumption
Loading