summaryrefslogtreecommitdiff
path: root/docs/examples/tcpoly
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/tcpoly')
-rw-r--r--docs/examples/tcpoly/collection/HOSeq.scala46
-rw-r--r--docs/examples/tcpoly/monads/Monads.scala14
2 files changed, 30 insertions, 30 deletions
diff --git a/docs/examples/tcpoly/collection/HOSeq.scala b/docs/examples/tcpoly/collection/HOSeq.scala
index a9414e3b3c..a6757b95ba 100644
--- a/docs/examples/tcpoly/collection/HOSeq.scala
+++ b/docs/examples/tcpoly/collection/HOSeq.scala
@@ -6,40 +6,40 @@ trait HOSeq {
// values implementing this interface, in order to provide more performant ways of building that structure
trait Accumulator[+coll[x], elT] {
def += (el: elT): Unit
- def result: coll[elT]
+ def result: coll[elT]
}
-
-
+
+
// Iterable abstracts over the type of its structure as well as its elements (see PolyP's Bifunctor)
- // m[x] is intentionally unbounded: fold can then be defined nicely
- // variance: if we write m[+x] instead of +m[+x], x is an invariant position because its enclosing type
+ // m[x] is intentionally unbounded: fold can then be defined nicely
+ // variance: if we write m[+x] instead of +m[+x], x is an invariant position because its enclosing type
// is an invariant position -- should probably rule that out?
trait Iterable[+m[+x], +t] {
//def unit[a](orig: a): m[a]
def iterator: Iterator[t]
-
+
// construct an empty accumulator that will produce the same structure as this iterable, with elements of type t
def accumulator[t]: Accumulator[m, t]
-
+
def filter(p: t => Boolean): m[t] = {
val buf = accumulator[t]
val elems = elements
while (elems.hasNext) { val x = elems.next; if (p(x)) buf += x }
buf.result
}
-
+
def map[s](f: t => s): m[s] = {
val buf = accumulator[s]
val elems = elements
while (elems.hasNext) buf += f(elems.next)
buf.result
}
-
+
// flatMap is a more specialized map, it only works if the mapped function produces Iterable values,
// which are then added to the result one by one
// the compiler should be able to find the right accumulator (implicit buf) to build the result
// to get concat, resColl = SingletonIterable, f = unit for SingletonIterable
- def flatMap[resColl[x] <: Iterable[resColl, x], s](f: t => resColl[s])(implicit buf: Accumulator[resColl, s]): resColl[s] = {
+ def flatMap[resColl[x] <: Iterable[resColl, x], s](f: t => resColl[s])(implicit buf: Accumulator[resColl, s]): resColl[s] = {
// TODO: would a viewbound for resColl[x] be better?
// -- 2nd-order type params are not yet in scope in view bound
val elems = elements
@@ -48,9 +48,9 @@ trait HOSeq {
while (elemss.hasNext) buf += elemss.next
}
buf.result
- }
+ }
}
-
+
final class ListBuffer[A] {
private var start: List[A] = Nil
private var last: ::[A] = _
@@ -78,7 +78,7 @@ trait HOSeq {
exported = !start.isEmpty
start
}
-
+
/** Clears the buffer contents.
*/
def clear: unit = {
@@ -97,13 +97,13 @@ trait HOSeq {
}
}
}
-
+
implicit def listAccumulator[elT]: Accumulator[List, elT] = new Accumulator[List, elT] {
private[this] val buff = new ListBuffer[elT]
def += (el: elT): Unit = buff += el
def result: List[elT] = buff.toList
}
-
+
trait List[+t] extends Iterable[List, t] {
def head: t
def tail: List[t]
@@ -111,16 +111,16 @@ trait HOSeq {
def iterator: Iterator[t] = error("TODO")
// construct an empty accumulator that will produce the same structure as this iterable, with elements of type t
- def accumulator[t]: Accumulator[List, t] = error("TODO")
+ def accumulator[t]: Accumulator[List, t] = error("TODO")
}
-
+
// TODO: the var tl approach does not seem to work because subtyping isn't fully working yet
final case class ::[+b](hd: b, private val tl: List[b]) extends List[b] {
def head = hd
def tail = if(tl==null) this else tl // hack
override def isEmpty: boolean = false
}
-
+
case object Nil extends List[Nothing] {
def isEmpty = true
def head: Nothing =
@@ -149,18 +149,18 @@ trait HOSeq {
def filter(f: T=>Boolean): FilterResult
def subseq(from: int, to: int): Subseq
def flatMap[S <: Seq[K], K](f: T => S): S#Concat // legal?
- def concat(others: Seq[T]): Concat
+ def concat(others: Seq[T]): Concat
*/
-
+
/*trait Iterator[t] {
// @post hasAdvanced implies hasNext
// model def hasAdvanced: Boolean
-
+
def hasNext: Boolean // pure
-
+
// @pre hasAdvanced
def current: t // pure
-
+
// @pre hasNext
// @post hasAdvanced
def advance: Unit
diff --git a/docs/examples/tcpoly/monads/Monads.scala b/docs/examples/tcpoly/monads/Monads.scala
index 5a966ce960..b6e3d5b9a8 100644
--- a/docs/examples/tcpoly/monads/Monads.scala
+++ b/docs/examples/tcpoly/monads/Monads.scala
@@ -6,18 +6,18 @@ trait Monads {
* (>>=) :: m a -> (a -> m b) -> m b
* return :: a -> m a
*
- * MonadTC encodes the above Haskell type class,
+ * MonadTC encodes the above Haskell type class,
* an instance of MonadTC corresponds to a method dictionary.
* (see http://lampwww.epfl.ch/~odersky/talks/wg2.8-boston06.pdf)
*
* Note that the identity (`this') of the method dictionary does not really correspond
- * to the instance of m[x] (`self') that is `wrapped': e.g., unit does not use `self' (which
+ * to the instance of m[x] (`self') that is `wrapped': e.g., unit does not use `self' (which
* corresponds to the argument of the implicit conversion that encodes an instance of this type class)
*/
// Option =:= [x] => Option[x] <: [x] => Any
-// trait MonadTC[m <: [x] => Any, a] {
+// trait MonadTC[m <: [x] => Any, a] {
// MonadTC[m[x], a] x is a type parameter too -- should not write e.g., m[Int] here
- trait MonadTC[m[x], a] {
+ trait MonadTC[m[x], a] {
def unit[a](orig: a): m[a]
// >>='s first argument comes from the implicit definition constructing this "method dictionary"
@@ -32,7 +32,7 @@ trait Monads {
*/
trait OptionMonad extends Monads {
// this implicit method encodes the Monad type class instance for Option
- implicit def OptionInstOfMonad[a](self: Option[a]): MonadTC[Option, a]
+ implicit def OptionInstOfMonad[a](self: Option[a]): MonadTC[Option, a]
= new MonadTC[Option, a] {
def unit[a](orig: a) = Some(orig)
def >>=[b](fun: a => Option[b]): Option[b] = self match {
@@ -47,8 +47,8 @@ object main extends OptionMonad with Application {
}
-/*
-trait MonadTC[m[x], a] requires m[x] {
+/*
+trait MonadTC[m[x], a] requires m[x] {
def unit[a](orig: a): m[a]
// >>='s first argument comes from the implicit definition constructing this "method dictionary"