I want to write an Ord instance for a data type Foo which delegates all comparison to a function bar :: Foo -> Bar where Bar is a data type that has an Ord instance available.
If I manually write this instance it looks like:
instance Ord Foo where
compare x y
| bar x == bar y = EQ
| bar x <= bar y = LT
| otherwise = GT
Is there a more concise way to write this?
In Scala (with Scalaz), I can write:
implicit val FooOrder: Order[Foo] = Order[Bar] contramap bar
Does Haskell have anything similar?
is the most concise version I can think of OTTOMH.
Slightly less concise, but better to generalise is