I'm trying to demonstrate a Trampoline[+A] as a Functor (i.e., provides map[B](f: A = >B)).
I understand the classic implementation of the Trampoline as a Monad as described in Stackless Scala.
However, is there a way to implement the map function (and not flatMap) with only Done and More subclasses of Trampoline, or do i need to add the Flatmap subclass also?
Asked
Active
Viewed 415 times
2

Shimi Bandiel
- 5,773
- 3
- 40
- 49
1 Answers
1
You can't cause Trampoline represents a computational process. Functor gives you a map
function, which can only transform you final value, but to spawn a new computation is a job of a Monads, which in some way abstracts over the computation process, with a flatMap
operation (although i definetly prefer Haskell bind
name, cause it's better denotes the actual semantics of the operation). So with functors you can't chain different computation pieces together, e.g in scalaz Trampoline is an alias for a Free Monad which defines map like this:
def map[B](f: A => B): Free[S, B] =
flatMap(a => Return(f(a)))
As you can see it only transforms a value, but doesn't attach a new step, what you want to achieve with Trampolines.

4lex1v
- 21,367
- 6
- 52
- 86
-
So, if i want to show only tail-calls (w/o map, flatmap) i can use Done and More, but adding "map", i must introduce the Flatmap subclass. I thought it was only needed in order to support flatMap – Shimi Bandiel Jun 24 '14 at 11:07
-
I had the feeling that in order to support "map", it could be done somehow with only Done and More. – Shimi Bandiel Jun 24 '14 at 11:11
-
@ShimiBandiel you can. But the idea is that you are stepping through the computation and this step is done with a `flatMap` and that's why `map` operation is defined with `flatMap`, not as a simple transformation, but a transformation done in a new step. – 4lex1v Jun 24 '14 at 11:17
-
I tried to implement it, but i get always StackOverflowException without adding the Flatmap subclass. The reason i'm trying to do it is for presentation. Start with simple Tailcalls, use Done and More, show "map" and then, move to flatMap and the Flatmap subclass. But i can't seem to implement the "map" without the Flatmap subclass. And for most monads, i can show the incremental step in implementation between the "Functor" and the "Monad". – Shimi Bandiel Jun 24 '14 at 11:25
-
1@ShimiBandiel You can implement map simply tramp match { case Done(v) => f(v) case More(thunk) => f(thunk()) }, but this f(thunk()) in bigger cases is just f(() => f(() => f(() => f(v))))), this is the reason why u r getting SOE. `FlatMap` chunks this chain into smaller steps, if we have a value (Done case) then we continue the computation with a next step, if we have more steps (More case) we are computing it and then using `FlatMap`. In your presentation you can start with a simple `Map`, show that it causes SOE, then show that it's a Monad and FlatMap is a solution – 4lex1v Jun 24 '14 at 11:55
-
@Alexlv So, first of all, i'll use the Sub name instead of Flatmap as it will be more meaningful in the Functor example and then i'll move into flatMap with Sub(x, Sub(...)) that shows the monadic properties. Thanks! – Shimi Bandiel Jun 24 '14 at 12:06
-
@ShimiBandiel I think it would be much much much better if you draw this "bouncing" process, instead of writing Sub/FlatMap, it would be much easier to grasp, that building this chains in the head. – 4lex1v Jun 24 '14 at 12:11