I'm curious, and have been unable to find a proposal for something like this in Haskell. Consider if sort
had been written but not sortBy
.
sortBy :: forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy f = map getX . sort . map X
where
newtype X = X { getX :: a }
instance Ord X where
compare (X a) (X b) = f a b
Has anyone seen this proposal?
Apparently, local instances have been discussed briefly at Haskell Prime mailing list: http://web.archiveorange.com/archive/v/eKcS7T2qBpy7czBE2Jei, and more fully in the 6th chapter of Oleg’s paper “Functional Pearl: Implicit Configurations”. I have heard practically nothing about local data declarations, though.