• No results found

Complexity bounds for container functors and comonads

N/A
N/A
Protected

Academic year: 2021

Share "Complexity bounds for container functors and comonads"

Copied!
16
0
0

Loading.... (view fulltext now)

Full text

(1)

Kent Academic Repository

Full text document (pdf)

Copyright & reuse

Content in the Kent Academic Repository is made available for research purposes. Unless otherwise stated all

content is protected by copyright and in the absence of an open licence (eg Creative Commons), permissions

for further reuse of content should be sought from the publisher, author or other copyright holder.

Versions of research

The version in the Kent Academic Repository may differ from the final published version.

Users are advised to check http://kar.kent.ac.uk for the status of the paper. Users should always cite the

published version of record.

Enquiries

For any further enquiries regarding the licence status of this document, please contact:

[email protected]

If you believe this document infringes copyright then please contact the KAR admin team with the take-down

information provided at http://kar.kent.ac.uk/contact.html

Citation for published version

Orchard, Dominic A. (2018) Complexity bounds for container functors and comonads. Information

and Computation . ISSN 0890-5401.

DOI

https://doi.org/10.1016/j.ic.2018.05.008

Link to record in KAR

http://kar.kent.ac.uk/66631/

Document Version

Publisher pdf

(2)

Contents lists available atScienceDirect

Information

and

Computation

www.elsevier.com/locate/yinco

Complexity

bounds

for

container

functors

and

comonads

Dominic Orchard

UniversityofKent,UnitedKingdomofGreatBritainandNorthernIreland

a

r

t

i

c

l

e

i

n

f

o

a

b

s

t

r

a

c

t

Articlehistory:

Received11August2015 Availableonlinexxxx

Abbottetal.’snotionofcontainerscharacterisesasubsetofparametricdata typeswhich can be describedby aset of shapesand set ofpositions for eachshape.This captures commondatatypessuchastuples,lists,trees,arrays,andgraphs.Varioususefulcategorical structurescanbederivedforcontainersgivenadditionalinformation.Forexample,directed

containers give rise to containercomonads (Ahmanet al.).Containers provide auseful reasoning principleand asbtraction mechanismforprogramming. Thispaper studiesthe performancecharacteristicsoftraversalschemesovercontainers,modelledbyfunctorand comonad structures. A cost model for container transformations is defined from which complexity bounds for the operationsof container functors and comonadsare derived. These bounds suggest optimisations for programs structured using these idioms. The abstract interface provided by the syntax of containers and category theory provides complexity bounds and subsequent optimisations that are implementation agnostic (machinefree).

2018TheAuthor.PublishedbyElsevierInc.ThisisanopenaccessarticleundertheCC BYlicense(http://creativecommons.org/licenses/by/4.0/).

1. Introduction

Consider the following two program fragments, written in some imperative language, where

A,

B,

and

C

are one-dimensionalarraysand

f

and

g

arepurefunctions:

for i = 1..n for u = 1..n B(u) = f(A, u) C(i) = g(B, i) for i = 1..n B(i) = f(A, i) for i = 1..n C(i) = g(B, i) (1)

The two programs are extensionallyequivalent; given thesame inputs, they compute thesame result. However, thetwo programsarenotintensionally equivalent;theleft-handprogramhasworseperformance.Thisinefficiencyisrevealedbyan execution-timeanalysis. Therightprogramhasexecutiontime

∈ O(

n

[f]

+

n

[g])

where

[f],

[g]

aretheexecutiontimesof

f

and

g.

Theleftprogramhoweverhasexecutiontime

∈ O(

n2

[f]

+

n

[g])

wherethenestedloopfor f performsredundant recomputation. Given this computational complexity information, it would be reasonable foran optimising compiler to transformthe left program into theright, since this leads toa potential asymptotic improvement.Ifthe termn

[g]

does not dominate(grow fasterthan)eithern2

[f]

orn

[f]

thenthe transformationfromleft to rightis an improvementfrom quadratictolineartimeinn.Otherwise,ifn

[g]

isdominant,theoptimisationdoesnotmakethecomplexityanyworse.

This paper studies a generalisation of the above example and reasoning (and we return to the above example later, Example6,p. 12). Weconsider a generalclassof parametricdata typesknown ascontainers,ascharacterisedby Abbott

E-mailaddress:[email protected].

https://doi.org/10.1016/j.ic.2018.05.008

0890-5401/2018TheAuthor.PublishedbyElsevierInc.ThisisanopenaccessarticleundertheCCBYlicense (http://creativecommons.org/licenses/by/4.0/).

(3)

et al. [1,2] (which includes arrays, lists, labelled trees andgraphs) and consider functor and comonad structures which model common traversalschemes over containersakin to map and gather patterns. We give complexity boundsfor the operationsofcontainerfunctorsandcomonads.Fromthesecomplexitybounds,theusualaxiomsoffunctorsandcomonads can beconvertedtooptimisingrewriterules whichcanbeexploitedbyaprogrammer orcompiler(or byalibrarywhich canspecifyrulestothecompiler,e.g.,asprovidedbytheGlasgowHaskellCompiler’srewriterulessystem [19]).

Westudyfunctorssincetheycapturethecommonprogrammingpatternofanelement-wisetraversalofadatastructure,

e.g., themap function onlists,ortransforming everyelement ofan array [21]. Westudycomonads sincethey generalise afunctor’straversal, capturinglocaltransformations [17,18,23] whichmaydependonanelementanditsneighbours,(often calledagather [9]),e.g.,convolutions,fluidsimulations,andcellularautomatasuchastheGameofLife.Westudycontainers with additionalstructure, calleddirectedcontainers, whichinduce a comonadforthe container, asintroduced by Ahman, Chapman,andUustalu [3].Thenoveltyinthisworkistheanalysisofthecomplexityofthesederivedfunctorandcomonad structuresoncontainers.

Our generalmotivationis toprovide abasis forderiving compiler optimisations oncontainer-based programs. Tothis end, weconsidertwovarietiesoffunctor/comonadstructurethroughout:(1)thosederived from(directed)container struc-tures and(2) thosethat are abstract (non-derived),where theimplementation isunknown (as inan abstractdata type). An exampleabstract situationmaybe apiece ofcodeparameterised by a (container)functor, e.g., an overloadedHaskell function oftype

f

::

(Container

c,

Functor

c)

=>

c

a

->

...,

wherewe wish toreasonaboutcomplexity of

f

regardlessoftheunknowninstantiationofthecontainerfunctor

c.

Intheabstractcase,complexityboundssimilarto thoseforthederivedstructurecanbecalculated,butwithsomeapproximation.Thisprovidesakindofimplicit-complexity measure, whichismadeexplicitinthederived structurewheretheimplementationisgiven. Complexitymeasures inthe abstract settingenable optimising compilationeven inthe presenceof abstractcontainer parameters, oras-yet-unknown implementationsofcontainersandtheirassociatedstructures.

The following section introduces preliminarydefinitions ofcontainers, container cost analysis, andnotation. Section 3 andSection4studycontainerfunctorsandcomonads,andtheircomplexity.Section5considersrelatedworkandSection6 concludeswithsomefurtherdiscussion.

2. Preliminaries

2.1. Containers

The notionof a container corresponds to parametric data types that contain onlystrictly positive occurrences of the parameter type. An alternate characterisation is that containers comprise a set ofshapes and a set of positions foreach possibleshape [1,2].Sincewe studycomplexityboundsonoperationsovercontainers,we restrictourselvestoasubsetof containersthathavefinitesetsofpositionsforeveryshape(butthenumberofshapesmaybeinfinite)Weintroducehere themaindefinitions,butmoredetailcanbefoundintheworkofAbbottetal. [1,2].

Section4introducestheadditionalstructureofdirectedcontainers.

All definitions are made in some base category

C

assumed to be Cartesian closed and locally Cartesian closed. The

λ-calculus

isthereforeusedastheinternallanguageof

C

forconvenience.

Definition1(Containers).AcontainercomprisesasetofshapesS

:

Set andashape-indexedfamilyofpositions P

:

S

Set,

often writtenas thepair S

P .Shapes can be thought ofastemplates fora datastructure where positionsidentify the “holes”thatcanbefilledwithdata.

Forthesakeofgenerality,wecanreplaceSet with

C

above,situatingacontainerwithinsomearbitrary(locally)Cartesian closedcategory.Thereadermayinstantiate

C

toSet iftheywishtothinkconcretely,andourexamplesareallwithinSet. Example1(Lists).Arunning examplewill be the datatype of lists,defined by thecontainer

N

Fin.Shapes are natural numbers,corresponding tothelengthofalist,andpositions aredrawnfromfinitesets,whereforashape s

: N

positions areFin s

= {0,

. . . ,

s

1}.

Definition 2 (Containerdatatype).From a container S

P , a parametric data type (object mapping on

C

) is defined

J

S

P

K

A

= 

s

:

S

.(

P s

A

)

as a dependent sum of a shape s

:

S and a valuation from positions in the shape (taken from P s)tovalues A.Thesevaluesarecalledtheelements ofthecontainer.

Throughout,a tuplenotation

(

s

,

v

)

willbe usedforinhabitantsofthedependent sumofa containertype, withshape

s

:

S andvaluation v

:

P s

A.Thesetuples

(

s

,

v

)

willbereferredtoascontainervalues.

Definition3(Finitecontainers).Acontainer S

P isfinite ifforeveryshapes

:

S itspositionset P s isfinite.Thatis,thereis anaturaltransformationsizeA

:

J

S

P

K

A

→ N,

defined:

(4)

wherethesizeofacontainervalueisthecardinalityofthepositionsetforitsshape.Finitenessnecessarilyimpliesthatthe setofpositionsisdiscrete.

Listsare finitecontainers since forevery shape s

: N

(length) the set ofpositions is the finite set

{0,

. . . ,

s

1} (see Example1above).Notethatlistsarestillfinitecontainersdespitetherebeinganinfinitenumberofshapes.Othercommon containersinprogramming(trees,arrays)followasimilarscheme:foragivenshapethereareafinitenumberofpositions.

Example2(Streams).The stream container has a single shape, denoted

∗,

whose positions are drawn fromthe natural numbers,i.e.,Stream

= {∗}



(

const

N).

Thus,streamsarenotfinitecontainers;streamsareinfinite.

Finitecontainersrelate toGirard’sdefinitionofanormalfunctor (representablebyapowerseries) [8],whichare equiv-alenttocontainerfunctorswithacountablesetofshapesandfinitesetsofpositionsforeachshape [2].Whilsttheabove definitionoffinitecontainersisnotrestrictedtocountablymanyshapes,thisissufficientforourpurposessince weneed notenumerateallshapes.Inanycase,alltheexamplesinthispaperhappentohavecountableshapessincethisiscommon inprogramming.

Forbrevity,thewordcontainer willreferthroughouttofinite containersunless explicitlystatedotherwise.We restrict thedevelopment tofinitecontainerssincethereisa clearnotionofinputsize which givefiniteboundsonthecomplexity ofwhole-structuretraversals.WediscussinfinitecontainersbrieflyinSection6,usingthestreamcontainerexample.

Example3(Realfunctions).For completeness sake, an example of an infinite and non-discrete (continuous) container is thecontainer ofmulti-dimensionalreal-valued functions

N



n

.R

n

)

whereshapescountthenumberofdimensionsand positionsarevectorsofrealnumbers.Wedonotconsidercomplexityboundsforsuchcontainerssincethereisnonatural measureofinputsize.

2.2. Costsemantics

Acostmodelforcontainervaluesiskeytotheresultsofthispaper.

Forafunction f

:

A

B,itsexecutiontimeon inputs A ofsizen willbe denoted

[

f

]

n.Wedonotnecessarilyhavea modelforthe executiontimeofevery f intheunderlyingcategory, nora sizefunction for A values. Therefore,insome cases,thisexecutiontimewillbeleftabstract.However,functionsoncontainersop

:

J

S

P

K

A

B willhavetheirexecution time

[op]

n givenconcretelybyacostmodelfunction

[−]

whichmapscontainertermsto

N

,where:

[

op

]

n

= [

op

(

s

,

v

)]

iff

∃(

s

,

v

) :

J

S

P

K

A

sizeA

(

s

,

v

) =

n

Thecostingfunction

[−]

isrecursivelydefinedoverthesyntaxoftermsusedtodefinecontainer operationsop whichisa subsetof

λ-terms

(Proposition1):

Proposition1.Containeroperationsop (mappingfromcontainervaluestocontainervalues)aredefinedinthispaperintermsofthe followingsub-grammarof

λ

-calculustermst extendedwithpositiontermsp,shapetermss,andabstractfunctionsf whichareeither parameterstoop orinputvaluationsv:

t

::= (

s

,

t

) |

x

| (λ

xp

.

t

) |

p

|

f t

| (λ

xp

.

t

)

p (2)

Thatisop(s

,

v

)

=

t wheretermst areeithercontainervalues

(

s

,

t

)

,variables,

λ

-termsabstractingoverpositions(whereposition variablesaredenotexp),positionterms,

β

-redexesonabstractfunctionsorvaluationsf ,or

β

-redexesofa

λ

-termwithaposition.

Definition4(Costmodel).Thecostfunction

[−] :

t

→ N

assignsacosttocontaineroperations.

The costforinput valuationsorany(universallyquantified)function f that parameterisesacontainer operation op is keptabstractintermsof

[

f

]

n.Thisprovidesaflexiblecostsemanticsthatcanbeinstantiatedforparticularabstract-machine modelsviainstantiationsof

[−]

n.Theonlyassumptionweplaceon

[−]

n isthatitismonotoniconitsinputsize.Thecosting isdefined:

[(

s

,

t

)] =



p:P s

[

t p

]

[

x

]

=

0

xp

.

t

]

=

0

[

p

]

=

0

[

f t

]

=

1

+ [

f

]

|t|

+ [

t

]

[(λ

xp

.

t

)

p

]

=

1

+ [

t

[

p

/

xp

]]

(3)

The first clause gives a cost analysis of a container value

(

s

,

t

)

: 

s

:

S

.(

P s

A

)

which enumerates every position for theshape s,summingthecostofevaluatingthecontainer valuationt ateach position.Therefore,givenacontainer value

(5)

(

s

,

v

)

ofsizen

= |

P s

|

(numberofpositionsatthepresentshape), if

p

:

P s

.

[

vp

]

∈ O(

f

)

itfollowsthatthecomplexityof evaluatingthecontaineris

[(

s

,

v

)] ∈ O(

nf

).

Variables, position terms, andnaked

λ-terms

cost nothing.Thus complexity measures inthis paperdo not take into accountthedifferentsizesofpositions,whichareinsteaduniformlytreatedashavingaconstantunitsize

|

p

|

=

1.

The applicationof an abstract function or valuation f to a termt is defined using the parametercosting

[−]

n. This connects theconcrete costing functionto theabstract computation-time,wherethe costof applicationis

[

f

]

|t|

(parame-terisedbythesizeofitsargument)plustheevaluationcostoft andplus1 tomarkthecomputationstepinvolvedinthe

β

-reduction ofthe application. A similar costing is usedby Wadler [26, §3] forstrict timing analysis, wherethe cost of functionapplicationisthecostofthefunctionbodyplusone,plusthecostofthearguments.

The costofa

β-redex

withaposition argumenthasthe costofoneplus thecostofthe reducedterm(withsyntactic substitution

[

p

/

xp

]

oftheposition p forvariable xp). Thismatches theideathat positions areofconstant sizeandincur onlyaconstantcostwhensubstituted.

Counting

β-reductions

isknowntobeanimprecisecostmodelforthe

λ-calculus

asthenumberofsyntacticsubstitutions requiredisnotconstantandthesizeofsubstitutedtermsisnotaccountedfor(seethediscussionin [6]).However,inthe abovedefinition,theabstractcosting

[−]

nprovidesthemaincostandcanbeinstantiatedwithsomemorepreciseabstract machine model.The countingof

β

-reductions isnot usedubiquitouslyin thecosting, onlyto accountforapplications to position constants orapplicationsofabstract(parameter)functions. Thekey pointofthe costing

[−]

isto allowthe cost of a valuation to be scaled by the numberof elements inthe container. As shall be noted throughout, the counting of

β

-reductionsincurredby f t or

x

.

t

)

p endsupbeingsubsumedbythecostsoftheabstractfunctions f beingscaledbya functionoftheinputsizen.

Definition5(Structuralsizes).For nested datastructures, the notation n

[≤

m

]

denotes a container value of size n whose

elements areboundedabovebysizem. ThisissimilartothenotationofSkillicornandCaiwhowritethesizeofanested datastructureasalistofelements

[

n

,

m

]

meaningtheouterlayerhassizen andtheinnerelementsofsizeatmostm [22]. Similarly, n

[

m

≤]

denotes a container value of size n whose elements are bounded below by size m (i.e.,at least of size m).

Proposition2.Consideranextensionalequalityf

g betweentwoprogramfragments f andg (thatis,theyhavethesameoutput forthesameinput).If

[

f

]

n

∈ ([

g

]

n

)

(orequivalently

[

g

]

n

∈ O([

f

]

n

)

)thentheequalitycanbeorientedasarewriterulefromleftto

rightas f

;

g whichimprovestheasymptoticcomplexityoftheprogram.Thusthisrewriterulemayprovideaprogramoptimisation.

Section 3and Section 4 studyfunctor andcomonad structureson containers. In both, we consider derived instances of thisstructure, where the definitionis givenexplicitly, andabstract instances ofthe structure, wherethe definition is unknown.Forbothfunctorsandcomonads,wecharacterisewhatitmeanstobederivedandabstract.Inthecaseofabstract containercomonads,wecanstillreasonaboutcomplexitybasedontheaxiomsofthecomonadstructure.

3. Functors

Weconsidercontainerswhichhavetheadditionalstructureofafunctor.Themorphismmappingofacontainerfunctor capturesthecommonprogrammingpatternofapplyingafunctiontoeveryelement ina containerdatatype,generalising themap combinatorforlists.

Definition 6(Derivedcontainerfunctor).Every container S

P induces an (endo)functor

J

S

P

K

: C → C

(for some base category

C

),definedonobjectsandmorphismsrespectivelyby:

J

S

P

K

A

= 

s

:

S

.(

P s

A

)

(asinDefinition2);

J

S

P

K

f

(

s

,

v

)

= (

s

,

f

v

)

where f

:

A

B,s

:

S andv

:

P s

A.

Thus, the morphismmapping isthe post-compositionofthe morphism f

:

A

B withthe valuation, givingacontainer value

J

S

P

K

B oftransformedelements.

Notethat thecomposition f

v canbe equivalentlyexpressedas

λ

p

.

f

(

vp

),

i.e.,atermintherestrictedsubsetofthe

λ-calculus

usedtoconstructvaluations(seeProposition1).

Theorem1(Containerfunctorcost).ForacontainerS

P ,themorphismmappingofitsfunctorF

=

J

S

P

K

hascomplexity:

[

F f

]

n[≤m]

O

(

n

[

f

]

m

+

n Qn

)

forallf

:

A

B andforsomecostfunctionQnoftheinputsizen whichcharacterisesthetimetocomputevaluationsateachposition

(the indexingtime).Thus,theexecutiontimeofamorphismmappingislinearinn timesthecostof f (thetransformation)plusthe costofindexing.

(6)

Proof. Followingfromthedefinitionofcontainerfunctors(Definition6)andthecostfunction(Definition4,p.3),thenfor some

(

s

,

v

)

:

J

S

P

K

A wheren

= |

P s

|

and

p

:

P s

.

|

vp

|

m (i.e.,elementshavesizeofatmostm),then:

[

F f

(

s

,

v

)] = [(

s

,

f

v

)] = [(

s

, λ

p

.

f

(

v p

))] =



p:P s

([λ

p

.

f

(

v p

)])

=



p:P s

([

f

(

v p

)] +

1

)

=



p:P s

([

f

]

|v p|

+ [

v p

]

+

2

)

{

since

|

p

| =

1

}

=



p:P s

([

f

]

|v p|

+ [

v

]

1

+

3

)

=

n

[

f

]

|v p|

+

n

[

v

]

1

+

3n

{

since

|

v p

| ≤

m

}

n

[

f

]

m

+

n

[

v

]

1

+

3n

{

assuming

[

f

]

m

>

0 and

[

v

]

1

>

0

}

⇒ [

F f

(

s

,

v

)] ∈

O

(

n

[

f

]

m

+

n

[

v

]

1

)

{

let Qn

>

0 be upper-bound cost of

[

v

]

1

}

[

F f

]

n[≤m]]

O

(

n

[

f

]

m

+

n Qn

)

Note, whilst

[

v

]

1 is parameterised by constant input size 1, its cost depends also on the container value size n since

v

:

P s

A,hencethetermQ isparameterisedbytheinputsizen.

2

Remark1.The cost to reduce the term

p

.(

f

(

vp

))

p in the resulting valuation is givenas 3n byour costing function. However,thiscostissubsumedbythecostsn

[

f

]

m andn

[

v

]

1.

Corollary1.Theorem1assumesthattheelementsoftheinputcontainerhaveawell-definedsizewhichisatmostm.Ifthereisno suchsizedefinedfortheelements,thetheorem(anditsproof)stillholdsbutwithm

=

1, i.e.,

[F

f

]

n

∈ O(

n

[

f

]

1

+

n Qn

)

.

The resultofTheorem1 isstraightforward fromthedefinition andwell-known,atleasttacitly,for concreteinstances ofcontainerssuchaslists,trees,andother inductivetypes(where Qn isconstant).However,inthecontextofan optimis-ing compiler,the implementationofa container functormaybe unknown, ratherthan derivedasin Definition 6.Whilst a container functor maybe extensionally equivalent to that ofDefinition 6, it maydiffer intensionally. Forexample, an inefficientimplementationforlistsmightuse alinear-time lookupforeach element(based onits index),ratherthanthe usualrecursivedefinition;or,listswithrepeatedelementsmightbestoredmoreefficientlywitharun-lengthencodingstyle implementation.However,wecanstillderiveusefulcomplexityboundseveninthisabstractcase,withsomerestrictionon whatitmeanstobeanabstractfinitecontainerfunctor.

Definition7.AnendofunctorF

: C → C

isanabstractfinitecontainerfunctor ifthereexistsafinitecontainer S

P suchthat F

= J

S

P

K,

thatisthereexistsanaturalisomorphism,with

α

A

:

F A

J

S

P

K

A andinverse

α

A1

:

J

S

P

K

A

F A.

Remark2.Anabstractfinitecontainervaluehasadefinitionofsizeviatheisomorphismtosomeconcretefinitecontainer. Thatis,size′A

:

F A

→ N

isgivenbysize′A

=

sizeA

α

A.

Wefirstshowalowerboundforsuchabstractcontainerfunctorsintheworstcase.

Proposition3.LetF beanabstractfinitecontainerfunctor,isomorphicto

J

S

P

K

.Assumingthatthecosting

[F

f

]

nisdefined,then

themorphismmappingofF hasworst-caselower-boundcomplexity

[F

f

]

n

∈ (

n

[

f

]

1

)

,wheretheinputsizen iscalculatedbysize′A.

Proof. Theisomorphismtoaconcretecontainerexplainstheextensionalbehaviouroftheabstractcontainerfunctor;bythe isomorphism F

= J

S

P

K

itfollowsthat Ff

=

α

−1

B

J

S

P

K

f

α

A forsome f

:

A

B.Bythedefinitionofthemorphism mapping ofconcrete containerfunctors

J

S

P

K

f

(

s

,

v

)

= (

s

,

f

v

),

it followsthat F f extensionally preservesthe size of theincomingcontainerandapplies f toeveryelementinacontainervalue.Thus,

[F

f

]

nisboundedbelowbyn

[

f

]

1 inthe

worstcase.

2

Remark3.Theabove propositionisstatedasa worst-case lowerbound.Thisaccountsforfunctorimplementationswitha tighter lowerbound complexityforsome inputsbyutilisingfeatures oftheunderlyingelements. Forexample,acompact representationcouldbeprovidedforlistswithsharedorrepeatedelements.Aworst casescenarioforsuch an implemen-tationwouldbealistwithnocommonelements,inducingthelinearlowerbound.

(7)

A lower-bound complexity result is interesting, but not particularly useful for optimisation purposes: given two ex-tensionally equivalent programs f

g if we onlyknow two lower-bounds

[

f

]

n

∈ (

t

(

n

))

and

[

g

]

n

∈ (

t

(

n

))

we cannot determine whethereither f

;

g or g

;

f providesanyimprovement;upperboundsaremuch moreuseful.However, in theabstractsettingwecanonlygiveanupperboundparameterisedbyrepresentationsofadditionalunknowncosts.

Theorem2(Abstractcontainerfunctorcost).LetF beanabstractfinitecontainerfunctor.Itsmorphismmappinghasupper-bound complexity,whereforall f

:

A

B

[

F f

]

n[≤m]

O

(

n

[

f

]

m

+

n Qn

+

Rn

)

whereQnisafunctionoftheinputsizen representingthetimetoaccesseachelementinthecontainer(thetimetoevaluatea

valua-tion).ThetermRnisafunctionofn andactsasa catchall:wecannotruleoutthatpossibilityofsomepathologicalimplementations

withredundantexpensivecomputationwhosecomplexityfaroutweighsthe

(

n

[

f

]

m

+

n Qn

)

term.

Ifthereisnowell-definednotionofsizeforelementsA,thenm

=

1 (seeCorollary1,p.5).

Proof. Thisresultbuildsonthelower-boundcomplexityofF (Proposition3)whichwasbasedontheextensionalbehaviour of F viaitsisomorphism toaconcrete container.An upperboundmustbe atleastasbigasthelowerbound, hencethe firstpartofcomplexityboundhereisn

[

f

]

m.

Intheisomorphicconcretecontainer,accessinganelementhasconstanttimebutthismaynotbethecaseinF.Instead, the cost of accessing each element is accounted for by the termn Qn (since there must be at least n accesses by the earlierargument).Finally,thefactor Rn providestheopportunityforanyadditionalwastefulcomputationtodominatethe linear-timeportion.

2

Remark4.Theabovetheoremisquiteweak:the Rn factormeanswe donothaveaconcreteupperbound.However,this still provides a bound that can beused to reasonaboutoptimisations. (A similarsituation occurswithcomonads inthe nextsection.) Wecananalysetwocases:when Rn isdominant,andwhenitisnot,andshowthatanoptimisationdoesnot getasymptoticallyworseevenifRn isdominant.

Optimisation1(Functoroptimisation).ForanabstractorderivedcontainerfunctorF,theaxiomFid

idFcanbeorientedas

anoptimisingrewriteruleFid

;

idF.

Proof. Straightforwardforeitherderivedorabstractcontainerfunctors.LetidF

(

s

,

v

)

= (

s

,

v

),

whichinourmodelhascost:

[

idF

(

s

,

v

)] = [(

s

,

v

)] =



p:P s

[

v p

]

=



p:P s

(

1

+ [

v

]

|p|

+ [

p

]) =



p:P s

(

1

+ [

v

]

1

) =

n

+

n

[

v

]

1

Thus,let Qn

= [

v

]

1thenidentityhascomplexity

[

idF

]

n[≤m]

∈ O(

n Qn

)

and

[Fid]

n[≤m]

∈ O(

n

[

f

]

m

+

n Qn

+

Rn

)

whereRn

=

0 in thecaseofderivedfunctors.Therefore,regardlessofwhetherRndominatesornot,therewritingFid

;

idFisanoptimisation sinceiteitherreducesasymptoticcomplexity(if

[

f

]

m dominatesQn)ormaintainsit.

2

Remark5.Onemightexpect thecostofthe identityfunction tobe constant,i.e.,

x

.

x

)

∈ O(1).

However,we make com-plexityclaimsrelativetoourcostmodelofcontaineroperations.Themodelessentiallyincludesanevaluationofacontainer value ateveryposition(inthecurrentshape),thusidentityonacontainervalueisnotconstant,butrequirestheresulting containertobeevaluated.

The remaining functor axiom F(g

f

)

=

Fg

F f is exploited inthe classic deforestation transformation Fg

F f

;

F(g

f

)

[27].Performanceisimprovedbyeliminatingtheintermediatedatastructurebetweenthetwomorphismmappings andperformingonetraversalinsteadoftwo.However,thisrewriteisnotjustifiedviatheresultsheresincethecomplexity of each termis the same; asymptotic boundscannot distinguishn from 2n.To justify thisaxiom (in a general setting) requiresamorefine-grained(non-asymptotic)costinganalysisoutsidethescopeofthispaper.

4. Containercomonads

Themorphismmappingofthefunctorconstructionforcontainerscapturestheprogrammingpatternofanelement-wise traversal,applyingatransformation f

:

A

B toeachelement.Comonadsgeneralisethiselement-wisetraversalbyallowing atransformationtodependnotjustonasingleelement,butonanelementanditsneighbours.Thefollowingcomparesthe signaturesofthemorphismmappingforafunctor(left)withtheextension operationofacomonad(right):

functor f

:

A

B

F f

:

F A

FB comonad

g

:

F A

B g

:

F A

FB

(8)

On theleft, the functor applies themorphism f pointwise, where f computes a B value froma single A value. Onthe right, extension applies themorphism g contextwise,where g computes a B value frompossiblymultiple A values from the“context” providedbyF A. Thatis,F A ismorethanjustacontainerfunctor, itisacontainer functorwitha notionof context, such asa pointer to a particularelement. Thus, g can accessmore thanjust a single element, andistherefore describedasacontext-dependentcomputation (see [18]).Forexample,thekernelfunctionofaGaussianblurtakesthemean ofanelementatthecurrentcontextanditsimmediateneighbours.

Definition8(Comonads).ForanendofunctorF,acomonadisatriple

(F,

ε

,

(−)

)

whichcomprises

anaturaltransformation

ε

A

:

F A

A,calledthecounit;

anextension operation,(uniquely)mappingmorphisms f

:

F A

B tomorphisms f

:

F A

FB satisfyingtheaxioms:

[

C1

]

ε

A

idF A

[

C2

]

ε

B

f

f

[

C3

]

g

f

≡ (

g

f

)

Extensionlifts(or,extends) amorphism f

:

F A

B whichmodelsa computationlocalisedtoan incomingcontext F A, toaglobaloperation f

:

F A

FB byapplying f ateverypossiblecontextwithintheincomingFA value.FortheGaussian blurexample, extension would apply the kernel function to thearray focussed at every possible indexin the array.The counit

ε

A

:

F A

A definesthenotionofacurrentcontext atwhichanelementislocatedandprojectedoutby

ε

.Thisacts astheidentityforextension[C1],[C2].Thethirdaxiom[C3]definesassociativityof

(−)

†.

Example4(Non-emptylistcomonad).Non-emptylists have a comonad structure, where

ε

takes the headof the list and extensionappliestheparameterfunction f

: [

A

]

B tosuccessivesublistsoftheinputlist,defined:

ε

xs

=

head xs fxs

=



[

f

[

x

]]

xs

= [

x

]

(

f xs

) :

f

(

tail xs

)

otherwise

Thus,the“context”ateachelementofthelistisitssuffix,towhichextensionapplies f .Therequirementthatthelistsare non-emptyissuchthat

ε

istotal.

Wefocus firstonthe axiom[C3] g

f

≡ (

g

f

)

† which reassociatesextension. Thenesteduseofextension onthe right-handsidesuggeststhepossibilityofaquadraticdifferenceincomplexitycomparedwiththeleft, whichhasnosuch nesting.Thisisexactlythesituationdescribedintheintroductoryexample(Section1).Thiskindofnestingcaneasilyarise duringprogramconstructionandcanleadtosignificantperformanceissues.Therefore,itispreferabletoautomatically elim-inatethisnesting (byacompiler)givenaguaranteethat itisalwaysan improvement.Weinferthecomplexity difference betweenthe two sidesof [C3]from the comonad axiomsandshow that the rewrite

(

g

f

)

;

(

g

f

)

is indeed an optimisation.

Proposition4(Shapepreservation).Theextensionoperationofacomonadis shape preserving [18,16],similarlytothemorphism mappingpartofafunctor.Thatis,foranabstractnotionofshapegivenbyshapeA

:

F A

F1 definedastheliftingoftheterminal

morphismshapeA

=

F!A(alsousedbyJayandCockett [14]),thenforallf

:

F A

B:

shapeB

f

=

shapeA

Thus,thecomonadicextensionofanymorphismf

:

F A

B preservestheshapeoftheinputvalueinitsoutput.

Proof. By[C1]

ε

A

idF A,forall f

:

F A

B then:

shapeB

f† def

F

!

B

f† ext

≡ (!

B

f

)

!A

≡ (!

A

ε

A

)

† ext

F

!

A

ε

A

[C1]

shapeA

where the step (ext) followsfrom F f

≡ (

f

ε

)

† (proof not shown) and[C2], [C3],and

!

A isthe universal morphism of terminalobjects.

2

Corollary2(Sizepreservation).Forafinitecontainercomonad,theextensionoperation

(−)

is sizepreserving,thatis:

sizeB

f

=

sizeA

Proof. SinceshapeA

=

F!A,bynaturalityofsizeA itfollowsthat size1

shapeA

=

sizeA.CombiningthiswithProposition4 givessizepreservation:

sizeB

f† natur.

size1

shapeB

f† Prop 4

size1

shapeA natur.

(9)

Thesizepreservationofcomonadicextension isausefulpropertyforderivingcomplexity boundsonextensionandhas thefurthercorollaryaboutcost:

Corollary3.ForafinitecontainercomonadonF andforanymorphisms f

:

F A

B andg

:

B

C thenthefollowingpropertyholds fortheexecutiontime:

[

g

f

]

n

= [

g

]

n

+ [

f

]

n

Thatis,since fissizepreserving,theoutputsizeof fisthesameastheinputandthereforetheexecutiontimeofthecomposition

canbedecomposedwith

[

g

]

parameterisedbysizen.

Similarly tofunctors inthelast section,both derived andabstract structuresare consideredin thissection. Comonads canbederivedfromcontainerswithsomeadditionalstructure.

4.1. Directedcontainers

The subclassofcontainersknownasdirectedcontainers hasadditionalstructureonshapesandpositionswhichinduces acontainercomonad [3].Theadditionalstructuredescribesasystemofsubshapes relatedtoeachpositioninashape.

Definition9(Directedcontainers).Acontainer S

P iscalleddirected ifithasthefollowingadditionaloperationsfor manip-ulatingshapesandpositions [3,4]:

o

: {

s

:

S

}.

P s –aroot positionforeveryshape(theshapeparameters isgivenimplicitly)1;

• ↓: 

s

:

S

.

P s

S –computesthesubshape ataparticularposition;

• ⊕

: {

s

:

S

}.



p

:

P s

.

P

(

s

p

)

P s –givenaposition pinasubshapestartingat p,then p

pgivesthepositionof

pwithintheparentshape.

Thesesatisfythefollowingtwoequationsonshapes[S1–2]andthreepositionalequations[P1–3]: [S1]s

o

=

s

[S2]s

↓ (

p

p

)

= (

s

p

)

p

[P1] p

o

=

p

[P2]o

p

=

p

[P3]

(

p

p

)

p′′

=

p

⊕ (

p

p′′

)

The shapeparametershavebeenkeptimplicitintheaboveaxioms, but[P3],forexample,canberenderedwithits shape argumentsexplicitas

(

p

⊕{

s

}

p

)

⊕{

s

}

p′′

=

p

⊕{

s

}

(

p

⊕{

s

p

}

p′′

).

Modulothedependenttypes,rules[P1–3]areessentiallythat

ando formamonoidoverpositions,and[S1–2]that

isaright-monoidactionwiththesetofshapes.

Definition 10(Derivedcontainer comonad).Every directed container

(

S

P

,

↓,

o,

⊕)

induces a comonad for the functor

J

S

P

K

: C → C

withcounitandextension:

ε

(

s

,

v

) =

v o

{

s

}

f

(

s

,

v

) = (

s

, λ

p

.

f

(

s

p

, λ

p

.

v

(

p

⊕{

s

}

p

)))

The counitoperation

ε

takesthevaluationattherootpositiono.Theextension f† onacontainer value

(

s

,

v

)

produces a containerwhereateachposition p themorphism f

:

F A

B isappliedtoasub-containerofshapes

p whosevaluation takeselementsatpositionsoffsetbyp

⊕{

s

}.

Example5(Directedlistcontaineranditscomonad).Example1(p.2)definedthelistcontainer as

N

Fin whereshapesare list lengths.Non-empty listsare acontainer with

N

>0

Fin andadirected container withs

p

=

s

p (the subshapeis the (lengthofthe)suffix)ando{s

}

=

0 (theheadelement)and p

p

=

p

+

p′ (theglobalpositionofp′inthesubshape startingat p isitspositionintheentirelist).

This produces a non-empty list comonad structure which is extensionally equivalent to the non-empty list comonad giveninExample4.

(10)

Theorem3(Derivedcontainercomonadcost).Foradirectedcontainer

(

S

P

,

↓,

o,

⊕)

,thederivedcomonadhasextension

(−)

with theupper-boundcomplexity:

[

f

]

n

O

(

n

[

f

]

m

+

nm Qm

)

wherem

=

maxp:P s

|

P

(

s

p

)|

isthelargestsubshapefroms forsomeinput

(

s

,

v

)

:

J

S

P

K

A andQmisthecostofapplying

valua-tions v (parameterisedonthemaximumsubshapesizem).

Thereforethecostofapplyingftoacontainerofsizen isboundedabovebyn timesthecostoff oninputsizem (thesizeoflargest possiblesubcontainerthat f isappliedto),plusnm timesthevaluationcost(thecostofretrievingvaluesfromthecontainerineach applicationof f ).

Proof. Bythederivedcontainercomonaddefinition(Definition10).Letn

= |

P s

|

andQm

= [

v

]

1,then:

[

f

(

s

,

v

)] =



p:P s

(

1

+ [

f

(

s

p

, λ

p

.

v

(

p

⊕{

s

}

p

))])

=



p:P s

(

1

+ [

f

]

|P(sp)|

+



p:P(sp)

(

1

+ [

v

(

p

⊕{

s

}

p

)]))

{

since

|

p

| =

1

}

=



p:P s

(

1

+ [

f

]

|P(sp)|

+



p:P(sp)

(

2

+ [

v

]

1

))

=

n

+

n

[

f

]

|P(sp)|

+

n



p:P(sp)

(

2

+ [

v

]

1

)

{

m

=

maxp:P s

|

P

(

s

p

)|}

n

+

n

[

f

]

m

+

nm

(

2

+ [

v

]

1

)

{

Qm

= [

v

]

1

Qm

>

0

} ∴ [

f

]

n

O

(

n

[

f

]

m

+

nm Qm

)

Sincepositionsareofconstantsize,wetreatthecostof

asconstantalsointheaboveproof.

2

Remark6.Inthe abovetheorem andproof, themaximum sizeof thesubshapes m is

n bythe axiom [C2]

ε

f

f

instantiatedwith f

=

size giving

ε

N

size†A

=

sizeA i.e.,thesizeofthesubshapeatthecurrentcontextisequaltothesize oftheoriginalcontainer (thisisexposedinthedirected containerstructureby axiom[S1]).Therefore,asm isthesize of thelargestsubshape,andthesubshapeattherootpositionisthesizeofn,itfollowsthatm

n.

Theabovetheoremprovidesthebasistoshowthat[C3]canbeorientedasanoptimisation:

Optimisation2.Foraderivedcontainercomonad,axiom[C3]canbeorientedas

(

g

f

)

;

g

f†guaranteeingan asymp-toticimprovementornotmakingthecomplexityworse.

Proof. Assumeinput

(

s

,

v

).

Letn

= |

P s

|,

m1

=

maxp:P s

|

P

(

s

p

)|

(largestsubshapesize)andm2

=

maxp:P s,p:P(sp)

|

P

((

s

p

)

p

)|

(largestsub-subshapesize).ByTheorem3andCorollary3:

[

g

f

]

n

= [

g

]

n

+ [

f

]

n

O

(

n

[

g

]

m1

+

nm1Qm1

+

n

[

f

]

m1

+

nm1Qm1

)

[(

g

f

)

]

n

O

(

n

[

g

f

]

m1

+

nm1Qm1

)

O

(

n

([

g

]

m1

+

m1

[

f

]

m2

+

m1m2Qm2

) +

nm1Qm1

)

O

(

n

[

g

]

m1

+

nm1Qm1

+

nm1

[

f

]

m2

+

nm1m2Qm2

)

Eachbound shares a commonsummand n

[

g

]

m1

+

nm1Qm1 anda commonfactorn on theremaining terms. It therefore

remainstoprove

([

f

]

m1

+

m1Qm1

)

∈ O(

m1

[

f

]

m2

+

m1m2Qm2

)

(theremainingdistinctsubterms).

Thisfollowsfromthepropertythatm1

=

m2,thatisthemaximumsubshapesizeisequaltothemaximumsub-subshape

size,whichfollowsfromthedirectedcontaineraxioms:

1. First,considerageneralpropertyonfunctions:let X andY besetsanda

:

X

Y andb

:

Y

Z befunctionswhere X

andY arefiniteanda issurjective.

Then:

{

b

(

y

)

: ∀

y

Y

}

= {

b

(

a

(

x

))

: ∀

x

X

}

sincea mapstoitsentireco-domainY .

2. From axiom [P1], it follows that

⊕{

s

}

is surjective, mapping to all values in its co-domain P s. That is,

p

∈ (

P s

)

p1

∈ (

P s

)

and

p2

∈ (

P

(

s

p

))

suchthat p1

⊕{

s

}

p2

=

p,where p1

=

p andp2

=

o byaxiom[P1] p

o

=

p.

(11)

3. Instantiatethegeneralproperty(1)withX

= 

p

:

P s

.

P

(

s

p

)

(whichisfiniteduetoourrestrictiontofinitecontainers and hencefinite sets of positions), Y

=

P s (also finite) and Z

= N,

witha

= ⊕

s (which is surjective (2)) andbp

=

|

P

(

s

p

)|

(thecardinalityofpositionsatsubshapes fromposition p).Thus:

{|

P

(

s

p

)| : ∀

p

P s

} = {|

P

(

s

↓ (

p1

⊕ {

s

}

p2

)| : ∀

p1

P s

, ∀

p2

P

(

s

p

)}

whicharebothfinitesetsbythefinitenessof X andY .

4. Fromaxiom[S2],

∀{

s

,

p1

:

P s

,

p2

:

P

(

s

p1

)}

.

s

↓ (

p1

p2

)

= (

s

p1

)

p2 itfollowsthat:

{|

P

(

s

p

)| : ∀

p

P s

} = {|

P

((

s

p1

) ↓

p2

)| : ∀

p1

P s

, ∀

p2

P

(

s

p

)}

5. Letm1

=

maxp:P s

|(

s

p

)|

andm2

=

maxp1:P s,p2:P(sp1)

|

P

((

s

p1

)

p2

)|

thenfrom(3)and(4)itfollowsthatm1

=

m2:

m1

=

max

{|

P

(

s

p

)| : ∀

p

P s

} =

max

{|

P

((

s

p1

) ↓

p2

)| : ∀

p1

P s

, ∀

p2

P

(

s

p

)} =

m2

Therefore the largest subshape size m1 equals the largest sub-subshape size m2 and therefore

([

f

]

m1

+

m1Qm1

)

O(

m1

[

f

]

m1

+

m1m1Qm1

).

Overall,thisgivesboundsforeithersideof[C3]:

[

g

f

]

n

O

(

n

[

g

]

m1

+

nm1Qm1

+

n

[

f

]

m1

+

nm1Qm1

)

[(

g

f

)

]

n

O

(

n

[

g

]

m1

+

nm1Qm1

+

nm1

[

f

]

m1

+

nm

2 1Qm1

)

RegardlessofwhetherQ or

[

f

]

dominatesthen

[

g

f

]

n

∈ O[(

g

f

)

]

n justifyingtherewrite.

2

4.2. Abstractcontainercomonads

For abstract(non-derived) container comonads, thissection shows their complexity issimilar to those ofthe derived container comonads.Buthowdowecharacteriseabstractcontainercomonadsinordertohaveenoughstructuretoderive thecomplexitybounds?

Definition11(Abstractfinitecontainercomonad).A comonad structure

(F,

ε

,

(−)

)

is an abstractfinitecontainercomonad if

thereexistsafinitecontainer S

P suchthatF

= J

S

P

K.

The abovedefinitionisessentiallythatofanabstractfinitecontainer functor(Definition7) butwhere F isacomonad. Thisisallthatisneededtoderivethefollowingcomplexitybound;anisomorphicdirected containerstructureonS

P (and

thusacomonadon

J

S

P

K)

isnotrequired,justthatthereisanisomorphiccontainer S

P .

Theorem4(Abstractcontainercomonadcost).LetF beanabstractfinitecontainerwithcomonadstructure

(F,

ε

,

(−)

)

anda con-tainerS

P suchthat

α

:

F

= J

S

P

K

.Thesizeofabstractcontainervaluesisgivenbysize′A

:

F A

→ N

=

sizeA

α

A.Theextension

(−)

thenhastheupperbound(assumingthecost

[

f

]

nisindeeddefined):

[

f

]

n

O

(

n

[

f

]

m

+

nm Qn

+

Rn

)

withm

n wherem representsthesizeofthelargestsubcontainerpassedto f ,Qnrepresentscostsforconstructingsubcontainers

andvaluationcost,andRncoverspathologicalimplementations.

Proof. TheproofresemblesthatofTheorem2forabstractfunctors withtheparameterisationofthecomplexityboundby

Qn and Rn. Incontrast withthecomplexity bounds ofabstractcontainer functors (Proposition 3,p. 5), thisresultrelies moreonthegeneralaxiomsofthestructureonF.

1. Byshape preservationofcomonads (Proposition4),the sizeofa containercomputedby theextension f† isthe size oftheinputcontainer,i.e.,size′B

f

=

size′Abythefollowingdiagram(recallsize′A

=

sizeA

α

A),wheretheunlabelled commutingsquaresarenaturalityproperties:

FB αB F!B

J

S

P

K

B sizeB JSPK!B F A P r.4 f† αA F!A F1 α1

J

S

P

K

1 size1

N

J

S

P

K

A JSPK!A sizeA

(12)

2. By thecomonad axiom[C2]

ε

N

size′A

=

size′A,the sizeofthesubcontainer computedforthe currentcontextisthe

sizeoftheinputcontainer(Remark6,p.9).Thusthemaximumsizem ofasubcontainercomputedbyextension

(−)

† isatleastn,i.e. m

n.

3. Thecomonadaxiom[C1]

ε

A

=

idF Aimpliesthatforsomeinputcontainerx

:

FA andfunction f

:

FA

B then fx

:

FB

applies f ton subcontainersofx whereeachsubcontainerhaseachelementofx atitscurrentcontext.

Thisfollowsbyacontradiction:assumethat f† doesnotapply f ton subcontainers.Therefore,tocomputeanoutput container ofsize n (from point(1) above)of elementtype B,the operation f† must copysome of theresults from applying f .Therefore,applying

ε

A toaninputcontainerwithdistinctelements A wouldproduceanoutputcontainer withduplicateelements.Therefore,

ε

A

=

idF Aandtheinitialassumptionmustbefalse.

Combined withpoint (2),theexecutiontime isthereforeboundedabove byn

[

f

]

m wherem isthemaximumsize of subcontainers,plusnm Qn to accountforconstructingsubcontainersforeachapplicationof f andaccessingelements (thevaluationcost).

4. Finally,thetermRn accountsforarbitrarywastefulimplementations.

2

Optimisation3.Foranycontainer comonad,axiom[C3]canbeorientedas

(

g

f

)

;

g

f†guaranteeingan asymptotic improvementornotmakingthecomplexityworse(inthecasewhere Qn or Rn dominateallotherterms).

Proof. Essentiallythe sameastheproofforOptimisation2 (p.9), modulotheadditionalterm Rn which appearsinboth complexityterms,butwithanadditionallinearfactorforthe(morecostly)

(

g

f

)

†.Thus,

[

g

f

]

n

∈ O([(

g

f

)

]

n

).

2

4.3. Optimisationsforcounit

Thecomputationalcost ofthecounitoperation

ε

A

:

F A

A hasyettobeconsidered.Its axioms[C1]

ε

id and[C2]

ε

f

f areclearlyamenabletoorientationasoptimisingrewrites.Thisisformalisedbrieflyhere,usingsimilarapproaches totheabove.

Remark7.Notmuchcanbesaidaboutthecomputationalcomplexityof

ε

asafunctionofitsinput.Foradirectedcontainer

(

S

P

,

↓,

o,

⊕),

thederivedcomonadhascounit

ε

(

s

,

v

)

=

vo{s

}.

Theexecutiontimeof

ε

isthen thetimetakentoaccess therootpositionfromthecontainer value,

[

ε

]

n

∈ O(

Qn

),

where Qn

= [

v

]

1 is unknownsince itdependson thevaluation

functionoftheinputcontainervalue.Thesameistruefortheabstractcasewherethereisnofurtherinformation.However, axioms[C1]and[C2]canstillbeshowntobeoptimisations.

Optimisation4(Comonad[C1]).For all derived or abstract container comonads, axiom [C1] can be oriented asa rewrite

ε

;

id providinganoptimisation.

Proof. For derived container comonads

[

ε

]

∈ O(

n

[

ε

]

m

+

nm Qn

)

and for abstract container comonads

[

ε

]

∈ O(

n

[

ε

]

m

+

nm Qn

+

Rn

).

Since

[

idF

]

n

∈ O(

n Qn

)

(see proof ofOptimisation1, p.6) thisrewrite isguaranteed to preserve asymptotic complexityorimprovethecomplexity(ifeitherm

>

1 or

[

ε

]

m hasnon-constantcomplexity,orRndominates).

2

Optimisation5(Comonad[C2]).For all derived or abstract container comonads, axiom [C2] can be oriented asa rewrite

ε

f

;

f providinganoptimisation.

Proof. Let

(

s

,

v

)

beaninputcontainervalue,withn

= |

P s

|.

Forderivedcontainercomonads

[

ε

f

]

n

∈ O(

n

[

f

]

m

+

nm Qn

+ [

ε

]

n

)

wherem isthesizeofthemaximumsubshape (The-orem3,p.9).By[S1]itfollowsthatm

n sincethecurrentshapeisasubshapeofitself(attherootposition)andtherefore themaximumsubshapesizemustbeatleastn.Thus

[

f

]

n

∈ O(

n

[

f

]

m

)

andtherefore

[

f

]

n

∈ O(

n

[

f

]

m

+

nm Qn

+ [

ε

]

n

).

Forabstractcontainercomonads,

[

ε

f

]

n

∈ O(

n

[

f

]

m

+

nm Qn

+

Rn

+ [

ε

]

n

)

(Theorem4,p.10).Bysimilarreasoningto the derived case, the [S1] axiom corresponds to the general [C2]comonad axiom

ε

size†

=

size, (see Remark 6, p. 9) that is,thesize ofthesubshapeatthe currentcontextisthe sizeofthe inputcontainer.Thus m

n andit followsthat

[

f

]

n

∈ O(

n

[

f

]

m

+

nm Qn

+

Rn

+ [

ε

]

n

).

Subsequently,inboththederivedandabstractcases

ε

f

;

f isanoptimisation.

2

4.4. Summary

Theresultsofthissectionshowedcomplexityboundsforderivedandabstractcontainercomonads,providingtheproof thateachofthecomonadaxiomscanbeorientedtogiveanoptimisation.Therewrite

(

g

f

)

;

(

g

f

)

(Optimisation2 and3)corresponds totheflattening transformationdiscussedintheintroduction(Section1), movingfromaquadraticto linearprogram.Werevisitthisexampleconcretelyusingtheformalismofthispaper.

(13)

Example6(Arrays).Theintroductory exampleofthispapershowedtwoimperative array programswiththesame exten-sionalbehaviour,butdifferingasymptoticbehaviours:

for i = 1..n for u = 1..n B(u) = f(A, u) C(i) = g(B, i)

∈ O(

n2

[f]

+

n

[g])

for i = 1..n B(i) = f(A, i) for i = 1..n C(i) = g(B, i)

∈ O(

n

[f]

+

n

[g])

where

A,

B,

and

C

are one-dimensional arraysand

f

and

g

are pure functions. This example can be explained in the framework ofthispaper becausethe above traversals are instances ofthe extension operation of an arraycomonad [17] whichhasacursor –a positionwhichidentifies orfocusses onaparticularelementinthecontainer(see [4,§4.5]).Inthe aboveloops, theiterationvariables

i

and

u

actascursorstothearrays. Forexample,intheleftprogram,

u

isusedasthe cursorfor

A

and

i

isusedasthecursorfor

B.

One-dimensionalarrayswithacursorarecapturedbythecontainer:

Array

= (

l

: N)(

c

:

Fin l

)  (λ(

l

,

c

).

Fin l

)

Shapes area dependent-sumofa lengthl

: N

andacursorposition (writtenasc) drawnfromthefinitesetFin l.Positions forshapeswithlengthl aredrawnfromthefinitesetFin l.Thegeneratedcontainerdatatype2 isthen

JArrayK

A

= (

l

: N,

c

:

Fin l).(Fin l

A

),

thatis,adependent-sumofthelengthl ofthearray,thecursorc intothearray,andthevaluationmap frompositionswithinthelengthtovalues.

TheArray containeralsohasthefollowingdirectedstructure:

o

{(

l

,

c

)} =

c the root position is marked by the cursor

(

l

,

c

) ↓

p

= (

l

,

p

)

the subshape at position p has same length, but with cursor p p

p

=

pthe offset of position p to pis just the cursor p

Inliningtheabovedefinitions,thederivedextensionoperationofthecomonadforthisdirectedcontainerisdefinedas:

f

((

l

,

c

),

v

) = ((

l

,

c

), λ

p

.

f

((

l

,

p

),

v

))

That is,extension

(−)

† produces an arraywith thesamesize andcursorasthe input array,whereelements ofthe new arrayarecomputedbyapplying f toarrays

((

l

,

p

),

v

)

whicharetheoriginalarray“refocussed”ateachpositionp bysetting

p asthecursor.Thisproducesthefollowingbehaviourcapturedbytheimperativecode: for p = 0..(l-1)

B(p) = f(A, p)

where

A

correspondstothevaluationand

p

tothepositionwhichisusedasthecursorwithinthearray(oflength

l).

The outputarray

B

correspondstothenewvaluation.Thus,thetwoarrayprogramsintheintroductory examplecorrespondto twocomonadicarraycontainercomputations:

(g ◦ f

)

† and

g

◦ f

† respectively.3

Applying Theorem 3on the derived containercomonad cost andCorollary 3on thecost ofpost-composingfunctions aftercomonadicextension,werecoverthecomplexityresultsshownintheintroduction(withsomeadditionalprecision).By theabovedefinitionofthearraycomonad,thesizeoflargestsubshapem

=

maxp:P s

|

P

(

s

p

)|

= |

P s

|

=

n sincesubshaping

justchangesthepositionofacursorratherthanchangingthesizeofanarray.Thus,assumingthatthevaluation costis constant Qn

=

1,wederivethecomplexitybounds:

(g ◦ f

)

O

(

n

[g ◦ f

]

n

+

n2Qn

)

O

(

n

([g]

n

+

n

[f]

n

) +

n2Qn

) +

n2Qn

)

O

(

n

g

n

+

n2

[f]

n

+

n3Qn

+

n2Qn

)

O

(

n

[g]

n

+

n2

[f]

n

+

n3

)

g

◦ f

O

(

n

[g]

n

+

n2Qn

+

n

[f]

n

+

n2Qn

)

O

(

n

[g]

n

+

n

[f]

n

+

n2

)

On theright,the extensiontraversals aresequentiallycomposed,whereas ontheleft theextension of

f

is nestedwithin the traversalfor theextension of

g,

leading to the quadraticfactor onthe cost of

f.

Applying Optimisation3 (p. 11) to thisprogramrewrites

(g ◦ f

)

† to

g

◦ f

†,thusreducingtheasymptoticcomplexity.Thenestedarray-traversaloptimisation

2 FollowingDefinition2exactly,thedatatypeisJArrayKA= (s: (l: N)(Fin l))((λ(l,c).Fin l)sA).Thetextprovidesan(isomorphic)simplification. 3 WhilsttheelementsofFin l rangefrom0 tol1,itiseasytotranslatethesetomatchtheindexspaceoftheoriginalexample(withindicesdrawn

References

Related documents

Manakala sebanyak 30.0% responden mengetahui bahawa siri animasi Upin Ipin ini memaparkan elemen identiti Melayu dan sebanyak 70% responden menjawab kurang pasti

Methods: Ours is an ongoing embedded mixed-methods study with four phases: (1) qualitative interviews with GPs to explore their views on the content, format, and use of the e-CDSS,

Bob’s contributions to the club over the past several years are many: build-out of the concession Kiosk, organization of concession activities, design and build-out of the 120V AC

Netscape Navigator Netscape Navigator Generated Code Module (PHP) Client Page (HTML) Edited Code Module (PHP) UML Application Model (XMI) Edited Client Page (HTML)

Jeremiah said, 'The word of God came to me: Hanamel son of your uncle Shallum is going to come to you and say, "Buy my field that is at Anathoth, for the right of redemp:on

Compared to the initial investment of a SMEs, enterprises with rich investment experience are more likely to seek the existing large market, much cheap labor and rich

Earlier this week, Bishop Brown informed the clergy of the diocese that the Episcopal Church in Maine would be worshipping online only at this time (with the exception of

Should an accident occur at an accredited institution involving serious injury or seriously impacting on the welfare of a visitor, staff, or institution animal, a written report