I am trying to learn Liquid Haskell from the book.
To test my understanding, I wanted to write a function log2
which takes an input of the form 2^n and outputs n.
I have the following code:
powers :: [Int]
powers = map (2^) [0..]
{-@ type Powers = {v:Nat | v elem powers } @-}
{-@ log2 :: Powers -> Nat @-}
log2 :: Int -> Int
log2 n
| n == 1 = 0
| otherwise = 1 + log2 (div n 2)
But some strange error occurs while executing this code, namely "Sort Error in Refinement". I am unable to understand and resolve this error.
Any help would be really appreciated.
EDIT: From the Liquid Haskell book:
A Predicate is either an atomic predicate, obtained by comparing two expressions, or, an application of a predicate function to a list of arguments...
In the Liquid Haskell logic syntax, one of the allowed predicates are: e r e
where r
is an atomic binary relation (and functions are just special kind of relations).
Also, in the tutorial, they define the Even
subtype as:
{-@ type Even = {v:Int | v mod 2 == 0 } @-}
Based on that, I thought elem
should work.
But now as @ThomasM.DuBuisson pointed out, I thought of writing my own elem'
instead, so as to avoid confusion.
elem' :: Int -> [Int] -> Bool
elem' _ [] = False
elem' e (x:xs)
| e==x = True
| otherwise = elem' e xs
Now, as far as I understand, to be able to use this elem'
as a predicate function, I need to lift it as measure. So I added the following:
{-@ measure elem' :: Int -> [Int] -> Bool @-}
Now I replaced elem
by elem'
in type definition of Powers
. But I still get the same error as the previous one.