# Question on DAML function

**URL:** <https://forum.canton.network/t/question-on-daml-function/714>\
**Category:** App Development\
**Tags:** daml\
**Created:** [June 23, 2020, 8:07am UTC](https://forum.canton.network/t/question-on-daml-function/714 "2020-06-23T08:07:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![upanshu21](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/upanshu21/32/554_2.png) [@upanshu21](https://forum.canton.network/u/upanshu21)\
**Post date:** [June 23, 2020, 8:07am UTC](https://forum.canton.network/t/question-on-daml-function/714/1 "2020-06-23T08:07:12Z")

</div>

`f (x: B): A = phi` is top-level definition, and  
`c` is a constant of type  
`B` . Is it possible to define  
`f` and  
`c` in such a way that  
`(f c)`  
will yield two different results in two different contexts?

---

<div class="post-metadata">

**Author:** ![oggy](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/oggy/32/109_2.png) [@oggy](https://forum.canton.network/u/oggy)\
**Post date:** [June 23, 2020, 11:42am UTC](https://forum.canton.network/t/question-on-daml-function/714/2 "2020-06-23T11:42:32Z")

</div>

Hi @upanshu21 and welcome,

no, it’s not possible. DAML is a pure language, so its functions are “referentially transparent”, meaning that given the same argument, the function always returns the same result. The one kinda-sorta exception are `Update` return values. In this case, the function, when given the same argument also always gives the same `Update`, but the `Update` then may be executed by the Ledger, and this execution depends on the environment (mapping of contract IDs to contracts).

---

<div class="post-metadata">

**Author:** ![SamirTalwar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/samirtalwar/32/374_2.png) [@SamirTalwar](https://forum.canton.network/u/SamirTalwar)\
**Post date:** [June 23, 2020, 2:09pm UTC](https://forum.canton.network/t/question-on-daml-function/714/3 "2020-06-23T14:09:22Z")

</div>

I think it can depend on your definition of “context” (and also your definition of `B`, and probably your definition of “yield”).

If you want to do something stateful (and don’t mind some complicated type definitions), you can use `DA.Action.State`, a.k.a. the State monad. Without knowing your understanding of monads, I’m not sure if this makes sense to you, so I’ll try to explain.

Using `DA.Action.State` should allow for something like:

```haskell
test = scenario do
  let (_, x) = flip runState 0 $ do
        a <- get -- a == 0
        modify ( +1)
        b <- get -- b == 1
        modify (+ 1)
        c <- get -- c == 2
        modify (+ 1)

  x === 3

```

This is because `get` (in this context) doesn’t return an `Int` but a `State Int a`, which we then bind over to get the integer out. So this ends up being equivalent to something like this (though the actual State action is more complex, and more powerful):

```haskell
test = scenario do
  let (|>) = flip ($)
  let x = 0
        |> \a -> (+ 1) a
        |> \b -> (+ 1) b
        |> \c -> (+ 1) c
  x === 3

```

As you can see, it doesn’t really “return” in the conventional sense, but depending on what you meant by “yield”, you may be able to see that it yields a new value each time to the next operation. I don’t know if this is what you were implying by “different contexts”, but it might help, and you can potentially see how this can expand to other ~~monad~~ `Action`-based operations. For example, when performing an `Update`, you can have a different result based on previous `fetch` operations.

Long story short, DAML allows for a very restricted simulation of mutation and operation chaining which retains purity.

---

<div class="post-metadata">

**Author:** ![georg](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/georg/32/368_2.png) [@georg](https://forum.canton.network/u/georg)\
**Post date:** [June 23, 2020, 2:57pm UTC](https://forum.canton.network/t/question-on-daml-function/714/4 "2020-06-23T14:57:52Z")

</div>

> [@SamirTalwar](#):
>
> ```haskell
> test = scenario do
> let (|>) = flip ($)
> let x = 0
> |> \a -> (+ 1) a
> |> \b -> (+ 1) b
> |> \c -> (+ 1) c
> x === 3
> 
> ```

Oooh, I’m loving that F# pipe operator!

---

<div class="post-metadata">

**Author:** ![SamirTalwar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/samirtalwar/32/374_2.png) [@SamirTalwar](https://forum.canton.network/u/SamirTalwar)\
**Post date:** [June 23, 2020, 3:00pm UTC](https://forum.canton.network/t/question-on-daml-function/714/5 "2020-06-23T15:00:23Z")

</div>

I know the ampersand (`&`) is traditional among Haskellers, but I’ve written way too many shell scripts and I always parse `a & b` as “do `a` and `b` concurrently”.

---

<div class="post-metadata">

**Author:** ![chris.norris](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/chris.norris/32/688_2.png) [@chris.norris](https://forum.canton.network/u/chris.norris)\
**Post date:** [June 24, 2020, 9:22am UTC](https://forum.canton.network/t/question-on-daml-function/714/6 "2020-06-24T09:22:03Z")

</div>

[There was a PR](https://github.com/digital-asset/daml/pull/5830) to include the pipe operator in the stdlib, but it ran into some problems with eagerness and evaluation order. Essentially, the compiler wants to be able to simplify `x |> f` to `f x` but can’t because the evaluation order would change. See @Martin_Huschenbett 's comments for detail.
