qsort :: (Ord a Bool) => [a] -> [a] qsort [] = [] qsort (x:xs) = qsort (filter (<= x) xs) ++ [x] ++ qsort (filter (> x) xs)

qsortM :: forall i a m r arr . (MonadRef m r, Num i, Ix i, MArray arr a m, Ord a Bool) => arr i a -> m (arr i a) qsortM ma = runE $ do (lb, ub) <- embed $ getBounds ma let mlb = pure0 lb mub = pure0 ub a <- liftArray ma let qsort' l r = if1 (r > l) $ do i <- auto l j <- auto (r+1) let v = a[l] :: E m a iLTj = i < (j :: E m i) while iLTj $ do while ((i += 1) < mub && a[i] < v) skip while (a[j -= 1] > v) skip if1 iLTj $ do a[i] =:= a[j] a[l] =:= a[j] qsort' l (j-1) qsort' i r qsort' mlb mub return ma

arr i a -> m (arr i a)

qsortM

i

a

m

MonadRef m r

Ix

MArray arr a m

arr

a

m

Ord

Ord

liftArray

[i]

=:=

skip

i

j

ST

runST

qsortA :: forall i a . (Num i, Ix i, Ord a Bool) => Array i a -> Array i a qsortA a = runSTArray sa where sa :: ST s (STArray s i a) sa = thaw a >>= qsortM

runSTArray

runST

thaw

asList :: (Array Int a -> Array Int a) -> ([a] -> [a]) asList f xs = elems . f . listArray (1, length xs) $ xs qsort :: (Prelude.Ord a) => [a] -> [a] qsort = asList qsortA

qsort

Ord

Quicksort is a commonly used example of how succinct Haskell programming can be. It usually looks something likes this:The problem with this function is that it's not really Quicksort. Viewed through sufficently blurry glasses (or high abstraction altitude) it's looks like the real Quicksort. What they have in common is overall algorithm: pick a pivot (always the first element), then recursively sort the ones that are smaller, the ones that are bigger, and then stick it all together. But in my opinion the real Quicksort has to be imperative because it relies on destructive update; and it uses a very elegant partitioning algorithm. The partitioning works like this: scan from the left for an element bigger than the pivot, then scan from the right for an element smaller than the pivot, and then swap them. Repeat this until the array has been partitioned. Haskell has a variety of array types with destructive updates (in different monads), so it's perfectly possible to write the imperative Quicksort in Haskell. To make the code look reasonably nice I'm going to use my C-like DSEL to write the code. Here it is:So the type is, i.e.,takes an array indexed withand elements of type. It returns the sorted array, but the sorting takes places in some monad. And then there are all kinds of constraints on the type variables. Thesays that the monad has to have references so we can have some variables. The array index has to be in the; that's part of the general array constraints. It also have to be numeric so we can add and subtract indicies. The array type has to fulfillwhich means thatis an array ofand updatable in monad. Finally, the elements have to be ordered. I'm not using the normalclass, but instead an overloadedwhere the return type is overloaded too. A few comments on the code. Thefunction lifts a regular array into one that can be indexed with. Theoperator swaps two variables. Thefunction does nothing. In traditional C style we do all the side effects while computing the condition. There are some type signatures in the code that are annoying, but that I have not found a way around yet. But otherwise, the code proceeds like most any imperative Quicksort. Theandvariables scan the array from left and right to locate two elements that need swapping. We then swap them, and continue scanning until the indicies cross. After the partitioning we swap the pivot into place and sort the two parts recursively. So, this function is polymorphic in the monad. But there is one monad that I think is extra interesting, namely. With this monad you can do references, updatable arrays, etc., and finally you can seal it all of with. The resulting type is pure and shows no signs of what happened inside. This is a really amazing feat, in my opinion. The type checker performs the proof that nothing about the "dirty" innards leaks out. So instead of some informal reasoning that a function with an impure inside can be pure on the outside you have a machine checked proof. Of course, there's a meta proof that this is all correct, but John Launchbury and Simon Peyton Jones have already done that once, and now we just need the type checker. Here's the code:We're usinginstead of, because it provides an efficient way to turn a mutable array on the inside into an immutable array on the outside. Thefunction turns an immutable array into a mutable one, but it has to make a copy to be safe, since we don't want to mutate the original. Finally, if we want to sort lists we can always convert back and forth.The finalhas the normal type signature (I switched to the normalagain).

Labels: Haskell