[Haskell-cafe] And another basic typing question -- empty list

Wolfgang Jeltsch wolfgang at jeltsch.net
Fri Sep 16 10:48:59 EDT 2005


Am Freitag, 16. September 2005 16:02 schrieb Adam Wyner:
> [...]

> Suppose I have two expressions:
>
> emptyListA = null
>
> emptyListB = []
>
> emptyListA is apparently a function from empty lists to Bool.

emptyListA is a function from *arbitrary* lists to Bool.

> [...]

> The problem is that there is no "show" function for
> emptyListB, which is just []
>
>  > emptyListB
>
> ERROR - Cannot find "show" function for:
> *** Expression : emptyListB
> *** Of type    : [a]

The type [a] means forall a. [a], i.e., you can replace the "a" in [a] with an 
arbitrary type t and the expression in question has type [t].  This is only 
true for the empty list.  [] has type [Integer], [String] etc.

A show function which only accepted empty lists would have type
(forall a. [a]) -> String.  This is not possible in Haskell 98.  You would 
need explicit universal quantification.

What is possible in Haskell is to give show the type [a] -> String.  This 
means forall a. ([a] -> String), i.e., show has every type [t] -> String 
where t is an arbitrary type.  This would allow show to be applied to 
non-empty lists whose elements itself cannot be shown via the show functions.  
This has, of course, to be disallowed.  The type [a] -> String is too 
general.

The actual type of show in Haskell 98 is Show a => [a] -> String.  The element 
type has to be an instance of Show which means that its values can be shown 
via the show function.

> What I would like, simply is:
>  >emptyListB
>
> []

You can specialize the type of emptyListB:

	input: emptyListB :: [Int]
	output: []

> [...]

Best wishes,
Wolfgang


More information about the Haskell-Cafe mailing list