[Haskell-cafe] type families with non-type indices

Doug McIlroy doug at cs.dartmouth.edu
Wed Oct 8 12:28:05 UTC 2014


Is there an analog or variant of type families that is indexed
by values rather than by types? An example of such a family
is integers mod p. One wants to define the operations once
for all p, so that a type for any particular p can be
introduced by a one-line declaration.

Of course this can be done with p being a run-time parameter:

data Modp = Modp Integer Int
instance Num (Modp) where
        (Modp x p) + (Modp y q) =
                if p==q then Modp ((x+y) 'mod' p) p
                        else error "unequal moduli in Modp operation"

But it would be better to catch the error at compile time and
not have to check at every operation.

Doug


More information about the Haskell-Cafe mailing list