Algorithm Search⌘K

Definitionadvanced

A-normal form

Also called ANF, administrative normal form.

A direct-style IR where each argument is a variable or constant and each intermediate result is named by let, with the benefits of CPS.

A term is in A-normal form (ANF) when every operand of every call and operator is a variable or a constant, and every intermediate result is named by a let. So f (g x) + 1 becomes let t1 = g x in let t2 = f t1 in t2 + 1. The form is in direct style: functions return their results, and there are no continuations. Yet it has what compiler writers wanted from continuation-passing style: the order of evaluation is fixed by the nesting of lets, and every value that a later step uses has a name. Flanagan, Sabry, Duba and Felleisen (1993) defined the form and named it after the A-reductions that produce it. They showed that a compiler that translates to CPS, simplifies, and translates back gets exactly the A-normal form. For a language without first-class functions, ANF is close to three-address code, with nesting in place of jumps.

Definition

Take a call-by-value lambda calculus with constants, operators ⊕\oplus, let and if. A term MM is in A-normal form if it is generated by:

V::=c∣x∣λx. M(values)C::=V∣V1 V2∣V1⊕V2(simple computations)M::=C∣let x=C in M∣if V then M1 else M2(terms)\begin{aligned} V &::= c \mid x \mid \lambda x.\,M && \text{(values)}\\ C &::= V \mid V_1\,V_2 \mid V_1 \oplus V_2 && \text{(simple computations)}\\ M &::= C \mid \mathtt{let}\ x = C\ \mathtt{in}\ M \mid \mathtt{if}\ V\ \mathtt{then}\ M_1\ \mathtt{else}\ M_2 && \text{(terms)} \end{aligned}

So the right side of a let is never a let or an if, operands and conditions are values, and the body of every function is again in A-normal form. A call V1 V2V_1\,V_2 in the position of CC at the end of a term, not on the right of a let, is a tail call.

A-reductions. Let EE be a call-by-value evaluation context, E::=[ ]∣E M∣V E∣E⊕M∣V⊕E∣let x=E in M∣if E then M1 else M2E ::= [\,] \mid E\,M \mid V\,E \mid E \oplus M \mid V \oplus E \mid \mathtt{let}\ x = E\ \mathtt{in}\ M \mid \mathtt{if}\ E\ \mathtt{then}\ M_1\ \mathtt{else}\ M_2, with E≠[ ]E \ne [\,] in the rules below:

  • (A1) E[let x=M in N]→let x=M in E[N]E[\mathtt{let}\ x = M\ \mathtt{in}\ N] \to \mathtt{let}\ x = M\ \mathtt{in}\ E[N], where xx is not free in EE.
  • (A2) E[if V then M1 else M2]→if V then E[M1] else E[M2]E[\mathtt{if}\ V\ \mathtt{then}\ M_1\ \mathtt{else}\ M_2] \to \mathtt{if}\ V\ \mathtt{then}\ E[M_1]\ \mathtt{else}\ E[M_2].
  • (A3) E[C]→let t=C in E[t]E[C] \to \mathtt{let}\ t = C\ \mathtt{in}\ E[t], where CC is a call or an operation, tt is new, and EE is not of the form E′[let z=[ ] in N]E'[\mathtt{let}\ z = [\,]\ \mathtt{in}\ N], so that the hole is not already the right side of a let.

Applied anywhere in a term until none applies, they give a term in A-normal form. Rule A1 flattens nested lets, rule A2 moves the work after a conditional into both branches, and rule A3 names an intermediate result.

A-normalization keeps the meaning. Each rule is an equation of call-by-value evaluation. The key fact is that E[M]E[M], for an evaluation context EE, runs MM first: if MM ends with a value vv, then E[M]E[M] continues as E[v]E[v]; if MM is stuck or runs forever, so is E[M]E[M].

  • For A3, both sides run CC first. If it gives vv, the left side continues as E[v]E[v] and the right side as E[t][t:=v]=E[v]E[t][t := v] = E[v], since tt is new.
  • For A1, both sides run MM first. If it gives vv, the left side continues as E[N[x:=v]]E[N[x := v]] and the right side as E[N][x:=v]E[N][x := v]. These are the same term, because xx is not free in EE.
  • For A2, VV is already a value. If it is true, both sides continue as E[M1]E[M_1]; if false, as E[M2]E[M_2]; otherwise both are stuck.

The same holds after a substitution of closed values for the free variables and inside any further evaluation context, because the argument uses only the order of evaluation and substitution. So the two sides of each rule have the same outcome in every program, and a rule may be applied under a fun or in a branch. A sequence of A-reductions therefore keeps the outcome of the program: the same constant, stuck in both, or no end in both.

A direct algorithm reaches the same normal form in one pass. Flanagan et al. give one that walks the term once with a translation-time continuation for the context, as the one-pass CPS conversion does. Its output is linear in the input except for the copies that rule A2 makes.

Examples and non-examples

Nested calls. f (g x) (h y) has the normal form let t1 = g x in let t2 = h y in f t1 t2 (with a curried f, let t3 = f t1 in t3 t2). The lets fix the order: g x runs before h y, as call-by-value from left to right says.

Nested let. let x = (let y = a + 1 in y * 2) in x - 3 becomes let y = a + 1 in let x = y * 2 in x - 3 by rule A1 and then A3.

Conditional in an operand. (if a then b else c) + 1 becomes if a then b + 1 else c + 1 by rule A2. The context [ ] + 1 is copied into both branches. With a large context and many nested conditionals the copies grow without bound. Compilers give the context a name instead, a join point let j = fun r -> r + 1 in if a then j b else j c, where every use of j is a tail call (Maurer, Downen, Ariola and Peyton Jones 2017).

Functions. map (fun x -> x * x + 1) xs is map (fun x -> let t = x * x in t + 1) xs in normal form: the function is a value, and only its body changes.

Non-examples.

  • f (g x): the operand g x is not a value.
  • let x = (let y = a in b) in c: the right side of a let is a let.
  • if x < 1 then a else b: the condition is not a value. Its normal form is let t = x < 1 in if t then a else b.
  • let y = (if a then b else c) in y + 1: the right side is an if. The definition above forbids it. Some variants allow it, as a join point in disguise.
  • g x (fun v -> k v): this is a CPS call, not ANF. ANF has no continuation arguments.
  • Continuation-passing style. The CPS image of an A-normal form term needs no administrative redexes, and translating CPS terms without administrative redexes back to direct style gives A-normal forms. Sabry and Felleisen (1993) showed that the A-reductions are what the administrative reductions of CPS do, seen in the source.
  • CPS conversion. Its one-pass form is A-normalization with a continuation argument added. Kennedy (2007) argues that CPS is still the better intermediate language, because ANF is not closed under inlining: putting a function body with lets in the place of a call on the right of a let breaks the form, and the term must be normalized again.
  • Static single assignment form. Every name in ANF is bound once, as in SSA. Chakravarty, Keller and Zadarnowski (2003) translate SSA programs into ANF, with blocks as local functions and phi-functions as parameters. The correspondence of SSA and functional programs states this.
  • Three-address code. A three-address instruction t = a op b is the ANF binding let t = a op b. ANF adds nesting, local functions and lexical scope.

References

  1. Cormac Flanagan, Amr Sabry, Bruce F. Duba and Matthias Felleisen, The essence of compiling with continuations, Proceedings of the ACM SIGPLAN 1993 Conference on Programming Language Design and Implementation (PLDI 1993), pages 237-247, the A-reductions, the grammar of A-normal forms, the linear-time A-normalization algorithm, and the argument that CPS compilers do the same work as a compiler on A-normal forms
  2. Amr Sabry and Matthias Felleisen, Reasoning about programs in continuation-passing style, LISP and Symbolic Computation 6(3-4), 1993, pages 289-360, the equational correspondence between call-by-value terms and their CPS images, in which the A-reductions are the source image of administrative reductions
  3. Andrew Kennedy, Compiling with continuations, continued, Proceedings of the 12th ACM SIGPLAN International Conference on Functional Programming (ICFP 2007), pages 177-190, the comparison of CPS with A-normal form, including the need to renormalize after inlining and the treatment of conditionals
  4. Luke Maurer, Paul Downen, Zena M. Ariola and Simon Peyton Jones, Compiling without continuations, Proceedings of the 38th ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI 2017), pages 482-494, join points in a direct-style intermediate language, which avoid the duplication of contexts into the branches of a conditional
  5. Manuel M. T. Chakravarty, Gabriele Keller and Patryk Zadarnowski, A functional perspective on SSA optimisation algorithms, Compiler Optimization Meets Compiler Verification (COCV 2003), Electronic Notes in Theoretical Computer Science 82(2), pages 347-361, the translation of SSA programs into A-normal form, and sparse conditional constant propagation done on A-normal form

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/3Y2H9E/text.yaml