Skip to content

Commit

Permalink
modified how projection are reduced in lsimp
Browse files Browse the repository at this point in the history
should improve state of #20 but still not solved
  • Loading branch information
lecopivo committed Sep 6, 2023
1 parent 7c9ac31 commit 3589f70
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 3 deletions.
21 changes: 21 additions & 0 deletions SciLean/Lean/Meta/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,20 @@ private def reduceProjections (e : Expr) (projections : List Projection) : CoreM
else
pure (applyProjections e projections)


/-- Return none if no projections happen
-/
private def reduceProjections? (e : Expr) (projections : List Projection) : CoreM (Option Expr) :=
match projections with
| [] => pure none
| p :: ps =>
matchConstCtor e.getAppFn (fun _ => pure none) fun info _ => do
if e.getAppNumArgs = info.numParams + info.numFields then
reduceProjections (e.getArg! (info.numParams + p.index)) ps
else
pure none


end ReduceProjOfCtor

open ReduceProjOfCtor in
Expand All @@ -455,3 +469,10 @@ def reduceProjOfCtor (e : Expr) : MetaM Expr := do
let (e',ps) ← peelOfProjections e
reduceProjections e' ps

open ReduceProjOfCtor in
/-- Reduces structure projection of explicit constructors
-/
def reduceProjOfCtor? (e : Expr) : MetaM (Option Expr) := do
let (e',ps) ← peelOfProjections e
reduceProjections? e' ps

7 changes: 4 additions & 3 deletions SciLean/Tactic/LSimp/Main.lean
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ def isOfNatNatLit (e : Expr) : Bool :=
def reduceProj? (e : Expr) : MetaM (Option Expr) := do
match e with
| Expr.proj _ _ (.fvar _) => return none -- do not reduce projections on fvars
| Expr.proj _ i c =>
| Expr.proj n i c =>
letTelescope c λ xs b => do
let some b ← Meta.project? b i
-- let some b ← Meta.project? b i
let some b ← reduceProjOfCtor? (.proj n i b)
| return none
mkLambdaFVars xs b
| _ => return none
Expand Down Expand Up @@ -378,7 +379,7 @@ where
| none => return { expr := e }

simpProj (e : Expr) : M Result := do
match (← reduceProj?' e) with
match (← reduceProj? e) with
| some e => return { expr := e }
| none =>
let s := e.projExpr!
Expand Down
84 changes: 84 additions & 0 deletions test/issues/20.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import Std.Tactic.GuardMsgs
import SciLean.Util.RewriteBy
import SciLean.Tactic.LSimp.Elab
import SciLean.Tactic.LetNormalize

open SciLean


/--
info: fun i =>
let j := 42;
let foo := ((i * j, i + j), i ^ j, i / j);
foo.fst : Nat → Nat × Nat
-/
#guard_msgs in
#check
(fun i : Nat =>
let j := 42
let foo := ((i * j, i+j), (i ^ j, i / j))
foo.fst)
rewrite_by
lsimp (config := {zeta:=false}) only


/--
info: fun i =>
(let j := 42;
let foo := ((i * j, i + j), i ^ j, i / j);
foo).fst : Nat → Nat × Nat
-/
#guard_msgs in
#check
(fun i : Nat =>
(let j := 42
let foo := ((i * j, i+j), (i ^ j, i / j))
foo).fst)
rewrite_by
ldsimp (config := {zeta:=false}) only


/--
info: fun i =>
(let j := 42;
let foo := ((i * j, i + j), i ^ j, i / j);
foo.fst).snd : Nat → Nat
-/
#guard_msgs in
#check
(fun i : Nat =>
(let j := 42
let foo := ((i * j, i+j), (i ^ j, i / j))
foo.fst).snd)
rewrite_by
lsimp (config := {zeta:=false, proj:=false}) only


/--
info: fun i =>
let j := 42;
let foo := ((i * j, i + j), i ^ j, i / j);
foo.fst.snd : Nat → Nat
-/
#guard_msgs in
#check
(fun i : Nat =>
let j := 42
let foo := ((i * j, i+j), (i ^ j, i / j))
foo.fst.snd)
rewrite_by
lsimp (config := {zeta:=false}) only


/--
info: let a := 42;
a : Nat
-/
#guard_msgs in
#check
(let a := 42
let b := 7
(a,b)).1
rewrite_by
lsimp (config := {zeta:=false}) only

0 comments on commit 3589f70

Please sign in to comment.