``Magic'' Ord defaulting?

kahl@heraklit.informatik.UniBw-Muenchen.de kahl@heraklit.informatik.UniBw-Muenchen.de
17 Jan 2001 14:44:58 -0000


Hi all,

  a student of mine was puzzled by the following output,
where, what appears to be equivalent expressions,
yields non-equivalent results:

========================================================================
kahl@dionysos:/tmp/will > hugs Tree.hs 
__   __ __  __  ____   ___      _________________________________________
||   || ||  || ||  || ||__      Hugs 98: Based on the Haskell 98 standard
||___|| ||__|| ||__||  __||     Copyright (c) 1994-1999
||---||         ___||           World Wide Web: http://haskell.org/hugs
||   ||                         Report bugs to: hugs-bugs@haskell.org
||   || Version: February 2000  _________________________________________

Haskell 98 mode: Restart with command line option -98 to enable extensions

Reading file "/usr/global/share/hugs/lib/Prelude.hs":
Reading file "Tree.hs":
                   
Hugs session for:
/usr/global/share/hugs/lib/Prelude.hs
Tree.hs
Type :? for help
Main> printH (listToTree [1,2,3,4,5])
                    []
               [1]--|
                    []
          [2]--|
               []
     [3]--|
          []
[5]--|
          []
     [4]--|
          []

Elapsed time (ms): 20 (user), 10 (system)
Main> listToTree [1,2,3,4,5]
Node 5 (Node 4 (Node 3 (Node 2 (Node 1 Empty Empty) Empty) Empty) Empty) Empty
Elapsed time (ms): 20 (user), 0 (system)
Main> printH (Node 5 (Node 4 (Node 3 (Node 2 (Node 1 Empty Empty) Empty) Empty) Empty) Empty)
     []
[5]--|
          []
     [4]--|
               []
          [3]--|
                    []
               [2]--|
                         []
                    [1]--|
                         []

Elapsed time (ms): 30 (user), 0 (system)


========================================================================

for the following definitions:

========================================================================
data Tree a = Empty | Node a (Tree a) (Tree a)
   deriving Show

insert :: (Ord a, Show a) => Tree a -> a -> Tree a
insert Empty a = Node a Empty Empty
insert (Node a left right) new
  | new < a   = Node a (insert left new) right
  | otherwise = Node a left (insert right new)

listToTree :: (Ord a, Show a) => [a] -> Tree a
listToTree [] = Empty
listToTree (x:xs) = insert (listToTree xs) x

printH :: ((Ord a, Show a) => Tree a) -> IO()      -- Problem here!
printH t = putStr (hilf1 t 0)

hilf1 :: (Ord a, Show a) => Tree a -> Int -> String

hilf1 Empty space = replicate space ' ' ++ "[]\n"
 
hilf1 (Node a left right) space =
       hilf1 right newspace 
    ++ replicate space ' ' ++ "[" ++ show a ++ "]--|\n"
    ++ hilf1 left newspace 
  where newspace = space + length (show a ++ "[]-|")

========================================================================

I know the problem lies in the illegal parentheses on line 14,
and I suspect this triggers defaulting.
The following seems to support this at least partially:

Main> printH (listToTree [1::Integer,2,3,4,5])
                    []
               [1]--|
                    []
          [2]--|
               []
     [3]--|
          []
[5]--|
          []
     [4]--|
          []
Main> printH (listToTree [1::Int,2,3,4,5])
     []
[5]--|
          []
     [4]--|
               []
          [3]--|
                    []
               [2]--|
                         []
                    [1]--|
                         []


However, I still have

Main> (4 :: Integer) < (5 :: Integer)
True
Main> (5 :: Integer) < (4 :: Integer)
False

So it does not seem to be a bug in Integer comparison.

In addition I do not understand

1) why Hugs accepts these parentheses (without -98) (btype has no context)

2) why this is the result of accepting them.

I am not on hugs-bugs.


Best regards,

Wolfram