type-level-sets icon indicating copy to clipboard operation
type-level-sets copied to clipboard

Alphabetical sort for `Cmp` instance

Open etorreborre opened this issue 7 years ago • 0 comments

I am working on a variation of the typelevel Set (a set with possibly "missing" values). I would like to find a way to define a Cmp instance for 2 type a and b so that the result depends on the names of a and b. So far I was able to write

-- | taken from http://www.mchaver.com/posts/2017-12-12-type-name-to-string.html
type family TypeName a :: Symbol where
  TypeName Double = "Double"
  TypeName Int = "Int"
  TypeName String = "String"
  TypeName (M1 D ('MetaData name _ _ _) f ()) = name
  TypeName a = TypeName (Rep a ())

type family Cmp (a :: k) (b :: k) :: Ordering where
  Cmp a b = CmpSymbol (TypeName a) (TypeName b)

But this approach necessitates that a and b derive Generic is there another way to achieve a meaningful "default" comparison between Haskell types which does not require any constraint?

Thanks.

etorreborre avatar Jun 01 '18 22:06 etorreborre