Formal Component-Based Semantics

Report 4 Downloads 133 Views
Formal Component-Based Semantics Ken Madlener, Sjaak Smetsers, Marko van Eekelen Radboud University Nijmegen

August 28, 2011

Madlener, Smetsers, van Eekelen (RU)

Formal Component-Based Semantics

August 28, 2011

1 / 13

Component-Based Semantics

Introduction

A common problem with language formalizations is the lack of reusability. E.g.: command sequencing is a part of virtually every imperative language.

Solution: Component-Based Semantics, proposed by Peter D. Mosses in 2009. Think Intentional Programming (Microsoft) Combines basic language constructs contained in an open-ended repository to develop languages. Components can be defined using Action Semantics or Modular SOS.

This talk presents a first formalization, developed in Coq.

Madlener, Smetsers, van Eekelen (RU)

Formal Component-Based Semantics

August 28, 2011

2 / 13

Component-Based Semantics

A While-Loop with Break

CmdJwhile (E ) C K = catch(cond-loop(ExpJE K, CmdJC K), eq(”breaking”, skip))

CmdJbreakK = throw(”breaking”)

Madlener, Smetsers, van Eekelen (RU)

Formal Component-Based Semantics

August 28, 2011

3 / 13

Component-Based Semantics

A While-Loop with Break and Continue

CmdJwhile (E ) C K = catch(cond-loop(ExpJE K, catch(CmdJC K, eq(”continuing”, skip)), eq(”breaking”, skip))

CmdJbreakK = throw(”breaking”) CmdJcontinueK = throw(”continuing”)

Madlener, Smetsers, van Eekelen (RU)

Formal Component-Based Semantics

August 28, 2011

4 / 13

Modular SOS

Exceptions in Modular SOS Cmd ::= skip | throw(String) Label := { : String, . . .}

Cmd ::= eq(String, Cmd) Label := { : String, . . .} =E

{0 =E ,−}

throw(E ) −−−−−−→ skip

{,−}

eq (E , C ) −−−→ C

Cmd ::= catch (Cmd, Cmd) Label := { : String, . . .} {,X }

C1 −−−→ C10 {,X }

 = ()

catch (C1 , C2 ) −−−→ catch (C10 , C2 )

Madlener, Smetsers, van Eekelen (RU)

{,X }

C1 −−−→ C10

 6= () {,X }

catch (C1 , C2 ) −−−→ C2

Formal Component-Based Semantics

August 28, 2011

5 / 13

Modular SOS

Command Sequencing in Modular SOS

Cmd ::= skip | seq (Cmd, Cmd) Cmd ::= skip

Label := {. . .} {−}

Label := {. . .}

seq (skip, c) −−→ c {X }

c1 −−→ c10 {X }

seq (c1 , c2 ) −−→ seq (c10 , c2 )

Madlener, Smetsers, van Eekelen (RU)

Formal Component-Based Semantics

August 28, 2011

6 / 13

Modular SOS

Labels The ”state” is encoded by labels on the transition relation. These labels are the arrows of a suitable product category: Composition of arrows is needed for consecutive transitions: a→b

b 0 →c

S −−−→ T −−−→ R only if b = b 0 .

Madlener, Smetsers, van Eekelen (RU)

Formal Component-Based Semantics

August 28, 2011

7 / 13

Modular SOS

Labels The ”state” is encoded by labels on the transition relation. These labels are the arrows of a suitable product category: Composition of arrows is needed for consecutive transitions: a→b

b 0 →c

S −−−→ T −−−→ R only if b = b 0 . Identity arrows express silent transitions {−}.

Madlener, Smetsers, van Eekelen (RU)

Formal Component-Based Semantics

August 28, 2011

7 / 13

Modular SOS

Labels The ”state” is encoded by labels on the transition relation. These labels are the arrows of a suitable product category: Composition of arrows is needed for consecutive transitions: a→b

b 0 →c

S −−−→ T −−−→ R only if b = b 0 . Identity arrows express silent transitions {−}. Write-only components require multiple arrows, e.g.: ”foo” ∗−−−→∗ print(”foo”); print(”bar ”) −−−−−−→ skip; print(”bar ”) ()

∗−−−→∗ −−−−−−→ print(”bar ”) ”bar ” ∗−−−→∗ −−−−−−→ skip

Madlener, Smetsers, van Eekelen (RU)

Formal Component-Based Semantics

August 28, 2011

7 / 13

Coq Formalization

Classes for Transition Relations Class Arrows (O: Type): Type := Arrow: O → O → Type. Infix ”−→ ” := Arrow (at level 90, right associativity).

Context (O : Type) {Ar: Arrows O} B. Class Step := step : ∀ {x y: O}, (x −→ y) → B → B → Prop. Class LocalStep ‘{C : Constructor} := localstep : ∀ {x y: O}, (x −→ y) → restr C → B → Prop.

Madlener, Smetsers, van Eekelen (RU)

Formal Component-Based Semantics

August 28, 2011

8 / 13

Coq Formalization

Classes for Language Constructors

Class IP Pair A B (inj: Inject A B) (prj: Project A B) := { H i: ∀ x: A, prj (inj x) = Some x; H p: ∀ b: B, match project b with | None ⇒ True | Some x ⇒ inj x = b end }. Class Constructor (name: string) A B (inj: Inject A B) (prj: Project A B) (ip: IP Pair A B inj prj) := placeholder: unit.

Madlener, Smetsers, van Eekelen (RU)

Formal Component-Based Semantics

August 28, 2011

9 / 13

Coq Formalization

Class for Labels

Context (M: Type) (O M: M → Type) (A M: ∀ m, Arrows (O M m)) ‘{ip: IP Pair M L} {O L: L → Type} {A L: ∀ l, Arrows (O L l)}. Class Label := { cover O: ∀ m: M, O M m = O L (’ m); cover A: ∀ m: M, A M m = hh fun T ⇒ Arrows T # eq sym (cover O m) ii A L (’ m) }.

Madlener, Smetsers, van Eekelen (RU)

Formal Component-Based Semantics

August 28, 2011

10 / 13

Coq Formalization

Example: Seq Encoded in Coq Section seq. Context ‘{Seq: @Constructor ”seq” (Cmd∗Cmd) Cmd seq p seq ip seq} ‘{Skip: @Constructor ”skip” unit Cmd skip p skip ip skip} ‘{label: Label M none O M none A M none} {Step Cmd: Step Obj Cmd}. Inductive lstep {x y: Obj} (ar: x −→ y): restr Seq → Cmd → Prop := Global Instance LS seq: LocalStep Obj := @lstep. Lemma det seq (c1 c2 : Cmd): End seq. Madlener, Smetsers, van Eekelen (RU)

Formal Component-Based Semantics

August 28, 2011

11 / 13

Coq Formalization

Example: Cmd Encoded in Coq

Inductive s Cmd {x y: O} (ar: x −→ y): Cmd → Cmd → Prop := | s Cmd skip: ∀ (c: restr Skip) (c’: Cmd), localstep ar c c’ → s Cmd ar (I c) c’ | s Cmd seq: ∀ (c: restr Seq) (c’: Cmd), let := (@s Cmd: Step O Cmd) in localstep ar c c’ → s Cmd ar (I c) c’. Instance S Cmd: Step O Cmd := @s Cmd.

Madlener, Smetsers, van Eekelen (RU)

Formal Component-Based Semantics

August 28, 2011

12 / 13

Future Work

Future Work

Support for software verification. I’m working on MSOS constructs for design contracts. Currently developing a VCG. Slicing of semantics.

A notion of bisimulation for MSOS to support for reasoning about extensions. E.g. under which extensions does S; (T ; R) = (S; T ); R hold? The presented formalization is based on first-order inductive types. Probably need a representation of inductive types `a la System F.

Relation between MSOS and monads. Can’t execute the semantics; would like to have an equivalent functional encoding.

Madlener, Smetsers, van Eekelen (RU)

Formal Component-Based Semantics

August 28, 2011

13 / 13