Skip to content

Commit

Permalink
interfaces: add an experimental toTypeRep builtin. (#11378)
Browse files Browse the repository at this point in the history
This adds an experimental `toTypeRep: forall t. t -> TypeRep` builtin.
It will only work on interface payloads and crash horribly otherwise.

CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
Robin Krom authored Oct 25, 2021
1 parent 5654d5c commit f89ecc6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/damlc/daml-stdlib-src/DA/Experimental/Interface.daml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module DA.Experimental.Interface(
interfaceCreate,
interfaceSignatory,
interfaceObserver,
toTypeRep,
) where

import GHC.Types (primitive)
Expand All @@ -17,3 +18,6 @@ interfaceSignatory payload = primitive @"$RESOLVE_VIRTUAL_SIGNATORY" payload pay

interfaceObserver: t -> [Party]
interfaceObserver payload = primitive @"$RESOLVE_VIRTUAL_OBSERVER" payload payload

toTypeRep: t -> TypeRep
toTypeRep arg = primitive @"$TO_TYPE_REP" arg
10 changes: 10 additions & 0 deletions compiler/damlc/tests/daml-test-files/InterfaceExperimental.daml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
module InterfaceExperimental where

import DA.Assert ((===))
import DA.Action (unless)
import DA.Experimental.Interface
import GHC.Types (primitive)

Expand Down Expand Up @@ -62,13 +63,22 @@ template Asset
do
pure ()

template NotAsset
with
p : Party
where
signatory p

main = scenario do
alice <- getParty "Alice"
bob <- getParty "Bob"
let asset = Asset alice bob 15
let notAsset = NotAsset alice
let token = toToken asset
submit alice do
interfaceCreate token
interfaceSignatory token === [alice]
interfaceObserver token === [bob, alice]
unless (toTypeRep token == toTypeRep asset) $ abort "TypeReps are not equal"
unless (toTypeRep token /= toTypeRep notAsset) $ abort "TypeReps are equal"
pure ()
Original file line number Diff line number Diff line change
Expand Up @@ -1667,9 +1667,17 @@ private[lf] object SBuiltin {
machine.returnValue = SInt64(42L)
}

private object SBExperimentalToTypeRep extends SBuiltinPure(1) {
override private[speedy] def executePure(args: util.ArrayList[SValue]): STypeRep = {
val id = getSRecord(args, 0).id
STypeRep(Ast.TTyCon(id))
}
}

private val mapping: Map[String, SExpr] =
List(
"ANSWER" -> SBExperimentalAnswer,
"TO_TYPE_REP" -> SBExperimentalToTypeRep,
"RESOLVE_VIRTUAL_CREATE" -> new SBResolveVirtual(CreateDefRef),
"RESOLVE_VIRTUAL_SIGNATORY" -> new SBResolveVirtual(SignatoriesDefRef),
"RESOLVE_VIRTUAL_OBSERVER" -> new SBResolveVirtual(ObserversDefRef),
Expand Down

0 comments on commit f89ecc6

Please sign in to comment.