Skip to content

Commit

Permalink
Add Eq instances for AnyTemplate, AnyChoice and AnyContractKey (digit…
Browse files Browse the repository at this point in the history
…al-asset#3816)

* Add Eq instances for AnyTemplate, AnyChoice and AnyContractKey

CHANGELOG_BEGIN

- [DAML Standard Library] Add ``Eq`` instances for ``AnyTemplate``, ``AnyChoice`` and ``AnyContractKey``.

CHANGELOG_END

* Add DAML_ANY_TYPE to the CPP guard
  • Loading branch information
cocreature authored and mergify[bot] committed Dec 11, 2019
1 parent ac47a84 commit 013d668
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
22 changes: 22 additions & 0 deletions compiler/damlc/daml-stdlib-src/DA/Internal/LF.daml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,28 @@ data AnyContractKey = AnyContractKey
, getanyContractKeyTemplateRep : TemplateTypeRep
}

#ifdef DAML_GENMAP && DAML_ANY_TYPE
-- We do not separate generic equality from generic maps so this guards on DAML_GENMAP.

-- We do not have a general Eq instance for Any since Any is not exposed
-- to users directly and that instance will crash on some values but
-- we can have total Eq instances for AnyTemplate, AnyChoice and AnyContractKey
-- since those are guaranteed to be serializable.
eqAny : Any -> Any -> Bool
eqAny = primitive @"BEEqual"

instance Eq AnyTemplate where
AnyTemplate a == AnyTemplate b = eqAny a b

instance Eq AnyChoice where
AnyChoice aAny aRep == AnyChoice bAny bRep =
eqAny aAny bAny && aRep == bRep

instance Eq AnyContractKey where
AnyContractKey aAny aRep == AnyContractKey bAny bRep =
eqAny aAny bAny && aRep == bRep
#endif

-- | Value-level representation of a type.
-- We do not expose this directly and instead only expose TemplateTypeRep.
data TypeRep = TypeRep Opaque
Expand Down
58 changes: 58 additions & 0 deletions compiler/damlc/tests/daml-test-files/AnyEq.daml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
-- Copyright (c) 2019, Digital Asset (Switzerland) GmbH and/or its affiliates.
-- All rights reserved.

-- @SINCE-LF 1.8
daml 1.2
module AnyEq where

import DA.Action

template T1
with
x : Int
p : Party
where
signatory p
key p : Party
maintainer key
choice C : ()
controller p
do pure ()

template T1'
with
x : Int
p : Party
where
signatory p
key p : Party
maintainer key

template T2
with
y : Text
p : Party
where
signatory p
key (p, y) : (Party, Text)
maintainer key._1

-- We don’t have a Show instance for AnyTemplate so we cannot use ===

assertBool : CanAbort m => Text -> Bool -> m ()
assertBool err success = unless success $ abort ("Failure: " <> show err)

main = scenario do
p <- getParty "alice"
let t1A = T1 0 p
let t1B = T1 1 p
let t1'A = T1' 0 p
assertBool (show (t1A, t1A)) (toAnyTemplate t1A == toAnyTemplate t1A)
assertBool (show (t1A, t1B)) (toAnyTemplate t1A /= toAnyTemplate t1B)
assertBool (show (t1'A, t1'A)) (toAnyTemplate t1'A == toAnyTemplate t1'A)
assertBool (show (t1A, t1'A)) (toAnyTemplate t1A /= toAnyTemplate t1'A)
assertBool (show (C, C)) (toAnyChoice @T1 C == toAnyChoice @T1 C)
assertBool (show (C, Archive)) (toAnyChoice @T1 C /= toAnyChoice @T1 Archive)
assertBool ("T1, T2" <> show (Archive, Archive)) (toAnyChoice @T1 C /= toAnyChoice @T2 Archive)
assertBool ("T1, T1'" <> show (p, p)) (toAnyContractKey @T1 p /= toAnyContractKey @T1' p)
assertBool ("T1, T1" <> show (p, p)) (toAnyContractKey @T1 p == toAnyContractKey @T1 p)

0 comments on commit 013d668

Please sign in to comment.