Type Constraints in Data Declaration Haskell

Yes:

{-# LANGUAGE GADTs #-}
data Scale s where
    Scale :: Num s => s -> s -> Scale s

However, it’s generally considered best practice not to do this. Instead, put the Num constraint only on the functions that use Scales and need the Num constraint. Being relaxed about such constraints allows you to temporarily break the invariant where appropriate; e.g. it’s common to wish for a Functor instance for such a type, which is impossible if you constrain the constructor as above.

Leave a Comment