New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discuss: Homogeneous maps #557
Comments
Is it worth sketching some use cases for this feature, to make it clear what we’re designing for? I guess anywhere that someone uses dhall-json’s mapkey/mapvalue thing. I can certainly imagine using this for generating terraform json. Something like:
Where we are using a homogeneous map to define multiple |
I'm honestly more interested in more ergonomics / best practises around association lists than a whole other Map type and syntax.
|
I am curious how far this can go without a foldWithKey/toList primitive. Not saying it's a must, just something for me to think about |
I'm not clear with what the use-case for this is. If there is no support for accessing values by key, then what added value is there in using a |
@ari-becker @philandstuff: It's mainly for ergonomics, but if people don't strongly desire this feature then I can hold off on it. The context for this was that I was expanding the page on design choices to explain why the language couldn't add support for homogeneous maps, but then I realized that it could. The main use case I can think of for this is nicer syntax when generating types that are translated to JSON or YAML |
I'm also having doubts about use-cases for this feature. If this is the goal then it seems to me that the |
The main issue I currently have with association lists in Dhall is that they cannot be generically marshalled as maps in an implementation. dhall-to-json currently solves this by having a command line argument that allows specifying the record selector for keys. I think it might be useful to solve this in a more reusable way. I can think of three ways to do this:
|
@singpolyma if I read your comment correctly I'll note that right now we're doing 2. |
I'll note that right now we're doing 2. I.e. the defaults in `dhall-json` for associative lists are `mapKey`/`mapValue` (we settled on this because `key` is too common in business domains) and they are a "best practice" and used everywhere we need associative lists.
I would argue that a dhall-to-json feature not mentioned in dhall-lang hardly counts as a "best practise" though it may indeed form the basis for one.
There *is* an associative list defined in the standard, for import headers, and it uses the selector "header" for the key.
|
@singpolyma you most likely missed my edit to the message above, but it's also used in the Prelude so I'd consider this as a "mention" in dhall-lang |
One thing we could do is add a let Map : Type → Type = λ(a : Type) → List { mapKey : Text, mapValue : a }
in Map |
> let Map : Type → Type = λ(a : Type) → List { mapKey : Text, mapValue : a }
I think this could be good, though I would take a key type as well (to allow for symbol keys like <prod|test> for example)
What do people think about moving the import headers to be this kind of map?
|
@singpolyma: One other thing: if we allow parametrizing on the key-type, then we have the option of using a record to store the type parameters instead of passing them positionally, like this: let Map
: { key : Type, value : Type } → Type
= λ(type : { key : Type, value : Type })
→ List { key : type.key, value : type.value }
in Map ... that way you can write: let Headers = Map { key = Text, value = Text }
in … This takes advantage of Dhall's support for type-level records. |
I don't think I feel strongly on this vs the curried version, but others may.
|
Thinking about it more, it's probably better to curry, if only so that it's easier to partially apply, which will be a common use case |
I ran into a minor issue when attempting to add the above |
|
@Nadrieril: If we add a |
... as suggested in #557 (comment)
Pull request for adding |
... as suggested in #557 (comment)
I think this is done now. Closing (feel free to reopen if I’m wrong) |
This is an informal sketch of an idea to add homogeneous maps to the language.
Here is the idea:
Add a
{{ ... }}
syntax for authoring a homogeneous mapIt could be any syntax. I'm not married to that particular choice, so feel free to suggest
alternative syntax ideas
Add a built-in
Map : Type → Type
constructorExample:
Add two built-in functions
Map/length : ∀(a : Type) → Map a → Natural
Map/map : ∀(a : Type) → ∀(b : Type) → (a -> b) -> Map a -> Map b
Carefully note here that:
Keys are not computed
Specifically, map keys cannot be derived from
Text
valuesThe
Map
type is not parametrized on the key typeIn other words, the key type is essentially opaque. You could pretend that the keys are "symbol" or "atom"s (to use terminology from other languages)
These latter two constraints mean that this proposed support for homogeneous maps could not be used as a backdoor for comparing
Text
values for equality.Notably, there would probably not be support for accessing values by key (i.e. no
someMap.key
support) since in that case the user more likely wanted a record instead of aMap
.We also add
Set
s in the same way if theSet
type were not parametrized (i.e. it could only store sets of field names):... but for now I'm limiting the proposal to just homogeneous maps.
I'm making this a separate issue from #234 which is more about how to make it easier to author association lists, whereas this is more about how to replace association lists with true homogeneous maps. This would not obsolete the other issue because users would probably still want a way to convert homogeneous records to homogeneous maps.
The text was updated successfully, but these errors were encountered: