[Haskell-cafe] Deferred instance declarations (context without type variables)

Misha Aizatulin avatar at hot.ee
Sat Sep 16 07:41:55 EDT 2006


hello,

  I have a question about context in type signature. I would like to
write a function, say (f :: T -> T) which also relies on an instance of
class C being defined for T. The problem is, I don't want this instance
defined at the same time f is defined, instead I would like to defer
this definition until f is called (in some other module). Naively the
code would look like this:
=================================
module DefineF where

class C a where
  fC :: a -> a

data T = T

f :: (C T) => T -> T
f T = fC T
=================================
module CallF where

instance C T where
  fC = id

call = f T
=================================
  The definition of f in DefineF won't compile because "All of the type
variables .. are already in scope". Could you recommend any other way to
achieve what I am trying to do?

  An interesting thing is that I can easily make the code compile with
the same meaning by changing the 
declaration of T to include a dummy type variable:
> data T a = T
  it's just a pity that I have to trick the compiler in such an ugly
way.

Cheers,
  Misha


More information about the Haskell-Cafe mailing list