Record as a Key
- Jean_SafarOP
Hi,
Sorry for the basic question, but I can’t find it in the doc … if I have a data type that i need to be in a Set , is there (like for a template) a way to indicate what attributes compose a Key?
data MyElem = MyElem with foo: Text bar: Text something: Int somethingelse: Party deriving (Eq, Show)How can I have
Set MyElem - bernhard 2
Sets (
DA.Next.Set) are implemented as Maps.Set MyElemis justMap MyElem (). DAML currently only has native maps with text keys (typeTextMap. So theMapandSetimplementations inDA.Next.SetandDA.Next.Maprequire one to write serialization and deserialization code toText.You can use
showas serialization, but then have to write parsing code, which is non-trivial.This is clearly awkward so we have “Generic Maps” and thus also Generic Sets on our roadmap. You can already test Generic Maps as follows:
Set your project to build DAML-LF 1.dev (which will eventually become 1.9) by adding this to your
daml.yaml:build-options: - --target=1.devThen
import DA.Mapin your project, addOrdto the list of typeclasses derived byMyElemand treatMap MyElem ()as your set. TheDA.Setwrapper library doesn’t exist yet, but it would be an easy adaption from daml/Set.daml at a080b47e347277e099167fc59affde9dac313342 · digital-asset/daml · GitHub. - Gary_Verhaegen 1
I feel the need to point out that any dar produced using
--target=1.devis very much not supported for any kind of production use. Even just uploading it to a persistent ledger is likely to break things. This is really just meant for dev; please only upload such dars to throwaway ledgers.While there is a point at which we will create a DAML LF
1.9, it would certainly not be safe to assume1.9will be exactly what1.devis today, nor a strict superset of it. - Luciano 1
bernhard:Then
import DA.Mapin your project, addOrdto the list of typeclasses derived byMyElemand treatMap MyElem ()as your set.So, are you saying that the ordering determines the keys used for hashing, in answer to OP? I expect I’ve misunderstood this, but if not, isn’t this a pretty hefty constraint? Is it not enough to have some sort of a
Hashabletypeclass?Would this mean that in OP’s example, you’d need to write an
instance Ordthat then picks which particular field to order on - i.e. usingderiving Ordin this particular case wouldn’t work?Btw. is
Orda total order? - Gary_Verhaegen 2
I’m not in the loop on the specific work for the new
DA.Map, but I think I can offer some elements of an answer nonetheless.First off, yes, the
Ordtypeclass is intended to represent a total order.deriving Ord, when it works, does that.Regarding the requirement for
Ord, while one could argue it is not, strictly speaking, a requirement to store something in an associative data structure, and aHashabletypeclass could do the trick if the underlying implementation were indeed a hash table, it’s also possible to implement maps (and sets) using binary trees based on ordering the given elements, as is the case for Data.Set in Haskell. Based on @bernhard’s stated requirement to addOrd, I suspect the current prototype forDA.Mapis based on similar techniques. - Leonid_Rozenberg 2
Something like this is what you’re looking for
import DA.Text -- We need a separator character that is not in either of our text fields -- nor a digit -- nor a Party (https://github.com/digital-asset/daml/blob/ba74146c10d149687d7f42972a9ced6203c3f807/daml-lf/spec/daml-lf-1.rst#literals) sep : Text sep = ";" instance M.MapKey MyElem where keyToText c = c.foo <> sep <> c.bar <> sep <> M.keyToText c.something <> sep <> M.keyToText c.somethingelse keyFromText t = case splitOn sep t of [ foo, bar, somethingT, somethingelseT] -> MyElem with foo bar something = M.keyFromText somethingT somethingelse = M.keyFromText somethingelseT _ -> error $ "Malformed key" <> t - Gary_Verhaegen 2
Note that this "
;can never be used in a string ever" is likely to bite you in the future. If adding anOrdinstance does not work for you, and you really need to support translation to and from a text format, I’d recommend implementing a simple string-based replacement à la htmlencode: on the way in, replace;with&semicolonand&with&, and do the opposite on the way out. Then you know there is no;in the encoded strings and you can safely use that as a delimiter. - Leonid_Rozenberg 1
+1 to Gary’s suggestion!
- cocreature
Let me add some clarification around the upcoming generic map type:
Together with the generic Map type, DAML-LF has gained a builtin generic comparison function. This works for roughly anything that is serializable (not quite accurate but a decent approximation). For incomparable values, e.g., functions, this throws a runtime error.
For DAML, we want to prevent invalid values statically. Currently, this is accomplished by requiring an
Ordinstance. However, theOrdinstance will not actually be used by the map. Instead it will use DAML-LF’s builtin notion of equality. Since nothing stops you from defining anOrdinstance for a record containing a function, you can technically still produce something that crashes.Having two notions of ordering is rather confusing so there is an open question here if it would be better to forbid user-defined
Ordinstances to make sure thatOrdinstances always match up with the builtin notion of equality which is also much faster.