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.

(+) : Int → Int → Int<br />

(×) : Int → Int → Int<br />

(−) : Int → Int → Int<br />

(=) : Int → Int → Bool<br />

Literals<br />

e type of literal integer and boolean types is trivially their respective types.<br />

Γ ⊢ n : Int<br />

Γ ⊢ True : Bool<br />

Γ ⊢ False : Bool<br />

(T-Int)<br />

(T-True)<br />

(T-False)<br />

Constraint Generation<br />

e previous implementation of Hindley Milner is simple, but has this odd property of intermingling<br />

two separate processes: constraint solving and traversal. Let’s discuss another implementation of the<br />

inference algorithm that does not do this.<br />

In the constraint generation approach, constraints are generated by bottom-up traversal, added to a ordered<br />

container, canonicalized, solved, and then possibly back-substituted over a typed AST. is will be the<br />

approach we will use from here out, and while there is an equivalence between the “on-line solver”, using<br />

the separate constraint solver becomes easier to manage as our type system gets more complex and we<br />

start building out the language.<br />

Our inference monad now becomes a RWST ( Reader-<strong>Write</strong>r-State Transformer ) + Except for typing<br />

errors. e inference state remains the same, just the fresh name supply.<br />

-- | Inference monad<br />

type Infer a = (RWST<br />

Env<br />

-- Typing environment<br />

[Constraint] -- Generated constraints<br />

InferState -- Inference state<br />

(Except<br />

-- Inference errors<br />

TypeError)<br />

a) -- Result<br />

-- | Inference state<br />

data InferState = InferState { count :: Int }<br />

Instead of unifying type variables at each level of traversal, we will instead just collect the unifiers inside<br />

the <strong>Write</strong>r and emit them with the uni function.<br />

92

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

Saved successfully!

Ooh no, something went wrong!