Quantcast

Re: default instance for IsString

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: default instance for IsString

Markus Läll-2
Hi Greg

You *can* have what you want -- enable extended defaulting and set
Text as default by

> {-# LANGUAGE ExtendedDefaultRules #-}
> default (Integer, Double, T.Text) -- keep the default defaults

At the moment you only by chance have one instance of NoDefault in
scope, which is why it shouldn't work at the moment.

(ExtendedDefaultRules is required because the standard defaulting
rules apply only when defaulting numeric literals.)

On Sun, Apr 22, 2012 at 7:55 AM, Greg Weber <[hidden email]> wrote:

> This is a better demonstration of the issue. I am going to open a GHC
> bug report, as I can't see how this behavior is desirable.
>
>
> {-# LANGUAGE OverloadedStrings #-}
> import Data.Text as T
>
> class    NoDefault a      where noDefault :: a -> Text
> instance NoDefault T.Text where noDefault = id
>
> main = print (noDefault "Hello!")
>
> default.hs:7:15:
>    Ambiguous type variable `a0' in the constraints:
>      (NoDefault a0) arising from a use of `noDefault'
>                     at default.hs:7:15-23
>      (Data.String.IsString a0) arising from the literal `"Hello!"'
>                                at default.hs:7:25-32
>    Probable fix: add a type signature that fixes these type variable(s)
>    In the first argument of `print', namely `(noDefault "Hello!")'
>    In the expression: print (noDefault "Hello!")
>    In an equation for `main': main = print (noDefault "Hello!")
>
>
> On Sat, Apr 21, 2012 at 7:51 PM, Greg Weber <[hidden email]> wrote:
>> my actual use case looks more like this:
>>
>> {-# LANGUAGE OverloadedStrings #-}
>> {-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
>>
>> import Data.Text as T
>>
>> class ShowT a where
>>   showT :: a -> String
>>
>> instance ShowT T.Text where
>>   showT = show
>>
>> instance ShowT String where
>>   showT = show
>>
>> main = print (showT "Hello!")
>>
>>    Ambiguous type variable `a0' in the constraints:
>>      (ShowT a0) arising from a use of `showT' at default.hs:16:15-19
>>      (Data.String.IsString a0) arising from the literal `"Hello!"'
>>
>>
>> So I actually want to define a default instance for a typeclass I
>> define that uses isString instances.
>>
>>
>>
>> On Sat, Apr 21, 2012 at 6:24 PM, Daniel Peebles <[hidden email]> wrote:
>>> I think it'll be hard to do that without putting Text in base, which I'm not
>>> sure anyone wants to do.
>>>
>>> Dan
>>>
>>> On Sat, Apr 21, 2012 at 8:20 PM, Greg Weber <[hidden email]> wrote:
>>>>
>>>> I would like to default IsString to use the Text instance to avoid
>>>> ambiguous type errors.
>>>> I see defaulting capability is available for Num. Is there any way to
>>>> do this for IsString?
>>>>
>>>> Thanks,
>>>> Greg Weber
>>>>
>>>> _______________________________________________
>>>> Glasgow-haskell-users mailing list
>>>> [hidden email]
>>>> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
>>>
>>>
>
> _______________________________________________
> Glasgow-haskell-users mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users



--
Markus Läll

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: default instance for IsString

Greg Weber
Thanks Markus, I think you have saved the day!
Even after googling for this extension and searching in the manual I
am still coming up pretty blank.
Is there somewhere I missed where this is documented or somewhere I
can contribute documentation?

On Sun, Apr 22, 2012 at 4:47 AM, Markus Läll <[hidden email]> wrote:
> ExtendedDefaultRules

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: default instance for IsString

Markus Läll-2
The core of it is in the GHC docs' overloaded strings section [1].

It could be clearer though -- reading about defaulting in the reports,
in the type defaulting section of GHC docs and in [1] can be a bit
confusing.

[1] http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#overloaded-strings

On Sun, Apr 22, 2012 at 4:54 PM, Greg Weber <[hidden email]> wrote:
> Thanks Markus, I think you have saved the day!
> Even after googling for this extension and searching in the manual I
> am still coming up pretty blank.
> Is there somewhere I missed where this is documented or somewhere I
> can contribute documentation?
>
> On Sun, Apr 22, 2012 at 4:47 AM, Markus Läll <[hidden email]> wrote:
>> ExtendedDefaultRules



--
Markus Läll

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: default instance for IsString

Greg Weber
so how can I update the documentation? I asked some of the most
experienced Haskell users at the Hackathon about this, and looked
through any documentation I could find and there was nothing
indicating I could do what you sent in your last message.

On Sun, Apr 22, 2012 at 8:15 AM, Markus Läll <[hidden email]> wrote:

> The core of it is in the GHC docs' overloaded strings section [1].
>
> It could be clearer though -- reading about defaulting in the reports,
> in the type defaulting section of GHC docs and in [1] can be a bit
> confusing.
>
> [1] http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#overloaded-strings
>
> On Sun, Apr 22, 2012 at 4:54 PM, Greg Weber <[hidden email]> wrote:
>> Thanks Markus, I think you have saved the day!
>> Even after googling for this extension and searching in the manual I
>> am still coming up pretty blank.
>> Is there somewhere I missed where this is documented or somewhere I
>> can contribute documentation?
>>
>> On Sun, Apr 22, 2012 at 4:47 AM, Markus Läll <[hidden email]> wrote:
>>> ExtendedDefaultRules
>
>
>
> --
> Markus Läll

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: default instance for IsString

Markus Läll-2
In reply to this post by Markus Läll-2
Isn't this a special case already?

I.e you already can default a string literal to anything you want, the
only thing needed is a language pragma (and more obvious docs). The
only "problem" is, that it's not part of the Haskell2010 report, and
thus not supported in other compilers (or is it?).

On Sun, Apr 22, 2012 at 8:51 PM, Johan Tibell <[hidden email]> wrote:

> On Sun, Apr 22, 2012 at 10:37 AM, Brent Yorgey <[hidden email]> wrote:
>> I do not think this is a bug.  Since type classes are open, GHC does
>> not do any reasoning of the form "X is the only instance in scope, so
>> I will pick that one".  Other instances could be added at any time
>> (perhaps in other modules).  In this particular instance, GHC has no
>> reason to choose the Text instance other than the fact that it is the
>> only instance in scope -- that is, type inference is not enough to
>> determine that the Text instance should be chosen.
>>
>> However, I do agree that it would be nice to have a mechanism for
>> specifying default instances for arbitrary (user-defined) type
>> classes.
>
> Couldn't we make a special case for IsString, like we do for Num,
> given it's special syntactic association with OverloadedStrings?
>
> -- Johan
>
> _______________________________________________
> Glasgow-haskell-users mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users



--
Markus Läll

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Loading...