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.

eturn (TArr l (setLoc l ty) tv)<br />

Lit l _ -> return (setLoc l typeInt)<br />

Now specifically at the call site of our unification solver, if we encounter a unification fail we simply<br />

pluck the location information off the two type terms and plug it into the type error fields.<br />

unifies t1 t2 | t1 == t2 = return emptyUnifer<br />

unifies (TVar _ v) t = v ‘bind‘ t<br />

unifies t (TVar _ v) = v ‘bind‘ t<br />

unifies (TArr _ t1 t2) (TArr _ t3 t4) = unifyMany [t1, t2] [t3, t4]<br />

unifies (TCon _ a) (TCon _ b) | a == b = return emptyUnifer<br />

unifies t1 t2 = throwError $ UnificationFail t1 (getLoc t1) t2 (getLoc t2)<br />

bind :: TVar -> Type -> Solve Unifier<br />

bind a t<br />

| eqLoc t a = return (emptySubst, [])<br />

| occursCheck a t = throwError $ InfiniteType a (getLoc t) t<br />

| otherwise = return $ (Subst $ Map.singleton a t, [])<br />

So now we can explicitly trace the provenance of the specific constraints that gave rise to a given type<br />

error all the way back to the source that generated them.<br />

Cannot unify types:<br />

Int<br />

Introduced at line 27 column 5<br />

f 2 3<br />

with<br />

Int -> c<br />

Introduced at line 5 column 9<br />

let f x y = x y<br />

is is of course the simplest implementation of the this tracking method and could be further extended<br />

by giving an weighted ordering to the constraints based on their likelihood of importance and proximity<br />

and then choosing which location to report based on this information. is remains an open area of<br />

work.<br />

Indentation<br />

<strong>Haskell</strong>’s syntax uses indentation blocks to delineated sections of code. is use of indentation sensitive<br />

layout to convey the structure of logic is sometimes called the offside rule in parsing literature. At the<br />

beginning of “laidout” block the first declaration or definition can start in any column, and the parser<br />

marks that indentation level. Every subsequent top-level declaration must have the same indentation.<br />

136

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

Saved successfully!

Ooh no, something went wrong!