alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (tcpoly_monads.scala)

This example Scala source code file (tcpoly_monads.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

app, monads, monads, monadtc, monadtc, none, none, option, option, optioninstofmonad, optionmonad, some, test

The Scala tcpoly_monads.scala source code

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)
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala tcpoly_monads.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.