sum
and psum
mRTheorem
mRTheorem
on plus1.1 Length Preservation: The function append
below takes two lists and returns a new list that is the concatenation of the two input lists. Complete the type specification of append
below, so that the property prop_append_1
is accepted by LiquidHaskell.
1.2 Element Preservation: Append also preserves the elements of the input lists. We encode the elements of the list using the measure elems
below.
Edit the type specification of append
above, so that the properties prop_append_2
and prop_append_3
are accepted by LiquidHaskell.
1.3 Halve: The function halve
below behaves the opposite of append. It takes a list and returns a pair of lists such that the elements of the input list are the union of the elements of the two output lists. Complete the definition of halve
below, so that the type signature is accepted by LiquidHaskell.
In the class we saw that refinements of data type definitions can be used to encode increasing lists and use them to define insertion sort:
2.1 Length Preservation: Measures can be defined for IncList
s too. For example, the measure ilen
below defines the length of an IncList
.
Give the proper type specification for insert
so that isort
satisfies the below type:
{-@ isort :: (Ord a) => xs:[a] -> {v:IncList a | ilen v = len xs } @-}
Note: You should have discovered a bug in the code above. Fix it!
2.2 Element Preservation: We can also measure the elements of an IncList
using the measure ielems
below.
Give the proper type specification for insert
so that isort
satisfies the below type:
{-@ isort :: (Ord a) => xs:[a] -> {v:IncList a | ilen v = len xs && ielems v = elems xs } @-}
The code below defines merge sort using lists refined to be increasing.
3.1 Merge Complete the definition of merge
above so that the type signature is accepted by LiquidHaskell.
3.2 Termination of Merge Termination of merge
is not trivially shown. Provide a termination metric for merge
so that you remove the lazy
annotation below.
3.3 Termination of Msort Termination of msort
is also not trivial. Can you remove the lazy
annotation below by providing a termination metric for msort
?
Hint: You have to refine the result of halve
to ensure that when the input list has more than 2 elements, then each of the two output lists is smaller than the input list.