Library HoTT.Spaces.List.Core
Require Import Basics.Overture.
Local Unset Elimination Schemes.
Local Set Universe Minimization ToSet.
Local Set Polymorphic Inductive Cumulativity.
Local Unset Elimination Schemes.
Local Set Universe Minimization ToSet.
Local Set Polymorphic Inductive Cumulativity.
Declare Scope list_scope.
Local Open Scope list_scope.
A list is a sequence of elements from a type A. This is a very useful datatype and has many applications ranging from programming to algebra. It can be thought of a free monoid.
Inductive list@{i|} (A : Type@{i}) : Type@{i} :=
| nil : list A
| cons : A → list A → list A.
Arguments nil {A}.
Arguments cons {A} _ _.
Delimit Scope list_scope with list.
Bind Scope list_scope with list.
| nil : list A
| cons : A → list A → list A.
Arguments nil {A}.
Arguments cons {A} _ _.
Delimit Scope list_scope with list.
Bind Scope list_scope with list.
This messes with Coq's parsing of ☐ in ltac. Therefore we keep it commented out. It's not difficult to write nil instead.
(* Notation "" := nil : list_scope. *)
Infix "::" := cons : list_scope.
Scheme list_rect := Induction for list Sort Type.
Scheme list_ind := Induction for list Sort Type.
Scheme list_rec := Minimality for list Sort Type.
Infix "::" := cons : list_scope.
Scheme list_rect := Induction for list Sort Type.
Scheme list_ind := Induction for list Sort Type.
Scheme list_rec := Minimality for list Sort Type.
A tactic for doing induction over a list that avoids spurious universes.
Ltac simple_list_induction l h t IH :=
try generalize dependent l;
fix IH 1;
intros [| h t];
[ clear IH | specialize (IH t) ].
try generalize dependent l;
fix IH 1;
intros [| h t];
[ clear IH | specialize (IH t) ].
Notation "[ x ]" := (x :: nil) : list_scope.
Notation "[ x , y , .. , z ]" := (x :: (y :: .. (z :: nil) ..)) : list_scope.
Notation "[ x , y , .. , z ]" := (x :: (y :: .. (z :: nil) ..)) : list_scope.
Length
Concatenation
Definition app {A : Type} : list A → list A → list A :=
fix app l m :=
match l with
| nil ⇒ m
| a :: l1 ⇒ a :: app l1 m
end.
Infix "++" := app : list_scope.
fix app l m :=
match l with
| nil ⇒ m
| a :: l1 ⇒ a :: app l1 m
end.
Infix "++" := app : list_scope.
Folding
Fixpoint fold_left {A B} (f : A → B → A) (l : list B) (default : A) : A :=
match l with
| nil ⇒ default
| cons b l ⇒ fold_left f l (f default b)
end.
match l with
| nil ⇒ default
| cons b l ⇒ fold_left f l (f default b)
end.
Fixpoint fold_right {A B} (f : B → A → A) (default : A) (l : list B) : A :=
match l with
| nil ⇒ default
| cons b l ⇒ f b (fold_right f default l)
end.
match l with
| nil ⇒ default
| cons b l ⇒ f b (fold_right f default l)
end.
Maps - Functoriality of Lists
Fixpoint list_map {A B : Type} (f : A → B) (l : list A) :=
match l with
| nil ⇒ nil
| x :: l ⇒ (f x) :: (list_map f l)
end.
match l with
| nil ⇒ nil
| x :: l ⇒ (f x) :: (list_map f l)
end.
The list_map2 function applies a binary function to corresponding elements of two lists. When one of the lists run out, it uses one of the default functions to fill in the rest.
Fixpoint list_map2 {A B C : Type} (f : A → B → C)
(def_l : list A → list C) (def_r : list B → list C) l1 l2 :=
match l1, l2 with
| nil, nil ⇒ nil
| nil, _ ⇒ def_r l2
| _, nil ⇒ def_l l1
| x :: l1, y :: l2 ⇒ (f x y) :: (list_map2 f def_l def_r l1 l2)
end.
(def_l : list A → list C) (def_r : list B → list C) l1 l2 :=
match l1, l2 with
| nil, nil ⇒ nil
| nil, _ ⇒ def_r l2
| _, nil ⇒ def_l l1
| x :: l1, y :: l2 ⇒ (f x y) :: (list_map2 f def_l def_r l1 l2)
end.
Fixpoint reverse_acc {A : Type} (acc : list A) (l : list A) : list A :=
match l with
| nil ⇒ acc
| x :: l ⇒ reverse_acc (x :: acc) l
end.
match l with
| nil ⇒ acc
| x :: l ⇒ reverse_acc (x :: acc) l
end.
Reversing the order of a list. The list [a1; a2; ...; an] becomes [an; ...; a2; a1] .
Definition head {A : Type} (l : list A) : option A :=
match l with
| nil ⇒ None
| a :: _ ⇒ Some a
end.
match l with
| nil ⇒ None
| a :: _ ⇒ Some a
end.
The tail of a list is the list without its first element.
The last element of a list. If the list is empty, it returns None.
Fixpoint last {A : Type} (l : list A) : option A :=
match l with
| nil ⇒ None
| a :: nil ⇒ Some a
| _ :: l ⇒ last l
end.
match l with
| nil ⇒ None
| a :: nil ⇒ Some a
| _ :: l ⇒ last l
end.
The n-th element of a list. If the list is too short, it returns None.
Fixpoint nth {A : Type} (l : list A) (n : nat) : option A :=
match n, l with
| O, x :: _ ⇒ Some x
| S n, _ :: l ⇒ nth l n
| _, _ ⇒ None
end.
match n, l with
| O, x :: _ ⇒ Some x
| S n, _ :: l ⇒ nth l n
| _, _ ⇒ None
end.
Fixpoint remove_last {A : Type} (l : list A) : list A :=
match l with
| nil ⇒ nil
| _ :: nil ⇒ nil
| x :: l ⇒ x :: remove_last l
end.
match l with
| nil ⇒ nil
| _ :: nil ⇒ nil
| x :: l ⇒ x :: remove_last l
end.
Ascending sequence of natural numbers < n.
Fixpoint repeat {A : Type} (x : A) (n : nat) : list A :=
match n with
| O ⇒ nil
| S n ⇒ x :: repeat x n
end.
match n with
| O ⇒ nil
| S n ⇒ x :: repeat x n
end.
Fixpoint InList@{i|} {A : Type@{i}} (a : A) (l : list A) : Type@{i} :=
match l with
| nil ⇒ Empty
| b :: m ⇒ (b = a) + InList a m
end.
match l with
| nil ⇒ Empty
| b :: m ⇒ (b = a) + InList a m
end.