Skip to content

Instantly share code, notes, and snippets.

@dashster18
Created July 16, 2014 18:22
Show Gist options
  • Save dashster18/91774497bb3db3d5e295 to your computer and use it in GitHub Desktop.
Save dashster18/91774497bb3db3d5e295 to your computer and use it in GitHub Desktop.
import Test.QuickCheck
import Data.List
qsort :: Ord a => [a] -> [a]
qsort [] = []
qsort (x:xs) = qsort lhs ++ [x] ++ qsort rhs
where lhs = filter (< x) xs
rhs = filter (>= x) xs
prop_idempotent xs = qsort (qsort xs) == qsort xs
prop_minimum xs = head (qsort xs) == minimum xs
prop_minimum' xs = not (null xs) ==> head (qsort xs) == minimum xs
prop_ordered xs = ordered (qsort xs)
where ordered [] = True
ordered [x] = True
ordered (x:y:xs) = x <= y && ordered (y:xs)
prop_permutation xs = permutation xs (qsort xs)
where permutation xs ys = null (xs \\ ys) && null (ys \\ xs)
prop_maximum xs = not (null xs) ==> last (qsort xs) == maximum xs
prop_append xs ys = not (null xs) ==> not (null ys) ==>
head (qsort (xs ++ ys)) == min (minimum xs) (minimum ys)
prop_sort_model xs = sort xs == qsort xs
run = do
quickCheck (prop_idempotent :: [Int] -> Bool)
quickCheck (prop_minimum' :: [Int] -> Property)
quickCheck (prop_ordered :: [Int] -> Bool)
quickCheck (prop_permutation :: [Int] -> Bool)
quickCheck (prop_maximum :: [Int] -> Property)
quickCheck (prop_append :: [Int] -> [Int] -> Property)
quickCheck (prop_sort_model :: [Int] -> Bool)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment