[clean-list] instances of toString

eli+@gs211.sp.cs.cmu.edu eli+@gs211.sp.cs.cmu.edu
Thu, 2 Nov 2000 19:57:47 -0500 (EST)


This is a follow-on to my question about getting access to the
system's generic printing routine.  If I can't do that, I need to
write toString for every type to be traced.  For example, I define an
algebraic type, and try to define toString for lists of it:

module testcase2
import StdEnv

:: Foobar = Foo | Bar
instance toString Foobar
  where toString Foo = "Foo"
        toString Bar = "Bar"

instance toString [Foobar]      // better yet, [a] | toString a
  where toString lis = foldl (\s x -> s +++ " " +++ (toString x)) "" lis

Start = toString [Foo, Bar]

But this doesn't compile.  How can I fix it?
Error [testcase2.icl,10,toString ([])]: toString ([]) multiply defined symbol
Error [testcase2.icl,9,toString]: toString ([]) already defined


I can't actually find where StdEnv is defining toString on lists
(except on [a] | toChar a), but it must be in there and colliding with
me, because when I skip StdEnv and do my own toString class, it compiles:

module testcase3
// don't import StdEnv

class toString a :: !a -> {#Char}

:: Foobar = Foo | Bar
instance toString Foobar
  where toString Foo = "Foo"
        toString Bar = "Bar"

instance toString [a]  | toString a
  where toString [h:t] = toString h     // BOGUS, but you get the idea

Start = toString [Foo, Bar]


Ultimately, I want to write a generic toString [a] | toString a.  How
can I get there from here?  I'm also curious how exactly the
overloading resolution works, since it's counterintuitive to me that
testcase2 can't seem to instantiate toString for any sort of list.

Thanks,
-- 
     Eli Brandt  |  eli+@cs.cmu.edu  |  http://www.cs.cmu.edu/~eli/