我想在Dafny的一些事情。我想編寫一個簡單的數據結構,在內存中保存的壓縮前的圖像:Dafny:類型與contraints
datatype image' = image(width: int, height: int, data: array<byte>)
newtype byte = b: int | 0 <= b <= 255
真正使用它:
method Main() {
var dat := [1,2,3];
var im := image(1, 3, dat);
}
datatype image' = image(width: int, height: int, data: array<byte>)
newtype byte = b: int | 0 <= b <= 255
導致Dafny抱怨:
stdin.dfy(3,24): Error: incorrect type of datatype constructor argument (found seq, expected array) 1 resolution/type errors detected in stdin.dfy
我可能還需要要求字節數組不爲空,並且字節數組的大小等於width * height * 3(以存儲表示該像素的RGB值的三個字節)。
我該如何執行此操作?我研究過newtype,它可以讓你對某些類型的變量進行一些約束,但是這隻適用於數字類型。