在dhall(Haskell)中编码Map([Text],[Text])Text`

时间:2020-05-11 21:17:43

标签: haskell dhall

在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])类型。

1 个答案:

答案 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