Zulip Chat Archive
Stream: new members
Topic: cannot get hypothesis from if then
Jem Bishop (Dec 03 2020 at 18:03):
Hi I am trying to define a from_list function for vectors. What I have is
def from_list {α : Type} {n : ℕ} : list α → option (vector α n)
| l := if l.length = n then some ⟨l, by sorry⟩ else none
within the 'by sorry' i need the hypothesis that l.length=n
, which is obviously true due to the if but does not appear in the proof checker.
How would I write this correctly? thanks
Adam Topaz (Dec 03 2020 at 18:03):
The length of the vector is built into the type itself.
Rob Lewis (Dec 03 2020 at 18:04):
if h : l.length = n
Jem Bishop (Dec 03 2020 at 18:05):
@Rob Lewis Thanks! no idea you could do that!
Adam Topaz (Dec 03 2020 at 18:05):
Oh I see what's happening.
Adam Topaz (Dec 03 2020 at 18:07):
Is it clear that this function will be useful? How would you actually tell lean about the length of a given list?
Adam Topaz (Dec 03 2020 at 18:07):
def from_list {α : Type} : Π (l : list α), vector α (l.length) := λ l, ⟨l,rfl⟩
Jem Bishop (Dec 03 2020 at 18:08):
i just have it as a constant
Jem Bishop (Dec 03 2020 at 18:09):
my use case is parsing some fixed width string to a vector of characters
Adam Topaz (Dec 03 2020 at 18:12):
I see.
Yakov Pechersky (Dec 03 2020 at 18:16):
One can supply the proof of a length of a list for an explicitly given list just by dec_trivial
Adam Topaz (Dec 03 2020 at 18:17):
Yeah... the code with if then else
works because it's decidable :)
Being a mathematician, I still don't think of lean as a programming language
Last updated: Dec 20 2023 at 11:08 UTC