aboutsummaryrefslogtreecommitdiff
path: root/tests/run/tcpoly_monads.scala
diff options
context:
space:
mode:
authorDmitry Petrashko <dmitry.petrashko@gmail.com>2015-09-14 17:07:23 +0200
committerDmitry Petrashko <dmitry.petrashko@gmail.com>2015-09-14 17:07:23 +0200
commit12053fa4d55497fc4df06afd67ba3762019969c3 (patch)
tree0cbab1dee0784793d2ab09124a0103412e813072 /tests/run/tcpoly_monads.scala
parent91f992c8af3e61a76bd862ad43b9abef9a6c3403 (diff)
downloaddotty-12053fa4d55497fc4df06afd67ba3762019969c3.tar.gz
dotty-12053fa4d55497fc4df06afd67ba3762019969c3.tar.bz2
dotty-12053fa4d55497fc4df06afd67ba3762019969c3.zip
Enable more tests that pass
Diffstat (limited to 'tests/run/tcpoly_monads.scala')
-rw-r--r--tests/run/tcpoly_monads.scala45
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/run/tcpoly_monads.scala b/tests/run/tcpoly_monads.scala
new file mode 100644
index 000000000..978f88959
--- /dev/null
+++ b/tests/run/tcpoly_monads.scala
@@ -0,0 +1,45 @@
+
+import scala.language.{ higherKinds, implicitConversions }
+
+trait Monads {
+ /**
+ * class Monad m where
+ * (>>=) :: m a -> (a -> m b) -> m b
+ * return :: a -> m a
+ *
+ * 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
+ * corresponds to the argument of the implicit conversion that encodes an instance of this type class)
+ */
+ trait MonadTC[m[x], a] {
+ def unit[a](orig: a): m[a]
+
+ // >>='s first argument comes from the implicit definition constructing this "method dictionary"
+ def >>=[b](fun: a => m[b]): m[b]
+ }
+}
+
+/**
+ * instance Monad Maybe where
+ * (Just x) >>= k = k x
+ * Nothing >>= _ = Nothing
+ */
+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]
+ = new MonadTC[Option, a] {
+ def unit[a](orig: a) = Some(orig)
+ def >>=[b](fun: a => Option[b]): Option[b] = self match {
+ case Some(x) => fun(x)
+ case None => None
+ }
+ }
+}
+
+object Test extends OptionMonad with App {
+ Console.println((Some("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") >>= (x => Some(x.length))).get)
+}