Import Map/

This commit is contained in:
Charlotte 🦝 Delenk 2022-09-01 19:55:52 +01:00
parent c74b46c3e8
commit 182738f0fb
Signed by: darkkirb
GPG key ID: AB2BD8DAF2E37122
13 changed files with 240 additions and 0 deletions

17
Location/Type.dhall Normal file
View file

@ -0,0 +1,17 @@
--| This is the union type returned when you import something `as Location`
let Location
: Type
= < Environment : Text | Local : Text | Missing | Remote : Text >
let example0 =
assert
: missing
sha256:f428188ff9d77ea15bc2bcd0da3f8ed81b304e175b07ade42a3b0fb02941b2aa as Location
? missing as Location
≡ < Environment : Text
| Local : Text
| Missing
| Remote : Text
>.Missing
in Location

1
Location/package.dhall Normal file
View file

@ -0,0 +1 @@
{ Type = ./Type.dhall }

6
Map/Entry.dhall Normal file
View file

@ -0,0 +1,6 @@
--| 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

25
Map/Type.dhall Normal file
View file

@ -0,0 +1,25 @@
{-|
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:
```
[ { mapKey = "foo", mapValue = 1 }
, { mapKey = "bar", mapValue = 2 }
] : ./Map Text Natural
```
... 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

11
Map/empty.dhall Normal file
View file

@ -0,0 +1,11 @@
--| An empty `Map` of the given key and value types
let Map = ./Type.dhall
let empty
: ∀(k : Type) → ∀(v : Type) → Map k v
= λ(k : Type) → λ(v : Type) → [] : Map k v
let example0 =
assert : empty Text Bool ≡ ([] : List { mapKey : Text, mapValue : Bool })
in empty

15
Map/keyText.dhall Normal file
View file

@ -0,0 +1,15 @@
{-|
Builds a key-value record such that a `List` of them will be converted to a
homogeneous record by dhall-to-json and dhall-to-yaml.
Both key and value are fixed to `Text`.
Take a look at `./keyValue` for a polymorphic version.
-}
let keyText =
λ(key : Text) → λ(value : Text) → { mapKey = key, mapValue = value }
let example0 =
assert : keyText "foo" "bar" ≡ { mapKey = "foo", mapValue = "bar" }
in keyText

17
Map/keyValue.dhall Normal file
View file

@ -0,0 +1,17 @@
{-|
Builds a key-value record such that a List of them will be converted to a
homogeneous record by dhall-to-json and dhall-to-yaml.
-}
let keyValue =
λ(v : Type) →
λ(key : Text) →
λ(value : v) →
{ mapKey = key, mapValue = value }
let example0 =
assert : keyValue Natural "foo" 2 ≡ { mapKey = "foo", mapValue = 2 }
let example1 =
assert : keyValue Text "bar" "baz" ≡ { mapKey = "bar", mapValue = "baz" }
in keyValue

30
Map/keys.dhall Normal file
View file

@ -0,0 +1,30 @@
--| Get all of the keys of a `Map` as a `List`
let Map = ./Type.dhall
let Entry = ./Entry.dhall
let List/map = ../List/map.dhall
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)
let example0 =
assert
: keys
Text
Natural
[ { mapKey = "A", mapValue = 2 }
, { mapKey = "B", mapValue = 3 }
, { mapKey = "C", mapValue = 5 }
]
≡ [ "A", "B", "C" ]
let example1 =
assert
: keys Text Natural ([] : List { mapKey : Text, mapValue : Natural })
≡ ([] : List Text)
in keys

49
Map/map.dhall Normal file
View file

@ -0,0 +1,49 @@
--| Transform a `Map` by applying a function to each value
let Map = ./Type.dhall
let Entry = ./Entry.dhall
let List/map = ../List/map.dhall
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
let example0 =
assert
: 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 }
]
let example1 =
assert
: map
Text
Natural
Bool
Natural/even
([] : List { mapKey : Text, mapValue : Natural })
≡ ([] : List { mapKey : Text, mapValue : Bool })
in map

11
Map/package.dhall Normal file
View file

@ -0,0 +1,11 @@
λ(nix : ../NixPrelude.dhall) →
{ Type = ./Type.dhall
, Entry = ./Entry.dhall
, empty = ./empty.dhall
, keyText = ./keyText.dhall
, keyValue = ./keyValue.dhall
, keys = ./keys.dhall
, map = ./map.dhall
, unpackOptionals = ./unpackOptionals.dhall nix
, values = ./values.dhall
}

26
Map/unpackOptionals.dhall Normal file
View file

@ -0,0 +1,26 @@
λ(nix : ../NixPrelude.dhall) →
let List/concatMap = ../List/concatMap.dhall
let Map/Entry = ./Entry.dhall
let Map/Type = ./Type.dhall
let Optional/fold = ../Optional/fold.dhall nix
let unpackOptionals
: ∀(k : Type) → ∀(v : Type) → Map/Type k (Optional v) → Map/Type k v
= λ(k : Type) →
λ(v : Type) →
List/concatMap
(Map/Entry k (Optional v))
(Map/Entry k v)
( λ(e : Map/Entry k (Optional v)) →
Optional/fold
v
e.mapValue
(Map/Type k v)
(λ(v : v) → [ { mapKey = e.mapKey, mapValue = v } ])
([] : Map/Type k v)
)
in unpackOptionals

30
Map/values.dhall Normal file
View file

@ -0,0 +1,30 @@
--| Get all of the values of a `Map` as a `List`
let Map = ./Type.dhall
let Entry = ./Entry.dhall
let List/map = ../List/map.dhall
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)
let example0 =
assert
: values
Text
Natural
[ { mapKey = "A", mapValue = 2 }
, { mapKey = "B", mapValue = 3 }
, { mapKey = "C", mapValue = 5 }
]
≡ [ 2, 3, 5 ]
let example1 =
assert
: values Text Natural ([] : List { mapKey : Text, mapValue : Natural })
≡ ([] : List Natural)
in values

View file

@ -5,6 +5,8 @@
, Function = ./Function/package.dhall , Function = ./Function/package.dhall
, Integer = ./Integer/package.dhall nix , Integer = ./Integer/package.dhall nix
, List = ./List/package.dhall nix , List = ./List/package.dhall nix
, Location = ./Location/package.dhall
, Map = ./Map/package.dhall nix
, Misc = ./Misc/package.dhall nix , Misc = ./Misc/package.dhall nix
, Monoid = ./Monoid.dhall , Monoid = ./Monoid.dhall
, Natural = ./Natural/package.dhall nix , Natural = ./Natural/package.dhall nix