Constructive Proofs and Program Extraction Christoph Kreitz 1. Type - - PowerPoint PPT Presentation

constructive proofs and program extraction
SMART_READER_LITE
LIVE PREVIEW

Constructive Proofs and Program Extraction Christoph Kreitz 1. Type - - PowerPoint PPT Presentation

Constructive Proofs and Program Extraction Christoph Kreitz 1. Type Theory vs. Set Theory 2. Overview of the Nuprl System 3. Proofs of the Integer Square Root Problem What distinguishes Type Theory from Set Theory? n r r 2 n


slide-1
SLIDE 1

Constructive Proofs and Program Extraction

Christoph Kreitz

  • 1. Type Theory vs. Set Theory
  • 2. Overview of the Nuprl System
  • 3. Proofs of the Integer Square Root Problem
slide-2
SLIDE 2

PRL Seminar, February 23, 2004 1 Constructive Proofs and Program Extraction

What distinguishes Type Theory from Set Theory? What is the meaning of ∀n ∃r r2≤n ∧ n<(r+1)2

slide-3
SLIDE 3

PRL Seminar, February 23, 2004 1 Constructive Proofs and Program Extraction

What distinguishes Type Theory from Set Theory? What is the meaning of ∀n ∃r r2≤n ∧ n<(r+1)2

  • Set Theory

– Integer square roots exist for all natural numbers

slide-4
SLIDE 4

PRL Seminar, February 23, 2004 1 Constructive Proofs and Program Extraction

What distinguishes Type Theory from Set Theory? What is the meaning of ∀n ∃r r2≤n ∧ n<(r+1)2

  • Set Theory

– Integer square roots exist for all natural numbers – A proof shows that the notion ⌊√n⌋ is well-defined

slide-5
SLIDE 5

PRL Seminar, February 23, 2004 1 Constructive Proofs and Program Extraction

What distinguishes Type Theory from Set Theory? What is the meaning of ∀n ∃r r2≤n ∧ n<(r+1)2

  • Set Theory

– Integer square roots exist for all natural numbers – A proof shows that the notion ⌊√n⌋ is well-defined – Different proofs lead to the same insight

slide-6
SLIDE 6

PRL Seminar, February 23, 2004 1 Constructive Proofs and Program Extraction

What distinguishes Type Theory from Set Theory? What is the meaning of ∀n ∃r r2≤n ∧ n<(r+1)2

  • Set Theory

– Integer square roots exist for all natural numbers – A proof shows that the notion ⌊√n⌋ is well-defined – Different proofs lead to the same insight

  • Type Theory

– There is a method to construct integer square roots

slide-7
SLIDE 7

PRL Seminar, February 23, 2004 1 Constructive Proofs and Program Extraction

What distinguishes Type Theory from Set Theory? What is the meaning of ∀n ∃r r2≤n ∧ n<(r+1)2

  • Set Theory

– Integer square roots exist for all natural numbers – A proof shows that the notion ⌊√n⌋ is well-defined – Different proofs lead to the same insight

  • Type Theory

– There is a method to construct integer square roots – Algorithms for computing ⌊√n⌋ can be extracted from proofs

slide-8
SLIDE 8

PRL Seminar, February 23, 2004 1 Constructive Proofs and Program Extraction

What distinguishes Type Theory from Set Theory? What is the meaning of ∀n ∃r r2≤n ∧ n<(r+1)2

  • Set Theory

– Integer square roots exist for all natural numbers – A proof shows that the notion ⌊√n⌋ is well-defined – Different proofs lead to the same insight

  • Type Theory

– There is a method to construct integer square roots – Algorithms for computing ⌊√n⌋ can be extracted from proofs – Different proofs lead to different algorithms

slide-9
SLIDE 9

PRL Seminar, February 23, 2004 2 Constructive Proofs and Program Extraction

Proofs for ∀n ∃r r2≤n ∧ n<(r+1)2

  • Nonconstructive

– Assume r wouldn’t exist for some n

slide-10
SLIDE 10

PRL Seminar, February 23, 2004 2 Constructive Proofs and Program Extraction

Proofs for ∀n ∃r r2≤n ∧ n<(r+1)2

  • Nonconstructive

– Assume r wouldn’t exist for some n – Then for all r: r2 > n or (r+1)2≤n

slide-11
SLIDE 11

PRL Seminar, February 23, 2004 2 Constructive Proofs and Program Extraction

Proofs for ∀n ∃r r2≤n ∧ n<(r+1)2

  • Nonconstructive

– Assume r wouldn’t exist for some n – Then for all r: r2 > n or (r+1)2≤n – Consider r1 = min{r|r2 > n}-1

slide-12
SLIDE 12

PRL Seminar, February 23, 2004 2 Constructive Proofs and Program Extraction

Proofs for ∀n ∃r r2≤n ∧ n<(r+1)2

  • Nonconstructive

– Assume r wouldn’t exist for some n – Then for all r: r2 > n or (r+1)2≤n – Consider r1 = min{r|r2 > n}-1 – Then r2

1≤n and (r1+1)2 > n – a contradiction

slide-13
SLIDE 13

PRL Seminar, February 23, 2004 2 Constructive Proofs and Program Extraction

Proofs for ∀n ∃r r2≤n ∧ n<(r+1)2

  • Nonconstructive

– Assume r wouldn’t exist for some n – Then for all r: r2 > n or (r+1)2≤n – Consider r1 = min{r|r2 > n}-1 – Then r2

1≤n and (r1+1)2 > n – a contradiction

Unnecessary indirect approach - it masks a constructive argument

slide-14
SLIDE 14

PRL Seminar, February 23, 2004 2 Constructive Proofs and Program Extraction

Proofs for ∀n ∃r r2≤n ∧ n<(r+1)2

  • Nonconstructive

– Assume r wouldn’t exist for some n – Then for all r: r2 > n or (r+1)2≤n – Consider r1 = min{r|r2 > n}-1 – Then r2

1≤n and (r1+1)2 > n – a contradiction

Unnecessary indirect approach - it masks a constructive argument

  • Mathematical rigor: Induction on n
slide-15
SLIDE 15

PRL Seminar, February 23, 2004 2 Constructive Proofs and Program Extraction

Proofs for ∀n ∃r r2≤n ∧ n<(r+1)2

  • Nonconstructive

– Assume r wouldn’t exist for some n – Then for all r: r2 > n or (r+1)2≤n – Consider r1 = min{r|r2 > n}-1 – Then r2

1≤n and (r1+1)2 > n – a contradiction

Unnecessary indirect approach - it masks a constructive argument

  • Mathematical rigor: Induction on n

– Base Case: prove ∃r r2≤0 ∧ 0<(r+1)2

slide-16
SLIDE 16

PRL Seminar, February 23, 2004 2 Constructive Proofs and Program Extraction

Proofs for ∀n ∃r r2≤n ∧ n<(r+1)2

  • Nonconstructive

– Assume r wouldn’t exist for some n – Then for all r: r2 > n or (r+1)2≤n – Consider r1 = min{r|r2 > n}-1 – Then r2

1≤n and (r1+1)2 > n – a contradiction

Unnecessary indirect approach - it masks a constructive argument

  • Mathematical rigor: Induction on n

– Base Case: prove ∃r r2≤0 ∧ 0<(r+1)2 Choose r = 0, prove 02≤0 ∧ 0<(0+1)2 using standard arithmetic

slide-17
SLIDE 17

PRL Seminar, February 23, 2004 2 Constructive Proofs and Program Extraction

Proofs for ∀n ∃r r2≤n ∧ n<(r+1)2

  • Nonconstructive

– Assume r wouldn’t exist for some n – Then for all r: r2 > n or (r+1)2≤n – Consider r1 = min{r|r2 > n}-1 – Then r2

1≤n and (r1+1)2 > n – a contradiction

Unnecessary indirect approach - it masks a constructive argument

  • Mathematical rigor: Induction on n

– Base Case: prove ∃r r2≤0 ∧ 0<(r+1)2 Choose r = 0, prove 02≤0 ∧ 0<(0+1)2 using standard arithmetic – Step case: assume ∃rn r2≤n ∧ n<(rn+1)2 and prove ∃r r2≤n+1 ∧ n+1<(r+1)2

slide-18
SLIDE 18

PRL Seminar, February 23, 2004 2 Constructive Proofs and Program Extraction

Proofs for ∀n ∃r r2≤n ∧ n<(r+1)2

  • Nonconstructive

– Assume r wouldn’t exist for some n – Then for all r: r2 > n or (r+1)2≤n – Consider r1 = min{r|r2 > n}-1 – Then r2

1≤n and (r1+1)2 > n – a contradiction

Unnecessary indirect approach - it masks a constructive argument

  • Mathematical rigor: Induction on n

– Base Case: prove ∃r r2≤0 ∧ 0<(r+1)2 Choose r = 0, prove 02≤0 ∧ 0<(0+1)2 using standard arithmetic – Step case: assume ∃rn r2≤n ∧ n<(rn+1)2 and prove ∃r r2≤n+1 ∧ n+1<(r+1)2

✻ ✲

1 2 3 1 2 3 4 5 6 7 8 9

slide-19
SLIDE 19

PRL Seminar, February 23, 2004 2 Constructive Proofs and Program Extraction

Proofs for ∀n ∃r r2≤n ∧ n<(r+1)2

  • Nonconstructive

– Assume r wouldn’t exist for some n – Then for all r: r2 > n or (r+1)2≤n – Consider r1 = min{r|r2 > n}-1 – Then r2

1≤n and (r1+1)2 > n – a contradiction

Unnecessary indirect approach - it masks a constructive argument

  • Mathematical rigor: Induction on n

– Base Case: prove ∃r r2≤0 ∧ 0<(r+1)2 Choose r = 0, prove 02≤0 ∧ 0<(0+1)2 using standard arithmetic – Step case: assume ∃rn r2≤n ∧ n<(rn+1)2 and prove ∃r r2≤n+1 ∧ n+1<(r+1)2

✻ ✲

1 2 3 1 2 3 4 5 6 7 8 9 rn rn+1 n n+1 3

– If (rn+1)2≤n+1 then choose r = rn+1

slide-20
SLIDE 20

PRL Seminar, February 23, 2004 2 Constructive Proofs and Program Extraction

Proofs for ∀n ∃r r2≤n ∧ n<(r+1)2

  • Nonconstructive

– Assume r wouldn’t exist for some n – Then for all r: r2 > n or (r+1)2≤n – Consider r1 = min{r|r2 > n}-1 – Then r2

1≤n and (r1+1)2 > n – a contradiction

Unnecessary indirect approach - it masks a constructive argument

  • Mathematical rigor: Induction on n

– Base Case: prove ∃r r2≤0 ∧ 0<(r+1)2 Choose r = 0, prove 02≤0 ∧ 0<(0+1)2 using standard arithmetic – Step case: assume ∃rn r2≤n ∧ n<(rn+1)2 and prove ∃r r2≤n+1 ∧ n+1<(r+1)2

✻ ✲

1 2 3 1 2 3 4 5 6 7 8 9 rn+1 = rn n n+1 4

– If (rn+1)2≤n+1 then choose r = rn+1

  • therwise choose r = rn
slide-21
SLIDE 21

PRL Seminar, February 23, 2004 2 Constructive Proofs and Program Extraction

Proofs for ∀n ∃r r2≤n ∧ n<(r+1)2

  • Nonconstructive

– Assume r wouldn’t exist for some n – Then for all r: r2 > n or (r+1)2≤n – Consider r1 = min{r|r2 > n}-1 – Then r2

1≤n and (r1+1)2 > n – a contradiction

Unnecessary indirect approach - it masks a constructive argument

  • Mathematical rigor: Induction on n

– Base Case: prove ∃r r2≤0 ∧ 0<(r+1)2 Choose r = 0, prove 02≤0 ∧ 0<(0+1)2 using standard arithmetic – Step case: assume ∃rn r2≤n ∧ n<(rn+1)2 and prove ∃r r2≤n+1 ∧ n+1<(r+1)2

✻ ✲

1 2 3 1 2 3 4 5 6 7 8 9

– If (rn+1)2≤n+1 then choose r = rn+1

  • therwise choose r = rn

– Proof obligation follow using standard arithmetic

slide-22
SLIDE 22

PRL Seminar, February 23, 2004 2 Constructive Proofs and Program Extraction

Proofs for ∀n ∃r r2≤n ∧ n<(r+1)2

  • Nonconstructive

– Assume r wouldn’t exist for some n – Then for all r: r2 > n or (r+1)2≤n – Consider r1 = min{r|r2 > n}-1 – Then r2

1≤n and (r1+1)2 > n – a contradiction

Unnecessary indirect approach - it masks a constructive argument

  • Mathematical rigor: Induction on n

– Base Case: prove ∃r r2≤0 ∧ 0<(r+1)2 Choose r = 0, prove 02≤0 ∧ 0<(0+1)2 using standard arithmetic – Step case: assume ∃rn r2≤n ∧ n<(rn+1)2 and prove ∃r r2≤n+1 ∧ n+1<(r+1)2

✻ ✲

1 2 3 1 2 3 4 5 6 7 8 9

– If (rn+1)2≤n+1 then choose r = rn+1

  • therwise choose r = rn

– Proof obligation follow using standard arithmetic – Proof leads to algorithm that constructs ⌊√n⌋ inductively

slide-23
SLIDE 23

PRL Seminar, February 23, 2004 3 Constructive Proofs and Program Extraction

How to extract algorithms from proofs?

  • Use formal logic to express proof

– First-Order Logic + Induction + Basic Arithmetic ⊆ Type Theory – Proof rules tie proof steps to algorithm fragments

slide-24
SLIDE 24

PRL Seminar, February 23, 2004 3 Constructive Proofs and Program Extraction

How to extract algorithms from proofs?

  • Use formal logic to express proof

– First-Order Logic + Induction + Basic Arithmetic ⊆ Type Theory – Proof rules tie proof steps to algorithm fragments

  • Use computerized proof assistant to formalize proof

– Nuprl proof development system supports formal proofs in type theory – Use proof tactics to keep formalization “simple”

slide-25
SLIDE 25

PRL Seminar, February 23, 2004 3 Constructive Proofs and Program Extraction

How to extract algorithms from proofs?

  • Use formal logic to express proof

– First-Order Logic + Induction + Basic Arithmetic ⊆ Type Theory – Proof rules tie proof steps to algorithm fragments

  • Use computerized proof assistant to formalize proof

– Nuprl proof development system supports formal proofs in type theory – Use proof tactics to keep formalization “simple”

  • Extract algorithm from computerized proof

– Nuprl composes algorithm fragments of rules used in proof – Algorithm can be executed in Nuprl

slide-26
SLIDE 26

PRL Seminar, February 23, 2004 4 Constructive Proofs and Program Extraction

The Nuprl System Proof & program refinement in Type Theory

  • Interactive Proof Editor

❀ readable proofs

slide-27
SLIDE 27

PRL Seminar, February 23, 2004 4 Constructive Proofs and Program Extraction

The Nuprl System Proof & program refinement in Type Theory

  • Interactive Proof Editor

❀ readable proofs

  • Proof Tactics

❀ user-defined inferences

slide-28
SLIDE 28

PRL Seminar, February 23, 2004 4 Constructive Proofs and Program Extraction

The Nuprl System Proof & program refinement in Type Theory

  • Interactive Proof Editor

❀ readable proofs

  • Proof Tactics

❀ user-defined inferences

  • Decision Procedures

❀ proof automation

slide-29
SLIDE 29

PRL Seminar, February 23, 2004 4 Constructive Proofs and Program Extraction

The Nuprl System Proof & program refinement in Type Theory

  • Interactive Proof Editor

❀ readable proofs

  • Proof Tactics

❀ user-defined inferences

  • Decision Procedures

❀ proof automation

  • Flexible definition mechanism

❀ user-defined terms

slide-30
SLIDE 30

PRL Seminar, February 23, 2004 4 Constructive Proofs and Program Extraction

The Nuprl System Proof & program refinement in Type Theory

  • Interactive Proof Editor

❀ readable proofs

  • Proof Tactics

❀ user-defined inferences

  • Decision Procedures

❀ proof automation

  • Flexible definition mechanism

❀ user-defined terms

  • Customizable Term Display

❀ flexible notation

slide-31
SLIDE 31

PRL Seminar, February 23, 2004 4 Constructive Proofs and Program Extraction

The Nuprl System Proof & program refinement in Type Theory

  • Interactive Proof Editor

❀ readable proofs

  • Proof Tactics

❀ user-defined inferences

  • Decision Procedures

❀ proof automation

  • Flexible definition mechanism

❀ user-defined terms

  • Customizable Term Display

❀ flexible notation

  • Structure Editor for Terms

❀ no ambiguities

slide-32
SLIDE 32

PRL Seminar, February 23, 2004 4 Constructive Proofs and Program Extraction

The Nuprl System Proof & program refinement in Type Theory

  • Interactive Proof Editor

❀ readable proofs

  • Proof Tactics

❀ user-defined inferences

  • Decision Procedures

❀ proof automation

  • Flexible definition mechanism

❀ user-defined terms

  • Customizable Term Display

❀ flexible notation

  • Structure Editor for Terms

❀ no ambiguities

  • Library mechanism

❀ user-theories

– Large mathematical libraries and tactic collections

slide-33
SLIDE 33

PRL Seminar, February 23, 2004 4 Constructive Proofs and Program Extraction

The Nuprl System Proof & program refinement in Type Theory

  • Interactive Proof Editor

❀ readable proofs

  • Proof Tactics

❀ user-defined inferences

  • Decision Procedures

❀ proof automation

  • Flexible definition mechanism

❀ user-defined terms

  • Customizable Term Display

❀ flexible notation

  • Structure Editor for Terms

❀ no ambiguities

  • Library mechanism

❀ user-theories

– Large mathematical libraries and tactic collections

  • Program Extraction and Evaluation ❀ program synthesis
slide-34
SLIDE 34

PRL Seminar, February 23, 2004 5 Constructive Proofs and Program Extraction

Nuprl: System Architecture A Platform for Cooperating Reasoning Systems

GUI Evaluator Translator GUI GUI Evaluator Evaluator Evaluator Translator

Inference Engine Inference Engine Inference Engine Inference Engine Inference Engine

Java OCaml Maude MetaPRL SoS (Lisp) Structure

Library

Nuprl Refiner

MetaPRL JProver

PVS MinLog

Web Editor

Emacs Mode

PRL

(PVS) (HOL)

.... .... .... THEORY defs, thms, tactics rules, structure, code rules, structure, code rules, structure, code defs, thms, tactics defs, thms, tactics rules, structure, code rules, structure, code defs, thms, tactics rules, structure, code defs, thms, tactics defs, thms, tactics THEORY THEORY THEORY THEORY THEORY

Basic System uses Library, Editor, and Nuprl Refiner

slide-35
SLIDE 35

PRL Seminar, February 23, 2004 6 Constructive Proofs and Program Extraction

Tactics: User-defined inference rules

  • Meta-level programs built using

– Basic inference rules, standard tactics, predefined tacticals – Meta-level analysis of the proof goal and its context → Applying a tactic always results in a valid proof

slide-36
SLIDE 36

PRL Seminar, February 23, 2004 6 Constructive Proofs and Program Extraction

Tactics: User-defined inference rules

  • Meta-level programs built using

– Basic inference rules, standard tactics, predefined tacticals – Meta-level analysis of the proof goal and its context → Applying a tactic always results in a valid proof

  • Basic Tactics

→ Nuprl Manual

– Hypothesis: Prove ...C...⊢ C′ where C′ α-equal to C – D c: Decompose the outermost connective of clause c – EqD c: Decompose immediate subterms of an equality in clause c – EqTypeD c: Decompose type subterm of an equality in clause c – Assert t: Assert (or cut) term t as last hypothesis – Auto: Apply trivial reasoning, decomposition, decision procedures also rules tailored for: Logic, Induction, . . .

slide-37
SLIDE 37

PRL Seminar, February 23, 2004 7 Constructive Proofs and Program Extraction

Formal proof of Integer Square Root Theorem

∀n:N. ∃r:N. r2 ≤ n < (r+1)2 BY allR n:N ⊢ ∃r:N. r2 ≤ n < (r+1)2 BY NatInd 1 .....basecase..... ⊢ ∃r:N. r2 ≤ 0 < (r+1)2 √ BY existsR

⌈0⌉ THEN Auto

.....upcase..... i:N+, r:N, r2 ≤ i-1 < (r+1)2 ⊢ ∃r:N. r2 ≤ i < (r+1)2 BY Decide

⌈(r+1)2 ≤ i⌉ THEN Auto

.....Case 1..... i:N+, r:N, r2 ≤ i-1 < (r+1)2, (r+1)2 ≤ i ⊢ ∃r:N. r2 ≤ i < (r+1)2 √ BY existsR

⌈r+1⌉ THEN Auto’

.....Case 2..... i:N+, r:N, r2 ≤ i-1 < (r+1)2, ¬((r+1)2 ≤ i) ⊢ ∃r:N. r2 ≤ i < (r+1)2 √ BY existsR

⌈r⌉ THEN Auto

slide-38
SLIDE 38

PRL Seminar, February 23, 2004 8 Constructive Proofs and Program Extraction

Algorithm Extracted from the proof

  • In raw Type Theory

let rec sqrt i = if i=0 then <0,pf 0> else let <r,pfi−1> = sqrt (i-1) in if (r+1)2≤n then <r+1,pf i> else <r,pf i’>

  • In SML notation (after stripping proof components)

fun sqrt n = if n=0 then 0 else let val r = sqrt (n-1) in if n < (r+1)ˆ2 then r else r+1 end

slide-39
SLIDE 39

PRL Seminar, February 23, 2004 9 Constructive Proofs and Program Extraction

Are there better proofs?

  • Mathematically

– Proof is short and “elegant” – why change it?

slide-40
SLIDE 40

PRL Seminar, February 23, 2004 9 Constructive Proofs and Program Extraction

Are there better proofs?

  • Mathematically

– Proof is short and “elegant” – why change it?

  • Computationally

– Extracted algorithm for ⌊√n⌋ is linear in size of input n O(n) Proof uses standard induction on n

∀P:N→P. (P(0)

∧ (∀i:N+. P(i-1) ⇒ P(i))) ⇒ (∀i:N. P(i))

slide-41
SLIDE 41

PRL Seminar, February 23, 2004 9 Constructive Proofs and Program Extraction

Are there better proofs?

  • Mathematically

– Proof is short and “elegant” – why change it?

  • Computationally

– Extracted algorithm for ⌊√n⌋ is linear in size of input n O(n) Proof uses standard induction on n

∀P:N→P. (P(0)

∧ (∀i:N+. P(i-1) ⇒ P(i))) ⇒ (∀i:N. P(i))

– A better algorithm would increase r until (r+1)2 > n O(√n) Corresponding proof needs schema for bounded search

∀P:N→P. ∀n:N. P(n) ⇒ (∃k:{0..n}. P(k)

∧ (∀j:{0..k-1}. ¬P(j)))

slide-42
SLIDE 42

PRL Seminar, February 23, 2004 9 Constructive Proofs and Program Extraction

Are there better proofs?

  • Mathematically

– Proof is short and “elegant” – why change it?

  • Computationally

– Extracted algorithm for ⌊√n⌋ is linear in size of input n O(n) Proof uses standard induction on n

∀P:N→P. (P(0)

∧ (∀i:N+. P(i-1) ⇒ P(i))) ⇒ (∀i:N. P(i))

– A better algorithm would increase r until (r+1)2 > n O(√n) Corresponding proof needs schema for bounded search

∀P:N→P. ∀n:N. P(n) ⇒ (∃k:{0..n}. P(k)

∧ (∀j:{0..k-1}. ¬P(j)))

– An even better algorithm computes ⌊√n⌋ bit for bit O(log2 n) Proof almost identical to first one, but needs 4-adic induction

∀P:N→P. (P(0)

∧ (∀i:N. P(i÷4) ⇒ P(i))) ⇒ (∀i:N. P(i))