I am reading this where I find this:
Measures -- In order to allow Haskell functions to appear in refinement types, we need to lift them to the refinement type level.
And there are other documents asserting that measures are needed to use such function in contract. But I tried this:
{-@ 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
and this works but len
is not measure. So what is measure exactly and when I need it?
Another example which does not work without 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)
Usage of {-@ measure length @-}
like I found in many documents leads to error Cannot extract measure from haskell function
(ie. from length
).
noun. the extent, quantity, amount, or degree of something, as determined by measurement or calculation. a device for measuring distance, volume, etc, such as a graduated scale or container.
An example of measurement means the use of a ruler to determine the length of a piece of paper. An example of measurement is 15" by 25". The act of measuring. The bust, waistline, and hip dimensions of a woman.
1 : to find out the size, extent, or amount of You should measure the cloth before cutting. 2 : to separate out a fixed amount She measured the rice. 3 : estimate entry 1 I had to measure the distance with my eye. 4 : to bring into comparison Why don't you measure your skill against mine?
Measurement is a comparison of an unknown quantity with a known fixed quantity of the same kind. The value obtained on measuring a quantity is called its magnitude. The magnitude of a quantity is expressed as numbers in its unit.
What is Measurement? “Measurement” is the act of determining a target's size, length, weight, capacity, or other aspect. There are a number of terms similar to “measure” but which vary according to the purpose (such as “weight,” “calculate,” and “quantify.”)
Measurement is fundamental to the sciences; to engineering, construction, and other technical fields; and to almost all everyday activities. For that reason the elements, conditions, limitations, and theoretical foundations of measurement have been much studied.
Measurement theory is the study of how numbers are assigned to objects and phenomena, and its concerns include the kinds of things that can be measured, how different measures relate to each other, and the problem of error in the measurement process.
A measure is also one of the small equal parts into which a piece of music is divided, containing a fixed number of beats; a bar. Next, we need to measure the temperature of the mixture.
A measure is just a function that can be run by LiquidHaskell at verification time, to be used in refinements and termination checking. You likely already knew this.
The reason your first example "works" (I think it's incomplete as-is, but I can tell what you were going for) is that len
is already defined as a measure in the LiquidHaskell prelude (technically it's a "class measure", which means it's polymorphic, and thus can be used both with []
lists and your custom List
). Assuming you've added annotations for Nil
and Cons
as in this answer from your earlier question, the len
used in the refinement for mymap
is not your len
, but the prelude len
, which is already a measure.
In your second example, measure
is required because ln
is a new symbol in the measure namespace, and doesn't exist unless you make it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With