diff options
author | michelou <michelou@epfl.ch> | 2009-06-16 10:49:14 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2009-06-16 10:49:14 +0000 |
commit | 53ed9b920e738e784289548d7cc90e8e2dcd0124 (patch) | |
tree | 9c6f7ef79cdc127c77c4105ef63a44833870d707 /docs | |
parent | a4bdfdcccb498aeec2511f9230647ec27da6ff41 (diff) | |
download | scala-53ed9b920e738e784289548d7cc90e8e2dcd0124.tar.gz scala-53ed9b920e738e784289548d7cc90e8e2dcd0124.tar.bz2 scala-53ed9b920e738e784289548d7cc90e8e2dcd0124.zip |
added headers, svn keywords, updated pilib exam...
added headers, svn keywords, updated pilib examples
Diffstat (limited to 'docs')
-rw-r--r-- | docs/examples/pilib/piNat.scala | 27 | ||||
-rw-r--r-- | docs/examples/pilib/scheduler.scala | 42 | ||||
-rw-r--r-- | docs/examples/pilib/semaphore.scala | 34 | ||||
-rw-r--r-- | docs/examples/pilib/twoPlaceBuffer.scala | 32 |
4 files changed, 67 insertions, 68 deletions
diff --git a/docs/examples/pilib/piNat.scala b/docs/examples/pilib/piNat.scala index 8f0b11e27e..ee9e5ba1af 100644 --- a/docs/examples/pilib/piNat.scala +++ b/docs/examples/pilib/piNat.scala @@ -1,38 +1,37 @@ package examples.pilib import scala.concurrent.pilib._ -//import pilib._; /** Church encoding of naturals in the Pi-calculus */ object piNat extends Application { /** Locations of Pi-calculus natural */ - class NatChan extends Chan[Triple[Chan[unit], Chan[NatChan], Chan[NatChan]]] + class NatChan extends Chan[Triple[Chan[Unit], Chan[NatChan], Chan[NatChan]]] /** Zero */ - def Z(l: NatChan): unit = choice ( + def Z(l: NatChan): Unit = choice ( l * { case Triple(z, sd, d) => z.write(()) } ) /** Successor of Double */ - def SD(n: NatChan, l: NatChan): unit = choice ( + def SD(n: NatChan, l: NatChan): Unit = choice ( l * { case Triple(z, sd, d) => sd.write(n) } ) /** Double */ - def D(n: NatChan, l: NatChan): unit = choice ( + def D(n: NatChan, l: NatChan): Unit = choice ( l * { case Triple(z, sd, d) => d.write(n) } ) /** Make "l" a location representing the natural "n" */ - def make(n: int, l: NatChan): unit = + def make(n: Int, l: NatChan): Unit = if (n == 0) Z(l) else if (n % 2 == 0) { val l1 = new NatChan; spawn < D(l1, l) >; make(n/2, l1) } else { val l1 = new NatChan; spawn < SD(l1, l) >; make(n/2, l1) } /** Consume the natural "m" and put it successor at location "n" */ - def Succ(m: NatChan, n: NatChan): unit = { - val z = new Chan[unit] + def Succ(m: NatChan, n: NatChan) { + val z = new Chan[Unit] val sd = new Chan[NatChan] val d = new Chan[NatChan] spawn < m.write(Triple(z, sd, d)) >; @@ -44,8 +43,8 @@ object piNat extends Application { } /** Consume the natural "l" and put two copies at locations "m" and "n" */ - def Copy(l: NatChan, m: NatChan, n: NatChan): unit = { - val z = new Chan[unit] + def Copy(l: NatChan, m: NatChan, n: NatChan) { + val z = new Chan[Unit] val sd = new Chan[NatChan] val d = new Chan[NatChan] spawn < l.write(Triple(z, sd, d)) >; @@ -61,8 +60,8 @@ object piNat extends Application { } /** Consume the natural at location "n" and return its value */ - def value(n: NatChan): int = { - val z = new Chan[unit] + def value(n: NatChan): Int = { + val z = new Chan[Unit] val sd = new Chan[NatChan] val d = new Chan[NatChan] spawn < n.write(Triple(z, sd, d)) >; @@ -84,7 +83,7 @@ object piNat extends Application { make(i, l) | Copy(l, l1, l2) | Succ(l2, l3) | - System.out.println("" + i + " = " + value(l1)) | - System.out.println("succ " + i + " = " + value(l3)) > + println("" + i + " = " + value(l1)) | + println("succ " + i + " = " + value(l3)) > } diff --git a/docs/examples/pilib/scheduler.scala b/docs/examples/pilib/scheduler.scala index 8946a5a0b2..9205ae3f0c 100644 --- a/docs/examples/pilib/scheduler.scala +++ b/docs/examples/pilib/scheduler.scala @@ -7,14 +7,14 @@ object scheduler { /** * Random number generator. */ - val random = new java.util.Random() + val random = new util.Random() //***************** Scheduler ******************// /** * A cell of the scheduler whose attached agent is allowed to start. */ - def A(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit = { + def A(a: Chan[Unit], b: Chan[Unit])(d: Chan[Unit], c: Chan[Unit]) { ///- ... complete here ... choice ( a * { x => C(a, b)(d, c) }) ///+ @@ -23,7 +23,7 @@ object scheduler { /** * A cell of the scheduler in another intermediate state. */ - def C(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit = { + def C(a: Chan[Unit], b: Chan[Unit])(d: Chan[Unit], c: Chan[Unit]) { ///- ... complete here ... choice (c * { x => B(a, b)(d, c) }) ///+ @@ -32,7 +32,7 @@ object scheduler { /** * A cell of the scheduler whose attached agent is allowed to finish. */ - def B(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit = { + def B(a: Chan[Unit], b: Chan[Unit])(d: Chan[Unit], c: Chan[Unit]) { ///- ... complete here ... // choice (b * { x => D(a, b)(d, c) }) // incorrect naive solution choice ( @@ -45,7 +45,7 @@ object scheduler { /** * A cell of the scheduler whose attached agent is not yet allowed to start. */ - def D(a: Chan[unit], b: Chan[unit])(d: Chan[unit], c: Chan[unit]): unit = { + def D(a: Chan[Unit], b: Chan[Unit])(d: Chan[Unit], c: Chan[Unit]) { ///- ... complete here ... choice (d(()) * A(a, b)(d, c)) ///+ @@ -53,16 +53,16 @@ object scheduler { //***************** Agents ******************// - def agent(i: Int)(a: Chan[unit], b: Chan[unit]): unit = { + def agent(i: Int)(a: Chan[Unit], b: Chan[Unit]) { // 50% chance that we sleep forever if (i == 0 && random.nextInt(10) < 5) { - a.attach(x => System.out.println("Start and sleeps ----> " + i)) + a.attach(x => println("Start and sleeps ----> " + i)) Thread.sleep(random.nextInt(1000)) a.write(()) } else { - a.attach(x => System.out.println("Start ----> " + i)) - b.attach(x => System.out.println("Stop -> " + i)) + a.attach(x => println("Start ----> " + i)) + b.attach(x => println("Stop -> " + i)) Thread.sleep(random.nextInt(1000)) a.write(()) Thread.sleep(random.nextInt(1000)) @@ -77,7 +77,7 @@ object scheduler { * Creates a scheduler for five agents (programs). */ - def main(args: Array[String]): unit = { + def main(args: Array[String]) { val agentNb = 5 val agents = List.range(0, agentNb) map agent scheduleAgents(agents) @@ -89,22 +89,22 @@ object scheduler { * A cell is modelled as a function that takes as parameters * input and output channels and which returns nothing. */ - type Cell = (Chan[unit], Chan[unit]) => unit + type Cell = (Chan[Unit], Chan[Unit]) => Unit /** * Creates a cell composed of two cells linked together. */ def join(cell1: Cell, cell2: Cell): Cell = - (l: Chan[unit], r: Chan[unit]) => { - val link = new Chan[unit]; + (l: Chan[Unit], r: Chan[Unit]) => { + val link = new Chan[Unit]; spawn < cell1(l, link) | cell2(link, r) > }; /** * Links the output of a cell to its input. */ - def close(cell: Cell): unit = { - val a = new Chan[unit] + def close(cell: Cell) { + val a = new Chan[Unit] cell(a, a) } @@ -117,25 +117,25 @@ object scheduler { /** * Creates a cell consisting of a chain of cells. */ - def makeRing(cells: List[Cell]): unit = + def makeRing(cells: List[Cell]): Unit = close(chain(cells)) /** * An agent is modelled as a function that takes as parameters channels to * signal that it has started or finished. */ - type Agent = (Chan[unit], Chan[unit]) => unit + type Agent = (Chan[Unit], Chan[Unit]) => Unit /** * Takes a list of agents and schedules them. */ - def scheduleAgents(agents: List[Agent]): unit = { + def scheduleAgents(agents: List[Agent]) { var firstAgent = true; val cells = agents map (ag => { - val a = new Chan[unit]; - val b = new Chan[unit]; + val a = new Chan[Unit]; + val b = new Chan[Unit]; spawn < ag(a, b) >; - (d: Chan[unit], c: Chan[unit]) => if (firstAgent) { + (d: Chan[Unit], c: Chan[Unit]) => if (firstAgent) { firstAgent = false; A(a, b)(d, c) } diff --git a/docs/examples/pilib/semaphore.scala b/docs/examples/pilib/semaphore.scala index 30e3c00975..ed224890e2 100644 --- a/docs/examples/pilib/semaphore.scala +++ b/docs/examples/pilib/semaphore.scala @@ -1,19 +1,19 @@ package examples.pilib +import scala.concurrent.pilib._ + /** Solution of exercise session 6 (first question). */ object semaphore { - import scala.concurrent.pilib._ - - class Signal extends Chan[unit] { + class Signal extends Chan[Unit] { def send = write(()) def receive = read } /** Interface. */ trait Semaphore { - def get: unit - def release: unit + def get: Unit + def release: Unit } /** First implementation. */ @@ -22,10 +22,10 @@ object semaphore { private val g = new Signal private val r = new Signal - def get: unit = g.send - def release: unit = r.send + def get: Unit = g.send + def release: Unit = r.send - private def Sched: unit = choice ( + private def Sched: Unit = choice ( g * (x => { r.receive; Sched }), r * (x => Sched) ) @@ -38,8 +38,8 @@ object semaphore { private val a = new Signal private val na = new Signal - def get: unit = { a.receive; spawn< na.send > } - def release: unit = choice ( + def get { a.receive; spawn< na.send > } + def release: Unit = choice ( a * (x => spawn< a.send >), na * (x => spawn< a.send >) ) @@ -47,24 +47,24 @@ object semaphore { } /** Test program. */ - def main(args: Array[String]): unit = { - val random = new java.util.Random() + def main(args: Array[String]) { + val random = new util.Random() val sem = new Sem2 - def mutex(p: => unit): unit = { sem.get; p; sem.release } + def mutex(p: => Unit) { sem.get; p; sem.release } spawn< { Thread.sleep(1 + random.nextInt(100)); mutex( { - System.out.println("a1"); + println("a1"); Thread.sleep(1 + random.nextInt(100)); - System.out.println("a2") + println("a2") } ) } | { Thread.sleep(1 + random.nextInt(100)); mutex( { - System.out.println("b1"); + println("b1"); Thread.sleep(1 + random.nextInt(100)); - System.out.println("b2") + println("b2") } ) } >; } diff --git a/docs/examples/pilib/twoPlaceBuffer.scala b/docs/examples/pilib/twoPlaceBuffer.scala index 020f3e4992..f0f278317a 100644 --- a/docs/examples/pilib/twoPlaceBuffer.scala +++ b/docs/examples/pilib/twoPlaceBuffer.scala @@ -8,18 +8,18 @@ object twoPlaceBuffer extends Application { /** * Specification. */ - def Spec[a](in: Chan[a], out: Chan[a]): Unit = { + def Spec[A](in: Chan[A], out: Chan[A]) { - def B0: unit = choice ( + def B0: Unit = choice ( in * (x => B1(x)) ) - def B1(x: a): unit = choice ( + def B1(x: A): Unit = choice ( out(x) * (B0), in * (y => B2(x, y)) ) - def B2(x: a, y: a): unit = choice ( + def B2(x: A, y: A): Unit = choice ( out(x) * (B1(y)) ) @@ -29,38 +29,38 @@ object twoPlaceBuffer extends Application { /** * Implementation using two one-place buffers. */ - def Impl[a](in: Chan[a], out: Chan[a]): unit = { + def Impl[A](in: Chan[A], out: Chan[A]) { ///- ... complete here ... // one-place buffer - def OnePlaceBuffer[a](in: Chan[a], out: Chan[a]): Unit = { - def B0: unit = choice ( in * (x => B1(x)) ) - def B1(x: a): unit = choice ( out(x) * (B0)) + def OnePlaceBuffer[A](in: Chan[A], out: Chan[A]) { + def B0: Unit = choice ( in * (x => B1(x)) ) + def B1(x: A): Unit = choice ( out(x) * (B0)) B0 } - val hidden = new Chan[a] + val hidden = new Chan[A] spawn < OnePlaceBuffer(in, hidden) | OnePlaceBuffer(hidden, out) > ///+ } - val random = new java.util.Random() + val random = new util.Random() - def Producer(n: Int, in: Chan[String]): Unit = { + def Producer(n: Int, in: Chan[String]) { Thread.sleep(random.nextInt(1000)) val msg = "" + n choice (in(msg) * {}) Producer(n + 1, in) } - def Consumer(out: Chan[String]): unit = { - Thread.sleep(random.nextInt(1000)); - choice (out * { msg => () }); + def Consumer(out: Chan[String]) { + Thread.sleep(random.nextInt(1000)) + choice (out * { msg => () }) Consumer(out) } val in = new Chan[String] - in.attach(s => System.out.println("put " + s)) + in.attach(s => println("put " + s)) val out = new Chan[String] - out.attach(s => System.out.println("get " + s)) + out.attach(s => println("get " + s)) //spawn < Producer(0, in) | Consumer(out) | Spec(in, out) > spawn < Producer(0, in) | Consumer(out) | Impl(in, out) > |