Interface methods can use plain function declaration syntax (#13102)

* Update 'implements' blocks to use new syntax

* Add InterfaceSyntax test case

* Add InterfaceMultipleMethodDeclsError test case

* Add InterfaceDifferentNumArgsError test case

changelog_begin
changelog_end
This commit is contained in:
Moisés Ackerman 2022-03-02 16:59:01 +01:00 committed by GitHub
parent 7acc6a9009
commit 2dbd91aab9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 269 additions and 180 deletions

View File

@ -17,11 +17,11 @@ load("@os_info//:os_info.bzl", "is_linux", "is_windows")
load("@dadew//:dadew.bzl", "dadew_tool_home")
load("@rules_haskell//haskell:cabal.bzl", "stack_snapshot")
GHC_LIB_REV = "00c5083b2ce26066cad19364d6d0c1fe"
GHC_LIB_SHA256 = "6d6a92aa0de6287f33a2947951a3dd6b448d85f866afb7ae42602d8264f3791e"
GHC_LIB_REV = "da07ac689bc661fe22e12f60692a18b8"
GHC_LIB_SHA256 = "14da7208388eaf47a351c1fe8272ffed5dd7f109d9c76d26cd56fdc7658560af"
GHC_LIB_VERSION = "8.8.1"
GHC_LIB_PARSER_REV = "00c5083b2ce26066cad19364d6d0c1fe"
GHC_LIB_PARSER_SHA256 = "515cddda730b39aa98809874105a1de967c8dcf482495df7b43c153394cd275d"
GHC_LIB_PARSER_REV = "da07ac689bc661fe22e12f60692a18b8"
GHC_LIB_PARSER_SHA256 = "5cd22bb40e3acfbe5104000883e7231664d778ff9708b30763134fe1f881e3e5"
GHC_LIB_PARSER_VERSION = "8.8.1"
GHCIDE_REV = "0572146d4b792c6c67affe461e0bd07d49d9df72"
GHCIDE_SHA256 = "7de56b15d08eab19d325a93c4f43d0ca3d634bb1a1fdc0d18fe4ab4a021cc697"

View File

@ -12,7 +12,7 @@ jobs:
variables:
ghc-lib-sha: '905f51296d979d79da511bed9ab2da7cb9429c9f'
base-sha: '9c787d4d24f2b515934c8503ee2bbd7cfac4da20'
patches: '1c14a526ce9143d59269c89f23bab2554fd513dc 833ca63be2ab14871874ccb6974921e8952802e9'
patches: '54cc6dfaa7151cd89a8e9d9fb573035ef4cf6ef3 833ca63be2ab14871874ccb6974921e8952802e9'
flavor: 'da-ghc-8.8.1'
steps:
- checkout: self

View File

@ -194,7 +194,7 @@ unitTests =
, " where"
, " signatory field1"
, " implements Bar where"
, " let method = pure ()"
, " method = pure ()"
]
(\md -> assertBool
("Expected interface implementation, got " <> show md)

View File

@ -56,25 +56,23 @@ template Asset
where
signatory issuer, owner
implements Token where
let getOwner = owner
let getAmount = amount
let setAmount = \x -> toInterface @Token (this with amount = x)
-- TODO https://github.com/digital-asset/daml/issues/12051
-- (maybe) support `let setAmount x = ...` syntax.
getOwner = owner
getAmount = amount
setAmount x = toInterface @Token (this with amount = x)
let splitImpl = \splitAmount -> do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Token cid1, toInterfaceContractId @Token cid2)
splitImpl splitAmount = do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Token cid1, toInterfaceContractId @Token cid2)
let transferImpl = \newOwner -> do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token cid)
transferImpl newOwner = do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token cid)
let noopImpl = \nothing -> do
[1] === [1] -- make sure `mkMethod` calls are properly erased in the presence of polymorphism.
pure ()
noopImpl nothing = do
[1] === [1] -- make sure `mkMethod` calls are properly erased in the presence of polymorphism.
pure ()
main = scenario do
p <- getParty "Alice"

View File

@ -37,6 +37,6 @@ template T with
where
signatory owner
implements InterfaceA where
let getOwnerA = owner
getOwnerA = owner
implements InterfaceB where
let getOwnerB = owner
getOwnerB = owner

View File

@ -0,0 +1,25 @@
-- Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
-- @SINCE-LF-FEATURE DAML_INTERFACE
-- @ENABLE-SCENARIOS
-- @ERROR range=23:7-25:24; Equations for method m0 in template T implementation of interface I have different numbers of arguments
module InterfaceDifferentNumArgsError where
interface I where
m0 : Bool -> Bool -> Party
template T
with
p0 : Party
p1 : Party
p2 : Party
f : Int
where
signatory p0, p1
implements I where
m0 False = const p0
m0 True False = p1
m0 True True = p2

View File

@ -34,12 +34,12 @@ template Asset
signatory issuer
observer owner
implements Token where
let getOwner = owner
let getIssuer = issuer
let getAmount = amount
let transferImpl = \newOwner -> do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token cid)
getOwner = owner
getIssuer = issuer
getAmount = amount
transferImpl newOwner = do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token cid)
main = scenario do
alice <- getParty "Alice"

View File

@ -19,7 +19,7 @@ template Asset
signatory issuer
observer issuer, owner
implements Token where
let getAmount = amount
getAmount = amount
main = scenario do
alice <- getParty "Alice"

View File

@ -31,9 +31,9 @@ template Asset
signatory owner
implements Token where
let getOwner = owner
let getAmount = amount
let setAmount = \x -> toInterface @Token (this with amount = x)
getOwner = owner
getAmount = amount
setAmount x = toInterface @Token (this with amount = x)
exception GuardException
with

View File

@ -0,0 +1,27 @@
-- Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
-- @SINCE-LF-FEATURE DAML_INTERFACE
-- @ENABLE-SCENARIOS
-- @ERROR range=24:7-24:20; Multiple declarations of method m0 in template T implementation of interface I
module InterfaceMultipleMethodDeclsError where
interface I where
m0 : Bool -> Party
m1 : Text
template T
with
p0 : Party
p1 : Party
f : Int
where
signatory p0, p1
implements I where
-- first m0 declaration
m0 False = p0
m1 = "m1"
-- second m0 declaration
m0 True = p1

View File

@ -58,23 +58,23 @@ template Asset
ensure (amount >= 5 && amount <= 8)
implements Token1 where
let getOwner1 = owner
let getAmount1 = amount
let splitImpl = \splitAmount -> do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Token1 cid1, toInterfaceContractId @Token1 cid2)
let transferImpl = \newOwner -> do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token1 cid)
getOwner1 = owner
getAmount1 = amount
splitImpl splitAmount = do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Token1 cid1, toInterfaceContractId @Token1 cid2)
transferImpl newOwner = do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token1 cid)
implements Token2 where
let getOwner2 = owner
let getAmount2 = amount
let noopImpl = \nothing -> do
pure ()
getOwner2 = owner
getAmount2 = amount
noopImpl nothing = do
pure ()
-- Same as Asset, but the precondition is different and it implements Token3.
template Asset2
@ -88,25 +88,25 @@ template Asset2
ensure (amount > 10)
implements Token1 where
let getOwner1 = owner
let getAmount1 = amount
let splitImpl = \splitAmount -> do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Token1 cid1, toInterfaceContractId @Token1 cid2)
let transferImpl = \newOwner -> do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token1 cid)
getOwner1 = owner
getAmount1 = amount
splitImpl splitAmount = do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Token1 cid1, toInterfaceContractId @Token1 cid2)
transferImpl newOwner = do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token1 cid)
implements Token2 where
let getOwner2 = owner
let getAmount2 = amount
let noopImpl = \nothing -> do
pure ()
getOwner2 = owner
getAmount2 = amount
noopImpl nothing = do
pure ()
implements Token3 where
let getMessage = "boom tail"
getMessage = "boom tail"
template Asset3
with
@ -119,25 +119,25 @@ template Asset3
ensure (amount > 10)
implements Token1 where
let getOwner1 = owner
let getAmount1 = amount
let splitImpl = \splitAmount -> do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Token1 cid1, toInterfaceContractId @Token1 cid2)
let transferImpl = \newOwner -> do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token1 cid)
getOwner1 = owner
getAmount1 = amount
splitImpl splitAmount = do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Token1 cid1, toInterfaceContractId @Token1 cid2)
transferImpl newOwner = do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token1 cid)
implements Token3 where
let getMessage = "boom middle"
getMessage = "boom middle"
implements Token2 where
let getOwner2 = owner
let getAmount2 = amount
let noopImpl = \nothing -> do
pure ()
getOwner2 = owner
getAmount2 = amount
noopImpl nothing = do
pure ()
template Asset4
with
@ -150,26 +150,26 @@ template Asset4
ensure (amount > 10)
implements Token3 where
let getMessage = "boom head"
getMessage = "boom head"
implements Token1 where
let getOwner1 = owner
let getAmount1 = amount
let splitImpl = \splitAmount -> do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Token1 cid1, toInterfaceContractId @Token1 cid2)
let transferImpl = \newOwner -> do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token1 cid)
getOwner1 = owner
getAmount1 = amount
splitImpl splitAmount = do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Token1 cid1, toInterfaceContractId @Token1 cid2)
transferImpl newOwner = do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token1 cid)
implements Token2 where
let getOwner2 = owner
let getAmount2 = amount
let noopImpl = \nothing -> do
pure ()
getOwner2 = owner
getAmount2 = amount
noopImpl nothing = do
pure ()
main = scenario do
p <- getParty "Alice"

View File

@ -0,0 +1,41 @@
-- Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
-- @SINCE-LF-FEATURE DAML_INTERFACE
-- @ENABLE-SCENARIOS
module InterfaceSyntax where
interface I where
m0 : Bool -> Party
m1 : Either Int Bool -> Text
-- empty interfaces with 'where'
interface J where
interface K where
template T
with
p0 : Party
p1 : Party
f : Int
where
signatory p0, p1
implements I where
-- method defined over multiple clauses with pattern matches
m0 False = p0
m0 True = p1
-- method defined over multiple clauses with pattern matches and guards
m1 (Left n)
| n >= 0 = "non-negative number"
| otherwise = "negative number"
m1 (Right True) = "true"
m1 (Right False) = "false"
-- empty implements block without 'where'
implements J
-- empty implements block with 'where'
implements K where

View File

@ -20,7 +20,7 @@ template T1
where
signatory owner1
implements I where
let getOwner = owner1
getOwner = owner1
template T2
with
@ -28,7 +28,7 @@ template T2
where
signatory owner2
implements I where
let getOwner = owner2
getOwner = owner2
main = scenario do
p <- getParty "Alice"

View File

@ -26,9 +26,9 @@ template T1
where
signatory p1
implements A where
let getOwner = p1
getOwner = p1
implements B where
let getCoolness = 20
getCoolness = 20
template T2
with
@ -36,7 +36,7 @@ template T2
where
signatory p2
implements A where
let getOwner = p2
getOwner = p2
main = scenario do
p <- getParty "Alice"

View File

@ -16,25 +16,23 @@ template Asset
where
signatory issuer, owner
implements Interface.Token where
let getOwner = owner
let getAmount = amount
let setAmount = \x -> toInterface @Interface.Token (this with amount = x)
-- TODO https://github.com/digital-asset/daml/issues/12051
-- (maybe) support `let setAmount x = ...` syntax.
getOwner = owner
getAmount = amount
setAmount x = toInterface @Interface.Token (this with amount = x)
let splitImpl = \splitAmount -> do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Interface.Token cid1, toInterfaceContractId @Interface.Token cid2)
splitImpl splitAmount = do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Interface.Token cid1, toInterfaceContractId @Interface.Token cid2)
let transferImpl = \newOwner -> do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Interface.Token cid)
transferImpl newOwner = do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Interface.Token cid)
let noopImpl = \nothing -> do
[1] === [1] -- make sure `mkMethod` calls are properly erased in the presence of polymorphism.
pure ()
noopImpl nothing = do
[1] === [1] -- make sure `mkMethod` calls are properly erased in the presence of polymorphism.
pure ()
main = scenario do
p <- getParty "Alice"

View File

@ -1580,23 +1580,23 @@ tests tools@Tools{damlc,validate,oldProjDar} = testGroup "Data Dependencies" $
, " where"
, " signatory issuer, owner"
, " implements Token where"
, " let getOwner = owner"
, " let getAmount = amount"
, " let setAmount = \\x -> toInterface @Token (this with amount = x)"
, " getOwner = owner"
, " getAmount = amount"
, " setAmount x = toInterface @Token (this with amount = x)"
, " let splitImpl = \\splitAmount -> do"
, " assert (splitAmount < amount)"
, " cid1 <- create this with amount = splitAmount"
, " cid2 <- create this with amount = amount - splitAmount"
, " pure (toInterfaceContractId @Token cid1, toInterfaceContractId @Token cid2)"
, " splitImpl splitAmount = do"
, " assert (splitAmount < amount)"
, " cid1 <- create this with amount = splitAmount"
, " cid2 <- create this with amount = amount - splitAmount"
, " pure (toInterfaceContractId @Token cid1, toInterfaceContractId @Token cid2)"
, " let transferImpl = \\newOwner -> do"
, " cid <- create this with owner = newOwner"
, " pure (toInterfaceContractId @Token cid)"
, " transferImpl newOwner = do"
, " cid <- create this with owner = newOwner"
, " pure (toInterfaceContractId @Token cid)"
, " let noopImpl = \\nothing -> do"
, " [1] === [1] -- make sure `mkMethod` calls are properly erased in the presence of polymorphism."
, " pure ()"
, " noopImpl nothing = do"
, " [1] === [1] -- make sure `mkMethod` calls are properly erased in the presence of polymorphism."
, " pure ()"
, "main = scenario do"
, " p <- getParty \"Alice\""
@ -1694,23 +1694,23 @@ tests tools@Tools{damlc,validate,oldProjDar} = testGroup "Data Dependencies" $
, " where"
, " signatory issuer, owner"
, " implements Token where"
, " let getOwner = owner"
, " let getAmount = amount"
, " let setAmount = \\x -> toInterface @Token (this with amount = x)"
, " getOwner = owner"
, " getAmount = amount"
, " setAmount x = toInterface @Token (this with amount = x)"
, " let splitImpl = \\splitAmount -> do"
, " assert (splitAmount < amount)"
, " cid1 <- create this with amount = splitAmount"
, " cid2 <- create this with amount = amount - splitAmount"
, " pure (toInterfaceContractId @Token cid1, toInterfaceContractId @Token cid2)"
, " splitImpl splitAmount = do"
, " assert (splitAmount < amount)"
, " cid1 <- create this with amount = splitAmount"
, " cid2 <- create this with amount = amount - splitAmount"
, " pure (toInterfaceContractId @Token cid1, toInterfaceContractId @Token cid2)"
, " let transferImpl = \\newOwner -> do"
, " cid <- create this with owner = newOwner"
, " pure (toInterfaceContractId @Token cid)"
, " transferImpl newOwner = do"
, " cid <- create this with owner = newOwner"
, " pure (toInterfaceContractId @Token cid)"
, " let noopImpl = \\nothing -> do"
, " [1] === [1] -- make sure `mkMethod` calls are properly erased in the presence of polymorphism."
, " pure ()"
, " noopImpl nothing = do"
, " [1] === [1] -- make sure `mkMethod` calls are properly erased in the presence of polymorphism."
, " pure ()"
]
]
[

View File

@ -291,15 +291,15 @@ testsForDamlcTest damlc scriptDar script1DevDar = testGroup "damlc test" $
, "template S with p: Party where"
, " signatory p"
, " implements I where"
, " let iGetParty = p"
, " iGetParty = p"
, " implements J where"
, " let jGetParty = p"
, " jGetParty = p"
, "template T with p: Party where"
, " signatory p"
, " implements I where"
, " let iGetParty = p"
, " iGetParty = p"
, " implements J where"
, " let jGetParty = p"
, " jGetParty = p"
, "x = script do"
, " alice <- allocateParty \"Alice\""

View File

@ -21,7 +21,7 @@ template T1
where
signatory owner1
implements I1 where
let getOwner1 = owner1
getOwner1 = owner1
choice OwnChoice : ()
controller owner1
do pure ()
@ -32,6 +32,6 @@ template T2
where
signatory owner2
implements I1 where
let getOwner1 = owner2
getOwner1 = owner2
implements I2 where
let getOwner2 = owner2
getOwner2 = owner2

View File

@ -43,21 +43,21 @@ template Asset
where
signatory issuer, owner
implements Token where
let getOwner = owner
let getAmount = amount
getOwner = owner
getAmount = amount
let splitImpl = \splitAmount -> do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Token cid1, toInterfaceContractId @Token cid2)
splitImpl splitAmount = do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Token cid1, toInterfaceContractId @Token cid2)
let transferImpl = \newOwner -> do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token cid)
transferImpl newOwner = do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token cid)
let noopImpl = \nothing -> do
pure ()
noopImpl nothing = do
pure ()
test : Script ()
test = script do

View File

@ -196,11 +196,11 @@ template Asset with
signatory issuer
observer owner
implements Token where
let getTokenOwner = owner
let transferImpl = \newOwner -> do
cid <- create Asset with issuer = issuer, owner = newOwner
pure (toInterfaceContractId @Token cid)
getTokenOwner = owner
transferImpl newOwner = do
cid <- create Asset with issuer = issuer, owner = newOwner
pure (toInterfaceContractId @Token cid)
implements Other where
let getOtherOwner = owner
let somethingImpl = do
pure ()
getOtherOwner = owner
somethingImpl = do
pure ()

View File

@ -61,18 +61,18 @@ template Asset
where
signatory issuer
implements Token where
let getOwner = owner
let getAmount = amount
getOwner = owner
getAmount = amount
let splitImpl = \splitAmount -> do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Token cid1, toInterfaceContractId @Token cid2)
splitImpl splitAmount = do
assert (splitAmount < amount)
cid1 <- create this with amount = splitAmount
cid2 <- create this with amount = amount - splitAmount
pure (toInterfaceContractId @Token cid1, toInterfaceContractId @Token cid2)
let transferImpl = \newOwner -> do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token cid)
transferImpl newOwner = do
cid <- create this with owner = newOwner
pure (toInterfaceContractId @Token cid)
let noopImpl = \nothing -> do
pure ()
noopImpl nothing = do
pure ()