0

I try to learn free monads. I found following:

data Free f r = Free (f (Free f r)) | Pure r

What does Pure mean? And why do I need r with Pure?

RuF
  • 548
  • 1
  • 11
  • 31
  • Did you read http://stackoverflow.com/questions/13352205/what-are-free-monads already? – Zeta May 28 '14 at 07:04
  • 1
    `Pure` is the name of a constructor. You need `r` in `Pure r` because you need to put something inside of `Pure`. You can't put `f` inside of `Pure` because there are no values of type `f`. So you have a second type variables, and that's what you put inside `Pure`. If the questions is why you need `Pure` at all, the answer is you need a way to end your recursive structure (why do you need `[]`?). The constructor which is the leaf node can be in the `Free` datatype, or it can be in the `f` which is inside `Free`, in which case you would have 2 distinct (redundant) leaf node constructors. – user2407038 May 28 '14 at 08:09

1 Answers1

2

Pure corresponds to the return operation -- conceptually it attaches to your functor the ability to "inject" a value. The Free constructor corresponds to "join".

If you want to understand why you "need" Pure, try to remove it and give the Monad instance and see where you get stuck!

sclv
  • 38,665
  • 7
  • 99
  • 204