I'm trying to do something in Coq similar to this liquid Haskell trick, which defines a partial function but proves it's actually total:
{-@ head :: {xs:[a] | len xs > 0} -> a @-}
head (x:xs) = x
Here is my first attempt, but Coq doesn't like it ("Casts are not supported in this pattern."):
Definition safeHead {A : Type} (xs : list A) (nonempty : xs <> nil) : A.
Proof.
refine (fun {A : type} (xs : list A) (pf : xs <> nil) =>
match xs with
| x : xs => x
| nil => _
end).
(* Some tactic here to prove the hole from nonempty *)
Defined.
I also tried adapting option 1 from this blog post, but that fails with the same error:
Definition safeHead {A : Type} (xs : list A) (not_nil : xs <> nil) : A
match xs return _ with
| x : xs => fun _ => x
| nil => fun is_nil => False_rect _ (not_nil is_nil)
end eq_refl.
Is there a way to get this to work in coq? I'd also love to understand the error message; what is the 'cast' and in which pattern is it failing?