-
Notifications
You must be signed in to change notification settings - Fork 102
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
Add NonemptyList #16
Add NonemptyList #16
Conversation
What's the application? I'm basically neutral on adding this, but it would be nice to have some examples of code that already depends on it, in keeping with std4's trend of taking data structures from other libraries that have already been using the type in practice. Regarding |
I'm using it in my SML formalization; the syntax has a couple places with syntactic non-empty restrictions (for instance, RE: the |
A similar type in Mathlib 3: https://leanprover-community.github.io/mathlib_docs/find/free_semigroup/ |
I've used this under the friendlier(?) name Have you considered making the type an abbreviation for abbrev List1 (α : Type _) := α × List α
def List1.toList {α} : List1 α → List α
| (x, xs) => x :: xs
example (x : Nat) (xs : List Nat) : List1.toList (x, xs) = x :: xs := rfl |
I'm not sure about programming but abbreviations are bad for reasoning: e.g., you can't have custom instances. If you use a semireducible @[simp]
theorem Prod.map_fst (f : α → β) (g : γ → δ) (x : α × γ) :
(Prod.map f g x).fst = f x.fst :=
rfl to BTW, your example can be written with a custom structure without loss of readability: structure List1 (α : Type _) where
hd : α
tl : List α
def List1.toList {α} : List1 α → List α
| ⟨x, xs⟩ => x :: xs
example (x : Nat) (xs : List Nat) : List1.toList ⟨x, xs⟩ = x :: xs := rfl |
Yes, absolutely. Named structures are often better. The basic issue is that there are two different ways to view this type
I think it's worth looking back at @leodemoura's idea to implement "type views" as briefly illustrated at: https://github.com/leanprover/lean4/blob/1a6663a41baf95cb995b79cd8a466ab81e230b35/tests/lean/run/posView.lean Since forward and backward translations are O(1) this is probably worth while! |
I lean towards not including this in Std for now, but I'll note that I recently found myself using the |
Closing since there wasn't activity and this is currently more of a stub. Please reopen if needed. |
Sort of a stub at the moment, but a useful type to have rather than inlining the head/tail at use site.
I suspect we'd want to replicate many (most?) functions on
List
to have equivalents here, but my current use case just pattern matches the head/tail out so I don't know yet what functions will be useful.