What is the diff between pure() and return() ?Could you give me an example?

I defined template ,when i want to get nothing , i would use pure() or return() .But i could not find the diff between pure() and return()

2 Likes

In the context that you are seeing these two statements inside a do block (i.e. when writing a Choice for example), they probably mean the same thing.

pure is a function that is defined in Applicative typeclass, whereas return is defined in the Action typeclass (aka Monad), which is a ‘subclass’ of the former, if you like. I believe that the implementation of return is just return = pure.

The only difference, as a developer, when looking at code outside the do notation, is that if you see return , you can assume that you can use monadic >>= in addition to <*> and liftA2 from Applicative. The converse is not true.

**edit: Please see @cocreature’s post below **

I also recommend you look at the recent tutorial If you are not 100% sure about `do` blocks, `<-` notation and `return`, you should read this, which explains the relationship between do notation and Action.

I think that return was added originally just as syntactic sugar, so you could have a do ... return statement. I recall that in Haskell there also exists an applicative do extension, but I don’t think we have this in DAML.

1 Like

In DAML they are literally equivalent. return is part of Action in Haskell but in DAML, it is simply a synonym for pure.

1 Like

This is … surprising. Do you know if there’s some reason behind this?

1 Like

Many people consider the separation between pure and return in Haskell to be a mistake. From what I recall, it’s because Monad wasn’t defined in terms of Applicative when it first showed up, so it needed its own function to lift a value.

In GHC 7.10, this heirarchy was fixed, making return redundant. However, there are plenty of cases where return and pure don’t do exactly the same thing, or where return was defined regardless, so it couldn’t just be removed.

There are plans to stop people overriding return (and >>) in the monad of no return proposal, but I believe it’s going to be a while.

DAML doesn’t have this historical baggage, so doesn’t need return in the Action typeclass. It can be defined outside. I don’t know the history, but I’m assuming it wasn’t made overridable because there’s no way to make it do something different from pure without breaching the laws.

4 Likes

return is an alias to pure.

Learning DAML, as a domain expert but not FP developer, return is arguably easier to grasp.

pure has a strong smell of Haskell, which like the durian fruit is an acquired taste (at first it smells/tastes pretty bad, later you just can’t get enough).

Today, I prefer pure.

1 Like

Thanks for your explaining.Very interesting :clap: