1

Is it possible to set a constraint on type:

data T a = T a

So that a could be only types that have instances on certain type class C. Though I'm not sure if it is even relevant, but couldn't find info about it. If it is not possible what is a simple explanation of this?

WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181
  • Do you want to restrict the possible values only during construction of `T` values or do you need the consumers of such values to have access to the `C` instance as well? – Fyodor Soikin May 07 '21 at 01:30
  • 1
    Or, to put it more simply: assuming such feature exists, what are you going to do with it? Can you provide a pseudocode example perhaps? – Fyodor Soikin May 07 '21 at 01:31

1 Answers1

3

It's no possible to constrain a data constructor, no. There is an extension that allows this in Haskell, but it's considered to be a not very successful one, since it still requires use of the constraint in all signatures where the type is used too, so it's not really giving you anything.

More info on it in Haskell here: DatatypeContexts Deprecated in Latest GHC: Why?

gb.
  • 4,629
  • 1
  • 20
  • 19