在 dhall 中编码 `Map ([Text], [Text]) Text` (Haskell)
Encoding `Map ([Text], [Text]) Text` in dhall (Haskell)
在 dhall 中对 Haskell 类型 Map ([Text], [Text]) Text
进行编码的最佳方法是什么?
尝试。看来我们不能用toMap
来做这个:
-- ./config.dhall
toMap { foo = "apple", bar = "banana"} : List { mapKey : Text, mapValue : Text }
x <- input auto "./config.dhall" :: IO Map Text Text
因为我们需要地图域的类型为 ([Text], [Text])
。
Dhall 中的 Map
只是 mapKey
/mapValue
对中的 List
:
$ dhall <<< 'https://prelude.dhall-lang.org/v16.0.0/Map/Type'
λ(k : Type) → λ(v : Type) → List { mapKey : k, mapValue : v }
... 而 Haskell 实现将像 (a, b)
这样的二元组编码为 { _1 : a, _2 : b }
,因此对应于您的 Haskell 类型的 Dhall 类型是:
List { mapKey : { _1 : List Text, _2 : List Text }, mapValue : Text }
您说得对,您不能使用 toMap
来构建该类型的值,因为 toMap
仅支持具有 Text
值的键的 Map
.但是,由于 Map
只是特定类型 List
的同义词,您可以直接写出 List
(就像您在 Haskell 中使用 Data.Map.fromList
),像这样:
let example
: List { mapKey : { _1 : List Text, _2 : List Text }, mapValue : Text }
= [ { mapKey = { _1 = [ "a", "b" ], _2 = [ "c", "d" ] }, mapValue = "e" }
, { mapKey = { _1 = [ "f" ], _2 = [ "g", "h", "i" ] }, mapValue = "j" }
]
in example
在 dhall 中对 Haskell 类型 Map ([Text], [Text]) Text
进行编码的最佳方法是什么?
尝试。看来我们不能用toMap
来做这个:
-- ./config.dhall
toMap { foo = "apple", bar = "banana"} : List { mapKey : Text, mapValue : Text }
x <- input auto "./config.dhall" :: IO Map Text Text
因为我们需要地图域的类型为 ([Text], [Text])
。
Dhall 中的 Map
只是 mapKey
/mapValue
对中的 List
:
$ dhall <<< 'https://prelude.dhall-lang.org/v16.0.0/Map/Type'
λ(k : Type) → λ(v : Type) → List { mapKey : k, mapValue : v }
... 而 Haskell 实现将像 (a, b)
这样的二元组编码为 { _1 : a, _2 : b }
,因此对应于您的 Haskell 类型的 Dhall 类型是:
List { mapKey : { _1 : List Text, _2 : List Text }, mapValue : Text }
您说得对,您不能使用 toMap
来构建该类型的值,因为 toMap
仅支持具有 Text
值的键的 Map
.但是,由于 Map
只是特定类型 List
的同义词,您可以直接写出 List
(就像您在 Haskell 中使用 Data.Map.fromList
),像这样:
let example
: List { mapKey : { _1 : List Text, _2 : List Text }, mapValue : Text }
= [ { mapKey = { _1 = [ "a", "b" ], _2 = [ "c", "d" ] }, mapValue = "e" }
, { mapKey = { _1 = [ "f" ], _2 = [ "g", "h", "i" ] }, mapValue = "j" }
]
in example