Skip to content
Discussions/App Development/What is the diff between pure() and return() ?Could you give me an example?Forum ↗

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

App Development8 posts2,570 views10 likesLast activity Dec 2020
HU
huixin_liuOP
Aug 2020

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()

LU
Luciano
Aug 2020

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 **

LU
Luciano
Aug 2020

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.

CO
cocreature
Aug 2020

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

LU
Luciano
Aug 2020

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

SA
SamirTalwar
Aug 2020

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.

SO
sofusmortensen
Oct 2020

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.

HU
huixin_liu
Dec 2020
sofusmortensen:

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 .

Thanks for your explaining.Very interesting :clap:

← Back to Discussions