1 (*********************************************************************************************************************************)
2 (* General: general data structures *)
3 (*********************************************************************************************************************************)
5 Require Import Coq.Unicode.Utf8.
6 Require Import Coq.Classes.RelationClasses.
7 Require Import Coq.Classes.Morphisms.
8 Require Import Coq.Setoids.Setoid.
9 Require Import Coq.Strings.String.
11 Require Import Coq.Lists.List.
12 Require Import Preamble.
13 Generalizable All Variables.
16 Definition EqDecider T := forall (n1 n2:T), sumbool (n1=n2) (not (n1=n2)).
17 Class EqDecidable (T:Type) :=
19 ; eqd_dec : forall v1 v2:T, sumbool (v1=v2) (not (v1=v2))
21 Coercion eqd_type : EqDecidable >-> Sortclass.
24 (*******************************************************************************)
27 Inductive Tree (a:Type) : Type :=
28 | T_Leaf : a -> Tree a
29 | T_Branch : Tree a -> Tree a -> Tree a.
30 Implicit Arguments T_Leaf [ a ].
31 Implicit Arguments T_Branch [ a ].
33 Notation "a ,, b" := (@T_Branch _ a b) : tree_scope.
35 (* tree-of-option-of-X comes up a lot, so we give it special notations *)
36 Notation "[ q ]" := (@T_Leaf (option _) (Some q)) : tree_scope.
37 Notation "[ ]" := (@T_Leaf (option _) None) : tree_scope.
38 Notation "[]" := (@T_Leaf (option _) None) : tree_scope.
40 Open Scope tree_scope.
42 Fixpoint mapTree {a b:Type}(f:a->b)(t:@Tree a) : @Tree b :=
44 | T_Leaf x => T_Leaf (f x)
45 | T_Branch l r => T_Branch (mapTree f l) (mapTree f r)
47 Fixpoint mapOptionTree {a b:Type}(f:a->b)(t:@Tree ??a) : @Tree ??b :=
49 | T_Leaf None => T_Leaf None
50 | T_Leaf (Some x) => T_Leaf (Some (f x))
51 | T_Branch l r => T_Branch (mapOptionTree f l) (mapOptionTree f r)
53 Fixpoint mapTreeAndFlatten {a b:Type}(f:a->@Tree b)(t:@Tree a) : @Tree b :=
56 | T_Branch l r => T_Branch (mapTreeAndFlatten f l) (mapTreeAndFlatten f r)
58 Fixpoint mapOptionTreeAndFlatten {a b:Type}(f:a->@Tree ??b)(t:@Tree ??a) : @Tree ??b :=
61 | T_Leaf (Some x) => f x
62 | T_Branch l r => T_Branch (mapOptionTreeAndFlatten f l) (mapOptionTreeAndFlatten f r)
64 Fixpoint treeReduce {T:Type}{R:Type}(mapLeaf:T->R)(mergeBranches:R->R->R) (t:Tree T) :=
66 | T_Leaf x => mapLeaf x
67 | T_Branch y z => mergeBranches (treeReduce mapLeaf mergeBranches y) (treeReduce mapLeaf mergeBranches z)
69 Definition treeDecomposition {D T:Type} (mapLeaf:T->D) (mergeBranches:D->D->D) :=
70 forall d:D, { tt:Tree T & d = treeReduce mapLeaf mergeBranches tt }.
73 forall {Q}(t1 t2:Tree ??Q),
74 (forall q1 q2:Q, sumbool (q1=q2) (not (q1=q2))) ->
75 sumbool (t1=t2) (not (t1=t2)).
80 destruct a; destruct t2.
85 right; unfold not; intro; apply H. inversion H0; subst. auto.
86 right. unfold not; intro; inversion H.
87 right. unfold not; intro; inversion H.
89 right. unfold not; intro; inversion H.
91 right. unfold not; intro; inversion H.
94 right. unfold not; intro; inversion H.
95 set (IHt1_1 t2_1 X) as X1.
96 set (IHt1_2 t2_2 X) as X2.
97 destruct X1; destruct X2; subst.
116 Lemma mapOptionTree_compose : forall A B C (f:A->B)(g:B->C)(l:Tree ??A),
117 (mapOptionTree (g ○ f) l) = (mapOptionTree g (mapOptionTree f l)).
130 (*******************************************************************************)
133 Notation "a :: b" := (cons a b) : list_scope.
134 Open Scope list_scope.
135 Fixpoint leaves {a:Type}(t:Tree (option a)) : list a :=
137 | (T_Leaf l) => match l with
141 | (T_Branch l r) => app (leaves l) (leaves r)
143 (* weak inverse of "leaves" *)
144 Fixpoint unleaves {A:Type}(l:list A) : Tree (option A) :=
147 | (a::b) => [a],,(unleaves b)
150 (* a map over a list and the conjunction of the results *)
151 Fixpoint mapProp {A:Type} (f:A->Prop) (l:list A) : Prop :=
154 | (a::al) => f a /\ mapProp f al
157 Lemma map_id : forall A (l:list A), (map (fun x:A => x) l) = l.
164 Lemma map_app : forall `(f:A->B) l l', map f (app l l') = app (map f l) (map f l').
171 Lemma map_compose : forall A B C (f:A->B)(g:B->C)(l:list A),
172 (map (g ○ f) l) = (map g (map f l)).
180 Lemma list_cannot_be_longer_than_itself : forall `(a:A)(b:list A), b = (a::b) -> False.
184 inversion H. apply IHb in H2.
187 Lemma list_cannot_be_longer_than_itself' : forall A (b:list A) (a c:A), b = (a::c::b) -> False.
196 Lemma mapOptionTree_on_nil : forall `(f:A->B) h, [] = mapOptionTree f h -> h=[].
199 destruct o. inversion H.
204 Lemma mapOptionTree_comp a b c (f:a->b) (g:b->c) q : (mapOptionTree g (mapOptionTree f q)) = mapOptionTree (g ○ f) q.
215 (* handy facts: map preserves the length of a list *)
216 Lemma map_on_nil : forall A B (s1:list A) (f:A->B), nil = map f s1 -> s1=nil.
225 Lemma map_on_singleton : forall A B (f:A->B) x (s1:list A), (cons x nil) = map f s1 -> { y : A & s1=(cons y nil) & (f y)=x }.
228 simpl in H; assert False. inversion H. inversion H0.
234 inversion H. apply map_on_nil in H2. auto.
238 inversion H. apply map_on_nil in H2. auto.
244 Lemma map_on_doubleton : forall A B (f:A->B) x y (s1:list A), ((x::y::nil) = map f s1) ->
245 { z : A*A & s1=((fst z)::(snd z)::nil) & (f (fst z))=x /\ (f (snd z))=y }.
259 Lemma mapTree_compose : forall A B C (f:A->B)(g:B->C)(l:Tree A),
260 (mapTree (g ○ f) l) = (mapTree g (mapTree f l)).
269 Lemma lmap_compose : forall A B C (f:A->B)(g:B->C)(l:list A),
270 (map (g ○ f) l) = (map g (map f l)).
279 (* sends a::b::c::nil to [[[[],,c],,b],,a] *)
280 Fixpoint unleaves' {A:Type}(l:list A) : Tree (option A) :=
283 | (a::b) => (unleaves' b),,[a]
286 (* sends a::b::c::nil to [[[[],,c],,b],,a] *)
287 Fixpoint unleaves'' {A:Type}(l:list ??A) : Tree ??A :=
290 | (a::b) => (unleaves'' b),,(T_Leaf a)
293 Fixpoint filter {T:Type}(l:list ??T) : list T :=
296 | (None::b) => filter b
297 | ((Some x)::b) => x::(filter b)
300 Inductive distinct {T:Type} : list T -> Prop :=
301 | distinct_nil : distinct nil
302 | distinct_cons : forall a ax, (@In _ a ax -> False) -> distinct ax -> distinct (a::ax).
304 Lemma map_preserves_length {A}{B}(f:A->B)(l:list A) : (length l) = (length (map f l)).
310 (* decidable quality on a list of elements which have decidable equality *)
311 Definition list_eq_dec : forall {T:Type}(l1 l2:list T)(dec:forall t1 t2:T, sumbool (eq t1 t2) (not (eq t1 t2))),
312 sumbool (eq l1 l2) (not (eq l1 l2)).
315 induction l1; intros.
318 right; intro H; inversion H.
319 destruct l2 as [| b l2].
320 right; intro H; inversion H.
321 set (IHl1 l2 dec) as eqx.
324 set (dec a b) as eqy.
328 right. intro. inversion H. subst. apply n. auto.
336 Instance EqDecidableList {T:Type}(eqd:EqDecidable T) : EqDecidable (list T).
337 apply Build_EqDecidable.
343 (*******************************************************************************)
344 (* Length-Indexed Lists *)
346 Inductive vec (A:Type) : nat -> Type :=
348 | vec_cons : forall n, A -> vec A n -> vec A (S n).
350 Fixpoint vec2list {n:nat}{t:Type}(v:vec t n) : list t :=
353 | vec_cons n a va => a::(vec2list va)
356 Require Import Omega.
357 Definition vec_get : forall {T:Type}{l:nat}(v:vec T l)(n:nat)(pf:lt n l), T.
372 Definition vec_zip {n:nat}{A B:Type}(va:vec A n)(vb:vec B n) : vec (A*B) n.
377 apply vec_cons; auto.
380 Definition vec_map {n:nat}{A B:Type}(f:A->B)(v:vec A n) : vec B n.
384 apply vec_cons; auto.
387 Fixpoint vec_In {A:Type} {n:nat} (a:A) (l:vec A n) : Prop :=
390 | vec_cons _ n m => (n = a) \/ vec_In a m
392 Implicit Arguments vec_nil [ A ].
393 Implicit Arguments vec_cons [ A n ].
395 Definition append_vec {n:nat}{T:Type}(v:vec T n)(t:T) : vec T (S n).
397 apply (vec_cons t vec_nil).
398 apply vec_cons; auto.
401 Definition list2vec {T:Type}(l:list T) : vec T (length l).
404 apply vec_cons; auto.
407 Definition vec_head {n:nat}{T}(v:vec T (S n)) : T.
410 Definition vec_tail {n:nat}{T}(v:vec T (S n)) : vec T n.
414 Lemma vec_chop {T}{l1 l2:list T}{Q}(v:vec Q (length (app l1 l2))) : vec Q (length l1).
419 inversion v; subst; auto.
421 inversion v; subst; auto.
424 Lemma vec_chop' {T}{l1 l2:list T}{Q}(v:vec Q (length (app l1 l2))) : vec Q (length l2).
428 apply IHl1; clear IHl1.
429 inversion v; subst; auto.
432 Lemma vec2list_len {T}{n}(v:vec T n) : length (vec2list v) = n.
438 Lemma vec2list_map_list2vec {A B}{n}(f:A->B)(v:vec A n) : map f (vec2list v) = vec2list (vec_map f v).
444 Lemma vec2list_list2vec {A}(v:list A) : vec2list (list2vec v) = v.
446 set (vec2list (list2vec (a :: v))) as q.
454 Notation "a ::: b" := (@vec_cons _ _ a b) (at level 20).
458 (*******************************************************************************)
461 (* a ShapedTree is a tree indexed by the shape (but not the leaf values) of another tree; isomorphic to (ITree (fun _ => Q)) *)
462 Inductive ShapedTree {T:Type}(Q:Type) : Tree ??T -> Type :=
463 | st_nil : @ShapedTree T Q []
464 | st_leaf : forall {t}, Q -> @ShapedTree T Q [t]
465 | st_branch : forall {t1}{t2}, @ShapedTree T Q t1 -> @ShapedTree T Q t2 -> @ShapedTree T Q (t1,,t2).
467 Fixpoint unshape {T:Type}{Q:Type}{idx:Tree ??T}(st:@ShapedTree T Q idx) : Tree ??Q :=
471 | st_branch _ _ b1 b2 => (unshape b1),,(unshape b2)
474 Definition mapShapedTree {T}{idx:Tree ??T}{V}{Q}(f:V->Q)(st:ShapedTree V idx) : ShapedTree Q idx.
477 apply st_leaf. apply f. apply q.
478 apply st_branch; auto.
481 Definition zip_shapedTrees {T:Type}{Q1 Q2:Type}{idx:Tree ??T}
482 (st1:ShapedTree Q1 idx)(st2:ShapedTree Q2 idx) : ShapedTree (Q1*Q2) idx.
490 apply st_branch; auto.
491 inversion st1; subst; apply IHidx1; auto.
492 inversion st2; subst; auto.
493 inversion st2; subst; apply IHidx2; auto.
494 inversion st1; subst; auto.
497 Definition build_shapedTree {T:Type}(idx:Tree ??T){Q:Type}(f:T->Q) : ShapedTree Q idx.
502 apply st_branch; auto.
505 Lemma unshape_map : forall {Q}{b}(f:Q->b){T}{idx:Tree ??T}(t:ShapedTree Q idx),
506 mapOptionTree f (unshape t) = unshape (mapShapedTree f t).
518 (*******************************************************************************)
519 (* Type-Indexed Lists *)
521 (* an indexed list *)
522 Inductive IList (I:Type)(F:I->Type) : list I -> Type :=
523 | INil : IList I F nil
524 | ICons : forall i is, F i -> IList I F is -> IList I F (i::is).
525 Implicit Arguments INil [ I F ].
526 Implicit Arguments ICons [ I F ].
528 (* a tree indexed by a (Tree (option X)) *)
529 Inductive ITree (I:Type)(F:I->Type) : Tree ??I -> Type :=
530 | INone : ITree I F []
531 | ILeaf : forall i: I, F i -> ITree I F [i]
532 | IBranch : forall it1 it2:Tree ??I, ITree I F it1 -> ITree I F it2 -> ITree I F (it1,,it2).
533 Implicit Arguments INil [ I F ].
534 Implicit Arguments ILeaf [ I F ].
535 Implicit Arguments IBranch [ I F ].
540 (*******************************************************************************)
541 (* Extensional equality on functions *)
543 Definition extensionality := fun (t1 t2:Type) => (fun (f:t1->t2) g => forall x:t1, (f x)=(g x)).
544 Hint Transparent extensionality.
545 Instance extensionality_Equivalence : forall t1 t2, Equivalence (extensionality t1 t2).
546 intros; apply Build_Equivalence;
547 intros; compute; intros; auto.
548 rewrite H; rewrite H0; auto.
550 Add Parametric Morphism (A B C:Type) : (fun f g => g ○ f)
551 with signature (extensionality A B ==> extensionality B C ==> extensionality A C) as parametric_morphism_extensionality.
552 unfold extensionality; intros; rewrite (H x1); rewrite (H0 (y x1)); auto.
554 Lemma extensionality_composes : forall t1 t2 t3 (f f':t1->t2) (g g':t2->t3),
555 (extensionality _ _ f f') ->
556 (extensionality _ _ g g') ->
557 (extensionality _ _ (g ○ f) (g' ○ f')).
559 unfold extensionality.
560 unfold extensionality in H.
561 unfold extensionality in H0.
570 Definition map2 {A}{B}(f:A->B)(t:A*A) : (B*B) := ((f (fst t)), (f (snd t))).
574 (* the Error monad *)
575 Inductive OrError (T:Type) :=
576 | Error : forall error_message:string, OrError T
577 | OK : T -> OrError T.
578 Notation "??? T" := (OrError T) (at level 10).
579 Implicit Arguments Error [T].
580 Implicit Arguments OK [T].
582 Definition orErrorBind {T:Type} (oe:OrError T) {Q:Type} (f:T -> OrError Q) :=
587 Notation "a >>= b" := (@orErrorBind _ a _ b) (at level 20).
589 Inductive Indexed {T:Type}(f:T -> Type) : ???T -> Type :=
590 | Indexed_Error : forall error_message:string, Indexed f (Error error_message)
591 | Indexed_OK : forall t, f t -> Indexed f (OK t)
595 Require Import Coq.Arith.EqNat.
596 Instance EqDecidableNat : EqDecidable nat.
597 apply Build_EqDecidable.
602 (* for a type with decidable equality, we can maintain lists of distinct elements *)
603 Section DistinctList.
604 Context `{V:EqDecidable}.
606 Fixpoint addToDistinctList (cv:V)(cvl:list V) :=
609 | cv'::cvl' => if eqd_dec cv cv' then cvl' else cv'::(addToDistinctList cv cvl')
612 Fixpoint removeFromDistinctList (cv:V)(cvl:list V) :=
615 | cv'::cvl' => if eqd_dec cv cv' then removeFromDistinctList cv cvl' else cv'::(removeFromDistinctList cv cvl')
618 Fixpoint removeFromDistinctList' (cvrem:list V)(cvl:list V) :=
621 | rem::cvrem' => removeFromDistinctList rem (removeFromDistinctList' cvrem' cvl)
624 Fixpoint mergeDistinctLists (cvl1:list V)(cvl2:list V) :=
627 | cv'::cvl' => mergeDistinctLists cvl' (addToDistinctList cv' cvl2)
631 Lemma list2vecOrFail {T}(l:list T)(n:nat)(error_message:nat->nat->string) : ???(vec T n).
632 set (list2vec l) as v.
633 destruct (eqd_dec (length l) n).
634 rewrite e in v; apply OK; apply v.
635 apply (Error (error_message (length l) n)).