Skip to content

Instantly share code, notes, and snippets.

@edwinb
Last active August 29, 2015 14:23
Show Gist options
  • Save edwinb/4fca14d54fb29316330b to your computer and use it in GitHub Desktop.
Save edwinb/4fca14d54fb29316330b to your computer and use it in GitHub Desktop.
Type level quicksort
qsort : Ord a => List a -> List a
qsort [] = []
qsort (x :: xs) = let (l, r) = partition (< x) xs in
assert_total (qsort l ++ x :: qsort r) -- assert totality otherwise it won't reduce in the type
data Singleton : a -> Type where
MkSingleton : Singleton x
t_qsort : Ord a => (xs : List a) -> Singleton (qsort xs)
t_qsort xs = MkSingleton
{-
*tlsort> t_qsort [1,6,9,2,4,7,3,5,8]
MkSingleton : Singleton [1, 2, 3, 4, 5, 6, 7, 8, 9]
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment