Seiryn
Seiryn

Reputation: 5

Contrained type familly

I have the following type family in haskell :

type family Foo (a :: Bar) :: * where

followed by a bunch of equality. I want to put a constraint on the kind return to ensure they're all instance of a typeclass. How can i do that ?

Upvotes: 0

Views: 100

Answers (2)

leftaroundabout
leftaroundabout

Reputation: 120711

Seems like you're looking for

class (YourConstraint (Foo a)) => Fooish (a :: Bar) where
  type Foo a :: Type

As Carl noted, this is somewhat analogous to the old data declaration style with

data C a => D a = D ...

which is widely considered a bad idea because it's not possible to use the constraint, all it accomplishes is preventing to build values that don't obey the constraint.

But unlike those data declarations, a constraint to an associated type family is useful IMO, because there's an easy way to get at the constrait whenever it's needed: you just need to mention Fooish.

Upvotes: 4

Carl
Carl

Reputation: 27013

In Haskell, it's better not to do that. (See also the desire to put constraints on type variables in data declarations.) You should put constraints where they're actually going to be used. That will still get called out during type checking if it's missing when it's needed, but it will allow things that don't need it to be more flexible.

Upvotes: 2

Related Questions