Skip to content

Commit

Permalink
fix to lsimp, call reduce instead of full dsimp in post pass, fixes #26
Browse files Browse the repository at this point in the history
  • Loading branch information
lecopivo committed Oct 31, 2023
1 parent 4ac0357 commit a4797fb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 40 deletions.
2 changes: 1 addition & 1 deletion SciLean/Core/FunctionPropositions/HasAdjDiffAt.lean
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ theorem HSMul.hSMul.arg_a1.HasAdjDiffAt_rule
: HasAdjDiffAt K (fun x => c • g x) x :=
by
have ⟨_,_⟩ := hg
constructor; fprop; ftrans; simp; fprop
constructor; fprop; ftrans; fprop



Expand Down
57 changes: 28 additions & 29 deletions SciLean/Tactic/LSimp2/Main.lean
Original file line number Diff line number Diff line change
Expand Up @@ -249,34 +249,33 @@ private partial def reduce (e : Expr) : SimpM Expr := withIncRecDepth do
| none => return e

private partial def dsimp (e : Expr) : M Expr := do
return e
-- let cfg ← Simp.getConfig
-- withTraceNode `ldsimp (fun _ => pure s!"ldsimp {cfg.dsimp}") do
-- unless cfg.dsimp do
-- return e
-- let pre (e : Expr) : M TransformStep := do
-- withTraceNode `ldsimp (fun _ => pure "ldsimp pre") do
-- if let Step.visit r ← rewritePre e (fun _ => pure none) (rflOnly := true) then
-- if r.expr != e then
-- return .visit r.expr
-- return .continue
-- let post (e : Expr) : M TransformStep := do
-- withTraceNode `ldsimp (fun _ => pure "ldsimp post") do
-- -- lsimp modification
-- -- remove unsued let bindings
-- let e :=
-- if e.isLet && ¬e.letBody!.hasLooseBVars then
-- e.letBody!
-- else
-- e
-- if let Step.visit r ← rewritePost e (fun _ => pure none) (rflOnly := true) then
-- if r.expr != e then
-- return .visit r.expr
-- let mut eNew ← reduce e
-- if cfg.zeta && eNew.isFVar then
-- eNew ← reduceFVar cfg eNew
-- if eNew != e then return .visit eNew else return .done e
-- transform (usedLetOnly := cfg.zeta) e (pre := pre) (post := post)
let cfg ← Simp.getConfig
withTraceNode `ldsimp (fun _ => pure s!"ldsimp {cfg.dsimp}") do
unless cfg.dsimp do
return e
let pre (e : Expr) : M TransformStep := do
withTraceNode `ldsimp (fun _ => pure "ldsimp pre") do
if let Step.visit r ← rewritePre e (fun _ => pure none) (rflOnly := true) then
if r.expr != e then
return .visit r.expr
return .continue
let post (e : Expr) : M TransformStep := do
withTraceNode `ldsimp (fun _ => pure "ldsimp post") do
-- lsimp modification
-- remove unsued let bindings
let e :=
if e.isLet && ¬e.letBody!.hasLooseBVars then
e.letBody!
else
e
if let Step.visit r ← rewritePost e (fun _ => pure none) (rflOnly := true) then
if r.expr != e then
return .visit r.expr
let mut eNew ← reduce e
if cfg.zeta && eNew.isFVar then
eNew ← reduceFVar cfg eNew
if eNew != e then return .visit eNew else return .done e
transform (usedLetOnly := cfg.zeta) e (pre := pre) (post := post)

instance : Inhabited (M α) where
default := fun _ _ _ => default
Expand Down Expand Up @@ -468,7 +467,7 @@ where
let r ← mkEqTrans r r'
-- lsimp modification
-- clean up expressions after rewrites
let r ← mkEqTrans r { expr := ← dsimp r.expr }
let r ← mkEqTrans r { expr := ← reduce r.expr }
if cfg.singlePass || init == r.expr then
cacheResult cfg r
else
Expand Down
13 changes: 3 additions & 10 deletions test/issues/26.lean
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import SciLean


variable (a : Nat)

/- expected
/--
info: let x := a + a;
a + (a + x) : ℕ
-/

/--
info: a +
let x := a + a;
a + x : ℕ
-/
#guard_msgs in
#check
(a +
(a +
(a +
(a +
let x := a + a
x))
rewrite_by
Expand Down

0 comments on commit a4797fb

Please sign in to comment.