Clinton
Clinton

Reputation: 23135

Partially applied equality constraint

Consider the following:

class (a ~ b) => Equal a b
instance (a ~ b) => Equal a b

Lets say I later have a data type:

data D (c :: * -> Constraint) where
  D :: Proxy c -> D c

Then something like this is valid:

D (Proxy :: (Proxy (Equal Int)))

My question is, is there any way to write Equal other than the repetitive class/instance notation I've used?

It seems that when I define Equal as a type family Equal Int doesn't work because it's an incomplete application.

The class/instance hack looks bad but at least it works, but is there any other maybe cleaner way of achieving this?

Note I'll be using constraints more complex than equality in my actual code, I just thought this would be a good example.

Upvotes: 3

Views: 160

Answers (1)

kosmikus
kosmikus

Reputation: 19637

You do not need a class at all, you can just use (~) in prefix notation.

There is a bug in GHC 8.0.1 which sometimes makes it crash, but it is just a bug, and it's fixed in 8.0.2. The class / instance combo you're describing is indeed an effective workaround for this bug.

If you do not need backwards compatibility with 8.0.1, you can use (~) Int or Proxy ((~) Int) directly.

Upvotes: 4

Related Questions