Zulip Chat Archive

Stream: new members

Topic: Linked list const constructor on the right


C7X (May 24 2025 at 00:57):

Hi! For a project I'm working on I would like to have a type that's like List Nat, but with a constructor for appending a natural on the right instead of the left. I guess I could make an inductive type myself, but I also want to use all of Mathlib's functions on lists and lemmas about lists. I'd like to have appending to the right as a constructor, so that proofs by cases will work. Is there a canonical way to do this?

Aaron Liu (May 24 2025 at 00:59):

Why can't you just use List Nat and read all the list right-to-left?

C7X (May 24 2025 at 01:02):

I currently have code that's based on the programmer writing all List literals in reverse and having to swap left and right in their head, but I think it might be more convenient to have code which doesn't require accounting for left-to-right reflection in the programmer's head

Kevin Buzzard (May 24 2025 at 07:54):

Why don't you write your own custom recursor for lists and instead of cases do induction x using List.myrecursor?

Kevin Buzzard (May 24 2025 at 07:56):

There might even be an option to change the default recursor for an inductive type but I can't remember whether you'd need a core change to access this functionality for List or whether it can be done locally in a file

Aaron Liu (May 24 2025 at 10:28):

I think it can be done locally with @[cases_eliminator] and @[induction_eliminator]

Eric Wieser (May 24 2025 at 14:11):

Or if this is really for programmers, you could use Array and push

C7X (May 24 2025 at 20:02):

Thanks, I can try this! I'm relatively new to Lean so I can't guarantee that I will be able to write a recursor yet, but I'll see what I can do!

Kevin Buzzard (May 24 2025 at 20:36):

You can practice by writing the induction principle first:

variable {α : Type u}

example
    (motive : List α  Prop)
    (hnil : motive [])
    (hsnoc :  (init : List α) (last : α), motive init  motive (init ++ [last])) :
     l : List α, motive l := by
  sorry

Eric Wieser (May 24 2025 at 22:02):

It already exists as


Last updated: Dec 20 2025 at 21:32 UTC