[GHC] #8171: Extending ExtendedDefaultRules

GHC ghc-devs at haskell.org
Wed Aug 28 14:50:31 UTC 2013


#8171: Extending ExtendedDefaultRules
-------------------------------------+------------------------------------
        Reporter:  ekmett            |            Owner:
            Type:  feature request   |           Status:  new
        Priority:  normal            |        Milestone:
       Component:  Compiler          |          Version:  7.6.3
      Resolution:                    |         Keywords:
Operating System:  Unknown/Multiple  |     Architecture:  Unknown/Multiple
 Type of failure:  None/Unknown      |       Difficulty:  Unknown
       Test Case:                    |       Blocked By:
        Blocking:                    |  Related Tickets:
-------------------------------------+------------------------------------

Comment (by ekmett):

 No more so than someone's instance of `Show` messing up another's.

 Consider Shae Erisson's `ghclive` from last year's GSoC. There we're
 running a ghci analogue in the web browser, but we want to support doing
 things like rendering diagrams using Brent Yorgey's diagrams tool, so we
 have our own class of things we can show on the web.

 {{{
 class Display a where
     displayList :: [a] -> DisplayResult
     displayList = displayListOf display

     display :: a -> DisplayResult
     default display :: (Generic a, GDisplay (Rep a)) => a -> DisplayResult
     display = gdisplay . from
 }}}

 This is very much like `Show`, but it an include embedded images,
 hyperlinks, etc. as it is ultimately destined for the web.

 We can tell hint to infer for it by wrapping whatever expression we get
 from the user in a call to `display`, but it won't do defaulting.

 To fool `hint` into defaulting we have to add a completely unnecessary
 constraint to the environment to make the call elegible for defaulting! Of
 those, Show is the most innocuous, so we instead have to

 {{{
 displaying :: (Display a, Show a) => a -> DisplayResult
 displaying = display
 }}}

 We then infer for it by wrapping the expression the user supplied in a
 'displaying' call.

 https://github.com/shapr/ghclive/blob/master/src-main/Main.hs#L214

 But that is a lie. We don't need Show. We don't want Show. We just need to
 fake Show even for a number of un-Show-able things like diagrams to make
 GHC happy.

 Worse, the `displaying` function used to have to be a separate function
 rather than be placed as a superclass of `Display` because GHC wouldn't
 rummage through the superclasses when figuring out if extended defaulting
 should fire, so we not only had to have an unnecessary Show constraint,
 but it had to be on the 'outside' of the current constraint set. That
 issue appears to have since been fixed though, so it should be possible
 with the addition of Defaulting to say:

 {{{
 class Defaulting a => Display a where
 }}}

 or to say the more targeted:

 {{{
 display :: (Display a, Defaulting a) => a -> DisplayResult
 }}}

 I'm open to other solutions. The instance seems somewhat clunky. In many
 ways maybe what you really kind of want is something like the roles
 annotations where you annotate the class arguments you want to allow
 defaulting on. That may be a way to address the current discussion about
 `IsString`, and could be a vehicle for enabling limited defaulting in the
 presence of MPTCs, but it was a lot more engineering effort than I felt
 comfortable asking for! =)

-- 
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/8171#comment:4>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler




More information about the ghc-tickets mailing list