And another basic typing question -- empty list

Hi, I have some questions about using the empty list in Hugs. In June 2001, this was raised as a bug in hugs (see Hugs-Bugs Archives). References therein to a bug list on S. Thompson's pages come up a dead end. I haven't found any follow up or solution to this discussed. I would particularly like to use an empty list in the Trex module of Hugs. Suppose I have two expressions: emptyListA = null emptyListB = [] emptyListA is apparently a function from empty lists to Bool. I would have thought that emptyListB would just be the empty list and output it as such.
:t emptyListA
emptyListA :: [a] -> Bool
emptyListA [] True
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]
What I would like, simply is:
emptyListB []
More to my purpose, I would like to use the empty list as a value of a record in Trex. For instance, while the first example below gives a good result, the second example has no "show" function. Looking at the Trex module, it would seem that there is a show function for the empty list (?? but frankly, I have never touched this area of haskell, so I haven't given it much thought). Sample command line I/O.
(a = 'a', b = [2]) (a = 'a', b = [2])
(a = 'a', b = []) ERROR - Cannot find "show" function for: *** Expression : (a = 'a', b = []) *** Of type : Rec (a :: Char, b :: [a])
Clearly, the problem is the empty list labelled b. What I want is the following sort of result:
(a = 'a', b = []) (a = 'a', b = [])
Further along, what I want is to have values for the field labelled b to be lists which contain something. So, in one case, the value is [], while in another it is [2], say. Moreover, I want to define a type which has a label with values of type lists. That is, I want to have a record something like Rec (a :: Char, b :: List). However, List is not a basic data type. And in any case, the values of labels cannot show empty lists. I'm unsure whether Trex or any record type can handle this. Here are some examples. The following is OK. type TestList01 = Rec (a :: Char, b :: [Char]) testList01 :: TestList01 testList01 = (a = 'a', b = "b") Input and output are:
testList01 (a = 'a', b = "b")
ComplexActions01> :t (a = 'a', b = "b") (a = 'a', b = "b") :: Rec (a :: Char, b :: [Char]) But the following type is uninterpretable: type TestList02 = Rec (a :: Char, b :: []) ERROR "ComplexActions01.lhs":196 - Illegal type "[]" in constructor application. Suggestions about how to treat empty lists? Thanks, Adam Wyner

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
participants (2)
-
Adam Wyner
-
Wolfgang Jeltsch