I trying to use coq as a programming language with dependent type. I created the following small program:
Inductive Good : list nat -> Set :=
| GoodNonEmpty : forall h t, Good (h :: t).
Definition get_first(l : list nat)(good : Good l) : nat :=
match l with
| h :: t => h
| nil =>
match good with
end
end.
I defined a type for non empty list and create a function which gets the first element of such a list provided there’s a proof that it’s not empty. I handle well the case where head items consists of two items, but I can’t handle the impossible case of empty list. How can I do this in coq?
One way to do it that is simpler than your try is:
Here is a way to do it in the way you wanted to do it. You’ll notice it is very verbose to prove that “Good nil” does not exist, inlined.
You can surely define some of that outside and reuse it. I am not aware of the best practices though. Maybe someone can come with a shorter way to do the same thing.
EDIT:
By the way, you can get to pretty much the same result, in a much easier way, in proof mode:
You can then:
To see how Coq defines it. However, for more involved things, you might be better off following what gdsfhl from the #coq IRC channel proposed as a solution:
http://paste.in.ua/4782/
You can see that he uses the
refinetactic to provide part of the skeleton of the term to write, and defer the missing proofs.