{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances #-}
{-# LANGUAGE Safe, CPP, ConstraintKinds #-}
#if __GLASGOW_HASKELL__ >= 806
{-# LANGUAGE QuantifiedConstraints, RankNTypes #-}
#endif
module SDP.Sort
(
Sort (..), Sort1, Sort2,
#if __GLASGOW_HASKELL__ >= 806
Sort', Sort'',
#endif
sort, sortOn, sorted, sortedOn
)
where
import Prelude ()
import SDP.SafePrelude
import SDP.Zip
import qualified Data.List as L
default ()
class Sort s e | s -> e
where
{-# MINIMAL sortBy, sortedBy #-}
sortedBy :: (e -> e -> Bool) -> s -> Bool
sortBy :: Compare e -> s -> s
instance Sort [a] a
where
sortedBy :: (a -> a -> Bool) -> [a] -> Bool
sortedBy a -> a -> Bool
f (a
e : [a]
es) = (a -> a -> Bool) -> [a] -> [a] -> Bool
forall (z :: * -> *) a b.
Zip z =>
(a -> b -> Bool) -> z a -> z b -> Bool
all2 a -> a -> Bool
f (a
e a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
es) [a]
es
sortedBy a -> a -> Bool
_ [] = Bool
True
sortBy :: Compare a -> [a] -> [a]
sortBy = Compare a -> [a] -> [a]
forall a. Compare a -> [a] -> [a]
L.sortBy
type Sort1 rep e = Sort (rep e)
type Sort2 rep i e = Sort (rep i e)
#if __GLASGOW_HASKELL__ >= 806
type Sort' rep = forall e . Sort (rep e)
type Sort'' rep = forall i e . Sort (rep i e)
#endif
sorted :: (Sort s e, Ord e) => s -> Bool
sorted :: s -> Bool
sorted = (e -> e -> Bool) -> s -> Bool
forall s e. Sort s e => (e -> e -> Bool) -> s -> Bool
sortedBy e -> e -> Bool
forall a. Ord a => a -> a -> Bool
(<=)
sortedOn :: (Sort s e, Ord o) => (e -> o) -> s -> Bool
sortedOn :: (e -> o) -> s -> Bool
sortedOn = (e -> e -> Bool) -> s -> Bool
forall s e. Sort s e => (e -> e -> Bool) -> s -> Bool
sortedBy ((e -> e -> Bool) -> s -> Bool)
-> ((e -> o) -> e -> e -> Bool) -> (e -> o) -> s -> Bool
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((o -> o -> Bool) -> (e -> o) -> e -> e -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
on o -> o -> Bool
forall a. Ord a => a -> a -> Bool
(<=))
sort :: (Sort s e, Ord e) => s -> s
sort :: s -> s
sort = Compare e -> s -> s
forall s e. Sort s e => Compare e -> s -> s
sortBy Compare e
forall a. Ord a => a -> a -> Ordering
compare
sortOn :: (Sort s e, Ord o) => (e -> o) -> s -> s
sortOn :: (e -> o) -> s -> s
sortOn = Compare e -> s -> s
forall s e. Sort s e => Compare e -> s -> s
sortBy (Compare e -> s -> s)
-> ((e -> o) -> Compare e) -> (e -> o) -> s -> s
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (e -> o) -> Compare e
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing