Skip to content

RFC: @[override_repr] attribute #9508

Open

Description

@Rob23oba

Proposal

Add an attribute that allows overriding the representation of an inductive type. All projections, constructors and casesOn should then become noncomputable by default; this should be overridable by @[implemented_by] and friends though. However, @[implemented_by] and friends should not work when the inductive type wasn't declared with @[override_repr].

Motivation:

In some cases there are two representations of a type: one that works well for proofs and another that works well at runtime but doesn't allow for proofs. Infinite lists are an example of this:

-- works well for proofs but inefficient
def InfiniteList (α : Type u) := Nat → α

-- works well at runtime but is formally an empty type
inductive InfiniteList (α : Type u) where
  | cons (x : α) (l : Thunk (InfiniteList α))

The @[override_repr] attribute would then allow specifying a "model implementation" of a type for proofs and using another type for runtime:

inductive InfiniteListImpl (α : Type u) where
  | cons (x : α) (l : Thunk (InfiniteList α))

@[override_repr InfiniteListImpl]
structure InfiniteList (α : Type u) where
  get : Nat → α

Since InfiniteListImpl is now used as the representation of InfiniteList, the usual implementation of InfiniteList.mk and InfiniteList.get doesn't make any sense anymore (since it relied on the representation being the one of InfiniteList). Thus they should become noncomputable by default. However, you should then be able to override this by specifying your own implementation:

def InfiniteListImpl.get {α : Type u} (l : InfiniteListImpl α) (n : Nat) : α :=
  match n, l with
  | 0, .cons x t => x
  | k + 1, .cons x t => t.get.get k

unsafe def InfiniteList.getImpl {α : Type u} (l : InfiniteList α) (n : Nat) : α := (unsafeCast l : InfiniteListImpl α).get n

attribute [implemented_by InfiniteList.getImpl] InfiniteList.get

Overriding projections (and constructors) only makes sense with @[override_repr] though; as such it should be forbidden for regular inductives and point to @[override_repr]:

structure InfiniteList (α : Type u) where
  get : Nat → α

unsafe def InfiniteList.getImpl {α : Type u} (l : InfiniteList α) (n : Nat) : α := (unsafeCast l : InfiniteListImpl α).get n

/-
invalid implemented_by: target is a projection of InfiniteList but InfiniteList doesn't have an `@[override_repr]` attribute.
-/
attribute [implemented_by InfiniteList.getImpl] InfiniteList.get

Another use case is a simpler definition of Erased that has good reduction properties:

@[override_repr True]
structure Erased (α : Sort u) where
  out : α

Specifying True here should make the representation into the same of True: ◾ (erased). The behavior should then also match what you'd expect from Erased: Erased.out is noncomputable and Erased.mk would be noncomputable but is erased anyways so works similarly to proofs.

Community Feedback

https://leanprover.zulipchat.com/#narrow/channel/270676-lean4/topic/RFC.3A.20.60.40.5Boverride_repr.5D.60.20attribute/with/530131350

Impact

Add 👍 to issues you consider important. If others benefit from the changes in this proposal being added, please ask them to add 👍 to it.

Activity

  1. added
    RFC
    on Jul 24, 2025
  2. nomeata commented on Jul 24, 2025

    @nomeata
    Collaborator

    Thanks for the RFC! Something like this is on my wishlist as well.

    I wonder if we can do without unsafe here; an equivalent to csimp would be nice. What if you first register the whole type as noncomputable, then define an equivalence to another type (back and forth with proofs, like Equiv), and then tell the compiler to make these conversation functions casts. Then one can add csimp lemmas for the existing noncomputable functions on the model type. Would that work for your use cases?

  3. Rob23oba commented on Jul 24, 2025

    @Rob23oba
    ContributorAuthor

    For these use cases, mathlib could perhaps define:

    import Mathlib.Logic.Equiv.Defs
    
    def WithReprImpl (α : Sort u) (repr : Sort v) (_eqv : α ≃ repr) := repr
    
    @[override_repr WithReprImpl]
    structure WithRepr (α : Sort u) (repr : Sort v) (eqv : α ≃ repr) where
      value : α
    
    variable {α : Sort u} {r : Sort v} {eqv : α ≃ r}
    
    unsafe def WithRepr.toReprImpl (x : WithRepr α r eqv) : r := unsafeCast x
    unsafe def WithRepr.ofReprImpl (x : r) : WithRepr α r eqv := unsafeCast x
    
    @[implemented_by WithRepr.toReprImpl] def WithRepr.toRepr (x : WithRepr α r eqv) : r := eqv x.value
    @[implemented_by WithRepr.ofReprImpl] def WithRepr.ofRepr (x : r) : WithRepr α r eqv := ⟨eqv.symm x⟩
    
    def WithRepr.mkImpl (x : α) : WithRepr α r eqv := .ofRepr (eqv x)
    def WithRepr.valueImpl (x : WithRepr α r eqv) : α := eqv.symm x.toRepr
    
    @[csimp] theorem WithRepr.mk_eq_mkImpl : @mk = @mkImpl := by funext α r eqv x; simp [mkImpl, ofRepr]
    @[csimp] theorem WithRepr.value_eq_valueImpl : @value = @valueImpl := by funext α r eqv x; simp [valueImpl, toRepr]

    But there are cases where this doesn't work (the examples I've provided above) that need to be supported directly.

  4. datokrat commented on Jul 24, 2025

    @datokrat
    Contributor

    @nomeata's suggestion could be generalized to use cases such as Erased if instead of an equivalence, only an arbitrary map from the kernel's representation to the internal representation is required.

    Worked example for `Erased`
    @[no_repr] -- makes all of the auto-generated functions noncomputable even before @[override_repr]
    structure Erased (α : Type u) where
      out : α
    
    -- In this proposal, @[override_repr] is attached to a function mapping the "kernel type"
    -- to its intended internal representation.
    -- The provided definition can be noncomputable, but the compiler will automatically provide an
    -- implementation that is compiled to the identity.
    @[override_repr]
    noncomputable def Erased.toImpl {α : Type u} (x : Erased α) : PUnit
    
    -- Moreover, a computable map into the reverse direction, also implemented by `id`, will be auto-generated:
    
    -- info: implToQuot : {α : Type u} (x : PUnit) -> (∃ a, toImpl a = x) -> Quot (fun a b => toImpl a = toImpl b)
    #check Erased.implToQuot

    @nomeata's extension would also make it possible to safely create a version of Mathlib's Shrink, which "shrinks" a type into a lower universe given a nonconstructive proof that the type is small enough, where the shrinking and unshrinking operations are both computable (and compiled to id).

  5. Rob23oba commented on Jul 24, 2025

    @Rob23oba
    ContributorAuthor

    Hmm that's also an interesting idea; I guess the idea is similar to the WithRepr except the ofRepr / toRepr part is handled for you (that you then use csimp later to implement constructors and projections). Kinda funny since @zwarich questioned whether csimp should even be supported (I guess the answer is yes?)
    Some questions:

    1. What if you wrote
      def funnyFunction (x : Erased Nat) : List (Erased Nat) := [x]
      in between the no_repr and override_repr? Should this simply be rejected in the sense of "Erased doesn't have a representation"?
    2. What about InfiniteList? There, you can't really write a toImpl function that isn't unsafe (since InfiniteListImpl isn't inhabited).
    3. What about writing @[override_repr] in a different module? Should this be prohibited? (probably)

    Otherwise, this idea is quite nice because you don't need to unsafeCast yourself (and in particular often don't need unsafe at all) and it's simpler to specify the override than with my WithReprImpl above.

  6. zwarich commented on Jul 24, 2025

    @zwarich
    Contributor
    inductive InfiniteListImpl (α : Type u) where
      | cons (x : α) (l : Thunk (InfiniteList α))
    
    @[override_repr InfiniteListImpl]
    structure InfiniteList (α : Type u) where
      get : Nat → α

    This is implicitly assuming that the compiler can't exploit a type (or individual constructor) being uninhabited in its representation choices. Edit: I guess the assumption is technically being made by the unsafe code, not the override_repr itself.

  7. Rob23oba commented on Jul 24, 2025

    @Rob23oba
    ContributorAuthor

    You're right; but Lean doesn't have support for coinductive types and you certainly want to be able to use such a representation. I guess if it would exploit this then either:

    1. Anything hidden behind a Thunk or a function wouldn't be considered
    2. It uses the original "kernel type" (InfiniteList) to make such judgments.
  8. sgraf812 commented on Jul 24, 2025

    @sgraf812
    Member

    FWIW, I think a "safe" implementation could be achieved by axiomatizing a wrapper around Thunk called Later together with the "usual" applicative instance and the Löb induction/guarded fixpoint operator gfix : (Later α -> α) -> α: https://github.com/sgraf812/abs-den.lean/blob/3afc0c5532ef85f5056da8437dfdd30722224937/igdtt/IGDTT/Axioms.lean
    I don't think the compiler would be prone to peeking into the definition of Later as it would be prone to reasoning about Thunk.

    My axiomatization is often enough for simple guarded recursive programs. Plus, you could justify every such axiom by providing a proof in the model of guarded domain theory. (Eliminating the later modality in a finite way is still a safety issue, though; every use of unsafeForce in a function such as Stream.take must be audited.)

    Unfortunately last time I tried I encountered some strange segfaults. Perhaps it's better with the new compiler.

  9. datokrat commented on Jul 24, 2025

    @datokrat
    Contributor

    Responding to #9508 (comment):

    1. What if you wrote def funnyFunction (x : Erased Nat) : List (Erased Nat) := [x]
      in between the no_repr and override_repr? Should this simply be rejected in the sense of "Erased doesn't have a representation"?

    In my opinion yes, because this behavior feels consistent with the fact that I can't use a noncomputable function in computable code even if I later used csimp/implemented_by. I suppose this helps that things are compiled in order.

    1. What about InfiniteList? There, you can't really write a toImpl function that isn't unsafe (since InfiniteListImpl isn't inhabited).

    Sadly, the inductive-based implementation doesn't fit nicely into this approach. However, we can get (at least asymptotically) efficient InfiniteLists with a different implementation:

    an attempt at `InfiniteList`
    @[no_repr]
    structure InfiniteList (α : Type u) where
      get : Nat -> α
    
    -- state machine implementation of `InfiniteList`
    structure InfiniteListImpl (α : Type u) : Type (u + 1) where
      State : Type u
      state : State
      next: State -> State \times α
    
    -- We could make this noncomputable, it is only needed to construct an equivalence relation.
    def InfiniteListImpl.toInfiniteList {α : Type u} (xs : InfiniteListImpl α) : InfiniteList α where
      get n :=
        let rec get' xs n := match n with
          | 0 => (xs.next xs.state).2
          | n + 1 => get' (InfiniteListImpl.mk xs.State (xs.next xs.state).1 xs.next) n
        get' xs n
    
    /-- Two state machines are equivalent if they emit the same elements in order. -/
    def InfiniteListImpl' (α : Type u) : Type (u + 1) := Quot (fun xs ys : InfiniteListImpl α => xs.toInfiniteList = ys.toInfiniteList)
    
    /-- Derives a state machine from the kernel type `InfiniteList α`. -/
    @[override_repr]
    noncomputable def InfiniteList.toImpl {α : Type u} (xs : InfiniteList α) : InfiniteListImpl' α :=
      Quot.mk (InfiniteListImpl.mk (InfiniteList α) xs fun ys => (InfiniteList.mk fun n => ys.get (n + 1), ys.get 0))
    
    -- info:
    -- implToQuot  : {α : Type u} -> (xs : InfiniteListImpl' α) -> (∃ ys, ys.toImpl = xs) ->
    --     Quot (fun xs ys : InfiniteList α => xs.toImpl = ys.toImpl
    #check InfiniteList.implToQuot
    
    -- Now, we can define and `csimp` all the functions we'd like to have on `InfiniteList`.

    Given that override_repr works as proposed, no unsafeCast/implemented_by is necessary at all, so all the "unsafety" is encapsulated in the proposed language feature. I haven't tested whether this comes with a performance penalty, though.

    1. What about writing @[override_repr] in a different module? Should this be prohibited? (probably)

    Yes, I think I'd forbid that. Personally, I even find it questionable that implemented_by works beyond module boundaries because it makes it really hard to find the actual implementation of a type/def.

  10. self-assigned this
    on Jul 25, 2025
  11. Rob23oba commented on Jul 25, 2025

    @Rob23oba
    ContributorAuthor

    Maybe to address 1 and 3 we can use a design similar to computed fields? The with clause currently only supports computed fields but what about:

    structure InfiniteListImpl (α : Type u) : Type (u + 1) where
      State : Type u
      state : State
      next : State → State × α
    
    def InfiniteListImpl.get (x : InfiniteListImpl α) : Nat → α :=
      go x.state
    where
      go (s : x.State) : Nat → α
      | 0 => (x.next s).2
      | k + 1 => go (x.next s).1 k
    
    structure InfiniteList (α : Type u) where
      get : Nat → α
    with
      @[override_repr]
      toImpl : InfiniteList α → Quot (fun a b : InfiniteListImpl α => ∀ n, a.get n = b.get n)
      | ⟨get⟩ => Quot.mk _ (InfiniteListImpl.mk Nat 0 fun val => (val + 1, get val))

    Computed fields need a rework anyways so why not combine it with a similar feature :-)
    (computed fields + override_repr would obviously not be allowed since computed fields already assume something like an override_repr)

  12. nomeata commented on Jul 25, 2025

    @nomeata
    Collaborator

    (Small note for when this discussion eventually converges: we should probably look for a different name for the attribute that does not sounds like you are changing the Repr instance.)

  13. cppio commented on Jul 26, 2025

    @cppio
    Contributor

    I had an idea similar to WithRepr in https://leanprover.zulipchat.com/#narrow/channel/341532-lean4-dev/topic/Proposal.3A.20Customizable.20runtime.20representations/near/525950357 (I called it Encoded). As I mentioned there, you could use an opaque equivalence for unsafe/external representations:

    unsafe structure InfListImpl (α : Type u) where
      hd : α
      tl : Thunk (InfListImpl α)
    
    unsafe def InfListImpl.get (xs : InfListImpl α) : Nat → α
      | 0 => xs.hd
      | n + 1 => xs.tl.get.get n
    
    unsafe def InfListImpl.iter (f : Nat → α) (n := 0) : InfListImpl α :=
      ⟨f n, iter f (n + 1)⟩
    
    unsafe def InfListImpl.Repr (α : Type u) : (β : Type u) × ((Nat → α) ≃ β) := ⟨InfListImpl α, iter, get, lcProof, lcProof⟩
    
    @[implemented_by InfListImpl.Repr]
    opaque InfList.Repr (α : Type u) : (β : Type u) × ((Nat → α) ≃ β) := ⟨Nat → α, id, id, fun _ => rfl, fun _ => rfl⟩

    Similarly, you could use the existing noncomputable equivalence Erased.equiv to implement the better Erased.

    A downside with this approach is that it does not support the case where only one of the constructor and projection is computable, though I'm not sure of a situation where you would want that.

  14. Rob23oba commented on Jul 27, 2025

    @Rob23oba
    ContributorAuthor

    Yeah, I believe something like this could be useful tool but shouldn't be "the main feature". In other words, you should be able to write this (mathlib should probably have this) and also use it to simplify the procedure for other types but other variants should also be allowed.

  15. added
    P-medium
    on Aug 8, 2025
  16. 12 remaining items

  17. digama0 commented on Aug 21, 2025

    @digama0
    Collaborator

    This RFC doesn't mention the prior work #2292, which I think is all that is needed here. To recap that approach:

    • The kernel / logical model doesn't care at all, so there is no new function or otherwise like rep and abs.
    • Instead, there is simply an annotation on the type informing the compiler that the type has an overridden repr, and therefore constructors cannot be elided in certain situations where they are deleted currently (for example, if it's a structure with one field).
    • This doesn't really do anything on its own, but it makes it legal to write implemented_by and/or csimp for:
      • The constructors
      • The projections
      • The casesOn function
      • The recursor

    This is a more low-level approach than def_type_with_rep, but it covers situations which cannot be handled in that way, for example if the underlying repr can't be itself expressed as a lean type (e.g. String, ByteArray, Array, Nat). Indeed I think the best examples of use of @[override_repr] are in lean core itself. You could of course define def_type_with_rep as a macro on top, by defining a wrapper type, and overriding the constructor / projections etc.

  18. datokrat commented on Aug 21, 2025

    @datokrat
    Contributor

    Agreed that it's nice that opaque_repr allows for more use cases theoretically. Having an optional safe wrapper around it would be nice, too.

    However: If opaque_repr simply tells the compiler to make less assumptions, it seems that the compiler would have a hard time to apply optimizations based on the compiled representation, right? In contrast, def_type_with_rep seems to give the compiler enough information about the representation to apply vital optimizations.

    So for example, Erased would technically work with opaque_repr, but the compiler wouldn't be able to realize that its representation does not carry any data. This is only one desirable optimization that we would be miss out. Also, wrapping any type into computable Shrink would prevent the compiler from unboxing (I expect the impact from that to be severe if we decide to make the iterator library more universe-polymorphic using Shrink).

    All in all, I would say that although these approaches look similar, they are quite orthogonal when it comes to performance-critical use cases: opaque_repr would allow for highly efficient Lean-external representations in a safe way while def_type_with_rep would allow for Lean-internal representations that lend themselves better to important compiler optimizations.

  19. digama0 commented on Aug 21, 2025

    @digama0
    Collaborator

    I don't think Erased is / should be a use of @[override_repr]. That should be a separate thing (and def_type_with_rep could also hook into that). Erasure (and ABI type more generally) works at the type level, representations work at the function level (at least currently).

    Shrink would work just fine with this.

    However: If opaque_repr simply tells the compiler to make less assumptions, it seems that the compiler would have a hard time to apply optimizations based on the compiled representation, right?

    It can still inline the projection function once it's been overridden, although this may be at odds with whatever typing system the compiler is working with.

  20. datokrat commented on Aug 21, 2025

    @datokrat
    Contributor

    It can still inline the projection function once it's been overridden, although this may be at odds with whatever typing system the compiler is working with.

    It can, but would the compiler be able to apply an unboxing optimization in a case like this (which seems to me like a perfectly plausible situation)?

    -- let's assume `g` is not getting inlined
    def g (x : Shrink (α × β)) : Shrink (α × β) :=
      -- do something
    
    def f (x : Shrink (α × β)) : Nat :=
      -- do some stuff using the two projections of `(unshrink x)`
      f (g x) -- some recursive call

    I believe: No. At least I do not see how inlining the projection functions or the constructor would help us here. The compiler effectively has no explicit type information on which to rely and can only use whatever it can infer from a dataflow analysis; and I'm not yet convinced that would be equally efficient and/or effective as without Shrink (I'm assuming that the compiler generally supports the unboxing optimizations @zwarich is working on).

    And I do think that the ABI of f should be the same as def f (x : α × β) : Nat. How else would the snippet above be unboxable? But perhaps I'm asking for too much?

  21. digama0 commented on Aug 21, 2025

    @digama0
    Collaborator

    That is, again, about ABI type, which I would say is a different (orthogonal) feature. You would need some way of talking about ABI types, or alternatively, provide only a few presets: transparent (use the ABI of a field), erased, opaque. But importantly, ABI types are a weaker type system from lean, so there need not be actual formal agreement between the ABI type and the effective type from the overridden projection and other functions. That's why I'm suspicious of an approach where the target type is an actual lean type and you can write actual lean functions implementing everything, because this doesn't really make sense for e.g. a type wrapping an external handle from C.

  22. Rob23oba commented on Aug 21, 2025

    @Rob23oba
    ContributorAuthor

    This RFC doesn't mention the prior work #2292, which I think is all that is needed here.

    You're right, I forgot to mention that here; this RFC generally expands upon #2292 with the following points:

    • @[opaque_repr] doesn't work well with unboxing. Because it is opaque, the compiler can't do anything with it and needs to assign the "any" type (tobj). However, when we're going to get unboxed types, we'd want Shrink (UInt8 × UInt16) to be represented the same way as UInt8 × UInt16 and not as tobj (the same argument applies to Shrink UInt8 etc. at the current moment)
    • @[override_repr] (or @[opaque_repr] or whatever) should make the constructors and projections noncomputable. When the representation is overridden / no longer the default one, the usual implementation of projections and constructors doesn't make sense anymore.
    • @[implemented_by] and all variants should be forbidden on projections and constructors unless @[override_repr] (or @[opaque_repr] or whatever) is present

    That's why I'm suspicious of an approach where the target type is an actual lean type

    What's the problem? I'd expect the compiler to just pseudo-delta-reduce to the overridden type while it converts regular Lean types into compiler types.

  23. digama0 commented on Aug 23, 2025

    @digama0
    Collaborator

    That's why I'm suspicious of an approach where the target type is an actual lean type

    What's the problem? I'd expect the compiler to just pseudo-delta-reduce to the overridden type while it converts regular Lean types into compiler types.

    The problem is that it's insufficiently general. I think it would be nice to have good specialization to the case where the replacement type is another lean type, e.g. with special syntax and/or enough low-level annotations that something like def_type_with_rep can be written in user code, but there are cases that are not covered by this, and I would want to ensure that these work too.

  24. nomeata commented on Aug 23, 2025

    @nomeata
    Collaborator

    One reason I'm hesitant about the more basic approach of an attribute to tell the compiler to not assume anything about the type, and then separate implemented_by on the constructor and cases is: what happens with definitions that are compiled between the attribute and the implemented_by? Also, do we get more possible combinations of which definition is overriden where?

    By exposing a slightly higher level primitive that does it all in one go with clear semantics, I expect less complexity, more reliability and more wiggle room for the compiler to optimize.

  25. Rob23oba commented on Aug 23, 2025

    @Rob23oba
    ContributorAuthor

    but there are cases that are not covered by this, and I would want to ensure that these work too.

    I'm not opposed to having something like @[opaque_repr] in addition to @[override_repr]/def_type_with_rep/whatever (which we might put on UInt8, etc.) but the point is that we also should allow using another Lean type as replacement (I could imagine them being frontend to the same kind of thing in the compiler, as a ParametricAttribute (Option Expr) or the EnvExtension equivalent or whatever)

  26. cppio commented on Aug 23, 2025

    @cppio
    Contributor

    I don't understand how def_type_with_rep is insufficiently general. Why can't Array can't be implemented like:

    private opaque ArrayImpl (α : Type u) : Type u
    
    @[extern "lean_array_to_list"]
    private unsafe def ArrayImpl.toList : ArrayImpl α → List α := toList
    
    @[extern "lean_array_mk"]
    private unsafe def ArrayImpl.mk : List α → ArrayImpl α := mk
    
    def_type_with_rep Array (α : Type u) where
      absType := List α
      repType := ArrayImpl α
      to := ArrayImpl.mk
      from := ArrayImpl.toList
  27. zwarich commented on Sep 5, 2025

    @zwarich
    Contributor

    One reason I'm hesitant about the more basic approach of an attribute to tell the compiler to not assume anything about the type, and then separate implemented_by on the constructor and cases is: what happens with definitions that are compiled between the attribute and the implemented_by? Also, do we get more possible combinations of which definition is overriden where?

    By exposing a slightly higher level primitive that does it all in one go with clear semantics, I expect less complexity, more reliability and more wiggle room for the compiler to optimize.

    The other more general thing here is that it's usually a bad idea to add a feature defined by telling the compiler not to do something. Inevitably, the compiler will later do something else that also happens to be unacceptable to the user who requested the earlier feature, yet seems completely orthogonal to the compiler implementor.

  28. zwarich commented on Sep 5, 2025

    @zwarich
    Contributor

    I don't understand how def_type_with_rep is insufficiently general. Why can't Array can't be implemented like:

    This actually raises an interesting question (related to the points @Rob23oba made earlier). Can the compiler assume that every opaque type declared like this is actually a ref and can't ever be a scalar in a tagged pointer? This would mean that types like Nat/Int would remain compiler builtins forever, but the rest of our builtins could use the same mechanism as users.

  29. cppio commented on Sep 5, 2025

    @cppio
    Contributor

    Can the compiler assume that every opaque type declared like this is actually a ref and can't ever be a scalar in a tagged pointer?

    Perhaps this should be controlled by an attribute?

  30. Equilibris commented on Sep 28, 2025

    @Equilibris

    Hi this would be super useful to have. Do we know when this might be added?

  31. Rob23oba commented on Aug 1, 2026

    @Rob23oba
    ContributorAuthor

    I'd like to propose a new idea: Essentially, we let the compiler have its own notion of inductives that can be distinct from what the kernel thinks of as inductives. You'd then be able to control these "compiler inductives" using two new attributes:

    • @[override_runtime_type ident?] on an inductive prevents the compiler from seeing it as an inductive type. You can also optionally provide a specific compiler type that it should instead have. This is essentially a generalized @[opaque_repr] that allows you to specify the compiler type (the type parameter is mostly useful for implementing the runtime built-in types).
    • @[compiled_cases] on an eliminator that's shaped like a casesOn causes the compiler to make that the "canonical casesOn" of the inductive, that is, it sets the constructors of the inductive based off of the eliminator (all from the compiler's perspective). It also checks that the eliminator laws hold using rfl or simp (maybe simp is not the best option though, not sure).

    The reason I think this is such a nice idea is that we can mostly keep the compiler code the same except that the compiler now e.g. uses its own getConstInfoCtorOverride instead of getConstInfoCtor (the reality is obviously a bit more complicated but this is the gist). Also I believe it can handle everything we've talked about here, including coinductives, in a safe way; I have a test here that shows how this can work.

    I have a prototype for this at #14626; don't expect this to get merged any time soon though. But, anyone curious can try out the PR toolchain leanprover/lean4-pr-releases:pr-release-14626.
    Anyways, I'd like to know if that sounds like a good idea and if there are maybe some problems with this idea that would make it unsafe; I tried to add enough checks including proving the eliminator laws to make sure this is safe (in the same way as csimp) but I could well have missed something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P-mediumRFC

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions