# The relationship between applicative/monadic action composition and the implementation of contract law

**URL:** <https://forum.canton.network/t/the-relationship-between-applicative-monadic-action-composition-and-the-implementation-of-contract-law/3479>\
**Category:** App Development\
**Created:** [November 19, 2021, 12:11pm UTC](https://forum.canton.network/t/the-relationship-between-applicative-monadic-action-composition-and-the-implementation-of-contract-law/3479 "2021-11-19T12:11:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gyorgybalazsi](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gyorgybalazsi/32/684_2.png) [@gyorgybalazsi](https://forum.canton.network/u/gyorgybalazsi)\
**Post date:** [November 19, 2021, 12:11pm UTC](https://forum.canton.network/t/the-relationship-between-applicative-monadic-action-composition-and-the-implementation-of-contract-law/3479/1 "2021-11-19T12:11:54Z")

</div>

I want to understand the relationship between monadic action composition and the propose/accept pattern, which is the cornerstone of the implementation of contract law.

The following slides reflect my current understanding. Please correct me if I’m missing something.

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/a/ab90f176d72ae5c057e2abbb3fe08d404c6d19d3.png)

Update: replaced the second slide by two new slides based on the comments made by @cocreature and @Gary_Verhaegen.

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/0/002948d8a4911b4aa9798aaee7ea74aabb5ccb16.png)

 ![image](https://canada1.discourse-cdn.com/flex028/uploads/daml/original/2X/d/dd3b11ebc335b90b42ec4cdf9a84309c8a70dc1e.png)

---

<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:** [November 20, 2021, 7:35am UTC](https://forum.canton.network/t/the-relationship-between-applicative-monadic-action-composition-and-the-implementation-of-contract-law/3479/2 "2021-11-20T07:35:20Z")

</div>

I’m not quite sure I understand your question. It looks like you are only looking at the `Accept` choice. You can implement that using only applicative operators:

To make the archival explicit, I made accept a non-consuming choice

```haskell
template T
  with
    proposer : Party
    accepter : Party
  where
    signatory proposer, accepter

template TProposal
  with
    t : T
  where
    signatory t.proposer
    observer t.accepter
    nonconsuming choice Accept : ContractId T
      controller t.accepter
      do archive self *> create t

```

---

<div class="post-metadata">

**Author:** ![gyorgybalazsi](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gyorgybalazsi/32/684_2.png) [@gyorgybalazsi](https://forum.canton.network/u/gyorgybalazsi)\
**Post date:** [November 23, 2021, 7:52am UTC](https://forum.canton.network/t/the-relationship-between-applicative-monadic-action-composition-and-the-implementation-of-contract-law/3479/3 "2021-11-23T07:52:46Z")

</div>

Yes, that’s right. `exerciseByKey` would be a better example for the usefulness of monadic composition I guess. What do you thin?

---

<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:** [November 23, 2021, 9:30am UTC](https://forum.canton.network/t/the-relationship-between-applicative-monadic-action-composition-and-the-implementation-of-contract-law/3479/4 "2021-11-23T09:30:38Z")

</div>

Can you expand on your thinking here? It isn’t clear to me how an individual function exemplifies monadic composition.

---

<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:** [November 23, 2021, 11:38am UTC](https://forum.canton.network/t/the-relationship-between-applicative-monadic-action-composition-and-the-implementation-of-contract-law/3479/5 "2021-11-23T11:38:29Z")

</div>

Hi @gyorgybalazsi,

One way to think about actions in Daml is as little programs that run “within” the ledger. From that perspective, the entire “program” is run atomically, and can _modify_ the ledger (ultimately by adding create and archive lines at the end).

From that (simplistic) perspective:

- An applicative action is one in which the client knows, in advance, exactly the lines that need to be written to the ledger. There’s still a chance the action can fail, if for example a contract you want to archive has already been archived. But you could think of applicative actions as being purely a list of lines to add “as is” at the end of the ledger (assuming they’re compatible with the state of the ledger at the time the transaction gets executed).
- A monadic action is one in which, instead of sending “pure data” to the ledger, you send a “function” (not actually a Daml function; actually represented as data, but bear with me) that will be run “inside” the ledger in order to generate the lines to add. That means that a monadic action can do something like fetch a contract, look at its payload, and then, based on that, decide whether or not to archive it.

Hopefully that helps clarify things a bit.

Of course, as you know, it’s not quite correct to say that an action “is” monadic or applicative. In reality, an action is an action. If you have two actions, you can _compose_ them in either a monadic or an applicative way, but in both cases the result is an action. So you could compose applicatively two actions that happen to both “be monadic inside”; in that case, the applicative composition expresses that there is no need for a transfer of information from the first to the second _while the transaction is executing “inside” the ledger_.

In the propose/accept pattern, as @cocreature illustrated, the `Accept` choice needs to do two things: archive one contract, and create a new one, and both things are “fully known” client-side and don’t depend on each other, so you can use applicative composition. (Note that this does not mean they are necessarily commutative: in this case they are, but if we were to archive-then-recreate a contract for which the template defined a key, they would not be.)

When would we need a monadic composition? Let us imagine the owner of the `T` contract wanted to verify the citizenship of the new owner before executing the transfer. The `Accept` choice would then take as an argument a ContractId for a contract signed by the government. It could look something like:

```haskell
module Main where

import Daml.Script

template T
  with
    proposer : Party
    accepter : Party
  where
    signatory proposer, accepter

template Citizenship
  with
    gov: Party
    citizen: Party
  where
    signatory gov
    observer citizen

template TProposal
  with
    t : T
    acceptedGovs: [Party]
  where
    signatory t.proposer
    observer t.accepter
    nonconsuming choice Accept : ContractId T
      with cit: ContractId Citizenship
      controller t.accepter
      do cit <- fetch cit
         if cit.gov `elem` acceptedGovs then
           archive self *> create t
         else
           fail "Wrong citizenship"

setup : Script ()
setup = script do
  alice <- allocatePartyWithHint "Alice" (PartyIdHint "Alice")
  bob <- allocatePartyWithHint "Bob" (PartyIdHint "Bob")
  wonderland <- allocatePartyWithHint "Wonderland" (PartyIdHint "Wonderland")

  prop <- submit alice do
    createCmd TProposal with t = (T alice bob), acceptedGovs = [wonderland]

  cit <- submit wonderland do
    createCmd Citizenship with gov = wonderland, citizen = bob

  submit bob do
    exerciseCmd prop Accept with cit

  return ()

```

In `Accept`, we now need to _fetch a contract from the ledger_, which in a transactional system we want to do at transaction time, i.e. we want this to happen “within the ledger”, not just done by the client ahead of time. (In practice the client does it ahead of time and then the ledger verifies it, but let’s gloss over that here.)

The crucial difference is that in @cocreature’s version, we don’t need to use any information resulting from the `archive` call in the `create` call, whereas here we do want to _make a decision_ (`if/else`, but that could just as well be “include the value of this attribute”) based on the _result_ of executing the `fetch` action. (There’s also of course the entire `setup` script, which has to be monadic because we want to use `prop` in `bob`'s `Accept` call etc.)

Hopefully this has clarified things, not made them more confusing.

---

<div class="post-metadata">

**Author:** ![gyorgybalazsi](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gyorgybalazsi/32/684_2.png) [@gyorgybalazsi](https://forum.canton.network/u/gyorgybalazsi)\
**Post date:** [November 24, 2021, 1:07pm UTC](https://forum.canton.network/t/the-relationship-between-applicative-monadic-action-composition-and-the-implementation-of-contract-law/3479/6 "2021-11-24T13:07:58Z")

</div>

Thanks, updated the slides based on your answers.
