25.10.2015 Views

Write You a Haskell Stephen Diehl

1kEcQTb

1kEcQTb

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Typeclass Declarations<br />

Typeclass declarations consist simply of the list of typeclass constraints, the name of the class, and the<br />

type variable ( single parameter only ). e body of the class is simply a sequence of scoped FunDecl<br />

declarations with only the matchType field.<br />

class [context] => classname [var] where<br />

[body]<br />

Consider a very simplified Num class.<br />

class Num a where<br />

plus :: a -> a -> a<br />

ClassDecl<br />

[]<br />

(Name ”Num”)<br />

[ Name ”a” ]<br />

[ FunDecl<br />

BindGroup<br />

{ _matchName = Name ”plus”<br />

, _matchPats = []<br />

, _matchType =<br />

Just<br />

(Forall<br />

[]<br />

[]<br />

(TArr<br />

(TVar TV { tvName = Name ”a” })<br />

(TArr<br />

(TVar TV { tvName = Name ”a” })<br />

(TVar TV { tvName = Name ”a” }))))<br />

, _matchWhere = []<br />

}<br />

]<br />

Typeclass instances follow the same pattern, they are simply the collection of instance constraints, the<br />

name of name of the typeclass, and the head of the type class instance type. e declarations are a<br />

sequence of FunDecl objects with the bodies of the functions for each of the overloaded function implementations.<br />

instance [context] => head where<br />

[body]<br />

For example:<br />

123

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!