[GHC] #11671: Allow labels starting with uppercase with OverloadedLabels
GHC
ghc-devs at haskell.org
Sat Mar 5 00:14:24 UTC 2016
#11671: Allow labels starting with uppercase with OverloadedLabels
-------------------------------------+-------------------------------------
Reporter: inaki | Owner:
Type: feature request | Status: new
Priority: normal | Milestone:
Component: Compiler | Version: 8.0.1-rc1
(Parser) |
Resolution: | Keywords: ORF
Operating System: Unknown/Multiple | Architecture:
| Unknown/Multiple
Type of failure: None/Unknown | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s):
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by inaki):
Replying to [comment:6 mpickering]:
> If you want these values to actually look like constructors then have
you tried using pattern synonyms?
Yes, indeed, that is what I do now. (For context, this is for
https://github.com/haskell-gi/haskell-gi , a set of autogenerated
libraries for a number of libraries in the gtk ecosystem.) The following
works beautifully:
{{{#!hs
{-# LANGUAGE DataKinds, FlexibleInstances,
MultiParamTypeClasses,
PatternSynonyms, ViewPatterns, ScopedTypeVariables, KindSignatures,
TypeApplications #-}
import GHC.TypeLits
class IsOverloadedPattern (tag :: Symbol) (a :: *) where
checkOverloadedPattern :: a -> Bool
buildOverloadedPattern :: a
pattern Truish :: IsOverloadedPattern "Truish" a => a
pattern Truish <- ((checkOverloadedPattern @"Truish") -> True) where
Truish = buildOverloadedPattern @"Truish"
data Statement = Provable | Refutable
instance IsOverloadedPattern "Truish" Statement where
checkOverloadedPattern Provable = True
checkOverloadedPattern Refutable = False
buildOverloadedPattern = Provable
test :: Statement -> Int
test Truish = 42
test _ = -1
main :: IO ()
main = print (test Truish)
}}}
The problem is that this requires one pattern for each constructor one
wants to overload in this way. Which are quite a few hundred/thousand for
a large library like gtk (or glib, webkit, etc.). Not an issue in itself,
but we cannot ask the user of the library to write them by hand, they
should be automatically in the namespace when importing `GI.Gtk` or any
other autogenerated binding. And we cannot bundle this with the library
directly, since patterns with the same name may be easily generated by
different autogenerated libraries, giving rise to name clashes.
The way we solve it now is by asking the user of the library to run some
command such that all possible such patterns, for all libraries used in
the project, are generated in advance, and compiled into a single module.
The resulting module should then be compiled as part of the users's
project. Which works, but it is clunky.
In similar situations (overloaded property names and overloaded signals)
we can do away completely with this somewhat annoying "overloading module"
by using OverloadedLabels, but I don't currently see a way of doing the
same thing allowing to pattern match against the overloaded symbols. The
construction in #comment:4 comes close, perhaps it can be made to work
somehow.
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/11671#comment:7>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the ghc-tickets
mailing list