Created
March 14, 2011 01:04
-
-
Save paf31/868615 to your computer and use it in GitHub Desktop.
quicksort algorithm implemented in the Purity language, see http://typesandotherdistractions.com/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# empty and unit types | |
type (Void,,,) = lfix id | |
type (Unit,MakeUnit,) = Void -> Void | |
data Unique = MakeUnit id | |
# natural number type | |
type (Nat,MakeNat,UnmakeNat,FoldNat) = lfix const Unit + id | |
data Zero = MakeNat . inl Unique | |
data Succ = MakeNat . inr | |
data Pred = <const Zero, id> . UnmakeNat | |
data Add = FoldNat <const id, f => $f . Succ> | |
# generic list type | |
type (List A?,MakeList,UnmakeList,FoldList) = lfix const Unit + const A? . id | |
data Empty = MakeList . inl Unique | |
data Cons = curry MakeList . inr | |
# boolean type | |
type (Bool,MakeBool,UnmakeBool) = Unit + Unit | |
data True = MakeBool (inl Unique) | |
data False = MakeBool (inr Unique) | |
data IfThenElse = b => t => f => <const $t, const $f> . UnmakeBool $b | |
# a comparator compares pairs of values | |
type (Comparator A?,MakeComparator,Compare) = A? -> A? -> Bool | |
# a comparator for natural numbers | |
data Leq = MakeComparator (FoldNat <const (<const True, const False> . UnmakeNat), f => $f . Pred>) | |
# concatenate two lists | |
data Concat = FoldList <const id, uncurry (x => f => (Cons $x) . $f)> | |
# split a list into the list of elements less than x and the list greater than x | |
data Split = c => x => FoldList | |
< | |
(const Empty, const Empty), | |
uncurry ( | |
next => ls => | |
IfThenElse (Compare $c $x $next) | |
((curry id) (Cons $next (outl $ls)) (outr $ls)) | |
((curry id) (outl $ls) (Cons $next (outr $ls))) | |
) | |
> | |
# the quicksort algorithm | |
data Quicksort = c => FoldList | |
< | |
const Empty, | |
uncurry (x => | |
(ls => Concat (outl $ls) (Cons $x (outr $ls))) . (Split $c $x) | |
) | |
> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment