# Can the DAML REPL use pure functions or does it only submit commands to a ledger?

**URL:** <https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866>\
**Category:** App Development\
**Tags:** repl\
**Created:** [July 16, 2020, 4:51pm UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866 "2020-07-16T16:51:27Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![anthony](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/anthony/32/419_2.png) [@anthony](https://forum.canton.network/u/anthony)\
**Post date:** [July 16, 2020, 4:51pm UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866/1 "2020-07-16T16:51:28Z")

</div>

It seems like the DAML REPL needs to connect to a ledger. Is there any way to use it with just pure functions for when you want to test out some functionality but not necessarily record a transaction on a ledger?

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [July 16, 2020, 4:58pm UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866/2 "2020-07-16T16:58:08Z")

</div>

At the moment you always need a ledger connection but we might relax that in the future. However, if you do have a ledger connection you can easily test a pure function, e.g.,

```haskell
debug (1 + 1)

```

will run the computation and print `2` (prefixed by some trace output).

---

<div class="post-metadata">

**Author:** ![anthony](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/anthony/32/419_2.png) [@anthony](https://forum.canton.network/u/anthony)\
**Post date:** [July 16, 2020, 5:02pm UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866/3 "2020-07-16T17:02:35Z")

</div>

What would be the easiest way to spin up the DAML REPL against a test ledger then?

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [July 16, 2020, 5:04pm UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866/4 "2020-07-16T17:04:19Z")

</div>

Start sandbox via `daml sandbox` (you don’t need a DAR or anything else) and then connect via `daml repl --ledger-host localhost --ledger-port 6865`. You need the latest snapshot (`1.4.0-snapshot.20200715.4733.0.d6e58626` or later) for this. Otherwise you also need to pass in a DAR.

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [July 16, 2020, 8:06pm UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866/5 "2020-07-16T20:06:57Z")

</div>

Wouldn’t `return (1 + 1)` with an `--output-file` option be the more orthodox way of getting your hands on the value?

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [July 17, 2020, 6:27am UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866/6 "2020-07-17T06:27:15Z")

</div>

`daml repl` and `daml script` have mostly orthogonal usescases:

`daml script` requires you to first build a DAR so the initial effort is fairly high. But once you have done that it’s easy to run the same script multiple times (e.g. everytime you initialize your ledger) and you can do so without a human watching it. `--output-file` ties in well with that since it gives you machine-readable output.

`daml repl` on the other hand is targeted at an interactive usecase. So it’s great for one-off usecases for interacting with the ledger or to explore how a function behaves for different inputs. You often don’t know what exactly you want to do here upfront so having to create a DAML projects, build a DAR and run it with `daml script` is a pretty clunky replacement for this.

So if your goal is just to get the output of `1 + 1` and you never want to change that, `daml script --output-file` might do the trick but it is by no means a replacement for exploring pure functions in `daml repl`.

---

<div class="post-metadata">

**Author:** ![drsk](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/drsk/32/43_2.png) [@drsk](https://forum.canton.network/u/drsk)\
**Post date:** [July 17, 2020, 11:15am UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866/7 "2020-07-17T11:15:59Z")

</div>

It would actually be really nice if you could run pure functions in the REPL without wrapping them in `debug` for the learning experience in the katacoda’s I’m writing right now.

---

<div class="post-metadata">

**Author:** ![anthony](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/anthony/32/419_2.png) [@anthony](https://forum.canton.network/u/anthony)\
**Post date:** [July 17, 2020, 11:35am UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866/8 "2020-07-17T11:35:30Z")

</div>

💯

This would also bring the DAML REPL in line with how other REPLs behave.

---

<div class="post-metadata">

**Author:** ![Gary\_Verhaegen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gary_verhaegen/32/81_2.png) [@Gary\_Verhaegen](https://forum.canton.network/u/Gary_Verhaegen)\
**Post date:** [July 17, 2020, 12:04pm UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866/9 "2020-07-17T12:04:16Z")

</div>

I’ve actually just had a pretty long chat with @cocreature on that topic. My current thinking is I would not want the REPL to behave like the Haskell one, i.e. automatically print out pure expressions _and_ execute effects. Given that effects in this case are recorded on a permanent, remote ledger, I’d be uncomfortable with providing an environment that is not either explicitly safe or explicitly unsafe.

Which I believe leaves us with two options:

- The REPL keeps not running pure expressions as it does now, but maybe we improve the error message to say something like “The DAML REPL can only execute Script expressions. If you want to run a pure value, wrap your expression in pure/return”. If we go down that route we should also imo always print out the result, such that:

```haskell
daml> 1 + 1
<error msg>
If you want to run a pure expression, wrap it in return, e.g.:
return $ 1 + 1
daml> return $ 1 + 1
=> 2
daml> debug $ 1 + 1
2
=> ()
daml>

```

- The REPL assumes expressions are “pure” by default, and if the user wants to execute something, they have to explicitly wrap the expression in some other construct. Maybe `script`? The issue with this approach is it sort of requires introducing a special keyword just for the REPL, which only means something from the top-level of the REPL, which leads to a lot of special-casing and could end up murkying the code quite a bit. Also we don’t really have a great way to print out Script- or Action-typed values if we don’t execute them.

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [July 17, 2020, 12:06pm UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866/10 "2020-07-17T12:06:24Z")

</div>

Opened [Make it easier to run pure functions in DAML REPL · Issue #6780 · digital-asset/daml · GitHub](https://github.com/digital-asset/daml/issues/6780) to track this.

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [July 17, 2020, 12:15pm UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866/11 "2020-07-17T12:15:36Z")

</div>

As for @Gary_Verhaegen’s point, I’m interested what others think here. I’ve used the Haskell REPL so much that the behavior there is how I expect all REPL to behave but that might not apply if you aren’t as familiar with it.

Given that we are only talking about allowing more pure operations, I don’t think this is particularly risky (switching the other way around and executing ledger operations in more places would be risky).

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [July 17, 2020, 12:31pm UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866/12 "2020-07-17T12:31:35Z")

</div>

Having a special keyword that needs to go in front of any action to be executed is my least liked option. Other than that, I probably have a slight preference for making it behave like the Haskell REPL over having to wrap things in `pure`. When I see error messages like `If you want to run a pure expression, wrap it in return, e.g.: `, I just think: “If you know what I’m trying to do it, why don’t you just do it for me?!”

But I think either way we should print the result of every statement by default. If people want to swallow it, they can just use `void`.

---

<div class="post-metadata">

**Author:** ![anthony](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/anthony/32/419_2.png) [@anthony](https://forum.canton.network/u/anthony)\
**Post date:** [July 17, 2020, 12:37pm UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866/13 "2020-07-17T12:37:16Z")

</div>

Perhaps a flag that switches between these two behaviors?

`daml repl` - works like every other repl  
`daml repl --ledger-mode` - works how it currently is

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [July 20, 2020, 8:02pm UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866/14 "2020-07-20T20:02:13Z")

</div>

Here’s a preview of the new improved behavior that should land in this or next weeks snapshot.

 ![out](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/1X/10b0d4953139a41ca4c6e90c05cee65f67d77688.gif)

---

<div class="post-metadata">

**Author:** ![anthony](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/anthony/32/419_2.png) [@anthony](https://forum.canton.network/u/anthony)\
**Post date:** [July 21, 2020, 12:34am UTC](https://forum.canton.network/t/can-the-daml-repl-use-pure-functions-or-does-it-only-submit-commands-to-a-ledger/866/15 "2020-07-21T00:34:32Z")

</div>

![so beautiful](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/1X/c8ed5ef080cb7c96b48853e3d223c7744f249b41.gif)
