Q: Resolving ambiguous type variable

tty@SAFe-mail.net tty@SAFe-mail.net
Fri, 17 May 2002 11:58:34 -0500


Hello,  
   I am writing a function which returns an exception/error string should an unexpected parameter is passed in. Borrowing from the Maybe module I have the following:
------------------------------------------------------------
data Result a = Ex String | Value a deriving Show

-- Testing the Result a type
first_h :: [a] -> Result a
first_h [] = Ex "No list"
first_h (x:xs) = Value x

-- Trying out the first_h
main :: IO()
main = print (first_h [])
------------------------------------------------------------

Which the compiler complains:

Ambiguous type variable(s) `a' in the constraint `Show a'
    arising from use of `print' at atype.hs:8
    In the definition of `main': print (first_h [])

This is understandable since it is unable to unify the empty list with a concrete list of type 'a' i.e. there are an infinite types which would match. My question is how can I indicate to the compiler that it does not matter which type the empty list is since the return result is of a concreate type.

Thanks

Tee