Skip to content

Commit

Permalink
Add Map type and utility functions to Prelude (#575)
Browse files Browse the repository at this point in the history
... as suggested in #557 (comment)
  • Loading branch information
Gabriella439 committed Jun 4, 2019
1 parent 40dfa6a commit a652ef6
Show file tree
Hide file tree
Showing 19 changed files with 210 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Prelude/Map.dhall
@@ -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
7 changes: 7 additions & 0 deletions Prelude/Map/Entry
@@ -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
37 changes: 37 additions & 0 deletions Prelude/Map/keys
@@ -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
50 changes: 50 additions & 0 deletions Prelude/Map/map
@@ -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
10 changes: 10 additions & 0 deletions Prelude/Map/package.dhall
@@ -0,0 +1,10 @@
{ keys =
./keys sha256:d13ec34e6acf7c349d82272ef09a37c7bdf37f0dab489e9df47a1ff215d9f5e7
? ./keys
, map =
./map sha256:23e09b0b9f08649797dfe1ca39755d5e1c7cad2d0944bdd36c7a0bf804bde8d0
? ./map
, values =
./values sha256:ae02cfb06a9307cbecc06130e84fd0c7b96b7f1f11648961e1b030ec00940be8
? ./values
}
37 changes: 37 additions & 0 deletions Prelude/Map/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
3 changes: 3 additions & 0 deletions Prelude/package.dhall
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/normalization/success/prelude/Map/keys/0A.dhall
@@ -0,0 +1,7 @@
./../../../../../../Prelude/Map/keys
Text
Natural
[ { mapKey = "A", mapValue = 2 }
, { mapKey = "B", mapValue = 3 }
, { mapKey = "C", mapValue = 5 }
]
1 change: 1 addition & 0 deletions tests/normalization/success/prelude/Map/keys/0B.dhall
@@ -0,0 +1 @@
[ "A", "B", "C" ]
4 changes: 4 additions & 0 deletions tests/normalization/success/prelude/Map/keys/1A.dhall
@@ -0,0 +1,4 @@
./../../../../../../Prelude/Map/keys
Text
Natural
([] : List { mapKey : Text, mapValue : Natural })
1 change: 1 addition & 0 deletions tests/normalization/success/prelude/Map/keys/1B.dhall
@@ -0,0 +1 @@
[] : List Text
9 changes: 9 additions & 0 deletions tests/normalization/success/prelude/Map/map/0A.dhall
@@ -0,0 +1,9 @@
./../../../../../../Prelude/Map/map
Text
Natural
Bool
Natural/even
[ { mapKey = "A", mapValue = 2 }
, { mapKey = "B", mapValue = 3 }
, { mapKey = "C", mapValue = 5 }
]
4 changes: 4 additions & 0 deletions tests/normalization/success/prelude/Map/map/0B.dhall
@@ -0,0 +1,4 @@
[ { mapKey = "A", mapValue = True }
, { mapKey = "B", mapValue = False }
, { mapKey = "C", mapValue = False }
]
6 changes: 6 additions & 0 deletions tests/normalization/success/prelude/Map/map/1A.dhall
@@ -0,0 +1,6 @@
./../../../../../../Prelude/Map/map
Text
Natural
Bool
Natural/even
([] : List { mapKey : Text, mapValue : Natural })
1 change: 1 addition & 0 deletions tests/normalization/success/prelude/Map/map/1B.dhall
@@ -0,0 +1 @@
[] : List { mapKey : Text, mapValue : Bool }
6 changes: 6 additions & 0 deletions tests/normalization/success/prelude/Map/values/0A.dhall
@@ -0,0 +1,6 @@
../../../../../../Prelude/Map/values Text Natural
[ { mapKey = "A", mapValue = 2 }
, { mapKey = "B", mapValue = 3 }
, { mapKey = "C", mapValue = 5 }
]

1 change: 1 addition & 0 deletions tests/normalization/success/prelude/Map/values/0B.dhall
@@ -0,0 +1 @@
[ 2, 3, 5 ]
1 change: 1 addition & 0 deletions tests/normalization/success/prelude/Map/values/1A.dhall
@@ -0,0 +1 @@
../../../../../../Prelude/Map/values Text Natural ([] : List { mapKey : Text, mapValue : Natural })
1 change: 1 addition & 0 deletions tests/normalization/success/prelude/Map/values/1B.dhall
@@ -0,0 +1 @@
[] : List Natural

0 comments on commit a652ef6

Please sign in to comment.