Type Classes� an Exploration of the Design Space

Type Classes� an Exploration of the Design Space

Type classes an exploration of the design space Simon Peyton Jones Mark Jones University of Glasgow and Oregon Graduate Institute University of Nottingham Erik Meijer University of Utrecht and Oregon Graduate Institute May Abstract to Haskell can b e a painful exp erience One feature that is particularly often missed is multiparameter type classes Section explains why When type classes were rst introduced in Haskell they were regarded as a fairly exp erimental language feature and The obvious question is whether there is an upward therefore warranted a fairly conservative design Since that compatible way to extend Haskells class system to enjoy time practical exp erience has convinced many programmers some or all of the expressiveness that Gofer provides and of the b enets and convenience of type classes However on p erhaps some more b esides The main b o dy of this pap er o ccasion these same programmers have discovered examples explores this question in detail It turns out that there are where seemingly natural applicatio ns for type class overload a number of interlocking design decisions to b e made Gofer ing are prevented by the restrictions imp osed by the Haskell and Haskell each embo dy a particular set but it is very design useful to tease them out indep endently and see how they interact Our goal is to explore the design space as clearly It is p ossible to extend the type class mechanism of Haskell as p ossible laying out the choices that must b e made and in various ways to overcome these limitations but such pro the factors that aect them rather than prescribing a par p osals must b e designed with great care For example sev ticular solution Section We nd that the design space eral dierent extensions have b een implemented in Gofer is rather large we identify nine separate design decisions Some of these particularly the supp ort for multiparameter each of which has two or more p ossible choices though not classes have proved to b e very useful but interactions b e all combinations of choices make sense In the end however tween other asp ects of the design have resulted in a type we do oer our own opinion ab out a sensible set of choices system that is b oth unsound and undecidable Another illus Section tration is the introduction of constructor classes in Haskell which came without the prop er generalizatio n of the no A new language feature is only justiable if it results in a tion of a context As a consequence certain quite reasonable simplicati on or unication of the original language design programs are not typable or if the extra expressiveness is truly useful in practice One contribution of this pap er is to collect together a fairly large In this pap er we review the rationale b ehind the design of set of examples that motivate various extensions to Haskells Haskells class system we identify some of the weaknesses type classes in the current situation and we explain the choices that we face in attempting to remove them Why multiparameter type classes Introduction The most visible extension to Haskell type classes that we discuss is supp ort for multiparameter type classes The p os Type classes are one of the most distinctive features of sibility of multiparameter type classes has b een recognised Haskell Hudak et al They have b een used for an since the original pap ers on the sub ject Kaes Wadler 1 impressive variety of application s and Haskell signif Blott and Gofer has always supp orted them icantly extended their expressiveness by introducing con structor classes Jones a This section collects together examples of multiparameter type classes that we have encountered None of them are All programmers want more than they are given and many new none will b e surprising to the cognescenti and many p eople have bump ed up against the limitation s of Haskells have app eared inter alia in other pap ers Our purp ose in class system Another language Gofer Jones that collecting them is to provide a shared database of motivating has developed in parallel with Haskell enjoys a much more examples We would welcome new contributions lib eral and expressive class system This expressiveness is denitely b oth useful and used and transferring from Gofer 1 The current iteration of the Haskell language is Haskell but it is identical to Haskell in all resp ects relevant to this pap er Overloading with coupled parameters newtype Env e a Env e a instance ReaderMonad Env e e where Concurrent Haskell Peyton Jones Gordon Finne introduces a number of types such as mutable variables Work in Glasgow and the Oregon Graduate Institute MutVar synchronised mutable variables MVar channel on hardware description languages has led to class dec variables CVar communication channels Channel and skip larations similar to this channels SkipChan all of which come with similar op era tions that take the form class Monad ct Hard ct sg where newX a IO X a const a ct sg a getX X a IO a op a b sg a ct sg b putX X a a IO op a b c sg a sg b ct s c where X ranges over MVar etc Here are similar op erations instance Hard NetCircuit NetSignal where in the standard state monad instance Hard SimCircuit SimSignal where newST a ST s MutableVar s a Here the circuit constructor ct is a monad while getST MutableVar s a ST s a the signal constructor sg serves to distinguish values putST MutableVar s a a ST s available at circuitconstruction time of type Int say These are manifestly candidates for overloading yet a single from those owing along the wires at circuitexecution parameter type class cant do the trick The trouble is that time of type SimSignal Int say Each instance of in each case the monad type and the reference type come as Hard gives a dierent interpretation of the circuit for a pair IO MutVar and ST s MutableVar s What we example one might pro duce a net list while another want is a multiple parameter class that abstracts over b oth might simulate the circuit Like the VarMonad example the instance type come as class Monad m VarMonad m v where a pair it would make no sense to give an instance for new a m v a Hard NetCircuit SimSignal get v a m a put v a a m 2 The Haskell prelude denes denes the following two functions for reading and writing les instance VarMonad IO MutVar where instance VarMonad ST s MutableVar s where readFile FilePath IO String writeFile FilePath String IO This is quite a common pattern in which a twoparameter type class is needed b ecause the class signature is really over Similar functions can b e dened for many more pairs a tuple of types and where instance declarations capture di of device handles and communicatable types such as rect relationshi ps b etween sp ecic tuples of type construc mice buttons timers windows rob ots etc tors We call this overloading with coupled parameters Here are a number of other examples we have collected readMouse Mouse IO MouseEvent readButton Button IO readTimer Timer IO Float The class StateMonad Jones carries the state around naked instead of inside a container as in the sendWindow Window Picture IO VarMonad example sendRobot Robot Command IO sendTimer Timer Float IO class Monad m StateMonad m s where getS m s These functions are quite similar to the metho ds putS s m get VarMonad r m r a m a and put VarMonad r m r a a m of the VarMonad Here the monad m carries along a state of type s getS family except that here the monad m is xed to IO and extracts the state from the monad and putS overwrites the choice of the value type a is coupled with the b ox the state with a new value One can then dene in type v a So what we need here is a multiparameter stances of StateMonad class that overloads on v a and a instead newtype State s a State s as class IODevice handle a where instance StateMonad State s s where receive handle IO a send handle a IO a Notice the coupling b etween the parameters arising from the rep eated type variable s Jones also Perhaps one could go one step further and unify class denes a related class ReaderMonad that describ es IODevice r a and class Monad m StateMonad m computations that read some xed environment r into a three parameter class class Monad m Device m r a class Monad m ReaderMonad m e where env e m a m a 2 This example was suggested by Enno Scholz getenv m e 3 An app ealing application of type classes is to de serves as a ho ok either for one of the other arguments or scrib e mathematical structures such as groups elds for the instance context and member functions to use monoids and so on But it is not long b efore the need The parametric type classes of Chen Hudak Odersky for coupled overloading arises For example also deal quite nicely with the bulktypes example but their assymetry do es not suit the examples of the pre class Field k AdditiveGroup a vious section so well A full discussion of the design choices VectorSpace k a where for a bulktypes library is contained in Peyton Jones k a a Type relations Here the op erator multiplies a vector by a scalar One can also construct applications for multiparameter classes where the relationshi ps b etween dierent parame Overloading with constrained parame ters are much lo oser than in the examples that we have ters seen ab ove After all in the most general setting a multi parameter type class C could b e used to represent an arbi Libraries that implement sets bags lists nite maps and so trary relation b etween types where for example a b is in on all use similar functions empty

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    16 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us