Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Pistache: A π-Calculus Internal Domain Specific Language for Scala

Pedro Matiello
September 26, 2011

Pistache: A π-Calculus Internal Domain Specific Language for Scala

Presented at: XIV Simpósio Brasileiro de Métodos Formais (SBMF 2011), September/2011.

Pedro Matiello

September 26, 2011
Tweet

More Decks by Pedro Matiello

Other Decks in Research

Transcript

  1. PISTACHE
    A π-Calculus Internal Domain Specific Language for Scala

    View Slide

  2. •Pedro Matiello
    [email protected]
    •Ana Cristina de Melo
    [email protected]
    •http://code.google.com/p/pistache/

    View Slide

  3. • Pistache is an implementation of the π-Calculus as a domain
    specific language hosted in Scala
    INTRODUCTION

    View Slide

  4. • It provides π-Calculus’ abstractions for concurrent
    computation within the Scala programming language
    INTRODUCTION

    View Slide

  5. π-CALCULUS

    View Slide

  6. •π-Calculus is a formal language for describing
    concurrent computation with dynamic
    reconfiguration

    View Slide

  7. •Agents communicate by exchanging names through
    channels (which are also names)
    •Connections between agents may change in the
    course of the computation

    View Slide

  8. AGENTS
    0 Nil
    α.P Prefix
    P + Q Sum
    PʛQ Parallel
    (νx)P Restriction
    [x=y].P Match
    [x≠y].P Mismatch

    View Slide

  9. PREFIXES
    yx Output
    y(x) Input
    τ Silent
    _

    View Slide

  10. • The Agents:
    C = (νz) y(p).pz
    S = yx.S
    P = x(w).α.P
    • The composition:
    C | S | P
    &
    6
    3
    \ [
    _
    _
    EXAMPLE
    Example adapted from: An Introduction to the pi-Calculus, by Joachim Parrow

    View Slide

  11. SCALA

    View Slide

  12. •Scala is a general-purpose programming language
    providing features both of object-oriented and
    functional programming

    View Slide

  13. •Flexible syntax
    •Statically-typed
    •Runs on the Java Virtual Machine

    View Slide

  14. •Actively-developed
    •Growing community

    View Slide

  15. PISTACHE

    View Slide

  16. •Pistache is an implementation of π-Calculus as an
    internal Domain Specific Language for Scala

    View Slide

  17. val P = Agent(...)
    lazy val recP:Agent = Agent(...)
    val restrP = Agent {
    val restrictedName = Name(...)
    ...
    }
    AGENT DEFINITION

    View Slide

  18. val name = Name(some_object)
    val name = Name[Type]
    name := other_object
    value = name.value
    NAMES

    View Slide

  19. val y = Link[Type]
    y~x yx
    y(x) y(x)
    CHANNELS
    _

    View Slide

  20. val silent = Action{ doSomething() } τ
    SILENT TRANSITIONS

    View Slide

  21. val P = Agent { p1 * p2 * Q } P = α.β.Q
    CONCATENATION

    View Slide

  22. val P = Agent { Q | R | S } P = Q | R | S
    COMPOSITION

    View Slide

  23. val P = Agent {
    (p1 :: Q) + (p2 :: R) + (p3 :: S) P = αQ + βR + γS
    }
    SUMMATION

    View Slide

  24. val P = Agent(If (x==y) {Q}) P = [x=y] Q
    MATCHING

    View Slide

  25. • The Agents:
    C = (νz) y(p).pz
    S = yx.S
    P = x(w).α.P
    • The composition:
    C | S | P
    &
    6
    3
    \ [
    _
    _
    EXAMPLE

    View Slide

  26. val y = Link[Link[String]]
    val x = Link[String]
    EXAMPLE

    View Slide

  27. val y = Link[Link[String]]
    val x = Link[String]
    val C = Agent {
    val p = Name[Link[String]]
    val z = "message"
    y(p) * p~z
    }
    EXAMPLE
    C = (νz) y(p).pz
    _

    View Slide

  28. val y = Link[Link[String]]
    val x = Link[String]
    val C = Agent {
    val p = Name[Link[String]]
    y(p) * p~"message"
    }
    lazy val S:Agent = Agent { y~x*S }
    EXAMPLE
    S = yx.S
    _

    View Slide

  29. val y = Link[Link[String]]
    val x = Link[String]
    val C = Agent {
    val p = Name[Link[String]]
    y(p) * p~"message"
    }
    lazy val S:Agent = Agent { y~x*S }
    lazy val P:Agent = Agent {
    val w = Name[String]
    val act = Action { println(msg.value) }
    x(w) * act * P
    }
    EXAMPLE
    P = x(w).α.P

    View Slide

  30. val y = Link[Link[String]]
    val x = Link[String]
    val C = Agent {
    val p = Name[Link[String]]
    y(p) * p~"message"
    }
    lazy val S:Agent = Agent { y~x*S }
    lazy val P:Agent = Agent {
    val msg = Name[String]
    val act = Action { println(msg.value) }
    x(msg) * act * P
    }
    new ThreadedRunner(C | S | P) start
    EXAMPLE
    C | S | P

    View Slide

  31. MESSAGE PASSING
    • Channels are implemented as shared buffers
    • Communication between agents is synchronous

    View Slide

  32. MESSAGE PASSING
    Output yx Input y(x)
    Wait until y is empty Wait until y is not empty
    Put x on y Put the contents of y in x
    Signal y not empty Signal y empty
    Wait until y is empty
    _

    View Slide

  33. val P = Agent ( p1 * p2 * p3 * Q )
    DATA STRUCTURE

    View Slide

  34. val P = Agent ( p1 * p2 * p3 * Q )
    DATA STRUCTURE
    3
    S
    &RQFDWHQDWLRQ$JHQW
    &RQFDWHQDWLRQ3UHIL[
    &RQFDWHQDWLRQ3UHIL[
    4
    S
    S

    View Slide

  35. def execute(agent:PiObject) {
    agent match {
    case ConcatenationAgent(left, right) => ...
    case CompositionAgent(left, right) => ...
    ...
    }
    }
    EXECUTION

    View Slide

  36. case ConcatenationAgent(left, right) =>
    execute(left apply)
    execute(right apply)
    EXECUTION

    View Slide

  37. case CompositionAgent(left, right) =>
    executeInNewThread(left apply)
    executeInNewThread(right apply)
    EXECUTION

    View Slide

  38. def executeInNewThread(agent:PiObject) {
    val runnable = new Runnable() {
    override def run() { execute(agent) }
    }
    executor.execute(runnable)
    }
    THREAD SPAWNING

    View Slide

  39. THREAD SPAWNING
    • CachedThreadPool
    • Caches finished threads
    • Reuses cached threads
    • Creates new threads if none are available
    • Deletes from the pool threads that have not been used
    reused for 60 seconds

    View Slide

  40. THREAD SPAWNING
    Strategy Time consumed for 100k agents
    new Thread 23 743 ms
    CachedThreadPool 2 089 ms

    View Slide

  41. • We knew that:
    • Concurrent programming is hard
    CONCLUSION

    View Slide

  42. • We learned that:
    • Proper abstractions can improve our understanding of
    concurrency and concurrent programs
    CONCLUSION

    View Slide

  43. • We also learned that:
    • The abstractions present in π-Calculus provide a feasible
    model for concurrency in actual software programming
    CONCLUSION

    View Slide

  44. PISTACHE
    Pedro Matiello
    Ana Cristina de Melo

    View Slide