Hostname: page-component-76d6cb85b7-dqfph Total loading time: 0 Render date: 2026-07-11T14:50:46.687Z Has data issue: false hasContentIssue false

Folding left and right matters: Direct style, accumulators, and continuations

Published online by Cambridge University Press:  14 February 2023

OLIVIER DANVY*
Affiliation:
Yale-NUS College & School of Computing, National University of Singapore (e-mail: danvy@acm.org)
Rights & Permissions [Opens in a new window]

Abstract

The equivalence of folding left and right over Peano numbers and lists makes it possible to minimalistically inter-derive (1) structurally recursive functions in direct style, (2) structurally tail-recursive functions that use an accumulator, and (3) structurally tail-recursive functions in delimited continuation-passing style, using Ohori and Sasano’s lightweight fusion by fixed-point promotion. When the fold-left and the fold-right functions account for primitive iteration for Peano numbers, this equivalence is unconditional. When they account for primitive recursion for Peano numbers, this equivalence is modulo left permutativity of their induction-step parameter – a property which is more general than associativity and commutativity. And when they account for primitive iteration or for primitive recursion over lists, this equivalence is modulo left permutativity of their induction-step parameter if these two fold functions have the same type. Since the 1980s, however, the two fold functions for lists do not have the same type: the arguments for their induction-step parameter are swapped, a re-ordering that complicated Bird and Wadler’s duality theorems and whose history is reviewed in an appendix. Without this re-ordering, Bird and Wadler’s second duality theorem more visibly accounts for “re-bracketing,” which is a key step to make recursive programs tail recursive in the general area of program development, from Cooper in the 1960s and onwards.

Information

Type
Functional Pearl
Creative Commons
Creative Common License - CCCreative Common License - BY
This is an Open Access article, distributed under the terms of the Creative Commons Attribution licence (https://creativecommons.org/licenses/by/4.0/), which permits unrestricted re-use, distribution and reproduction, provided the original article is properly cited.
Copyright
© The Author(s), 2023. Published by Cambridge University Press
Figure 0

Fig. 1. Folding left and right matters, diagrammatically

Figure 1

Fig. 2. Materialization of Fig. 1

Figure 2

Fig. 3. Fig. 2, revisited and expanded

Figure 3

Fig. 4. Successive factorial functions

Figure 4

Fig. 5. Left-permutativity of the successive induction-step parameters in Fig. 4

Supplementary material: File

Danvy supplementary material

Danvy supplementary material 1

Download Danvy supplementary material(File)
File 172.2 KB
Supplementary material: File

Danvy supplementary material

Danvy supplementary material 2

Download Danvy supplementary material(File)
File 180.9 KB
Submit a response

Discussions

No Discussions have been published for this article.