This PR adjusts the results of `inferInstanceAs` and the `def` `deriving` handler to conform to recently strengthened restrictions on reducibility. This change ensures that when deriving or inferring an instance for a semireducible type definition, the definition's RHS is not leaked when the instance is reduced at lower than semireducible transparency. More specifically, given the "source type" and "target type" (the given and expected type for `inferInstanceAs`, the right-hand side and applied left-hand side of the `def` for `deriving`), we synthesize an instance for the source type and then unfold and rewrap its components (fields, nested instances) as necessary to make them compatible with the target type. The individual steps are represented by the following options, which all default to enabled and can be disabled to help with porting: - `backward.inferInstanceAs.wrap`: master switch for instance adjustment in both `inferInstanceAs` and the default `deriving` handler - `backward.inferInstanceAs.wrap.reuseSubInstances`: reuse existing instances for the target type for sub-instance fields to avoid non-defeq instance diamonds - `backward.inferInstanceAs.wrap.instances`: wrap non-reducible instances in auxiliary definitions - `backward.inferInstanceAs.wrap.data`: wrap data fields in auxiliary definitions (proof fields are always wrapped) This PR is an extension and rewrite of prior work in Mathlib: https://github.com/leanprover-community/mathlib4/pull/36420 Last(?) part of fix for #9077 🤖 Prepared with Claude Code # Breaking changes Proofs that relied on the prior "defeq abuse" of these instance or that depended on their specific structure may need adjustments. As `inferInstanceAs A` now needs to know the source and target types exactly before it can continue, it cannot be used anymore as a synonym for `(inferInstance : A)`, use the latter instead when source and target type are identical.
73 lines
2.6 KiB
Text
73 lines
2.6 KiB
Text
inductive LazyList (α : Type u)
|
||
| nil : LazyList α
|
||
| cons (hd : α) (tl : LazyList α) : LazyList α
|
||
| delayed (t : Thunk (LazyList α)) : LazyList α
|
||
|
||
namespace LazyList
|
||
def length : LazyList α → Nat
|
||
| nil => 0
|
||
| cons _ as => length as + 1
|
||
| delayed as => length as.get
|
||
|
||
def force : LazyList α → Option (α × LazyList α)
|
||
| delayed as => force as.get
|
||
| nil => none
|
||
| cons a as => some (a,as)
|
||
end LazyList
|
||
|
||
def rotate (f : LazyList τ) (r : List τ) (a : LazyList τ)
|
||
(h : f.length + 1 = r.length) : LazyList τ :=
|
||
match r with
|
||
| List.nil => False.elim (by simp +arith [LazyList.length] at h)
|
||
| y::r' =>
|
||
match f.force with
|
||
| none => LazyList.cons y a
|
||
| some (x, f') => LazyList.cons x (rotate f' r' (LazyList.cons y a) (sorry))
|
||
|
||
theorem rotate_inv {F : LazyList τ} {R : List τ} : (h : F.length + 1 = R.length) → (rotate F R nil h).length = F.length + R.length := by
|
||
match F with
|
||
| LazyList.nil => intro h; unfold rotate; sorry
|
||
| LazyList.cons Fh Ft => sorry
|
||
| LazyList.delayed Ft => sorry
|
||
|
||
def LazyList.ind {α : Type u} {motive : LazyList α → Sort v}
|
||
(nil : motive LazyList.nil)
|
||
(cons : (hd : α) → (tl : LazyList α) → motive tl → motive (LazyList.cons hd tl))
|
||
(delayed : (t : Thunk (LazyList α)) → motive t.get → motive (LazyList.delayed t))
|
||
(t : LazyList α) : motive t :=
|
||
match t with
|
||
| LazyList.nil => nil
|
||
| LazyList.cons h t => cons h t (ind nil cons delayed t)
|
||
| LazyList.delayed t => delayed t (ind nil cons delayed t.get)
|
||
-- Remark: Lean used well-founded recursion behind the scenes to define LazyList.ind
|
||
|
||
/--
|
||
trace: case cons
|
||
τ : Type u_1
|
||
nil : LazyList τ
|
||
R : List τ
|
||
h : τ
|
||
t : LazyList τ
|
||
ih : ∀ (h : t.length + 1 = R.length), (rotate t R nil h).length = t.length + R.length
|
||
⊢ ∀ (h_1 : (LazyList.cons h t).length + 1 = R.length),
|
||
(rotate (LazyList.cons h t) R nil h_1).length = (LazyList.cons h t).length + R.length
|
||
---
|
||
trace: case delayed
|
||
τ : Type u_1
|
||
nil : LazyList τ
|
||
R : List τ
|
||
t : Thunk (LazyList τ)
|
||
a✝ : ∀ (h : t.get.length + 1 = R.length), (rotate t.get R nil h).length = t.get.length + R.length
|
||
⊢ ∀ (h : (LazyList.delayed t).length + 1 = R.length),
|
||
(rotate (LazyList.delayed t) R nil h).length = (LazyList.delayed t).length + R.length
|
||
---
|
||
warning: declaration uses `sorry`
|
||
---
|
||
warning: declaration uses `sorry`
|
||
-/
|
||
#guard_msgs in
|
||
theorem rotate_inv' {F : LazyList τ} {R : List τ} : (h : F.length + 1 = R.length) → (rotate F R nil h).length = F.length + R.length := by
|
||
induction F using LazyList.ind with
|
||
| nil => intro h; unfold rotate; sorry
|
||
| cons h t ih => trace_state; sorry
|
||
| delayed t => trace_state; sorry
|