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.

let f x = x + 1<br />

Let ”f” [] (Lam ”x” (Op Add [Var ”x”, Lit (LitInt 1)]))<br />

Inference will generate a system of constraints which are solved via a process known as unification to yield<br />

the type of the expression.<br />

Int -> Int -> Int ~ a -> b<br />

b ~ Int -> c<br />

f :: Int -> Int<br />

In some cases this type will be incorporated directly into the AST and the inference will transform the<br />

frontend language into an explicitly typed core language.<br />

Let ”f” []<br />

(Lam ”x”<br />

(TArr TInt TInt)<br />

(App<br />

(App<br />

(Prim ”primAdd”) (Var ”x”))<br />

(Lit (LitInt 1))))<br />

Transformation<br />

e type core representation is often suitable for evaluation, but quite often different intermediate representations<br />

are more amenable to certain optimizations and make various semantic properties of the<br />

language explicit. ese kind of intermediate forms will often attach information about free variables,<br />

allocations, and usage information directly in the AST structure.<br />

e most important form we will use is called the Spineless Tagless G-Machine ( STG ), an abstract machine<br />

that makes many of the properties of lazy evaluation explicit directly in the AST.<br />

Code Generation<br />

From the core language we will either evaluate it on top of a high-level interpreter written in <strong>Haskell</strong><br />

itself, or into another intermediate language like C or LLVM which can itself be compiled into native<br />

code.<br />

let f x = x + 1<br />

Quite often this process will involve another intermediate representation which abstracts over the process<br />

of assigning and moving values between CPU registers and main memory. LLVM and GHC’s Cmm are<br />

two target languages serving this purpose.<br />

12

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

Saved successfully!

Ooh no, something went wrong!