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.

e expression or Expr type is the core AST type that we will deal with and transform most frequently.<br />

is is effectively a simple untyped lambda calculus with let statements, pattern matching, literals, type<br />

annotations, if/these/else statements and do-notation.<br />

type Constr = Name<br />

data Expr<br />

= EApp Expr Expr -- ^ a b<br />

| EVar Name -- ^ x<br />

| ELam Name Expr -- ^ \\x . y<br />

| ELit Literal -- ^ 1, ’a’<br />

| ELet Name Expr Expr -- ^ let x = y in x<br />

| EIf Expr Expr Expr -- ^ if x then tr else fl<br />

| ECase Expr [Match] -- ^ case x of { p -> e; ... }<br />

| EAnn Expr Type -- ^ ( x : Int )<br />

| EDo [Stmt] -- ^ do { ... }<br />

| EFail -- ^ pattern match fail<br />

deriving (Eq, Show)<br />

Inside of case statements will be a distinct pattern matching syntax, this is used both at the toplevel<br />

function declarations and inside of case statements.<br />

data Match = Match<br />

{ _matchPat :: [Pattern]<br />

, _matchBody :: Expr<br />

, _matchGuard :: [Guard]<br />

} deriving (Eq, Show)<br />

data Pattern<br />

= PVar Name -- ^ x<br />

| PCon Constr [Pattern] -- ^ C x y<br />

| PLit Literal -- ^ 3<br />

| PWild -- ^ _<br />

deriving (Eq, Show)<br />

e do-notation syntax is written in terms of two constructions, one for monadic binds and the other<br />

for monadic statements.<br />

data Stmt<br />

= Generator Pattern Expr -- ^ pat

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

Saved successfully!

Ooh no, something went wrong!