Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 596
Clinton Avatar asked Aug 11 '26 09:08

Clinton


1 Answers

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.

like image 82
kosmikus Avatar answered Aug 13 '26 08:08

kosmikus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!