84
NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion 1

NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

NICTA Advanced Course

Theorem Proving

Principles, Techniques, Applications

Recursion

1

Page 2: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

CONTENT

➜ Intro & motivation, getting started with Isabelle

➜ Foundations & Principles

• Lambda Calculus

• Higher Order Logic, natural deduction

• Term rewriting

➜ Proof & Specification Techniques

• Inductively defined sets, rule induction

• Datatypes, recursion, induction

• Calculational reasoning, mathematics style proofs

• Hoare logic, proofs about programs

CONTENT 2

Page 3: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

LAST TIME

➜ Sets in Isabelle

➜ Inductive Definitions

➜ Rule induction

➜ Fixpoints

➜ Isar: induct and cases

LAST TIME 3

Page 4: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

LAST TIME

➜ Sets in Isabelle

➜ Inductive Definitions

➜ Rule induction

➜ Fixpoints

➜ Isar: induct and cases

LAST TIME 3-A

Page 5: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

LAST TIME

➜ Sets in Isabelle

➜ Inductive Definitions

➜ Rule induction

➜ Fixpoints

➜ Isar: induct and cases

LAST TIME 3-B

Page 6: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

LAST TIME

➜ Sets in Isabelle

➜ Inductive Definitions

➜ Rule induction

➜ Fixpoints

➜ Isar: induct and cases

LAST TIME 3-C

Page 7: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

LAST TIME

➜ Sets in Isabelle

➜ Inductive Definitions

➜ Rule induction

➜ Fixpoints

➜ Isar: induct and cases

LAST TIME 3-D

Page 8: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DATATYPES

Example:datatype ’a list = Nil | Cons ’a ”’a list”

Properties:

➜ Constructors:

Nil :: ’a list

Cons :: ’a ⇒ ’a list ⇒ ’a list

➜ Distinctness: Nil 6= Cons x xs

➜ Injectivity: (Cons x xs = Cons y ys) = (x = y ∧ xs = ys)

DATATYPES 4

Page 9: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DATATYPES

Example:datatype ’a list = Nil | Cons ’a ”’a list”

Properties:

➜ Constructors:

Nil :: ’a list

Cons :: ’a ⇒ ’a list ⇒ ’a list

➜ Distinctness: Nil 6= Cons x xs

➜ Injectivity: (Cons x xs = Cons y ys) = (x = y ∧ xs = ys)

DATATYPES 4-A

Page 10: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DATATYPES

Example:datatype ’a list = Nil | Cons ’a ”’a list”

Properties:

➜ Constructors:

Nil :: ’a list

Cons :: ’a ⇒ ’a list ⇒ ’a list

➜ Distinctness: Nil 6= Cons x xs

➜ Injectivity: (Cons x xs = Cons y ys) = (x = y ∧ xs = ys)

DATATYPES 4-B

Page 11: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DATATYPES

Example:datatype ’a list = Nil | Cons ’a ”’a list”

Properties:

➜ Constructors:

Nil :: ’a list

Cons :: ’a ⇒ ’a list ⇒ ’a list

➜ Distinctness: Nil 6= Cons x xs

➜ Injectivity: (Cons x xs = Cons y ys) = (x = y ∧ xs = ys)

DATATYPES 4-C

Page 12: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

THE GENERAL CASE

datatype (α1, . . . , αn) τ = C1 τ1,1 . . . τ1,n1

| . . .| Ck τk,1 . . . τk,nk

➜ Constructors: Ci :: τi,1 ⇒ . . . ⇒ τi,ni⇒ (α1, . . . , αn) τ

➜ Distinctness: Ci . . . 6= Cj . . . if i 6= j

➜ Injectivity: (Ci x1 . . . xni= Ci y1 . . . yni

) = (x1 = y1 ∧ . . . ∧ xni= yni

)

Distinctness and Injectivity applied automatically

THE GENERAL CASE 5

Page 13: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

THE GENERAL CASE

datatype (α1, . . . , αn) τ = C1 τ1,1 . . . τ1,n1

| . . .| Ck τk,1 . . . τk,nk

➜ Constructors: Ci :: τi,1 ⇒ . . . ⇒ τi,ni⇒ (α1, . . . , αn) τ

➜ Distinctness: Ci . . . 6= Cj . . . if i 6= j

➜ Injectivity: (Ci x1 . . . xni= Ci y1 . . . yni

) = (x1 = y1 ∧ . . . ∧ xni= yni

)

Distinctness and Injectivity applied automatically

THE GENERAL CASE 5-A

Page 14: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

THE GENERAL CASE

datatype (α1, . . . , αn) τ = C1 τ1,1 . . . τ1,n1

| . . .| Ck τk,1 . . . τk,nk

➜ Constructors: Ci :: τi,1 ⇒ . . . ⇒ τi,ni⇒ (α1, . . . , αn) τ

➜ Distinctness: Ci . . . 6= Cj . . . if i 6= j

➜ Injectivity: (Ci x1 . . . xni= Ci y1 . . . yni

) = (x1 = y1 ∧ . . . ∧ xni= yni

)

Distinctness and Injectivity applied automatically

THE GENERAL CASE 5-B

Page 15: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

THE GENERAL CASE

datatype (α1, . . . , αn) τ = C1 τ1,1 . . . τ1,n1

| . . .| Ck τk,1 . . . τk,nk

➜ Constructors: Ci :: τi,1 ⇒ . . . ⇒ τi,ni⇒ (α1, . . . , αn) τ

➜ Distinctness: Ci . . . 6= Cj . . . if i 6= j

➜ Injectivity: (Ci x1 . . . xni= Ci y1 . . . yni

) = (x1 = y1 ∧ . . . ∧ xni= yni

)

Distinctness and Injectivity applied automatically

THE GENERAL CASE 5-C

Page 16: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

THE GENERAL CASE

datatype (α1, . . . , αn) τ = C1 τ1,1 . . . τ1,n1

| . . .| Ck τk,1 . . . τk,nk

➜ Constructors: Ci :: τi,1 ⇒ . . . ⇒ τi,ni⇒ (α1, . . . , αn) τ

➜ Distinctness: Ci . . . 6= Cj . . . if i 6= j

➜ Injectivity: (Ci x1 . . . xni= Ci y1 . . . yni

) = (x1 = y1 ∧ . . . ∧ xni= yni

)

Distinctness and Injectivity applied automatically

THE GENERAL CASE 5-D

Page 17: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

HOW IS THIS TYPE DEFINED?

datatype ’a list = Nil | Cons ’a ”’a list”

➜ internally defined using typedef

➜ hence: describes a set

➜ set = trees with constructors as nodes

➜ inductive definition to characterize which trees belong to datatype

More detail: Datatype Universe.thy

HOW IS THIS TYPE DEFINED? 6

Page 18: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

HOW IS THIS TYPE DEFINED?

datatype ’a list = Nil | Cons ’a ”’a list”

➜ internally defined using typedef

➜ hence: describes a set

➜ set = trees with constructors as nodes

➜ inductive definition to characterize which trees belong to datatype

More detail: Datatype Universe.thy

HOW IS THIS TYPE DEFINED? 6-A

Page 19: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

HOW IS THIS TYPE DEFINED?

datatype ’a list = Nil | Cons ’a ”’a list”

➜ internally defined using typedef

➜ hence: describes a set

➜ set = trees with constructors as nodes

➜ inductive definition to characterize which trees belong to datatype

More detail: Datatype Universe.thy

HOW IS THIS TYPE DEFINED? 6-B

Page 20: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

HOW IS THIS TYPE DEFINED?

datatype ’a list = Nil | Cons ’a ”’a list”

➜ internally defined using typedef

➜ hence: describes a set

➜ set = trees with constructors as nodes

➜ inductive definition to characterize which trees belong to datatype

More detail: Datatype Universe.thy

HOW IS THIS TYPE DEFINED? 6-C

Page 21: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

HOW IS THIS TYPE DEFINED?

datatype ’a list = Nil | Cons ’a ”’a list”

➜ internally defined using typedef

➜ hence: describes a set

➜ set = trees with constructors as nodes

➜ inductive definition to characterize which trees belong to datatype

More detail: Datatype Universe.thy

HOW IS THIS TYPE DEFINED? 6-D

Page 22: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DATATYPE LIMITATIONS

Must be definable as set.

➜ Infinitely branching ok.

➜ Mutually recursive ok.

➜ Stricly positive (left of function arrow) occurence ok.

Not ok:

datatype t = C (t ⇒ bool)| D ((bool ⇒ t) ⇒ bool)| E ((t ⇒ bool) ⇒ bool)

Because: Cantor’s theorem (α set is larger than α)

DATATYPE LIMITATIONS 7

Page 23: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DATATYPE LIMITATIONS

Must be definable as set.

➜ Infinitely branching ok.

➜ Mutually recursive ok.

➜ Stricly positive (left of function arrow) occurence ok.

Not ok:

datatype t = C (t ⇒ bool)| D ((bool ⇒ t) ⇒ bool)| E ((t ⇒ bool) ⇒ bool)

Because: Cantor’s theorem (α set is larger than α)

DATATYPE LIMITATIONS 7-A

Page 24: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DATATYPE LIMITATIONS

Must be definable as set.

➜ Infinitely branching ok.

➜ Mutually recursive ok.

➜ Stricly positive (left of function arrow) occurence ok.

Not ok:

datatype t = C (t ⇒ bool)| D ((bool ⇒ t) ⇒ bool)| E ((t ⇒ bool) ⇒ bool)

Because: Cantor’s theorem (α set is larger than α)

DATATYPE LIMITATIONS 7-B

Page 25: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DATATYPE LIMITATIONS

Must be definable as set.

➜ Infinitely branching ok.

➜ Mutually recursive ok.

➜ Stricly positive (left of function arrow) occurence ok.

Not ok:

datatype t = C (t ⇒ bool)| D ((bool ⇒ t) ⇒ bool)| E ((t ⇒ bool) ⇒ bool)

Because: Cantor’s theorem (α set is larger than α)

DATATYPE LIMITATIONS 7-C

Page 26: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DATATYPE LIMITATIONS

Must be definable as set.

➜ Infinitely branching ok.

➜ Mutually recursive ok.

➜ Stricly positive (left of function arrow) occurence ok.

Not ok:

datatype t = C (t ⇒ bool)

| D ((bool ⇒ t) ⇒ bool)| E ((t ⇒ bool) ⇒ bool)

Because: Cantor’s theorem (α set is larger than α)

DATATYPE LIMITATIONS 7-D

Page 27: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DATATYPE LIMITATIONS

Must be definable as set.

➜ Infinitely branching ok.

➜ Mutually recursive ok.

➜ Stricly positive (left of function arrow) occurence ok.

Not ok:

datatype t = C (t ⇒ bool)| D ((bool ⇒ t) ⇒ bool)

| E ((t ⇒ bool) ⇒ bool)

Because: Cantor’s theorem (α set is larger than α)

DATATYPE LIMITATIONS 7-E

Page 28: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DATATYPE LIMITATIONS

Must be definable as set.

➜ Infinitely branching ok.

➜ Mutually recursive ok.

➜ Stricly positive (left of function arrow) occurence ok.

Not ok:

datatype t = C (t ⇒ bool)| D ((bool ⇒ t) ⇒ bool)| E ((t ⇒ bool) ⇒ bool)

Because: Cantor’s theorem (α set is larger than α)

DATATYPE LIMITATIONS 7-F

Page 29: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

CASE

Every datatype introduces a case construct, e.g.

(case xs of [] ⇒ . . . | y #ys ⇒ ... y ... ys ...)

In general: one case per constructor

➜ Same order of cases as in datatype

➜ No nested patterns (e.g. x#y#zs)

(But nested cases)

➜ Needs () in context

CASE 8

Page 30: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

CASE

Every datatype introduces a case construct, e.g.

(case xs of [] ⇒ . . . | y #ys ⇒ ... y ... ys ...)

In general: one case per constructor

➜ Same order of cases as in datatype

➜ No nested patterns (e.g. x#y#zs)

(But nested cases)

➜ Needs () in context

CASE 8-A

Page 31: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

CASE

Every datatype introduces a case construct, e.g.

(case xs of [] ⇒ . . . | y #ys ⇒ ... y ... ys ...)

In general: one case per constructor

➜ Same order of cases as in datatype

➜ No nested patterns (e.g. x#y#zs)

(But nested cases)

➜ Needs () in context

CASE 8-B

Page 32: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

CASE

Every datatype introduces a case construct, e.g.

(case xs of [] ⇒ . . . | y #ys ⇒ ... y ... ys ...)

In general: one case per constructor

➜ Same order of cases as in datatype

➜ No nested patterns (e.g. x#y#zs)

(But nested cases)

➜ Needs () in context

CASE 8-C

Page 33: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

CASE

Every datatype introduces a case construct, e.g.

(case xs of [] ⇒ . . . | y #ys ⇒ ... y ... ys ...)

In general: one case per constructor

➜ Same order of cases as in datatype

➜ No nested patterns (e.g. x#y#zs)

(But nested cases)

➜ Needs () in context

CASE 8-D

Page 34: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

CASE

Every datatype introduces a case construct, e.g.

(case xs of [] ⇒ . . . | y #ys ⇒ ... y ... ys ...)

In general: one case per constructor

➜ Same order of cases as in datatype

➜ No nested patterns (e.g. x#y#zs)

(But nested cases)

➜ Needs () in context

CASE 8-E

Page 35: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

CASES

apply (case tac t)

creates k subgoals

[[t = Ci x1 . . . xp; . . . ]] =⇒ . . .

one for each constructor Ci

CASES 9

Page 36: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

CASES

apply (case tac t)

creates k subgoals

[[t = Ci x1 . . . xp; . . . ]] =⇒ . . .

one for each constructor Ci

CASES 9-A

Page 37: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DEMO

10

Page 38: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

RECURSION

11

Page 39: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

WHY NONTERMINATION CAN BE HARMFUL

How about f x = f x + 1?

Subtract f x on both sides.

=⇒0 = 1

! All functions in HOL must be total !

WHY NONTERMINATION CAN BE HARMFUL 12

Page 40: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

WHY NONTERMINATION CAN BE HARMFUL

How about f x = f x + 1?

Subtract f x on both sides.

=⇒0 = 1

! All functions in HOL must be total !

WHY NONTERMINATION CAN BE HARMFUL 12-A

Page 41: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

WHY NONTERMINATION CAN BE HARMFUL

How about f x = f x + 1?

Subtract f x on both sides.

=⇒0 = 1

! All functions in HOL must be total !

WHY NONTERMINATION CAN BE HARMFUL 12-B

Page 42: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

WHY NONTERMINATION CAN BE HARMFUL

How about f x = f x + 1?

Subtract f x on both sides.

=⇒0 = 1

! All functions in HOL must be total !

WHY NONTERMINATION CAN BE HARMFUL 12-C

Page 43: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

PRIMITIVE RECURSION

primrec guarantees termination structurally

Example primrec def:

consts app :: ”’a list ⇒ ’a list ⇒ ’a list”

primrec

”app Nil ys = ys”

”app (Cons x xs) ys = Cons x (app xs ys)”

PRIMITIVE RECURSION 13

Page 44: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

PRIMITIVE RECURSION

primrec guarantees termination structurally

Example primrec def:

consts app :: ”’a list ⇒ ’a list ⇒ ’a list”

primrec

”app Nil ys = ys”

”app (Cons x xs) ys = Cons x (app xs ys)”

PRIMITIVE RECURSION 13-A

Page 45: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

THE GENERAL CASE

If τ is a datatype (with constructors C1, . . . , Ck) then f :: τ ⇒ τ ′ canbe defined by primitive recursion:

f (C1 y1,1 . . . y1,n1) = r1

...

f (Ck yk,1 . . . yk,nk) = rk

The recursive calls in ri must be structurally smaller(of the form f a1 . . . yi,j . . . ap)

THE GENERAL CASE 14

Page 46: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

THE GENERAL CASE

If τ is a datatype (with constructors C1, . . . , Ck) then f :: τ ⇒ τ ′ canbe defined by primitive recursion:

f (C1 y1,1 . . . y1,n1) = r1

...

f (Ck yk,1 . . . yk,nk) = rk

The recursive calls in ri must be structurally smaller(of the form f a1 . . . yi,j . . . ap)

THE GENERAL CASE 14-A

Page 47: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

HOW DOES THIS WORK?primrec just fancy syntax for a recursion operator

Example:

list rec :: ”’b ⇒ (’a ⇒ ’a list ⇒ ’b ⇒ ’b) ⇒ ’a list ⇒ ’b”list rec f1 f2 Nil = f1

list rec f1 f2 (Cons x xs) = f2 x xs (list rec f1 f2 xs)

app ≡ list rec (λys. ys) (λx xs xs′. λys. Cons x (xs′ ys))

Defined: automatically, first inductively (set), then by epsilon

(Nil, f1) ∈ list rel f1 f2

(xs, xs′) ∈ list rel f1 f2

(Cons x xs, f2 x xs xs′) ∈ list rel f1 f2

list rec f1 f2 xs ≡ SOME y. (xs, y) ∈ list rel f1 f2

HOW DOES THIS WORK? 15

Page 48: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

HOW DOES THIS WORK?primrec just fancy syntax for a recursion operator

Example:

list rec :: ”’b ⇒ (’a ⇒ ’a list ⇒ ’b ⇒ ’b) ⇒ ’a list ⇒ ’b”list rec f1 f2 Nil = f1

list rec f1 f2 (Cons x xs) = f2 x xs (list rec f1 f2 xs)

app ≡ list rec (λys. ys) (λx xs xs′. λys. Cons x (xs′ ys))

Defined: automatically, first inductively (set), then by epsilon

(Nil, f1) ∈ list rel f1 f2

(xs, xs′) ∈ list rel f1 f2

(Cons x xs, f2 x xs xs′) ∈ list rel f1 f2

list rec f1 f2 xs ≡ SOME y. (xs, y) ∈ list rel f1 f2

HOW DOES THIS WORK? 15-A

Page 49: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

HOW DOES THIS WORK?primrec just fancy syntax for a recursion operator

Example:

list rec :: ”’b ⇒ (’a ⇒ ’a list ⇒ ’b ⇒ ’b) ⇒ ’a list ⇒ ’b”list rec f1 f2 Nil = f1

list rec f1 f2 (Cons x xs) = f2 x xs (list rec f1 f2 xs)

app ≡ list rec (λys. ys) (λx xs xs′. λys. Cons x (xs′ ys))

Defined: automatically, first inductively (set), then by epsilon

(Nil, f1) ∈ list rel f1 f2

(xs, xs′) ∈ list rel f1 f2

(Cons x xs, f2 x xs xs′) ∈ list rel f1 f2

list rec f1 f2 xs ≡ SOME y. (xs, y) ∈ list rel f1 f2

HOW DOES THIS WORK? 15-B

Page 50: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

HOW DOES THIS WORK?primrec just fancy syntax for a recursion operator

Example:

list rec :: ”’b ⇒ (’a ⇒ ’a list ⇒ ’b ⇒ ’b) ⇒ ’a list ⇒ ’b”list rec f1 f2 Nil = f1

list rec f1 f2 (Cons x xs) = f2 x xs (list rec f1 f2 xs)

app ≡ list rec (λys. ys) (λx xs xs′. λys. Cons x (xs′ ys))

Defined: automatically, first inductively (set), then by epsilon

(Nil, f1) ∈ list rel f1 f2

(xs, xs′) ∈ list rel f1 f2

(Cons x xs, f2 x xs xs′) ∈ list rel f1 f2

list rec f1 f2 xs ≡ SOME y. (xs, y) ∈ list rel f1 f2

HOW DOES THIS WORK? 15-C

Page 51: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

HOW DOES THIS WORK?primrec just fancy syntax for a recursion operator

Example:

list rec :: ”’b ⇒ (’a ⇒ ’a list ⇒ ’b ⇒ ’b) ⇒ ’a list ⇒ ’b”list rec f1 f2 Nil = f1

list rec f1 f2 (Cons x xs) = f2 x xs (list rec f1 f2 xs)

app ≡ list rec (λys. ys) (λx xs xs′. λys. Cons x (xs′ ys))

Defined: automatically, first inductively (set), then by epsilon

(Nil, f1) ∈ list rel f1 f2

(xs, xs′) ∈ list rel f1 f2

(Cons x xs, f2 x xs xs′) ∈ list rel f1 f2

list rec f1 f2 xs ≡ SOME y. (xs, y) ∈ list rel f1 f2

HOW DOES THIS WORK? 15-D

Page 52: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

PREDEFINED DATATYPES

16

Page 53: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

NAT IS A DATATYPE

datatype nat = 0 | Suc nat

Functions on nat definable by primrec!

primrec

f 0 = ...

f (Suc n) = ... f n ...

NAT IS A DATATYPE 17

Page 54: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

NAT IS A DATATYPE

datatype nat = 0 | Suc nat

Functions on nat definable by primrec!

primrec

f 0 = ...

f (Suc n) = ... f n ...

NAT IS A DATATYPE 17-A

Page 55: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

OPTION

datatype ’a option = None | Some ’a

Important application:

’b ⇒ ’a option ∼ partial function:

None ∼ no resultSome a ∼ result a

Example:consts lookup :: ’k ⇒ (’k × ’v) list ⇒ ’v optionprimrec

lookup k [] = None

lookup k (x #xs) = (if fst x = k then Some (snd x) else lookup k xs)

OPTION 18

Page 56: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

OPTION

datatype ’a option = None | Some ’a

Important application:

’b ⇒ ’a option ∼ partial function:

None ∼ no resultSome a ∼ result a

Example:consts lookup :: ’k ⇒ (’k × ’v) list ⇒ ’v optionprimrec

lookup k [] = None

lookup k (x #xs) = (if fst x = k then Some (snd x) else lookup k xs)

OPTION 18-A

Page 57: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

OPTION

datatype ’a option = None | Some ’a

Important application:

’b ⇒ ’a option ∼ partial function:

None ∼ no resultSome a ∼ result a

Example:consts lookup :: ’k ⇒ (’k × ’v) list ⇒ ’v optionprimrec

lookup k [] = None

lookup k (x #xs) =

(if fst x = k then Some (snd x) else lookup k xs)

OPTION 18-B

Page 58: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

OPTION

datatype ’a option = None | Some ’a

Important application:

’b ⇒ ’a option ∼ partial function:

None ∼ no resultSome a ∼ result a

Example:consts lookup :: ’k ⇒ (’k × ’v) list ⇒ ’v optionprimrec

lookup k [] = None

lookup k (x #xs) = (if fst x = k then Some (snd x) else lookup k xs)

OPTION 18-C

Page 59: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DEMO: PRIMREC

19

Page 60: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

INDUCTION

20

Page 61: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

STRUCTURAL INDUCTION

P xs holds for all lists xs if

➜ P Nil

➜ and for arbitrary x and xs, P xs =⇒ P (x#xs)

Induction theorem list.induct:[[P [];

V

a list. P list =⇒ P (a#list)]] =⇒ P list

➜ General proof method for induction: (induct x)

• x must be a free variable in the first subgoal.

• type of x must be a datatype.

STRUCTURAL INDUCTION 21

Page 62: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

STRUCTURAL INDUCTION

P xs holds for all lists xs if

➜ P Nil

➜ and for arbitrary x and xs, P xs =⇒ P (x#xs)

Induction theorem list.induct:[[P [];

V

a list. P list =⇒ P (a#list)]] =⇒ P list

➜ General proof method for induction: (induct x)

• x must be a free variable in the first subgoal.

• type of x must be a datatype.

STRUCTURAL INDUCTION 21-A

Page 63: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

STRUCTURAL INDUCTION

P xs holds for all lists xs if

➜ P Nil

➜ and for arbitrary x and xs, P xs =⇒ P (x#xs)

Induction theorem list.induct:[[P [];

V

a list. P list =⇒ P (a#list)]] =⇒ P list

➜ General proof method for induction: (induct x)

• x must be a free variable in the first subgoal.

• type of x must be a datatype.

STRUCTURAL INDUCTION 21-B

Page 64: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

BASIC HEURISTICS

Theorems about recursive functions are proved by induction

Induction on argument number i of f

if f is defined by recursion on argument number i

BASIC HEURISTICS 22

Page 65: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

EXAMPLE

A tail recursive list reverse:

consts itrev :: ’a list ⇒ ’a list ⇒ ’a list

primrec

itrev [] ys =

ys

itrev (x#xs) ys = itrev xs (x#ys)

lemma itrev xs [] = rev xs

EXAMPLE 23

Page 66: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

EXAMPLE

A tail recursive list reverse:

consts itrev :: ’a list ⇒ ’a list ⇒ ’a list

primrec

itrev [] ys = ys

itrev (x#xs) ys =

itrev xs (x#ys)

lemma itrev xs [] = rev xs

EXAMPLE 23-A

Page 67: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

EXAMPLE

A tail recursive list reverse:

consts itrev :: ’a list ⇒ ’a list ⇒ ’a list

primrec

itrev [] ys = ys

itrev (x#xs) ys = itrev xs (x#ys)

lemma itrev xs [] = rev xs

EXAMPLE 23-B

Page 68: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

EXAMPLE

A tail recursive list reverse:

consts itrev :: ’a list ⇒ ’a list ⇒ ’a list

primrec

itrev [] ys = ys

itrev (x#xs) ys = itrev xs (x#ys)

lemma itrev xs [] = rev xs

EXAMPLE 23-C

Page 69: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DEMO: PROOF ATTEMPT

24

Page 70: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

GENERALISATION

Replace constants by variables

lemma itrev xs ys = rev xs@ys

Quantify free variables by ∀

(except the induction variable)

lemma ∀ys. itrev xs ys = rev xs@ys

GENERALISATION 25

Page 71: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

GENERALISATION

Replace constants by variables

lemma itrev xs ys = rev xs@ys

Quantify free variables by ∀

(except the induction variable)

lemma ∀ys. itrev xs ys = rev xs@ys

GENERALISATION 25-A

Page 72: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

GENERALISATION

Replace constants by variables

lemma itrev xs ys = rev xs@ys

Quantify free variables by ∀

(except the induction variable)

lemma ∀ys. itrev xs ys = rev xs@ys

GENERALISATION 25-B

Page 73: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

ISAR

26

Page 74: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DATATYPE CASE DISTINCTION

proof (cases term)case Constructor1...

next...next

case (Constructork ~x)· · · ~x · · ·

qed

case (Constructori ~x) ≡

fix ~x assume Constructori : ”term = Constructori ~x”

DATATYPE CASE DISTINCTION 27

Page 75: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DATATYPE CASE DISTINCTION

proof (cases term)case Constructor1...

next...next

case (Constructork ~x)· · · ~x · · ·

qed

case (Constructori ~x) ≡

fix ~x assume Constructori : ”term = Constructori ~x”

DATATYPE CASE DISTINCTION 27-A

Page 76: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

STRUCTURAL INDUCTION FOR TYPE NAT

show P n

proof (induct n)

case 0 ≡ let ?case = P 0

. . .

show ?case

next

case (Suc n) ≡ fix n assume Suc: P n

. . . let ?case = P (Suc n)

· · · n · · ·

show ?case

qed

STRUCTURAL INDUCTION FOR TYPE NAT 28

Page 77: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

STRUCTURAL INDUCTION WITH =⇒ AND∧

show ”∧

x. A n =⇒ P n”proof (induct n)

case 0 ≡ fix x assume 0: ”A 0”. . . let ?case = ”P 0”show ?case

nextcase (Suc n) ≡ fix n and x

. . . assume Suc: ”∧

x. A n =⇒ P n”· · · n · · · ”A (Suc n)”. . . let ?case = ”P (Suc n)”show ?case

qed

STRUCTURAL INDUCTION WITH =⇒ ANDV

29

Page 78: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

DEMO

30

Page 79: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

WE HAVE SEEN TODAY ...

➜ Datatypes

➜ Primite Recursion

➜ Case distinction

➜ Induction

WE HAVE SEEN TODAY ... 31

Page 80: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

WE HAVE SEEN TODAY ...

➜ Datatypes

➜ Primite Recursion

➜ Case distinction

➜ Induction

WE HAVE SEEN TODAY ... 31-A

Page 81: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

WE HAVE SEEN TODAY ...

➜ Datatypes

➜ Primite Recursion

➜ Case distinction

➜ Induction

WE HAVE SEEN TODAY ... 31-B

Page 82: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

WE HAVE SEEN TODAY ...

➜ Datatypes

➜ Primite Recursion

➜ Case distinction

➜ Induction

WE HAVE SEEN TODAY ... 31-C

Page 83: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

EXERCISES

➜ look at http://isabelle.in.tum.de/library/HOL/Datatype_Universe.html

➜ define a primitive recursive function listsum :: nat list ⇒ natthat returns the sum of the elements in a list.

➜ show ”2 ∗ listsum [0..n] = n ∗ (n + 1)”

➜ show ”listsum (replicate n a) = n ∗ a”

➜ define a function listsumT using a tail recursive version of listsum.

➜ show that the two functions are equivalent: listsum xs = listsumT xs

EXERCISES 32

Page 84: NICTA Advanced Course Theorem Proving Principles, Techniques, Applications Recursion · 2004-08-31 · Datatypes, recursion, induction Calculational reasoning, mathematics style proofs

NEXT LECTURE

Nicolas Magaud

on

The Coq System

Monday 15:00 – 16:30

NEXT LECTURE 33