public class ANormalize extends ExpExpVisitor<gnu.expr.ANormalize.Context>
1. Monadic conversion:In the actual Java code "return" operation is called "identity", while the "bind" operation is called "normalizeName" as in the Flanagan et al. paper. The ExpVisitor type matching mechanism replaces the role of the "match" in the paper, while the Context class replaces the "k" parameter. Lambdas are simulated with classes for backward compatibility with Java version 7 and lower. Each visit[...]Exp method is called with two parameters, an expression and a context (the very first context is the identity function that returns its argument). If the visit method is called with a non-atomic expression a new context is created and the passed context is called only inside the new one. The new-context is then passed to "normalizeName" that has two purposes: 1. to create a context, that generates a "let" expression to let-bind the expression next to come in the "visiting" process; 2. to call visit() on the passed expression, to continue the syntax tree traversing. This chain will finish when a leaf (an atomic expression) is encountered in the tree, in this case the passed context is invoked (which in turn will invoke the previous context and so on). At this point the chain of context invocations starts to wrap each expression in a "let" binding, processing the expressions backward, and enclosing them step by step in nested "let" expressions. This backward traversing stops when the context called is the identity function. When the expression to normalize is a conditional, "normalizeTerm" is used on each branch expression. Instead of creating a let binding for each branch, as they cannot be evaluated before the test outcome, "normalizeTerm" calls the visit method with the identity context, restarting the normalization in each branch.becomes:(+ 1 (if (>= x 0) (f x) 0))
2. The result is interpreted in the Identity monad:(bind (if (>= x 0) (f x) (return 0)) (lambda (t) (+ 1 t)))
(return a) becomes: a (bind a (lambda (x) b)) becomes: (let ((x a)) b)(bind (if (>= x 0) (f x) (return 0)) (lambda (t) (+ 1 t)))becomes:(let ((t (if (>= x 0) (f x) (return 0)))) (+ 1 t))3. Nested let are flattened:(let ((x (let ((y a)) (let ((y a)) b))) becomes: (let ((x b)) c) c))
SourceLocator.Simple
currentLambda, exitValue, messages
Constructor and Description |
---|
ANormalize() |
Modifier and Type | Method and Description |
---|---|
static void |
aNormalize(Expression exp,
Compilation comp) |
protected static boolean |
isAtomic(Expression exp)
Determines if an Expression is atomic, that is if it needs to
be normalized or not.
|
protected Expression |
normalizeName(Expression exp,
gnu.expr.ANormalize.Context context)
Performs the bind operation, introducing a new let expression
to capture the result of non-trivial expressions, which is bound
to a new let variable.
|
protected Expression |
normalizeNames(Expression[] exps,
int index,
gnu.expr.ANormalize.MultiContext context)
Deals with the normalization of multiple expressions.
|
protected Expression |
normalizeTerm(Expression exp)
Starts the normalization of expression exp.
|
protected CatchClause |
toCatchClause(LetExp exp,
CatchClause next) |
protected Expression |
visitApplyExp(ApplyExp exp,
gnu.expr.ANormalize.Context context) |
protected Expression |
visitBeginExp(BeginExp exp,
gnu.expr.ANormalize.Context context) |
protected Expression |
visitBlockExp(BlockExp exp,
gnu.expr.ANormalize.Context context) |
protected Expression |
visitCaseExp(CaseExp exp,
gnu.expr.ANormalize.Context context) |
protected Expression |
visitClassExp(ClassExp exp,
gnu.expr.ANormalize.Context context) |
protected Expression |
visitExitExp(ExitExp exp,
gnu.expr.ANormalize.Context context) |
protected Expression |
visitIfExp(IfExp exp,
gnu.expr.ANormalize.Context context) |
protected Expression |
visitLambdaExp(LambdaExp exp,
gnu.expr.ANormalize.Context context) |
protected Expression |
visitLetExp(LetExp exp,
gnu.expr.ANormalize.Context context)
Besides handling "let" and "fluidlet" normalization, it flattens the
nesting of let expressions.
|
protected Expression |
visitModuleExp(ModuleExp exp,
gnu.expr.ANormalize.Context context) |
protected Expression |
visitQuoteExp(QuoteExp exp,
gnu.expr.ANormalize.Context context) |
protected Expression |
visitReferenceExp(ReferenceExp exp,
gnu.expr.ANormalize.Context context) |
protected Expression |
visitSetExp(SetExp exp,
gnu.expr.ANormalize.Context context) |
protected Expression |
visitSynchronizedExp(SynchronizedExp exp,
gnu.expr.ANormalize.Context context) |
protected Expression |
visitTryExp(TryExp exp,
gnu.expr.ANormalize.Context context) |
defaultValue, error, error, update
error, getColumnNumber, getCompilation, getCurrentLambda, getEndColumn, getEndLine, getExitValue, getFileName, getLanguage, getLineNumber, getMessages, getPublicId, getStartColumn, getStartLine, getSystemId, isStableSourceLocation, noteError, setColumn, setContext, setFile, setLine, setLine, visit, visit, visitAndUpdate, visitDeclarationType, visitDeclarationTypes, visitDefaultArgs, visitExpression, visitExps, visitExps, visitFluidLetExp, visitLangExp, visitObjectExp, visitScopeExp, visitThisExp
public static void aNormalize(Expression exp, Compilation comp)
protected Expression normalizeTerm(Expression exp)
protected static boolean isAtomic(Expression exp)
protected Expression normalizeName(Expression exp, gnu.expr.ANormalize.Context context)
protected Expression normalizeNames(Expression[] exps, int index, gnu.expr.ANormalize.MultiContext context)
protected Expression visitQuoteExp(QuoteExp exp, gnu.expr.ANormalize.Context context)
visitQuoteExp
in class ExpVisitor<Expression,gnu.expr.ANormalize.Context>
protected Expression visitReferenceExp(ReferenceExp exp, gnu.expr.ANormalize.Context context)
visitReferenceExp
in class ExpVisitor<Expression,gnu.expr.ANormalize.Context>
protected Expression visitApplyExp(ApplyExp exp, gnu.expr.ANormalize.Context context)
visitApplyExp
in class ExpVisitor<Expression,gnu.expr.ANormalize.Context>
protected Expression visitBeginExp(BeginExp exp, gnu.expr.ANormalize.Context context)
visitBeginExp
in class ExpVisitor<Expression,gnu.expr.ANormalize.Context>
protected Expression visitLetExp(LetExp exp, gnu.expr.ANormalize.Context context)
visitLetExp
in class ExpVisitor<Expression,gnu.expr.ANormalize.Context>
protected Expression visitIfExp(IfExp exp, gnu.expr.ANormalize.Context context)
visitIfExp
in class ExpVisitor<Expression,gnu.expr.ANormalize.Context>
protected Expression visitCaseExp(CaseExp exp, gnu.expr.ANormalize.Context context)
visitCaseExp
in class ExpVisitor<Expression,gnu.expr.ANormalize.Context>
protected Expression visitLambdaExp(LambdaExp exp, gnu.expr.ANormalize.Context context)
visitLambdaExp
in class ExpVisitor<Expression,gnu.expr.ANormalize.Context>
protected Expression visitSetExp(SetExp exp, gnu.expr.ANormalize.Context context)
visitSetExp
in class ExpVisitor<Expression,gnu.expr.ANormalize.Context>
protected Expression visitModuleExp(ModuleExp exp, gnu.expr.ANormalize.Context context)
visitModuleExp
in class ExpVisitor<Expression,gnu.expr.ANormalize.Context>
protected Expression visitTryExp(TryExp exp, gnu.expr.ANormalize.Context context)
visitTryExp
in class ExpVisitor<Expression,gnu.expr.ANormalize.Context>
protected CatchClause toCatchClause(LetExp exp, CatchClause next)
protected Expression visitSynchronizedExp(SynchronizedExp exp, gnu.expr.ANormalize.Context context)
visitSynchronizedExp
in class ExpVisitor<Expression,gnu.expr.ANormalize.Context>
protected Expression visitBlockExp(BlockExp exp, gnu.expr.ANormalize.Context context)
visitBlockExp
in class ExpVisitor<Expression,gnu.expr.ANormalize.Context>
protected Expression visitExitExp(ExitExp exp, gnu.expr.ANormalize.Context context)
visitExitExp
in class ExpVisitor<Expression,gnu.expr.ANormalize.Context>
protected Expression visitClassExp(ClassExp exp, gnu.expr.ANormalize.Context context)
visitClassExp
in class ExpVisitor<Expression,gnu.expr.ANormalize.Context>