Someone proposed a solution on Stackoverflow, but it doesn't compile either https://stackoverflow.com/questions/1992532/monad-trait-in-scala
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thanks, that solves the first problem. However, the error message is misleading because I defined unit, but not with the type Scala requires.
But why is
def unit[A](a: A)(s: String) = List((a,s))
different to
def unit[A](a: A) = (s: String) => List((a,s))
Both should have type A => String => List[(A, String)]
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Why can the A in Monad[M[A]] be different from the A in unit[A]?
I want to ensure that unit has type
M a -> (a -> M b) -> M b
where M is the monad, eg Option or List.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
22 juni 2016 kl. 16:18 skrev boris....@gmail.com:Why can the A in Monad[M[A]] be different from the A in unit[A]?
I want to ensure that unit has type
M a -> (a -> M b) -> M b
where M is the monad, eg Option or List.
I have the following problems with my parser monad:
- I get a compiler error when I try to inherit it from my Monad trait.
- I want bind to be an infix operator.
Someone proposed a solution on Stackoverflow, but it doesn't compile either https://stackoverflow.com/questions/1992532/monad-trait-in-scala
trait Monad[M[A]] {
def unit[A](a: A): M[A]
def bind[A, B](m: M[A])(f: A => M[B]): M[B]
}
class Parser[A] extends Monad[String => List[(A,String)]] { // doesn't compile
type P[A] = String => List[(A,String)]
def unit[A](a: A)(s: String) = List((a,s))
def bind[A,B](ma: P[A])(f: A => P[B]): P[B] = {(s: String) =>
ma(s) flatMap {case (a,t) => f(a)(t)}
}
}
Your monad instance generally shouldn't be a polymorphic class, it should be a single instance. If Parser is a monad then there be a Parser class, plus an implicit object of type Monad[Parser]. If you think about it, it has to be this way because:- You shouldn't need to instantiate something to call unit, it's basically a global function (per monad)- A Parser instance needs to know its A, but the monad instance doesn't
--