[Haskell-cafe] accessing a type variable in instance declaration

TP paratribulations at free.fr
Wed May 22 16:18:55 CEST 2013


Hi all,

I wonder if there is some means to get a type variable value in the body of 
a class instance definition. It seems it is not possible (a workaround has 
already been given on this list), but I would like a confirmation.
For example, imagine that I have a Tensor type constructor, that takes a 
type-level integer (representing the Tensor order) to make a concrete type:

---------
instance Show (Tensor order) where
     show TensorVar str = show "tensor " ++ str ++ " of order "
                                    ++ (show (c2num order))
----------

where c2num transforms a type-level integer to an integer, through 
typeclasses (see 
http://www.haskell.org/haskellwiki/The_Monad.Reader/Issue5/Number_Param_Types)

I obtain a compilation error: order is not known in the body of the 
instance. Putting ScopedTypeVariable as extension does not change anything 
(http://www.haskell.org/ghc/docs/7.0-latest/html/users_guide/other-type-extensions.html#scoped-type-variables).

I have tried also using forall, without more success:

------------
instance forall order. Show (Tensor order) where
      show TensorVar str = show ”tensor ” ++ str ++ ” of order ”
                                ++ (show (c2num order))
------------

Thanks in advance,

TP




More information about the Haskell-Cafe mailing list