Synthesis of distributed mobile programs using monadic types in Coq - - PowerPoint PPT Presentation

synthesis of distributed mobile programs using monadic
SMART_READER_LITE
LIVE PREVIEW

Synthesis of distributed mobile programs using monadic types in Coq - - PowerPoint PPT Presentation

Synthesis of distributed mobile programs using monadic types in Coq Marino Miculan Marco Paviotti Dept. of Mathematics and Computer Science University of Udine ITP 2012 August 13th, 2012 1 / 22 The problem The extraction of certified


slide-1
SLIDE 1

Synthesis of distributed mobile programs using monadic types in Coq

Marino Miculan Marco Paviotti

  • Dept. of Mathematics and Computer Science

University of Udine

ITP 2012

August 13th, 2012

1 / 22

slide-2
SLIDE 2

The problem

The extraction of certified functional and effect-free programs is a well-know practice in the field of Type Theory, however: ✔ There are many other computational effects (and corresponding Type Theories, possibly) ✔ These scenarios would greatly benefit from a mechanisms for extraction ✘ Languages implementing these aspects usually do not support the Curry-Howard isomorphism ✘ Implementing a specific proof-assistant would be a daunting task anyway.

2 / 22

slide-3
SLIDE 3

Our contribution

We propose:

  • a general methodology for circumventing this problem using

the existing technology (Coq)

+ encapsulate non-functional aspects in monadic types + implement a post-extraction compiler for realizing monadic constructors in the target language

  • example: distributed programs with effects in Erlang.

3 / 22

slide-4
SLIDE 4

A general methodology Type Theory

with effects

Step 1:

Encode a given Type theory in Coq

CiC Sets

+ Monadic Types

Step 2:

Define a Monad in Coq cover- ing the computational aspects

< >

Step 3:

Prove the soundness theo- rem

Functional Code (e.g

Haskell) with compu- tational annotations Extraction

Use the Extraction fa- cility to get the func- tional code

Step 4:

Define the translation function E

4 / 22

slide-5
SLIDE 5

A general methodology Type Theory

with effects

Step 1:

Encode a given Type theory in Coq

CiC Sets

+ Monadic Types

Step 2:

Define a Monad in Coq cover- ing the computational aspects

< >

Step 3:

Prove the soundness theo- rem

Functional Code (e.g

Haskell) with compu- tational annotations Extraction

Use the Extraction fa- cility to get the func- tional code

Step 4:

Define the translation function E

4 / 22

slide-6
SLIDE 6

A general methodology Type Theory

with effects

Step 1:

Encode a given Type theory in Coq

CiC Sets

+ Monadic Types

Step 2:

Define a Monad in Coq cover- ing the computational aspects

< >

Step 3:

Prove the soundness theo- rem

Functional Code (e.g

Haskell) with compu- tational annotations Extraction

Use the Extraction fa- cility to get the func- tional code

Step 4:

Define the translation function E

4 / 22

slide-7
SLIDE 7

A general methodology Type Theory

with effects

Step 1:

Encode a given Type theory in Coq

CiC Sets

+ Monadic Types

Step 2:

Define a Monad in Coq cover- ing the computational aspects

< >

Step 3:

Prove the soundness theo- rem

Functional Code (e.g

Haskell) with compu- tational annotations Extraction

Use the Extraction fa- cility to get the func- tional code

Step 4:

Define the translation function E

4 / 22

slide-8
SLIDE 8

A general methodology Type Theory

with effects

Step 1:

Encode a given Type theory in Coq

CiC Sets

+ Monadic Types

Step 2:

Define a Monad in Coq cover- ing the computational aspects

< >

Step 3:

Prove the soundness theo- rem

Functional Code (e.g

Haskell) with compu- tational annotations Extraction

Use the Extraction fa- cility to get the func- tional code

Step 4:

Define the translation function E

4 / 22

slide-9
SLIDE 9

A general methodology Type Theory

with effects

Step 1:

Encode a given Type theory in Coq

CiC Sets

+ Monadic Types

Step 2:

Define a Monad in Coq cover- ing the computational aspects

< >

Step 3:

Prove the soundness theo- rem

Functional Code (e.g

Haskell) with compu- tational annotations Extraction

Use the Extraction fa- cility to get the func- tional code

Step 4:

Define the translation function E

4 / 22

slide-10
SLIDE 10

A general methodology Type Theory

with effects

Step 1:

Encode a given Type theory in Coq

CiC Sets

+ Monadic Types

Step 2:

Define a Monad in Coq cover- ing the computational aspects

< >

Step 3:

Prove the soundness theo- rem

Functional Code (e.g

Haskell) with compu- tational annotations Extraction

Use the Extraction fa- cility to get the func- tional code

Step 4:

Define the translation function E

4 / 22

slide-11
SLIDE 11

A general methodology Type Theory

with effects

Step 1:

Encode a given Type theory in Coq

CiC Sets

+ Monadic Types

Step 2:

Define a Monad in Coq cover- ing the computational aspects

< >

Step 3:

Prove the soundness theo- rem

Functional Code (e.g

Haskell) with compu- tational annotations Extraction

Use the Extraction fa- cility to get the func- tional code

Step 4:

Define the translation function E

Target Code E

4 / 22

slide-12
SLIDE 12

A general methodology Type Theory

with effects

Step 1:

Encode a given Type theory in Coq

CiC Sets

+ Monadic Types

Step 2:

Define a Monad in Coq cover- ing the computational aspects

< >

Step 3:

Prove the soundness theo- rem

Functional Code (e.g

Haskell) with compu- tational annotations Extraction

Use the Extraction fa- cility to get the func- tional code

Step 4:

Define the translation function E

Target Code E

4 / 22

slide-13
SLIDE 13

A general methodology Type Theory

with effects

Step 1:

Encode a given Type theory in Coq

CiC Sets

+ Monadic Types

Step 2:

Define a Monad in Coq cover- ing the computational aspects

< >

Step 3:

Prove the soundness theo- rem

Functional Code (e.g

Haskell) with compu- tational annotations Extraction

Use the Extraction fa- cility to get the func- tional code

Step 4:

Define the translation function E

Target Code E

4 / 22

slide-14
SLIDE 14

Extraction of distributed code

We define the distributed monad in the Calculus of Inductive Constructions

IO w A :Set

forall w: World and A: Set

5 / 22

slide-15
SLIDE 15

Extraction of distributed code

We define the distributed monad in the Calculus of Inductive Constructions

IO w A :Set

forall w: World and A: Set

A computation localized on the specified host

5 / 22

slide-16
SLIDE 16

Extraction of distributed code

We define the distributed monad in the Calculus of Inductive Constructions

IO w A :Set

forall w: World and A: Set

By Curry-Howard Isomorphism, the (constructive) proofs of these specifications are turned into decorated Haskell code

IO w A

Extraction

⇒ H

5 / 22

slide-17
SLIDE 17

Extraction of distributed code

We define the distributed monad in the Calculus of Inductive Constructions

IO w A :Set

forall w: World and A: Set

By Curry-Howard Isomorphism, the (constructive) proofs of these specifications are turned into decorated Haskell code

IO w A

Extraction

⇒ H

These decorations are exploited by the Haskell-Erlang Compiler

E : H → E

5 / 22

slide-18
SLIDE 18

Extraction of distributed code

We define the distributed monad in the Calculus of Inductive Constructions

IO w A :Set

forall w: World and A: Set

By Curry-Howard Isomorphism, the (constructive) proofs of these specifications are turned into decorated Haskell code

IO w A

Extraction

⇒ H

These decorations are exploited by the Haskell-Erlang Compiler

E : H → E

Haskell

annotated code

Erlang dis-

tributed code

5 / 22

slide-19
SLIDE 19

Monads in Coq

We define a family of monads indexed by worlds from Set to Set. Given a world w a monad is a functor defined as

IO w A = S → ((R w A) + Error)

6 / 22

slide-20
SLIDE 20

Monads in Coq

We define a family of monads indexed by worlds from Set to Set. Given a world w a monad is a functor defined as

IO w A = S → ((R w A) + Error)

A Localized com- putation

6 / 22

slide-21
SLIDE 21

Monads in Coq

We define a family of monads indexed by worlds from Set to Set. Given a world w a monad is a functor defined as

IO w A = S → ((R w A) + Error)

Function Space from the global store to the results plus the error state

6 / 22

slide-22
SLIDE 22

Monads in Coq

We define a family of monads indexed by worlds from Set to Set. Given a world w a monad is a functor defined as

IO w A = S → ((R w A) + Error)

Monadic Operators

IOgetw A : IO remote A → IO w A λ κ σ. κ(σ)

(Operator’s implementation)

6 / 22

slide-23
SLIDE 23

Monads in Coq

We define a family of monads indexed by worlds from Set to Set. Given a world w a monad is a functor defined as

IO w A = S → ((R w A) + Error)

Monadic Operators

IOgetw A : IO remote A → IO w A λ κ σ. κ(σ)

(Operator’s implementation) Other monadic operators IOreturnw A : A → IO w A IObindw A B : IO w A → (A → IO w B) → IO w B IOlookupw A : Refw → (N → IO w A) → IO w A IOupdatew A : Refw → N → IO w A → IO w A IOneww A : N → (Refw → IO w A) → IO w A

6 / 22

slide-24
SLIDE 24

Extraction

Lemma (Remote Procedure Call)

∀w w’, (N → IO w’ bool) → (N → IO w bool)

  • Proof. simpl; introv f. intro n. apply* IOget.

Qed. Haskell rpc w w’ f n = iOget w w’ (f n)

7 / 22

slide-25
SLIDE 25

Extraction

Lemma (Remote Procedure Call)

∀w w’, (N → IO w’ bool) → (N → IO w bool)

  • Proof. simpl; introv f. intro n. apply* IOget.

Qed. Haskell rpc w w’ f n = iOget w w’ (f n)

Given two worlds w w’

7 / 22

slide-26
SLIDE 26

Extraction

Lemma (Remote Procedure Call)

∀w w’, (N → IO w’ bool) → (N → IO w bool)

  • Proof. simpl; introv f. intro n. apply* IOget.

Qed. Haskell rpc w w’ f n = iOget w w’ (f n)

Given a function f

7 / 22

slide-27
SLIDE 27

Extraction

Lemma (Remote Procedure Call)

∀w w’, (N → IO w’ bool) → (N → IO w bool)

  • Proof. simpl; introv f. intro n. apply* IOget.

Qed. Haskell rpc w w’ f n = iOget w w’ (f n)

Given a value, say n

7 / 22

slide-28
SLIDE 28

Extraction

Lemma (Remote Procedure Call)

∀w w’, (N → IO w’ bool) → (N → IO w bool)

  • Proof. simpl; introv f. intro n. apply* IOget.

Qed. Haskell rpc w w’ f n = iOget w w’ (f n)

Apply IOget to (f n)

(World parameters are inferred) 7 / 22

slide-29
SLIDE 29

Extraction

Lemma (Remote Procedure Call)

∀w w’, (N → IO w’ bool) → (N → IO w bool)

  • Proof. simpl; introv f. intro n. apply* IOget.

Qed. Haskell rpc w w’ f n = iOget w w’ (f n) Notice: The annotated Haskell code is not runnable yet!

7 / 22

slide-30
SLIDE 30

The HEC Compiler: the mobility fragment

MiOget A1 A2 (F A3)ρ = spawn(element(2, EA1ρ), ρ(η), dispatcher,[fun () -> EFρEA3ρend, EA2ρ, {self(), node()}]), receive{result, Z} -> Z

Erlang Primitives remind: Pid = spawn (Host, Module, Function, Parameters) (Code Mobilty) receive {Pattern} -> Expression (Receive) Pid ! Expression (Send)

8 / 22

slide-31
SLIDE 31

The HEC Compiler: the location fragment

New MiOnew A1 A2 A3ρ = (EA3ρ)(spawn(element(2, EA1ρ), ρ("module name"), location, [EA2ρ])) Update MiOupdate w A1 A2 A3ρ = EA1ρ!{update, EA2ρ}, EA3ρ Lookup MiOlookup w A1 A2ρ = EA1ρ!{get, {self(), node()}}, receive{result, Z} → (EA2ρ)(Z)

Erlang Primitives remind: Pid = spawn (Host, Module, Function, Parameters) (Code Mobilty) receive {Pattern} -> Expression (Receive) Pid ! Expression (Send)

9 / 22

slide-32
SLIDE 32

The rpc example

Haskell rpc w w’ f n = iOget w w’ (f n) Erlang spawn(element(2, w), ρ(η), dispatcher, [fun () -> f(n) end, w’, {self(), node()}]), receive{result, Z} -> Z

10 / 22

slide-33
SLIDE 33

The rpc example

Haskell rpc w w’ f n = iOget w w’ (f n) Erlang spawn(element(2, w), ρ(η), dispatcher, [fun () -> f(n) end, w’, {self(), node()}]), receive{result, Z} -> Z

M : H → E

Haskell annotations are exploited by the

HEC compiler

10 / 22

slide-34
SLIDE 34

The rpc example

Haskell rpc w w’ f n = iOget w w’ (f n) Erlang spawn(element(2, w), ρ(η), dispatcher, [fun () -> f(n) end, w’, {self(), node()}]), receive{result, Z} -> Z

Main W W’

(1) Dispatch! λ. f(n) (2) Execute! λ. f(n) (3) Result, f(n) 10 / 22

slide-35
SLIDE 35

Adding a Type Theory Type Theory

with effects

Step 1:

Encode a given Type theory in Coq

CiC Sets

+ Monadic Types

Step 2:

Define a Monad in Coq cover- ing the computational aspects

< >

Step 3:

Prove the soundness theo- rem

Functional Code (e.g

Haskell) with compu- tational annotations Extraction

Use the Extraction fa- cility to get the func- tional code

Step 4:

Define the translation function E

Target Code E

11 / 22

slide-36
SLIDE 36

λXD : A Type Theory for distributed computations

We give a Type theory similar to the Intuitionistic Hybrid Modal Logic of Licata and Harper1

Syntax

(Hybrid Modal Logic IS5) Types A ::= Unit | Bool | Nat |A × B|A → B | Ref (Locations) | ∀w.A (Necessarily ✷A) | ∃w.A (Possibly ✸A) | A@w (Nominal) | A (Lax Modality)

1“A monadic formalization of ML5”. In Proc. LFMTP, EPTCS 34, 2010. 12 / 22

slide-37
SLIDE 37

λXD: Type System Γ ⊢ M : A[w]

“A is inhabited with M at world w” (Judgment)

Environment Term Type

World Fragment Monadic Fragment 13 / 22

slide-38
SLIDE 38

λXD: Type System Γ ⊢ M : A[w]

“A is inhabited with M at world w” (Judgment)

Environment Term Type

World Fragment (A small subset) Γ ⊢ M : A[w] Γ ⊢ some w M : ∃w.A[w] (some) Γ ⊢ M : ∃u.A[w] Γ, x : A{z/u}[w] ⊢ N : C[w′] z fresh in Γ Γ ⊢ letd (z, x) = M in N : C[w′] (letd)

Intuitionistic ∃

  • a world w as Witness
  • a Proof of A(w)

World Fragment Monadic Fragment 13 / 22

slide-39
SLIDE 39

λXD: Type System Γ ⊢ M : A[w]

“A is inhabited with M at world w” (Judgment)

Environment Term Type

Monadic Fragment (A small subset)

Mobile A

Γ ⊢ M : A[w′] Γ ⊢ get w′ M : A[w] (Get)

World Fragment Monadic Fragment 13 / 22

slide-40
SLIDE 40

Soundness

We restrict the mobile types which are movable: b ∈ {Unit, Nat, Bool}

Mobile b

(Basic)

Mobile A Mobile B Mobile A × B

(Prod)

Mobile A@w (At) Mobile A Mobile ∀w.A (Forall) Mobile A Mobile ∃w.A (Exists)

Lemma (Mobility)

∀A ∈ Type, if Mobile A, then ∀w, w ′ ∈ W, A <w> = A <w ′>. [Coq proof]

14 / 22

slide-41
SLIDE 41

λXD: Translation into CIC Sets <w> : TypeλXD → Type

Unit <w> = unit Nat <w> = nat A × B <w> = A <w> * B <w> Bool <w> = bool Ref <w> = ref w A@w ′ <w> = A <w’> A → B <w> = A <w> -> B <w> ∀w ′.A <w> = forall w’, (A w ′) <w> ∃w ′.A <w> = { w’ : world & (A w ′) <w> } A <w> = IO w (A <w>)

Theorem (Soundness)

Let Γ ∈ Ctxt, t ∈ Term, A ∈ Type, w ∈ World, let {w1, . . . , wn} be all free worlds in Γ, A, t, w.

Γ ⊢XD t : A[w] → w1 : W, . . . , wn : W ⊢ : Γ → A <w>. [Coq proof]

15 / 22

slide-42
SLIDE 42

Example: Remote Write

Lemma (Remote Write)

∀w’ w’’, N@w’ →XD (Ref @w’’) <w’>

  • Proof. simpl; introv value. apply

IOget with (remote := w2). apply

  • IOnew. exact value. intros address.

apply IOret. exact address. Defined.

Location(X) Executer (W1) Dispatcher Main W2 (2) Execute (1) Spawn (5) Pid (3) Value (4) Pid The host W2 contains two pro- cess, the spawned location and the executer

16 / 22

slide-43
SLIDE 43

Example: Remote Read

Lemma (Remote Read)

∀w’ w’’, Ref @w’’ →XD (N) <w’>

  • Proof. simpl; introv address. apply

IOget with (remote := w2). apply*

  • IOlookup. intros. apply* IOret.

Defined.

Location(X) Executer (W1) Dispatcher Main W2 (2) Execute (1) Spawn (5) Value (3) get (4) value The location is already present in W2 and replies to set and get requests

17 / 22

slide-44
SLIDE 44

Update Lemma

A suitable lemma can be stated and proved in order to guarantee the system behaves correctly

Lemma (Update operation is correct)

∀w w’,∀N, ∀s : Store, getvalue ((IObind ((@remotewrite w w’ value )) (λ l. (@remoteread w w’ l))) s) = Some value. [Coq proof]

18 / 22

slide-45
SLIDE 45

Update lemma: proof deals with monadic constructors

Proof is by unfolding the Definitions of the monadic operators

19 / 22

slide-46
SLIDE 46

Conclusions Summary

  • We presented a generic methodology for code extraction

which works in principle with every computational aspect

+ Define a front-end type theory on top (optional) + Define a IO monad over Set + Implement the back-end compiler

  • We shown how to extract distributed code by defining a

Distributed Monad IO w A

  • We defined the λXD on top

20 / 22

slide-47
SLIDE 47

Conclusions Future work

  • Other computational aspects / target languages
  • is the compiler correct? ⇒ Compiler Certification could be

achieved by using the implementation of monadic operators as the specification of how the target code must behave

  • Alternatively, an Axiomatization for the operators is needed

+ which takes into account the mobility + not available yet (AFAIK) + but could be achived extending similar work (see e.g., Power and Plotkin

Axioms ) 21 / 22

slide-48
SLIDE 48

Thanks for your attention. Questions?

22 / 22

slide-49
SLIDE 49

Appendix: λXD Typesystem - World Fragment

Γ ⊢ M : A[w′] w fresh in Γ Γ ⊢ Λw.M : ∀w.A[w′] (Box) Γ ⊢ M : ∀w.A[w] Γ ⊢ unbox w′M : A[w] (Unbox) ⊢ Γ x : A[w] ∈ Γ Γ ⊢ x : A[w] (Var) Γ ⊢ M : A[w] Γ ⊢ some w M : ∃w.A[w] (Some) Γ ⊢ M : ∃u.A[w] Γ, x : A{z/u}[w] ⊢ N : C[w′] z fresh in Γ Γ ⊢ letd (z, x) = M in N : C[w′] (LetD) Γ ⊢ M : A[w] Γ ⊢ hold M : A@w[w′] (Hold) Γ ⊢ M : A@z[w] Γ, x : A[z] ⊢ N : C[w] Γ ⊢ leta x = M in N : C[w] (LetA)

Return 23 / 22

slide-50
SLIDE 50

Appendix: λXD Typesystem - Monadic fragment

Γ ⊢ M : A[w] Γ ⊢ return M : A[w] (Ret) Γ ⊢ M : A[w] Γ ⊢ N : A → B[w] Γ ⊢ bind M N : B[w] (

Mobile A

Γ ⊢ M : A[w′] Γ ⊢ get w′ M : A[w] (Get) Γ ⊢ M : Nat[w] Γ ⊢ N : Ref → A[w] Γ ⊢ new M N : A[w] (New) Γ ⊢ M : Ref [w] Γ ⊢ N : Nat → A Γ ⊢ lookup M N : A[w] (Lookup) Γ ⊢ T1 : Ref [w] Γ ⊢ T2 : Nat[w] Γ ⊢ T3 : A[w] Γ ⊢ update T1 T2 T3 : A[w] (Update)

Return 24 / 22

slide-51
SLIDE 51

Appendix: On the axiomatization

Rules for the lookup and update are borrowed from Plotkin and Power 1: 1.lloc(uloc,v(x))v = x 2.lloc(lloc(tvv′)v)v′ = lloc(tvv)v 3.uloc,v(uloc,v′(x)) = uloc,v′(x) 4.uloc,v(lloc(tv′)v′) = uloc,v(tv) 5.lloc(l′

loc(tvv′)v′)v = l′ loc(lloc(tvv′)v)v′where loc = loc′

6.uloc,v(uloc′,v′(x)) = uloc′,v′(uloc,v(x))where loc = loc′′

Return

  • 1G. D. Plotkin and J. Power. Notions of computation determine monads. In
  • Proc. FoSSaCS, LNCS 2303, 2002.

25 / 22