> I would like to avoid using show all the time for printing strings e.g.
> 
> > val = "the sum of 2 and 2 is "++(show $ 2 + 2)++" whenever."
> 
> I would prefer to type something like:
> 
> > val = "the sum of 2 and 2 is "./(2+2)./" whenever." 
> > -- i can' find a better haskell compatible operator

Let me advertise Olivier Danvy's very cunning idea to implement
printf in Haskell/ML.

        http://www.brics.dk/RS/98/5/index.html


> So I tried creating my own Stringable class:
> > class Stringable a where
> >  toString::a -> String
> 
> > (./) :: (Stringable a,Stringable b)=> a->b->String
> > x./y = (toString x)++(toString y)
> 
> The trouble is that when I try doing things like:
> 
> > res = (2+2) ./ " hello"
> 
> I get an "Unresolved top-level overloading" error.
>
> Is there any way to convince Haskell to just resolve these numbers to
> SOMETHING by default?  Then I can just declare that type an instance of
> Stringable.

What's going on is this.  You've got the context

        (Stringable a, Num a)

arising from the RHS of res, and no further info about what "a" is.
Under extremely restricted circumstances Haskell will choose 
particular type for you, namely

        when the classes involved are all standard prelude classes
        and at least one is numeric.

Why so restrictive?  Because it it *might* make a massive difference
which type is chosen.  Suppose you had

        instance Stringable Int where
           toString n = "Urk!"

        instance Srringable Integer where
           toString n = ".....Ha ha....."

Now whether your program yields "Urk" or "...Ha ha...." depends on
which type Haskell chooses.

There's no technical issue here. One could relax the defaulting
restriction, at the cost of (perhaps) sometimes unexpected behaviour.
Or, as you show, you can just tell it which type to use:

        res = (2+2)::Int ./ "hello"

Simon 



Reply via email to