Coq Manual (Section 4.4.5) G.C. Alexandru Jochem Raat May 26, 2020 - - PowerPoint PPT Presentation

coq manual section 4 4 5
SMART_READER_LITE
LIVE PREVIEW

Coq Manual (Section 4.4.5) G.C. Alexandru Jochem Raat May 26, 2020 - - PowerPoint PPT Presentation

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions Coq Manual (Section 4.4.5) G.C. Alexandru Jochem Raat May 26, 2020 G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5) Inductive


slide-1
SLIDE 1

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Coq Manual (Section 4.4.5)

G.C. Alexandru Jochem Raat May 26, 2020

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-2
SLIDE 2

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Introduction

This section gives an overview of inductive definitions and their rules in Coq: Inductive definitions and their typing/correctness rules Template polymorphism on inductive definitions Destructors of inductive definitions

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-3
SLIDE 3

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Structure

1 Inductive Definitions 2 Template Polymorphism 3 Destructors 4 Pattern Matching 5 Guarded Fixpoints 6 Questions

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-4
SLIDE 4

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Notation

Ind [p] (ΓI := ΓC), where: p: number of parameters ΓI: types of the inductive types, ”type constructors” ΓC: types of the constructors of the inductive types, ”value constructors” Example of parameterized lists: Inductive list (A:Set) : Set := | nil : list A | cons : A -> list A -> list A.

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-5
SLIDE 5

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Example 2

Inductive even : nat -> Prop := | even_O : even 0 | even_S : forall n, odd n -> even (S n) with odd : nat -> Prop := | odd_S :forall n, even n -> odd (S n).

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-6
SLIDE 6

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Arity of sort

”A type T is an arity of sort s if it converts to the sort s

  • r to a product ∀x : T.U with U an arity of sort s.” (p.

211) ∀A : Prop.A → Prop, is an arity of sort Prop. Set, is an arity of sort Set A → B → Set, is an arity of sort Set.

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-7
SLIDE 7

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Arity

Any type is an arity if there is a sort s, for which it is an arity of sort s. So, A → B → Set is an arity, but A → B is not.

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-8
SLIDE 8

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Positivity

Condition used to prevent logical contradiction caused by inductive definitions By preventing the type constructors from being used in a wrong way in the value constructors

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-9
SLIDE 9

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Three definitions for a type T (all in regards to a term X):

1 Positivity condition

T = X t1...tn and X does not occur freely in ti T = ∀x : U, V , where U is strictly positive and V satisfies the positivity condition

2 Strict positivity

X does not occur in T T converts to X t1...tn and X does not occur in ti T converts to ∀x : U, V and X does not occur in U and strictly positively in V T converts to I a1 . . . am t1 . . . tp, where I is inductive with m parameters and X does not occur in ti and the (instantiated) types of constructors of I satisfy nested positivity.

3 Nested positivity

T = (I b1 . . . bm u1 . . . up, I is an inductive type with m parameters and X does not occur in ui T = ∀x : U, V and U strictly positive and V nested positive

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-10
SLIDE 10

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Correctness Rule

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-11
SLIDE 11

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Examples!

Switch to the editor, with the example Coq file! :)

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-12
SLIDE 12

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Structure

1 Inductive Definitions 2 Template Polymorphism 3 Destructors 4 Pattern Matching 5 Guarded Fixpoints 6 Questions

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-13
SLIDE 13

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Template Polymorphism

Inductive types, polymorphic over Sort Used when arity is in Type hierarchy

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-14
SLIDE 14

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Example

Inductive option (A:Type) : Type := | None : option A | Some : A -> option A. Should be able to package terms of any sort. E.g. Type(1), Set, Prop, etc. Should return the lowest applicable sort, not just Type

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-15
SLIDE 15

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Example

For example, if a : Set then option a should be in Set as well. This way a function of type Set -> Set that can take a value like 2, can also take a value like Some 2.

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-16
SLIDE 16

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Typing rule

If A is an arity of some sort, A/s is A with its sort replaced by the sort s. We have r recursively uniform parameters. These are the same in all occurrences of Ij in all constructors, even in the hypotheses. The sorts si are are introduced by the inductive declaration and allow all eliminations.

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-17
SLIDE 17

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Examples

Check (fun A:Type => option A). fun A : Type => option A : Type -> Type Check (fun A:Set => option A). fun A : Set => option A : Set -> Set Check (fun A:Prop => option A). fun A : Prop => option A : Prop -> Set

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-18
SLIDE 18

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Structure

1 Inductive Definitions 2 Template Polymorphism 3 Destructors 4 Pattern Matching 5 Guarded Fixpoints 6 Questions

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-19
SLIDE 19

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Introduction

Basic Question: How to use inductive types Want to retain strong normalization ⇒ primitive recursion Several possible ways to go about this, in Coq the problem is factorized into:

pattern matching recursion with guarded fixpoints

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-20
SLIDE 20

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Structure

1 Inductive Definitions 2 Template Polymorphism 3 Destructors 4 Pattern Matching 5 Guarded Fixpoints 6 Questions

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-21
SLIDE 21

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Pattern matching construct

Concrete syntax: match m as x in I a return P with | (c1 x11 . . . x1p1) ⇒ f1 | . . . | (cn xn1 . . . xnpn) ⇒ fn end

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-22
SLIDE 22

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Pattern matching construct

Concrete syntax:

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-23
SLIDE 23

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Pattern matching construct

Concrete syntax:

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-24
SLIDE 24

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Pattern matching construct

Concrete syntax:

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-25
SLIDE 25

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Pattern matching construct

Concrete syntax:

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-26
SLIDE 26

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Pattern matching construct

Concrete syntax:

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-27
SLIDE 27

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Pattern matching construct

Concrete syntax: match m as x in I a return P with | (c1 x11 . . . x1p1) ⇒ f1 | . . . | (cn xn1 . . . xnpn) ⇒ fn end Abstract syntax: case (m, (λax.P), λx11 . . . x1p1.f1| . . . |λxn1 . . . xnpn.fn)

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-28
SLIDE 28

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Allowed Elimination sorts

We can’t let Prop eliminate to Set, because this would mean doing a case analysis over a non-computational object Elimination from Prop to Type is also excluded, since it is a supertype of Set. Singleton or Empty types are exempt

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-29
SLIDE 29

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Allowed Elimination sorts

Explanation of the stated rules:

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-30
SLIDE 30

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Allowed Elimination sorts

Explanation of the stated rules:

1

1“P is a predicate over n + 1 arguments: The n first ones correspond to the

arguments [indices] of [the type constructor]”

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-31
SLIDE 31

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Allowed Elimination sorts

Explanation of the stated rules:

1

1“P is a predicate over n + 1 arguments [. . . ] the last one corresponds to

  • bject m ”

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-32
SLIDE 32

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Allowed Elimination sorts

Explanation of the stated rules:

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-33
SLIDE 33

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Typing Branches of a case expression

Explanation of the stated rules:

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-34
SLIDE 34

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Typing Branches of a case expression

Explanation of the stated rules:

1

1compare base case of Def. type of constructor G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-35
SLIDE 35

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Typing Branches of a case expression

Explanation of the stated rules:

1

1compare inductive case of Def. type of constructor G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-36
SLIDE 36

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Example

Dependent Elimination Inductive vect (A : Set) : nat -> Set := | vnil : vect A 0 | vcons (a : A) {n} (v : vect A n) : vect A (S n). Fixpoint concat_vect0 {A} {n m} (l:vect A n) (r:vect A m) {struct l} : vect A (n + m) := match l as x in vect _ n return vect A (n + m) with | vnil _ => r | vcons _ a l' => vcons A a (concat_vect1 l' r) end.

P : ∀(n : N), ∀( : vect A n), vect A (n + m)

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-37
SLIDE 37

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Translation

{(vnil N)}P ≡ {(vnil N) : (vect A 0)}P ≡ P 0 (vnil A 0) ≡ vect A (0 + m) {(vcons N)}P ≡ {(vcons N) : (∀(a : A), ∀(v : vect A n), vect A (S n))}P ≡ ∀(a : A), {(vcons N a) : (∀(v : vect A n), (vect A (S n)))}P ≡ ∀(a : A), ∀(v : vect A n), {(vcons N a v) : (vect A (S n))}P ≡ ∀(a : A), ∀(v : vect A n), P (S n) (vect A (S n)) ≡ ∀(a : A), ∀(v : vect A n), vect A ((S n) + m)

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-38
SLIDE 38

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Putting it all together

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-39
SLIDE 39

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Putting it all together

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-40
SLIDE 40

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Putting it all together

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-41
SLIDE 41

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Putting it all together

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-42
SLIDE 42

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Structure

1 Inductive Definitions 2 Template Polymorphism 3 Destructors 4 Pattern Matching 5 Guarded Fixpoints 6 Questions

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-43
SLIDE 43

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Syntax

Concrete Syntax: fix f1(Γ1) : A1 := t1 with . . . with fn(Γn) : An := tm for fi Abstract Syntax: Fix fi{f ′

1 : A′ 1 := t′ 1 . . . f ′ n : A′ n := t′ n}

The typing rule is the expected one for a fixpoint.

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-44
SLIDE 44

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Normalization

Non-normalizing terms allow proofs of absurdity Allow only primitive recursion2 More precisely: “One of the arguments belongs to an inductive type, the function starts with a case analysis and recursive calls are done on variables coming from patterns representing subterms”

2well-founded recursion with Program Fixpoint tactic G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-45
SLIDE 45

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Extended Abstract Syntax

Fix fi{f1/k1 : A1 := t1 . . . fn/kn : An := tn}

3

side conditions: Ai ≤βδιζη ∀y1 : B1, . . . ∀yn : Bn, n ≥ ki, Bki an inductive type. “In the definition ti, if fj occurs then the kjth argument should be structurally smaller than yki” read: The argument meant to be decreasing in a recursive call should be a (nested) subterm of the original argument

3it is still fi : Ai, ki is just an index G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-46
SLIDE 46

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Recursive Arguments

Ind[r](ΓI := ΓC), c : ∀p1 : P1, . . . ∀pr : Pr, ∀x1 : T1 . . . ∀xm : Tm, (Ij p1 . . . pr t1 . . . ts)

Recursive arguments: i, Il occurs in Ti

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-47
SLIDE 47

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Subterm

Fixpoint add (n m:nat) {struct n} : nat := match n with | O => m | S p => S (add p m) end.

Source: https://coq.inria.fr/refman/language/gallina-specification-language.html#coq:cmd.fixpoint

“case(c, P, f1 . . .n) [. . . ] c is y [. . . ] the variables yj occuring in gi corresponding to recursive arguments Bi are structurally smaller than y”

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-48
SLIDE 48

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Deeper subterm

Fixpoint mod2 (n:nat) : nat := match n with | O => O | S p => match p with | O => S O | S q => mod2 q end end.

Source: https://coq.inria.fr/refman/language/gallina-specification-language.html#coq:cmd.fixpoint

“case(c, P, f1 . . .n) [. . . ] c is [. . . ] structurally smaller than y [. . . ] the variables yj occuring in gi corresponding to recursive arguments Bi are structurally smaller than y”

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-49
SLIDE 49

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Compositionality

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-50
SLIDE 50

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Demo

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-51
SLIDE 51

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Reduction

The expected one for fixpoints.

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)

slide-52
SLIDE 52

Inductive Definitions Template Polymorphism Destructors Pattern Matching Guarded Fixpoints Questions

Structure

1 Inductive Definitions 2 Template Polymorphism 3 Destructors 4 Pattern Matching 5 Guarded Fixpoints 6 Questions

G.C. Alexandru, Jochem Raat Coq Manual (Section 4.4.5)