Updating records haskell


Unlike normal classes and type families, the instances of An excellent way to deal with nested data structures, and to alleviate the shortcomings of the Haskell record system, is to use one of the many Haskell lens libraries, such as lens, data-lens, fclabels and so on.These pair together a "getter" and a "setter" for a field (or more generally, a substructure of a larger type), allowing them to be composed as a single unit.Proposals for better systems are welcome, but while it is easy to propose a more powerful design in isolation, integrating it with other extensions and syntax in GHC is another matter!Unfortunately, the extension will not be ready for GHC 7.8, to allow time for the design to settle and the codebase changes to mature.People have been either using prefixing (foo Id and bar Id) or splitting up modules to solve this problem, which can both considered to be ugly hacks.



A prototype implementation, that works in GHC 7.6.3, shows how the classes are defined and gives examples of the instances that will be generated by GHC.Keep an eye out for the extension to land in GHC HEAD later this year, and please try it out and give your feedback!I'm very grateful to Simon Peyton Jones for acting as mentor for this project and as my guide to the GHC codebase.If a module does not export a field, clients of that module cannot use the can be used at two different record types, and the typechecker will figure out which type is meant, just like any other polymorphic function.

Similarly, functions can be polymorphic in the record type: extension does not attempt to generalise this syntax, so a record update expression will always refer to a single datatype, and if the field names do not determine the type uniquely, a type signature may be required. The type family is used so that the field type is a function of the data type and field name, which improves type inference.

However, it should land in HEAD soon after the 7.8 release is cut, so the adventurous are encouraged to build GHC and try it out.