type families with non-type indices

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

On 2014-10-08 15:28, Doug McIlroy wrote:
Is there an analog or variant of type families that is indexed by values rather than by types? It depends on what you want from your indices. If all you want is to lift values to types and back, then the 'reflection' package is what you want[0]. This suffices to define modulo-bearing numbers and local typeclass instances, for example.
You might also want to look into the 'singletons' package[1], which to my understanding provides lifted functions and Template Haskell to lift values to the type level. However, I haven't investigated this package in any depth, so take this paragraph with a grain of salt. HTH, Gesh [0] - https://hackage.haskell.org/package/reflection [1] - https://hackage.haskell.org/package/singletons
participants (2)
-
Doug McIlroy
-
Gesh