ghc-7.8.20140130: The GHC API

Safe HaskellNone
LanguageHaskell98

CoreUtils

Contents

Description

Commonly useful utilites for manipulating the Core language

Synopsis

Constructing expressions

mkCast :: CoreExpr -> Coercion -> CoreExprSource

Wrap the given expression in the coercion safely, dropping identity coercions and coalescing nested coercions

mkTick :: Tickish Id -> CoreExpr -> CoreExprSource

Wraps the given expression in the source annotation, dropping the annotation if possible.

bindNonRec :: Id -> CoreExpr -> CoreExpr -> CoreExprSource

bindNonRec x r b produces either:

 let x = r in b

or:

 case r of x { _DEFAULT_ -> b }

depending on whether we have to use a case or let binding for the expression (see needsCaseBinding). It's used by the desugarer to avoid building bindings that give Core Lint a heart attack, although actually the simplifier deals with them perfectly well. See also mkCoreLet

needsCaseBinding :: Type -> CoreExpr -> BoolSource

Tests whether we have to use a case rather than let binding for this expression as per the invariants of CoreExpr: see "CoreSyn#let_app_invariant"

mkAltExprSource

Arguments

:: AltCon

Case alternative constructor

-> [CoreBndr]

Things bound by the pattern match

-> [Type]

The type arguments to the case alternative

-> CoreExpr 

This guy constructs the value that the scrutinee must have given that you are in one particular branch of a case

Taking expressions apart

findDefault :: [(AltCon, [a], b)] -> ([(AltCon, [a], b)], Maybe b)Source

Extract the default case alternative

findAlt :: AltCon -> [(AltCon, a, b)] -> Maybe (AltCon, a, b)Source

Find the case alternative corresponding to a particular constructor: panics if no such constructor exists

mergeAlts :: [(AltCon, a, b)] -> [(AltCon, a, b)] -> [(AltCon, a, b)]Source

Merge alternatives preserving order; alternatives in the first argument shadow ones in the second

trimConArgs :: AltCon -> [CoreArg] -> [CoreArg]Source

Given:

 case (C a b x y) of
        C b x y -> ...

We want to drop the leading type argument of the scrutinee leaving the arguments to match agains the pattern

filterAltsSource

Arguments

:: [Unique]

Supply of uniques used in case we have to manufacture a new AltCon

-> Type

Type of scrutinee (used to prune possibilities)

-> [AltCon]

imposs_cons: constructors known to be impossible due to the form of the scrutinee

-> [(AltCon, [Var], a)]

Alternatives

-> ([AltCon], Bool, [(AltCon, [Var], a)]) 

Properties of expressions

exprType :: CoreExpr -> TypeSource

Recover the type of a well-typed Core expression. Fails when applied to the actual Type expression as it cannot really be said to have a type

coreAltType :: CoreAlt -> TypeSource

Returns the type of the alternatives right hand side

coreAltsType :: [CoreAlt] -> TypeSource

Returns the type of the first alternative, which should be the same as for all alternatives

exprIsHNF :: CoreExpr -> BoolSource

exprIsHNF returns true for expressions that are certainly already evaluated to head normal form. This is used to decide whether it's ok to change:

 case x of _ -> e

into:

 e

and to decide whether it's safe to discard a seq.

So, it does not treat variables as evaluated, unless they say they are. However, it does treat partial applications and constructor applications as values, even if their arguments are non-trivial, provided the argument type is lifted. For example, both of these are values:

 (:) (f x) (map f xs)
 map (...redex...)

because seq on such things completes immediately.

For unlifted argument types, we have to be careful:

 C (f x :: Int#)

Suppose f x diverges; then C (f x) is not a value. However this can't happen: see "CoreSyn#let_app_invariant". This invariant states that arguments of unboxed type must be ok-for-speculation (or trivial).

exprOkForSpeculation :: Expr b -> BoolSource

exprOkForSpeculation returns True of an expression that is:

  • Safe to evaluate even if normal order eval might not evaluate the expression at all, or
  • Safe not to evaluate even if normal order would do so

It is usually called on arguments of unlifted type, but not always In particular, Simplify.rebuildCase calls it on lifted types when a 'case' is a plain seq. See the example in Note [exprOkForSpeculation: case expressions] below

Precisely, it returns True iff:

  • The expression guarantees to terminate,
  • soon,
  • without raising an exception,
  • without causing a side effect (e.g. writing a mutable variable)

Note that if exprIsHNF e, then exprOkForSpecuation e. As an example of the considerations in this test, consider:

 let x = case y# +# 1# of { r# -> I# r# }
 in E

being translated to:

 case y# +# 1# of { r# ->
    let x = I# r#
    in E
 }

We can only do this if the y + 1 is ok for speculation: it has no side effects, and can't diverge or raise an exception.

exprOkForSideEffects :: Expr b -> BoolSource

exprOkForSpeculation returns True of an expression that is:

  • Safe to evaluate even if normal order eval might not evaluate the expression at all, or
  • Safe not to evaluate even if normal order would do so

It is usually called on arguments of unlifted type, but not always In particular, Simplify.rebuildCase calls it on lifted types when a 'case' is a plain seq. See the example in Note [exprOkForSpeculation: case expressions] below

Precisely, it returns True iff:

  • The expression guarantees to terminate,
  • soon,
  • without raising an exception,
  • without causing a side effect (e.g. writing a mutable variable)

Note that if exprIsHNF e, then exprOkForSpecuation e. As an example of the considerations in this test, consider:

 let x = case y# +# 1# of { r# -> I# r# }
 in E

being translated to:

 case y# +# 1# of { r# ->
    let x = I# r#
    in E
 }

We can only do this if the y + 1 is ok for speculation: it has no side effects, and can't diverge or raise an exception.

exprIsBig :: Expr b -> BoolSource

Returns True of expressions that are too big to be compared by cheapEqExpr

exprIsConLike :: CoreExpr -> BoolSource

Similar to exprIsHNF but includes CONLIKE functions as well as data constructors. Conlike arguments are considered interesting by the inliner.

rhsIsStatic :: Platform -> (Name -> Bool) -> CoreExpr -> BoolSource

This function is called only on *top-level* right-hand sides. Returns True if the RHS can be allocated statically in the output, with no thunks involved at all.

Expression and bindings size

exprSize :: CoreExpr -> IntSource

A measure of the size of the expressions, strictly greater than 0 It also forces the expression pretty drastically as a side effect Counts *leaves*, not internal nodes. Types and coercions are not counted.

data CoreStatsSource

Constructors

CS 

Fields

cs_tm :: Int
 
cs_ty :: Int
 
cs_co :: Int
 

Equality

cheapEqExpr :: Expr b -> Expr b -> BoolSource

A cheap equality test which bales out fast! If it returns True the arguments are definitely equal, otherwise, they may or may not be equal.

See also exprIsBig

Eta reduction

Manipulating data constructors and types

applyTypeToArgs :: CoreExpr -> Type -> [CoreExpr] -> TypeSource

A more efficient version of applyTypeToArg when we have several arguments. The first argument is just for debugging, and gives some context

applyTypeToArg :: Type -> CoreExpr -> TypeSource

Determines the type resulting from applying an expression with given type to a given argument expression