HAL Id: hal-00384689
https://hal.archives-ouvertes.fr/hal-00384689
Submitted on 15 May 2009
HAL is a multi-disciplinary open access archive for the deposit and dissemination of sci- entific research documents, whether they are pub- lished or not. The documents may come from teaching and research institutions in France or abroad, or from public or private research centers.
L’archive ouverte pluridisciplinaire HAL, est destinée au dépôt et à la diffusion de documents scientifiques de niveau recherche, publiés ou non, émanant des établissements d’enseignement et de recherche français ou étrangers, des laboratoires publics ou privés.
On the asymptotic behaviour of primitive recursive algorithms
René David
To cite this version:
René David. On the asymptotic behaviour of primitive recursive algorithms. Theoretical Computer Science, Elsevier, 2001, pp.159-193. �hal-00384689�
algorithms
Rene DAVID
Abstract
Thispaperdevelopsanewsemantics(thetrace ofacomputation)thatis
usedtostudyintensionalpropertiesofprimitiverecursivealgorithms. Itgives
anewproofof the\ultimateobstinationtheorem\of L.Colson andextends
ittothecase whenmutualrecursionispermitted. Theultimateobstination
theorem fails whenother data types(e.g. lists) are used. I dene another
property (thebacktracking property)ofthe samenaturebutwhichisweaker
thanthe obstinate obstination. Thisproperty is proved for everyprimitive
recursivealgorithmusinganykindofdatatypes.
Keywords primitiverecursivedenitions, intensionality,complexity.
1 Introduction
In [3], [7] the denotational semantics of lazy integersis used to proveintensional
properties of primitive recursive algorithms. L.Colson proves the ultimate obsti-
nation theorem and T.Coquand gives a constructive proof of it. An important
consequence of the ultimate obstination theorem is that the inf of two integers
cannotbecomputed,byaprimitiverecursivealgorithm,neitherinthedesiredway
(i.e. by decrementing alternatively the two arguments), nor in the desired time
complexity(i.e. O(inf)).
Idevelophereanewsemanticstostudytheintensionalbehaviourofalgorithms.
Theintuitionisthefollowing. LetN bethedomainoflazyintegers.Anelemente
of N canbeseenasapartial function thatlls someaccessiblecells (inthesense
of [2])withtheconstructorsS and0:Forexampleine
0
=S(0)theaccessiblecells
aretheonesdenotedbytheiraddress0and1.TherstoneislledwithSandthe
second with0:Ine
1
=S 2
(?) theaccessiblecellsare theonesdenoted 0;1;2. The
cells0and1arelledwithS andthethirdoneisunlled (seegure1).
e
0
=
cellnumber 0 1
constructor S 0
and e
1
=
cellnumber 0 1 2
constructor S S
g. 1
The set of tracesis dened as follows. Let W be the set of (nite or innite)
wordson thealphabet fx
n
=n 0; x is aletterg. A traceis apair(e;) where
e2N and isalabelling, i.e. afunction fromthe accessiblecellsof eto W (see
examplesingure2).
LaboratoiredeMathematiques. CampusScientique. 73376LeBourgetduLacCedex. email
david@univ-savoie.fr
totraceswhich"codes"thewayf getsitsresult: Thefactthatthetokenx
i occurs
in (n) intuitivelymeansthat thecelli oftheelementnamed x hasbeenusedto
gete(n):
An example is given in gure 2: Dene add as usual by add(0;m) = m and
add(Sn;m)=Sadd(n;m):
-Thetracet
2
meansthat to getS thealgorithmhas usedthecell0of t
0 and
to get0thealgorithmhasusedrstthecell1oft
0
andnextthecell0oft
1 .
-Thetracet
3
meansthatto getS thealgorithmhasused rstthecell0oft
1
andnextthecell0oft
0
andtoget0thealgorithmhasusedthecell1oft
0 :
t
0
=
cellnumber 0 1
constructor S 0
labelling x
0 x
1 t
1
=
cellnumber 0
constructor 0
labelling y
0
t
2
=[[add]](t
0
;t
1 )=
cellnumber 0 1
constructor S 0
labelling x
0 x
1 y
0
t
3
=[[add]](t
1
;t
0 )=
cellnumber 0 1
constructor S 0
labelling y
0 x
0 x
1
g. 2
Thisis easily generalizedto anydata type. In thiscase, thecellsare nomore
givenbyintegersbutbytheiraddresses(i.e. listsofintegers)inthetreerepresenting
an element of the data type. This notion of trace is related to the sequential
algorithmsintroducedbyBerryandCurien([2]or[1],chapter14)asfollows. Intheir
terminology,asequentialalgorithmisatree. Each branchofthistreecorresponds
tothecomputationofthealgorithmonparticulararguments,thatisexactly(with
aslightvariationonthesyntaxandtheterminology)whatIcallatrace.
Themainadvantagesofthis approacharethefollowing:
(1)There is anotionofmodularity (seetheorem 34): Ife isanelementof N,
lete[x]bethetrace(e;)where(n)=x
n
foreachn. Then,fort=(e;
0
);[[f]](t)
isobtainedbysubstitutingx
i with
0
(i)in [[f]](e[x]):
(2)Asingleinnitetracecontainstheinformationabouteachnitecomputation
(seeproposition36).Thiswillbeextensivelyusedintheforthcomingpapers[8]and
[11].
(3)Thisnotionallowstointroducenewpropertiesofcomputations: Theback-
trackingproperty(seebelow)cannotbeexpressedin theusualsemantics.
(4)I believeitalsomakestheproofseasierand,atleast,closertotheintuition.
In particular, the extension of Coquand's constructive result to the case where
mutual recursion is allowed would probably be impossible without the notion of
trace.
Say that atrace(e;) is ultimatelyobstinate if,in theword obtainedbycon-
catenatingthewords(n);thereisatmostoneletterwhichoccurswithunbounded
indexes. Theintuitivemeaningisthat,ifthetracerepresentsaninnitecomputa-
tion,atmostoneargumentmaybeusedentirely. Theultimateobstinationtheorem
followsimmediatelyfromthefact(seetheorem13)that, ift
1
; :::; t
n
areultimately
obstinate,thensois[[f]](t
1
; :::; t
n
):Themain argumentin itsproofisthat,when
therstS inaninnitesequenceofS isremoved,wegetthesamesequence.Thisis
fails whenotherdatatypesmaybeused.
Saythattheletterx backtracks in thewordwif,fornlargeenough,x
n occurs
innitely many times in w. This intuively means that the argument denoted by
x maynotbe"garbage-collected"in thecomputation representedbyw. Saythat
a trace t has the backtracking property if the following holds for any branch b in
t (a branch in t is the usual notion on the underlying element) : let w be the
wordobtained byconcatenating thewordsalong b:There is at most oneletter x
such that : x occurs with unbounded indexes and x does notbacktrack . When
t represents the computation of an algorithm, this intuitively means that, in the
computationofthebranchboftheresult,atmostoneargumentcanbememorized
(recallthat beingultimatelyobstinatemeansatmostoneargumentcanbeused).
I prove(seetheorem16) thatift
1
; :::; t
n
havethebacktrackingproperty, thenso
does[[f]](t
1
; :::; t
n ):
Theultimate obstination theorem is aresultaboutintensionality but ithas a
consequenceintermsofcomplexity. Ibelievethisisakindofchance.I introduced
(andproved)thebacktrackingpropertybecauseitwasthoughtthatsuchaproperty
wouldgiveO(inf 2
)asalowerboundforthetimecomplexityoftheinf functionbut
itdoesnot: seethealgorithmgivenin[9]. Ithushavenoapplicationofthisresult
in terms of complexity (see section 6for a discussion about this point). However
thenotionoftraceallowstoprovesomeotherresults.Inaforthcomingpaper([8])
I will extend Coquand's constructiveresult to the case when mutual recursion is
allowed. In another paper, in preparation with Valarcher ([11]), we will use the
tracestoansweropenquestionsinhis thesis([19]).
Warning A primitive recursive denition becomes an algorithm only when a
strategy of reduction is given. Even if the strategy does not appear explicitly in
thispaper,itishiddeninthedenitionof[[f]](seeproposition9)andcorresponds
to call by name. [13], [14] show that, in call by value, the inf function cannot
becomputed in timeO(inf) evenwhen listsormutual recursionis allowed. Note
that,inthiscase,theproblemsare,atleastintuitively,mucheasiersince,whenan
argumentis used, thecomputation time is, bydenition ofcallby value,at least
thevalueofthisargument.
The paper is organized as follows : The section 2 gives the main denitions
and results of the paper. In section 3, I prove the main properties of traces, in
particular theorem 34aboutsubstitutions. Thesection 4and 5givethe proofsof
the preservation of the ultimate obstination (as well asits consequencesin terms
of complexity) and of the backtrackingproperty. The section 6 gives someopen
questions.
Acknowledgement Thispaperhasaverylongstory.Manypeoplehelpedmeto
transformaveryroughdraftintothis nalversion. Thanksto allofthemand, in
particular,TCoquand,CBerline,PLCurienandtheanonymousreferees.
2 Denitions
2.1 Primitive recursive algorithms
NotationsAdatatypeisgivenbyalistoftypedconstructors. Letcf:D
1 :::
D
n
!D beaconstructorofD(niscalled thearityofcf).Then:
1. TheD
j
areeither Dorpreviouslydeneddata types.
2. IfD
j
=D,thenj iscalled arecursiveargumentofcf.
3. cf isrecursiveifD
j
=D forsomej.
Note that,in order to benon-empty,a datatype must haveat leastonenon-
recursiveconstructor.
Examples
1. ThedatatypeofintegersisgivenbyN =f0:N; S:N!Ng. 0isterminal
andSisrecursive.
2. ThedatatypeoflistsoftypeN isgivenbyL=fnil:L; cons:NL!Lg.
conshasarecursiveandanon-recursiveargument.
3. Thedata type ofsequences of 0and 1is given byD =fnil :D; s
0 :D !
D; s
1
:D!Dg.
Denition 1 1. The sets of n-ary typed prc (primitive recursive combinators)
aredened,asusual,astheleastsetscontainingtheprojections,theconstruc-
torsandwhich are closedunder composition andprimitive recursion.
2. Primitive recursionis dened asfollows (I will assume, withoutloss of gen-
erality, that the recursion always ison the rst argument of the prc). There
isoneequation for eachconstructor cf ofthe datatype ofthe rstargument.
Assumecf haspargumentsand(forsimplicity ofnotation),the recursivear-
gumentsof cf are fj =1j mg. Note that por m may be 0. Then, the
recursiveequationfor cf is(hisapreviouslydenedprcassociatedtocf) :
f(cf(x
1
;:::;x
p );
!
y)=h(f(x
1
;
!
y);:::;f(x
m
;
!
y);x
1
;:::;x
p
;
!
y):
Examples
1. The addition is dened, as usual, by : add(0;n) = n and add(Sm;n) =
Sadd(m;n):
2. Thesumoftheelementsofalistofintegersisdenedby: sum(nil)=0and
sum(cons(n;l))=add(n;sum(l)):
3. Thenumberof0in alistof 0and 1isdened by: nb(nil)=0;nb(s
0 (l))=
Snb(l);nb(s
1
(l))=nb(l):
Remark
In thesection 3 we will also allow the denition of k functions by mutual re-
cursion (for an arbitraryk). Forexample : even(0) =true and odd(0) = false:
even(Sx)=odd(x)andodd(Sx)=even(x):
2.2 The trace
IntherestofthepaperIwilladoptthefollowingconventions(words,traces,... are
dened inthis section):
symbols rangeover symbols rangeover
i;j;k;m;n;p;q integers u;v;w words
e elementsofadatatype r;s;t;;; traces
x;y;z;X letters f;g;h prc
a;b;c;d;; addressesoraddressingbranches
Denition 2 1. An address is anite list of positive integers. The empty list
isdenotedby ".
2. Ifa;a are addresses,aa meansthat aisan initialsegmentof a.
3. lg(a) represents the length of a and thus, if lg(a) =n; a may be written as
[a(0); :::; a(n 1)]:
4. If a isa (nite or innite) list of integers of lengthatleast m, a"m is the
prex oflengthm ofa, i.e. a"m=[a(0); :::; a(m 1)]:
5. Ifaisanaddress andpaninteger,a+pdenotes thelist obtainedbyconcate-
natingpatthe endof a.
Commentand examples
An addresscorrespondsto acellin[2]. [0;1]+3=[0;1;3]
Denition 3 LetD beadatatype.
1. An element eof D isapartial function froma prex closed set of addresses
(denotedby dom(e))satisfyingthe following conditions:
(a) If"2dom(e)thene(")isaconstructorof D:
(b) Ifa+p2dom(e); e(a)=cfandcf :D 0
1
:::D 0
n
!D 0
then1pn
ande(a+p)isaconstructor ofD 0
p .
2. Let ebeanelement ofD andabean address.Denethe accessibility ofain
eby thefollowing rules:
(a) "isaccessiblein e.
(b) a+pisaccessible ine i a2dom(e)and1parity(e(a)).
3. Let e be an element of D. Denote by Acc(e) the set of addresses that are
accessiblein e.
4. Anelement eisnitei dom(e)isnite.
5. Let e;e 0
be elements of D. e e 0
means : dom(e) dom(e 0
) and for all
a2dom(e);e(a)=e 0
(a):
6. Anaddressaismaximalinanelementeifa2Acc(e)andnoproperextension
ofaisin dom(e).
Commentand examples
1. Itiseasytoseethat Disadomain.
2. Usually,anelementof adatatypeisanite treewhosenodesarelledwith
constructors. Hereanelementagainisatreebut :
the tree may have innite branches. Innite branches may be seen as
\streams\.
itsleavesmaybeunlled.
a2dom(e)and cf =e(a) meansthat thecellofaddressais lled with the
constructor cf: An unlled cell a(i.e. a2Acc(e) dom(e)) correspondsto
alack of information for the content of the cell. The correspondence with,
inparticular,[2]isthefollowing: I callhereaccessible(respectivelyunlled)
whattheycallenabled(respectivelyaccessible).
lledwithaterminalconstructor.
4. Inthedatatypeofintegerstheelementsarethefollowing(Iwillwrite: 1 0
="
and1 i
=[1; :::; 1]
| {z }
i ).
S n
(0)standsfor: f(1 i
;S)=0i<ng[f(1 n
;0)g:
S n
(?)standsfor: f(1 i
;S)=0i<ng:Note that heretheaddress1 n
isaccessible.
S
!
standsfor: f(1 i
;S)=0ig:
5. In the data type of lists of type N; the lists e
0
= [0;1], e
1
= [0;0;:::] (the
innitelist)and e
2
=cons(0;?);aregiven in gure3. Ine
2
theaddress[2]
isaccessiblebut [2]2=dom(e
2
)andis,asusual,labelledby?:
accessibleaddresses correspondingconstructors
e
0
" [2] [2;2]
[1] [2;1]
[2;1;1]
cons cons nil
0 S
0
e
1
" [2] ::: [2;:::;2] :::
[1] [2;1] ::: [2;:::;2;1] :::
cons cons ::: cons :::
0 0 ::: 0 :::
e
2
" [2]
[1]
cons
0
g. 3
Denition 4 1. Let=fx
a
=x isaletterandaisanaddressg.Theelements
ofarecalledtokens.
2. Aword isanite (possibly empty) orinnite sequenceof tokens. The setof
wordsisthusW =
[
!
:The emptywordisdenotedby;:
3. Let u;u 0
be words. uu 0
means that u isa prex of u 0
and u"pdenotes,
for plg(u);the prex of uof lengthp:
4. u+u 0
is the result of concatenating u 0
at the end of u. When u is innite,
this is justu again. More generally, if (u
k
) isa (nite or innite) sequence
ofwords u
0 +u
1
+ ::: will bedenotedby P
u
k :
Abbreviation Inthispaper,when xcorrespondsto anelementofN;I willwrite
x
i
insteadof x
1 i:
Denition 5 1. A trace over the data type D is a pair (e;) where e is an
elementof D andis alabelling function :Acc(e)!W suchthat : 8a2
dom(e); (a)2
:
2. Atrace(e;) isnite ifeisnite andalllabelsarenite, i.e. rge()
:
3. The ordering on traces is given by : (e;) (e 0
; 0
) i e e 0
and 8a 2
Acc(e)(a) 0
(a)and 8a2dom(e)(a)= 0
(a):
type. The setof tracesisdenotedbyT.
5. ThesetofnitetracesoverD isdenotedby T
f
(D). Thesetofnitetracesis
denotedby T
f .
6. Let ebe an elementof D andxbealetter. The trace (e;) where (a)=x
a
forall a2Acc(e)willbedenotedas e[x]. Atracease[x]iscalledanelement
namedx.
7. Let t=(e;) beatrace. eiscalled the valueof tandisdenotedby Val(t):
iscalledthe labellingof tandisdenoted bylab(t):
Commentand notations
1. Thelabellingofacellintuitivelycodesthepartofthecomputationthat has
beenmadetogetthecontentofthiscell. Thiscomputationhastobeniteif
theconstructoriseventuallyfound(i.e. ifthecellislled). Otherwiseitmay
beinnite.
2. Let t=(e;)beatrace. Byextendingthefunctionefora2Acc(e) dom(e)
bye(a)=?onemayconsiderthat atraceisatreewhosenodesarelabelled
byapair: therstelementiseitheraconstructoror?andthesecondelement
isaword.A tracethasthusoneofthetwoshapes.
(a) A single accessibleaddress (the empty sequence) which is unlled and
labelledwiththewordw2W:Thiswillbedenotedas: t=(?;w):
(b) A tree whose root is (c;w); where c is an n-ary constructor, w 2
and each of the n branches is another tree. This will be denoted as:
t = h(c;w) t
1
;:::; t
n
i: This case has a degenerate situation when c is
terminal. Then, the only accessibleaddress is " and we simply write :
t=(c;w):
3. Thenamed elementsof N are(since thetree hasonly onebranch, I do not
writethe"h"and"i"):
S n
(0)[x]=(S;x
0 )(S;x
1 ):::(S;x
n 1 )(0;x
n ).
S n
(?)[x]=(S;x
0 )(S;x
1 ):::(S;x
n 1 )(?;x
n )
S
!
[x]=(S;x
0 )(S;x
1 ):::(S;x
n ):::
4. Lett=(e;) beatrace. Acc(t)willdenoteAcc(e).
5. Weoftenwillhaveto "choosefresh letters"andforthat itcouldbeusefulto
ensurethat thealphabet (i.e. theset oflettersoccurring in thetokens)ofa
traceisnite. Since thisintroducesonlyinessentialproblems,I willnotcare
hereaboutthis.
Proposition6 T(D)withitsorderingformsadomain. In particular:
1. Everytraceisaleastupperbound(denotedbySup)ofanincreasingsequence
ofnite traces.
2. Everyincreasing sequencehas aSup.
Proof. Immediate.
Aprimitiverecursivedenitionf inducesafunctiononthedomainassociatedto
thecorrespondingdatatype. Theproposition9showsthatitalsoinducesafunction
(denoted by[[f]])onthecorrespondingtraces. Itisthestudy ofthisfunctionthat
will allowto understandtheintensional behaviouroff: