diff --git a/Location/Type.dhall b/Location/Type.dhall new file mode 100644 index 0000000..dfff5b0 --- /dev/null +++ b/Location/Type.dhall @@ -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 diff --git a/Location/package.dhall b/Location/package.dhall new file mode 100644 index 0000000..ac35283 --- /dev/null +++ b/Location/package.dhall @@ -0,0 +1 @@ +{ Type = ./Type.dhall } diff --git a/Map/Entry.dhall b/Map/Entry.dhall new file mode 100644 index 0000000..16d02f9 --- /dev/null +++ b/Map/Entry.dhall @@ -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 diff --git a/Map/Type.dhall b/Map/Type.dhall new file mode 100644 index 0000000..7118c3b --- /dev/null +++ b/Map/Type.dhall @@ -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 diff --git a/Map/empty.dhall b/Map/empty.dhall new file mode 100644 index 0000000..508947d --- /dev/null +++ b/Map/empty.dhall @@ -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 diff --git a/Map/keyText.dhall b/Map/keyText.dhall new file mode 100644 index 0000000..0e9a223 --- /dev/null +++ b/Map/keyText.dhall @@ -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 diff --git a/Map/keyValue.dhall b/Map/keyValue.dhall new file mode 100644 index 0000000..b7d60cb --- /dev/null +++ b/Map/keyValue.dhall @@ -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 diff --git a/Map/keys.dhall b/Map/keys.dhall new file mode 100644 index 0000000..eeafc0d --- /dev/null +++ b/Map/keys.dhall @@ -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 diff --git a/Map/map.dhall b/Map/map.dhall new file mode 100644 index 0000000..5c1b2e4 --- /dev/null +++ b/Map/map.dhall @@ -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 diff --git a/Map/package.dhall b/Map/package.dhall new file mode 100644 index 0000000..c6c505c --- /dev/null +++ b/Map/package.dhall @@ -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 + } diff --git a/Map/unpackOptionals.dhall b/Map/unpackOptionals.dhall new file mode 100644 index 0000000..00f22cc --- /dev/null +++ b/Map/unpackOptionals.dhall @@ -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 diff --git a/Map/values.dhall b/Map/values.dhall new file mode 100644 index 0000000..a60af6e --- /dev/null +++ b/Map/values.dhall @@ -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 diff --git a/package.dhall b/package.dhall index ee7b7e9..575f3c6 100644 --- a/package.dhall +++ b/package.dhall @@ -5,6 +5,8 @@ , Function = ./Function/package.dhall , Integer = ./Integer/package.dhall nix , List = ./List/package.dhall nix + , Location = ./Location/package.dhall + , Map = ./Map/package.dhall nix , Misc = ./Misc/package.dhall nix , Monoid = ./Monoid.dhall , Natural = ./Natural/package.dhall nix