From 4022d81d15926697622b59327497466adae3be7d Mon Sep 17 00:00:00 2001 From: Robin Krom Date: Sat, 23 Oct 2021 11:09:38 +0200 Subject: [PATCH] interfaces: add an experimental `toTypeRep` builtin. 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 --- .../daml-stdlib-src/DA/Experimental/Interface.daml | 4 ++++ .../tests/daml-test-files/InterfaceExperimental.daml | 10 ++++++++++ .../com/digitalasset/daml/lf/speedy/SBuiltin.scala | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/compiler/damlc/daml-stdlib-src/DA/Experimental/Interface.daml b/compiler/damlc/daml-stdlib-src/DA/Experimental/Interface.daml index 1960e4c6b252..30b050b97c93 100644 --- a/compiler/damlc/daml-stdlib-src/DA/Experimental/Interface.daml +++ b/compiler/damlc/daml-stdlib-src/DA/Experimental/Interface.daml @@ -5,6 +5,7 @@ module DA.Experimental.Interface( interfaceCreate, interfaceSignatory, interfaceObserver, + toTypeRep, ) where import GHC.Types (primitive) @@ -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 diff --git a/compiler/damlc/tests/daml-test-files/InterfaceExperimental.daml b/compiler/damlc/tests/daml-test-files/InterfaceExperimental.daml index 0cb2a7fc3114..19f2f6c7eafb 100644 --- a/compiler/damlc/tests/daml-test-files/InterfaceExperimental.daml +++ b/compiler/damlc/tests/daml-test-files/InterfaceExperimental.daml @@ -8,6 +8,7 @@ module InterfaceExperimental where import DA.Assert ((===)) +import DA.Action (unless) import DA.Experimental.Interface import GHC.Types (primitive) @@ -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 () diff --git a/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SBuiltin.scala b/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SBuiltin.scala index e0c8600a9214..72ef80a7721e 100644 --- a/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SBuiltin.scala +++ b/daml-lf/interpreter/src/main/scala/com/digitalasset/daml/lf/speedy/SBuiltin.scala @@ -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),