在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])
类型。
答案 0 :(得分:1)
Dhall中的Map
只是List
/ mapKey
对中的mapValue
:
$ dhall <<< 'https://prelude.dhall-lang.org/v16.0.0/Map/Type'
λ(k : Type) → λ(v : Type) → List { mapKey : k, mapValue : v }
...,并且Haskell实现将像(a, b)
这样的2元组编码为{ _1 : a, _2 : b }
,因此与您的Haskell类型相对应的Dhall类型为:
List { mapKey : { _1 : List Text, _2 : List Text }, mapValue : Text }
您是正确的,因为toMap
仅支持具有toMap
值的键的Map
,所以不能使用Text
来建立该类型的值。但是,由于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