I am trying to use "convoy pattern" to preserve dependency between 3 variables (two parameters and return value):
Require Import Vector.
(* "sparse" vector type *)
Notation svector A n := (Vector.t (option A) n).
Fixpoint svector_is_dense {A} {n} (v:svector A n) : Prop :=
match v with
| Vector.nil => True
| Vector.cons (None) _ _ => False
| Vector.cons (Some _) _ xs => svector_is_dense xs
end.
Lemma svector_tl_dense {A} {n} {v: svector A (S n)}:
svector_is_dense v -> svector_is_dense (Vector.tl v).
Admitted.
Lemma svector_hd {A} {n} (v:svector A (S n)): svector_is_dense v -> A.
Admitted.
Fixpoint vector_from_svector {A} {n} {v:svector A n} (D:svector_is_dense v): Vector.t A n :=
match n return (svector A n) -> (svector_is_dense v) -> (Vector.t A n) with
| O => fun _ _ => @Vector.nil A
| (S p) => fun v0 D0 => Vector.cons
(svector_hd v0 D0)
(vector_from_svector (Vector.tl v) (svector_tl_dense D))
end v D.
The problem arises in last definition. Any suggestions why it is not working?
You got it almost right. The issue is in your
return
clause, which is non-dependent. What you need isso that
D0
is not of typesvector_is_dense v
as it would be in your case, butsvector_is_dense v0
.By the way, in the second constructor, I guess you meant
Vector.tl v0
andsvector_tl_dense D0
. Here is the full term I wrote (don't mind the additionnal_
incons
, I don't have implicits activated):