# Is it possible to determine controllers of a choice programmatically?

**URL:** <https://forum.canton.network/t/is-it-possible-to-determine-controllers-of-a-choice-programmatically/3355>\
**Category:** App Development\
**Created:** [October 22, 2021, 2:47pm UTC](https://forum.canton.network/t/is-it-possible-to-determine-controllers-of-a-choice-programmatically/3355 "2021-10-22T14:47:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [October 22, 2021, 2:47pm UTC](https://forum.canton.network/t/is-it-possible-to-determine-controllers-of-a-choice-programmatically/3355/1 "2021-10-22T14:47:08Z")

</div>

I’m looking for a typeclass that’s the equivalent of `HasSignatory` for templates, but for choices i.e.

```haskell
class Choice t c o = > HasController c where
  controller : c -> [Party]

```

Does something like this exist?

---

<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:** [October 22, 2021, 3:29pm UTC](https://forum.canton.network/t/is-it-possible-to-determine-controllers-of-a-choice-programmatically/3355/2 "2021-10-22T15:29:05Z")

</div>

Unfortunately that’s not possible right now. I’ve opened an issue to track the request at [[FEATURE REQUEST] Expose controllers in Daml · Issue #11362 · digital-asset/daml · GitHub](https://github.com/digital-asset/daml/issues/11362).

Note that the type needs to be slightly different: The controllers can depend on both the template as well as the choice argument.

---

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [October 25, 2021, 8:18am UTC](https://forum.canton.network/t/is-it-possible-to-determine-controllers-of-a-choice-programmatically/3355/3 "2021-10-25T08:18:13Z")

</div>

> [@cocreature](#):
>
> The controllers can depend on both the template as well as the choice argument

Why is this? Shouldn’t there be a unique template for every choice? i.e. a type dependency `c -> t`?

---

<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:** [October 25, 2021, 8:59am UTC](https://forum.canton.network/t/is-it-possible-to-determine-controllers-of-a-choice-programmatically/3355/4 "2021-10-25T08:59:44Z")

</div>

Consider

```haskell
template T
  with
    p : Party
  where
    choice C : ()
      controller p
      do pure ()

```

Now try defining a function `C -> [Party]` that gives me back `p`. It won’t work, `C` is isomorphic to `()`. You get no information out of which you could construct a party.

---

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [October 25, 2021, 9:10am UTC](https://forum.canton.network/t/is-it-possible-to-determine-controllers-of-a-choice-programmatically/3355/5 "2021-10-25T09:10:04Z")

</div>

I suppose what I’m asking is why `Choice` isn’t defined as `class Choice t c r | c -> t`, so then you could write something like `getControllers : Choice t c r => c -> [Party] = this.p` or something along those lines (assuming `this` is provided by `Choice` - which is wrong - but I think you get the idea).

---

<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:** [October 25, 2021, 9:17am UTC](https://forum.canton.network/t/is-it-possible-to-determine-controllers-of-a-choice-programmatically/3355/6 "2021-10-25T09:17:35Z")

</div>

There are two questions here:

1. Why is there no functional dependency from the choice type to the template type?
2. Would a functional dependency mean that your suggested type works out?

For 1, the answer is slightly subtle: In Daml-LF different choices can share the argument type (across templates and for the same template). In Daml, this is easily missed since usually the compiler generates a new choice type for you. However there already is an important exception: The `Archive` type is the same for all templates which would break your functional dependency. With Daml Interfaces you can also define other choices that share the argument type.

For 2, it’s useful to remember what a functional dependency defines: It’s a dependency on the type meaning for `c -> t` given the choice _type_ you can define the template _type_. But that’s not what you need in your example. To define the controllers you don’t just need the template _type_ you need the template _payload_, i.e., the template _value_. A functional dependency absolutely does not give you that. Let’s ignore `Archive` and interfaces for a second and assume that we have the functional dependency. Going back to my earlier examples let’s assume I have two contracts `T alice` and `T bob`. Given `C` I would be able to determinate that the corresponding template is `T` but I have no idea which of the two contracts is relevant.

---

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [October 25, 2021, 9:27am UTC](https://forum.canton.network/t/is-it-possible-to-determine-controllers-of-a-choice-programmatically/3355/7 "2021-10-25T09:27:33Z")

</div>

> [@cocreature](#):
>
> there already is an important exception

Ah right, I had assumed that because the compiler typically gives you an error if you have two templates with an identical `choice` signature that this wasn’t possible. I hadn’t considered those exceptions.

> [@cocreature](#):
>
> you don’t just need the template _type_ you need the template _payload_ , i.e., the template _value_

Right, that is true. I was thinking I could lift single controllers to the type-level with some `newtype` + `IsParties` shenanigans. But let’s not go there 😂
