Skip to content

Commit

Permalink
added headers, svn keywords, updated pilib exam...
Browse files Browse the repository at this point in the history
added headers, svn keywords, updated pilib examples
  • Loading branch information
michelou committed Jun 16, 2009
1 parent a4bdfdc commit 53ed9b9
Show file tree
Hide file tree
Showing 65 changed files with 324 additions and 153 deletions.
27 changes: 13 additions & 14 deletions 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)) >;
Expand All @@ -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)) >;
Expand All @@ -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)) >;
Expand All @@ -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)) >

}
42 changes: 21 additions & 21 deletions docs/examples/pilib/scheduler.scala
Expand Up @@ -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) })
///+
Expand All @@ -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) })
///+
Expand All @@ -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 (
Expand All @@ -45,24 +45,24 @@ 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))
///+
}

//***************** 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))
Expand All @@ -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)
Expand All @@ -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)
}

Expand All @@ -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)
}
Expand Down
34 changes: 17 additions & 17 deletions 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. */
Expand All @@ -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)
)
Expand All @@ -38,33 +38,33 @@ 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 >)
)
spawn< a.send >
}

/** 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")
} )
} >;
}
Expand Down
32 changes: 16 additions & 16 deletions docs/examples/pilib/twoPlaceBuffer.scala
Expand Up @@ -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))
)

Expand All @@ -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) >

Expand Down
2 changes: 1 addition & 1 deletion src/library/scala/collection/generic/Addable.scala
Expand Up @@ -6,7 +6,7 @@
** |/ **
\* */

// $Id: Iterable.scala 15188 2008-05-24 15:01:02Z stepancheg $
// $Id$

package scala.collection.generic

Expand Down
2 changes: 1 addition & 1 deletion src/library/scala/collection/generic/AddingBuilder.scala
Expand Up @@ -6,7 +6,7 @@
** |/ **
\* */

// $Id: ListBuffer.scala 14378 2008-03-13 11:39:05Z dragos $
// $Id$

package scala.collection.generic

Expand Down
2 changes: 1 addition & 1 deletion src/library/scala/collection/generic/BitSetFactory.scala
Expand Up @@ -5,7 +5,7 @@
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id: Traversable.scala 15188 2008-05-24 15:01:02Z stepancheg $
// $Id$
package scala.collection.generic

trait BitSetFactory[Coll <: BitSet with BitSetTemplate[Coll]] {
Expand Down

0 comments on commit 53ed9b9

Please sign in to comment.