Type-Based Methods for Termination and Productivity in Coq Bruno - - PowerPoint PPT Presentation

type based methods for termination and productivity in coq
SMART_READER_LITE
LIVE PREVIEW

Type-Based Methods for Termination and Productivity in Coq Bruno - - PowerPoint PPT Presentation

Type-Based Methods for Termination and Productivity in Coq Bruno Barras 1 Jorge Luis Sacchini 2 1 INRIA Saclay & LIX 2 Carnegie Mellon University Qatar July 22, 2013 Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination


slide-1
SLIDE 1

Type-Based Methods for Termination and Productivity in Coq

Bruno Barras1 Jorge Luis Sacchini2

1INRIA Saclay & LIX 2Carnegie Mellon University – Qatar

July 22, 2013

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 1 / 26

slide-2
SLIDE 2

Coq

Coq is a total dependently-typed programming language Totality means:

◮ Functions must be defined in their entire domain (no partial functions) ◮ Recursive functions must be terminating ◮ Co-recursive functions must be productive

Non-terminations leads to inconsistencies Ex: (let f x = f x in f 0) : 0 = 1 Totality ensures logical consistency and decidability of type checking

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 2 / 26

slide-3
SLIDE 3

Coq

Termination and productivity are undecidable problems Approximate the answer Coq imposes syntactic restrictions on (co-)recursive definitions For termination: guarded-by-destructors Recursive calls performed only on structurally smaller terms Γ(f : I → T) ⊢ M : I → T Γ ⊢ (fix f : I → T := M) : I → T

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 3 / 26

slide-4
SLIDE 4

Coq

Termination and productivity are undecidable problems Approximate the answer Coq imposes syntactic restrictions on (co-)recursive definitions For termination: guarded-by-destructors Recursive calls performed only on structurally smaller terms Γ(f : I → T) ⊢ M : I → T G(f , M) Γ ⊢ (fix f : I → T := M) : I → T The predicate G(f , M) checks that all recursive calls of f in M are guarded by destructors

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 3 / 26

slide-5
SLIDE 5

Coq

Termination and productivity are undecidable problems Approximate the answer Coq imposes syntactic restrictions on (co-)recursive definitions For termination: guarded-by-destructors Recursive calls performed only on structurally smaller terms Γ(f : I → T) ⊢ M : I → T G(f , M) Γ ⊢ (fix f : I → T := M) : I → T The predicate G(f , M) checks that all recursive calls of f in M are guarded by destructors Actually, the guard condition is checked on a normal form of the body Γ(f : I → T) ⊢ M : I → T M →∗ N G(f , N) Γ ⊢ (fix f : I → T := M) : I → T

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 3 / 26

slide-6
SLIDE 6

Termination in Coq

Typical example: fix half : nat → nat := λx. case x of | O ⇒ O | S O ⇒ O | S (S p) ⇒ S(half p) Recursive call is guarded. The recursive argument is smaller. The initial implementation of G (due to Eduardo Gim´ enez around 1994) has been extended over the years to allow more functions. Most recent extension: commutative cuts (due to Pierre Boutillier).

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 4 / 26

slide-7
SLIDE 7

Termination in Coq

Typical example: fix half : nat → nat := λx. case x of | O ⇒ O | S O ⇒ O | S (S p) ⇒ S(half p) p ≺ S (S p) Recursive call is guarded. The recursive argument is smaller. The initial implementation of G (due to Eduardo Gim´ enez around 1994) has been extended over the years to allow more functions. Most recent extension: commutative cuts (due to Pierre Boutillier).

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 4 / 26

slide-8
SLIDE 8

Termination in Coq

Subterm relation

Subtraction: fix minus : nat → nat → nat := λxy. case x, y of | O, ⇒ x | S x1, O ⇒ S x1 | S x1, S y1 ⇒ minus x1 y1

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 5 / 26

slide-9
SLIDE 9

Termination in Coq

Subterm relation

Subtraction: fix minus : nat → nat → nat := λxy. case x, y of | O, ⇒ x | S x1, O ⇒ S x1 | S x1, S y1 ⇒ minus x1 y1

"

x1 ≺ x (x1 is a strict subterm of S x1 ≡ x)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 5 / 26

slide-10
SLIDE 10

Termination in Coq

Subterm relation

Subtraction: fix minus : nat → nat → nat := λxy. case x, y of | O, ⇒ x | S x1, O ⇒ S x1 | S x1, S y1 ⇒ minus x1 y1

"

x1 ≺ x (x1 is a strict subterm of S x1 ≡ x) Division: div x y =

  • x

y+1

  • fix div : nat → nat → nat := λxy. case x of

| O ⇒ O | S x1 ⇒ S(div (minus x1 y) y)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 5 / 26

slide-11
SLIDE 11

Termination in Coq

Subterm relation

Subtraction: fix minus : nat → nat → nat := λxy. case x, y of | O, ⇒ x | S x1, O ⇒ S x1 | S x1, S y1 ⇒ minus x1 y1

"

x1 ≺ x (x1 is a strict subterm of S x1 ≡ x) Division: div x y =

  • x

y+1

  • fix div : nat → nat → nat := λxy. case x of

| O ⇒ O | S x1 ⇒ S(div (minus x1 y) y)

"

minus x1 y x1 ≺ S x1 ≡ x

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 5 / 26

slide-12
SLIDE 12

Termination in Coq

Subterm relation

Subtraction: fix minus : nat → nat → nat := λxy. case x, y of | O, ⇒ x | S x1, O ⇒ S x1 | S x1, S y1 ⇒ minus x1 y1 Division: div x y =

  • x

y+1

  • fix div : nat → nat → nat := λxy. case x of

| O ⇒ O | S x1 ⇒ S(div (minus x1 y) y)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 5 / 26

slide-13
SLIDE 13

Termination in Coq

Subterm relation

Subtraction: fix minus : nat → nat → nat := λxy. case x, y of | O, ⇒ O | S x1, O ⇒ S x1 | S x1, S y1 ⇒ minus x1 y1

"

x1 ≺ x (x1 is a strict subterm of S x1 ≡ x) Division: div x y =

  • x

y+1

  • fix div : nat → nat → nat := λxy. case x of

| O ⇒ O | S x1 ⇒ S(div (minus x1 y) y)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 5 / 26

slide-14
SLIDE 14

Termination in Coq

Subterm relation

Subtraction: fix minus : nat → nat → nat := λxy. case x, y of | O, ⇒ O | S x1, O ⇒ S x1 | S x1, S y1 ⇒ minus x1 y1

"

x1 ≺ x (x1 is a strict subterm of S x1 ≡ x) Division: div x y =

  • x

y+1

  • fix div : nat → nat → nat := λxy. case x of

| O ⇒ O | S x1 ⇒ S(div (minus x1 y) y)

$

minus x1 y x1 ≺ S x1 ≡ x

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 5 / 26

slide-15
SLIDE 15

Termination in Coq

Nested fixpoints

Inductive rose(A) : Type := node : A → list (rose A) → rose A rmap := λf : A → B. fix rmap : rose A → rose B := λt. case t of node x ts ⇒ node (f x) (map rmap ts) map := λf : A → B. fix map : list A → list B := λl. case l of nil ⇒ nil cons x xs ⇒ cons (f x) (map xs)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 6 / 26

slide-16
SLIDE 16

Termination in Coq

Nested fixpoints

Inductive rose(A) : Type := node : A → list (rose A) → rose A rmap := λf : A → B. fix rmap : rose A → rose B := λt. case t of

"

node x ts ⇒ node (f x) (map rmap ts) map := λf : A → B. fix map : list A → list B := λl. case l of nil ⇒ nil

"

cons x xs ⇒ cons (f x) (map xs)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 6 / 26

slide-17
SLIDE 17

Termination in Coq

Nested fixpoints

Inductive rose(A) : Type := node : A → list (rose A) → rose A rmap := λf : A → B. fix rmap : rose A → rose B := λt. case t of node x ts ⇒ node (f x) (map rmap ts) map := fix map : (A → B) → list A → list B := λf l. case l of nil ⇒ nil

"

cons x xs ⇒ cons (f x) (map f xs)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 6 / 26

slide-18
SLIDE 18

Termination in Coq

Nested fixpoints

Inductive rose(A) : Type := node : A → list (rose A) → rose A rmap := λf : A → B. fix rmap : rose A → rose B := λt. case t of

$

node x ts ⇒ node (f x) (map rmap ts) map := fix map : (A → B) → list A → list B := λf l. case l of nil ⇒ nil

"

cons x xs ⇒ cons (f x) (map f xs)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 6 / 26

slide-19
SLIDE 19

Syntactic criteria

Limitations

Works on syntax: small changes in code can make functions ill-typed Not compositional Difficult to understand for users

◮ Many questions about termination in the Coq list ◮ Error messages not informative

Difficult to implement: termination checking is the most delicate part

  • f Coq’s kernel

Inefficient: guard condition is checked on the normal form of fixpoints bodies Difficult to study

◮ Little documentation ◮ Complicated to even define Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 7 / 26

slide-20
SLIDE 20

Termination in Coq

Many ways to get around the guard condition:

◮ Adding extra argument to act as measure of termination ◮ Wellfounded recursion ◮ Ad-hoc predicate (Bove) ◮ Tool support (Function, Program)

But this complicates function definition May affect efficiency

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 8 / 26

slide-21
SLIDE 21

Termination using sized types

Long history: Haskell [Pareto et al.], λ [Joao Frade et al.], Fω [Abel], CIC [Barthe et al.], CC+rewriting [Blanqui et al.] . . .

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 9 / 26

slide-22
SLIDE 22

Termination using sized types

Long history: Haskell [Pareto et al.], λ [Joao Frade et al.], Fω [Abel], CIC [Barthe et al.], CC+rewriting [Blanqui et al.] . . . Basic idea: user-defined datatypes are decorated with size information nat ::= O : nat | S : nat → nat Intuitive meaning: [nat] = {O, S O, S(S O), . . .}

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 9 / 26

slide-23
SLIDE 23

Termination using sized types

Long history: Haskell [Pareto et al.], λ [Joao Frade et al.], Fω [Abel], CIC [Barthe et al.], CC+rewriting [Blanqui et al.] . . . Basic idea: user-defined datatypes are decorated with size information nat ::= O : nat | S : nat → nat Intuitive meaning: [nat] = {O, S O, S(S O), . . .} Sized types are approximations nats Intuitive meaning: [nats] = {O, S O, . . . , S(. . . (S O) . . .)

  • s−1

}

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 9 / 26

slide-24
SLIDE 24

Termination using sized types

Size annotations keep track of the size of elements s ::= ı | s | ∞

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 10 / 26

slide-25
SLIDE 25

Termination using sized types

Size annotations keep track of the size of elements s ::= ı | s | ∞

  • ∞ = ∞

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 10 / 26

slide-26
SLIDE 26

Termination using sized types

Size annotations keep track of the size of elements s ::= ı | s | ∞ Γ ⊢ O : nat Γ ⊢ M : nat Γ ⊢ S M : nat

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 10 / 26

slide-27
SLIDE 27

Termination using sized types

Size annotations keep track of the size of elements s ::= ı | s | ∞ Γ ⊢ O : nat s Γ ⊢ M : nats Γ ⊢ S M : nat s

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 10 / 26

slide-28
SLIDE 28

Termination using sized types

Size annotations keep track of the size of elements s ::= ı | s | ∞ Γ ⊢ O : nat s Γ ⊢ M : nats upper bound Γ ⊢ S M : nat s

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 10 / 26

slide-29
SLIDE 29

Termination using sized types

Size annotations keep track of the size of elements s ::= ı | s | ∞ Γ ⊢ O : nat s Γ ⊢ M : nats Γ ⊢ S M : nat s Substage relation s ⊑ s s ⊑ ∞ defines a subtype relation s ⊑ r nats ≤ natr

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 10 / 26

slide-30
SLIDE 30

Termination using sized types

Fixpoint rule

Recursive functions are defined on approximations of datatypes: Γ(f : I → T) ⊢ M : I → T Γ ⊢ (fix f : I → T := M) : I → T

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 11 / 26

slide-31
SLIDE 31

Termination using sized types

Fixpoint rule

Recursive functions are defined on approximations of datatypes: Γ(f : Iı → T) ⊢ M : I ı → T Γ ⊢ (fix f : I → T := M) : Is → T ı fresh Recursive calls on terms of smaller size

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 11 / 26

slide-32
SLIDE 32

Termination using sized types

Fixpoint rule

Recursive functions are defined on approximations of datatypes: Γ(f : Iı → T) ⊢ M : I ı → T Γ ⊢ (fix f : I → T := M) : Is → T ı fresh Recursive calls on terms of smaller size Size-preserving functions: return type T can depend on ı

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 11 / 26

slide-33
SLIDE 33

Termination using sized types

Fixpoint rule

Recursive functions are defined on approximations of datatypes: Γ(f : Iı → T) ⊢ M : I ı → T Γ ⊢ (fix f : I → T := M) : Is → T ı fresh Recursive calls on terms of smaller size Size-preserving functions: return type T can depend on ı Non-structural recursion

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 11 / 26

slide-34
SLIDE 34

Example: quicksort

Non-structural recursion

filter ≡ . . . : ΠA.(A → bool) → list A → list A × list A (++) ≡ . . . : ΠA.list A → list A → list A

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 12 / 26

slide-35
SLIDE 35

Example: quicksort

Non-structural recursion

filter ≡ . . . : ΠA.(A → bool) → list A → list A × list A (++) ≡ . . . : ΠA.list A → list A → list A fix qsort : list A → list A := λx : list A. case x

  • f

| nil ⇒ nil | cons h t ⇒ let (s, g) = filter (< h) t in (qsort s ) ++ (cons h (qsort g ))

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 12 / 26

slide-36
SLIDE 36

Example: quicksort

Non-structural recursion

filter ≡ . . . : ΠA.(A → bool) → lists A → lists A × lists A (++) ≡ . . . : ΠA.lists A → listr A → list∞ A fix qsort : list A → list A := λx : list A. case x

  • f

| nil ⇒ nil | cons h t ⇒ let (s, g) = filter (< h) t in (qsort s ) ++ (cons h (qsort g ))

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 12 / 26

slide-37
SLIDE 37

Example: quicksort

Non-structural recursion

filter ≡ . . . : ΠA.(A → bool) → lists A → lists A × lists A (++) ≡ . . . : ΠA.lists A → listr A → list∞ A fix qsort : list A → list A := λx : list A. case xlist

ı of

| nil ⇒ nil | cons h tlistı ⇒ let (s, g) = filter (< h) tlistı in (qsort slistı) ++ (cons h (qsort glistı))

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 12 / 26

slide-38
SLIDE 38

Example: quicksort

Non-structural recursion

filter ≡ . . . : ΠA.(A → bool) → lists A → lists A × lists A (++) ≡ . . . : ΠA.lists A → listr A → list∞ A fix qsort : list A → list A := λx : list A. case xlist

ı of

| nil ⇒ nil | cons h tlistı ⇒ let (s, g) = filter (< h) tlistı in (qsort slistı) ++ (cons h (qsort glistı)) : ΠA.lists A → list∞ A

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 12 / 26

slide-39
SLIDE 39

Type-based termination

Handle higher-order data node : ΠA.A → list∞ (roses A) → rose s A Advantages over syntactic criteria

◮ Expressiveness ◮ Compositional ◮ Easier to understand (specially for ill-typed terms) ◮ Easier to implement (as shown in prototype implementations) ◮ Easier to study (semantically intuitive) ◮ Not intrusive for the user (minimal annotations required)

Good candidate to replace syntactic criterion in Coq

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 13 / 26

slide-40
SLIDE 40

Coinductive Types

Coinductive types are used to model and reason about infinite data and infinite processes. Coinductive types can be seen as the dual of inductive types. Inductive types Coinductive types Induction Coinduction Recursive functions consume data Corecursive functions produce data

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 14 / 26

slide-41
SLIDE 41

Coinductive Types in Coq

Streams: CoInductive stream A := scons : A → stream A → stream A

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 15 / 26

slide-42
SLIDE 42

Coinductive Types in Coq

Streams: CoInductive Empty as an inductive type stream A := scons : A → stream A → stream A

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 15 / 26

slide-43
SLIDE 43

Coinductive Types in Coq

Streams: CoInductive stream A := scons : A → stream A → stream A Corecursive functions produce streams: zeroes := cofix Z := scons(0, Z) zeroes produce the stream: scons(0, scons(0, scons(0, . . .)))

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 15 / 26

slide-44
SLIDE 44

Coinductive types

Inductive types Coinductive types Termination Productivity In proof assistants, termination of recursive functions is essential to ensure logical consistency and decidability of type checking. For corecursive functions, the dual condition to termination is productivity. In the case of streams, productivity means that we can compute any element of the stream in finite time: cofix Z1 := scons(0, Z1) cofix Z2 := scons(0, tail Z2)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 16 / 26

slide-45
SLIDE 45

Coinductive types

Inductive types Coinductive types Termination Productivity In proof assistants, termination of recursive functions is essential to ensure logical consistency and decidability of type checking. For corecursive functions, the dual condition to termination is productivity. In the case of streams, productivity means that we can compute any element of the stream in finite time: cofix Z1 := scons(0, Z1)

"

cofix Z2 := scons(0, tail Z2)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 16 / 26

slide-46
SLIDE 46

Coinductive types

Inductive types Coinductive types Termination Productivity In proof assistants, termination of recursive functions is essential to ensure logical consistency and decidability of type checking. For corecursive functions, the dual condition to termination is productivity. In the case of streams, productivity means that we can compute any element of the stream in finite time: cofix Z1 := scons(0, Z1)

"

cofix Z2 := scons(0, tail Z2)

$

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 16 / 26

slide-47
SLIDE 47

Coinductive types

Inductive types Coinductive types Termination Productivity In proof assistants, termination of recursive functions is essential to ensure logical consistency and decidability of type checking. For corecursive functions, the dual condition to termination is productivity. In the case of streams, productivity means that we can compute any element of the stream in finite time: cofix Z1 := scons(0, Z1)

"

cofix Z2 := scons(0, tail Z2) (tail Z2) loops

$

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 16 / 26

slide-48
SLIDE 48

Syntactic-Based Methods for Productivity

Inductive types Coinductive types Termination Productivity Guarded-by-Destructor Guarded-by-Constructor Guarded-by-constructor: every corecursive call is performed directly under a constructor Same limitations as in the inductive case

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 17 / 26

slide-49
SLIDE 49

Syntactic-Based Methods for Productivity

Inductive types Coinductive types Termination Productivity Guarded-by-Destructor Guarded-by-Constructor Guarded-by-constructor: every corecursive call is performed directly under a constructor Same limitations as in the inductive case nats := cofix nats := λn. scons(n, nats (1 + n))

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 17 / 26

slide-50
SLIDE 50

Syntactic-Based Methods for Productivity

Inductive types Coinductive types Termination Productivity Guarded-by-Destructor Guarded-by-Constructor Guarded-by-constructor: every corecursive call is performed directly under a constructor Same limitations as in the inductive case nats := cofix nats := λn. scons(n, nats (1 + n))

"

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 17 / 26

slide-51
SLIDE 51

Syntactic-Based Methods for Productivity

Inductive types Coinductive types Termination Productivity Guarded-by-Destructor Guarded-by-Constructor Guarded-by-constructor: every corecursive call is performed directly under a constructor Same limitations as in the inductive case nats := cofix nats := λn. scons(n, nats (1 + n))

"

nats := λn. cofix nats := scons(n, map (λx. 1+x) nats)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 17 / 26

slide-52
SLIDE 52

Syntactic-Based Methods for Productivity

Inductive types Coinductive types Termination Productivity Guarded-by-Destructor Guarded-by-Constructor Guarded-by-constructor: every corecursive call is performed directly under a constructor Same limitations as in the inductive case nats := cofix nats := λn. scons(n, nats (1 + n))

"

nats := λn. cofix nats := scons(n, map (λx. 1+x) nats)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 17 / 26

slide-53
SLIDE 53

Syntactic-Based Methods for Productivity

Inductive types Coinductive types Termination Productivity Guarded-by-Destructor Guarded-by-Constructor Guarded-by-constructor: every corecursive call is performed directly under a constructor Same limitations as in the inductive case nats := cofix nats := λn. scons(n, nats (1 + n))

"

nats := λn. cofix nats := scons(n, map (λx. 1+x) nats)

$

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 17 / 26

slide-54
SLIDE 54

Type-Based Methods for Productivity

Sized types can be applied to productivity checking as well!

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 18 / 26

slide-55
SLIDE 55

Type-Based Methods for Productivity

Sized types can be applied to productivity checking as well! Dual meaning of size annotations on coinductive types streams A is the type of streams of which at least s elements can be produced

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 18 / 26

slide-56
SLIDE 56

Type-Based Methods for Productivity

Sized types can be applied to productivity checking as well! Dual meaning of size annotations on coinductive types streams A is the type of streams of which at least s elements can be produced Size annotations are contra-variant: r ⊑ s streams T ≤ streamr T

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 18 / 26

slide-57
SLIDE 57

Type-Based Methods for Productivity

Sized types can be applied to productivity checking as well! Dual meaning of size annotations on coinductive types streams A is the type of streams of which at least s elements can be produced Size annotations are contra-variant: r ⊑ s streams T ≤ streamr T s ⊑ r lists T ≤ listr T

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 18 / 26

slide-58
SLIDE 58

Type-Based Methods for Productivity

Typing rules are similar to the inductive case Rules for constructors: Γ ⊢ M : A Γ ⊢ N : streams A Γ ⊢ scons(M, N) : stream s A Cofixpoint definition is also similar to fixpoint definition: Γ(f : streamı A) ⊢ M : stream ı A Γ ⊢ cofix f := M : streams A ı fresh

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 19 / 26

slide-59
SLIDE 59

Type-Based Methods for Productivity

Typing rules are similar to the inductive case Rules for constructors: Γ ⊢ M : A Γ ⊢ N : streams A Γ ⊢ scons(M, N) : stream s A Γ ⊢ M : A Γ ⊢ N : lists A Γ ⊢ cons(M, N) : list s A Cofixpoint definition is also similar to fixpoint definition: Γ(f : streamı A) ⊢ M : stream ı A Γ ⊢ cofix f := M : streams A ı fresh

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 19 / 26

slide-60
SLIDE 60

Type-Based Methods for Productivity

Typing rules are similar to the inductive case Rules for constructors: Γ ⊢ M : A Γ ⊢ N : streams A Γ ⊢ scons(M, N) : stream s A Γ ⊢ M : A Γ ⊢ N : lists A Γ ⊢ cons(M, N) : list s A Cofixpoint definition is also similar to fixpoint definition: Γ(f : streamı A) ⊢ M : stream ı A Γ ⊢ cofix f := M : streams A ı fresh Γ(f : listı A → U) ⊢ M : list ı A → U Γ ⊢ fix f := M : lists A → U ı fresh

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 19 / 26

slide-61
SLIDE 61

Co-recursive definitions

Examples

map : (A → B) → stream A → stream B merge : stream nat → stream nat → stream nat merge (1 3 5 . . .) (2 4 6 . . .) = (1 2 3 4 . . .) ham := cofix ham : stream nat := scons(1, merge (map (λx. 2∗x) ham ) (merge (map (λx. 3∗x) ham ) (map (λx. 5∗x) ham ))) ham = (1 2 3 4 5 6 8 9 10 12 15 . . .)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 20 / 26

slide-62
SLIDE 62

Co-recursive definitions

Examples

map : (A → B) → streamsA → streamsB merge : streamsnat → streamsnat → streamsnat merge (1 3 5 . . .) (2 4 6 . . .) = (1 2 3 4 . . .) ham := cofix ham : stream nat := scons(1, merge (map (λx. 2∗x) ham ) (merge (map (λx. 3∗x) ham ) (map (λx. 5∗x) ham ))) ham = (1 2 3 4 5 6 8 9 10 12 15 . . .)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 20 / 26

slide-63
SLIDE 63

Co-recursive definitions

Examples

map : (A → B) → streamsA → streamsB merge : streamsnat → streamsnat → streamsnat merge (1 3 5 . . .) (2 4 6 . . .) = (1 2 3 4 . . .) ham := cofix ham : stream nat := scons(1, merge (map (λx. 2∗x) hamstreamı) (merge (map (λx. 3∗x) hamstreamı) (map (λx. 5∗x) hamstreamı))) ham = (1 2 3 4 5 6 8 9 10 12 15 . . .)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 20 / 26

slide-64
SLIDE 64

Sized types for coinduction

Type-based productivity has several advantages over syntactic-based

◮ More expressive ◮ Compositional ◮ Easier to understand (specially for ill-typed terms) ◮ Easier to implement (as shown in prototype implementations) ◮ Easier to study (semantically intuitive) ◮ Not intrusive for the user (minimal annotations required)

Furthermore, sized types treat inductive and co-inductive types in a similar way

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 21 / 26

slide-65
SLIDE 65

What’s next?

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 22 / 26

slide-66
SLIDE 66

What’s next?

Design a type-based termination system for Coq Implementation!

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 22 / 26

slide-67
SLIDE 67

What’s next?

Design a type-based termination system for Coq Implementation! Sombrero line (Barthe et al.) : λ, F, CIC Sizes are declared implicitly (not first class): Size inference: little burden for the user

◮ Constraint-based algorithm ◮ Treats fixpoints and co-fixpoints in the same way

Still some issues remain in order to adapt to full Coq

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 22 / 26

slide-68
SLIDE 68

In a future Coq version . . .

Fixpoint map ı (f : A -> B) (xs : List<ı> A) : List<ı> B := match xs with nil => nil cons h t => cons (f h) (map f t) end.

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 23 / 26

slide-69
SLIDE 69

In a future Coq version . . .

Fixpoint map ı (f : A -> B) (xs : List<ı> A) : List<ı> B := match xs with nil => nil cons h t => cons (f h) (map f t) end. Check map. map : ∀ ı. (A -> B) -> List<ı> A -> List<ı> B.

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 23 / 26

slide-70
SLIDE 70

In a future Coq version . . .

Fixpoint map ı (f : A -> B) (xs : List<ı> A) : List<ı> B := match xs with nil => nil cons h t => cons (f h) (map f t) end. Check map. map : ∀ ı. (A -> B) -> List<ı> A -> List<ı> B. Fixpoint ntail ı A (x : nat<ı>) : List A → List A := ...

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 23 / 26

slide-71
SLIDE 71

In a future Coq version . . .

Fixpoint map ı (f : A -> B) (xs : List<ı> A) : List<ı> B := match xs with nil => nil cons h t => cons (f h) (map f t) end. Check map. map : ∀ ı. (A -> B) -> List<ı> A -> List<ı> B. Fixpoint ntail ı A (x : nat<ı>) : List A → List A := ... Check ntail.

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 23 / 26

slide-72
SLIDE 72

In a future Coq version . . .

Fixpoint map ı (f : A -> B) (xs : List<ı> A) : List<ı> B := match xs with nil => nil cons h t => cons (f h) (map f t) end. Check map. map : ∀ ı. (A -> B) -> List<ı> A -> List<ı> B. Fixpoint ntail ı A (x : nat<ı>) : List A → List A := ... Check ntail. ntail : ∀ ı ∀ . forall A, nat<ı> -> List<> A -> List<> A.

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 23 / 26

slide-73
SLIDE 73

In a future Coq version . . .

Fixpoint map ı (f : A -> B) (xs : List<ı> A) : List<ı> B := match xs with nil => nil cons h t => cons (f h) (map f t) end. Check map. map : ∀ ı. (A -> B) -> List<ı> A -> List<ı> B. Fixpoint ntail ı A (x : nat<ı>) : List A → List A := ... Check ntail. ntail : ∀ ı ∀ 1 ∀ 2. 2 ⊑ 1 => forall A, nat<ı> -> List<1> A -> List<2> A.

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 23 / 26

slide-74
SLIDE 74

Summary

Keep extending the guard condition is not sustainable Time is right to rethink termination checking in Coq Sized types seem to be an ideal candidate

◮ More expressive ◮ Compositional ◮ Easier to study and implement Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 24 / 26

slide-75
SLIDE 75

Summary

Keep extending the guard condition is not sustainable Time is right to rethink termination checking in Coq Sized types seem to be an ideal candidate

◮ More expressive ◮ Compositional ◮ Easier to study and implement

Project to start at CMU-Q in September

◮ Careful design before implementation Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 24 / 26

slide-76
SLIDE 76

Summary

Keep extending the guard condition is not sustainable Time is right to rethink termination checking in Coq Sized types seem to be an ideal candidate

◮ More expressive ◮ Compositional ◮ Easier to study and implement

Project to start at CMU-Q in September

◮ Careful design before implementation

Is this an opportunity to rethink coinduction in Coq?

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 24 / 26

slide-77
SLIDE 77

Summary

Keep extending the guard condition is not sustainable Time is right to rethink termination checking in Coq Sized types seem to be an ideal candidate

◮ More expressive ◮ Compositional ◮ Easier to study and implement

Project to start at CMU-Q in September

◮ Careful design before implementation

Is this an opportunity to rethink coinduction in Coq?

Thank you!

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 24 / 26

slide-78
SLIDE 78

A note on coinduction with dependent types

Coinduction in Coq is broken: it does not satisfy type preservation The problem: cofixpoint unfolding is only allowed inside case analysis case (cofix f := M) of . . . → case M[f := (cofix f := M)] of . . . Already observed by Gim´ enez in 1996 Some promising ideas: OTT (McBride) and copatterns (Abel et al.)

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 25 / 26

slide-79
SLIDE 79

A note on coinduction with dependent types

Example: consider a co-inductive type U with only one costructor in : U → U u : U force : U → U u def = cofix u := in u force def = λx.case x of in x′ ⇒ in x′ We can prove that x = force x for any x : U eq : Πx : U.x = force x eq def = λx.case x of in x′ ⇒ refl Then, eq u : u = force u,

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 26 / 26

slide-80
SLIDE 80

A note on coinduction with dependent types

Example: consider a co-inductive type U with only one costructor in : U → U u : U force : U → U u def = cofix u := in u force def = λx.case x of in x′ ⇒ in x′ We can prove that x = force x for any x : U eq : Πx : U.x = force x eq def = λx.case x of in x′ ⇒ refl Then, eq u : u = in u,

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 26 / 26

slide-81
SLIDE 81

A note on coinduction with dependent types

Example: consider a co-inductive type U with only one costructor in : U → U u : U force : U → U u def = cofix u := in u force def = λx.case x of in x′ ⇒ in x′ We can prove that x = force x for any x : U eq : Πx : U.x = force x eq def = λx.case x of in x′ ⇒ refl Then, eq u : u = in u, and eq u →∗ refl

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 26 / 26

slide-82
SLIDE 82

A note on coinduction with dependent types

Example: consider a co-inductive type U with only one costructor in : U → U u : U force : U → U u def = cofix u := in u force def = λx.case x of in x′ ⇒ in x′ We can prove that x = force x for any x : U eq : Πx : U.x = force x eq def = λx.case x of in x′ ⇒ refl Then, eq u : u = in u, and eq u →∗ refl But refl : u = u

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 26 / 26

slide-83
SLIDE 83

A note on coinduction with dependent types

Example: consider a co-inductive type U with only one costructor in : U → U u : U force : U → U u def = cofix u := in u force def = λx.case x of in x′ ⇒ in x′ We can prove that x = force x for any x : U eq : Πx : U.x = force x eq def = λx.case x of in x′ ⇒ refl Then, eq u : u = in u, and eq u →∗ refl But refl : u = u The types u = u and u = in u are not convertible since there is no case forcing the unfolding of u.

Bruno Barras, Jorge Luis Sacchini Type-Based Methods for Termination and Productivity in Coq 26 / 26