Skip to content

Commit 09cde79

Browse files
committed
fix: reenable structure eta during tc search
Fixes #2074.
1 parent 4b974fd commit 09cde79

3 files changed

Lines changed: 79 additions & 7 deletions

File tree

src/Lean/Meta/SynthInstance.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -668,13 +668,13 @@ def synthInstance? (type : Expr) (maxResultSize? : Option Nat := none) : MetaM (
668668
let opts ← getOptions
669669
let maxResultSize := maxResultSize?.getD (synthInstance.maxSize.get opts)
670670
/-
671-
We disable eta for structures that are not classes during TC resolution because it allows us to find unintended solutions.
672-
See discussion at
673-
https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/.60constructor.60.20and.20.60Applicative.60/near/279984801
671+
We explicitly enable eta for structures because that is required to make
672+
subobject projections and constructor applications commute (and we would get
673+
non-defeq diamonds otherwise).
674+
https://github.com/leanprover/lean4/issues/2074#issuecomment-1418198304
674675
-/
675676
withConfig (fun config => { config with isDefEqStuckEx := true, transparency := TransparencyMode.instances,
676-
foApprox := true, ctxApprox := true, constApprox := false,
677-
etaStruct := .notClasses }) do
677+
foApprox := true, ctxApprox := true, constApprox := false }) do
678678
let type ← instantiateMVars type
679679
let type ← preprocess type
680680
let s ← get

tests/lean/run/2074.lean

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
class NonUnitalNonAssocSemiring (α : Type u)
2+
3+
class NonUnitalSemiring (α : Type u) extends NonUnitalNonAssocSemiring α
4+
5+
class Semiring (α : Type u) extends NonUnitalSemiring α
6+
7+
class NonUnitalCommSemiring (α : Type u) extends NonUnitalSemiring α
8+
9+
class CommSemiring (R : Type u) extends Semiring R
10+
11+
class NonUnitalNonAssocRing (α : Type u) extends NonUnitalNonAssocSemiring α
12+
13+
class NonUnitalRing (α : Type _) extends NonUnitalNonAssocRing α, NonUnitalSemiring α
14+
15+
class Ring (R : Type u) extends Semiring R
16+
17+
class NonUnitalCommRing (α : Type u) extends NonUnitalRing α
18+
19+
class CommRing (α : Type u) extends Ring α
20+
21+
instance (priority := 100) NonUnitalCommRing.toNonUnitalCommSemiring [s : NonUnitalCommRing α] :
22+
NonUnitalCommSemiring α :=
23+
{ s with }
24+
25+
instance (priority := 100) CommRing.toCommSemiring [s : CommRing α] : CommSemiring α :=
26+
{ s with }
27+
28+
instance (priority := 100) CommSemiring.toNonUnitalCommSemiring [s : CommSemiring α] :
29+
NonUnitalCommSemiring α :=
30+
{ s with }
31+
32+
instance (priority := 100) CommRing.toNonUnitalCommRing [s : CommRing α] : NonUnitalCommRing α :=
33+
{ s with }
34+
35+
class StarRing' (R : Type _) [NonUnitalSemiring R]
36+
def starGizmo [CommSemiring R] [StarRing' R] : R → R := id
37+
theorem starGizmo_foo [CommRing R] [StarRing' R] (x : R) : starGizmo x = x := rfl
38+
39+
namespace ReidMWE
40+
41+
class A (α : Type u)
42+
43+
class B (α : Type u) extends A α
44+
45+
class C (α : Type u) extends B α
46+
47+
class D (α : Type u) extends B α
48+
49+
class E (α : Type u) extends C α, D α
50+
51+
class F (α : Type u) extends A α
52+
53+
class G (α : Type u) extends F α, B α
54+
55+
class H (α : Type u) extends C α
56+
57+
class I (α : Type u) extends G α, D α
58+
59+
class J (α : Type u) extends H α, I α, E α
60+
61+
class StarRing' (R : Type 0) [B R]
62+
def starGizmo [E R] [StarRing' R] : R → R := id
63+
64+
theorem starGizmo_foo [J R] [StarRing' R] (x : R) : starGizmo x = x := rfl
65+
66+
theorem T (i : J R) : (@D.toB.{0} R (@E.toD.{0} R (@J.toE.{0} R i))) = i.toB := rfl

tests/lean/run/tc_eta_struct_issue.lean

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
-- https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/.60constructor.60.20and.20.60Applicative.60/near/279949125
2+
13
def Op1 (F : Type u → Type v) α := F α
24

35
namespace Op1
@@ -13,10 +15,14 @@ variable {F} [Applicative F]
1315
instance : Applicative (Op1 F) where
1416
pure := pure (f := F)
1517
seq f x := ((λ x f => f x) <$> x () <*> f : F _)
16-
map := Functor.map (f := F)
1718

18-
variable [LawfulApplicative F]
19+
-- The original version of this mwe did not specify the mapConst etc. instances,
20+
-- causing a non-defeq diamond. Non-defeq diamonds for classes like Functor
21+
-- are not supported.
1922

23+
-- map := Functor.map (f := F)
24+
25+
variable [LawfulApplicative F]
2026
instance : LawfulApplicative (Op1 F) := by
2127
constructor
2228
repeat sorry

0 commit comments

Comments
 (0)