我正在读这个,我发现这个:
措施——为了让 Haskell 函数出现在细化类型中,我们需要将它们提升到细化类型级别。
还有其他文件声称需要采取措施才能在合同中使用此类功能。但我试过这个:
{-@ len :: List a -> Nat @-}
len :: List a -> Int
len Nil = 0
len (x `Cons` xs) = 1 + len xs
{-@ mymap :: (a -> b) -> xs : List a -> { ys : List b | len xs == len ys } @-}
mymap :: (a -> b) -> List a -> List b
mymap _ Nil = Nil
mymap f (x `Cons` xs) = f x `Cons` mymap f xs
这有效,但len
不是衡量标准。那么究竟什么是测量值,什么时候需要呢?
另一个不工作的例子measure
:
{-@ measure ln @-}
ln :: [a] -> Int
ln [] = 0
ln (x:y) = 1 + ln y
{-@ conc :: xs : [a] -> ys : [a] -> {zs : [a] | ln zs == ln xs + ln ys} @-}
conc :: [a] -> [a] -> [a]
conc [] ys = ys
conc (x:xs) ys = x : (conc xs ys)
我在许多文档中发现的 like 的使用{-@ measure length @-}
会导致错误Cannot extract measure from haskell function
(即 from length
)。