Functional Programming with Isabelle/HOL e H O l L l e b - - PowerPoint PPT Presentation

functional programming with isabelle hol
SMART_READER_LITE
LIVE PREVIEW

Functional Programming with Isabelle/HOL e H O l L l e b - - PowerPoint PPT Presentation

Functional Programming with Isabelle/HOL e H O l L l e b a s = I Florian Haftmann Technische Universit at M unchen January 2009 Overview Viewing Isabelle / HOL as a functional programming language: 1.


slide-1
SLIDE 1

Functional Programming with Isabelle/HOL

λ → ∀

=

I s a b e l l e

β α

H O L

Florian Haftmann Technische Universit¨ at M¨ unchen January 2009

slide-2
SLIDE 2

Overview

Viewing Isabelle/HOL as a functional programming language:

  • 1. Isabelle/HOL Specification Tools.
  • 2. Code Generation from Isabelle/HOL-Theories.
  • 3. Behind the Scene.

1 / 18

slide-3
SLIDE 3

Overview

Viewing Isabelle/HOL as a functional programming language:

  • 1. Isabelle/HOL Specification Tools.
  • 2. Code Generation from Isabelle/HOL-Theories.
  • 3. Behind the Scene.

Isabelle/HOL SML / OCaml / Haskell specification tools code generation

1 / 18

slide-4
SLIDE 4

Isabelle/HOL specification tools

slide-5
SLIDE 5

The definitional game

Aim: write “programs” in Isabelle/HOL as naturally as in, say, SML . . .

Isabelle/HOL specification tools 3 / 18

slide-6
SLIDE 6

The definitional game

Aim: write “programs” in Isabelle/HOL as naturally as in, say, SML . . . but it’s not enough just to claim arbitrary things:

axiomatization nonsense :: nat ⇒ nat where nonsense-def : nonsense n = Suc (nonsense n)

Isabelle/HOL specification tools 3 / 18

slide-7
SLIDE 7

The definitional game

Aim: write “programs” in Isabelle/HOL as naturally as in, say, SML . . . but it’s not enough just to claim arbitrary things:

axiomatization nonsense :: nat ⇒ nat where nonsense-def : nonsense n = Suc (nonsense n) lemma 0 = Suc 0 proof − from nonsense-def have nonsense 0 − nonsense 0 = Suc (nonsense 0) − nonsense 0 by simp then show 0 = Suc 0 by simp qed

Isabelle/HOL specification tools 3 / 18

slide-8
SLIDE 8

The definitional game

Aim: write “programs” in Isabelle/HOL as naturally as in, say, SML . . . but it’s not enough just to claim arbitrary things:

axiomatization nonsense :: nat ⇒ nat where nonsense-def : nonsense n = Suc (nonsense n) lemma 0 = Suc 0 proof − from nonsense-def have nonsense 0 − nonsense 0 = Suc (nonsense 0) − nonsense 0 by simp then show 0 = Suc 0 by simp qed

Things have to be properly constructed, that is:

  • Find an appropriate primitive definition.
  • Derive desired specification (honest toil).

Specification tools automate this.

Isabelle/HOL specification tools 3 / 18

slide-9
SLIDE 9

The Isabelle/HOL toolbox

Isabelle/HOL SML / OCaml / Haskell specification tools code generation inductive predicates Knaster-Tarski fixed point theorem inductive datatypes inductive predicate plus typedef primitive recursion primitive recursion combinator terminating functions explicit function graph plus definite choice

Isabelle/HOL specification tools 4 / 18

slide-10
SLIDE 10

Type classes

Leightweight mechanism for overloading plus abstract specification. Example: algebra

Isabelle/HOL specification tools 5 / 18

slide-11
SLIDE 11

Code generator basics

slide-12
SLIDE 12

Code generation paradigms

proof extraction animates proof derivations in the spirit of the Curry- Howard isomorphism (cf. Coq)

Code generator basics 7 / 18

slide-13
SLIDE 13

Code generation paradigms

proof extraction animates proof derivations in the spirit of the Curry- Howard isomorphism (cf. Coq) shallow embedding identifies term language of logic with term lan- guage of target language

Code generator basics 7 / 18

slide-14
SLIDE 14

Code generation paradigms

proof extraction animates proof derivations in the spirit of the Curry- Howard isomorphism (cf. Coq) shallow embedding identifies term language of logic with term lan- guage of target language In the HOL tradition the second approach is favoured, Isabelle/HOL permits proof extraction, though.

Code generator basics 7 / 18

slide-15
SLIDE 15

Code generation paradigms

proof extraction animates proof derivations in the spirit of the Curry- Howard isomorphism (cf. Coq) shallow embedding identifies term language of logic with term lan- guage of target language In the HOL tradition the second approach is favoured, Isabelle/HOL permits proof extraction, though. Isabelle/HOL SML / OCaml / Haskell specification tools code generation

Code generator basics 7 / 18

slide-16
SLIDE 16

Code generation using shallow embedding

Correctness criterion: semantics of generated target language program P describes a term rewrite system where each derivation can be simulated in the theory Θ of the logic: sum [Suc Zero_nat, Suc Zero_nat] Suc (Suc Zero_nat)

datatype nat = Suc of nat | Zero_nat; fun plus_nat (Suc m) n = plus_nat m (Suc n) | plus_nat Zero_nat n = n; fun sum [] = Zero_nat | sum (m :: ms) = plus_nat m (sum ms);

t t u u E Θ E P code generation identification identification

Code generator basics 8 / 18

slide-17
SLIDE 17

Code generation using shallow embedding

Correctness criterion: semantics of generated target language program P describes a term rewrite system where each derivation can be simulated in the theory Θ of the logic: sum [Suc Zero_nat, Suc Zero_nat] Suc (Suc Zero_nat)

datatype nat = Suc of nat | Zero_nat; fun plus_nat (Suc m) n = plus_nat m (Suc n) | plus_nat Zero_nat n = n; fun sum [] = Zero_nat | sum (m :: ms) = plus_nat m (sum ms);

t t u u E Θ E P code generation identification identification (partial correctness)

Code generator basics 8 / 18

slide-18
SLIDE 18

Examples

  • amortised queues
  • amortised queues with poor man’s datatype abstraction
  • algebra with type classes

Code generator basics 9 / 18

slide-19
SLIDE 19

A closer look at code generation

slide-20
SLIDE 20

How does a code generator look like?

A closer look at code generation 11 / 18

slide-21
SLIDE 21

How does a code generator look like?

A closer look at code generation 11 / 18

slide-22
SLIDE 22

Architecture

Isabelle/HOL tools Isabelle theory code equations intermediate language serialisation SML OCaml . . . Haskell selection preprocessing translation

A closer look at code generation 12 / 18

slide-23
SLIDE 23

Intermediate language

purpose: add “structure” to bare logical equations

A closer look at code generation 13 / 18

slide-24
SLIDE 24

Intermediate language

purpose: add “structure” to bare logical equations

data κ αk = f 1 of τ1 | . . . | f n of τn fun f :: ∀ α::sk. τ where f [α::sk] t1 = t1 | . . . | f [α::sk] tk = tk class c ⊆ c1 ∩ . . . ∩ cm where f 1 :: ∀ α. τ 1, . . ., f n :: ∀ α. τ n inst κ α::sk :: c where f 1 [κ α::sk] = t1, . . ., f n [κ α::sk] = tn

. . . a kind of “Mini-Haskell”

A closer look at code generation 13 / 18

slide-25
SLIDE 25

Intermediate language

purpose: add “structure” to bare logical equations

data κ αk = f 1 of τ1 | . . . | f n of τn fun f :: ∀ α::sk. τ where f [α::sk] t1 = t1 | . . . | f [α::sk] tk = tk class c ⊆ c1 ∩ . . . ∩ cm where f 1 :: ∀ α. τ 1, . . ., f n :: ∀ α. τ n inst κ α::sk :: c where f 1 [κ α::sk] = t1, . . ., f n [κ α::sk] = tn

. . . a kind of “Mini-Haskell” . . . not “All-gol”, but “Thin-gol”

A closer look at code generation 13 / 18

slide-26
SLIDE 26

Selecting

Two degrees of freedom: code equations by default: definition, primrec, fun, function explicitly: attribute [code] datatype constructors by default: datatype, record explicitly: code-datatype

A closer look at code generation 14 / 18

slide-27
SLIDE 27

Preprocessing

Interface to plugin arbitrary theorem transformations: rewrites simpset function transformators theory -> thm list -> thm list

A closer look at code generation 15 / 18

slide-28
SLIDE 28

Serialising

Adaption to target-language specifics:

  • improving readability and aesthetics of generated code (bools, tuples,

lists, . . . )

  • gaining efficiency (target-language integers)
  • interface with language parts which have no direct counterpart in HOL

(imperative data structures)

A closer look at code generation 16 / 18

slide-29
SLIDE 29

Serialising

Adaption to target-language specifics:

  • improving readability and aesthetics of generated code (bools, tuples,

lists, . . . )

  • gaining efficiency (target-language integers)
  • interface with language parts which have no direct counterpart in HOL

(imperative data structures) . . . but: know what you are doing!

A closer look at code generation 16 / 18

slide-30
SLIDE 30

Serialising

Adaption to target-language specifics:

  • improving readability and aesthetics of generated code (bools, tuples,

lists, . . . )

  • gaining efficiency (target-language integers)
  • interface with language parts which have no direct counterpart in HOL

(imperative data structures) . . . but: know what you are doing! Remember the fundamental rule of software engineering: Don’t write your own foo; if you can, use somebody else’s.

A closer look at code generation 16 / 18

slide-31
SLIDE 31

Serialising

Adaption to target-language specifics:

  • improving readability and aesthetics of generated code (bools, tuples,

lists, . . . )

  • gaining efficiency (target-language integers)
  • interface with language parts which have no direct counterpart in HOL

(imperative data structures) . . . but: know what you are doing! Remember the fundamental rule of software engineering: Don’t write your own foo; if you can, use somebody else’s. foo ∈ { operating system, garabage collector, cryptographic algorithm, concurrency framework, theorem prover, . . . }

A closer look at code generation 16 / 18

slide-32
SLIDE 32

Serialising

Adaption to target-language specifics:

  • improving readability and aesthetics of generated code (bools, tuples,

lists, . . . )

  • gaining efficiency (target-language integers)
  • interface with language parts which have no direct counterpart in HOL

(imperative data structures) . . . but: know what you are doing! Remember the fundamental rule of software engineering: Don’t write your own foo; if you can, use somebody else’s. foo ∈ { operating system, garabage collector, cryptographic algorithm, concurrency framework, theorem prover, . . . } ∪ {serialisation}

A closer look at code generation 16 / 18

slide-33
SLIDE 33

What remains

Not mentioned here

  • implementing equality
  • code extraction from proofs

Ongoing work and research

  • turning inductive predicates into equations
  • Haskabelle: importing Haskell files
  • Quickcheck
  • concept for datatype abstraction

Further reading

  • Tutorials in the Isabelle distribution for functions, code generation

etc.

  • PhD thesis on code generation (under heavy construction. . . )

. . .

A closer look at code generation 17 / 18

slide-34
SLIDE 34

Happy proving, happy hacking Thanks for your attention