@@ -382,13 +382,12 @@ span p (x::xs) =
382break : (a -> Bool) -> List a -> (List a, List a)
382break : (a -> Bool) -> List a -> (List a, List a)
383break p = span (not . p)
383break p = span (not . p)
386split : (a -> Bool) -> List a -> List (List a)
385split : (a -> Bool) -> List a -> List (List a)
387split p [] = []
386split p [] = []
388split p xs =
387split p xs =
389 case break p xs of
388 case break p xs of
390 (chunk, []) => [chunk]
389 (chunk, []) => [chunk]
391− (chunk, (c :: rest)) => chunk :: split p rest
390+ (chunk, (c :: rest)) => chunk :: split p (assert_smaller xs rest)
393partition : (a -> Bool) -> List a -> (List a, List a)
392partition : (a -> Bool) -> List a -> (List a, List a)
394partition p [] = ([], [])
393partition p [] = ([], [])
@@ -432,26 +431,23 @@ sorted (x::xs) =
432 Nil => True
431 Nil => True
433 (y::ys) => x <= y && sorted (y::ys)
432 (y::ys) => x <= y && sorted (y::ys)
435−%assert_total -- can't work this out, because in the case which is lifted out
436− -- y::ys and x::xs are bigger than the inputs...
437mergeBy : (a -> a -> Ordering) -> List a -> List a -> List a
434mergeBy : (a -> a -> Ordering) -> List a -> List a -> List a
438mergeBy order [] right = right
435mergeBy order [] right = right
439mergeBy order left [] = left
436mergeBy order left [] = left
440mergeBy order (x::xs) (y::ys) =
437mergeBy order (x::xs) (y::ys) =
441− case order x y of
438+ if order x y == LT
442− LT => x :: mergeBy order xs (y::ys)
439+ then x :: mergeBy order xs (y::ys)
443− _ => y :: mergeBy order (x::xs) ys
440+ else y :: mergeBy order (x::xs) ys
445merge : Ord a => List a -> List a -> List a
442merge : Ord a => List a -> List a -> List a
446merge = mergeBy compare
443merge = mergeBy compare
449sort : Ord a => List a -> List a
445sort : Ord a => List a -> List a
450sort [] = []
446sort [] = []
451sort [x] = [x]
447sort [x] = [x]
453− let (x, y) = split xs in
448+sort xs = let (x, y) = split xs in
449+ merge (sort (assert_smaller xs x))
454− merge (sort x) (sort y) -- not structurally smaller, hence assert
450+ (sort (assert_smaller xs y)) -- not structurally smaller, hence assert
456 splitRec : List a -> List a -> (List a -> List a) -> (List a, List a)
452 splitRec : List a -> List a -> (List a -> List a) -> (List a, List a)
457 splitRec (_::_::xs) (y::ys) zs = splitRec xs ys (zs . ((::) y))
453 splitRec (_::_::xs) (y::ys) zs = splitRec xs ys (zs . ((::) y))