29
Haskell-Coloured Petri Nets Implementation and Applications (a work-in-progress talk) Canterbury, Kent 05 August 2004 Claus Reinke

Haskell-Coloured Petri Nets Implementation and Applications ( a work-in-progress talk ) Canterbury, Kent 05 August 2004 Claus Reinke

Embed Size (px)

Citation preview

Haskell-ColouredPetri Nets

Implementation and Applications

(a work-in-progress talk)

Canterbury, Kent

05 August 2004

Claus Reinke

(high-level) Petri nets

• Petri nets are a generalisation of finite automata for concurrent and distributed systems:– distributed state

– local state transitions

• places hold multisets of resources, marked by anonymous ("black") tokens

• transitions consume resources from input places and produce resources on output places

• high-level nets annotate ("colour") tokens with data objects of some programming language, manipulated by transition inscriptions in the same language

HCPN

• Haskell-Coloured Petri Nets [Reinke 1999] are high-level Petri nets using Haskell as the inscription language

• some related tools (and inscription languages):– Design/CPN [Jensen et.al. 1989-2003]: Standard ML

– CPN Tools [Jensen et.al. 2003-]: Standard ML

– Graph [Schepers et.al. 1988-1992]: Kiel Reduction Language

– Genomic Object Net [Matsuno et.al. 2003] : unspecified functional language (HFPN are a hybrid functional Petri net formalism, used for representing and simulating bio-pathways)

– Cell Illustrator [Gene Networks Inc. 2003]: commercial version of GON

• basic actions: – consumer

– producer

• composition/communication:– asynchronous (buffered)

– synchronous (no buffer)

– concurrency (independent)

– conflict (sharing)

– coincidence (synchronisation)

building net models (building blocks)

example: critical region(mutually exclusive access to shared resource)

• system to be modelled:– two processes A and B, one shared resource

– each process does some non-critical computations,

– but occasionally needs exclusive access to the shared resource,

– when it gets access to the shared resource, it will enter its "critical region" to work with the resource,

– when done with the resource, the process will release it, and continue with non-critical computations

• a standard os/concurrency example

critical region – stage 1(mutually exclusive access to shared resource)

critical region – stage 2(mutually exclusive access to shared resource)

example: critical region(mutually exclusive access to shared resource)

– places have Haskell types

– consumer arcs can be labelled with patterns, transitions with boolean guards (pattern match failure or false guards disable)

– producer arcs can be labelled with expressions

– synchronised actions share input variable environments

towards HCPN models

Just a (a>0)

1+2

Just x

y (y/=0)

"done"

x/y

building net models (techniques)

• find the sequential components in your system, and model their state transitions as finite automatons

• compose the sequential subsystems as a net– asynchronous communication via buffer places (may be

producer/consumer causal relationship, or conflict about shared resources)

– synchronisation of transitions in separate subsystems

• identify identical subsystems– use labeled sum types to distinguish tokens in subsystems

– fold the subsystems together, using functional abstraction

• model static system aspects in the net structure

• model dynamic aspects in the net markings

• think about system invariants

• ..

example: starving philosophers

• actually, "dining philosophers":-)• system to be modelled:

– three philosophers sit a round table

– between each pair of philosophers is a fork

– the philosophers think about the worlds problems

– when a philosopher gets hungry, he takes his right and left fork, and starts to eat

– when a philosopher has sated his appetite, he puts both forks back down and resumes his higher pursuits

• typical abstraction of real-world concurrency problems, especially a more complex variant of shared resources

philosophers – stage 1

philosophers – stage 2

philosophers – stage 3

example: starving philosophers

philosophers – stage 4(adding colour)

philosophers – stage 5(folding structure into colour)

example: folded philosophers

we could fold further, until we're left with one transition/place;the trick is to find the right balance between structure and colour

example: erlang resource locker

a variable number of processes sharing several resourcesone process mover can move non-critical processes to different node

example: erlang resource locker

a variable number of processes sharing several resourcesone process mover can move non-critical processes to different node

embedding HCPN in Haskell (I)

• implementing a HCPN toolchain requires at least– graphical editor– net simulator– functional inscription language evaluator

• that is too much work (about 3 years?)!• to avoid re-implementing Haskell, need eval.., but:

Haskell doesn't offer runtime reflection • so we generate Haskell code from HCPN instead• the transition firing rule is tightly interwoven with

evaluation of Haskell patterns and guards, so we should deal with nets and inscriptions in the same code generation step

embedding HCPN in Haskell (II)places and markings

• the places of a net form a heterogeneous collection – maps to a record, with a field for each place

– (may need to revisit this when nets get modular)

• each place marking is a homogeneous multiset– use lists for now

– (probably inefficient for large markings; revisit then)

• so the places of a net map to a record type, and net markings map to records of this type, nice and simple:

data Mark = Mark { place_n :: [type_of_place_n] }mark = Mark { place_n = [initial_marking_of_place_n]}

embedding HCPN in Haskell (IIIa)transitions, arc labels, guards

• firing rule for an HCPN transition:– transition is enabled iff on each input place there is a token

matching the label on the corresponding input arc and the guard evaluates to True under the bindings introduced by the pattern matches

– enabled transition fires by consuming the enabling tokens from its input places and producing tokens on its output places; the values of the latter tokens are determined by the expression labels on the corresponding output arcs under the bindings introduced via the enabling pattern matches

• each transition can be enabled by multiple bindingstrans_n :: Mark -> [Mark]

embedding HCPN in Haskell (IIIb)transitions, arc labels, guards

t :: Mark -> [Mark]t m = do

(Just x,a_rest) <- select (a m)(y,b_rest) <- select (b m)if (y/=0)

then return m{a = a_rest ,b = b_rest ,c = ("done"):(c m) ,d = (x/y):(d m) }

else fail "guard failed"

Just x

y (y/=0)

"done"

x/y

t c

d

a

b

embedding HCPN in Haskell (IV)whole net and simulation loop

net_declarationsdata Mark = Mark { place_n :: [type_of_place_n] }mark = Mark { place_n = [initial_marking_of_place_n]}t_n :: Mark -> [Mark]net = Net{ Trans{name="t_n",action=t_n} }

main = run net markrun net marking = print marking >> if null enabledTs then putStrLn "no more enabled transitions!" else do trans <- choose enabledTs putStrLn (fst $ fst trans) run net (snd trans) where enabledTs = enabled net marking

embedding HCPN in Haskell (V)graphics

• nowadays, Haskell has (once again..) GUI libs• chose wxHaskell (no frills, but portable, aims for the long run,

and was agreed on as the lib to build on by the Haskell GUI folks; first higher-level libs based on it already on the way)

• build graphical net editor and Haskell code exporter [done]• invert simulation loop (drive it by timer events so it can be

slowed down to a visible 2 steps/sec) [done]• interface simulation loop to graphics [done]

– load and display net graphics– map transition and place names to node ids in loaded net– generate showMarking function that updates marking

• compilation&loading of generated code from editor – [in progress]– interpret error messages in terms of source net!

experience so far..• Petri nets and Haskell combine nicely (Haskell is good for

abstract languages/machines and Petri nets are great for concurrency concepts) could now use Haskell, e.g., in OS lectures instead of just compiler lectures, or in concurrent system design!

• HCPN now have initial tool support (graphical editing, code export, graphical simulation) – you can use them!

• original estimate was 2 weeks for editor and simulator; textual simulation was only a day, but fiddling with graphics issues and overall design took longer (lack of recent GUI experience; GUI lib still in development); now slightly over a month of work;

• doing practical stuff in Haskell (with GUI) is fun!• lots of by-hand refactoring (mostly to grow or change design,

i.e., for experimentation and development, not for cleanup!)• lots remains to be done (continued fiddling and bug fixing, but

also support for hierarchical nets, other arc types, state space generation/exploration, ..)

embedding a Pi-calculus in HCPN

embedding a Turing machine