Skip to content

Commit

Permalink
feat: Nat simprocs for simplifying bit expressions (#4874)
Browse files Browse the repository at this point in the history
This came up in the context of simplifying proof states for
https://github.com/leanprover/LNSym.
  • Loading branch information
bollu authored Jul 31, 2024
1 parent d5a8c96 commit f869902
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Lean/Meta/Tactic/Simp/BuiltinSimprocs/Nat.lean
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ builtin_dsimproc [simp, seval] reducePow ((_ ^ _ : Nat)) := fun e => do
unless (← checkExponent m) do return .continue
return .done <| toExpr (n ^ m)

builtin_dsimproc [simp, seval] reduceAnd ((_ &&& _ : Nat)) := reduceBin ``HOr.hOr 6 (· &&& ·)
builtin_dsimproc [simp, seval] reduceXor ((_ ^^^ _ : Nat)) := reduceBin ``HXor.hXor 6 (· ^^^ ·)
builtin_dsimproc [simp, seval] reduceOr ((_ ||| _ : Nat)) := reduceBin ``HOr.hOr 6 (· ||| ·)

builtin_dsimproc [simp, seval] reduceShiftLeft ((_ <<< _ : Nat)) :=
reduceBin ``HShiftLeft.hShiftLeft 6 (· <<< ·)

builtin_dsimproc [simp, seval] reduceShiftRight ((_ >>> _ : Nat)) :=
reduceBin ``HShiftRight.hShiftRight 6 (· >>> ·)

builtin_dsimproc [simp, seval] reduceGcd (gcd _ _) := reduceBin ``gcd 2 gcd

builtin_simproc [simp, seval] reduceLT (( _ : Nat) < _) := reduceBinPred ``LT.lt 4 (. < .)
Expand Down
8 changes: 8 additions & 0 deletions tests/lean/run/simprocNat.lean
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
variable (a b : Nat)

/- bitwise operation tests -/

#check_simp (3 : Nat) &&& (1 : Nat) ~> 1
#check_simp (3 : Nat) ^^^ (1 : Nat) ~> 2
#check_simp (2 : Nat) ||| (1 : Nat) ~> 3
#check_simp (3 : Nat) <<< (2 : Nat) ~> 12
#check_simp (3 : Nat) >>> (1 : Nat) ~> 1

/- subtract diff tests -/

#check_simp (1000 : Nat) - 400 ~> 600
Expand Down

0 comments on commit f869902

Please sign in to comment.