@@ -338,7 +338,7 @@ macro "initialize_simps_projections?" rest:simpsProj : command =>
338
338
end Command
339
339
end Lean.Parser
340
340
341
- initialize registerTraceClass `Simps .verbose
341
+ initialize registerTraceClass `simps .verbose
342
342
initialize registerTraceClass `simps.debug
343
343
344
344
/-- Projection data for a single projection of a structure -/
@@ -503,14 +503,14 @@ def simpsApplyProjectionRules (str : Name) (rules : Array ProjectionRule) :
503
503
/-- Auxilliary function for `simpsGetRawProjections`.
504
504
Find custom projections declared by the user. -/
505
505
def simpsFindCustomProjection (str : Name) (proj : ParsedProjectionData)
506
- (rawUnivs : List Level) (trc : Bool) : CoreM ParsedProjectionData := do
506
+ (rawUnivs : List Level) : CoreM ParsedProjectionData := do
507
507
let env ← getEnv
508
508
let (rawExpr, nrs) ← MetaM.run' (getCompositeOfProjections str proj.origName.getString!)
509
509
match env.find? (str ++ `Simps ++ proj.newName) with
510
510
| some d@(.defnInfo _) =>
511
511
let customProj := d.instantiateValueLevelParams! rawUnivs
512
- if trc then
513
- logInfo m! "[simps] > found custom projection for {proj.newName}: \n > { customProj}"
512
+ trace[simps.verbose] "[simps] > found custom projection for {proj.newName}: \n > {
513
+ customProj}"
514
514
match (← MetaM.run' $ isDefEq customProj rawExpr) with
515
515
| true => pure { proj with expr? := some customProj, projNrs := nrs, isChanged := true }
516
516
| false =>
@@ -529,8 +529,9 @@ def simpsFindCustomProjection (str : Name) (proj : ParsedProjectionData)
529
529
530
530
/-- Auxilliary function for `simpsGetRawProjections`.
531
531
Resolve a single notation class in `simpsFindAutomaticProjections`. -/
532
+ -- currently unused
532
533
def simpsResolveNotationClass (projs : Array ParsedProjectionData)
533
- (className : Name) (args : Array Expr) (eStr : Expr) (rawUnivs : List Level) (trc : Bool) :
534
+ (className : Name) (args : Array Expr) (eStr : Expr) (rawUnivs : List Level) :
534
535
MetaM (Array ParsedProjectionData) := do
535
536
let env ← getEnv
536
537
let classInfo := (getStructureInfo? env className).get!
@@ -551,38 +552,35 @@ def simpsResolveNotationClass (projs : Array ParsedProjectionData)
551
552
trace[simps.debug] "info: ({relevantProj}, {rawExprLambda})"
552
553
pure (relevantProj, rawExprLambda)
553
554
let some pos := projs.findIdx? fun x ↦ some x.origName == relevantProj | do
554
- if trc then
555
- logInfo m!"[simps] > Warning: The structure has an instance for {className}, {""
555
+ trace[simps.verbose] "[simps] > Warning: The structure has an instance for {className}, {""
556
556
}but it is not definitionally equal to any projection."
557
557
failure -- will be caught by `simpsFindAutomaticProjections`
558
558
trace[simps.debug] "The raw projection is:\n {rawExprLambda}"
559
559
projs.mapIdxM fun nr x ↦ do
560
560
unless nr.1 = pos do return x
561
561
if x.isChanged then
562
- if trc then
563
- logInfo m!"[simps] > Warning: Projection {relevantProj} is definitionally equal to\n {""
564
- }{rawExprLambda}\n However, this is not used since a custom simps projection is {""
565
- }specified by the user."
562
+ trace[simps.verbose] "[simps] > Warning: Projection {relevantProj} is definitionally equal to{
563
+ indentExpr rawExprLambda}\n However, this is not used since a custom simps projection is {
564
+ ""}specified by the user."
566
565
return x
567
- if trc then
568
- logInfo m!"[simps] > Using notation from {className} for projection {relevantProj}."
566
+ trace[simps.verbose] "[simps] > Using notation from {className} for projection {relevantProj}."
569
567
return { x with expr? := some rawExprLambda }
570
568
571
569
/-- Auxilliary function for `simpsGetRawProjections`.
572
570
Find custom projections, automatically found by simps.
573
571
These come from algebraic notation classes, like `+`. -/
574
- -- if performance becomes a problem, possible heuristic: use the names of the projections to
575
- -- skip all classes that don't have the corresponding field.
572
+ -- todo: just navigate all projections and check if there is one called "add"/"mul" etc.
573
+ -- currently unused
576
574
def simpsFindAutomaticProjections (str : Name) (projs : Array ParsedProjectionData)
577
- (strType : Expr) (rawUnivs : List Level) (trc : Bool) : CoreM (Array ParsedProjectionData) := do
575
+ (strType : Expr) (rawUnivs : List Level) : CoreM (Array ParsedProjectionData) := do
578
576
let env ← getEnv
579
577
MetaM.run' <| forallTelescope strType fun args _ ↦ do
580
578
let eStr := mkAppN (.const str rawUnivs) args
581
579
let automaticProjs := notationClassAttr.getState env
582
580
let mut projs := projs
583
581
if args.size == 1 then -- can be wrong if additional type-class arguments??
584
582
for (className, _) in automaticProjs do
585
- try projs ← simpsResolveNotationClass projs className args eStr rawUnivs trc
583
+ try projs ← simpsResolveNotationClass projs className args eStr rawUnivs
586
584
catch _ => pure ()
587
585
return projs
588
586
@@ -623,40 +621,38 @@ Optionally, this command accepts three optional arguments:
623
621
has the attribute `@[simpsStructure]`.
624
622
* The `rules` argument specifies whether projections should be added, renamed, used as prefix, and
625
623
not used by default.
626
- * if `trc` is true, this tactic will trace information.
624
+ * if `trc` is true, this tactic will trace information just as if
625
+ `set_option trace.simps.verbose true` was set.
627
626
-/
628
627
def simpsGetRawProjections (str : Name) (traceIfExists : Bool := false )
629
628
(rules : Array ProjectionRule := #[]) (trc := false ) :
630
629
CoreM (List Name × Array ProjectionData) := do
630
+ withOptions (· |>.updateBool `trace.simps.verbose (trc || ·)) <| do
631
631
let env ← getEnv
632
- let trc := trc || (← getOptions).getBool `trace.simps.verbose
633
- -- to do: double check tracing
634
632
if let some data := (simpsStructure.getState env).find? str then
635
633
-- We always print the projections when they already exists and are called by
636
634
-- `initialize_simps_projections`.
637
- if traceIfExists || (← getOptions).getBool `trace.simps.verbose then
638
- logInfo <|
635
+ withOptions (· |>.updateBool `trace.simps.verbose (traceIfExists || ·)) <| do
636
+ trace[simps.verbose]
639
637
projectionsInfo data.2 .toList "Already found projection information for structure" str
640
638
return data
641
- if trc then
642
- logInfo m!"[simps] > generating projection information for structure {str}."
639
+ trace[simps.verbose] "[simps] > generating projection information for structure {str}."
643
640
trace[simps.debug] "Applying the rules {rules}."
644
641
let some strDecl := env.find? str
645
642
| throwError "No such declaration {str}." -- maybe unreachable
646
643
let rawLevels := strDecl.levelParams
647
644
let rawUnivs := rawLevels.map Level.param
648
645
let projs ← simpsApplyProjectionRules str rules
649
- let projs ← projs.mapM fun proj ↦ simpsFindCustomProjection str proj rawUnivs trc
646
+ let projs ← projs.mapM fun proj ↦ simpsFindCustomProjection str proj rawUnivs
650
647
-- the following will not work properly with Lean 4-style structure bundling
651
- -- let projs ← simpsFindAutomaticProjections str projs strDecl.type rawUnivs trc
648
+ -- let projs ← simpsFindAutomaticProjections str projs strDecl.type rawUnivs
652
649
let projs := projs.map (·.toProjectionData)
653
- -- make all proof non-default.
650
+ -- make all proofs non-default.
654
651
let projs ← projs.mapM fun proj ↦ do
655
652
match (← MetaM.run' <| isProof proj.expr) with
656
653
| true => pure { proj with isDefault := false }
657
654
| false => pure proj
658
- if trc then
659
- logInfo <| projectionsInfo projs.toList "generated projections for" str
655
+ trace[simps.verbose] projectionsInfo projs.toList "generated projections for" str
660
656
simpsStructure.add str (rawLevels, projs)
661
657
trace[simps.debug] "Generated raw projection data:\n {(rawLevels, projs)}"
662
658
pure (rawLevels, projs)
@@ -716,8 +712,6 @@ structure Simps.Config where
716
712
E.g. if we write `@[simps] def e : α × β ≃ β × α := ...` we will generate `e_apply` and not
717
713
`e_apply_fst`. -/
718
714
notRecursive := [`Prod, `PProd]
719
- /-- Output tracing messages. Can be set to `true` by writing `@[simps?]`. -/
720
- trace := false
721
715
/-- Output debug messages. Not used much, use `set_option simps.debug true` instead. -/
722
716
debug := false
723
717
/-- [ TODO ] Add `@[to_additive]` to all generated lemmas. This can be set by marking the
@@ -780,7 +774,7 @@ def simpsGetProjectionExprs (tgt : Expr) (rhs : Expr) (cfg : Simps.Config) :
780
774
let str := tgt.getAppFn.constName?.getD default
781
775
-- the fields of the object
782
776
let rhsArgs := rhs.getAppArgs.toList.drop params.size
783
- let (rawUnivs, projDeclata) ← simpsGetRawProjections str false #[] cfg.trace
777
+ let (rawUnivs, projDeclata) ← simpsGetRawProjections str
784
778
return projDeclata.map fun proj ↦
785
779
(rhsArgs.getD (a₀ := default) proj.projNrs.head!,
786
780
{ proj with
@@ -818,8 +812,7 @@ def simpsAddProjection (declName : Name) (type lhs rhs : Expr) (args : Array Exp
818
812
let eqAp := mkApp3 (mkConst `Eq [lvl]) type lhs rhs
819
813
let declType ← mkForallFVars args eqAp
820
814
let declValue ← mkLambdaFVars args prf
821
- if cfg.trace then
822
- logInfo m!"[simps] > adding projection {declName}:\n > {declType}"
815
+ trace[simps.verbose] "[simps] > adding projection {declName}:\n > {declType}"
823
816
try
824
817
addDecl <| .thmDecl {
825
818
name := declName
@@ -832,7 +825,9 @@ def simpsAddProjection (declName : Name) (type lhs rhs : Expr) (args : Array Exp
832
825
addSimpTheorem simpExtension declName true false .global <| eval_prio default
833
826
-- cfg.attrs.mapM fun nm ↦ setAttribute nm declName tt -- todo: deal with attributes
834
827
if let some tgt := cfg.addAdditive then
835
- ToAdditive.addToAdditiveAttr declName ⟨false , cfg.trace, tgt, none, true , ref⟩
828
+ ToAdditive.addToAdditiveAttr declName
829
+ -- tracing seems to fail
830
+ ⟨false , (← getOptions) |>.getBool `trace.to_additive, tgt, none, true , ref⟩
836
831
837
832
/--
838
833
Perform head-structure-eta-reduction on expression `e`. That is, if `e` is of the form
@@ -919,8 +914,7 @@ partial def simpsAddProjections (nm : Name) (type lhs rhs : Expr)
919
914
if !rhsWhnf.getAppFn.isConstOf ctor then
920
915
-- if I'm about to run into an error, try to set the transparency for `rhsMd` higher.
921
916
if cfg.rhsMd == .reducible && (mustBeStr || !todoNext.isEmpty || !toApply.isEmpty) then
922
- if cfg.trace then
923
- logInfo m!"[simps] > The given definition is not a constructor {""
917
+ trace[simps.verbose] "[simps] > The given definition is not a constructor {""
924
918
}application:\n > {rhsWhnf}\n > Retrying with the options {""
925
919
}\{rhsMd := semireducible, simpRhs := tt}."
926
920
let nms ← simpsAddProjections nm type lhs rhs args mustBeStr
@@ -988,16 +982,15 @@ partial def simpsAddProjections (nm : Name) (type lhs rhs : Expr)
988
982
If `shortNm` is true, the generated names will only use the last projection name.
989
983
If `trc` is true, trace as if `trace.simps.verbose` is true. -/
990
984
def simpsTac (ref : Syntax) (nm : Name) (cfg : Simps.Config := {}) (todo : List String := [])
991
- (trc := false ) : AttrM (Array Name) := do
985
+ (trc := false ) : AttrM (Array Name) :=
986
+ withOptions (· |>.updateBool `trace.simps.verbose (trc || ·)) <| do
992
987
let env ← getEnv
993
988
let some d := env.find? nm | throwError "Declaration {nm} doesn't exist."
994
989
let lhs : Expr := mkConst d.name <| d.levelParams.map Level.param
995
990
let todo := todo.eraseDup.map fun proj ↦ "_" ++ proj
996
- let mut cfg :=
997
- { cfg with trace := cfg.trace || (← getOptions).getBool `trace.simps.verbose || trc }
991
+ let mut cfg := cfg
998
992
if let some addAdditive := ToAdditive.findTranslation? env nm then
999
- if cfg.trace then
1000
- logInfo m!"[simps] > @[to_additive] will be added to all generated lemmas."
993
+ trace[simps.verbose] "[simps] > @[to_additive] will be added to all generated lemmas."
1001
994
cfg := { cfg with addAdditive }
1002
995
MetaM.run' <| simpsAddProjections ref d.levelParams
1003
996
nm d.type lhs (d.value?.getD default) #[] (mustBeStr := true ) cfg todo []
0 commit comments