-
Notifications
You must be signed in to change notification settings - Fork 369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: handle dependent fields when deriving BEq
#3792
base: master
Are you sure you want to change the base?
Conversation
One downside of this derive handler is that the code it produces using def ZMod5 : Type := Quot (· % 5 = · % 5)
abbrev ZMod5.val : ZMod5 → Nat :=
Quot.lift (· % 5) sorry
instance : BEq ZMod5 where
beq x y := x.val == y.val
instance : LawfulBEq ZMod5 := sorry
structure MyStruct where
x : ZMod5
y : Fin x.val
-- This is approximately what would be generated by `deriving BEq` after this PR
def instBEqGenerated : BEq MyStruct where
beq x y :=
if h : x.1 == y.1 then
let x2' : Fin y.1.val := (eq_of_beq h) ▸ x.2
x2' == y.2
else
false
def instBEqPreferred : BEq MyStruct where
beq x y :=
if h : x.1 == y.1 then
let x2' : Fin y.1.val := Fin.cast (eq_of_beq h) x.2
x2' == y.2
else
false
#reduce instBEqPreferred.beq ⟨Quot.mk _ 6, 0⟩ ⟨Quot.mk _ 11, 0⟩ -- true
#reduce instBEqGenerated.beq ⟨Quot.mk _ 6, 0⟩ ⟨Quot.mk _ 11, 0⟩ -- _ (Nat.rec _ _ (⋯ ▸ 0)) That said, it doesn't seem feasible in the short term to generate code using functions like |
LGTM. Could you mark this as ready for review if you're happy to proceed with this? |
@arthur-adjedj, could you take a look at the CI failure? |
As it turns out, some parts of the code rely on the reduction behaviour of the derived instances, and these issues only arise when compiling on stage2, which I didn't try locally until now. Most of these are easy to correct, but I would rather not make this fix a breaking change, and I don't have much time to allocate to fixing this PR right now. I'll mark this as WIP and ping you back once I get this in a better state. Sorry for the trouble. |
WIP |
Mathlib CI status (docs):
|
@semorrison |
I have rebased this onto |
Oops, there are stage0 changes since |
Could you confirm that I correctly removed the |
Ah yes, not putting this as |
Closes #3740
If a field
x : xType
appears in the type of another fieldy : f xType
, it's generally not possible to derive a faithfulBEq
instance because the twoy
fields won't necessarily have definitionally equal types. However, such a problem can be avoided whenever the BEq onxType
is Lawful. This PR useseq_of_beq
wheneverxType
appears in another fvar's types, and unifies the dependent types as needed.