0size returns positive valuesavghead and tail are Safefoldr1averagemapinitinit'insertLists ElementsListmax type (I)max type (II)max type (III)max typekeysMapevalcreateunsafeTakeunpackchop
Abstract Your Property Away to make the specification Modular!
We saw how to define ordered lists:
{-@ data OList a =
OEmp
| (:<:) { oHd :: a
, oTl :: OList {v:a | oHd <= v}} @-}
We can get ordered lists as an instanse of an abstractly refined list type.
Consinder a monomorphic max function:
max :: Int -> Int -> Int
max x y = if x >= y then x else y
How can we refine the type of max?
max type (I)If max gets two Odd numbers:
max :: Odd -> Odd -> Odd
Odd number:
maxOdd :: Odd
maxOdd = max 3 7
max type (II)If max gets two Even numbers:
max :: Even -> Even -> Even
Even number:
maxOdd :: Even
maxOdd = max 40 42
max type (III)max gets two Prime numbers: max :: Prime -> Prime -> Prime
Prime number:
maxOdd :: Prime
maxOdd = max 7 13
max typeSo, what is the type of max?
max :: Odd -> Odd -> Odd
max :: Even -> Even -> Even
max :: Prime -> Prime -> Prime
max returns one of its two inputs x and y.
This holds, regardless of what that property was!
{-@ max :: forall <p :: Int -> Prop>.
Int<p> -> Int<p> -> Int<p> @-}
max :: Int -> Int -> Int
max x y = if x <= y then y else x
Where
Int<p> is just an abbreviation for {v:Int | (p v)}
This type states explicitly:
p, that is a property of Int,max takes two inputs that satisfy p,max returns an output that satisfies p.
p will be automagically instantiated with that concrete refinement,{-@ type Odd = {v:Int | (v mod 2) = 1} @-}
{-@ maxOdd :: Odd @-}
maxOdd :: Int
maxOdd = max 3 5
Abstract Your Property Away to make the specification Modular!
Then, instantiate the property back to get your specification.
Previously we saw how to refine the list data definition to get ordered lists:
{-@ data OList a =
OEmp
| (:<:) { oHd :: a
, oTl :: OList {v:a | oHd <= v}} @-}
Just abstract the property away!
a -> Prop> = Emp | (:::) { hd :: a , tl :: List a} @-}
Q: Use abstract refinements to refine the above list data declaration to describe ordered lists.
NIKI TODO: something goes terrible wrong in the presentation if I use abstract refinements here! todo it online!
Given increasing lists
{-@ type OList a = List <{\x v -> x <= v}> a @-}
We use them to specify insertion sort, as before!
Haskell build-in list comes parameterized for you!
You can just instantiate the abstract refinement!
And prove recursive list properties!
Next: Application