Zulip Chat Archive
Stream: new members
Topic: square brackets in def
Alex Zhang (Jun 03 2021 at 10:45):
open list
#check @head
gives me Π {α : Type u_1} [_inst_1 : inhabited α], list α → α
.
How should I interpret the [] here and what is the function of [] here?
Mario Carneiro (Jun 03 2021 at 10:48):
It is a typeclass argument. It links in to a system for automatically generating such proofs called typeclass inference
Mario Carneiro (Jun 03 2021 at 10:48):
Here inhabited A
means A
has a distinguished/default/arbitrary element, and list.head
will use that element when the list is empty
Mario Carneiro (Jun 03 2021 at 10:49):
so when you write list.head ([] : list nat)
you will get 0
because 0
is the default element of nat
Mario Carneiro (Jun 03 2021 at 10:50):
which is supplied by the proof that nat is inhabited, docs#nat.inhabited
Mario Carneiro (Jun 03 2021 at 10:51):
Since empty
is not an inhabited type, however, list.head ([] : list empty)
is an error, and it has to be because otherwise it would have type empty
which would allow us to prove false
Mario Carneiro (Jun 03 2021 at 10:52):
Unlike haskell, lean has no ability to "throw an error" instead of evaluating to a value, so this function looks a bit different from haskell's List.head
function
Alex Zhang (Jun 03 2021 at 11:27):
So is _inst_1
in [_inst_1 : inhabited α]
nothing more than an identifier?
Eric Wieser (Jun 03 2021 at 11:28):
Yes, Π {α : Type u_1} [inhabited α], list α → α
means the same thing
Bryan Gin-ge Chen (Jun 03 2021 at 13:07):
(Obligatory #tpil chapter link: https://leanprover.github.io/theorem_proving_in_lean/type_classes.html)
Last updated: Dec 20 2023 at 11:08 UTC