[Hat] failure w/ simple example

Olaf Chitil hat@haskell.org
Tue, 20 May 2003 11:28:55 +0100


> I have the following simple program for which Hat does not seem to
> produce compilable code.

> main = putStr (show $ 4 ^ 2)

> Any suggestions?

Yes. You came across a restriction of Hat which, as I just notice, is
unfortunately not documented.

This example needs type defaulting to work. You can see in Hugs

Prelude> :t putStr (show $ 4 ^ 2)
putStr (show $ 4 ^ 2) :: (Integral a, Num b) => IO ()

"a" and "b" are the types of "4" and "2", and the defaulting mechanism
of Haskell sets them to "Integer".

In the Hat-transformed code defaulting does not apply, because the
transformed code uses different classes "Integeral", "Num" etc, but by
definition defaulting only applies to a set number of standard numeric
classes.

The simple solution is to add type annotations:

main = putStr (show $ (4::Integer) ^ (2::Integer))

Don't worry, in practice this situation occures very rarely. (Only twice
in the Haskell standard libraries.) However, an "ambiguous type
variable" error message is a clear indicator of this problem. To locate
in your module where you have to place a type annotation, just add
"default ()" to the module and compile it normally.

Olaf

-- 
OLAF CHITIL, 
 Dept. of Computer Science, The University of York, York YO10 5DD, UK. 
 URL: http://www.cs.york.ac.uk/~olaf/
 Tel: +44 1904 434756; Fax: +44 1904 432767