Skip to content

Var Constructor Considered Harmful

xich edited this page Apr 30, 2013 · 3 revisions

There is a handy function:

varToCoreExpr :: Var -> CoreExpr

which always does the right thing when creating variable expressions. Depending on what the Var is, it will create: a value using the Var constructor, a type variable using (Type . TyVarTy), or a Coercion variable.

It is ok to pattern match on the Var constructor (in which case you know you have a value-level variable), but whenever you are building a CoreExpr, you should use varToCoreExpr. The exception is when you know you have a local value variable (an Id), maybe because you created it with newIdH, or found it with freeIdsT. Even then, using varToCoreExpr doesn't hurt.

Doing so has the nice knock-on effect that GHC's other higher-level functions Just Work.

Clone this wiki locally