mandubian / freek

Free wrapper specialized to Coproducts of DSL/Containers
Other
23 stars 4 forks source link

Add OnionN[N <: Nat] #5

Open fanf opened 8 years ago

fanf commented 8 years ago

The general use case is to be able to specify what depth of the onion stack you want to use as the correct return type of a step.

This is needed to be able to use an Onion stack with more than one layers, but for steps where the last layers are actually part of the business type (typically, a List, Option, Either or some other data containers) and should not be part of the monad transformer stack.

An example is available here: https://gist.github.com/fanf/845273a15377347a04375d51a0139b78 See also thread: https://twitter.com/mandubian/status/765809870734028800

mandubian commented 8 years ago

@fanf Can you precise a bit?

type O = Foo :&: Bar :&: Toto :&: Nil
Foo[Bar[List[Option[A]]].OnionN[_2, O]
// should OnionN seek in depth in type O or in type Foo[Bar[List[Option[A]]] ?

Last weekend, I've tried to simplify onionT vs onionP but I think it's impossible to get rid of that. It's the same as liftT & lift.

fanf commented 8 years ago

Ah. This is exactly my wondering, to. I believe that as an user (ie I don't know it it's possible), what I want is:

Not sure it helps.

fanf commented 8 years ago

Well, what I know is that in classical for { } yield { }, I'm accustomed to normalise to Xor[E, T]. So I want to make the really common use case (for me):

for {
  a     <- myService1() // Xor[E, A]
  list_b <- myService2(a)  //Xor[E, List[B]]
  opt_c  <- Xor.right(pure()) // with pure: Option[C]
  res   <- calculus(list_b, opt_c)  // calculus(l: List[B], opt: Option[C]): Xor[E, D]
} yield {
  res
}

To be portable in freek with the onion type explaining to future use what the program is about, i.e:

type O = Xor[E, ?] :&: Bulb

And whatever is needed to help scalac use the correct implicits - of course the leaner the better.

Hope it's clearer

mandubian commented 8 years ago

now you would have to write something like:

type O = Xor[E, ?] :&: Bulb
for {
  a     <- myService1().onionT[O]
  list_b <- myService2(a).onionP[O]
  opt_c  <- (()).onionT[O]
  res   <- calculus(list_b, opt_c)  // calculus(l: List[B], opt: Option[C]): Xor[E, D]
} yield {
  res
}

going further with implicits poses problems as soon as you encounter something like

for {
  a     <- myService1()
  list_b <- myService2(a)
  opt_c  <- ().onionT[O]
  res   <- optc match {
    case Some(c) => ...
    case None => ...
   }
   // scalac is enable to unify types in the for-comprehension and you have to help it
} yield {
  res
}
fanf commented 8 years ago

OK, what about if calculus returns a Xor[E, List[D]]? (because of course, you're right, the problem in my use case is in the last returned type...)

mandubian commented 8 years ago

yep that' s the case I was speaking about... the important isn't only the O but what you want to keep in your Xor[E, List[D]]...

Xor[E, List[D]].onionN[O, 1] would take Xor[E, List[D]], dig one layer in depth, keep List[D] and lift it into the onion O.

but I think this onionN can't be generalized...

fanf commented 8 years ago

@mandubian

yep that' s the case I was speaking about... the important isn't only the O but what you want to keep in your Xor[E, List[D]]...

Xor[E, List[D]].onionN[O, 1] would take Xor[E, List[D]], dig one layer in depth, keep List[D] and lift it into the onion O.

That would be perfect!

but I think this onionN can't be generalized...

Oh :/

fanf commented 8 years ago

Is it simpler if onionN[O, 1] consume the full stack and then drop one layer ? (no because it's the most external layer which is easely dropped ?)

mandubian commented 8 years ago

which full stack, the one you give in input or the onion?

fanf commented 8 years ago

the input

mandubian commented 8 years ago

actually if you look at package.scala, for now, I had to create an implicit conversion for every stack F[A], F[G[A]], F[G[H[A]]]... so I feel OnionN isn't so easy for now ;)

fanf commented 8 years ago

oOOHHHhhoo. What a lib dev has to do to let the user think things are nice :))

pvillega commented 8 years ago

What @fanf said, but I understand it may not be feasible :)