# interfaceで振る舞いを分ける

> Damlのinterfaceを、viewの型とmethodとchoiceで宣言してtemplateに実装させる所と、値を変える関数と、標準のinterfaceでほかのアプリと組む所を、2つの図で追います。

- URL: https://cardene.dev/diagrams/canton-daml-interfaces
- 公開日: 2026-11-22
- 更新日: 2026-11-22
- トピック: Blockchain
- タグ: Canton
- 親の図解: [Damlのinterface](https://cardene.dev/diagrams/canton-daml-interface-model.md): Damlのinterfaceを宣言してtemplateに実装させ、標準のinterfaceでほかのアプリと組む所と、値とコントラクトIDを変える所と、partyを表すPartyの型を文字列と行き来させる所を、章の中の図解で追います。

Damlのinterfaceは、viewの型と、methodのシグネチャと、choiceで振る舞いを定めた、抽象の型です。
templateがinterfaceに従うには、`view`という特別なmethodを含めて、interfaceの全てのmethodを実装する`interface instance`の定義が要ります。
この頁では、interfaceをどう宣言してtemplateに実装させ、templateとinterfaceの間で値をどう変え、標準のinterfaceでほかのアプリとどう組むかを追います。

methodは、interfaceが定め、インスタンスにするtemplateごとに中身を書く関数です。
関数のシグネチャは、関数が取る引数の型と、返す値の型を書いた宣言です。
interfaceの宣言、viewの型、interfaceのchoice、`interface instance`の節、`requires`で要求するinterface、`toInterface`と`fromInterface`の仲間の関数の全ての構文は、Daml Language ReferenceのReference: Interfacesの節にあります。

`interface instance`の節をtemplateの部品として置く所は、章「Damlのtemplateとchoice」の頁「templateでコントラクトを作る」で見ました。

https://cardene.dev/diagrams/canton-daml-template-choice

## interfaceを宣言すると同じ名前の抽象の型が定まる

```haskell
interface MyInterface where
```

interfaceの宣言は、templateの宣言にいくらか似ています。
interfaceの名前は、`interface`のキーワードの後に置き、後に`where`のキーワードを続けます。
名前は、ほかの型の名前と同じく、大文字で始めます。

interfaceを定めると、同じ名前の抽象の型も定まります。
interfaceの値はこの型の値で、データのコンストラクタで作れず、templateの値に`toInterface`の関数を適用して作ります。
データのコンストラクタは、型の値を作る名前で、型が取る形ごとに1つあります。

また、interfaceの値は`case`で直接調べられないので、`fromInterface`のような関数を使います。
`case`のキーワードを使うと、値を決まったパターンに照らせます。

interfaceの値は、作る元にしたtemplateの値の型と欄を、中に持ちます。
templateの欄は、templateの`with`のブロックに書く欄です。

ただし、templateと同じく、interfaceの型`I`の値を持つ変数`b`があっても、`b`を作るのに使ったtemplateの型と欄のコントラクトが、台帳にあるとは限りません。
そう考えてよいのは、`b`が同じトランザクションの中で台帳からfetchした結果の時だけです。
`fetch`は、コントラクトIDから台帳のコントラクトを読む操作で、読んだコントラクトを消費しません。
コントラクトIDは、コントラクトを一意に指す識別子です。

## methodはinterfaceの値にだけ適用できる

```haskell
method1 : Party
method2 : Int
method3 : Bool -> Int -> Int -> Int
```

interfaceは、methodをいくつでも定められます。
methodの定義は、methodの名前と型を、1つの`:`で区切った物です。
methodの名前は、小文字か`_`で始まる正しい識別子にします。

methodの定義は、同じ名前のトップレベルの関数を導きます。
トップレベルの定義は、モジュールの一番外に書いた定義で、モジュールは、Damlのコードを名前を付けてまとめる単位です。
interfaceの名前が`I`、methodの名前が`m`、methodの型が`M`なら、導く関数は`m : I -> M`で、`M`は関数の型のこともあります。

この関数の1つ目の引数の型は`I`なので、関数はinterfaceの型`I`の値にしか適用できません。
templateに`I`のインスタンスがあっても、methodをtemplateの値には適用できません。
templateの値にmethodを使うには、先に`toInterface`の関数で変換します。

関数をinterfaceの値に適用すると、型`M`の値が返ります。
返る値は、interfaceの値を作る元にしたtemplateの型`t`の、`I`のインスタンスでの`m`の実装に当たります。
また、特別なmethodの`view`を、viewの型のために必ず定めます。

## viewはrecordのviewの型の値を返す

```haskell
data MyInterfaceViewType =
  MyInterfaceViewType { name : Text, value : Int }
```

```haskell
viewtype MyInterfaceViewType
```

interfaceの全てのインスタンスは、`viewtype`で宣言した型の値を返す、特別な`view`のmethodを実装します。
`viewtype`で宣言するviewの型は、recordにします。
recordは、名前の付いた欄を持つ型です。

interfaceを対象にした購読が返すのも、この型です。
購読は、台帳から届く更新の流れを受け取り続けることです。

## interfaceのchoiceはinterfaceの値でexerciseする

```haskell
choice MyChoice : (ContractId MyInterface, Int)
  with
    argument1 : Bool
    argument2 : Int
  controller method1 this
  do
    let n0 = method2 this
    let n1 = method3 this argument1 argument2 n0
    pure (self, n1)

nonconsuming choice MyNonConsumingChoice : Int
  controller method1 this
  do
    pure $ method2 this
```

interfaceのchoiceは、templateのchoiceとほぼ同じに働きます。
interfaceのインスタンスがあるtemplateの型のコントラクトは、どれも、controllerのpartyにそのchoiceを与えます。
ただし、interfaceのchoiceは、対応するinterfaceの型の値でしかexerciseできません。
templateの値でexerciseするには、先に`toInterface`で変換します。

actionは、実行すると値が得られる処理です。
methodでは、choiceのcontrollerと、choiceをexerciseした時に動くactionを定められます。
例の`method1`はcontrollerを、`method2`と`method3`はexerciseした時に動くactionを定めるのに使います。

templateのchoiceと同じく、`choice`のキーワードの前に`nonconsuming`を付けると、exerciseしてもコントラクトを消費しないchoiceにできます。
付けなければ、choiceは`consuming`になります。
ただし、interfaceのchoiceは、`preconsuming`と`postconsuming`の注釈に対応しません。
choiceの宣言を`choice`でなく`controller`で始める書き方にも、対応しません。

消費の種類ごとにコントラクトをarchiveする時が変わる所は、章「Damlのtemplateとchoice」の頁「choiceでコントラクトを変える」が追っています。

https://cardene.dev/diagrams/canton-daml-contract-choice

## methodもchoiceも持たないinterfaceも定められる

```haskell
data EmptyInterfaceView = EmptyInterfaceView {}

interface YourInterface where
  viewtype EmptyInterfaceView
```

method、前提条件、choiceを持たないinterfaceも定められますが、役に立つとは限りません。
それでも、viewの型は必ず定め、unitにしてもかまいません。
unitの`()`は、値が`()`の1つだけの型です。

## interface instanceの節でtemplateに実装させる

例のtemplateの`NameOfTemplate`は、`field1`と`field2`の2つの欄を持ち、signatoryは`field1`です。

```haskell
template NameOfTemplate
  with
    field1 : Party
    field2 : Int
  where
    signatory field1
```

```haskell
interface instance MyInterface for NameOfTemplate where
  view = MyInterfaceViewType "NameOfTemplate" 100
  method1 = field1
  method2 = field2
  method3 False _ _ = 0
  method3 True x y
    | x > 0 = x + y
    | otherwise = y
```

templateを既にあるinterfaceのインスタンスにするには、templateの宣言の中に、`interface instance`の節を定めます。
節のtemplateは、節を置いたtemplateの宣言と同じにします。
つまり、template `T`の宣言が持てるのは、templateが`T`の`interface instance`の節だけです。

節は`interface instance`のキーワードで始め、interfaceの名前、`for`のキーワード、templateの名前、`where`のキーワードの順に続けます。
`where`が始めるブロックでは、interfaceの全てのmethodを実装します。

節の中では、methodを適用するコントラクトを指す`this`が暗に定まり、`this`の型はtemplateのデータのrecordの型です。
このコントラクトのtemplateの欄も、節の中で使えます。
methodの実装は、パターンマッチとガードを含めて、トップレベルの関数と同じ構文で書け、例の`method3`がそう書いた実装です。

パターンマッチは、値の形に合わせて中の値を取り出す書き方です。
ガードは、`|`の後に条件を並べ、上から順に試して最初に真になった条件の式を選ぶ書き方です。

それぞれのmethodの実装は、interfaceの宣言でそのmethodに定めた型を返します。
特別な`view`の実装は、interfaceの宣言で`viewtype`に定めた型を返します。
interfaceがmethodを持たない時、`interface instance`の節は`view`のmethodだけを実装すれば済みます。

```haskell
interface instance YourInterface for NameOfTemplate where
  view = EmptyInterfaceView
```

## interfaceとtemplateの部品のつながり

図Aでは、interfaceの宣言が定めるviewの型とmethodとinterfaceのchoiceと、interfaceの値と、templateの宣言の中のinterface instanceの節と、templateの値が、どうつながるかを段ごとに追います。
templateの値からinterfaceの値を作る変換の関数は、`toInterface`です。

```dragon
architecture interfaceとtemplateの部品のつながり

group IF as interface [note: 宣言と値, color: slate, icon: layers]
  box CH as interfaceのchoice [note: controllerに与える, icon: code]
  box IV as interfaceの値 [note: 抽象の型の値, icon: box]
  box ME as method [note: 名前と型, icon: code]
  box VT as viewの型 [note: recordの型, icon: card]
end
group TP as template [note: 節と値, color: blue, icon: doc]
  box TV as templateの値 [note: データのrecord, icon: card]
  box II as interface instanceの節 [note: 宣言の中に置く, icon: code]
end

step interfaceは同じ名前の抽象の型を定める
  say まず、interfaceを宣言すると、同じ名前の抽象の型が定まります。interfaceの値はこの型の値で、データのコンストラクタで作れず、値を決まったパターンに照らして直接調べることもできません。
  act IV: 抽象の型が定まる
  source CantonのDaml Language Reference [url: https://docs.canton.network/appdev/reference/daml-language-reference]

step templateの値からinterfaceの値を作る
  say そこで、interfaceの値は、templateの値に変換の関数を適用して作ります。作ったinterfaceの値は、元のtemplateの値の型と欄を中に持ちます。
  TV -> IV: 変換で作る [carry: none]
  source CantonのDaml Language Reference [url: https://docs.canton.network/appdev/reference/daml-language-reference]

step interface instanceの節が全てのmethodを実装する
  say 次に、templateの宣言の中に置くinterface instanceの節は、interfaceの全てのmethodを実装します。
  II -> ME: 全て実装する [carry: none]
  source CantonのDaml Language Reference [url: https://docs.canton.network/appdev/reference/daml-language-reference]
  source CantonのInterfaces [url: https://docs.canton.network/appdev/modules/m3-interfaces]

step 特別なmethodがviewの型の値を返す
  say methodのうち特別な1つは、viewの型のために必ず定めます。interfaceの全てのインスタンスがこのmethodを実装し、viewの型の値を返します。viewの型は、recordです。
  ME -> VT: 特別なmethodで返す [carry: none]
  source CantonのDaml Language Reference [url: https://docs.canton.network/appdev/reference/daml-language-reference]

step methodでchoiceのcontrollerとactionを定める
  say さらに、methodでは、interfaceのchoiceのcontrollerと、exerciseした時に動くactionを定められます。
  act ME: choiceで使う
  act CH: methodで定める
  source CantonのDaml Language Reference [url: https://docs.canton.network/appdev/reference/daml-language-reference]

step methodはinterfaceの値にだけ適用する
  say methodが導く関数は、interfaceの値にしか適用できません。templateの値に使うには、先にinterfaceの値に変換します。
  ME -> IV: 値に適用する [carry: none]
  source CantonのDaml Language Reference [url: https://docs.canton.network/appdev/reference/daml-language-reference]

step choiceはinterfaceの値でexerciseする
  say 最後に、interfaceのchoiceも、interfaceの値でしかexerciseできません。interfaceのインスタンスがあるtemplateの型のコントラクトは、どれも、controllerのpartyにこのchoiceを与えます。
  CH -> IV: 値でexerciseする [side: right, carry: none]
  source CantonのDaml Language Reference [url: https://docs.canton.network/appdev/reference/daml-language-reference]
```

## 値とコントラクトIDをtemplateとinterfaceの間で変える

interfaceの値とコントラクトIDを扱う関数は、次の表のとおりです。

| 関数 | 型 | 働き |
|---|---|---|
| `interfaceTypeRep` | `HasInterfaceTypeRep i => i -> TemplateTypeRep` | interfaceの値を作るのに使ったtemplateを、`TemplateTypeRep`の値で示す |
| `toInterface` | `forall i t. HasToInterface t i => t -> i` | templateの値をinterfaceの値に変える |
| `fromInterface` | `HasFromInterface t i => i -> Optional t` | interfaceの値をtemplateの値に戻そうとする。期待したtemplateの型が、コントラクトを作った元のtemplateの型と合わない時は`None`になる |
| `toInterfaceContractId` | `forall i t. HasToInterface t i => ContractId t -> ContractId i` | templateのコントラクトIDを、interfaceのコントラクトIDに変える |
| `fromInterfaceContractId` | `forall t i. HasFromInterface t i => ContractId i -> ContractId t` | interfaceのコントラクトIDを、templateのコントラクトIDに変える |
| `coerceInterfaceContractId` | `forall j i. (HasInterfaceTypeRep i, HasInterfaceTypeRep j) => ContractId i -> ContractId j` | interfaceのコントラクトIDを、別のinterfaceのコントラクトIDに変える |
| `fetchFromInterface` | `forall t i. (HasFromInterface t i, HasFetch i) => ContractId i -> Update (Optional (ContractId t, t))` | interfaceのコントラクトIDをfetchしてtemplateに変えようとし、変えられれば変えたコントラクトとそのコントラクトIDを、変えられなければ`None`を返す |

型のパラメータは、データ型を定める時に名前だけを置き、使う時に具体的な型を当てはめる所です。
関数の型にも、同じく型のパラメータを置けます。
`forall i t.`は、続く型の`i`と`t`が型のパラメータであることを示します。
たとえば、`toInterface`の型の`t`に`MyTemplate`を、`i`に`MyInterface`を当てはめると、`MyTemplate -> MyInterface`になります。

型の`=>`より前の`HasToInterface t i`のような部分は、型の制約です。
型の制約は、型のパラメータに当てはめる型が、決まったtypeclassのインスタンスであることを求める条件で、typeclassは、いくつかの型に共通する関数を決める仕組みです。

`Optional`は、欠けているかもしれない値を表す型で、値を取る`Some`と、値を取らない`None`の2つのコンストラクタを持ちます。
`TemplateTypeRep`は、templateのIDを一意に表す文字の表現で、templateのIDは、templateを1つに決める識別子です。
`Update a`は、台帳を更新するか照会し、その後で型`a`の値を返す処理の型です。
表の型では、templateのコントラクトIDの型は`ContractId t`で、interfaceのコントラクトIDの型は`ContractId i`です。

Daml Language Referenceでは、`fromInterfaceContractId`は、渡したコントラクトIDが、結果の型のコントラクトを本当に指すかを確かめず、指していないと、後の`fetch`、`exercise`、`archive`が失敗します。
そのため、元のコントラクトが結果の型だと分かっている時か、結果をすぐに`fetch`、`exercise`、`archive`に使い、型が合わない時にトランザクションが失敗することが望む振る舞いの時にだけ使います。
それ以外の時は、`fetchFromInterface`を使うことを考えます。

`coerceInterfaceContractId`も、指す先のコントラクトが結果の型かを確かめないので、同じ条件の時にだけ使います。
`fetch`、`exercise`、`archive`の式は、章「Damlのtemplateとchoice」の頁「choiceの本体で台帳を更新する」が追っています。

https://cardene.dev/diagrams/canton-daml-update-actions

## 要求するinterfaceを全て明示する

```haskell
interface OurInterface requires MyInterface, YourInterface where
  viewtype EmptyInterfaceView
```

interfaceは、ほかのinterfaceに依存できます。
依存するinterfaceは、interfaceの名前の後、`where`のキーワードの前に、`requires`のキーワードと、コンマで区切った名前で指定します。

宣言が正しいためには、要求するinterfaceの並びが推移的に閉じていることが要ります。
つまり、interface `I`は、interface `J`が要求する全てのinterfaceを明示して要求しない限り、`J`を要求できません。
一方、並べる順は問いません。

たとえば、次の宣言では、`Rectangle`が`Shape`を要求します。

```haskell
interface Shape where
  viewtype EmptyInterfaceView

interface Rectangle requires Shape where
  viewtype EmptyInterfaceView
```

この時、`Square`の宣言が`Rectangle`だけを要求すると、コンパイラのエラーになります。

```haskell
-- Compiler error! "Interface Square is missing requirement [Shape]"
interface Square requires Rectangle where
  viewtype EmptyInterfaceView
```

要求するinterfaceに`Shape`を明示して足すと、エラーは直ります。

```haskell
interface Square requires Rectangle, Shape where
  viewtype EmptyInterfaceView
```

また、template `T`をinterface `I`の正しいインスタンスにするには、`T`を、`I`が要求する各interfaceのインスタンスにもします。
要求の関係にあるinterfaceの間でも、次の表のように変換の関数を使えます。

| 関数 | 要求の関係で変えられる物 |
|---|---|
| `toInterface` | interfaceの値を、そのinterfaceが要求するinterfaceの値に変える |
| `fromInterface` | interfaceの値を、そのinterfaceを要求するinterfaceの値に変える |
| `toInterfaceContractId` | interfaceのコントラクトIDを、そのinterfaceが要求するinterfaceのコントラクトIDに変える |
| `fromInterfaceContractId` | interfaceのコントラクトIDを、そのinterfaceを要求するinterfaceのコントラクトIDに変える |
| `fetchFromInterface` | interfaceのコントラクトIDをfetchして、そのinterfaceを要求するinterfaceのコントラクトとコントラクトIDに変える |

## 標準のinterfaceでほかのアプリと組む

interfaceは、振る舞いを実装から切り離します。
そのため、ほかの開発者は、具体的なtemplateでなく、interfaceに向けてアプリを書けます。
違うtemplateで作ったコントラクトも、同じinterfaceを実装していれば、相互運用できます。
相互運用は、別々に作った物どうしが、組んで動けることです。

interfaceは、Canton Networkをまたぐ相互運用の中心です。
CIP-0056のToken Standardは、Canton Networkのトークンの標準のDamlのinterfaceを定めます。
CIPは、Canton Improvement Proposalの略で、Canton Networkに変更と標準と改善を提案する正式な仕組みです。

自分のアプリがトークンを発行するか移す時に、これらのinterfaceを実装すると、相手ごとの個別の統合なしに、エコシステムのwalletとほかのアプリが、自分のアプリの資産を使えるようになります。
walletは、利用者がトークンを持ち、送るのに使うアプリです。

CIP-0056が定める標準のAPIと、CIPを書いて提案する手続きは、章「CIPの統治の手続き」の頁「CIPを書いて提案する」が追っています。

https://cardene.dev/diagrams/canton-cip-writing

図Bでは、ほかの開発者のアプリと、違うtemplateのコントラクトが、interfaceでどう組むかを追います。
続けて、トークンを発行するか移す自分のアプリがToken Standardのinterfaceを実装し、walletとほかのアプリがその資産を使う所を、段ごとに追います。

```dragon
architecture 同じinterfaceでほかのアプリと組む

box AP as ほかの開発者のアプリ [note: interfaceに向けて書く, color: rose, icon: app]
box IF as interface [note: 振る舞いを定める, color: slate, icon: layers]
box CT as 違うtemplateのコントラクト [note: 同じinterfaceを実装, color: blue, icon: doc]
box TS as Token Standardのinterface [note: トークンの標準, color: slate, icon: layers]
box MA as 自分のアプリ [note: トークンを発行か移す, color: rose, icon: app]
box WL as wallet [note: 資産を使う, color: rose, icon: card]
box OA as ほかのアプリ [note: 資産を使う, color: rose, icon: app]

step ほかの開発者はinterfaceに向けて書く
  say まず、interfaceは振る舞いを実装から切り離すので、ほかの開発者は、具体的なtemplateでなく、interfaceに向けてアプリを書けます。
  AP -> IF: 向けて書く [side: right, carry: none]
  source CantonのDaml Language Reference [url: https://docs.canton.network/appdev/reference/daml-language-reference]
  source CantonのInterfaces [url: https://docs.canton.network/appdev/modules/m3-interfaces]

step 違うtemplateのコントラクトが同じinterfaceを実装する
  say そのため、違うtemplateで作ったコントラクトも、同じinterfaceを実装していれば、相互運用できます。
  CT -> IF: 実装する [side: right, carry: none]
  source CantonのInterfaces [url: https://docs.canton.network/appdev/modules/m3-interfaces]

step CIP-0056がトークンの標準のinterfaceを定める
  say さらに、interfaceは、Canton Networkをまたぐ相互運用の中心です。CIP-0056のToken Standardは、Canton Networkのトークンの標準のDamlのinterfaceを定めます。
  act TS: CIP-0056が定める
  source CantonのInterfaces [url: https://docs.canton.network/appdev/modules/m3-interfaces]

step 自分のアプリがinterfaceを実装する
  say 次に、自分のアプリがトークンを発行するか移す時に、これらのinterfaceを実装する所を追います。
  MA -> TS: 実装する [side: right, carry: none]
  source CantonのInterfaces [url: https://docs.canton.network/appdev/modules/m3-interfaces]

step walletとほかのアプリが資産を使う
  say 実装すると、相手ごとの個別の統合なしに、エコシステムのwalletとほかのアプリが、自分のアプリの資産を使えるようになります。
  WL -> MA: 資産を使う [side: right, carry: none]
  OA -> MA: 資産を使う [carry: none]
  source CantonのInterfaces [url: https://docs.canton.network/appdev/modules/m3-interfaces]
```

## 次に読む頁

次の頁「interfaceとpartyを変換する」は、Damlでtemplateとinterfaceの値とcontract IDをどう相互に変え、partyを`Text`とどう往復させるかを追います。

https://cardene.dev/diagrams/canton-daml-interface-party

## 出典

https://docs.canton.network/appdev/modules/m3-interfaces

https://docs.canton.network/appdev/reference/daml-language-reference
