These are chat archives for typelevel/cats

12th
Jul 2016
Nick Childers
@Voltir
Jul 12 2016 01:37
Z
anatoly yakovenko
@aeyakovenko
Jul 12 2016 05:56
so i almost have things working with combining xortT and stateT, the only weird thing is when i try to run it, the most inner monad that i have, trampoline, doesn’t run
any idea why its looking for S: cats.Comonad[Function0]?
anatoly yakovenko
@aeyakovenko
Jul 12 2016 06:29
duh, i found Id, so that works if i use Id instead of trampoline
Pascal Voitot
@mandubian
Jul 12 2016 08:26
@Voltir nice... like that, I don't see better way right now... freek/onion is smart but not too much and it can't guess that it needs to wrap left side of Xor into something bigger :D
José Antonio Jiménez
@logistark
Jul 12 2016 09:18

Hi, i am developing a compiler like this:

class Trouble[M[_]] {

  implicit def opsInterpreter(implicit R: Repository): MyOp ~> M = new (MyOp ~> M) {
    import R._

    override def apply[A](fa: MyOp[A]): M[A] = {
      fa match {
        case GetUser(id) =>
          Eval.later(getUser(id))
      }
    }
  }
}

but i keep getting the error:
Error:(15, 21) type mismatch; found : cats.Eval[Option[User]] required: M[A] Eval.later(getUser(id))

if i change M for Id or Eval
its compile
Olli Helenius
@liff
Jul 12 2016 09:43
@logistark if you promise to return an M[A], you'll have to return that and not an Eval :)
Olli Helenius
@liff
Jul 12 2016 09:50
@logistark would this do:
class Trouble[M[_]](implicit M: Monad[M]) {
  implicit def opsInterpreter(implicit R: Repository): MyOp ~> M = new (MyOp ~> M) {
    import R._

    override def apply[A](fa: MyOp[A]): M[A] = {
      fa match {
        case GetUser(id) =>
          M.pureEval(Eval.later(getUser(id)))
      }
    }
  }
}
José Antonio Jiménez
@logistark
Jul 12 2016 10:09
@liff you are right :smile:
Nick Childers
@Voltir
Jul 12 2016 12:48
@mandubian right, I think another possible way might be to target the onionT instead of the Free with either another extension method or possibly an implicit conversion, but I haven't tried that
Pascal Voitot
@mandubian
Jul 12 2016 13:52
@Voltir I've added some extensions in latest version to peelRight the onion... can be useful in some cases
anatoly yakovenko
@aeyakovenko
Jul 12 2016 17:12
i am trying to use trampoline, but i am seeing this error could not find implicit value for parameter S: cats.Comonad[Function0]
any ideas? is there not a comonad instance for function0?
anatoly yakovenko
@aeyakovenko
Jul 12 2016 17:28
so Funciton0 is a Bimonad, so it should also be a Command, but it doesn’t seem to find it
Frank S. Thomas
@fthomas
Jul 12 2016 17:32
@aeyakovenko you need to import cats.implicits._:
scala> implicitly[cats.Comonad[Function0]]
<console>:12: error: could not find implicit value for parameter e: cats.Comonad[Function0]
       implicitly[cats.Comonad[Function0]]
                 ^

scala> import cats.implicits._
import cats.implicits._

scala> implicitly[cats.Comonad[Function0]]
res1: cats.Comonad[Function0] = cats.instances.Function0Instances$$anon$1@5c5b0e6f
anatoly yakovenko
@aeyakovenko
Jul 12 2016 17:33
ah, thanks!
i am pretty new to scala, how do i know its in ‘cats.implicits’ ?
anatoly yakovenko
@aeyakovenko
Jul 12 2016 17:42
how do i pull that in explicitly? without the global ._? cats.implicits.{Function0Instances} doesn’t seem to bring that in
Frank S. Thomas
@fthomas
Jul 12 2016 17:42
The instance is defined in the Function0Instances trait, that is mixed into FunctionInstances, which is mixed into AllInstances, which is mixed into cats.implicits pacakge object.
import cats.instances.function._ should work
anatoly yakovenko
@aeyakovenko
Jul 12 2016 17:50
thanks! i am on 0.6.0, i think its in cats.std.function
i wish the compiler had a mode that dumped all all the wildcard imports as explicit, so i could find where these things are defined :)
Rob Norris
@tpolecat
Jul 12 2016 18:13
@aeyakovenko if all else fails you can use reify to see the expanded expression
scala> import cats._, cats.implicits._
import cats._
import cats.implicits._

scala> reflect.runtime.universe.reify { Comonad[Function0] }
res0: reflect.runtime.universe.Expr[cats.Comonad[Function0]] = Expr[cats.Comonad[Function0]](Comonad.apply[Function0](implicits.function0Instance))
And aha, it's implicits.function0Instance
anatoly yakovenko
@aeyakovenko
Jul 12 2016 18:19
@tpolecat thats awesome, thanks
anatoly yakovenko
@aeyakovenko
Jul 12 2016 19:18
is there an alternative operator <|>
idk if we have an operator for it
doesn't look like it
anatoly yakovenko
@aeyakovenko
Jul 12 2016 19:51
monads dont automatically get Alternative?
Adelbert Chang
@adelbertc
Jul 12 2016 20:17
Nope, they can't right
Alternative wants FA -> FA -> FA
Monads don't have enough information to do that without a function being passed to flatMap to tell it what to do
anatoly yakovenko
@aeyakovenko
Jul 12 2016 20:33
ah yea, you are right
anatoly yakovenko
@aeyakovenko
Jul 12 2016 20:44
and i can just use orElse directly
anatoly yakovenko
@aeyakovenko
Jul 12 2016 21:12

so the filter sugar in for, is that part of some typeclass? i have a type alias for my monad stack type ErrorTStateT[F[_], S, A] = XorT[({type l[X] = StateT[F, S, X]})#l, String, A] and i am trying to use an instance of it with a deconstructor

  def registered:SchedulerM[_] = for {
    D.Reregistered(_) <- nextEvent
  } yield(())

but its complaining that filter is not a member of SchedulerM[SchedulerEvent]

Adelbert Chang
@adelbertc
Jul 12 2016 21:15
@aeyakovenko doing a patmat like that in for-comprehension desugars to calling withFilter, which falls back to filter
it's not type class based, i believe the compiler literally just ends up attempting to call withFilter or filter on the data type
anatoly yakovenko
@aeyakovenko
Jul 12 2016 21:15
is there a way for me to add it? XorT is sealed, so i dont think i can extend it
Adelbert Chang
@adelbertc
Jul 12 2016 21:16
do we not have a filter on XorT ?
oh we don't
anatoly yakovenko
@aeyakovenko
Jul 12 2016 21:17
there is a XorTMonadFilter
Adelbert Chang
@adelbertc
Jul 12 2016 21:19
so i think the issue is
it expects filter to be something like
F[A] => (A => Boolean) => F[A]
in the case of XorT it isn't clear what to do if the predicate doesn't pass
i suppose you could use Monoid empty
which is what MonadFilter does
but the method itself doesn't exist
you can work around it by using an implicit class
implicit class XorTFilter[F[_], A, B](val xort: XorT[F, A, B]) extends AnyVal {
  def filter(f: B => Boolean)(implicit M: Monoid[A]): XorT[F, A, B] = ...
}
might also need an Applicative[F] constraint
anatoly yakovenko
@aeyakovenko
Jul 12 2016 21:24
i can try that
i guess that make sense, it would be my custom error, Left(“filter failure…”)
or yea, Monoid.empty would make sense too
anatoly yakovenko
@aeyakovenko
Jul 12 2016 21:52
stupid question, how would i implement the filter operation? don’t i need to pull out the ‘B’ value from something? were is that argument?
anatoly yakovenko
@aeyakovenko
Jul 12 2016 22:48
ah! i see, there is a case class constructor parameter xort, thats really weird, and that becomes a method on the type