Multi-(Co)Iteration, Categorically

259
Multi-(Co)Iteration, Categorically
Tarmo Uustalu
Inst. of Cybernetics at Tallinn Technical Univ.
Akadeemia tee 21, EE-12618 Tallinn, Estonia
email ta.rinocs ioc ee
.
Abstract Generic (total) functional programming combinators for multi
iteration (an iteration-like scheme that inducts over several data struc
tures simultaneously) and multi-coiteration (a coiteration-like scheme
that, simultaneously to coinducting over a codata structure, inducts over
one or more data structures) are described.
1
Introduction
The research in generic functional programming is, in its present state, largely
a quest for useful generic combinators with nice calculational properties. This
note is a study of generic (total) functional programming combinators for multi
iterat ion and multi-coiteration from the point of view of their calculational prop
erties. Multi-iteration and multi-coiteration are relatives of (plain) iteration and
coiteration, which are the basic function definition schemes associated to induc
tive and coinductive types. Multi-iteration, treated in [FSZ94} in a syntactically
oriented fashion and in [H1T97] in a category-theoretic framework, is a natu
ral way for defining a function with several arguments, all of inductive types,
which, when evaluated, consumes all argument data at the same pace. Typical
bi-iterative functions are functions of essentially comparative character. Multi
coiteration, although it does not seem to have received much attention, is an
equally natural way for defining a function to a coinductive type, with all but one
of its arguments inductive, which, when evaluated, produces the value codata at
the same pace as it consumes the argument data. Typical bi-coiterative functions
are functions of copying character. It has to be noted that multi-coiteration is not
dual to multi-iteration; multi-iteration relates to multi-coiteration in much the
same fashion as iteration with (static) parameters, studied carefully in [Par971,
relates to coiteration with (static) parameters. Multi-iteration and and multi
coiteration might, as a matter of fact, be thought of as iteration and coiteration
with dynamically destructed parameters, but such way of thinking obscures the
symmetry aspect of the multi schemes.
In this note, we give definitions by characterization and list some laws for
three generic combinators for multi-iteration and multi-coiteration, the uncur
ned (), left-curried (C) and right-curried (C’) multi-catamorphism and multi
anamorphism combinators. One of our intentions is to show that the equal in
beauty to the uncurried multi-catamorphism combinator among the multi-ana
morphism combinators is the the left-curried one.
260
Tarmo Uustalu
Throughout the note, we are working with one base category C which we
require to be cartesian closed, i.e., to have finite products (— x —‘) and expo
nentials (higher types) (—‘ = —). cur is the left-currying combinator; cur’ is
the right-currying combinator, cur’ f = cur(fo swap), where swap = (snd, fst).
uncur is the uncurrying combinator, uncur f = g if f = cur g; exch is the ar
gument swap combinator for curried functions, exchf = cur’(uncurf). If F is
an endofunctor on C such that a data type exists with base F, then iF is such
data type, IT-iF the associated constructor, and — F is the F-catamorphism
combinator. If a codata type exists with base F, then vF is such codata type,
OutF the associated destructor, and [— ]- is the F-anamorphism combinator.
Multi- Catamorphisms
Let F, G, H be endofunctors on C, and assume that data types exists both with
base F and base G.
The key to the definition by characterizations of three kinds of F, G; H-bi
catamorphisms, which we will present shortly, lies in the following proposition.
Proposition 1. Given q : HG — C.
r F(Y = Z) —* G Y = HZ is natural in Z, Y Inotation NATcFG.H(T)I,
then a unique f ,uF —* i1
G
C exists such that
(Ins
=
id) o
f o ‘F
=
(id
‘
)
0 TO
Ff
[notation CATK(r, q5, f)], i.e.,
II1F
FpF
F
4
F(uG=?C)
f
GjtG=.HC
jd-
>GpG=’C.z
In0
If r : G(X = Z) —* F X = HZ is natural in Z, X [notation NATG;H(T)],
then a unique f ,iG —* pF = C exists such that
(IflF
=
id) of o
=
(id
=
) or oGf
5
g
Multi-(Co)Iteration, Categorically
261
[notation CATK’(r, q
, f)], i.e.,
5
>uG
G,uG
GJ1
G(pF=’ C)
FuF=’HC
IflF1d
If r : FX x GY —* H(X x Y) is natural in X, Y [notation NATG;H(T)],
then a unique f : pF x ,zG —* C exists such that
fo(InF
[notation cATALIC(r,
,
f)],
X
ftc) =oHfor
i.e.,
IF XIn
tFxGpG
1
F
Fx,uG
H (F x jiG)
Hf
Proposition 1 follows straightforwardly from the following lemma which is
easy to verify.
Lemma 1. Given:HC-+C.
—
or
1
If NAT’,G;H(T), then (In
CATA’(r,b,f)
If NAT,,(r), then (In’
CATK’(r,q,f)
E
C)
F(jiG
—*
jiG
=
f=(InG’=)orF
q) o r G(jiF
f=
=
(InF’
C)
=
—*
jiF
=
q)orc
If NATG;H(r), then NATG;H(CUI(H(UnCUr id) o r)) and
NATG;H(cur’(H(Uncur’ id) or)) and
CATAc(r,
,
C and
f)
CATK(cur(H(uricur id) o r),
,
cur
f)
CATAC’ (cur’(H(imcur’ id) o r), 4, cur’
f)
C and
262
Tarmo Uustalu
Also easy to verify (but not needed in order to prove Proposition 1) is this
lemma used below.
Lemma 2. Given:HC-*C.
If NAT,G;H(T), then NATG.H(UX1CUr(T
GATAC (r, ,
f)
o
CATA’(uncur(r
If NAT,G;H(T), then NATG;H(uncur’(r
CATAC’ (r, q
,
5
F(cur id))) and
o
o
F(cur id)), , uncurf)
G(cur’ id))) and
GATA’(imcur’(r o G(cur’ id)),
f)
,
uncur’f)
By Proposition 1, the following is a well-formed definition of three kinds of
F, G; H-bi-catamorphisms.
Definition 1. Given : HG
—
—*
C.
If NATF,G;H(T), then say the f such that GATAC(r, , f) to be the leftcurried F, G; H-bi-catamorphism via r of [notation r, q ri.
If NATG;H(T), then say the f such that CATAC (r, , f) to be the rightcurried F, G; H-bi-catamorphism via r of [notation r, r’i.
If NATG.H(T), then say the f such that GATA(r, , f) to be the uncur
ned F, G; H-bi-catamorphism via T of [notation j r,
The following calculational laws of typing and cancellation for bi-catarnorphisms
are trivial consequences of the defining characterizations.
Corollary 1. Given q:HC-4 C.
If NA TFG.H(T), then r,cb : ,uF
(Ins
id)
or,r 0
If NAT,G;H(T), then
(InF
id)
InF
=
pG
=
C and
)
(id
r,
In
0
=
0T0FT,FC
C and
pG —* iF
r,
0
—*
(id
=‘
)or
5
q
0
G
T,
V
pF x pG —* C and
If NATG;H(T), then
r,o(InFxInG)=oHlr,or
More interesting consequences of the characterizations are the following bi
cata fusion laws.
Corollary 2. Given q
: HG
5
—
If NATG;H(T), h : D
—
—*
C, & HD
—*
D.
C, then
hob=boHh D
(idh)oiJr,’br=r,q5V
Multi-(Co)Iteration, Categorically
—
263
If NATG.H(T), ii: D —* C, then
ho’ib= q5oHh D
(id= h)olr,r’
=
If NATG;H(T), ii: D —* C, then
ho’z/’=,boH1i D
holT,?!Vw
Equally interesting are the cata - cata - bi-cata fusion laws.
Corollary 3. Given q: HG —* C, : FE —* E,
If NATG.H(T), Ii : E —* E’
(
id)oho’
: GE’ —* E’.
2, then
D
If NATQi-), ii: E’ —* E
‘
=
(a’Gid)oholF=lT,r
D, then
(id= q)oroG1i D (F
C
If NATO;H(T), Ii: E x E’ —* I3, then
=
ho(x’)=oHhor
D
=
0
id)oho’
=
ho(lFxQ)=lT,w
From Lemmas 1, 2, we niay learn the laws stated in the following corollary.
Corollary 4. Given
: HG
—*
C.
If NATG;H(T), then
J r,
=
I (In’
=
q)
0 T
=
curl uncur(r
o
F(cur id))
If NAT,G;H(T), then
IT,
—
3
=
ci (Ink’
=.
) or
=
cur’luncur’(r a G(cur’ id))
uc
If NATG;H(T), then
=
uncurl cur(H(uncur id
=
uncur’ cur’ (H(uticur’ id) o r), qf
0 T),
‘
Multi-Anamorphisms
Let F, G, H be endofunctors on C as before, but assume that a data type exists
with base F and a codata type exists with base G.
The definition to be presented below of three kinds of H; F, G-bi-anamorphisms
by characterization hinges upon Proposition 2.
264
Tarmo Uustalu
Proposition 2. Given : C
—
—>
H C.
If r : HX x GY —* F(X x Y) is natural in X, Y [notation NAT;F,Q(T)J,
then a unique f C x uG —* vF exists such that
OUtF ° fo(id x
mG)
=
Ff or o
(
x id)
[notation ANA;F,G(T, 4, f)], i.e.,
CxuG
0
idXIn
Xid
uG
1
CxG
G
1
>HCxG
G)
1
F(Cx
f
FvF
Out F
If r : G(X =. Z) —* H X = F Z is natural in Z, X [notation NAT;F,G(T)],
vF exists such that
then a unique f 4
uG —* C
(id= OutF)ofo In
=
(q id)oroGf
[notation ANA;F,G(T, , f)], i.e.,
In
GIG
G(C=’vF)
Ir
C=’vF
1d.OutF
>C=.FvF<
HC=’FvF
If r : H(Y
Z) —* GY
FZ is natural in Z, Y [notation NAT.J;Fc(T)J,
f
unique
vF exists such that
then a
C —+ pG
(mG
Outp) of
=
ro Hf o
[notation ANAj;p,G(T, , f)], i.e.,
C
H(pG=’vF)
In OUtF
GpG=FvF
Multi-(Co)Iteration, Categorically
265
Proposition 2 is a consequce of the following lemma.
Lemma3. Given:G—*HG.
If NAT;F,Q(T), then ro (q x In’)
C x G
f
ANA’;F,G(r,c;b,f)
If NAT;F,G(T), then
Out’)
(
ANA;FQ(T,,f)
o
E
=
F(C x jiG) and
[ro (q x In’)]F
r : G(C
f
-÷
=
vF) —* C
Out’)
vF and
OTG
If NATj;(T), then NAT;F,G(UnCUr(T o H(cur id))) and
NAT;F,G(exch(T o H(exchid))) and
ANAl;F,G(T, ,
f)
E
ANA;F,G(1]ncur(T o H(cur id)),
E
ANAIF,G(eXCh(T o H(exchid)),,exchf)
,
uncur f)
The following lemma will only be used below.
Lemma4. Given 4:C-*HC.
If NAT;p
(T), then NATI;F,G(CUr(F apply o r)) and
0
ANA7;F,G(T,
,
f)
ANAj;F(cur(F apply
0
r),
,
curf)
If NAT;F,C(T), then NAT;F,Q(exch(r o G(exch id))) and
(T, ,
0
ANA;p
f)
E
ANAI;FG(eXCh(T o G(exch id)),
,
exchf)
Proposition 2 entitles us to make the following definition of various kinds of
H; F, G-bi-anarnorphisms.
Definition 2. Given q: C
If NATp
(r), then
0
—k
say
HG.
the
f
such that ANA.Fc(r, , f) to be the un
r of
[notation [T, 4t ]Uc].
curried H; F, G-bi-anamorphism via
If NATcI;FG(T), then say the
f
such that ANAI;F,G(T, , f) to be the right[notation [r,
If NATI.FG(T), then say the f such that ANACH.FG(T, , f) to be the leftcurried H; F, G-bi-anamorphism via -z- of 4 [notation [r, q ]].
curried H; F, G-bi-anamorphism via r of
The first calculational laws for bi-anamorphisms directly readable off from
the defining characterizations are those of typing and cancellation.
Corollary 5. Given qS: C
—*
HG.
266
—
Tarmo Uustalu
If NAT;FG(T), then [r,] : Cx G
OUtF
o [r,
]UC
o (id x mG)
If NATj;F,G(T), then [r,]c’
(id
tG
q )c’
OutF) o [r, 5
=
then
If NATCII;F,G(T),
(mG
[T,]C
°
: C
= OUtF)
=
,uG
o [r,]c
o
F [r,
o
T
(
x id)
vF and
C
—>
’G =
1
—*
uF and
id) a r o G [r,
5
(q
=
=
vF and
r oH[T,]c
More interesting consequences of the characterizations are the bi-ana fusion
laws.
Corollary 6. Given:C—+HC, ,b:D—*HD.
If NAT;F,G(T), h : C —* D, then
/o1i=Hho,b
[7UC
o(hx id)
t1
[r,]
D
If NAT;F,G(T), h:C—*D, then
oh=Hhoq
If NATf;F,G(T), 11: C
—*
(h= jd)o[T,]c =[r]c’
D
D, then
[r,oh=[r,]c
oh=Hho,b D
Also useful in calculations are the laws of ana
Corollary 7. Given : C
—*
HG,
If NATj;FG(T), ii: C x E’
oho(id xe’)
=
—+
: E —* FE,
E, then .
Fhoro(x id)
If NA T.I.FG(T), h : E
—+
C
=
D
‘
cata
-
: GE’
bi-ana fusion.
—*
E’.
.
oho(idx’’)
=
E, then
(L
(id= )oho’
id)oroGh
=
If NATl;F,G(T), ii: C
-
D
(id
c:)oho4’i’
(r,.)c
E’ => E, then
(‘ )oh=roHhoq5
D
((
The following laws, finally, can be concluded from Lemmas 3 and 4.
Corollary 8. Given q: C
—*
HG.
If NAT;F,G(T), then
=
[ro(q x In’)]p
=
wacur[cur(Fapplyor)]c
If NATCF;F,G(T), then
[r, ]‘
=
j
(
Out.’) a r
=
exch[ exch(r a G(exzh id)) ]‘
If NATI;F,G(T), then
=
=
cur[uncur(r a H(curid)),)
exch[exch(r a H(exchid)),çb]C
Multi-(Co)Iteration, Categorically
4
267
Conclusion
Since multi-iteration and multi-coiteration are inherently symmetric schemes of
function definition, realizing the fact that some function of interest is definable
by one of these two schemes is a discovery worth making, as it means that there
are, in fact, many definitions of this function, and situations such as this are the
encouraging ones in program calculation. For that reason, well-behaved generic
combinators for multi-iteration and multi-coiteration are potentially interesting
as library combinators and the laws they obey may well deserve spelling out.
In this note, we described three generic combinators for multi-iteration and
as many for multi-coiteration. The laws we listed for these combinators demon
strate that, in case of multi-iteration, the inherent symmetry of the scheme is
most transparently captured in the uncurried multi-catamorphism combinator,
while the left-curried multi-anamorphism combinator is the most satisfactory
representation for the multi-coiteration function definition scheme. The uncur
ned multi-anamorphism combinator is, however, a neat enough alternative to
the left-curried one, if higher types are to be avoided for some reason. Folds and
unfolds, generic combinators for iteration and coiteration with parameters, also
come in uncurried and curried versions, but since no symmetry is involved in
coiteration with parameters, there are no reasons for preferring the left-curried
unfold combinator to the uncurried one.
Acknowledgements
Varmo Vene was the first one to tell me about “iterations inducting over multiple
data structures” atid I owe him thanks for this.
The work reported in this note was partially supported by the Estonian
Science Foundation under grant no 2976.
References
[FSZ94] Leonidas Fegaras, Tim Sheard, and Tong Zhou. Improving programs which
recurse over multiple inductive structures. In Proceedings ACM SICPLAN
Workshop on Partial Evaluation and Semantics-Based Program Manipulation,
PEPM’94, Orlando, FL, USA, June 1994, pages 21 32. ACM Press, New York,
1994.
[H1T97] Zimenjiang Hu, Hideya Iwasaki, and Masato Takeichi. An extension of the acid
rain theorem. In T. Ida, A. Oliori, and M. Takeichi, editors, Proceedings 2nd
Fuji Intl. Workshop on Functional and Logic Programming, Shonan Village
Center, Japan, 1 4 Nov 1996, pages 91 105. World Scientific, Singapore, 1997.
[Par97j Alberto Pardo. A calculational approach to strong datatypes. In M. Haveraaen
and 0. Owe, editors, Selected Papers 8th Nordic Workshop on Programming
Theory, NWPT’96, Oslo, Norway, 4-6 Dec 1996, Research report 248, Dept.
of Informatics, Univ. of Oslo, pages 187 197. May 1997.