25.10.2015 Views

Write You a Haskell Stephen Diehl

1kEcQTb

1kEcQTb

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

data Kind<br />

= KStar<br />

| KArr Kind Kind<br />

| KPrim<br />

| KVar Name<br />

Since the Core language is explicitly typed, it is trivial to implement an internal type checker for. Running<br />

the typechecker on the generated core is a good way to catch optimization, desugaring bugs, and<br />

determine if the compiler has produced invalid intermediate code.<br />

Type Classes<br />

Typeclasses are also remarkably subtle to implement. We will implement just single parameter typeclasses<br />

and use the usual dictionary passing translation when compiling the frontend to Core. Although the<br />

translation and instance search logic is not terribly complicated, it is however very verbose and involves<br />

a lot of bookkeeping about the global typeclass hierarchy.<br />

For example the following simplified Num typeclass generates quite a bit of elaborated definitions in the<br />

Core language to generate the dictionary and selector functions for the overloaded plus function.<br />

class Num a where<br />

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

mult :: a -> a -> a<br />

sub :: a -> a -> a<br />

instance Num Int where<br />

plus = plusInt<br />

mult = multInt<br />

sub = subInt<br />

plusInt :: Int -> Int -> Int<br />

plusInt (I# a) (I# b) = I# (plusInt# a b)<br />

is expands into the following set of Core definitions.<br />

plusInt :: Int -> Int -> Int<br />

plusInt = \(ds1 : Int)<br />

(ds2 : Int) -><br />

case ds1 of {<br />

I# ds8 -><br />

case ds2 of {<br />

I# ds9 -><br />

case (plusInt# ds8 ds9) of {<br />

__DEFAULT {ds5} -> (I# ds5)<br />

}<br />

115

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

Saved successfully!

Ooh no, something went wrong!