possible bug in Eq of packedStrings

David Roundy droundy@abridgegame.org
Sat, 26 Apr 2003 06:57:57 -0400


--Q68bSM7Ycu6FN28Q
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

I think I've come across a bug in the (==) function of PackedString, but
it's hard to reproduce.  I'm attaching a set of files that hopefully will
reproduce the problem.  My assumption is that if two packed strings are
unequal, then when their unpacked versions must also be equal.  This is, of
course, assuming one starts with the packed versions, since packing of
strings may be lossy...

Anyhow, the guts of the problem is in the following (where c1 and c2 are
packed strings):

 if c1 == c2
 then return True
 else do putStr $ "First version:\n"
         putStr $ unpackPS c1 ++ "\n"
         putStr $ "second version:\n"
         putStr $ unpackPS c2 ++ "\n"
         if unpackPS c1 == unpackPS c2
            then putStr "But unpacked, they are the same!!!\n"
            else putStr "And unpacked they are different!\n"
         return False

I think that it is a bug if the line "But unpacked, they are the same!!!"
ever gets printed (which it does--but it's hard to reproduce).  This bug is
rather troubling to me, since one of the reasons I'm using packed strings
is for the assumed efficiency of comparison, the other being of course
storage space, which would be sufficient in itself to mandate packed
strings.

Anyhow, I'm attaching a test program with a test file (which needs to be
named both test1 and test2) which is able to reproduce an error in the
above.  Sorry I haven't made it any simpler.
-- 
David Roundy
http://www.abridgegame.org

--Q68bSM7Ycu6FN28Q
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="fun.hs"

module Main ( main ) where

import PackedString
import IO

main = do h1 <- openFile "test1" ReadMode
          h2 <- openFile "test2" ReadMode
          same <- hcmp h1 h2
          if same then putStr "Files are identical.\n"
                  else putStr "Files differ.\n"

hcmp h1 h2 = do
  c1 <- hGetPS h1 4
  c2 <- hGetPS h2 4
  if c1 == c2 && lengthPS c1 == 4
     then hcmp h1 h2
     else if c1 == c2
          then return True
          else do putStr $ "First version:\n"
                  putStr $ unpackPS c1 ++ "\n"
                  putStr $ "second version:\n"
                  putStr $ unpackPS c2 ++ "\n"
                  if unpackPS c1 == unpackPS c2
                     then putStr "But unpacked, they are the same!!!\n"
                     else putStr "And unpacked they are different!\n"
                  return False

--Q68bSM7Ycu6FN28Q
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=test1

%  Copyright (C) 2002 David Roundy  
%
%  This program is free software; you can redistribute it and/or modify
%  it under the terms of the GNU General Public License as published by
%  the Free Software Foundation; either version 2, or (at your option)

--Q68bSM7Ycu6FN28Q--