Contents lists available atScienceDirect
Theoretical
Computer
Science
www.elsevier.com/locate/tcs
On
collecting
semantics
for
program
analysis
Gianluca Amato
∗
, Maria
Chiara Meo
∗
,
Francesca Scozzari
∗
UniversitàdiChieti–Pescara,Pescara,Italy
a
r
t
i
c
l
e
i
n
f
o
a
b
s
t
r
a
c
t
Articlehistory:
Received31January2019
Receivedinrevisedform6February2020 Accepted17February2020
Availableonline19February2020
Keywords:
Abstractinterpretation Staticanalysis Collectingsemantics Galoisconnection
Reasoningonacomplexsystemintheabstractinterpretationtheorystartswithaformal description of the system behavior specified by a collecting semantics. We take the commonpointofviewthatacollectingsemanticsisaveryprecisesemanticsfromwhich otherabstractions maybe derived.We elaborateonboththe conceptsofprecision and derivability,andintroduceanotionofadequacywhichtelluswhenacollectingsemantics is agoodchoice foragivenfamily ofabstractions.We instantiatethisapproach tothe caseoffirst-orderfunctionalprogramsbyconsideringthreecommoncollectingsemantics and someabstractproperties offunctions. We study theirrelative precision and give a constructive characterization of the classes of abstractions which are adequate for the collectingsemantics.
©2020TheAuthors.PublishedbyElsevierB.V.Thisisanopenaccessarticleunderthe CCBYlicense(http://creativecommons.org/licenses/by/4.0/).
1. Introduction
Abstractinterpretation,introducedbyCousotandCousot [14,15],isaframeworkforapproximatingthebehaviorof sys-tems.Ithasbeenusedbothfordevelopingstaticanalysis,verificationmethodsandforstudyingrelationsamongsemantics atdifferentlevelsofabstraction. Themain pointofabstractinterpretation istoreplacethe formalsemantics ofasystem withanabstractsemanticscomputedoveradomainofabstractobjects,whichdescribethepropertiesofthesystemweare interestedin.
Givenasysteme,we assumethatitssemantics
eisanelementofasetC ofconcreteproperties,calledtheconcrete domain.Inmostcaseswe onlyneedto knowsome abstractpropertiesofthesemanticsofasystem.Examplesofabstract properties are: theprogramterminatesforalltheinputvalues or theprogramoutputisavalueintheinterval[
0,
42]
. Given a set A ofabstractproperties(abstractdomain)weare interestedin,eachconcretepropertyinC mayenjoyseveralabstract propertiesin A.Therelationshipbetweenconcreteandabstractpropertiesmaybeformalizedinmanydifferentways,but one ofthemostcommonapproachesconsistsinfixing an approximationordering≤
A ontheset ofabstractproperties A, such that a1≤
Aa2 whena1 is astronger propertythan a2,anddefining an abstractionfunctionα
A:
C→
A whichmaps every concrete property to the strongest (smallest) abstract property it enjoys. Thisis what is presentedin Cousot and Cousot [16] underthe“existenceofabestabstractapproximationassumption”.Anabstractinterpretationproblemconsistsinansweringwhetherthesemantics
eofasysteme enjoysagivenproperty a.Intheory,thiscanbe verifiedbycomputingthestrongestabstractpropertyenjoyedbythesysteme,whichisα
A(
e)
, andcheckingthatitenjoysthepropertya weareinterestedin,thatisα
A(
e)
≤
Aa.Inpractice,sinceeisnotgenerally computable,thestandardapproachtosolvethisproblemistodesignanapproximateabstractsemanticseA∈
A suchthat*
Correspondingauthors.E-mailaddresses:gianluca.amato@unich.it(G. Amato),mariachiara.meo@unich.it(M.C. Meo),francesca.scozzari@unich.it(F. Scozzari).
https://doi.org/10.1016/j.tcs.2020.02.021
0304-3975/©2020TheAuthors.PublishedbyElsevierB.V.ThisisanopenaccessarticleundertheCCBYlicense (http://creativecommons.org/licenses/by/4.0/).
α
A(
e)
≤
Ae
A and show thate
A≤
Aa. In mostcases,e
A is not designed starting frome,
but from a so-called collectingsemantics.According to Cousot and Cousot [16], the term collecting semantics may be used to mean either “an instrumented version ofthestandardsemanticsinordertogatherinformationaboutprograms executions”or“aversionofthestandard semantics reducedtoessentialsinordertoignore irrelevantdetails aboutprogramexecution”.Inthispaper,we consider onlythesecondmeaningoftheterm,i.e.,weviewacollectingsemanticsasanabstractionofthestandardsemantics.
Onemightaskwhicharethepropertiesthatanabstractinterpretationenjoys whichentitletheabstractiontobecalled a collectingsemantics.It iscommonto findintheliterature thestatement thatthecollecting semanticsisaveryprecise abstraction fromwhichalltheother analysesmaybe derived.Herewewanttoelaborateandformalizethisstatement. In particular,we discusswhatistherightconceptofprecisionofabstractionstobeusedinthiscontext,andwhatisbehind thestatementthatananalysismaybederivedfromanother.
Inthecontext offirst-orderfunctionalprograms, weconsidercollectingsemantics commonlyusedinstaticanalysisof programs which firstappearedin CousotandCousot [17]. Weintroduce threecollecting semanticsCS1,CS2 andCS3 and
discuss the relative precision among them, showing that CS2 is a suitable collecting semantics for all properties, while
CS1 andCS3 areonlysuitable forsomepropertiesoffunctions. Wedefinea categoryofabstractinterpretations andtheir
morphismsbasedonthenotionofGaloisconnection,discusstheroleofinitialobjectsandconstructivelycharacterizeaclass ofabstractionsforwhichthecollectingsemanticsareinitial,i.e.,arewellsuitedascollectingsemantics.Moreover,weshow how commonabstractionsof functionalproperties,such asstrictness, totality,convergence,divergenceandmonotonicity, relatetothecollectingsemantics.
1.1. Planofthepaper
In Section 2 we explore theconcept ofprecision ofabstraction, show some examples ofabstraction for propertiesof functionsanddiscusshowtheycanbecomparedstartingfromtheabovenotionofprecision.Section3introducesthetwo collecting semantics CS1 andCS2.Wecomparethe precisionofthecollecting semanticsandformally statetheir
relation-ships. In Section 4we define the categoricalframework ofabstractinterpretations anddiscussthe role ofinitial objects. Section 5 extendsthischaracterizationto anadditional collectingsemantics CS3 andshowshowit relatestothe
seman-tics inSection 3.InSection 6wediscusswhathappenswhenusingdifferentformalizationsofabstractinterpretationand suggestsomefuturework.Alltheproofsareintheappendix.
The article isan extended andrevised version of[3]. The mostprominentchange isa completeoverhaul ofthe mo-tivations of this work and the reasons which led to our choice for the precision ordering. However, there are many otherchanges:wehaveincludedmoreexamplesofabstractions(totality,convergence,divergence,involution,idempotence, monotonicity, boundness),studieda newcollectingsemantics (CS3) andsignificantlyexpandedthe intuitiveexplanations.
Moreover,boththeappendixwiththeproofsandthesectiononrelatedworkarenew.
2. Precision,derivabilityandcollectingsemantics
Inthefollowing,wefixasetC ofconcreteproperties.Thestandardsemantics
eofasysteme isanelementofC .The concrete domainC may beendowedwithadditionalstructure whichallowsto derivethe semanticsofasystemthrough structuralinductionon thedescriptionofthesystemandleastfixpointcomputations.However, thewayeisdefinedis goingtoplayonlyamarginalroleinourdiscussion.Definition1(AbstractInterpretation).An abstractinterpretation for a set C is a pair
(
A,
α
A)
where the set A is partially ordered by≤
A andα
Aisamap C→
A.Ifc∈
C ,a∈
A andα
A(
c)
≤
Aa wesaythata isa correctapproximation ofc.With anabuseofnotation,wewillsometimesidentifyanabstractinterpretation(
A,
α
A)
withitsabstractdomain A.Often C is endowed with a partial ordering (called computationalordering) which is used in fixpoint computations. Computationalorderingandapproximationorderingmaybeunrelated,hencewedonotrequirethemap
α
A inanabstract interpretationtobemonotone.2.1. AbstractionsandGaloisconnections
ItisworthnotingthattheabstractionmapinourdefinitionofabstractionisnotrequiredtobepartofaGalois connec-tion.Manyabstractionsaremorenaturallyformalizedintermofabstractionfunctionsonly.Asaveryelementaryexample, consider the classical “rule of sign” forchecking the correctness ofthe sign ofthe product xy of two integer numbers, whichexploitsthefactthatthesignofthemultiplicationxy canberecoveredfromthesignofx andthesignof y.Ifwe formalize thisrule asanabstract interpretation,we maychoose
Z
astheconcrete domain,Sign= {
pos,
neg,
zero}
asthe abstractdomain,andα
S: Z
→
Signas:α
S(
x)
=
⎧
⎪
⎨
⎪
⎩
pos if x>
0;
neg if x<
0;
zero if x=
0.
NotethatforSignweusetheflatordering:a isacorrectabstractionofx iffa isexactly
α
S(
x)
.Inthiscase
α
S isnot partofa Galoisconnection.Actually,α
S is noteven monotoneifwe usethe standardordering forZ.
Wemayturntherule ofsignintoa Galoisconnectionif, forexample,we take C=
P(Z)
astheconcrete domain, A=
P(
Sign)
astheabstractdomainandα
:
C→
A definedasthe pointwiseextension ofα
S,i.e.,α
(
X)
= {
α
S(
x)
|
x∈
X}
, whereboth A andC areorderedbysubsetinclusion.Inthiscaseγ
(
X)
= {
x∈ Z
|
α
S(
x)
∈
X}
istherightadjointofα
.AlltheabstractionsweshowinthispapercanbeturnedintoGaloisconnectionsonlyifwechangetheconcretedomain. Thisactuallymeansselectingacollecting semanticsamongseveralpossiblechoices, anditisthemaintopicofthepaper. Only rarely the standard semantics of a programming language directly forms a Galois connection with their common abstractions.AnotableexampleistheTP likesemanticsforgoal-independentsemanticoflogicprogramming[11]. 2.2. Examplesofabstractions
Wetake asapplicative settingtheabstractinterpretation offunctionalprograms.Foreasinessofpresentation, we con-sider a simplecase where the concrete domain C is the set
D
⊥→
D
⊥ of unary functions from a base domainD
⊥ to itself,andassumethatD
⊥hasadistinguishedvalue⊥
whichrepresentsnon-terminatingcomputations.Dependingonthe concretedomain,⊥
mayalsoencoderun-timeerrors.Alltheresultspresentedareindependentfromitsconcretemeaning. Some ofthe resultswhich followdepend onthe fact thatD
⊥ hasat leastan element differentfrom⊥
.The casewhenD
⊥= {⊥}
isasingletonistrivialandwillnotbeconsidered.Example2(Asimpleprogram).Considerasimplesettingwherethe
D = N
isthesetofnaturalnumbers.Then,theprogram l e t rec prog n = i f ( n = 2) then prog ( n ) e l s e nhasthefollowingsemantics:
prog= λ
n.
⊥
if n=
2 or n= ⊥;
n otherwise.
Anexampleofasimpleandusefulabstraction isstrictness.Wesaythat afunction f
:
D
⊥→
D
⊥ isstrict if f(
⊥)
= ⊥
. Strictnessanalysisisusedtoprovethatafunction f eitherdivergesorneedsitsarguments.Thenextdefinitionformalizes strictnessanalysisasanabstractinterpretation.Definition3(Strictness).Thestrictness abstractinterpretation[24] isStr
= ({
str≤ },
α
str)
whereα
str(
f)
=
str if f
(
⊥) = ⊥
, otherwise.Anothercommonabstractionfor
D
⊥→
D
⊥isconstancy.Constancycapturesthefactthatafunction f:
D
⊥→
D
⊥either divergesorignoresitsarguments,i.e., f(
x)
=
f(
⊥)
foranyx∈
D
⊥.Definition4(Constancy).Theconstancy abstractinterpretation[27] isConst
= ({
const≤ },
α
const)
whereα
const(
f)
=
const if
∀
x∈
D
⊥,
f(
x)
=
f(
⊥)
, otherwise.Considertheprogram prog definedinExample2.Since prog usesitsargumentanddoesnotalways diverge,we have
α
str(
prog)
=
str andα
const(
prog)
=
.Other examples of simpleand usefulabstractions are totality,convergence, divergence [17], involution and idempotence [21].Wesaythatafunction f
:
D
⊥→
D
⊥isconvergentif,foranyx∈
D
⊥, f(
x)
= ⊥
,anditisdivergentif,foranyx∈
D
⊥, f(
x)
= ⊥
. We say that a function f is idempotent if f◦
f=
f , where◦
isthe standard function composition operator. Moreover f isaninvolutionif f◦
f=
id.Definition5(Totality).Thetotality abstractinterpretationisTot
= ({
tot≤ },
α
tot)
whereα
tot(
f)
=
tot if
∀
x∈
D, f(
x)
= ⊥
otherwise.Definition6(Convergence).Theconvergence abstractinterpretationisConv
= ({
conv≤ },
α
conv)
whereα
conv(
f)
=
conv if
∀
x∈
D
⊥, f(
x)
= ⊥
otherwise.Definition7(Divergence).Thedivergence abstractinterpretationisDiv
= ({
div≤ },
α
div)
whereα
div(
f)
=
div if
∀
x∈
D
⊥, f(
x)
= ⊥
otherwise.Definition8(Involution).Theinvolution abstractinterpretationisInv
= ({
inv≤ },
α
inv)
whereα
inv(
f)
=
inv if
∀
x∈
D
⊥, f(
f(
x))
=
x otherwise.Definition9(Idempotence).Theidempotence abstractinterpretationisIde
= ({
ide≤ },
α
ide)
whereα
ide(
f)
=
ide if
∀
x∈
D
⊥, f(
f(
x))
=
f(
x)
otherwise.Ifweconsidertheprogramprog definedinExample2,wehavethat
α
ide(
prog)
=
ide sinceprog describesan idempo-tentfunction.However,α
tot(
prog)
=
α
conv(
prog)
=
α
div(
prog)
=
α
inv(
prog)
=
sinceprog(2
)
= ⊥
,prog(1
)
=
1 andprog(
prog(
2))
= ⊥
=
2.Finally,considerthemonotonicityandboundnessproperties.Assume
D
⊥ ispartiallyorderedby.Wesaythata func-tion f:
D
⊥→
D
⊥ ismonotone if∀
x,
y∈
D
⊥,xy implies f(
x)
f(
y)
.Moreover f:
D
⊥→
D
⊥isbounded ifthereexists d∈
D
⊥suchthat∀
x∈
D
⊥, f(
x)
d.Definition10(Monotonicity).Themonotonicity abstractinterpretationisMon
= ({
mon≤ },
α
mon)
whereα
mon(
f)
=
mon if
∀
x,
y∈
D
⊥, xy implies f(
x)
f(
y)
otherwise.Definition11(Boundness).Theboundness abstractinterpretationisBou
= ({
bou≤ },
α
bou)
whereα
bou(
f)
=
bou if
∃
d∈
D
⊥,
∀
x∈
D
⊥, f(
x)
d otherwise.Letusconsidertheprogram prog definedinExample2,where
isthestandardorderrelation“lessthanorequalto” extended insuch awaythat⊥
0.Wehavethatα
mon(
prog)
=
sinceprog(
1)
=
1⊥
=
prog(
2)
.Moreover,itis easytocheckthatα
bou(
prog)
=
.2.3. Comparingabstractinterpretations
In order tocompare differentabstract interpretations,we need to define a notionofrelative precision1 amongthem. Of course,one might saythat there isalreadya standard notionof relativeprecision, which isthe existence ofa Galois connectionamongtheabstractdomains.WeagreethatGaloisconnectionsarequiteconvenientforcomparingabstractions, butwedonot thinkthatthischoiceshouldgo unquestioned,andwe wanttoprovidemoreelaborateargumentsinfavor ofthisapproach.Thisisbecauseinoursettingabstractinterpretationsaredefinedthroughtheuseofabstractionfunctions only,andthereshouldbeagoodreasonforintroducingGaloisconnections.
Asafirst,naïfapproach,wemaysaythat,giventwoabstractinterpretations
(
A,
α
A)
and(
B,
α
B)
foraconcretedomain C ,wehavethat(
A,
α
A)
ismoreprecisethan(
B,
α
B)
whenallthepropertiescomputedbyα
B mayberecoveredfromthe propertiescomputedbyα
A,thatiswhenα
B factorsthroughα
A,i.e.,thereisamapα
:
A→
B suchthatα
B=
α
◦
α
A.However, thisdefinition of precision is too weak for many purposes. Mostof the time, we are not able to compute the abstraction ofthe concrete semantics
α
A(
e)
butonly an over-approximation of the realvalue through an abstract semantics eA≥
A
α
A(
e)
.Inthiscase,knowingeA doesnotsayanythingaboutα
B(
e)
,sincewe havenotrequiredα
tobemonotone.1 Weusethetermrelativeprecision toindicatethatwedealwithprecisionamongabstractdomains,sinceinsomeworkprecision referstocompleteness propertiesofanabstractdomain,seeforinstance[15,23,6].
Example12.Wesaythatafunction f
:
D
⊥→
D
⊥isdefinedconstant ifthereisd∈
D
suchthat∀
x∈
D
⊥, f(
x)
=
d.Consider theabstractinterpretation(
A,
α
A)
suchthat A= {
defcon<
Adefconstr<
A}
andα
A(
f)
=
⎧
⎪
⎨
⎪
⎩
defcon if f is defined constant
defconstr if f is strict
otherwiseNotethat,although
α
A(
f)
=
defconstr exactlywhen f isstrict,thevaluedefconstr doesnotrepresentthepropertyofbeing strictbutthepropertyofbeingstrictordefinedconstant.Wemaydefine
α
:
A→
Strsuchthatα
= {
defcon→ ,
defconstr→
str,
→ }
,andwe haveα
◦
α
A=
α
str.However,α
isnot monotone:ifwehavea programe andwe knowthat eA=
defconstr≥
α
A(
e)
,wecannot concludethat str≥
α
str(
e),
i.e.,thate
isstrict.If we also require monotonicity of
α
in the definition of precision, things go better. In this case, if we know that eA≥
A
α
A(
e)
, then we also knowα
(
eA)
≥
Bα
B(
e)
, hence an approximate result forthe abstract interpretation A givesanapproximateresultfortheabstractinterpretation B.Definition13(Relative
α
-precision).Givenabstractinterpretations(
A,
α
A)
and(
B,
α
B)
overthedomainC ,wesaythat A is moreα
-precisethan B whenthereisamonotonemapα
:
A→
B suchthatα
◦
α
A=
α
B.It turnsout that the trivialabstraction
(
C,
id)
withtheflat ordering on C isthe mostα
-preciseabstraction. Actually, if(
A,
α
A)
isan abstraction, in orderto show that C is moreα
-precisethan A itis enough tochooseα
=
α
A, whichis monotonesincetheorderingofC isflat.2.4. Relativeprecisionandverification
Relative
α
-precision isstill notsatisfactory inallthe contexts.Ifabstractinterpretation wereused foranalysisonly,it mightbeagoodchoice.However,abstractinterpretationisalsousedforverification.Inthiscase,wedonotreallywantto computeα
A(
e)
.Onthecontrary,wefixapropertya∈
A andwanttodecidewhetherα
A(
e)
≤
Aa.Definition14(Abstractinterpretationproblem).Let C bea concretedomain ande be asystemwhose concretesemantics is
e∈
C . Given an abstract interpretation(
A,
α
)
for C and an abstract property a∈
A, an abstractinterpretationproblem consistsindecidingwhetherα
(
e)
≤
Aa.
According tothisnew perspective,ifwe have abstractinterpretations A and B, wemight saythat A ismoreprecise
than B when each abstract interpretation problem over the domain B may be transformed into an equivalent abstract
interpretationproblemoverthedomain A.Inthisway,ifwehypotheticallyhavean oracleforverifyingpropertiesover A, wecanuseittoverifypropertiesover B.Wemayformalizethispointofviewasfollows.
Definition15(Relative
γ
-precision).Giventwoabstractinterpretations(
A,
α
A)
and(
B,
α
B)
overthedomainC ,wesaythat A ismoreγ
-precisethan B whenthereisγ
:
B→
A suchthat,foreachc∈
C andb∈
B,α
B(
c)
≤
Bb⇐⇒
α
A(
c)
≤
Aγ
(
b) .
Weshowwithacoupleofexamplesthatthetwovariantsofrelativeprecisionaredifferentfromoneanother.
Example16.Considertheabstractinterpretation A
=
D
⊥∪ {}
withd<
Aforeachd∈
D
⊥.Theideaisthatstandsfor thetrivialpropertyenjoyedbyeveryfunction,whiled∈
D
⊥isthepropertyofbeingconstantlyequaltod.Inotherwords:α
A(
f)
=
d if
∀
x∈
D
⊥,
f(
x)
=
d, otherwiseThereisamonotonemap
α
:
A→
Constgivenbyα
(
a)
=
const if a
= ,
and
α
const=
α
◦
α
A.Therefore,A ismoreα
-precisethanconst.However,thereisnodirectwaytodecidewhetheramap f is constantbyhavingaverifierfortheabstractinterpretationproblemsin A.Actuallytocheckwhetherα
const(
f)
≤
Aconst,we wouldneedtosolveapossiblyinfinitenumberofproblemsα
A(
f)
≤
d foreachd∈
D
⊥.Therefore, A isnotmoreγ
-precise thanConst.Example17.Considerthedomain
(
A,
α
A)
givenby str defcon otherα
A(
f)
=
⎧
⎪
⎨
⎪
⎩
str if f is strictdefcon if f is defined constant other otherwise
and
(
B,
α
B)
whichisdefinedinexactlythesamewaybutwithouttheelement.Notethatα
A neverreturns.Then, A is moreγ
-precisethanB:wejustdefineγ
:
B→
A astheinjectionofA inB.However,thereisnomonotonemapα
:
A→
B suchthatα
B=
α
◦
α
Asincethereisnowaytomapconsistently.Although in the general case the two concepts of precision are different, there are several ways inwhich they may collapse.Thishappens,forexample,when
α
Aissurjective.Proposition18.If
α
Aissurjective,andA ismoreγ
-precisethanB,thenA ismoreα
-precisethanB andthereisaGaloisconnectionα
,
γ
:
AB suchthatα
◦
α
A=
α
B.Note thatwhile thetrivialabstraction
(
C,
id)
isthemostα
-precise abstractinterpretation,thesame isnottrue when consideringγ
-precision.Example19.The trivial abstraction is not more
γ
-precise than Str. This would amount to the existence of a functionγ
:
Str→
C suchthatα
str(
f)
=
str iff f=
γ
(
str)
.Inother word,thiswouldbepossibleonlyiftherewerea uniquestrict function,whichisobviouslynottrue.Inthegeneralcase,thefactthat
(
A,
α
A)
ismoreα
-preciseandγ
-precisethan(
B,
α
B)
doesnotimplythatα
,
γ
isa Galoisconnection,asshownbythefollowingexample.Example20.LetA
= ({
str,
other,
},
≤
A)
withstr<
Aother<
A.Givenafunction f:
D
⊥→
D
⊥,wedefine:α
A(
f)
=
str if f is strict,
otherwise.Let B
= ({
str,
},
≤
B)
with str<
B andα
B(
f)
=
α
A(
f)
for each f:
D
⊥→
D
⊥. Letα
:
A→
B be the map{
str→
str,
other→
str,
→ }
whileγ
:
B→
A istheidentitymap.We havethat
(
A,
α
A)
is moreα
-precisethan(
B,
α
B)
,sinceα
ismonotone andα
is theidentityon theimage ofα
A. Moreover,(
A,
α
A)
ismoreγ
-precisethan(
B,
α
B)
.Actually,≤
B and≤
AdocoincideonB,hence,forallb∈
B wehavethatα
B(
f)
≤
Bb⇐⇒
α
B(
f)
≤
Ab⇐⇒
α
A(
f)
≤
Aγ
(
b) .
However
α
andγ
donotformaGaloisconnection,sinceα
(
other)
≤
str butotherγ
(
str)
. 2.5. DerivabilityamongsemanticsIntheintuitivedefinitionofcollectingsemantics,oneoftherequirementsisthattheother abstractionsmaybe derived from it. What does it mean that an abstraction
(
B,
α
B)
may be derived from(
A,
α
A)
? It seems unavoidable that each sensibledefinitionof“maybederivedfromA”meansthat A ismoreα
-precisethan B.Inthehypotheticalcasethatweare abletocomputeeverythingwithoutlosinginformation,wewantthatgoingthroughthecollectingsemanticsinsteadofthe standardsemanticsdoesnotlose information.However,derivablealsomeansthatifwehavean(approximate)constructivedefinitionofanabstractsemantics
eA≥
Aα
A(
e)
,thismaybeusedasaguideforaconstructivedefinitionofanabstractsemanticseB≥
Bα
B(
e)
.Acommoncaseiswhen
e=
F ωe(
⊥
C)
forsomemap Fe:
C→
C andtheabstractsemanticsin A isbuiltbysimulating in the abstract domain the progression of the concrete iterates. It means that there is a map FA,e:
A→
A such that FiA,e(
α
A(
⊥
C))
≥
Aα
A(
Fei(
⊥
C))
andthereis an abstractjoin A: ℘ (
A)
→
A which is a correctapproximationof theleast upperbound ofC , i.e., ifai≥
Aα
A(
ci)
foreach i<
ω
,then Aai≥
Aα
A(
ici)
.The abstractsemanticson A is definedas eA=
In this situation, if A is both more
α
-precise andγ
-precise than B, we may define an abstract semanticse
B as B{
FiB,e(
α
B(
⊥
C))
|
i<
ω
}
where FB,e=
α
◦
FA,e◦
γ
andB(
X)
=
α
(
A{
γ
(
x)
|
x∈
X})
.Itispossibletoprovethat eB≥
Bα
B(
e)
.The cornerstone of the correctness proof is the following proposition. We show the proof here since we think it is importanttofollowtheremainingpartofthissection.
Proposition21.Let
(
A,
α
A)
and(
B,
α
B)
beabstractionssuchthatA isbothmoreα
-preciseandγ
-precisethanB.IfFA,e:
A→
A preservescorrectnessofabstractions,i.e.,foranya∈
A,
c∈
C,
a≥
Aα
A(
c)
impliesFA,e(
a)
≥
Aα
A(
Fe(
c))
,thenFB,e=
α
◦
FA,e◦
γ
alsopreservescorrectnessofabstractions.Proof. Givenb
∈
B andc∈
C ,assumeb≥
Bα
B(
c)
.Since A ismoreγ
-precisethan B,wehaveγ
(
b)
≥
Aα
A(
c) .
BycorrectnessofFA,e,itholdsthat
FA,e
(
γ
(
b))
≥
Aα
A(
Fe(
c))
andbycomposingwiththemonotonemap
α
,wehaveFB,e
(
b)
≥
Bα
B(
Fe(
c)) .
Althoughthisresultissimilartoknownresultsoncorrectnessofabstractoperators,itisessentialinthisproofthatwe trackthebehavioroftheconcreteiterates.Therefore,wehavethreesemanticsinvolved:theconcreteoneandtwoabstract ones.Thisisbecausethetwodefinitionsofrelativeprecisionareessentiallyblindonhow A andB arerelatedonelements whichare not anabstraction ofconcrete values.Ifwewant towork morefreely withouthavingto continuouslyrefer to theconcretesemantics,wemayrequirethat
α
andγ
formaGaloisconnection.Thisisactuallynotveryfarfromwhatwe alreadyhave,asshowninProposition18.Definition22(Relativeprecision).Given
(
A,
α
A)
and(
B,
α
B)
two abstractinterpretations overthedomainC ,wesaythat A ismoreprecisethan B whenthereisaGaloisconnectionα
,
γ
:
AB suchthatα
B=
α
◦
α
A.Thisisessentiallythesamedefinitionofprecisiongivenin[14],withtheadditionalrequirementthat
α
shouldrespect theabstractionmapsα
Aandα
B whichhavebeengivenbeforehand.Notethatweusetheterm“relative”precisionsincein somepapersthetermprecisionisusedtomeanα
-completeness,whichisadifferentconcept[23].Clearly,relativeprecisionisstrongerthanboth
α
-precisionandγ
-precision.WhenwehaveaGaloisconnection, correct-nessoftheabstractsemantics A w.r.t. theabstractsemantics B mayberephrasedwithoutinvolvingconcretesemantics,as inthenextwellknownresult.Proposition23.GivenFA
:
A→
A monotone,ifA ismoreprecisethanB throughtheGaloisconnectionα
,
γ
,thenFB=
α
◦
FA◦
γ
iscorrect,i.e.,ifb≥
Bα
(
a)
,thenFB(
b)
≥
Bα
(
FA(
a))
.Inthefollowing,weusethedefinitionofrelativeprecisionbasedonGaloisconnections.Pleasenotethatifweonlyhave an abstraction
(
A,
α
A)
anda Galoisconnectionα
,
γ
:
AB,then we maydefine an abstractionover B as(
B,
α
◦
α
A)
suchthat A ismoreprecisethanB.However,wearetakingadifferentpointofviewwhereabstractionsaredefineddirectly fromtheconcretesemantics,sothatconditionα
B=
α
◦
α
Ashouldbeincludedexplicitly.2.6. Collectingsemantics
Theconclusionfromtheprevioussectionsisthatifwewanttocomparedifferentabstractinterpretations,thenotionof relativeprecisionbasedonGaloisconnectionconjugatesinasimplewaydifferentaspects relativetoprecision (
α
- andγ
-precision)withaspectsrelativetoderivabilitybetweenabstractsemantics.Thelatterisparticularlyimportantwhenoneof thetwosemanticsweareconsideringisacollectingsemantics,sinceitsmainpurposeisguidingthedesignofanabstract semantics.Whenwe wantto designanabstractsemantics,wecaneithertake theconcretesemanticsasthereference, andusea relativelypoormathematicalframework,orusethecollectingsemanticsasthereference,andderivetheabstractsemantics usingtheGaloisconnectionframework.
Givenafamilyofabstractions
F
,aquestionwhicharisesiswhichisthe“best”collectingsemanticsfortheabstractions inF
.Obviously,werequirethecollectingsemanticstobemoreprecisethanalltheabstractionsinF
.However,wemight requiresomethingmore.Inthegeneralcase,ifA ismoreprecisethan B,thereareseveralGaloisconnectionsα
,
γ
:
AB such thatα
◦
α
A=
α
B. If(
S,
α
S)
is a collecting semantics for a familyF
, it would be better to havea unique Galois connectionfromS toeachabstractioninF
.Definition24(Adequatecollectingsemantics).Givenadomain C anda family
F
ofabstractions,anabstraction(
S,
α
S)
is a collecting semantics adequate forF
when,foreach abstraction(
A,
α
A)
∈
F
,there isa uniqueGalois connectionα
,
γ
:
SA suchthatα
◦
α
S=
α
A.In the rest of the paper, we analyze some common collecting semantics and abstractions for the case of first-order functionallanguages,westudytheirrelativeprecision,andwecharacterizetheclassofabstractionsforwhichthecollecting semanticsareadequate.
3. Collectingsemanticsforfunctionalprograms 3.1. Collectingsemantics
We definetwoabstractinterpretationsCS1 andCS2 whichare commonlyusedascollectingsemanticsfortheconcrete
domain
D
⊥→
D
⊥.Definition25(CollectingSemanticsCS1).WedefinetheabstractinterpretationCS1 on
D
⊥→
D
⊥ as: CS1= (
P
(
D
⊥)
→
−
∪P
(
D
⊥),
α
CS1)
where
P(D
⊥)
−
→
∪P(D
⊥)
isthesetofcompletejoin-morphisms2 orderedpointwise,α
CS1(
f)
= λ
X∈
P
(
D
⊥).
f(
X)
and f
(
X)
istheimageof f through X .Notethat,differentlyfromthedefinitioninCousotandCousot [17], CS1 isrestrictedtomaps
P(D
⊥)
−
→
∪P(D
⊥)
whichare completejoin-morphisms, otherwisetherewouldbemultipleabstractobjectswhichapproximateexactlythesameset ofconcrete functions.Thiswillbe importantwhenproving theadequacyoftheCS1 semantics.Adifferentsolutioncould
betochangeCS1 to
D
⊥→
P(D
⊥)
.Example26.The restriction to join-morphisms is required since the approximation of any function f
:
D
⊥→
D
⊥ in a functionφ
:
P(D
⊥)
−
→
∪P(D
⊥)
isactuallycompletelycharacterizedfromthebehaviorofφ
onsingletons.Forinstance,given d∈
D
,considerthefollowingfunctionsφ
0andφ
1:φ
0= λ
X.
∅
if X= ∅
,{⊥}
otherwise,φ
1= λ
X.
⎧
⎪
⎨
⎪
⎩
∅
if X= ∅
,{⊥}
if|
X| =
1,D
⊥ otherwise.Both
φ
0 andφ
1 approximatesonlyasinglefunction,namelythefunctionwhichdivergesforanyinput,butφ
0,whichisacompletejoin-morphisms,seemstobeacleanerchoice.
Definition27(CollectingSemanticsCS2).WedefinetheabstractinterpretationCS2 on
D
⊥→
D
⊥ as: CS2= (
P
(
D
⊥→
D
⊥),
α
CS2)
where
P(D
⊥→
D
⊥)
isorderedbystandardsubsetinclusionandα
CS2(
f)
= {
f} .
3.2. Strictnessandcollectingsemantics
Considertherelationbetween
α
CS1 andα
str.Firstofall,notethatitispossibletorecoverα
str(
e)
fromα
CS1(
e)
,sinceitholdsthat
α
str=
α
1str◦
α
CS1 whereα
1str:
CS1→
Strisdefinedas:α
1str(φ)
=
str if
φ (
{⊥}) ⊆ {⊥}
otherwise2 Afunctionφisacompletejoinmorphismwhenφ(∪
foreach
φ
:
P(D
⊥)
→
−
∪P(D
⊥)
.ThereforeCS1 ismoreα
-precisethanstrictness.Moreover,considertheproblemα
str(
e)
≤
str.Itisimmediatetoshowthatthisisequivalenttoα
CS1(
e)({⊥})
⊆ {⊥}
.Inturn,thisisequivalenttoα
CS1(
e)
≤ φ
str by definingφ
str= λ
X.
⎧
⎪
⎨
⎪
⎩
∅
if X= ∅
,{⊥}
if X= {⊥}
, D⊥ otherwise.This happens because the functions correctly approximated by
φ
str are exactly all the strict functions. The problemα
str(
e)
≤
isalways true, anditis equivalent toα
CS1(
e)
≤
CS1 whereCS1(
X)
=
D
⊥ foranynon empty X . There-fore,eachstrictnessproblemmaybereducedtoaproblemonthecollectingsemanticsCS1.Ifwedefineγ
1str:
Str→
CS1 as:γ
1str(
str)
=λ
X∈
P
(
D
⊥).
⎧
⎪
⎨
⎪
⎩
∅
if X= ∅
{⊥}
if X= {⊥}
D
⊥ otherwiseγ
1str(
) =λ
X∈
P
(
D
⊥).
∅
if X= ∅
,D
⊥ otherwisewehavethatCS1ismore
γ
-precisethanStrandα
1str,
γ
1strisaGaloisconnection.NotethatthesameholdsforthecollectingsemanticsCS2.Actually,CS2 ismore
α
-precisethanStrbytakingα
(
F)
=
str if
∀
f∈
F,
f(
⊥) = ⊥
otherwise.
Moreover
α
str(
e)
≤
str isequivalenttoα
CS2(
e)
⊆
Fstr whereFstr= {
f|
α
str(
f)
=
str}
.ThismakeCS2moreγ
-precisethanStrbydefining
γ
:
CS2→
Strsuchthatγ
(
str)
=
Fstr andγ
(
)
=
D
⊥→
D
⊥.Analogouslyto thestrictnessproperty,it iseasy toshow thatalsothe convergence,divergenceandtotalityproperties maybereducedtoaproblemonthecollectingsemanticsCS1bydefining
φ
conv= λ
X.
∅
if X= ∅
,D
otherwise,φ
div= λ
X.
∅
if X= ∅
,{⊥}
otherwise,φ
tot= λ
X.
⎧
⎪
⎨
⎪
⎩
∅
if X= ∅
,D
⊥ if⊥ ∈
X,D
otherwise. TheanalogousresultforCS2isimmediate.3.3. Constancyandcollectingsemantics
Not all the abstract interpretation problems may be reduced to problems in a given collecting semantics. Consider the problem
α
const(
e)
≤
const. It may be easily reduced to a problem in CS2, byα
CS2(
e)
⊆
Fconst where Fconst= {
f|
α
const(
f)
=
const}
,howeveritcannotbereducedtoaprobleminCS1.Wemustbecarefultounderstandwhatthismeans.Itisstillpossibletorecover
α
const(
f)
fromα
CS1(
f)
:actually,α
const=
α
1const◦
α
CS1 whereα
1const:
CS1→
Constisdefinedasα
1const(φ)
=
const if
∀
x∈
D
⊥. φ (
{
x}) ∩ φ({⊥}) = ∅,
otherwise.
Therefore,CS1 ismore
α
-precisethanConst.However,thereisnoelementinCS1 whichcorresponds tothesetofallthe constant functions. Actually, if f is an unknown constant function, we havethat f(
x)
mightbe potentially equal to any valuex∈
D
⊥.Therefore,theonlyabstractobjectφ
∈
P(D
⊥)
−
→
∪P(D
⊥)
whichisacorrectapproximationforalltheconstant functionsisthegreatestelementλ
X.
∅
if X= ∅
,D
⊥ otherwise.Unfortunately,thisisacorrectapproximationforallthefunctions,notonlytheconstantones.Moreingeneral,CS1 cannot
expressanyconstraintrelatingtheresultsofafunctionfordifferentinputs(itisaso-callednon-relationaldomain). Hence,thereisno
φ
constsuchthatα
const(
f)
≤
const isequivalenttoα
CS1(
f)
≤ φ
const,i.e.,CS1isnotmoreγ
-precisethan3.4. Involution,idempotence,monotonicityandcollectingsemantics
Analogouslytotheconstancyproperty,ifweconsidertheinvolution propertyandtheproblem
α
inv(
e)
≤
inv,wehave thatitmaybereducedtoaprobleminCS2,byα
CS2(
e)
⊆
FinvwhereFinv= {
f|
α
inv(
f)
=
inv}
.However,asfortheprevious case,itcannotbereducedtoaprobleminCS1,evenifα
inv=
α
1inv◦
α
CS1 whereα
1inv:
CS1→
Invisdefinedasα
1inv(φ)
=
inv if
∀
x∈
D
⊥. φ (φ (
{
x})) ⊇ {
x},
otherwise.
In fact, since there is no element in CS1 which corresponds to theset of all the involutions,there is no
φ
inv such thatα
inv(
f)
≤
inv isequivalenttoα
CS1(
f)
≤ φ
inv.ThesameholdsforIde,MonandBou.Therefore, the collecting semantics CS1 is not well suited to analyze the involution, idempotence, monotonicity and
boundnessproperties.
3.5. Relationshipsamongcollectingsemantics
Wenowexplorehowthetwocollectingsemanticsrelatetoeachother.Wenoticethatboth
α
CS1 andα
CS2 factorthroughtheother.Actually,
α
CS2=
α
12◦
α
CS1 whereα
12:
CS1→
CS2 isgivenbyα
12(φ)
= {
f:
D
⊥→
D
⊥| ∀
x∈
D
⊥,
f(
x)
∈ φ({
x})} ,
foreach
φ
∈
P(D
⊥)
→
−
∪P(D
⊥)
,whileα
CS1=
α
21◦
α
CS2 withα
21:
CS2→
CS1 givenbyα
21(
F)
= λ
X∈
P
(
D
⊥).
{
f(
X)
|
f∈
F}
foreach F
∈
P(D
⊥→
D
⊥)
.ThiscontrastswiththestandardconsiderationthatCS2 ismoreprecisethan CS1 andnotviceversa.However,thedifferentprecisionbetweenthetwoisapparentwhenwenotethat,foreach
φ
∈
CS1,wehaveα
CS1(
e)
≤ φ ⇐⇒
α
CS2(
e)
⊆
α
12(φ) .
However,theconverseisnottrue:givenF
∈
CS2,inthegeneralcasethereisnoφ
F∈
CS1 suchthatα
CS2(
e)
⊆
F⇐⇒
α
CS1(
e)
≤ φ
F.
(1)ThereforeCS2ismore
γ
-precisethanCS1butnotviceversa.Example28(CS1isnotmore
γ
-precisethanCS2).LetF= {λ
x.
⊥,
λ
x.
a}
foragivena∈
D
.Thenα
CS2(
e)
⊆
F iffe isaprogramwhichalways divergesoralwaysterminateswithresulta.Ifwechoose
φ
F=
α
21(
F)
,whichone mightthink asasensiblechoice in(1),we have
φ
F(
X)
= {⊥,
a}
foranynonempty X .Therefore,ife is aprogramwhichalways divergesoralways terminateswithresulta,wehaveα
CS1(
e)
≤ φ
F.However,thesameholdsforanyprogramwhichreturnsbothoutputs⊥
anda fordifferentinputs.Therefore,theleftandrighthand-sidesin(1) arenotequivalent. Ingeneral,foranyφ
F wemay choose,theconditionα
CS1(
e)
≤ φ
F isnotabletoselectfunctionswhichalwaysreturnthesamevalue.Thefollowingpropositionsummarizesthepreviousarguments.
Proposition29.Thefollowingholds:
•
thereisnoGaloisconnectionα
12,
γ
12:
CS1CS2suchthatα
CS2=
α
12◦
α
CS1•
α
21hasarightadjoint,whichisα
12•
α
1strhasarightadjointwhichisγ
1str:
Str→
CS1definedas:γ
1str(
str)
=λ
X∈
P
(
D
⊥).
⎧
⎪
⎨
⎪
⎩
∅
if X= ∅
{⊥}
if X= {⊥}
D
⊥ otherwiseγ
1str(
) =λ
X∈
P
(
D
⊥).
∅
if X= ∅
,D
⊥ otherwise•
thereisnoGaloisconnectionα
1const,
γ
1const:
CS1Constsuchthatα
const=
α
1const◦
α
CS1.4. Adequacyofcollectingsemantics
Inthissectionweelaborateontheconceptsofadequacyforafamilyofabstractions.Wedefineacategorywhoseobjects areabstractinterpretationsandwhosemorphismsaretransformationsfrommoreprecisetolesspreciseabstractions(see, e.g., AspertiandLongo [8] forstandarddefinitionsincategorytheory).
Definition30(Categoryofabstractinterpretations).Wecall
AI(C
)
thecategorywhoseobjectsareabstractinterpretationsfor C andwhosemorphismsareGaloisconnectionα
,
γ
:
AB suchthatα
B=
α
◦
α
A.Collecting semantics are a starting point when designing new abstract interpretations. If an abstract interpretation
(
A,
α
A)
is designed starting from the collecting semantics(
S,
α
S)
, it means that(
S,
α
S)
should be more precise than(
A,
α
A)
, hence thereshould be a map from(
S,
α
S)
to(
A,
α
A)
in ourcategoryAI(C
)
. Moreover, it wouldbe preferable tohavea uniquewayofderiving(
A,
α
A)
asaGaloisconnectionfrom(
S,
α
S)
,accordingtotheconceptofadequacy intro-ducedintheprevioussections.Inthecategoricalsettings,thisleadstothenotionofinitiality:if(
S,
α
S)
isinitialforagiven fullsubcategoryD
ofAI(C
)
,itmeansthat(
S,
α
S)
isadequateforthefamilyofabstractionsinD.
Given a collecting semantics
(
S,
α
S)
, we are interested in characterizing the maximal full subcategoryD
ofAI(C
)
suchthat(
S,
α
S)
isinitial forD.
Suchsubcategoriesimmediatelyinduceataxonomyonprogramanalysiswhichprecisely characterizestheprogrampropertiessuitable foragivencollectingsemantics.Intherestofthesection, weshowthat for thetwocollectingsemanticsCS1andCS2,suchafullsubcategorycanbeconstructivelydescribed.4.1. ThecollectingsemanticsCS2
Wefirstshow thatCS2 isinitialforalltheabstractinterpretations whichhaveenoughjoins,andthat thisisthelargest
classofabstractinterpretationswhichenjoysthisproperty.
Definition31(Havingenoughjoins).Wesaythattheabstractinterpretation
(
A,
α
A)
hasenoughjoinswhenAX existsfor each X whichisasubsetoftheimageofα
A.Obviously,if A isacompletejoin-semilattice,than
(
A,
α
A)
hasenoughjoins.Example32.Let
(D
⊥,
)
beaposetsuchthat⊥
d foreachd∈
D
⊥.Wesaythatafunction f:
D
⊥→
D
⊥hasamaximum value d∈
D
ifthereexists y∈
D
⊥ such thatforeach x∈
D
⊥, f(
x)
f(
y)
=
d.Consider theabstractinterpretation Max=(D
⊥,
α
max)
suchthatα
max(
f)
=
f
(
y)
if f(
y)
∈
D
and∀
x∈
D
⊥, f(
x)
f(
y)
⊥
otherwiseNotethat
α
max(
f)
=
d∈
D
when d isthe maximumvalue of f .We havethat Maxhasenough joinsifandonly ifD
is acompletejoin-semilattice.Therefore,ifD
⊥= N ∪ {⊥}
withthestandardorderingonnaturalnumbers,thenD
⊥ hasnot enoughjoins.Theorem33.Thefullsubcategoryofalltheabstractinterpretationswhichhaveenoughjoinsisthelargestclassofabstractionsfor whichthecollectingsemanticsCS2isinitial.
4.2. ThecollectingsemanticsCS1
We now show that the collecting semantics CS1 is initial for a large class of abstract interpretations, which can be
constructivelycharacterized.
Definition34(Mixoffunctions).Wesaythatafunction g
:
D
⊥→
D
⊥isamixofthesetoffunctions F⊆
D
⊥→
D
⊥ifffor eachx∈
D
⊥thereexists f∈
F suchthat g(
x)
=
f(
x)
.Definition35(Mixableinterpretations).Let
(
A,
α
A)
beanabstractinterpretationsuchthat A hasenoughjoins.Wecall(
A,
α
A)
mixable if,foranysetoffunctionsF⊆
D
⊥→
D
⊥,whenever g isamixoffunctionsin F ,wehaveα
A(
g)
≤
f∈Fα
A(
f)
.Aninterpretation is mixable whendecidingwhether