| 1 |
(************************************************************************) |
| 2 |
(* * The Rocq Prover / The Rocq Development Team *) |
| 3 |
(* v * Copyright INRIA, CNRS and contributors *) |
| 4 |
(* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 |
(* \VV/ **************************************************************) |
| 6 |
(* // * This file is distributed under the terms of the *) |
| 7 |
(* * GNU Lesser General Public License Version 2.1 *) |
| 8 |
(* * (see LICENSE file for the text of the license) *) |
| 9 |
(************************************************************************) |
| 10 |
|
| 11 |
Set Implicit Arguments. |
| 12 |
(* Set Universe Polymorphism. *) |
| 13 |
|
| 14 |
(******************************************************************) |
| 15 |
(** * Basics: definition of polymorphic lists and some operations *) |
| 16 |
(******************************************************************) |
| 17 |
|
| 18 |
#[local] Open Scope bool_scope. |
| 19 |
Open Scope list_scope. |
| 20 |
|
| 21 |
(************) |
| 22 |
(** ** Map *) |
| 23 |
(************) |
| 24 |
|
| 25 |
Section Map. |
| 26 |
|
| 27 |
Variables (A : Type) (B : Type). |
| 28 |
Variable f : A -> B. |
| 29 |
|
| 30 |
Fixpoint map (l:list A) : list B := |
| 31 |
match l with |
| 32 |
| nil => nil |
| 33 |
| a :: l => (f a) :: (map l) |
| 34 |
end. |
| 35 |
|
| 36 |
End Map. |
| 37 |
|
| 38 |
Section NatSeq. |
| 39 |
|
| 40 |
(** [seq] computes the sequence of [len] contiguous integers |
| 41 |
that starts at [start]. For instance, [seq 2 3] is [2::3::4::nil]. *) |
| 42 |
|
| 43 |
Fixpoint seq (start len:nat) : list nat := |
| 44 |
match len with |
| 45 |
| 0 => nil |
| 46 |
| S len => start :: seq (S start) len |
| 47 |
end. |
| 48 |
|
| 49 |
End NatSeq. |
| 50 |
|
| 51 |
Section Repeat. |
| 52 |
|
| 53 |
Variable A : Type. |
| 54 |
Fixpoint repeat (x : A) (n: nat ) := |
| 55 |
match n with |
| 56 |
| O => nil |
| 57 |
| S k => x :: repeat x k |
| 58 |
end. |
| 59 |
|
| 60 |
End Repeat. |
| 61 |
|
| 62 |
Section Elts. |
| 63 |
|
| 64 |
Variable A : Type. |
| 65 |
|
| 66 |
(*****************************) |
| 67 |
(** ** Nth element of a list *) |
| 68 |
(*****************************) |
| 69 |
|
| 70 |
Fixpoint nth (n:nat) (l:list A) (default:A) {struct l} : A := |
| 71 |
match n, l with |
| 72 |
| O, x :: l' => x |
| 73 |
| O, nil => default |
| 74 |
| S m, nil => default |
| 75 |
| S m, x :: l' => nth m l' default |
| 76 |
end. |
| 77 |
|
| 78 |
End Elts. |
| 79 |
|
| 80 |
Section Cutting. |
| 81 |
|
| 82 |
Variable A : Type. |
| 83 |
|
| 84 |
Fixpoint firstn (n:nat) (l:list A) : list A := |
| 85 |
match n with |
| 86 |
| 0 => nil |
| 87 |
| S n => match l with |
| 88 |
| nil => nil |
| 89 |
| a::l => a::(firstn n l) |
| 90 |
end |
| 91 |
end. |
| 92 |
|
| 93 |
Fixpoint skipn (n:nat) (l:list A) : list A := |
| 94 |
match n with |
| 95 |
| 0 => l |
| 96 |
| S n => match l with |
| 97 |
| nil => nil |
| 98 |
| a::l => skipn n l |
| 99 |
end |
| 100 |
end. |
| 101 |
|
| 102 |
End Cutting. |
| 103 |
|
| 104 |
Section Exists_Forall. |
| 105 |
|
| 106 |
(** * Existential and universal predicates over lists *) |
| 107 |
|
| 108 |
Variable A:Type. |
| 109 |
|
| 110 |
Section One_predicate. |
| 111 |
|
| 112 |
Variable P : A -> Prop. |
| 113 |
|
| 114 |
Inductive Forall : list A -> Prop := |
| 115 |
| Forall_nil : Forall nil |
| 116 |
| Forall_cons : forall x l, P x -> Forall l -> Forall (x :: l). |
| 117 |
|
| 118 |
End One_predicate. |
| 119 |
|
| 120 |
End Exists_Forall. |
| 121 |
|
| 122 |
(***********************) |
| 123 |
(** ** List comparison *) |
| 124 |
(***********************) |
| 125 |
|
| 126 |
Section Compare. |
| 127 |
|
| 128 |
Variable A : Type. |
| 129 |
Variable cmp : A -> A -> comparison. |
| 130 |
|
| 131 |
Fixpoint list_compare (xs ys : list A) : comparison := |
| 132 |
match xs, ys with |
| 133 |
| nil , nil => Eq |
| 134 |
| nil , _ => Lt |
| 135 |
| _ , nil => Gt |
| 136 |
| x :: xs, y :: ys => |
| 137 |
match cmp x y with |
| 138 |
| Eq => list_compare xs ys |
| 139 |
| c => c |
| 140 |
end |
| 141 |
end%list. |
| 142 |
|
| 143 |
End Compare. |
| 144 |
|
| 145 |
Unset Universe Polymorphism. |