[Haskell-cafe] CDouble type coercion

Twan van Laarhoven twanvl at gmail.com
Sun May 14 19:45:04 EDT 2006

SevenThunders wrote:
 > test.hs:14:11:
 >     No instance for (PrintfType (t t1))
 >       arising from use of `printf' at test.hs:14:11-16
 >     Probable fix: add an instance declaration for (PrintfType (t t1))
 >     In the result of a 'do' expression: printf "%14.7g" u
 >     In the definition of `test': test = do printf "%14.7g" u
 > Failed, modules loaded: none.

The problem here appears to be the monomorphism restriction. This means 
that all declarations without arguments get a monomorphic type, a type 
without variables. The type of 'test' would be 'PrintfType r => r', but 
the compiler does not allow this type. The solution is to either:
  - make the type explicit by adding a type declaration, "test :: IO ()" 
or "Test :: PrintfType r => r"
  - make test local in a context where it is only used as an IO action, 
for example:
    > main :: IO ()
    > main = do {... ; test ; ...}
    >   where test = printf "%14.7g" 3.14
  - add a parameter to test


More information about the Haskell-Cafe mailing list