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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  9. • 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  12. •Actively-developed
    •Growing community

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  24. 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 full-size slide

  25. 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 full-size slide

  26. 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 full-size slide

  27. 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 full-size slide

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

    View full-size slide

  29. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  36. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  41. PISTACHE
    Pedro Matiello
    Ana Cristina de Melo

    View full-size slide