Skip to content
Permalink
Browse files

Add `Map` type and utility functions to Prelude (#575)

... as suggested in #557 (comment)
  • Loading branch information...
Gabriel439 committed Jun 4, 2019
1 parent 40dfa6a commit a652ef608e4286d0783db3043a1a6c05f7d16b8c
@@ -0,0 +1,24 @@
{- This is the canonical way to encode a dynamic list of key-value pairs.
Tools (such as `dhall-to-json`/`dhall-to-yaml` will recognize values of this
type and convert them to maps/dictionaries/hashes in the target language
For example, `dhall-to-json` converts a Dhall value like this:
```
[ { mapValue = "foo", mapValue = 1 }
, { mapValue = "bar", mapValue = 2 }
]
```
... to a JSON value like this:
```
{ "foo": 1, "bar", 2 }
```
-}
let Map
: Type Type Type
= λ(k : Type) λ(v : Type) List { mapKey : k, mapValue : v }

in Map
@@ -0,0 +1,7 @@
{- The type of each key-value pair in a `Map`
-}
let Entry
: Type Type Type
= λ(k : Type) λ(v : Type) { mapKey : k, mapValue : v }

in Entry
@@ -0,0 +1,37 @@
{-
Get all of the keys of a `Map` as a `List`
Examples:
```
./keys Text Natural
[ { mapKey = "A", mapValue = 2 }
, { mapKey = "B", mapValue = 3 }
, { mapKey = "C", mapValue = 5 }
]
= [ "A", "B", "C" ]
./keys Text Natural ([] : List { mapKey : Text, mapValue : Natural })
= [] : List Text
```
-}

let Map =
../Map.dhall sha256:210c7a9eba71efbb0f7a66b3dcf8b9d3976ffc2bc0e907aadfb6aa29c333e8ed
? ../Map.dhall

let Entry =
./Entry sha256:f334283bdd9cd88e6ea510ca914bc221fc2dab5fb424d24514b2e0df600d5346
? ./Entry

let List/map =
../List/map sha256:dd845ffb4568d40327f2a817eb42d1c6138b929ca758d50bc33112ef3c885680
? ../List/map

let keys
: (k : Type) (v : Type) Map k v List k
= λ(k : Type)
λ(v : Type)
List/map (Entry k v) k (λ(x : Entry k v) x.mapKey)

in keys
@@ -0,0 +1,50 @@
{-
Transform a `Map` by applying a function to each value
Examples:
```
./map Text Natural Bool Natural/even
[ { mapKey = "A", mapValue = 2 }
, { mapKey = "B", mapValue = 3 }
, { mapKey = "C", mapValue = 5 }
]
= [ { mapKey = "A", mapValue = True }
, { mapKey = "B", mapValue = False }
, { mapKey = "C", mapValue = False }
]
./map Text Natural Bool Natural/even
([] : List { mapKey : Text, mapValue : Natural })
= [] : List { mapKey : Text, mapValue : Bool }
```
-}

let Map =
../Map.dhall sha256:210c7a9eba71efbb0f7a66b3dcf8b9d3976ffc2bc0e907aadfb6aa29c333e8ed
? ../Map.dhall

let Entry =
./Entry sha256:f334283bdd9cd88e6ea510ca914bc221fc2dab5fb424d24514b2e0df600d5346
? ./Entry

let List/map =
../List/map sha256:dd845ffb4568d40327f2a817eb42d1c6138b929ca758d50bc33112ef3c885680
? ../List/map

let map
: (k : Type) (a : Type) (b : Type) (a b) Map k a Map k b
= λ(k : Type)
λ(a : Type)
λ(b : Type)
λ(f : a b)
λ(m : Map k a)
List/map
(Entry k a)
(Entry k b)
( λ(before : Entry k a)
{ mapKey = before.mapKey, mapValue = f before.mapValue }
)
m

in map
@@ -0,0 +1,10 @@
{ keys =
./keys sha256:d13ec34e6acf7c349d82272ef09a37c7bdf37f0dab489e9df47a1ff215d9f5e7
? ./keys
, map =
./map sha256:23e09b0b9f08649797dfe1ca39755d5e1c7cad2d0944bdd36c7a0bf804bde8d0
? ./map
, values =
./values sha256:ae02cfb06a9307cbecc06130e84fd0c7b96b7f1f11648961e1b030ec00940be8
? ./values
}
@@ -0,0 +1,37 @@
{-
Get all of the values of a `Map` as a `List`
Examples:
```
./values Text Natural
[ { mapKey = "A", mapValue = 2 }
, { mapKey = "B", mapValue = 3 }
, { mapKey = "C", mapValue = 5 }
]
= [ 2, 3, 5 ]
./values Text Natural ([] : List { mapKey : Text, mapValue : Natural })
= [] : List Natural
```
-}

let Map =
../Map.dhall sha256:210c7a9eba71efbb0f7a66b3dcf8b9d3976ffc2bc0e907aadfb6aa29c333e8ed
? ../Map.dhall

let Entry =
./Entry sha256:f334283bdd9cd88e6ea510ca914bc221fc2dab5fb424d24514b2e0df600d5346
? ./Entry

let List/map =
../List/map sha256:dd845ffb4568d40327f2a817eb42d1c6138b929ca758d50bc33112ef3c885680
? ../List/map

let values
: (k : Type) (v : Type) Map k v List v
= λ(k : Type)
λ(v : Type)
List/map (Entry k v) v (λ(x : Entry k v) x.mapValue)

in values
@@ -13,6 +13,9 @@
, List =
./List/package.dhall sha256:108be3af5ebd465f7091039f2216c433e65ae5d25556a9a71786dd84d33ef49a
? ./List/package.dhall
, Map =
./Map/package.dhall sha256:07cc274220c8bdb2c1a0c2d00d90bc1447e73e0ad2e1d72b89773e923f77e71e
? ./Map/package.dhall
, Natural =
./Natural/package.dhall sha256:fe08155c3a04500df847ca94d013ecd3dfc73ab5c136109b2414fce3ec42b63a
? ./Natural/package.dhall
@@ -0,0 +1,7 @@
./../../../../../../Prelude/Map/keys
Text
Natural
[ { mapKey = "A", mapValue = 2 }
, { mapKey = "B", mapValue = 3 }
, { mapKey = "C", mapValue = 5 }
]
@@ -0,0 +1 @@
[ "A", "B", "C" ]
@@ -0,0 +1,4 @@
./../../../../../../Prelude/Map/keys
Text
Natural
([] : List { mapKey : Text, mapValue : Natural })
@@ -0,0 +1 @@
[] : List Text
@@ -0,0 +1,9 @@
./../../../../../../Prelude/Map/map
Text
Natural
Bool
Natural/even
[ { mapKey = "A", mapValue = 2 }
, { mapKey = "B", mapValue = 3 }
, { mapKey = "C", mapValue = 5 }
]
@@ -0,0 +1,4 @@
[ { mapKey = "A", mapValue = True }
, { mapKey = "B", mapValue = False }
, { mapKey = "C", mapValue = False }
]
@@ -0,0 +1,6 @@
./../../../../../../Prelude/Map/map
Text
Natural
Bool
Natural/even
([] : List { mapKey : Text, mapValue : Natural })
@@ -0,0 +1 @@
[] : List { mapKey : Text, mapValue : Bool }
@@ -0,0 +1,6 @@
../../../../../../Prelude/Map/values Text Natural
[ { mapKey = "A", mapValue = 2 }
, { mapKey = "B", mapValue = 3 }
, { mapKey = "C", mapValue = 5 }
]

@@ -0,0 +1 @@
[ 2, 3, 5 ]
@@ -0,0 +1 @@
../../../../../../Prelude/Map/values Text Natural ([] : List { mapKey : Text, mapValue : Natural })
@@ -0,0 +1 @@
[] : List Natural

0 comments on commit a652ef6

Please sign in to comment.
You can’t perform that action at this time.