Update documentation on template desugaring (#3171)

This includes the changes for AnyTemplate and AnyChoice as well as the
underscore prefixes.
This commit is contained in:
Moritz Kiefer 2019-10-14 17:00:00 +02:00 committed by GitHub
parent fc1a000090
commit a2f46cac8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -46,6 +46,8 @@ class Template t where
create : t -> Update (ContractId t) create : t -> Update (ContractId t)
fetch : ContractId t -> Update t fetch : ContractId t -> Update t
archive : ContractId t -> Update () archive : ContractId t -> Update ()
toAnyTemplate : t -> AnyTemplate
fromAnyTemplate : AnyTemplate -> Optional t
``` ```
In this example, `t` is identified with `Iou`. The rest of this section shows you how desugaring proceeds. In this example, `t` is identified with `Iou`. The rest of this section shows you how desugaring proceeds.
@ -71,50 +73,64 @@ Next we have a `class IouInstance` with the bulk of the definitions we will need
```haskell ```haskell
class IouInstance where class IouInstance where
signatoryIou : Iou -> [Party] _signatoryIou : Iou -> [Party]
signatoryIou this@Iou{..} = [issuer, owner] _signatoryIou this@Iou{..} = [issuer, owner]
observerIou : Iou -> [Party] _observerIou : Iou -> [Party]
observerIou this@Iou{..} = regulators _observerIou this@Iou{..} = regulators
ensureIou : Iou -> Bool _ensureIou : Iou -> Bool
ensureIou this@Iou{..} = amount > 0.0 _ensureIou this@Iou{..} = amount > 0.0
agreementIou : Iou -> Text _agreementIou : Iou -> Text
agreementIou this@Iou{..} = show issuer <> " will pay " <> show owner <> " " <> show amount _agreementIou this@Iou{..} = show issuer <> " will pay " <> show owner <> " " <> show amount
createIou : Iou -> Update (ContractId Iou) _createIou : Iou -> Update (ContractId Iou)
createIou = magic @"create" _createIou = magic @"create"
fetchIou : ContractId Iou -> Update Iou _fetchIou : ContractId Iou -> Update Iou
fetchIou = magic @"fetch" _fetchIou = magic @"fetch"
archiveIou : ContractId Iou -> Update () _archiveIou : ContractId Iou -> Update ()
archiveIou cid = exerciseIouArchive cid Archive _archiveIou cid = exerciseIouArchive cid Archive
_toAnyTemplateIou : Iou -> AnyTemplate
_toAnyTemplateIou = magic @"toAnyTemplate"
_fromAnyTemplateIou : AnyTemplate -> Optional Iou
_fromAnyTemplateIou = magic @"fromAnyTemplate"
consumptionIouArchive : PreConsuming Iou _consumptionIouArchive : PreConsuming Iou
consumptionIouArchive = PreConsuming _consumptionIouArchive = PreConsuming
controllerIouArchive : Iou -> Archive -> [Party] _controllerIouArchive : Iou -> Archive -> [Party]
controllerIouArchive this@Iou{..} arg@Archive = signatoryIou this _controllerIouArchive this@Iou{..} arg@Archive = signatoryIou this
actionIouArchive : ContractId Iou -> Iou -> Archive -> Update () _actionIouArchive : ContractId Iou -> Iou -> Archive -> Update ()
actionIouArchive self this@Iou{..} arg@Archive = pure () _actionIouArchive self this@Iou{..} arg@Archive = pure ()
exerciseIouArchive : ContractId Iou -> Archive -> Update () _exerciseIouArchive : ContractId Iou -> Archive -> Update ()
exerciseIouArchive = magic @"archive" _exerciseIouArchive = magic @"archive"
_toAnyChoiceIouArchive : proxy Iou -> Archive -> AnyChoice
_toAnyChoiceIouArchive = magic @"toAnyChoice"
_fromAnyChoiceIouArchive : proxy Iou -> AnyChoice -> Optional Archive
_fromAnyChoiceIouArchive = magic @"fromAnyChoice"
consumptionIouTransfer : PreConsuming Iou _consumptionIouTransfer : PreConsuming Iou
consumptionIouTransfer = PreConsuming _consumptionIouTransfer = PreConsuming
controllerIouTransfer : Iou -> Transfer -> [Party] _controllerIouTransfer : Iou -> Transfer -> [Party]
controllerIouTransfer this@Iou{..} arg@Transfer{..} = [owner] _controllerIouTransfer this@Iou{..} arg@Transfer{..} = [owner]
actionIouTransfer : ContractId Iou -> Iou -> Transfer -> Update (ContractId Iou) _actionIouTransfer : ContractId Iou -> Iou -> Transfer -> Update (ContractId Iou)
actionIouTransfer self this@Iou{..} arg@Transfer{..} = create this with owner = newOwner _actionIouTransfer self this@Iou{..} arg@Transfer{..} = create this with owner = newOwner
exerciseIouTransfer : ContractId Iou -> Transfer -> Update (ContractId Iou) _exerciseIouTransfer : ContractId Iou -> Transfer -> Update (ContractId Iou)
exerciseIouTransfer = magic @"exercise" _exerciseIouTransfer = magic @"exercise"
_toAnyChoiceIouTransfer : proxy Iou -> Transfer -> AnyChoice
_toAnyChoiceIouTransfer = magic @"toAnyChoice"
_fromAnyChoiceIouTransfer : proxy Iou -> AnyChoice -> Optional Transfer
_fromAnyChoiceIouTransfer = magic @"fromAnyChoice"
``` ```
With that class defined, we can define an `instance` declaration for `Iou` to declare its membership in `Template`: With that class defined, we can define an `instance` declaration for `Iou` to declare its membership in `Template`:
```haskell ```haskell
instance IouInstance => Template Iou where instance IouInstance => Template Iou where
signatory = signatoryIou signatory = _signatoryIou
observer = observerIou observer = _observerIou
ensure = ensureIou ensure = _ensureIou
agreement = agreementIou agreement = _agreementIou
create = createIou create = _createIou
fetch = fetchIou fetch = _fetchIou
archive = archiveIou archive = _archiveIou
toAnyTemplate = _toAnyTemplate
fromAnyTemplate = _fromAnyTemplate
instance IouInstance instance IouInstance
``` ```
@ -124,6 +140,8 @@ When a type `t` is a `Template` instance, `class Choice` (defined by the DAML st
```haskell ```haskell
class Template t => Choice t c r | t c -> r where class Template t => Choice t c r | t c -> r where
exercise : ContractId t -> c -> Update r exercise : ContractId t -> c -> Update r
_toAnyChoice : proxy t -> c -> AnyChoice
_fromAnyChoice : proxy t -> AnyChoice -> Optional c
``` ```
In this example, `c` is identified with `Transfer` and `r` with `ContractId Iou`. In this example, `c` is identified with `Transfer` and `r` with `ContractId Iou`.
@ -132,7 +150,9 @@ The `instance` declaration establishes the triple `(Iou, Transfer, ContractId Io
```haskell ```haskell
instance Choice Iou Transfer (ContractId Iou) where instance Choice Iou Transfer (ContractId Iou) where
exercise = exerciseIouTransfer exercise = _exerciseIouTransfer
_toAnyChoice = _toAnyChoiceIouTransfer
_fromAnyChoice = _fromAnyChoiceIouTransfer
``` ```
### Example (2) ### Example (2)
@ -181,51 +201,61 @@ data Enrollment =
deriving (Show, Eq) deriving (Show, Eq)
class EnrollmentInstance where class EnrollmentInstance where
signatoryEnrollment : Enrollment -> [Party] _signatoryEnrollment : Enrollment -> [Party]
signatoryEnrollment this@Enrollment{..} = [reg.student, reg.course.institution] _signatoryEnrollment this@Enrollment{..} = [reg.student, reg.course.institution]
observerEnrollment : Enrollment -> [Party] _observerEnrollment : Enrollment -> [Party]
observerEnrollment this@Enrollment{..} = [] _observerEnrollment this@Enrollment{..} = []
ensureEnrollment : Enrollment -> Bool _ensureEnrollment : Enrollment -> Bool
ensureEnrollment this@Enrollment{..} = True _ensureEnrollment this@Enrollment{..} = True
agreementEnrollment : Enrollment -> Text _agreementEnrollment : Enrollment -> Text
agreementEnrollment this@Enrollment{..} = "" _agreementEnrollment this@Enrollment{..} = ""
createEnrollment : Enrollment -> Update (ContractId Enrollment) _createEnrollment : Enrollment -> Update (ContractId Enrollment)
createEnrollment = magic @"create" _createEnrollment = magic @"create"
fetchEnrollment : ContractId Enrollment -> Update Enrollment _fetchEnrollment : ContractId Enrollment -> Update Enrollment
fetchEnrollment = magic @"fetch" _fetchEnrollment = magic @"fetch"
archiveEnrollment : ContractId Enrollment -> Update () _archiveEnrollment : ContractId Enrollment -> Update ()
archiveEnrollment cid = exerciseEnrollmentArchive cid Archive _archiveEnrollment cid = exerciseEnrollmentArchive cid Archive
_toAnyTemplateEnrollment : Enrollment -> AnyTemplate
_toAnyTemplateEnrollment = magic @"toAnyTemplate"
_fromAnyTemplateEnrollment : AnyTemplate -> Optional Enrollment
_fromAnyTemplateEnrollment = magic @"fromAnyTemplate"
hasKeyEnrollment : HasKey Enrollment _hasKeyEnrollment : HasKey Enrollment
hasKeyEnrollment = HasKey _hasKeyEnrollment = HasKey
keyEnrollment : Enrollment -> Registration _keyEnrollment : Enrollment -> Registration
keyEnrollment this@Enrollment{..} = reg _keyEnrollment this@Enrollment{..} = reg
maintainerEnrollment : HasKey Enrollment -> Registration -> [Party] _maintainerEnrollment : HasKey Enrollment -> Registration -> [Party]
maintainerEnrollment HasKey key = [key.course.institution] _maintainerEnrollment HasKey key = [key.course.institution]
fetchByKeyEnrollment : Registration -> Update (ContractId Enrollment, Enrollment) _fetchByKeyEnrollment : Registration -> Update (ContractId Enrollment, Enrollment)
fetchByKeyEnrollment = magic @"fetchByKey" _fetchByKeyEnrollment = magic @"fetchByKey"
lookupByKeyEnrollment : Registration -> Update (Optional (ContractId Enrollment)) _lookupByKeyEnrollment : Registration -> Update (Optional (ContractId Enrollment))
lookupByKeyEnrollment = magic @"lookupByKey" _lookupByKeyEnrollment = magic @"lookupByKey"
consumptionEnrollmentArchive : PreConsuming Enrollment _consumptionEnrollmentArchive : PreConsuming Enrollment
consumptionEnrollmentArchive = PreConsuming _consumptionEnrollmentArchive = PreConsuming
controllerEnrollmentArchive : Enrollment -> Archive -> [Party] _controllerEnrollmentArchive : Enrollment -> Archive -> [Party]
controllerEnrollmentArchive this@Enrollment{..} arg@Archive = signatoryEnrollment this _controllerEnrollmentArchive this@Enrollment{..} arg@Archive = signatoryEnrollment this
actionEnrollmentArchive : ContractId Enrollment -> Enrollment -> Archive -> Update () _actionEnrollmentArchive : ContractId Enrollment -> Enrollment -> Archive -> Update ()
actionEnrollmentArchive self this@Enrollment{..} arg@Archive = pure () _actionEnrollmentArchive self this@Enrollment{..} arg@Archive = pure ()
exerciseEnrollmentArchive : ContractId Enrollment -> Archive -> Update () _exerciseEnrollmentArchive : ContractId Enrollment -> Archive -> Update ()
exerciseEnrollmentArchive = magic @"archive" _exerciseEnrollmentArchive = magic @"archive"
_toAnyChoiceEnrollmentArchive : proxy Enrollment -> Archive -> AnyChoice
_toAnyChoiceEnrollmentArchive = magic @"toAnyChoice"
_fromAnyChoiceEnrollmentArchive : proxy Enrollment -> AnyChoice -> Optional Archive
_fromAnyChoiceEnrollmentArchive = magic @"fromAnyChoice"
instance EnrollmentInstance instance EnrollmentInstance
instance EnrollmentInstance => Template Enrollment where instance EnrollmentInstance => Template Enrollment where
signatory = signatoryEnrollment signatory = _signatoryEnrollment
observer = observerEnrollment observer = _observerEnrollment
ensure = ensureEnrollment ensure = _ensureEnrollment
agreement = agreementEnrollment agreement = _agreementEnrollment
create = createEnrollment create = _createEnrollment
fetch = fetchEnrollment fetch = _fetchEnrollment
archive = archiveEnrollment archive = _archiveEnrollment
toAnyTemplate = _toAnyTemplateEnrollment
fromAnyTemplate = _fromAnyTemplateEnrollment
instance TemplateKey Enrollment Registration where instance TemplateKey Enrollment Registration where
key = keyEnrollment key = keyEnrollment
@ -268,75 +298,93 @@ data Accept = Accept with
deriving (Eq, Show) deriving (Eq, Show)
class Template t => ProposalInstance t where class Template t => ProposalInstance t where
signatoryProposal : Proposal t -> [Party] _signatoryProposal : Proposal t -> [Party]
signatoryProposal this@Proposal{..} = signatory asset \\ receivers _signatoryProposal this@Proposal{..} = signatory asset \\ receivers
observerProposal : Proposal t -> [Party] _observerProposal : Proposal t -> [Party]
observerProposal this@Proposal{..} = receivers _observerProposal this@Proposal{..} = receivers
ensureProposal : Proposal t -> Bool _ensureProposal : Proposal t -> Bool
ensureProposal this@Proposal{..} = True _ensureProposal this@Proposal{..} = True
agreementProposal : Proposal t -> Text _agreementProposal : Proposal t -> Text
agreementProposal this@Proposal{..} = implode _agreementProposal this@Proposal{..} = implode
[ "Proposal:\n" [ "Proposal:\n"
, "* proposers: " <> show (signatory this) <> "\n" , "* proposers: " <> show (signatory this) <> "\n"
, "* receivers: " <> show receivers <> "\n" , "* receivers: " <> show receivers <> "\n"
, "* agreement: " <> agreement asset , "* agreement: " <> agreement asset
] ]
createProposal : Proposal t -> Update (ContractId (Proposal t)) _createProposal : Proposal t -> Update (ContractId (Proposal t))
createProposal = magic @"create" _createProposal = magic @"create"
fetchProposal : ContractId (Proposal t) -> Update (Proposal t) _fetchProposal : ContractId (Proposal t) -> Update (Proposal t)
fetchProposal = magic @"fetch" _fetchProposal = magic @"fetch"
archiveProposal : ContractId (Proposal t) -> Update () _archiveProposal : ContractId (Proposal t) -> Update ()
archiveProposal cid = exerciseProposalArchive cid Archive _archiveProposal cid = exerciseProposalArchive cid Archive
_toAnyTemplateProposal : Proposal t -> AnyTemplate
_toAnyTemplateProposal = magic @"toAnyTemplate"
_fromAnyTemplateProposal : AnyTemplate -> Optional (Proposal t)
_fromAnyTemplateProposal = magic @"fromAnyTemplate"
hasKeyProposal : HasKey (Proposal t) _hasKeyProposal : HasKey (Proposal t)
hasKeyProposal = HasKey _hasKeyProposal = HasKey
keyProposal : Proposal t -> ([Party], Text) _keyProposal : Proposal t -> ([Party], Text)
keyProposal this@Proposal{..} = (signatory this, name) _keyProposal this@Proposal{..} = (signatory this, name)
maintainerProposal : HasKey (Proposal t) -> ([Party], Text) -> [Party] _maintainerProposal : HasKey (Proposal t) -> ([Party], Text) -> [Party]
maintainerProposal HasKey key = fst key _maintainerProposal HasKey key = fst key
fetchByKeyProposal : ([Party], Text) -> Update (ContractId (Proposal t), Proposal t) _fetchByKeyProposal : ([Party], Text) -> Update (ContractId (Proposal t), Proposal t)
fetchByKeyProposal = magic @"fetchByKey" _fetchByKeyProposal = magic @"fetchByKey"
lookupByKeyProposal : ([Party], Text) -> Update (Optional (ContractId (Proposal t))) _lookupByKeyProposal : ([Party], Text) -> Update (Optional (ContractId (Proposal t)))
lookupByKeyProposal = magic @"lookupByKey" _lookupByKeyProposal = magic @"lookupByKey"
consumptionProposalArchive : PreConsuming (Proposal t) _consumptionProposalArchive : PreConsuming (Proposal t)
consumptionProposalArchive = PreConsuming _consumptionProposalArchive = PreConsuming
controllerProposalArchive : Proposal t -> Archive -> [Party] _controllerProposalArchive : Proposal t -> Archive -> [Party]
controllerProposalArchive this@Proposal{..} arg@Archive = signatoryProposal this _controllerProposalArchive this@Proposal{..} arg@Archive = signatoryProposal this
actionProposalArchive : ContractId (Proposal t) -> Proposal t -> Archive -> Update () _actionProposalArchive : ContractId (Proposal t) -> Proposal t -> Archive -> Update ()
actionProposalArchive self this@Proposal{..} arg@Archive = pure () _actionProposalArchive self this@Proposal{..} arg@Archive = pure ()
exerciseProposalArchive : ContractId (Proposal t) -> Archive -> Update () _exerciseProposalArchive : ContractId (Proposal t) -> Archive -> Update ()
exerciseProposalArchive = magic @"archive" _exerciseProposalArchive = magic @"archive"
_toAnyChoiceProposalArchive : proxy (Proposal t) -> Archive -> AnyChoice
_toAnyChoiceProposalArchive = magic @"toAnyChoice"
_fromAnyChoiceProposalArchive : proxy (Proposal t) -> AnyChoice -> Optional Archive
_fromAnyChoiceProposalArchive = magic @"fromAnyChoice"
consumptionProposalAccept : PreConsuming (Proposal t) _consumptionProposalAccept : PreConsuming (Proposal t)
consumptionProposalAccept = PreConsuming _consumptionProposalAccept = PreConsuming
controllerProposalAccept : Proposal t -> Accept -> [Party] _controllerProposalAccept : Proposal t -> Accept -> [Party]
controllerProposalAccept this@Proposal{..} arg@Accept = receivers _controllerProposalAccept this@Proposal{..} arg@Accept = receivers
actionProposalAccept : ContractId (Proposal t) -> Proposal t -> Accept -> Update (ContractId t) _actionProposalAccept : ContractId (Proposal t) -> Proposal t -> Accept -> Update (ContractId t)
actionProposalAccept self this@Proposal{..} arg@Accept = do _actionProposalAccept self this@Proposal{..} arg@Accept = do
create asset create asset
exerciseProposalAccept : ContractId (Proposal t) -> Accept -> Update (ContractId t) _exerciseProposalAccept : ContractId (Proposal t) -> Accept -> Update (ContractId t)
exerciseProposalAccept = magic @"exercise" _exerciseProposalAccept = magic @"exercise"
_toAnyChoiceProposalAccept : proxy (Proposal t) -> Accept -> AnyChoice
_toAnyChoiceProposalAccept = magic @"toAnyChoice"
_fromAnyChoiceProposalAccept : proxy (Proposal t) -> AnyChoice -> Optional Accept
_fromAnyChoiceProposalAccept = magic @"fromAnyChoice"
instance ProposalInstance t => Template (Proposal t) where instance ProposalInstance t => Template (Proposal t) where
signatory = signatoryProposal signatory = _signatoryProposal
observer = observerProposal observer = _observerProposal
ensure = ensureProposal ensure = _ensureProposal
agreement = agreementProposal agreement = _agreementProposal
create = createProposal create = _createProposal
fetch = fetchProposal fetch = _fetchProposal
archive = archiveProposal archive = _archiveProposal
toAnyTemplate = _toAnyTemplate
fromAnyTemplate = _fromAnyTemplate
instance ProposalInstance t => TemplateKey (Proposal t) ([Party], Text) where instance ProposalInstance t => TemplateKey (Proposal t) ([Party], Text) where
key = keyProposal key = _keyProposal
fetchByKey = fetchByKeyProposal fetchByKey = _fetchByKeyProposal
lookupByKey = lookupByKeyProposal lookupByKey = _lookupByKeyProposal
instance ProposalInstance t => Choice (Proposal t) Accept (ContractId t) where instance ProposalInstance t => Choice (Proposal t) Accept (ContractId t) where
exercise = exerciseProposalAccept exercise = _exerciseProposalAccept
_toAnyChoice = _toAnyChoiceProposalAccept
_fromAnyChoice = _fromAnyChoiceProposalAccept
instance ProposalInstance t => Choice (Proposal t) Archive () where instance ProposalInstance t => Choice (Proposal t) Archive () where
exercise = exerciseProposalArchive exercise = exerciseProposalArchive
_toAnyChoice = _toAnyChoiceProposalArchive
_fromAnyChoice = _fromAnyChoiceProposalArchive
``` ```
### Example (3)(cont) ### Example (3)(cont)