Skip to content

Commit

Permalink
Add conversions from/to interfaces in Daml parser (#10954)
Browse files Browse the repository at this point in the history
* Add conversions from/to interfaces in Daml parser

changelog_begin
changelog_end

* .

changelog_begin
changelog_end

* .

changelog_begin
changelog_end

* .

changelog_begin
changelog_end
  • Loading branch information
cocreature authored Sep 21, 2021
1 parent d50df11 commit 4acf34b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ci/da-ghc-lib/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
variables:
ghc-lib-sha: '42e5c306dcfbc84b83336fdd531023e93bfcc5b2'
base-sha: '9c787d4d24f2b515934c8503ee2bbd7cfac4da20'
patches: '9f72c788c66c1ee913e5439308840afea5727e70 833ca63be2ab14871874ccb6974921e8952802e9'
patches: 'f369e0f5c6933e895f90ae4efcb3d4294aba532b 833ca63be2ab14871874ccb6974921e8952802e9'
flavor: 'ghc-8.8.1'
steps:
- checkout: self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,33 @@ convertPrim _ "UTryCatch" ((TUnit :-> TUpdate t1) :-> (TBuiltin BTAnyException :
(mkVar "x")
(EVar (mkVar "c") `ETmApp` EVar (mkVar "x"))

convertPrim _ "UFromInterfaceContractId" (TContractId (TCon iface) :-> TUpdate (TOptional (TContractId (TCon tpid)))) =
ETmLam (mkVar "cid", TContractId (TCon iface)) $
if iface == tpid
then EUpdate $ UPure (TOptional (TContractId (TCon iface))) (ESome (TContractId (TCon iface)) $ EVar $ mkVar "cid")
else EUpdate $
UBind
(Binding (mkVar "iface", TCon iface) (EUpdate $ UFetchInterface iface (EVar $ mkVar "cid"))) $
EUpdate $ UPure (TOptional (TContractId (TCon tpid))) $ ECase (EFromInterface iface tpid (EVar $ mkVar "iface"))
[ CaseAlternative CPNone (ENone (TContractId (TCon tpid)))
, CaseAlternative (CPSome $ mkVar "_") (ESome (TContractId (TCon tpid)) (EBuiltin BECoerceContractId `ETyApp` TCon iface `ETyApp` TCon tpid `ETmApp` (EVar $ mkVar "cid")))
]

convertPrim _ "EToInterfaceContractId" (TContractId tpid :-> TContractId iface) =
EBuiltin BECoerceContractId `ETyApp` tpid `ETyApp` iface

convertPrim _ "EToInterface" (TCon tpid :-> TCon iface) =
ETmLam (mkVar "t", TCon tpid) $
if tpid == iface
then EVar (mkVar "t")
else EToInterface iface tpid (EVar $ mkVar "t")

convertPrim _ "EFromInterface" (TCon iface :-> TOptional (TCon tpid)) =
ETmLam (mkVar "i", TCon iface) $
if tpid == iface
then ESome (TCon tpid) (EVar $ mkVar "i")
else EFromInterface iface tpid (EVar $ mkVar "i")

convertPrim (V1 PointDev) (L.stripPrefix "$" -> Just builtin) typ =
EExperimental (T.pack builtin) typ

Expand Down
1 change: 1 addition & 0 deletions compiler/damlc/tests/daml-test-files/Interface.daml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

-- @SINCE-LF-FEATURE DAML_INTERFACE
-- @WARN Modules compiled with the DatatypeContexts language extension
-- @ERROR EToInterface not yet implemented
{-# LANGUAGE DatatypeContexts #-}

module Interface where
Expand Down
13 changes: 13 additions & 0 deletions compiler/damlc/tests/daml-test-files/InterfaceDesugared.daml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

-- @SINCE-LF-FEATURE DAML_INTERFACE
-- @WARN Modules compiled with the DatatypeContexts language extension
-- @ERROR EToInterface not yet implemented
{-# LANGUAGE DatatypeContexts #-}

module InterfaceDesugared where
Expand All @@ -18,6 +19,10 @@ class
-- TODO https://github.com/digital-asset/daml/issues/10810
-- enable once we support pure functions
-- getAmount : t -> Decimal
toToken : t -> Token
fromToken : Token -> Optional t
toTokenContractId : ContractId t -> ContractId Token
fromTokenContractId : ContractId Token -> Update (Optional (ContractId t))

data GHC.Types.DamlInterface => Token = Token GHC.Types.Opaque

Expand All @@ -31,6 +36,10 @@ instance IsToken Token where
-- TODO https://github.com/digital-asset/daml/issues/10810
-- enable once we support pure functions
-- getAmount = primitivInterface @"getAmount"
toToken = GHC.Types.primitive @"EToInterface"
fromToken = GHC.Types.primitive @"EFromInterface"
toTokenContractId = GHC.Types.primitive @"EToInterfaceContractId"
fromTokenContractId = GHC.Types.primitive @"UFromInterfaceContractId"

data Asset = Asset { amount : Decimal, issuer : Party, owner : Party }
deriving (Eq, Show)
Expand All @@ -39,6 +48,10 @@ instance IsToken Asset where
-- TODO https://github.com/digital-asset/daml/issues/10810
-- enable once we support pure functions
-- getAmount Asset{..} = amount
toToken = GHC.Types.primitive @"EToInterface"
fromToken = GHC.Types.primitive @"EFromInterface"
toTokenContractId = GHC.Types.primitive @"EToInterfaceContractId"
fromTokenContractId = GHC.Types.primitive @"UFromInterfaceContractId"

_implements_AssetToken : DA.Internal.Desugar.Implements Asset Token
_implements_AssetToken = DA.Internal.Desugar.Implements
Expand Down
8 changes: 4 additions & 4 deletions stack-snapshot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

resolver: lts-18.0
packages:
- archive: https://daml-binaries.da-ext.net/da-ghc-lib/ghc-lib-12d888a292cb63ac2680ce92bad4ae1f.tar.gz
sha256: "88671cc2376affa82f103aaae930e502f30441ca1a94a78fcfd08b6aef77a7d3"
- archive: https://daml-binaries.da-ext.net/da-ghc-lib/ghc-lib-parser-12d888a292cb63ac2680ce92bad4ae1f.tar.gz
sha256: "492d50a671bb7d9d826c2e9c77c6d0fdf25030f3f953bbd6f2df92f9ebaceed9"
- archive: https://daml-binaries.da-ext.net/da-ghc-lib/ghc-lib-837a79515c4e70f9440d433ed7833e75.tar.gz
sha256: "b640032ae942c4edc4a5bc6a314701ed5f030e8a16c492281cf3ee5a5ba97a48"
- archive: https://daml-binaries.da-ext.net/da-ghc-lib/ghc-lib-parser-837a79515c4e70f9440d433ed7833e75.tar.gz
sha256: "2f70bebd6fa59210258a60ed4450238888dd20e7a06996924b56859cda1ae574"
- github: digital-asset/hlint
commit: "c8246c1feb932858ff2b5d7e9e900068a974bf57"
sha256: "3da24baf789c5f00211a92e24153e6b88102befaa946ada1f707935554500fe2"
Expand Down
8 changes: 4 additions & 4 deletions stackage_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__GENERATED_FILE_DO_NOT_MODIFY_MANUALLY": -192357799,
"all-cabal-hashes": "https://raw.githubusercontent.com/commercialhaskell/all-cabal-hashes/3c0c4b542d90a20d20df01b2a8c42cb33bc2f878",
"__GENERATED_FILE_DO_NOT_MODIFY_MANUALLY": -790502855,
"all-cabal-hashes": "https://raw.githubusercontent.com/commercialhaskell/all-cabal-hashes/c230f953f54d57e57ca0a33b6221ad6edf69b140",
"resolved": {
"Cabal": {"dependencies":[],"location":{"type":"core"},"name":"Cabal","version":"3.2.1.0"},
"Decimal": {"dependencies":["base","deepseq"],"location":{"type":"hackage","url":"https://hackage.haskell.org/package/Decimal-0.5.2/Decimal-0.5.2.tar.gz"},"name":"Decimal","pinned":{"url":["https://hackage.haskell.org/package/Decimal-0.5.2/Decimal-0.5.2.tar.gz","https://s3.amazonaws.com/hackage.fpcomplete.com/package/Decimal-0.5.2.tar.gz"],"sha256":"a37a0220424e4bcb8cae1d38844c7027ee314449758d0d14ff3e2e0a5c8a87a7","cabal-sha256":"83dd16a1c0737fd35fdb1088af36e1a53034e75090e3f0d4ad32296f1a35a13b"},"version":"0.5.2"},
Expand Down Expand Up @@ -100,8 +100,8 @@
"generic-deriving": {"dependencies":["base","containers","ghc-prim","template-haskell","th-abstraction"],"location":{"type":"hackage","url":"https://hackage.haskell.org/package/generic-deriving-1.14/generic-deriving-1.14.tar.gz"},"name":"generic-deriving","pinned":{"url":["https://hackage.haskell.org/package/generic-deriving-1.14/generic-deriving-1.14.tar.gz","https://s3.amazonaws.com/hackage.fpcomplete.com/package/generic-deriving-1.14.tar.gz"],"sha256":"d0abd5e423960b66867c6149c20b221b1351e3805d1bf787fc4efa3e7bb7cb02","cabal-sha256":"c9c6782a3cdce2f2bcaf891e0ffbf06df871e68498574b73a565771dc084273c"},"version":"1.14"},
"generics-sop": {"dependencies":["base","ghc-prim","sop-core","template-haskell","th-abstraction"],"location":{"type":"hackage","url":"https://hackage.haskell.org/package/generics-sop-0.5.1.1/generics-sop-0.5.1.1.tar.gz"},"name":"generics-sop","pinned":{"url":["https://hackage.haskell.org/package/generics-sop-0.5.1.1/generics-sop-0.5.1.1.tar.gz","https://s3.amazonaws.com/hackage.fpcomplete.com/package/generics-sop-0.5.1.1.tar.gz"],"sha256":"81b7c38b5c2a1ae3c790b1707a0e2a2031430e33b3683f88e2daa5b59ae4c5d8","cabal-sha256":"522a1a1da05d5acc03da448fa603c038cc15b991272846a591e26e7505d2b73a"},"version":"0.5.1.1"},
"ghc-byteorder": {"dependencies":["base"],"location":{"type":"hackage","url":"https://hackage.haskell.org/package/ghc-byteorder-4.11.0.0.10/ghc-byteorder-4.11.0.0.10.tar.gz"},"name":"ghc-byteorder","pinned":{"url":["https://hackage.haskell.org/package/ghc-byteorder-4.11.0.0.10/ghc-byteorder-4.11.0.0.10.tar.gz","https://s3.amazonaws.com/hackage.fpcomplete.com/package/ghc-byteorder-4.11.0.0.10.tar.gz"],"sha256":"86e50a89798181db4f44ec3848fc52940c73098e88549a351ceb54fefc691fb6","cabal-sha256":"d47fdef63f2a145091e91b1aedbd46bf61ca03bb935e81e3462a1d904c319de3"},"version":"4.11.0.0.10"},
"ghc-lib": {"dependencies":["alex","array","base","binary","bytestring","containers","deepseq","directory","filepath","ghc-lib-parser","ghc-prim","happy","hpc","pretty","process","rts","time","transformers","unix"],"location":{"type":"archive","url":"https://daml-binaries.da-ext.net/da-ghc-lib/ghc-lib-12d888a292cb63ac2680ce92bad4ae1f.tar.gz"},"name":"ghc-lib","pinned":{"sha256":"88671cc2376affa82f103aaae930e502f30441ca1a94a78fcfd08b6aef77a7d3","strip-prefix":"ghc-lib-8.8.1.20210916"},"version":"8.8.1.20210916"},
"ghc-lib-parser": {"dependencies":["alex","array","base","binary","bytestring","containers","deepseq","directory","filepath","ghc-prim","happy","hpc","pretty","process","time","transformers","unix"],"location":{"type":"archive","url":"https://daml-binaries.da-ext.net/da-ghc-lib/ghc-lib-parser-12d888a292cb63ac2680ce92bad4ae1f.tar.gz"},"name":"ghc-lib-parser","pinned":{"sha256":"492d50a671bb7d9d826c2e9c77c6d0fdf25030f3f953bbd6f2df92f9ebaceed9","strip-prefix":"ghc-lib-parser-8.8.1.20210916"},"version":"8.8.1.20210916"},
"ghc-lib": {"dependencies":["alex","array","base","binary","bytestring","containers","deepseq","directory","filepath","ghc-lib-parser","ghc-prim","happy","hpc","pretty","process","rts","time","transformers","unix"],"location":{"type":"archive","url":"https://daml-binaries.da-ext.net/da-ghc-lib/ghc-lib-837a79515c4e70f9440d433ed7833e75.tar.gz"},"name":"ghc-lib","pinned":{"sha256":"b640032ae942c4edc4a5bc6a314701ed5f030e8a16c492281cf3ee5a5ba97a48","strip-prefix":"ghc-lib-8.8.1.20210921"},"version":"8.8.1.20210921"},
"ghc-lib-parser": {"dependencies":["alex","array","base","binary","bytestring","containers","deepseq","directory","filepath","ghc-prim","happy","hpc","pretty","process","time","transformers","unix"],"location":{"type":"archive","url":"https://daml-binaries.da-ext.net/da-ghc-lib/ghc-lib-parser-837a79515c4e70f9440d433ed7833e75.tar.gz"},"name":"ghc-lib-parser","pinned":{"sha256":"2f70bebd6fa59210258a60ed4450238888dd20e7a06996924b56859cda1ae574","strip-prefix":"ghc-lib-parser-8.8.1.20210921"},"version":"8.8.1.20210921"},
"ghc-lib-parser-ex": {"dependencies":["base","bytestring","containers","extra","ghc-lib-parser","uniplate"],"location":{"type":"hackage","url":"https://hackage.haskell.org/package/ghc-lib-parser-ex-8.8.5.8/ghc-lib-parser-ex-8.8.5.8.tar.gz"},"name":"ghc-lib-parser-ex","pinned":{"url":["https://hackage.haskell.org/package/ghc-lib-parser-ex-8.8.5.8/ghc-lib-parser-ex-8.8.5.8.tar.gz","https://s3.amazonaws.com/hackage.fpcomplete.com/package/ghc-lib-parser-ex-8.8.5.8.tar.gz"],"sha256":"b36ef8b49da4e8c78b00dca9b9546b7d4db0b09b10da5e313d3f0dbb4af581d7","cabal-sha256":"04f164fd6a4a5b0c5627cf7fadd79174c3b1d4c696dc481a9909266bd6a2a6aa"},"version":"8.8.5.8"},
"ghc-prim": {"dependencies":[],"location":{"type":"core"},"name":"ghc-prim","version":"0.6.1"},
"ghcide": {"dependencies":["aeson","async","base","binary","bytestring","containers","data-default","deepseq","dependent-map","dependent-sum","directory","extra","filepath","fuzzy","ghc-lib","ghc-lib-parser","haddock-library","hashable","hslogger","lsp","lsp-types","mtl","network-uri","prettyprinter","prettyprinter-ansi-terminal","regex-tdfa","rope-utf16-splay","safe-exceptions","shake","some","sorted-list","stm","syb","text","time","transformers","unix","unliftio","unordered-containers","utf8-string"],"location":{"type":"vendored"},"name":"ghcide","version":"0.1.0"},
Expand Down
8 changes: 4 additions & 4 deletions stackage_snapshot_windows.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__GENERATED_FILE_DO_NOT_MODIFY_MANUALLY": -852443363,
"all-cabal-hashes": "https://raw.githubusercontent.com/commercialhaskell/all-cabal-hashes/3c0c4b542d90a20d20df01b2a8c42cb33bc2f878",
"__GENERATED_FILE_DO_NOT_MODIFY_MANUALLY": 712713201,
"all-cabal-hashes": "https://raw.githubusercontent.com/commercialhaskell/all-cabal-hashes/c230f953f54d57e57ca0a33b6221ad6edf69b140",
"resolved": {
"Cabal": {"dependencies":[],"location":{"type":"core"},"name":"Cabal","version":"3.2.1.0"},
"Decimal": {"dependencies":["base","deepseq"],"location":{"type":"hackage","url":"https://hackage.haskell.org/package/Decimal-0.5.2/Decimal-0.5.2.tar.gz"},"name":"Decimal","pinned":{"url":["https://hackage.haskell.org/package/Decimal-0.5.2/Decimal-0.5.2.tar.gz","https://s3.amazonaws.com/hackage.fpcomplete.com/package/Decimal-0.5.2.tar.gz"],"sha256":"a37a0220424e4bcb8cae1d38844c7027ee314449758d0d14ff3e2e0a5c8a87a7","cabal-sha256":"83dd16a1c0737fd35fdb1088af36e1a53034e75090e3f0d4ad32296f1a35a13b"},"version":"0.5.2"},
Expand Down Expand Up @@ -101,8 +101,8 @@
"generic-deriving": {"dependencies":["base","containers","ghc-prim","template-haskell","th-abstraction"],"location":{"type":"hackage","url":"https://hackage.haskell.org/package/generic-deriving-1.14/generic-deriving-1.14.tar.gz"},"name":"generic-deriving","pinned":{"url":["https://hackage.haskell.org/package/generic-deriving-1.14/generic-deriving-1.14.tar.gz","https://s3.amazonaws.com/hackage.fpcomplete.com/package/generic-deriving-1.14.tar.gz"],"sha256":"d0abd5e423960b66867c6149c20b221b1351e3805d1bf787fc4efa3e7bb7cb02","cabal-sha256":"c9c6782a3cdce2f2bcaf891e0ffbf06df871e68498574b73a565771dc084273c"},"version":"1.14"},
"generics-sop": {"dependencies":["base","ghc-prim","sop-core","template-haskell","th-abstraction"],"location":{"type":"hackage","url":"https://hackage.haskell.org/package/generics-sop-0.5.1.1/generics-sop-0.5.1.1.tar.gz"},"name":"generics-sop","pinned":{"url":["https://hackage.haskell.org/package/generics-sop-0.5.1.1/generics-sop-0.5.1.1.tar.gz","https://s3.amazonaws.com/hackage.fpcomplete.com/package/generics-sop-0.5.1.1.tar.gz"],"sha256":"81b7c38b5c2a1ae3c790b1707a0e2a2031430e33b3683f88e2daa5b59ae4c5d8","cabal-sha256":"522a1a1da05d5acc03da448fa603c038cc15b991272846a591e26e7505d2b73a"},"version":"0.5.1.1"},
"ghc-byteorder": {"dependencies":["base"],"location":{"type":"hackage","url":"https://hackage.haskell.org/package/ghc-byteorder-4.11.0.0.10/ghc-byteorder-4.11.0.0.10.tar.gz"},"name":"ghc-byteorder","pinned":{"url":["https://hackage.haskell.org/package/ghc-byteorder-4.11.0.0.10/ghc-byteorder-4.11.0.0.10.tar.gz","https://s3.amazonaws.com/hackage.fpcomplete.com/package/ghc-byteorder-4.11.0.0.10.tar.gz"],"sha256":"86e50a89798181db4f44ec3848fc52940c73098e88549a351ceb54fefc691fb6","cabal-sha256":"d47fdef63f2a145091e91b1aedbd46bf61ca03bb935e81e3462a1d904c319de3"},"version":"4.11.0.0.10"},
"ghc-lib": {"dependencies":["Win32","alex","array","base","binary","bytestring","containers","deepseq","directory","filepath","ghc-lib-parser","ghc-prim","happy","hpc","pretty","process","rts","time","transformers"],"location":{"type":"archive","url":"https://daml-binaries.da-ext.net/da-ghc-lib/ghc-lib-12d888a292cb63ac2680ce92bad4ae1f.tar.gz"},"name":"ghc-lib","pinned":{"sha256":"88671cc2376affa82f103aaae930e502f30441ca1a94a78fcfd08b6aef77a7d3","strip-prefix":"ghc-lib-8.8.1.20210916"},"version":"8.8.1.20210916"},
"ghc-lib-parser": {"dependencies":["Win32","alex","array","base","binary","bytestring","containers","deepseq","directory","filepath","ghc-prim","happy","hpc","pretty","process","time","transformers"],"location":{"type":"archive","url":"https://daml-binaries.da-ext.net/da-ghc-lib/ghc-lib-parser-12d888a292cb63ac2680ce92bad4ae1f.tar.gz"},"name":"ghc-lib-parser","pinned":{"sha256":"492d50a671bb7d9d826c2e9c77c6d0fdf25030f3f953bbd6f2df92f9ebaceed9","strip-prefix":"ghc-lib-parser-8.8.1.20210916"},"version":"8.8.1.20210916"},
"ghc-lib": {"dependencies":["Win32","alex","array","base","binary","bytestring","containers","deepseq","directory","filepath","ghc-lib-parser","ghc-prim","happy","hpc","pretty","process","rts","time","transformers"],"location":{"type":"archive","url":"https://daml-binaries.da-ext.net/da-ghc-lib/ghc-lib-837a79515c4e70f9440d433ed7833e75.tar.gz"},"name":"ghc-lib","pinned":{"sha256":"b640032ae942c4edc4a5bc6a314701ed5f030e8a16c492281cf3ee5a5ba97a48","strip-prefix":"ghc-lib-8.8.1.20210921"},"version":"8.8.1.20210921"},
"ghc-lib-parser": {"dependencies":["Win32","alex","array","base","binary","bytestring","containers","deepseq","directory","filepath","ghc-prim","happy","hpc","pretty","process","time","transformers"],"location":{"type":"archive","url":"https://daml-binaries.da-ext.net/da-ghc-lib/ghc-lib-parser-837a79515c4e70f9440d433ed7833e75.tar.gz"},"name":"ghc-lib-parser","pinned":{"sha256":"2f70bebd6fa59210258a60ed4450238888dd20e7a06996924b56859cda1ae574","strip-prefix":"ghc-lib-parser-8.8.1.20210921"},"version":"8.8.1.20210921"},
"ghc-lib-parser-ex": {"dependencies":["base","bytestring","containers","extra","ghc-lib-parser","uniplate"],"location":{"type":"hackage","url":"https://hackage.haskell.org/package/ghc-lib-parser-ex-8.8.5.8/ghc-lib-parser-ex-8.8.5.8.tar.gz"},"name":"ghc-lib-parser-ex","pinned":{"url":["https://hackage.haskell.org/package/ghc-lib-parser-ex-8.8.5.8/ghc-lib-parser-ex-8.8.5.8.tar.gz","https://s3.amazonaws.com/hackage.fpcomplete.com/package/ghc-lib-parser-ex-8.8.5.8.tar.gz"],"sha256":"b36ef8b49da4e8c78b00dca9b9546b7d4db0b09b10da5e313d3f0dbb4af581d7","cabal-sha256":"04f164fd6a4a5b0c5627cf7fadd79174c3b1d4c696dc481a9909266bd6a2a6aa"},"version":"8.8.5.8"},
"ghc-prim": {"dependencies":[],"location":{"type":"core"},"name":"ghc-prim","version":"0.6.1"},
"ghcide": {"dependencies":["aeson","async","base","binary","bytestring","containers","data-default","deepseq","dependent-map","dependent-sum","directory","extra","filepath","fuzzy","ghc-lib","ghc-lib-parser","haddock-library","hashable","hslogger","lsp","lsp-types","mtl","network-uri","prettyprinter","prettyprinter-ansi-terminal","regex-tdfa","rope-utf16-splay","safe-exceptions","shake","some","sorted-list","stm","syb","text","time","transformers","unliftio","unordered-containers","utf8-string"],"location":{"type":"vendored"},"name":"ghcide","version":"0.1.0"},
Expand Down

0 comments on commit 4acf34b

Please sign in to comment.