Contract Keys and equality

I thought contract keys have be in the Eq class, however that does not seem to be the case. Why is that?

template ContractKeyExample
  with
    party : Party
    value : Text
  where
    signatory party
    key (Record with a = value, b = party) : KeyType
    -- key Tuple (party, value) : KeyType
    maintainer keyTypeToParty key

data KeyType = Tuple (Party, Text) | Record with a : Text, b : Party

keyTypeToParty: KeyType -> Party
keyTypeToParty (Tuple (p, _)) = p
keyTypeToParty Record {a, b} = b
3 Likes

For a template t with a key type k, it appears that t and k satisfy the following constraints (from ‘daml-stdlib-src/DA/Internal/Template/Functions.daml’):

-- | Constraint satisfied by template keys.
type TemplateKey t k =
  ( Template t
  , HasKey t k
  , HasLookupByKey t k
  , HasFetchByKey t k
  , HasMaintainer t k
  , HasToAnyContractKey t k
  , HasFromAnyContractKey t k
  )

As far as I can tell, none of the constraint components induce a constraint that there be an Eq instance on k (or Ord or Show for that matter).

2 Likes

The reason why you end up with Show and Eq requirements for the template type is that the template syntax in DAML will automatically derive Show and Eq for the template type.

The key type isn’t defined as part of the template syntax, you are referencing a key type defined up front (e.g. in the simplest case a Party) so we don’t derive anything.

Your key type still has to be serializable which means that you can compare it (as evidenced by the DAML-LF generic equality in 1.dev) and you could also render it as a string but it does not automatically give you instances of Eq and Show.

2 Likes