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.

-- Desugared<br />

f x = case x of<br />

Just _a -> case _a of<br />

Just _b -> _b<br />

ere are many edge cases of pattern matching that we will have to consider. e confluence of all them<br />

gives rise to a rather complex set of AST rewrites:<br />

• Multiple arguments<br />

• Overlapping patterns<br />

• Literal patterns<br />

• Nested patterns<br />

• Non-exhaustive equations<br />

• Conditional equations<br />

• Non-linear patterns<br />

On top of pattern matching we will implement the following more trivial syntactic sugar translations:<br />

• Expand if/then statements into case expressions.<br />

• Expand pattern guards into case expressions.<br />

• Expand out do-notation for monads.<br />

• Expand list syntactic sugar.<br />

• Expand tuple syntactic sugar.<br />

• Expand out operator sections.<br />

• Expand out string literals.<br />

• Expand out numeric literals.<br />

We will however punt on an important part of the <strong>Haskell</strong> specification, namely overloaded literals. In<br />

GHC <strong>Haskell</strong> numeric literals are replaced by specific functions from the Num or Fractional typeclasses.<br />

-- Frontend<br />

42 :: Num a => a<br />

3.14 :: Fractional a => a<br />

-- Desugared<br />

fromInteger (42 :: Integer)<br />

fromRational (3.14 :: Rational)<br />

We will not implement this, as it drastically expands the desugarer scope.<br />

We will however follow GHC’s example in manifesting unboxed types are first class values in the language<br />

so literals that they appear in the AST rewritten in terms of the wired-in constructors (Int#, Char#,<br />

Addr#, etc).<br />

113

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

Saved successfully!

Ooh no, something went wrong!