[GHC] #11805: Ability to use record fields for automatic derivation of user-defined classes.

#11805: Ability to use record fields for automatic derivation of user-defined classes. -------------------------------------+------------------------------------- Reporter: Tientuine | Owner: Type: feature | Status: new request | Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- I propose that the compiler be empowered to automatically derive a user- defined class when a data type has fields that match (in both name and type) methods sufficient for a minimal complete definition of the class. (There is some overlap between this idea and the much larger and more contentious issue of extensible records generally.) I am relatively new to Haskell, but have quickly run into the problem of field namespacing. As a simple example, suppose we are creating a game with data types for Room and Item, and values of each of these types will have a short name and a long description (possibly among other fields not detailed here). Currently, we must name our fields differently, as in: {{{#!hs -- ugh! prefixing the field names is ugly and tedious data Room = Room { rname :: String, rdescribe :: String, item :: Maybe Item } data Item = Item { iname :: String, idescribe :: String, value :: Int } }}} Classes seem a natural solution to this problem, but then we must either have superfluous field names or drop the use of field names altogether: {{{#!hs -- extracting a common interface as a class makes sense type Desc d where name :: d -> String describe :: d -> String -- but now we either keep the ugly prefixed fields or drop fields entirely data Room = Room String String (Maybe Item) data Item = Item String String Int -- furthermore, it is tedious to implement these trivial observers instance Desc Room where name (Room n _ _) = n describe (Room _ d _) = d instance Desc Item where name (Item n _ _) = n describe (Item _ d _) = d }}} My proposal is to allow the compiler to rely on fields in order to automatically derive a user-defined class. This solves a couple of problems in a way that (I hope) is not excessively complex to implement. Such a feature would allow us to write code like the following: {{{#!hs -- class as a common interface still feels natural type Desc d where name :: d -> String describe :: d -> String -- now we get shared field names and let the compiler do some of the work data Room = Room { name :: String, describe :: String, item :: (Maybe Item) } deriving Desc data Item = Item { name :: String, describe :: String, value :: Int } deriving Desc }}} Obviously, there are a couple of restrictions here: * fields of the same name in different records must have the same type * fields of the same name in different records are still ambiguous (no different than the current situation) if we do //not// derive a class that declares those field names * this is //not// intended to be a general solution for either extensible records or deriving classes automatically, it is very limited in scope P.S. This is my first Ticket and attempt to get involved in the community, and I'm not yet sure of the correct tags to use. Please be gentle. :) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11805 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11805: Ability to use record fields for automatic derivation of user-defined classes. -------------------------------------+------------------------------------- Reporter: Tientuine | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 Resolution: | Keywords: 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 ezyang): Hello Tientuine, this area of Haskell is definitely well-trodden with proposals. One of the proposals, OverloadedRecordFields will be partially in GHC 8.0 (though not with type class deriving.) How does your proposal compare? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11805#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11805: Ability to use record fields for automatic derivation of user-defined classes. -------------------------------------+------------------------------------- Reporter: Tientuine | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 Resolution: | Keywords: 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 Tientuine): I appreciate the prompt reply. I was not aware of the OverloadedRecordFields proposal (at least, not the third part). I had done a bit of research using the Wiki page on ExtensibleRecords as a starting point, but this proposal was not mentioned there. It seems that Parts 1 and 3 of OverloadedRecords together accomplish everything I was looking for (and more, of course) from my own proposal. By making a subclass of two HasField magic-classes, it would be easy to define a class like //Desc// from my example above, which comprises multiple fields and has a name that is lighter-weight and domain-specific. Obviously, it's a big win over my proposal that we would still be able to use the field-update syntax. Since I don't see any advantage of my proposal over the functionality provided by OverloadedRecordFields, I suppose this Ticket can be closed (unless someone else sees any value in it). Should I close it myself? Not sure about the protocol. I'm looking forward to the completed OverloadedRecordFields proposal! -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11805#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11805: Ability to use record fields for automatic derivation of user-defined classes. -------------------------------------+------------------------------------- Reporter: Tientuine | Owner: Type: feature request | Status: closed Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 Resolution: duplicate | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by thomie): * status: new => closed * resolution: => duplicate Comment: Replying to [comment:2 Tientuine]:
It seems that Parts 1 and 3 of OverloadedRecords together accomplish everything I was looking for
I suppose this Ticket can be closed
-- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11805#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC