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.

]<br />

TokenSym ”x”,<br />

TokenAdd,<br />

TokenNum 1<br />

We can then scan the token stream via and dispatch on predefined patterns of tokens called productions<br />

and recursively builds up the syntax datatype for the abstract syntax tree (AST) by traversal of the input<br />

stream.<br />

type Name = String<br />

data Expr<br />

= Var Name<br />

| Lit Lit<br />

| Op PrimOp [Expr]<br />

| Let Name [Name] Expr<br />

data Lit<br />

= LitInt Int<br />

data PrimOp<br />

= Add<br />

So for example the following string is parsed into the resulting Expr value.<br />

let f x = x + 1<br />

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

Desugaring<br />

Desugaring is the process by which the frontend AST is transformed into a simpler form of itself by<br />

reducing the number of complex structures by expressing them in terms of a fixed set of simpler constructs.<br />

<strong>Haskell</strong>’s frontend is very large and many constructs are simplified down. For example where clauses<br />

and operator sections are the most common examples. Where clauses are effectively syntactic sugar for<br />

let bindings and operator sections are desugared into lambdas with the left or right hand side argument<br />

assigned to a fresh variable.<br />

Type Inference<br />

Type inference is the process by which the untyped syntax is endowed with type information by a process<br />

known as type reconstruction or type inference. e inference process may take into account explicit type<br />

annotations.<br />

11

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

Saved successfully!

Ooh no, something went wrong!