Typeclass instantiated on every value of a data kind

118 views Asked by At

Is it possible to specify that every member of a datakind satisfies a typeclass, such that the class constraint is implied? E.g.

data AB = A | B
class Foo (a :: AB) where get :: proxy a -> String
instance Foo A where get _ = "A"
instance Foo B where get _ = "B"

-- note lack of constraint here
get' :: proxy (a :: AB) -> String
get' = get

Basically a is an AB so we're sure there's an instance of Foo for it. I find it unlikely -- where is it going to get the Foo dictionary? -- but I've seen some magic in my day.

1

There are 1 answers

5
dfeuer On BEST ANSWER

No, you can't do that. The primary problem is that, as you mention, there's nothing there to get you a dictionary. But the other problem is that your claim that every type in AB is an instance of Foo is false.

type family Broken :: AB where