diff options
author | Adriaan Moors <adriaan.moors@epfl.ch> | 2007-04-06 09:39:53 +0000 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@epfl.ch> | 2007-04-06 09:39:53 +0000 |
commit | f96b6beefc08f56218ac68b37a4cecd757cb60ee (patch) | |
tree | 2001a44efae7ddd1699520d3855598ef13e30803 /test/files/run/tcpoly_monads.scala | |
parent | 10a651a13c323366223160bfa1b4fe8a6e818d1d (diff) | |
download | scala-f96b6beefc08f56218ac68b37a4cecd757cb60ee.tar.gz scala-f96b6beefc08f56218ac68b37a4cecd757cb60ee.tar.bz2 scala-f96b6beefc08f56218ac68b37a4cecd757cb60ee.zip |
adding test files from tcpoly branch without hi...
adding test files from tcpoly branch without history -- much faster this way, sorry
Diffstat (limited to 'test/files/run/tcpoly_monads.scala')
-rw-r--r-- | test/files/run/tcpoly_monads.scala | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/test/files/run/tcpoly_monads.scala b/test/files/run/tcpoly_monads.scala new file mode 100644 index 0000000000..0ee160803a --- /dev/null +++ b/test/files/run/tcpoly_monads.scala @@ -0,0 +1,42 @@ +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 Application { + Console.println((Some("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") >>= (x => Some(x.length))).get) +}
\ No newline at end of file |