在dhall中定义复杂类型的列表



我定义了几个类型,其中两个被组织到列表中(字段和选项(。函数的定义有助于定义条目(addfield和addoption(。我能够按预期定义字段和工作;然而,对于选项,当列表中有多个元素时,我会得到一个错误。

这是完整的dhall文件:

let FTypes = < U8 | U16 | U32 | F32 | BITS >
let Translate = List { mapKey : Text, mapValue : Text }
let Bits = < Bit0 | Bit1 | Bit2 | Bit3 | Bit4 | Bit5 | Bit6 | Bit7 >
let bits2Natural
: Bits → Natural
= λ(b : Bits) →
merge
{ Bit0 = 0
, Bit1 = 1
, Bit2 = 2
, Bit3 = 3
, Bit4 = 4
, Bit5 = 5
, Bit6 = 6
, Bit7 = 7
}
b
let Field =
let TypeBits = { bit : Natural, width : Natural }
in  { Type =
{ startbyte : Natural
, type : FTypes
, translate : Optional Translate
, typebits : Optional TypeBits
}
, default = { translate = None Translate, typebits = None TypeBits }
}
let FieldList = List { mapKey : Text, mapValue : Field.Type }
let Option = { dlc : Natural, fields : FieldList, telemjson : Optional Text }
let OptionList = List { mapKey : Text, mapValue : Option }
let addfield = λ(n : Text) → λ(f : Field.Type) → { mapKey = n, mapValue = f }
let addoption = λ(k : Text) → λ(v : Option) → { mapKey = k, mapValue = v }
let testoption : OptionList =
[ addoption "0"
{ dlc = 4
, fields =
[ addfield "field0" Field::{startbyte = 1, type = FTypes.U16 }
, addfield "field1" Field::{startbyte = 3, type = FTypes.U8 }
]
, telemjson = Some 
(  "{"sensor1":{"
++ ""time":"@(timestamp)","
++ ""id":"@(option)","
++ ""temp":@(field0.value),"
++ ""unit":"@(field1.value)""
++ "}}"
)
}
, addoption "1"
{ dlc = 2
, fields = [ addfield "field0" Field::{startbyte = 1, type FTypes.U8 } ]
, telemjson = Some
("{"sensor2":{"value":@(field1.value)}}")
}
]

in testoption

这是错误输出:

Error: Invalid input
trial2.dhall:60:9:
|
60 |         { dlc = 2
|         ^
unexpected '{'
expecting ',', ->, :, ], or whitespace

如果我删除第二个选项(第59-64行(,它会起作用吗?

我做错了什么?如何为一个OptionList定义多个Option

解析错误可能需要改进,但由于此处缺少=符号,从第60行开始的记录中有一个拼写错误:

, fields = [ addfield "field0" Field::{startbyte = 1, type FTypes.U8 } ]
↑

应该是:

, fields = [ addfield "field0" Field::{startbyte = 1, type = FTypes.U8 } ]

…如果您进行了更改,则文件将成功解析并进行类型检查。

最新更新