©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Inductive data types · Inductive data types Enumerated types Enumerated types : program by case...
Transcript of Inductive data types · Inductive data types Enumerated types Enumerated types : program by case...
![Page 1: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/1.jpg)
Inductive data types
Inductive data types
Assia Mahboubi
9 juin 2010
![Page 2: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/2.jpg)
Inductive data types
In this class, we shall present how Coq’s type system allows us todefine data types using inductive declarations.
![Page 3: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/3.jpg)
Inductive data types
Generalities
Inductive declarations
An arbitrary type as assumed by :
Variable T : Type.
gives no a priori information on the nature, the number, or theproperties of its inhabitants.
![Page 4: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/4.jpg)
Inductive data types
Generalities
Inductive declarations
An inductive type declaration explains how the inhabitants of thetype are built, by giving names to each construction rule :
Print bool.Inductive bool : Set := true : bool | false : bool.
Print nat.Inductive nat : Set := O : nat | S : nat -> nat.
Each such rule is called a constructor.
![Page 5: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/5.jpg)
Inductive data types
Generalities
Inductive declarations
An inductive type declaration explains how the inhabitants of thetype are built, by giving names to each construction rule :
Print bool.
Inductive bool : Set := true : bool | false : bool.
Print nat.Inductive nat : Set := O : nat | S : nat -> nat.
Each such rule is called a constructor.
![Page 6: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/6.jpg)
Inductive data types
Generalities
Inductive declarations
An inductive type declaration explains how the inhabitants of thetype are built, by giving names to each construction rule :
Print bool.Inductive bool : Set := true : bool | false : bool.
Print nat.Inductive nat : Set := O : nat | S : nat -> nat.
Each such rule is called a constructor.
![Page 7: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/7.jpg)
Inductive data types
Generalities
Inductive declarations
An inductive type declaration explains how the inhabitants of thetype are built, by giving names to each construction rule :
Print bool.Inductive bool : Set := true : bool | false : bool.
Print nat.
Inductive nat : Set := O : nat | S : nat -> nat.
Each such rule is called a constructor.
![Page 8: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/8.jpg)
Inductive data types
Generalities
Inductive declarations
An inductive type declaration explains how the inhabitants of thetype are built, by giving names to each construction rule :
Print bool.Inductive bool : Set := true : bool | false : bool.
Print nat.Inductive nat : Set := O : nat | S : nat -> nat.
Each such rule is called a constructor.
![Page 9: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/9.jpg)
Inductive data types
Generalities
Inductive declarations
An inductive type declaration explains how the inhabitants of thetype are built, by giving names to each construction rule :
Print bool.Inductive bool : Set := true : bool | false : bool.
Print nat.Inductive nat : Set := O : nat | S : nat -> nat.
Each such rule is called a constructor.
![Page 10: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/10.jpg)
Inductive data types
Generalities
Inductive declarations in Coq
Inductive types in Coq can be seen as the generalization of similartype constructions in more common programming languages.
They are in fact an extremely rich way of defining data-types,operators, connectives, specifications,...
They are at the core of powerful programming and reasoningtechniques.
![Page 11: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/11.jpg)
Inductive data types
Enumerated types
Enumerated types
Enumerated types are types which list and name exhaustively theirinhabitants.
Inductive bool : Set := true : bool | false : bool.
Inductive prevert_enum : Set :=| one_stone : prevert_enum| two_houses : prevert_enum| some_flowers : prevert_enum| dozen_of_oysters : prevert_enum| an_other_racoon : prevert_enum.
Labels refer to distinct elements.
![Page 12: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/12.jpg)
Inductive data types
Enumerated types
Enumerated types
Enumerated types are types which list and name exhaustively theirinhabitants.
Inductive bool : Set := true : bool | false : bool.
Inductive prevert_enum : Set :=| one_stone : prevert_enum| two_houses : prevert_enum| some_flowers : prevert_enum| dozen_of_oysters : prevert_enum| an_other_racoon : prevert_enum.
Labels refer to distinct elements.
![Page 13: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/13.jpg)
Inductive data types
Enumerated types
Enumerated types : program by case analysis
Inspect the enumerated type inhabitants and assign values :
Definition my_negb (b : bool) :=match b with true => false | false => true.
Definition is_prevert_animal (x : prevert_enum) : bool :=match x with| dozen_of_oyster => true| an_other_racoon => true| _ => falseend.
Eval compute in (is_prevert_animal one_stone).= false: bool
![Page 14: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/14.jpg)
Inductive data types
Enumerated types
Enumerated types : program by case analysis
Inspect the enumerated type inhabitants and assign values :
Definition my_negb (b : bool) :=match b with true => false | false => true.
Definition is_prevert_animal (x : prevert_enum) : bool :=match x with| dozen_of_oyster => true| an_other_racoon => true| _ => falseend.
Eval compute in (is_prevert_animal one_stone).= false: bool
![Page 15: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/15.jpg)
Inductive data types
Enumerated types
Enumerated types : program by case analysis
Inspect the enumerated type inhabitants and assign values :
Definition my_negb (b : bool) :=match b with true => false | false => true.
Definition is_prevert_animal (x : prevert_enum) : bool :=match x with| dozen_of_oyster => true| an_other_racoon => true| _ => falseend.
Eval compute in (is_prevert_animal one_stone).
= false: bool
![Page 16: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/16.jpg)
Inductive data types
Enumerated types
Enumerated types : program by case analysis
Inspect the enumerated type inhabitants and assign values :
Definition my_negb (b : bool) :=match b with true => false | false => true.
Definition is_prevert_animal (x : prevert_enum) : bool :=match x with| dozen_of_oyster => true| an_other_racoon => true| _ => falseend.
Eval compute in (is_prevert_animal one_stone).= false: bool
![Page 17: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/17.jpg)
Inductive data types
Enumerated types
Enumerated types : reason by case analysis
Inspect the enumerated type inhabitants and build proofs :
Lemma bool_case : forall b : bool, b = true ∨ b = false.Proof.intro b.case b.left; reflexivity.
right; reflexivity.Qed.
![Page 18: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/18.jpg)
Inductive data types
Enumerated types
Enumerated types : reason by case analysis
Inspect the enumerated type inhabitants and build proofs :
Lemma is_prevert_animalP : forall x : prevert_enum,is_prevert_animal x = true ->x = dozen_of_oysters ∨ x = an_other_racoon.
Proof.(* Case analysis + computation *)intro x; case x; simpl; intro e.(* In the three first cases: e: false = true *)discriminate e.discriminate e.discriminate e.
(* Now: e: true = true *)left; reflexivity.right; reflexivity.
Qed.
![Page 19: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/19.jpg)
Inductive data types
Enumerated types
Enumerated types : reason by case analysis
Two important tactics, not specific to enumerated types :
I simpl : makes computation progress (pattern matchingapplied to a term starting with a constructor)
I discriminate : allows to use the fact that constructors aredistincts :
I discriminate H : closes a goal featuring a hypothesis H like(H : true = false) ;
I discriminate : closes a goal like (O <> S n).
![Page 20: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/20.jpg)
Inductive data types
Recursive types
Recursive types
Inductive nat : Set :=| O : nat| S : nat -> nat.
Inductive list (A : Type) :=| nil : list A| cons : A -> list A -> list A.
Base case constructors do not feature self-reference to the type.Recursive case constructors do.
![Page 21: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/21.jpg)
Inductive data types
Recursive types
Recursive types
Inductive nat : Set :=| O : nat| S : nat -> nat.
Inductive list (A : Type) :=| nil : list A| cons : A -> list A -> list A.
Base case constructors do not feature self-reference to the type.Recursive case constructors do.
![Page 22: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/22.jpg)
Inductive data types
Recursive types
Recursive types
Inductive nat : Set :=| O : nat| S : nat -> nat.
Inductive list (A : Type) :=| nil : list A| cons : A -> list A -> list A.
Base case constructors do not feature self-reference to the type.Recursive case constructors do.
![Page 23: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/23.jpg)
Inductive data types
Recursive types
Recursive types
Let us craft new inductive types :
Inductive natBinTree : Set :=
| Leaf : nat -> natBinTree| Node : nat -> natBinTree -> natBinTree -> natBinTree
Inductive term : Set :=|Zero : term|One : term|Plus : term -> term -> term|Mult : term -> term -> term.
An inhabitant of a recursive type is built from a finite number ofconstructor applications.
![Page 24: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/24.jpg)
Inductive data types
Recursive types
Recursive types
Let us craft new inductive types :
Inductive natBinTree : Set :=| Leaf : nat -> natBinTree
| Node : nat -> natBinTree -> natBinTree -> natBinTree
Inductive term : Set :=|Zero : term|One : term|Plus : term -> term -> term|Mult : term -> term -> term.
An inhabitant of a recursive type is built from a finite number ofconstructor applications.
![Page 25: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/25.jpg)
Inductive data types
Recursive types
Recursive types
Let us craft new inductive types :
Inductive natBinTree : Set :=| Leaf : nat -> natBinTree| Node : nat ->
natBinTree -> natBinTree -> natBinTree
Inductive term : Set :=|Zero : term|One : term|Plus : term -> term -> term|Mult : term -> term -> term.
An inhabitant of a recursive type is built from a finite number ofconstructor applications.
![Page 26: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/26.jpg)
Inductive data types
Recursive types
Recursive types
Let us craft new inductive types :
Inductive natBinTree : Set :=| Leaf : nat -> natBinTree| Node : nat -> natBinTree -> natBinTree
-> natBinTree
Inductive term : Set :=|Zero : term|One : term|Plus : term -> term -> term|Mult : term -> term -> term.
An inhabitant of a recursive type is built from a finite number ofconstructor applications.
![Page 27: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/27.jpg)
Inductive data types
Recursive types
Recursive types
Let us craft new inductive types :
Inductive natBinTree : Set :=| Leaf : nat -> natBinTree| Node : nat -> natBinTree -> natBinTree -> natBinTree
Inductive term : Set :=|Zero : term|One : term|Plus : term -> term -> term|Mult : term -> term -> term.
An inhabitant of a recursive type is built from a finite number ofconstructor applications.
![Page 28: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/28.jpg)
Inductive data types
Recursive types
Recursive types
Let us craft new inductive types :
Inductive natBinTree : Set :=| Leaf : nat -> natBinTree| Node : nat -> natBinTree -> natBinTree -> natBinTree
Inductive term : Set :=|Zero : term|One : term|Plus : term -> term -> term|Mult : term -> term -> term.
An inhabitant of a recursive type is built from a finite number ofconstructor applications.
![Page 29: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/29.jpg)
Inductive data types
Recursive types
Recursive types : program by case analysis
We have already seen some examples of such pattern matching :
Definition isNotTwo x :=match x with| S (S O) => false| _ => true
end.
Definition is_single_nBT (t : natBinTree) :=match t with|Leaf _ => true|_ => falseend.
![Page 30: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/30.jpg)
Inductive data types
Recursive types
Recursive types : program by case analysis
We have already seen some examples of such pattern matching :
Definition isNotTwo x :=match x with| S (S O) => false| _ => true
end.
Definition is_single_nBT (t : natBinTree) :=match t with|Leaf _ => true|_ => falseend.
![Page 31: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/31.jpg)
Inductive data types
Recursive types
Recursive types : proofs by case analysis
Lemma is_single_nBTP : forall t,is_single_nBT t = true -> exists n : nat, t = Leaf n.
Proof.
(* We use the possibility to destruct the treewhile introducing *)
intros [ nleaf | nnode t1 t2] h.(* First case: we use the available label *)exists nleaf.reflexivity.
(* Second case: the test evaluates to false *)simpl in h.discriminate.Qed.
![Page 32: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/32.jpg)
Inductive data types
Recursive types
Recursive types : proofs by case analysis
Lemma is_single_nBTP : forall t,is_single_nBT t = true -> exists n : nat, t = Leaf n.
Proof.(* We use the possibility to destruct the treewhile introducing *)
intros [ nleaf | nnode t1 t2] h.
(* First case: we use the available label *)exists nleaf.reflexivity.
(* Second case: the test evaluates to false *)simpl in h.discriminate.Qed.
![Page 33: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/33.jpg)
Inductive data types
Recursive types
Recursive types : proofs by case analysis
Lemma is_single_nBTP : forall t,is_single_nBT t = true -> exists n : nat, t = Leaf n.
Proof.(* We use the possibility to destruct the treewhile introducing *)
intros [ nleaf | nnode t1 t2] h.(* First case: we use the available label *)exists nleaf.reflexivity.
(* Second case: the test evaluates to false *)simpl in h.discriminate.Qed.
![Page 34: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/34.jpg)
Inductive data types
Recursive types
Recursive types : proofs by case analysis
Lemma is_single_nBTP : forall t,is_single_nBT t = true -> exists n : nat, t = Leaf n.
Proof.(* We use the possibility to destruct the treewhile introducing *)
intros [ nleaf | nnode t1 t2] h.(* First case: we use the available label *)exists nleaf.reflexivity.
(* Second case: the test evaluates to false *)simpl in h.discriminate.
Qed.
![Page 35: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/35.jpg)
Inductive data types
Recursive types
Recursive types : proofs by case analysis
Lemma is_single_nBTP : forall t,is_single_nBT t = true -> exists n : nat, t = Leaf n.
Proof.(* We use the possibility to destruct the treewhile introducing *)
intros [ nleaf | nnode t1 t2] h.(* First case: we use the available label *)exists nleaf.reflexivity.
(* Second case: the test evaluates to false *)simpl in h.discriminate.Qed.
![Page 36: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/36.jpg)
Inductive data types
Recursive types
Recursive types
Constructors are injective :
Lemma inj_leaf : forall x y, Leaf x = Leaf y -> x = y.Proof.intros x y hLxLy.injection hLxLy.trivial.
Qed.
![Page 37: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/37.jpg)
Inductive data types
Recursive types
Recursive types : structural induction
Let us go back to the definition of natural numbers :
Inductive nat : Set := O : nat | S : nat -> nat.
The Inductive keyword means that at definition time, this systemgeneates an induction principle :
nat_ind: forall P : nat -> Prop,P 0 ->(forall n : nat, P n -> P (S n)) ->forall n : nat, P n
![Page 38: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/38.jpg)
Inductive data types
Recursive types
Recursive types : structural induction
Let us go back to the definition of natural numbers :
Inductive nat : Set := O : nat | S : nat -> nat.
The Inductive keyword means that at definition time, this systemgeneates an induction principle :
nat_ind: forall P : nat -> Prop,P 0 ->
(forall n : nat, P n -> P (S n)) ->forall n : nat, P n
![Page 39: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/39.jpg)
Inductive data types
Recursive types
Recursive types : structural induction
Let us go back to the definition of natural numbers :
Inductive nat : Set := O : nat | S : nat -> nat.
The Inductive keyword means that at definition time, this systemgeneates an induction principle :
nat_ind: forall P : nat -> Prop,P 0 ->(forall n : nat, P n -> P (S n)) ->forall n : nat, P n
![Page 40: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/40.jpg)
Inductive data types
Recursive types
Recursive types : structural induction
To prove that for P : term -> Prop, the theorem forall t :term, P t holds, it is sufficient to :
I Prove that the property holds for the base cases :I (P Zero)I (P One)
I Prove that the property is transmitted inductively :
I forall t1 t2 : term,P t1 -> P t2 -> P (Plus t1 t2)
I forall t1 t2 : term,P t1 -> P t2 -> P (Mult t1 t2)
The type term is the smallest type containing Zero and One, andclosed under Plus and Mult.
![Page 41: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/41.jpg)
Inductive data types
Recursive types
Recursive types : structural induction
To prove that for P : term -> Prop, the theorem forall t :term, P t holds, it is sufficient to :
I Prove that the property holds for the base cases :I (P Zero)I (P One)
I Prove that the property is transmitted inductively :
I forall t1 t2 : term,P t1 -> P t2 -> P (Plus t1 t2)
I forall t1 t2 : term,P t1 -> P t2 -> P (Mult t1 t2)
The type term is the smallest type containing Zero and One, andclosed under Plus and Mult.
![Page 42: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/42.jpg)
Inductive data types
Recursive types
Recursive types : structural induction
To prove that for P : term -> Prop, the theorem forall t :term, P t holds, it is sufficient to :
I Prove that the property holds for the base cases :I (P Zero)I (P One)
I Prove that the property is transmitted inductively :
I forall t1 t2 : term,P t1 -> P t2 -> P (Plus t1 t2)
I forall t1 t2 : term,P t1 -> P t2 -> P (Mult t1 t2)
The type term is the smallest type containing Zero and One, andclosed under Plus and Mult.
![Page 43: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/43.jpg)
Inductive data types
Recursive types
Recursive types : structural induction
To prove that for P : term -> Prop, the theorem forall t :term, P t holds, it is sufficient to :
I Prove that the property holds for the base cases :I (P Zero)I (P One)
I Prove that the property is transmitted inductively :
I forall t1 t2 : term,P t1 -> P t2 -> P (Plus t1 t2)
I forall t1 t2 : term,P t1 -> P t2 -> P (Mult t1 t2)
The type term is the smallest type containing Zero and One, andclosed under Plus and Mult.
![Page 44: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/44.jpg)
Inductive data types
Recursive types
Recursive types : structural induction
The induction principles generated at definition time by the systemallow to :
I Program by recursion (Fixpoint)
I Prove by induction (induction)
![Page 45: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/45.jpg)
Inductive data types
Recursive types
Recursive types : program by structural induction
We can compute some information on the size of a term :
Fixpoint height (t : natBinTree) : nat :=match t with|Leaf _ => 0|Node _ t1 t2 => Max.max (height t1) (height t2) + 1
end.
Fixpoint size (t : natBinTree) : nat :=match t with|Leaf _ => 1|Node _ t1 t2 => (size t1) + (size t2) + 1end.
![Page 46: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/46.jpg)
Inductive data types
Recursive types
Recursive types : program by structural induction
We can compute some information on the size of a term :
Fixpoint height (t : natBinTree) : nat :=match t with|Leaf _ => 0|Node _ t1 t2 => Max.max (height t1) (height t2) + 1
end.
Fixpoint size (t : natBinTree) : nat :=match t with
|Leaf _ => 1|Node _ t1 t2 => (size t1) + (size t2) + 1end.
![Page 47: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/47.jpg)
Inductive data types
Recursive types
Recursive types : program by structural induction
We can compute some information on the size of a term :
Fixpoint height (t : natBinTree) : nat :=match t with|Leaf _ => 0|Node _ t1 t2 => Max.max (height t1) (height t2) + 1
end.
Fixpoint size (t : natBinTree) : nat :=match t with|Leaf _ => 1
|Node _ t1 t2 => (size t1) + (size t2) + 1end.
![Page 48: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/48.jpg)
Inductive data types
Recursive types
Recursive types : program by structural induction
We can compute some information on the size of a term :
Fixpoint height (t : natBinTree) : nat :=match t with|Leaf _ => 0|Node _ t1 t2 => Max.max (height t1) (height t2) + 1
end.
Fixpoint size (t : natBinTree) : nat :=match t with|Leaf _ => 1|Node _ t1 t2 => (size t1) + (size t2) + 1end.
![Page 49: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/49.jpg)
Inductive data types
Recursive types
Recursive types : program by structural induction
We can access some information contained in a term :
Require Import List.Fixpoint label_at_occ (dflt : nat)
(t : natBinTree)(u : list bool) :=match u, t with|nil, _ =>(match t with Leaf n => n | Node n _ _ => n end)
|b :: tl, t =>match t with|Leaf _ => dflt| Node _ t1 t2 =>if b then label_at_occ t2 tl dfltelse label_at_occ t1 tl dflt
endend.
![Page 50: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/50.jpg)
Inductive data types
Recursive types
Recursive types : proofs by structural induction
We have already seen induction at work on nats and lists.Here its goes on binary trees :
Lemma le_height_size : forall t : natBinTree,height t <= size t.
Proof.induction t; simpl.auto.
apply plus_le_compat_r.apply max_case.apply (le_trans _ _ _ IHt1).apply le_plus_l.apply (le_trans _ _ _ IHt2).apply le_plus_r.
Qed.
![Page 51: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/51.jpg)
Inductive data types
They are also inductive types !
Option types
A polymorphic (like list) non recursive type :
Print option.Inductive option (A : Type) : Type :=Some : A -> option A | None : option A
Use it to lift a type to version with default value :
Fixpoint olast (A : Type)(l : list A) : option A :=match l with|nil => None|a :: nil => Some a|a :: l => olast A l
end.
![Page 52: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/52.jpg)
Inductive data types
They are also inductive types !
Option types
A polymorphic (like list) non recursive type :
Print option.Inductive option (A : Type) : Type :=Some : A -> option A | None : option A
Use it to lift a type to version with default value :
Fixpoint olast (A : Type)(l : list A) : option A :=match l with|nil => None|a :: nil => Some a|a :: l => olast A l
end.
![Page 53: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/53.jpg)
Inductive data types
They are also inductive types !
Pairs & coA polymorphic (like list) pair construction :
Print pair.Inductive prod (A B : Type) : Type :=
pair : A -> B -> A * B
The notation A * B denotes (prod A B).The notation (x, y) denotes (pair x y) (implicit argument).
Check (2, 4). : nat * natCheck (true, 2 :: nil). : bool * (list nat)
Fetching the components :
Eval compute in (fst (0, true)).= 0 : natEval compute in (snd (0, true)).= true : bool
![Page 54: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/54.jpg)
Inductive data types
They are also inductive types !
Pairs & coA polymorphic (like list) pair construction :
Print pair.Inductive prod (A B : Type) : Type :=
pair : A -> B -> A * B
The notation A * B denotes (prod A B).The notation (x, y) denotes (pair x y) (implicit argument).
Check (2, 4). : nat * natCheck (true, 2 :: nil). : bool * (list nat)
Fetching the components :
Eval compute in (fst (0, true)).= 0 : natEval compute in (snd (0, true)).= true : bool
![Page 55: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/55.jpg)
Inductive data types
They are also inductive types !
Pairs & co
Pairs can be nested :
Check (0, 1, true).: nat * nat * bool
Eval compute in (fst (0, 1, true)).= (0, 1)
: nat * nat
This can also be adapted to polymorphic n-tuples :
Inductive triple (T1 T2 T3 : Type) :=Triple T1 -> T2 -> T3 -> triple T1 T2 T3.
![Page 56: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/56.jpg)
Inductive data types
They are also inductive types !
Record types
A record type bundles pieces of data you wish to gather in a singletype.
Record admin_person := MkAdmin {id_number : nat;date_of_birth : nat * nat * nat;place_of_birth : nat;sex : bool}
They are also inductive types with a single constructor !
![Page 57: Inductive data types · Inductive data types Enumerated types Enumerated types : program by case analysis Inspect the enumerated type inhabitants and assign values : Definition my_negb](https://reader033.fdocuments.in/reader033/viewer/2022051606/601d5e7e03498227bf658032/html5/thumbnails/57.jpg)
Inductive data types
They are also inductive types !
Record types
You can access to the fields :
Variable t : admin_person.Check (id_number t).
: natCheck id_number.
fun a : admin_person =>let (id_number, _, _, _) := a in id_number
: admin_person -> nat
In proofs, you can break an element of record type with tacticscase/destruct.
Warning : this is pure functional programming...