[Haskell-cafe] Can this be improved?
Chris Kuklewicz
haskell at list.mightyreason.com
Fri Dec 30 07:36:11 EST 2005
Bruno Oliveira wrote:
> On Tue, 27 Dec 2005 16:39:34 +0000, Chris Kuklewicz wrote:
>>I was wondering if it this small snippet of code could be altered to
>>require fewer OPTIONS -fallow-... switches.
>
>
> Here is a partial solution using only -fglasgow-exts:
...deleted...
> The problem of this solution is that, because we are using show in the
> definition of swim for (a -> t), the strings are beeing printed with extra
> "\"".
>
> *MyShow> main
> Hello" ""World #"[17,18,19]"!"
> I also think [4,5,6]" and "7" are ""cool""."
The extra double quotes being what I am trying to avoid with MyShow
>
>>instance MyShow t => MyShow (String -> t) where
>> swim s x = swim (s ++ x)
This is interesting. It does remove the need for
-fallow-undecidable-instances. Thanks.
>>Could this be improved by TypeEq machinery? Is there a Haskell 98 way
>>to make myShow work this way?
>
>
> The problem of the previous solution is that you do not want, in the general case,
> use the default instance of Show for Strings. An alternative is to just introduce
> a newtype S (isomorphic to String) that allows you to handle Strings differently:
>
... deleted...
>
> By using the newtype S instead of Haskell String, you obtain an Haskell 98
> solution. I am not sure if this is good enough for you but it seems a good compromise.
>
> Hope it helps!
>
> Cheers,
>
> Bruno
I did find the newtype solution, and trying to avoid that is what
motivated such ugly switch flipping usage of GHC. My intuition was that
this example of creating "something like a sub-type-class of Show" which
treats String differently ought to be possible. And it is possible, and
I would hazard a guess that your solution with "MyShow (String->t)" is
safe in spite of the extra switches.
The sad thing is that the two -fallow switches infect client code that
uses the MyShow module. So allowing those tricks for class MyShow must
turn on for classes in code that imports MyShow. If GHC could -fallow
those flags only for certain typeclasses then it would be confinable.
In principle, could GHC ever be extended to confine -fallow this way?
--
Chris
More information about the Haskell-Cafe
mailing list