Dependent Type Theory of Stateful Higher-Order Functions Aleksandar - - PowerPoint PPT Presentation

dependent type theory of stateful higher order functions
SMART_READER_LITE
LIVE PREVIEW

Dependent Type Theory of Stateful Higher-Order Functions Aleksandar - - PowerPoint PPT Presentation

Dependent Type Theory of Stateful Higher-Order Functions Aleksandar Nanevski Harvard University joint with Greg Morrisett and Lars Birkedal TYPES 2006, Nottingham April 20, 2006 Dependent Type Theory of Stateful Higher-Order Functions p.


slide-1
SLIDE 1

Dependent Type Theory of Stateful Higher-Order Functions

Aleksandar Nanevski Harvard University joint with Greg Morrisett and Lars Birkedal TYPES 2006, Nottingham April 20, 2006

Dependent Type Theory of Stateful Higher-Order Functions – p. 1

slide-2
SLIDE 2

Dependent type theory

  • Type theory is a program logic:

− types can express and enforce precise program properties

  • Doubles up as a programming language.
  • Prototypical higher-order language (e.g, polymorphism,

inductive/recursive types, subset types, etc.)

  • Problem: must be purely functional

− recursion allowed, if you prove termination − effects like state, IO, etc., usually second class

Dependent Type Theory of Stateful Higher-Order Functions – p. 2

slide-3
SLIDE 3

Hoare Logic

  • Logic for imperative programs.
  • Specifies partial correctness via Hoare triple {P} E {Q}:

− if P holds, then E diverges or terminates in a state Q − P: precondition − Q: postcondition

  • Usually targets first-order languages

− but recent advances in the higher-order case

  • Reasoning about state and aliasing very streamlined

− Separation Logic by O’Hearn, Pym, Reynolds, Yang...

Dependent Type Theory of Stateful Higher-Order Functions – p. 3

slide-4
SLIDE 4

Type theory for imperative programs

  • Why not integrate Hoare Logic into a Type Theory?
  • Benefits:

− types can enforce correct use of effectful programs − add effects to type theory − preserves equational reasoning about pure programs

  • Idea: follow specifications-as-types principle

− Type of Hoare triples {P}x:A{Q} − precondition P, postcondition Q, return result of type A. − Dependencies allow P and Q to talk about program data.

  • In this talk: Hoare Type Theory (HTT)

− for reasoning about state and aliasing

Dependent Type Theory of Stateful Higher-Order Functions – p. 4

slide-5
SLIDE 5

Outline

  • Introduction
  • Assertion logic
  • Types and terms
  • Typechecking
  • Conclusions

Dependent Type Theory of Stateful Higher-Order Functions – p. 5

slide-6
SLIDE 6

Heaps

  • Partial functions, assigning to each natural number at most
  • ne value.
  • Assertion seleqτ(H, M, N):

− In the heap H, location M points to N : τ.

  • Function updτ(H, M, N):

− Returns a new heap in which M points to N : τ.

  • τ is a monomorphic type.

Dependent Type Theory of Stateful Higher-Order Functions – p. 6

slide-7
SLIDE 7

Axioms on heaps

  • McCarthy’s axioms for functional arrays.

(ax1)

seleqA(updA(H, M, N), M, N)

(ax2) M1 = M2 ∧ seleqA(updB(H, M1, N1), M2, N2) ⊃

seleqA(H, M2, N2)

  • And:

(ax3)

seleqA(empty, M, N) ⊃ ⊥

(ax4)

seleqA(H, M, N1) ∧ seleqA(H, M, N2) ⊃ N1 = N2

Dependent Type Theory of Stateful Higher-Order Functions – p. 7

slide-8
SLIDE 8

Assertions

  • Classical multi-sorted first-order logic with equality
  • Sorts: heaps and all types of HTT
  • Plus: type polymorphism (predicative)
  • Examples

− heap equality can be defi ned:

H1 = H2 ≡ ∀l:nat.∀α.∀x:α.

seleqα(H1, l, x) ⊂⊃ seleqα(H2, l, x)

− Also defi nable: disjoint union H = H

1 ⊎ H2

Dependent Type Theory of Stateful Higher-Order Functions – p. 8

slide-9
SLIDE 9

Some derived assertions

  • We can define propositions from Separation Logic.

− Variable mem denotes current heap.

emp

≡ (mem = empty) M →τ N ≡ (mem = updτ(empty, M, N)) M ֒ →τ N ≡

seleqτ(mem, M, N)

P ∗ Q ≡ ∃h1, h2:heap.(mem = h1 ⊎ h2) ∧[h1/mem]P ∧ [h2/mem]Q P — ∗ Q ≡ ∀h1, h2:heap.(h2 = h1 ⊎ mem) ⊃ [h1/mem]P ⊃ [h2/mem]Q

this(H)

≡ (mem = H)

Dependent Type Theory of Stateful Higher-Order Functions – p. 9

slide-10
SLIDE 10

Example: swap

  • Swap content of locations x and y (here natural numbers).
  • Spec with no aliasing between x and y:

− α, β: type variables

swap:∀α.∀β.Πx:nat.Πy:nat.

{x →α m ∗ y →β n}r : 1 {x →β n ∗ y →α m}

  • For a spec with aliasing, use ∧ instead of ∗

Dependent Type Theory of Stateful Higher-Order Functions – p. 10

slide-11
SLIDE 11

Example: swap

  • Swap content of locations x and y (here natural numbers).
  • Spec with no aliasing between x and y:

− α, β: type variables

swap:∀α.∀β.Πx:nat.Πy:nat.

m:α.n:β.{x →α m ∗ y →β n}r : 1 {x →β n ∗ y →α m}

  • For a spec with aliasing, use ∧ instead of ∗
  • m, n: dummy variables

Dependent Type Theory of Stateful Higher-Order Functions – p. 10

slide-12
SLIDE 12

Outline

  • Introduction
  • Assertion logic
  • Types and terms
  • Typechecking
  • Conclusions

Dependent Type Theory of Stateful Higher-Order Functions – p. 11

slide-13
SLIDE 13

Type structure

  • Primitive types: nat, bool, 1
  • Dependent functions: Πx:A. B – standard
  • Polymorphic types: ∀α. A – standard
  • Hoare types: {P}x:A{Q}

− Hoare types are monads − encapsulate effectful computations − but also formalize reasoning by strongest postconditions

Dependent Type Theory of Stateful Higher-Order Functions – p. 12

slide-14
SLIDE 14

Term structure

  • Pure fragment: higher-order functions, polymorphism...
  • Impure fragment – first-order imperative language

− sequence of commands, ending with a return value − primitives for allocation, strong update, lookup, deallocation,

conditionals, recursion

− recursive functions must be annotated with a type

  • Monadic constructs:

− dia E · suspends the effectful computation E ·

suspension is pure, so it can appear in types

− let dia x = M in E · run M, then E

Dependent Type Theory of Stateful Higher-Order Functions – p. 13

slide-15
SLIDE 15

Monadic terms

  • Definition and typing of characteristic monadic terms:

unit

: A → M(A) = λx. dia x

map

: (A → B) → M(A) → M(B) = λf. λx. dia (let dia y = x in f y)

idemp

: M(M(A)) → M(A) = λx. dia (let dia y = x in let dia z = y in z)

Dependent Type Theory of Stateful Higher-Order Functions – p. 14

slide-16
SLIDE 16

Monadic terms

  • Definition and typing of characteristic monadic terms:

unit

: A → M(A) = λx. dia x

map

: (A → B) → M(A) → M(B) = λf. λx. dia (let dia y = x in f y)

idemp

: M(M(A)) → M(A) = λx. dia (let dia y = x in let dia z = y in z)

  • Dependently typed unit:

unit’

: Πx:A. {P}y:A{x = y ∧ P} = λx. dia x

Dependent Type Theory of Stateful Higher-Order Functions – p. 14

slide-17
SLIDE 17

Example: swap

  • Swap content of x and y

swap :∀α.∀β. Πx:nat. Πy:nat. m:α. n:β. {x →α m * y →β n} r : unit

{x →β n * y →α m} = Λα.Λβ. λx. λy. dia (u = !x; v = !y;

y := u; x := v; ( ))

Dependent Type Theory of Stateful Higher-Order Functions – p. 15

slide-18
SLIDE 18

Example: swap twice

  • Swapping twice in a row is identity.

identity = Λα.Λβ.λx.λy. dia(let dia = swap α β x y dia = swap β α x y in ( ) end)

− Heap invariance apparent from the type.

identity : ∀α.∀β.Πx:nat.Πy:nat. m:α,n:β,h:heap.{(x →α m * y →β n) ∧ this(h)} r : 1

{this(h)}

Dependent Type Theory of Stateful Higher-Order Functions – p. 16

slide-19
SLIDE 19

Outline

  • Introduction
  • Assertion logic
  • Types and terms
  • Typechecking
  • Conclusions

Dependent Type Theory of Stateful Higher-Order Functions – p. 17

slide-20
SLIDE 20

Judgments

  • Typechecking by computing strongest postconditions.
  • Typechecking is completely syntax-directed.

− effectful programs are (part of) the proofs of their specs − remaining part of the proof must discharge intermediate assertions −

no whole-program reasoning

  • Judgment: ∆; P ⊢ E ⇒ x:A. Q

− ∆: variable context − E: computation − P: what holds before E runs (precondition) − A: return result − Q: how the heap is changed after E (strongest postcondition) − Q is output

Dependent Type Theory of Stateful Higher-Order Functions – p. 18

slide-21
SLIDE 21

Typechecking deallocation

  • dealloc(M); E

− deallocates memory at location M, and proceeds to run E

Dependent Type Theory of Stateful Higher-Order Functions – p. 19

slide-22
SLIDE 22

Typechecking deallocation

  • dealloc(M); E

− deallocates memory at location M, and proceeds to run E

  • Typing rule:

∆; P ⊢ dealloc(M); E ⇒ y:B. Q

Dependent Type Theory of Stateful Higher-Order Functions – p. 19

slide-23
SLIDE 23

Typechecking deallocation

  • dealloc(M); E

− deallocates memory at location M, and proceeds to run E

  • Typing rule:

∆ ⊢ M : nat ∆; P ⊢ dealloc(M); E ⇒ y:B. Q

Dependent Type Theory of Stateful Higher-Order Functions – p. 19

slide-24
SLIDE 24

Typechecking deallocation

  • dealloc(M); E

− deallocates memory at location M, and proceeds to run E

  • Typing rule:

∆ ⊢ M : nat ∆ ⊢ P ⊃ (M ֒ → −) ∆; P ⊢ dealloc(M); E ⇒ y:B. Q

  • proving P ⊃ (M ֒

→ −) can be postponed

Dependent Type Theory of Stateful Higher-Order Functions – p. 19

slide-25
SLIDE 25

Typechecking deallocation

  • dealloc(M); E

− deallocates memory at location M, and proceeds to run E

  • Typing rule:

∆ ⊢ M : nat ∆ ⊢ P ⊃ (M ֒ → −) ∆; ⊢ E ⇒ y:B. Q ∆; P ⊢ dealloc(M); E ⇒ y:B. Q

  • proving P ⊃ (M ֒

→ −) can be postponed

Dependent Type Theory of Stateful Higher-Order Functions – p. 19

slide-26
SLIDE 26

Typechecking deallocation

  • dealloc(M); E

− deallocates memory at location M, and proceeds to run E

  • Typing rule:

∆ ⊢ M : nat ∆ ⊢ P ⊃ (M ֒ → −) ∆; P ◦ ((M → −) ⊸ emp) ⊢ E ⇒ y:B. Q ∆; P ⊢ dealloc(M); E ⇒ y:B. Q

  • proving P ⊃ (M ֒

→ −) can be postponed

  • P ◦ (R1 ⊸ R2) is a heap obtained by switching R1 with R2 in P
  • connectives ◦ and ⊸ defi nable in HTT, but independent of ∗ and —

Dependent Type Theory of Stateful Higher-Order Functions – p. 19

slide-27
SLIDE 27

Soundness

  • In addition to equational theory, we define call-by-value
  • perational semantics
  • Soundness must show that P ⊢ E ⇒ x:A. Q indeed has the

intuitive semantics

  • Soundness requires Preservation and Progress (as usual in

type systems) but here much stronger

  • Preservation: evaluation preserves types and canonical

forms.

  • Progress: well-typed programs do not get stuck.
  • Progress depends on the soundness of the assertion logic.

− assertion logic soundness proved by simple denotational argument

Dependent Type Theory of Stateful Higher-Order Functions – p. 20

slide-28
SLIDE 28

Related work

  • Extended static checking tools: ESC/Java, SPlint, Spec#,

Cyclone... − Hoare-like annotations verifi ed during type checking − but usually no semantic foundations

  • Dependent types and effects ([Zhu, Xi’05], [Shao, Trifonov,

Saha, Papaspyrou’05]) − but types cannot depend on effectful programs

  • Hoare Logic for higher-order functions

([Schröder,Mossakowski’02], [Honda, Berger, Yoshida’05]) − simply typed underlying language (with effects) − Hoare triples do not integrate into a type system

Dependent Type Theory of Stateful Higher-Order Functions – p. 21

slide-29
SLIDE 29

Conclusions

  • HTT is a type-theoretic version of Hoare Logic

− dually: Hoare Logic for a dependently typed language − dually: Type Theory with monadic effects

  • Specifications-as-types principle via monad {P}x:A{Q}
  • Specifications like in Separation Logic.
  • Definable connectives ∗ and —

∗ from Separation Logic (but new connectives ◦ and ⊸ also needed).

  • Assertions checked by pushing strongest postconditions
  • Proofs-as-programs principle (modulo proofs of assertion)

guarantees no need for whole-program reasoning

  • Paper available at: http://www.eecs.harvard.edu/˜aleks

Dependent Type Theory of Stateful Higher-Order Functions – p. 22

slide-30
SLIDE 30

Future work

  • Higher-order assertion logic
  • Cook completeness
  • Abstract types
  • Local state
  • Hoare logic for concurrency and runST

Dependent Type Theory of Stateful Higher-Order Functions – p. 23

slide-31
SLIDE 31

Example

  • Swapping twice in a row is identity.

identity : ∀α.∀β.Πx:nat.Πy:nat. m:α,n:β,h:heap.{(x →α m * y →β n) ∧ this(h)} r : 1

{this(h)} = Λα.Λβ.λx.λy. dia(let dia u = swap α β x y

dia v = swap β α x y in ( ) end)

Dependent Type Theory of Stateful Higher-Order Functions – p. 24

slide-32
SLIDE 32

Monadic equations

  • Equational theory [Pfenning,Davies’99]
  • Implements monadic laws, but as β and η rules.

let dia x = dia E in F

= ⇒β E/xF M : {P}x:A{Q} = ⇒η

dia (let dia x = M in x)

  • Where E/xF is monadic linearization

M/xF = [M/x]F command; E′′/xF =

command; E′′/xF

let dia y = E′ in E′′/xF =

let dia y = E′ in E′′/xF

Dependent Type Theory of Stateful Higher-Order Functions – p. 25

slide-33
SLIDE 33

Example: swap

  • Swap content of locations x and y (here natural numbers).

− Spec with no aliasing between x and y:

swap:∀α, β.Πx, y:nat.

m:α.n:β.{x →α m ∗ y →β n}r : 1 {x →β n ∗ y →α m} − Spec with aliasing between x and y:

swap:∀α, β.Πx, y:nat.

m:α.n:β.h:heap.{x ֒ →α m ∧ y ֒ →β n ∧ this(h)}r : {this(updβ(updα(h, y, m), x, n))}

  • m, n, h – dummy variables

Dependent Type Theory of Stateful Higher-Order Functions – p. 26

slide-34
SLIDE 34

Typechecking allocation

  • x = allocτ(M); E

− allocates memory and initializes with M:τ − x binds the address of allocated memory

Dependent Type Theory of Stateful Higher-Order Functions – p. 27

slide-35
SLIDE 35

Typechecking allocation

  • x = allocτ(M); E

− allocates memory and initializes with M:τ − x binds the address of allocated memory

  • Typing rule:

∆; P ⊢ x = allocτ(M); E ⇒ y:B.

Dependent Type Theory of Stateful Higher-Order Functions – p. 27

slide-36
SLIDE 36

Typechecking allocation

  • x = allocτ(M); E

− allocates memory and initializes with M:τ − x binds the address of allocated memory

  • Typing rule:

∆ ⊢ τ : type ∆; P ⊢ x = allocτ(M); E ⇒ y:B.

Dependent Type Theory of Stateful Higher-Order Functions – p. 27

slide-37
SLIDE 37

Typechecking allocation

  • x = allocτ(M); E

− allocates memory and initializes with M:τ − x binds the address of allocated memory

  • Typing rule:

∆ ⊢ τ : type ∆ ⊢ M : τ ∆; P ⊢ x = allocτ(M); E ⇒ y:B.

Dependent Type Theory of Stateful Higher-Order Functions – p. 27

slide-38
SLIDE 38

Typechecking allocation

  • x = allocτ(M); E

− allocates memory and initializes with M:τ − x binds the address of allocated memory

  • Typing rule:

∆ ⊢ τ : type ∆ ⊢ M : τ ∆, x:nat; ⊢ E ⇒ y:B. Q ∆; P ⊢ x = allocτ(M); E ⇒ y:B.

Dependent Type Theory of Stateful Higher-Order Functions – p. 27

slide-39
SLIDE 39

Typechecking allocation

  • x = allocτ(M); E

− allocates memory and initializes with M:τ − x binds the address of allocated memory

  • Typing rule:

∆ ⊢ τ : type ∆ ⊢ M : τ ∆, x:nat; P ∗ (x →τ M) ⊢ E ⇒ y:B. Q ∆; P ⊢ x = allocτ(M); E ⇒ y:B. (∃x:nat.Q)

  • P ∗ (x →τ M) means x disjoint from P, and hence fresh.

Dependent Type Theory of Stateful Higher-Order Functions – p. 27

slide-40
SLIDE 40

Typechecking letdia

  • Typing rule:

∆; P ⊢ let dia x = K in E ⇒ y:B. (∃x:A. Q)

Dependent Type Theory of Stateful Higher-Order Functions – p. 28

slide-41
SLIDE 41

Typechecking letdia

  • Typing rule:

∆ ⊢ K : {R1}x:A{R2} ∆; P ⊢ let dia x = K in E ⇒ y:B. (∃x:A. Q)

Dependent Type Theory of Stateful Higher-Order Functions – p. 28

slide-42
SLIDE 42

Typechecking letdia

  • Typing rule:

∆ ⊢ K : {R1}x:A{R2} ∆ ⊢ P ⊃ R1 ∗ ⊤ ∆; P ⊢ let dia x = K in E ⇒ y:B. (∃x:A. Q)

  • P ⊃ R1 ∗ ⊤ implements “small footprints”

Dependent Type Theory of Stateful Higher-Order Functions – p. 28

slide-43
SLIDE 43

Typechecking letdia

  • Typing rule:

∆ ⊢ K : {R1}x:A{R2} ∆ ⊢ P ⊃ R1 ∗ ⊤ ∆, x:A; P ◦ (R1 ⊸ R2) ⊢ E ⇒ y:B. Q ∆; P ⊢ let dia x = K in E ⇒ y:B. (∃x:A. Q)

  • P ⊃ R1 ∗ ⊤ implements “small footprints”

Dependent Type Theory of Stateful Higher-Order Functions – p. 28

slide-44
SLIDE 44

Typechecking dia

  • Typing rule:

∆; R1 ∗ ⊤ ⊢ E ⇒ x:A. P ∆ ⊢ P ⊃ R1 ⊸ R2 ∆ ⊢ dia E : {R1}x:A{R2}

  • Precondition R1 ∗ ⊤:

− E can run in any heap with a fragment R1

  • Strongest postcondition P must imply R1 ⊸ R2

− the ending heap obtained from initial by swapping R1 with R2

Dependent Type Theory of Stateful Higher-Order Functions – p. 29