问题
I am trying to solve some exercise from LiquidHaskell tutorial. So, I wrote this:
data List a = Nil | Cons a (List a) deriving (Show)
infixr 5 `Cons`
{-@ 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
But I'm getting an error (excuse, pls, this formatting, it's the original LH error format):
53 | mymap f (x `Cons` xs) = f x `Cons` mymap f xs
^^^^^^^^^^^^^^^^^^^^^
Inferred type
VV : {v : (Main.List a) | Main.Cons##lqdc##$select v == ?a
&& Main.Cons##lqdc##$select v == ds_d35c x
&& v == Main.Cons (ds_d35c x) ?a}
not a subtype of Required type
VV : {VV : (Main.List a) | len ?b == len VV}
In Context
xs : (Main.List a)
?b : (Main.List a)
x : a
?a : {?a : (Main.List a) | len xs == len ?a}
What is the right "contract" of mymap
? How to fix this error? And how should be read/treated messages like Main.Cons##lqdc##$select v == ds_d35c x
?
回答1:
I had to explicitly annotate the constructors. After that, it compiles with LiquidHaskell.
data List a = Nil | Cons a (List a) deriving (Show)
infixr 5 `Cons`
{-@ len :: List a -> Nat @-}
len :: List a -> Int
len Nil = 0
len (x `Cons` xs) = 1 + len xs
{-@ Nil :: { ys : List a | len ys == 0 } @-}
{-@ Cons :: a -> xs : List a -> { ys : List a | len ys == 1 + len xs } @-}
{-@ mymap :: (a -> b) -> xs : List a -> { ys : List b | len xs == len ys } / [ len xs ] @-}
mymap :: (a -> b) -> List a -> List b
mymap _ Nil = Nil
mymap f (x `Cons` xs) = f x `Cons` mymap f xs
来源:https://stackoverflow.com/questions/56326273/what-is-the-correct-contract-of-the-function-map-in-liquid-haskell