An Executable Rewriting Logic Semantics for Concurrent Haskell - - PowerPoint PPT Presentation

an executable rewriting logic semantics for concurrent
SMART_READER_LITE
LIVE PREVIEW

An Executable Rewriting Logic Semantics for Concurrent Haskell - - PowerPoint PPT Presentation

Introduction Rewriting Logic Semantics of Haskell Conclusion An Executable Rewriting Logic Semantics for Concurrent Haskell Patrick Bahr s0404888@inf.tu-dresden.de Technische Universitt Dresden January 11, 2008 Patrick Bahr An Executable


slide-1
SLIDE 1

Introduction Rewriting Logic Semantics of Haskell Conclusion

An Executable Rewriting Logic Semantics for Concurrent Haskell

Patrick Bahr s0404888@inf.tu-dresden.de

Technische Universität Dresden

January 11, 2008

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-2
SLIDE 2

Introduction Rewriting Logic Semantics of Haskell Conclusion

Outline

1

Introduction Motivation Preliminaries

2

Rewriting Logic Semantics of Haskell Pure Haskell Concurrent Haskell Properties of the Semantic Theory

3

Conclusion Summary Future Work

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-3
SLIDE 3

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

Outline

1

Introduction Motivation Preliminaries

2

Rewriting Logic Semantics of Haskell Pure Haskell Concurrent Haskell Properties of the Semantic Theory

3

Conclusion Summary Future Work

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-4
SLIDE 4

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

Why Haskell?

declarative reading use as specification language program transformation to obtain efficient implementation

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-5
SLIDE 5

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

Why Haskell?

declarative reading use as specification language program transformation to obtain efficient implementation need for exact semantics to justify program transformations! yet: semantics of Haskell is well-studied

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-6
SLIDE 6

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

Why yet another semantics?

collect different aspects of the semantics in one semantic framework view the semantics from a different perspective use the semantics as input for tools to analyse the language

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-7
SLIDE 7

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

Why Rewriting Logic?

mature well-studied semantic framework there are rewrite semantics for many other languages implementation for rewriting logic is available remote goal: study relationship between Concurrent Haskell and Rewriting Logic

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-8
SLIDE 8

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

Rewriting Logic

atomic formulae: t − → t′ where t, t′ ∈ TΣ(X) in our context:

t and t′ encode program states t − → t′ reads: “During the computation the program state changes from t to t′.”

term are not taken as pure syntax: reasoning modulo an equational subtheory Membership Equational Logic (MEL): many-kinded term language atomic formulae:

t = t′ t : s “t has sort s”

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-9
SLIDE 9

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

Membership Equational Signature

Definition A membership equational signature (or MEL signature) is a triple Ω = (Σ, S, <), where S is a finite set of sorts, < is a strict order on S, Σ = {Σw,k}(w,k)∈K ∗×K is a K ∗ × K-indexed family of function symbols and K := S/≡< is the set of kinds induced by the equivalence closure of <. [s] will denote the equivalence class of s w.r.t ≡<, i.e. its kind.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-10
SLIDE 10

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

Sorts and Kinds — Example

Example S = {s1, s2, s3, s4, s5} — the set of sorts and < is s.t.

s2 < s1, s3 < s1, s5 < s4.

s1 s2 s3 s5 s4 s1

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-11
SLIDE 11

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

Sorts and Kinds — Example

Example S = {s1, s2, s3, s4, s5} — the set of sorts and < is s.t.

s2 < s1, s3 < s1, s5 < s4.

s1 s2 s3 s5 s4 s1 [s1] [s4]

K = {[s1], [s4]}, where [s1] = {s1, s2, s3} and [s4] = {s4, s5}.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-12
SLIDE 12

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

MEL Sentences, MEL Theories

Definition The following are membership equational sentences (or MEL sentences): (∀X) t = t′ ⇐

i∈I

ui = vi ∧

j∈J

wj : sj (Equation) (∀X) t′′ : s ⇐

i∈I

ui = vi ∧

j∈J

wj : sj (Membership) Definition A membership equational theory (or MEL theory) E is a pair (Ω, E) where Ω is a MEL signature and E a is set of MEL sentences.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-13
SLIDE 13

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

MEL Sentences, MEL Theories

Definition The following are membership equational sentences (or MEL sentences): ui = vi wj : sj t = t′ (Equation) (∀X) t′′ : s ⇐

i∈I

ui = vi ∧

j∈J

wj : sj (Membership) Definition A membership equational theory (or MEL theory) E is a pair (Ω, E) where Ω is a MEL signature and E a is set of MEL sentences.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-14
SLIDE 14

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

MEL Sentences, MEL Theories

Definition The following are membership equational sentences (or MEL sentences): ui = vi wj : sj t = t′ (Equation) ui = vi wj : sj t′′ : s (Membership) Definition A membership equational theory (or MEL theory) E is a pair (Ω, E) where Ω is a MEL signature and E a is set of MEL sentences.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-15
SLIDE 15

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

MEL Semantics in a Nutshell

if t = t′ ⇐

i∈I ui = vi ∧ j∈J wj : sj ∈ E and E ⊢ ui = vi and

E ⊢ wj : sj hold then also E ⊢ t = t′. if t : s ⇐

i∈I ui = vi ∧ j∈J wj : sj ∈ E and E ⊢ ui = vi and

E ⊢ wj : sj hold then also E ⊢ t : s. = is a congruence, sort membership is preserved by =, i.e if E ⊢ t = t′ and E ⊢ t : s then also E ⊢ t′ : s < means “sort” inclusion, i.e if s < s′ and E ⊢ t : s then also E ⊢ t : s′

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-16
SLIDE 16

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

GRL Sentences, Generalised Rewrite Theories

Definition The following is a generalised rewrite sentence (or GRL sentence): (∀X) t − → t′ ⇐

i∈I

ui = vi ∧

j∈J

wj : sj ∧

l∈L

tl − → t′

l

(Rewrite) Definition A generalised rewrite theory (or GRT) is a triple R = (Ω, E, R) where E = (Ω, E) is a MEL theory and R is a set of GRT sentences of signature Ω .

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-17
SLIDE 17

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

GRL Sentences, Generalised Rewrite Theories

Definition The following is a generalised rewrite sentence (or GRL sentence): ui = vi wj : sj tl − → t′

l

t − → t′ (Rewrite) Definition A generalised rewrite theory (or GRT) is a triple R = (Ω, E, R) where E = (Ω, E) is a MEL theory and R is a set of GRT sentences of signature Ω .

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-18
SLIDE 18

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

GRL Semantics in a Nutshell

if (∀X) t − → t′ ⇐

i∈I

ui = vi ∧

j∈J

wj : sj ∧

l∈L

tl − → t′

l ∈ R and

E ⊢ ui = vi, E ⊢ wj : sj, R ⊢ tl − → t′

l then R ⊢ t −

→ t′ plus “nested replacement”! − → is reflexive, transitive and congruent, reasoning about − → is done modulo the MEL subtheory, i.e. if E ⊢ t = u and E ⊢ t′ = u′ then R ⊢ u − → u′ implies R ⊢ t − → t′.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-19
SLIDE 19

Introduction Rewriting Logic Semantics of Haskell Conclusion Motivation Preliminaries

Notation

s0 < s1 < . . . < sn

  • perator declaration:

f : k1 · · · kn → k or c : k

  • f ∈ Σk1···kn,k or c ∈ Σε,k
  • perator declaration on sorts:

f : s1 · · · sn → s or c : s

  • as above plus membership sentence

mixfix operators: e.g. ( \ · -> · ) : Var Term → LambdaAbstraction implicit universal quantification of variables kind/sort of variables given when sort is introduced: e.g. Exp{ei} ∈ S : ei range over sort Exp; [e]i range over kind [Exp] variables ranging over sort

  • additional membership condition

where variable is used “otherwise” condition

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-20
SLIDE 20

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Outline

1

Introduction Motivation Preliminaries

2

Rewriting Logic Semantics of Haskell Pure Haskell Concurrent Haskell Properties of the Semantic Theory

3

Conclusion Summary Future Work

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-21
SLIDE 21

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Coverage of the Semantics

The semantics includes laziness, pattern matching, mutual recursion (function binding), imprecise exceptions (synchronous and asynchronous), I/O and concurrency.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-22
SLIDE 22

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Coverage of the Semantics

The semantics includes laziness, pattern matching, mutual recursion (function binding), imprecise exceptions (synchronous and asynchronous), I/O and concurrency. The semantics does not include recursive pattern bindings, full static semantics, i.e. context sensitive syntax and particularly the type system.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-23
SLIDE 23

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Semantics in Rewriting Logic

How can the semantics of a (programming) language be described in RL/MEL?

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-24
SLIDE 24

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Semantics in Rewriting Logic

How can the semantics of a (programming) language be described in RL/MEL? Approach taken for Pure Haskell define signature Ω s.t. a program of the considered language is a term of MEL (hence, the need for mixfix operators). define operators that transform programs (and fragments of them) into its denotation by giving equational sentences

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-25
SLIDE 25

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Semantics in Rewriting Logic

How can the semantics of a (programming) language be described in RL/MEL? Approach taken for Pure Haskell define signature Ω s.t. a program of the considered language is a term of MEL (hence, the need for mixfix operators). define operators that transform programs (and fragments of them) into its denotation by giving equational sentences Approach taken for Concurrent Haskell programs are also terms include function symbols that construct program states give rewrite sentences that describe the execution of a program by rewriting a program state to some successor state

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-26
SLIDE 26

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Considered Syntax Fragment: Basic Syntax

BinOp ::= + | - | * | / | <= | >= | < | > | /= | == | !! | && | || | ** | ++ | $ | $! BoolConst ::= True | False PDCtor ::= BoolConst | Float | Int | Char | String | () | [] CustCtor ::= < data constructor > Ctor ::= CustCtor | PDCtor Primitive ::= seq | not | raise | ( BinOp ) AtExp ::= Ctor | Var | Primitive AtPat ::= Var | _ | Ctor Case ::= Pat -> Exp Cases ::= Case | Cases ; Case

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-27
SLIDE 27

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Considered Syntax Fragment: Expressions

Exp ::= AtExp | Exp Exp | \ NePatList -> Exp | case Exp of { Cases } | if Exp then Exp else Exp | Exp : Exp | Exp BinOp Exp | [ ExpList ] | ( ExpList ) ExpList ::= Exp | ExpList , Exp

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-28
SLIDE 28

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Considered Syntax Fragment: Patterns, Programs

Pat ::= AtPat | Pat Pat | Pat : Pat | [ PatList ] | ( PatList ) PatList ::= Pat | PatList , Pat FuncBindLhs ::= Var | FuncBindLhs Pat FuncBind ::= FuncBindLhs = Exp Program ::= FuncBind | Program ; FuncBind

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-29
SLIDE 29

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Formulate Haskell Syntax as a MEL Theory: Examples

BNF definition Pat ::= AtPat | Pat Pat | Pat : Pat | [ PatList ] | ( PatList ) Primitive ::= seq | not | raise | . . .

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-30
SLIDE 30

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Formulate Haskell Syntax as a MEL Theory: Examples

BNF definition Pat ::= AtPat | Pat Pat | Pat : Pat | [ PatList ] | ( PatList ) Primitive ::= seq | not | raise | . . . MEL definition AtPat < Pat · · : Pat Pat → Pat · : · : Pat Pat → Pat [ · ] : PatList → Pat ( · ) : PatList → Pat seq : Primitive not : Primitive raise : Primitive . . .

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-31
SLIDE 31

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Haskell Semantics

What is the semantics of a Haskell expression?

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-32
SLIDE 32

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Haskell Semantics

What is the semantics of a Haskell expression? — Its normal form.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-33
SLIDE 33

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Haskell Semantics

What is the semantics of a Haskell expression? — Its normal form. Since Haskell is lazy (two) different other normal forms are considered: Weak head normal form (WHNF) — for pattern matching semantics Constructor applied to several expressions e.g.: (1, 2+3, digitToInt ’3’) or Node (3*5) (1+2) function expression (lambda expression, built-in function, function representation, . . . ) applied to too few arguments e.g.: (+) 2 ; (\x -> x + 1) . Constructor head normal form (CHNF) — for equality semantics same as weak head normal form but constructors must be applied to CHNFs e.g. Node (3*5) (1+2) is not in CHNF; Node 15 3 is the “equivalent” CHNF The overall semantics of a Haskell expression is then its CHNF.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-34
SLIDE 34

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Exceptions

Hence, expressions must be transformed to their CHNF/WHNF. Problem — order of evaluation the order of evaluation of Haskell expressions will not be defined (thanks to referential transparency this is possible): e.g. the “result” of (1+2) * (3+4) does not depend on whether 1+2 or 3+4 is evaluated first. the order of evaluation is significant if exceptions are raised! e.g. the expression (2 / 0) + (raise SomeException) either raises ArithException DivideByZero or SomeException depending on the order of evaluation

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-35
SLIDE 35

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Exceptions

Hence, expressions must be transformed to their CHNF/WHNF. Problem — order of evaluation the order of evaluation of Haskell expressions will not be defined (thanks to referential transparency this is possible): e.g. the “result” of (1+2) * (3+4) does not depend on whether 1+2 or 3+4 is evaluated first. the order of evaluation is significant if exceptions are raised! e.g. the expression (2 / 0) + (raise SomeException) either raises ArithException DivideByZero or SomeException depending on the order of evaluation Solution the semantics of a Haskell expression is either its CHNF or a set of exceptions. the set of exceptions contains all exceptions that are raised with some order of evaluation.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-36
SLIDE 36

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Transformation to Normal Form

constants to identify normal form type: whnf : NfType , chnf : NfType predicate to characterise normal form: · ⇓ · : Exp NfType → Bool expressions can have multiple (imprecise) exceptional behaviour result of transformation is either the normal form or a set of exceptions sort ExpUExc as “union” of Exp and Exceptions transformation function F · · : [Exp] [NfType] → [ExpUExc] Note: As both normal forms share some properties and the resp.

  • perators are parametric w.r.t. the normal form type, some sentences can

be shared by both normal forms.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-37
SLIDE 37

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Semantics of Haskell Programs

Programs induce substitutions each Haskell program induces a substitution every function defines by which expression the function name can be replaced

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-38
SLIDE 38

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Semantics of Haskell Programs

Programs induce substitutions each Haskell program induces a substitution every function defines by which expression the function name can be replaced Example Haskell function foo 1 y = y ; foo 2 y = 4 * y

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-39
SLIDE 39

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Semantics of Haskell Programs

Programs induce substitutions each Haskell program induces a substitution every function defines by which expression the function name can be replaced Example Haskell function foo 1 y = y ; foo 2 y = 4 * y

  • MEL representation

foo → { 1 , y :→ y ; 2 , y :→ 4 * y }

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-40
SLIDE 40

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Semantics of Haskell Programs

Programs induce substitutions each Haskell program induces a substitution every function defines by which expression the function name can be replaced Example Haskell function foo 1 y = y ; foo 2 y = 4 * y

  • MEL representation

foo → { 1 , y :→ y ; 2 , y :→ 4 * y } Semantics of a program The semantics of a Haskell program is the “least fixed point” of the substitution induced by the program. Taking the least fixed point enables mutual recursion!

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-41
SLIDE 41

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Program Semantics

Symbols env( · ) : Program → SimpSubst H · : Program → Subst H · in · : [Exp] [Program] → [ExpUExc] Sentences Hp1 = fix(env(p1)) program s1 = Hp1 He1 in p1 = F

  • e1[s1]
  • chnf

expression

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-42
SLIDE 42

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

What is Concurrent Haskell

New concepts Thread MVar: shared memory, synchronisation New primitives in the IO monad forkIO :: IO a -> IO ThreadId — spawns a thread newEmptyMVar :: IO (MVar a) — creates new empty MVar putMVar :: MVar a -> a -> IO () — stores value into an empty MVar takeMVar :: MVar a -> IO a — reads MVar’s content throw :: Exception -> IO () — raises an exception

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-43
SLIDE 43

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

What is Concurrent Haskell

New concepts Thread MVar: shared memory, synchronisation New primitives in the IO monad (cont.) catch :: IO a -> (Exception -> IO a) -> IO a — handles an exception throwTo :: ThreadId -> Exception -> IO () — forces a thread to raise an (asynchronous) exception block / unblock :: IO a -> IO a —disallows / allows asynchronous exceptions sleep , myThreadId

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-44
SLIDE 44

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Peyton Jones’ Semantics — Program States

Program States P, Q, R ::= ✭M✮t thread of computation named t | ✵t finished thread named t | m empty MVar named m | Mm full MVar named m, holding M | ❫te❴ pending asynchronous exception e for thread t | νx.P restriction | P | Q parallel composition

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-45
SLIDE 45

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Structural Congruence

Defines which program states are considered equal. Commutativity and associativity P | Q ≡ Q | P (Comm) P | (Q | R) ≡ (P | Q) | R (Assoc) Straightforward translation into equational sentences. Restrictions νx.νy.P ≡ νy.νx.P (Swap) (νx.P) | Q ≡ νx.(P | Q) x ∈ fn(Q) (Extrude) νx.P ≡ νy.P[y/x] y ∈ fn(P) (Alpha) Cannot be translated into an executable MEL theory.

Skip details on MEL-representation Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-46
SLIDE 46

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Program States as MEL Terms — Modularity

Extensibility: Record structure technique! instead of using only a single function symbol as a constructor (where each argument position defines some component) use a record structure, whose components are indexed by a name. Example Function symbol approach: definition: ✭ · , · , · ✮: ThreadId ThreadCont Flag → Thread example term: ✭1Th, ε, no✮. Record structure approach: definition: a bit more complicated example term: ✭tid : 1Th val : ε stuck : no✮

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-47
SLIDE 47

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Program States

The record structure of a program state contains input

  • utput

thread id generator MVar id generator process pool (contains threads, MVars, asynchronous exceptions)

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-48
SLIDE 48

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Process Pool

Thread, MVar, AException < Proc < ProcPool null : ProcPool · | · : ProcPool ProcPool → ProcPool Plus some sentences making | associative and commutative and make null its identity. pp1 | (pp2 | pp3) = (pp1 | pp2) | pp3

assoc

pp1 | pp2 = pp2 | pp1

comm

pp1 | null = pp1

id

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-49
SLIDE 49

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Thread as a Record Structure

Components tid: the thread’s id val: the thread’s value stuck: flag indicating whether the thread is stuck Example finished thread: ✵◦

t

  • ✭tid : 1Th

val : ε stuck : no✮ stuck thread: ✭ MVar m 0✮•

t

  • ✭tid : 0Th

val : putMVar 1MV 0 stuck : yes✮

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-50
SLIDE 50

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

MVar as a Record Structure

Components mid: the MVar’s id cont: the MVar’s content Example empty MVar: m

  • mid : 1MV

cont : ε full MVar holding 1 : 1 m

  • mid : 0MV

cont : 1

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-51
SLIDE 51

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Asynchronous Exception as a Function Symbol

❫ · · ❴ : ThreadId ExcExp → AException tgt( · ) : AException → ThreadId exc( · ) : AException → ExcExp tgt(❫ti1ee1❴) = ti1

tgt

exc(❫ti1ee1❴) = ee1

exc

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-52
SLIDE 52

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Initial Program States

Initial state of a program w.r.t an expression C · in · ( · ) : Exp Program String → State (1) Ce1 in pr1(str1) = { | in : str1I

  • ut : O

tgen : newThreadIdGen mgen : newMVarIdGen pool : ✭H unblock e1 in pr1✮mainThreadId | } Initial state of a program C · ( · ) : Program String → State Cpr1(str1) = C main in pr1(str1)

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-53
SLIDE 53

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Rules of Peyton Jones’ Semantics

Structural rules P

α

→ Q P | R

α

→ Q | R

(Par)

P

α

→ Q νx.P

α

→ νx.Q

(Nu)

P ≡ P′ P′

α

→ Q′ Q ≡ Q′ P

α

→ Q

(Equiv)

These rules are covered by the semantics of rewriting logic, i.e. deduction rules (Cong) and (Eq). For each primitive there is at least one axiom.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-54
SLIDE 54

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Axioms of the Concurrency Semantics — An Example

Original axiom for throwTo ✭❊ [ throwTo t e] ✮u → ✭❊ [ return () ] ✮u | ❫te❴ ✭ ✮ ✭ ✮ ❫ ❴

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-55
SLIDE 55

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Axioms of the Concurrency Semantics — An Example

Original axiom for throwTo ✭❊ [ throwTo t e] ✮u → ✭❊ [ return () ] ✮u | ❫te❴ Rewrite sentence formulation E(e1) = throwTo ti1 ece1 E(e1 ← return () ) = e2 ✭val : e1 prt1✮ − → ✭val : e2 prt1✮ | ❫ti1ece1❴

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-56
SLIDE 56

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Axioms of the Concurrency Semantics — Another Example

Original axiom for forkIO ✭❊ [ forkIO M] ✮t → νu.(✭❊ [ return u] ✮t | ✭ unblock M✮u) where u ∈ fn(❊, M) ✭ ✮ ✭ ✮ ✭ ✮

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-57
SLIDE 57

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Axioms of the Concurrency Semantics — Another Example

Original axiom for forkIO ✭❊ [ forkIO M] ✮t → νu.(✭❊ [ return u] ✮t | ✭ unblock M✮u) where u ∈ fn(❊, M) Rewrite sentence formulation E(e1) = forkIO e3 currentId(tg1) = ti1 E(e1 ← return ti1) = e2 pool : ✭val : e1 prt1✮ | pp1 tgen : tg1 pool : ✭val : e2 prt1✮ | ✭ unblock e3✮ti1 | pp1 tgen : nextGen(tg1) − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − − →

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-58
SLIDE 58

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Executability

Executability subsumes the following properties preregularity: terms have a smallest sort if any equations are applied from left to right

  • rder-sorted term rewriting system →E

→E must be:

ground terminating ground confluent ground sort decreasing

but: associativity and commutativity sentences can be disregarded for this properties the TRS →R induced by the rewrite sentences must be coherent w.r.t. →E

Skip executability Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-59
SLIDE 59

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Coherence

That is, the following diagramm has to commute: t R ✲ t′ u E ! ✲ s E! ❄ R ✲ s′ E ! ✲

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-60
SLIDE 60

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Properties Providing Executability

Properties of the semantic theory of Concurrent Haskell Preregularity Confluence Sort-decreasingness Coherence Termination

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-61
SLIDE 61

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Properties Providing Executability

Properties of the semantic theory of Concurrent Haskell Preregularity ✔ Confluence Sort-decreasingness Coherence Termination

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-62
SLIDE 62

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Properties Providing Executability

Properties of the semantic theory of Concurrent Haskell Preregularity ✔ Confluence ✔ Sort-decreasingness Coherence Termination

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-63
SLIDE 63

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Properties Providing Executability

Properties of the semantic theory of Concurrent Haskell Preregularity ✔ Confluence ✔ Sort-decreasingness ✔ Coherence Termination

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-64
SLIDE 64

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Properties Providing Executability

Properties of the semantic theory of Concurrent Haskell Preregularity ✔ Confluence ✔ Sort-decreasingness ✔ Coherence ✔ Termination

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-65
SLIDE 65

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Properties Providing Executability

Properties of the semantic theory of Concurrent Haskell Preregularity ✔ Confluence ✔ Sort-decreasingness ✔ Coherence ✔ Termination ✘

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-66
SLIDE 66

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Properties Providing Executability

Properties of the semantic theory of Concurrent Haskell Preregularity ✔ Confluence ✔ Sort-decreasingness ✔ Coherence ✔ Termination ✘ Termination issue Problem: Ftchnf does not have a normal form if the Haskell expression represented by t diverges. Hence: The semantic theory is only executable for converging expressions. Nevertheless: Divergence is exactly described by the theory: The Haskell expression represented by t converges iff EC ⊢ Ftchnf : ExpUExc.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-67
SLIDE 67

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Relation to Moran’s Coinductive Semantics of Imprecise Exceptions

Theorem Let EH be the semantic MEL theory for pure Haskell. Then the following equivalences hold true: (i) M ⇓ V iff EH ⊢ F

  • M
  • whnf = V

(ii) M ր S iff EH ⊢ F

  • M
  • whnf = S

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-68
SLIDE 68

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Relation to Moran’s Coinductive Semantics of Imprecise Exceptions

Theorem Let EH be the semantic MEL theory for pure Haskell. Then the following equivalences hold true: (i) M ⇓ V iff EH ⊢ F

  • M
  • whnf = V

(ii) M ր S iff EH ⊢ F

  • M
  • whnf = S

Corollary M ⇑ iff EH F

  • M
  • whnf : ExpUExc

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-69
SLIDE 69

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Relation to Peyton Jones’ SOS of Concurrent Haskell

Theorem Let P and Q be program states and RC the rewrite theory of the Concurrent Haskell semantics. Then, excluding functional nontermination, the following holds: P →∗ Q iff ∃s ∈ P, s′ ∈ Q. RC ⊢ s − → s′

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-70
SLIDE 70

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Relation to Peyton Jones’ SOS of Concurrent Haskell

Non-executable extension of RC to R′

C

· ⇑: Exp → Bool Fe1whnf : ExpUExc e1 ⇑ = false

div1

  • therwise

e1 ⇑ = true

div2

E(e1) = e3 e3 ⇑ = true E(e1 ← throw ece1) = e2 val : e1 − → val : e2

Raise div

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-71
SLIDE 71

Introduction Rewriting Logic Semantics of Haskell Conclusion Pure Haskell Concurrent Haskell Properties of the Semantic Theory

Relation to Peyton Jones’ SOS of Concurrent Haskell

Non-executable extension of RC to R′

C

· ⇑: Exp → Bool Fe1whnf : ExpUExc e1 ⇑ = false

div1

  • therwise

e1 ⇑ = true

div2

E(e1) = e3 e3 ⇑ = true E(e1 ← throw ece1) = e2 val : e1 − → val : e2

Raise div

Theorem Let P and Q be program states Then the following holds: P →∗ Q iff ∃s ∈ P, s′ ∈ Q. R′

C ⊢ s −

→ s′

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-72
SLIDE 72

Introduction Rewriting Logic Semantics of Haskell Conclusion Summary Future Work

Outline

1

Introduction Motivation Preliminaries

2

Rewriting Logic Semantics of Haskell Pure Haskell Concurrent Haskell Properties of the Semantic Theory

3

Conclusion Summary Future Work

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-73
SLIDE 73

Introduction Rewriting Logic Semantics of Haskell Conclusion Summary Future Work

Summary

Dynamic semantics for Concurrent Haskell including most of the language features:

laziness pattern matching mutual recursion imprecise (a)synchronous exceptions I/O concurrency

Proofs for equivalence to different existing semantics. The given theory is executable in the Maude system, i.e.:

interpreter semi-automated inductive proofs model checking . . .

Modularity ensures extendibility of the semantics to include further features as well as flexibility to change the semantics.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-74
SLIDE 74

Introduction Rewriting Logic Semantics of Haskell Conclusion Summary Future Work

Future Work

Minor features of the dynamic semantics are missing (in particular recursive pattern bindings). How can rewrite sentences be used to describe imprecise exceptions more naturally? A formulation of the static semantics in rewriting logic is still missing! Particularly the type calculus of Haskell is interesting. Amalgamation of the dynamic and static semantics in one framework is desirable, as both are interwoven (ad-hoc polymorphism)! Use of free theorems derived from the type information in semi-automated proofs.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell

slide-75
SLIDE 75

For Further Reading

José Meseguer and Grigore Rou. The rewriting logic semantics project.

  • Theor. Comput. Sci., 373(3):213–237, 2007.

Andrew Moran, Søren B. Lassen, and Simon Peyton Jones. Imprecise exceptions, co-inductively. In HOOTS ’99, Higher Order Operational Techniques in Semantics, ENTCS 26, pages 122–141, 1999. Simon Marlow, Simon L. Peyton Jones, Andrew Moran, and John H. Reppy. Asynchronous exceptions in haskell. In SIGPLAN Conference on Programming Language Design and Implementation, pages 274–285, 2001.

Patrick Bahr An Executable Rewriting Logic Semantics for Concurrent Haskell