12 October 2006 Foundations of Logic and Constraint Programming 1 Operational Semantics An overview...
-
date post
21-Dec-2015 -
Category
Documents
-
view
216 -
download
3
Transcript of 12 October 2006 Foundations of Logic and Constraint Programming 1 Operational Semantics An overview...
12 October 2006 Foundations of Logic and Constraint Programming 1
Operational Semantics
Anoverview
• Thelanguageofprograms
• Thecomputationmechanism
• Choicesandtheirimpact
12 October 2006 Foundations of Logic and Constraint Programming 2
Atoms and Herbrand Bases
The basic constructs of programs and queries are atoms (predicates).Syntacticaly, atoms are similar to functions, but defined over predicatesymbolsratherthanfunctionsymbols.
Given
thetermuniverseTUF,V(overvariablesVandFunctionsymbolsF)
arankedalphabetofpredicatesymbols
the termbaseTB ,F,V is (over , FandV) is thesmallestsetAofatomssuchthat
1. p A,ifp(0)
2. p(t1,t2, ... ,tn) Aifp (n)withn1,and
t1,t2, ... , tn TUF,V
Given the Herbrand Universe, HUF, and the ranked alphabet of predicatesymbols:
Herbrand BaseHB(over andF) : TB ,F,V
12 October 2006 Foundations of Logic and Constraint Programming 3
Programs and Queries
Fromatoms,bothprogramsandqueriescanbeconstructed.
Query: finitesequenceofatomsB1, B2, ..., Bn.
Empty Query : emptysequenceofatoms.
- Anemptyqueryisdenotedby□
H B (definiteclause) :
- H(theclausehead)isanatom,
- B(theclausebody)isaquery.
H (unitclause) :
- Theclausebodyisempty
(Definite) program : finitesetofdefiniteandunitclauses
Horn Clause : Clauseoranegatedquery
12 October 2006 Foundations of Logic and Constraint Programming 4
Intuitive Meaning of Clauses and Queries
Clauses are obtained from the atoms by connecting them with Booleanconnectives.Hencetheirintuitivemeaning:
AclauseH B1, .. , Bncanbeunderstoodastheformula
x1, ..., xk (B1 ... Bn H);or
x1, ..., xk (B1 ... Bn H)
wherex1, ... ,xkarethevariablesoccurringinH B1, .. , Bn.
AUnitclause,H ,encodesx1, ..., xk H.
Aquery,A1, ..., An,canbeunderstoodastheformula
x1, ..., xk (A1 ... An) ;or
x1, ..., xk (A1 ... An)
where,x1, ... ,xkarethevariablesoccurringinA1, ..., An.
Theemptyquery,□,isequivalenttotrue.
12 October 2006 Foundations of Logic and Constraint Programming 5
Queries and Negated Queries
Informally, one is interested in assessing whether a query is a logicalconsequence of a program. This is indeed equivalent to assess that thenegationofthequeryiscontradictorywiththeprogram.
Hence
x1, ..., xk (A1 ... An)
x1, ..., xk (A1 ... An)
x1, ..., xk ( A1 ... An)
x1, ..., xk (false A1 ... An )
x1, ..., xk (false (A1 ... An)
x1, ..., xk (false (A1 ... An)
A negated empty query is equivalent to false (since the empty query isequivalenttotrue)
Thecomputationmechanismoflogicprogrammingproveseither thataqueryisalogicalconsequenceoftheprogram thatthenegationofthequery,togetherwiththeprogram,entailsfalse.
12 October 2006 Foundations of Logic and Constraint Programming 6
What is computed
AprogramPcanbeunderstoodasasetofaxioms.
AqueryQcanbeinterpretedastherequestforfindinganinstance,Qθ,whichisalogicalconsequenceof P.
Asuccessfulderivationprovidessuchaθ
(bycompositionofthesubstitutionsperformedateachderivationstep)
Hence,thederivationisaproofofQθ(fromP).,i.e.
P |- Qθ
12 October 2006 Foundations of Logic and Constraint Programming 7
How it is computed
Acomputationisasequenceofderivationsteps.
In each step an atom A is selected from the current query and a program
clauseH Bischosen.
IfA andHareunifiable,andθisanmguofAand H,thenAisreplacedbyB
inthequery,andθisappliedtotheresultingquery.
Thecomputationissuccessfulifitendswiththeemptyquery.
The resultinganswersubstitution isobtainedbycompositionof themgusof
eachstep.
12 October 2006 Foundations of Logic and Constraint Programming 8
SLD-Derivations (proposicional)
Theschemadescribedassumesthat
atomsinthequeryareselectedaccordingtosomeSelectionrule,
derivationsarea(Linear)sequenceofresolutionsteps,andthat
theclausesintheprogramareallDefiniteclauses.
ThesequenceofresolutionstepsisthusknownasanSLD-derivation.
Inthepropositionalcase(novariables),given• Aprogram P• AqueryA, B, C(whereAmeansitcontainszeroormoreatoms).• AclauseB B
Bistheselectedatominthequery
Theresultingquery(A, B, C)iscalledtheSLD-resolvent.
ThenotationA, B, CA, B, Cmaybeusedinthiscase.
12 October 2006 Foundations of Logic and Constraint Programming 9
SLD-Derivations (example)
Givenprogram
1. happy :- sun, holidays.2. happy :- snow, holidays.3. snow :- cold, winter.4. cold :- winter5. precipitation :- holidays.6. winter.7. holidays.
ThefollowingisanSLD-derivationofquery?- happy
?- happy (clause 2)
?- snow, holidays (clause 3)
?- cold, winter, holidays(clause 4)
?- winter, winter, holidays (clause 6)
?- winter, holidays (clause 6)
?- holidays (clause 7)
?- □
12 October 2006 Foundations of Logic and Constraint Programming 10
SLD-Derivations (non proposicional)
SLDderivationscanbeextendedtofirst-orderpredicates(withvariables).
Given• Aprogram P• AqueryA, B, C• Aclausec P• AvariantH Bofc,variabledisjointwiththequery• AnmguθofBandH
SLD-resolventofA, B, Candcwrt.Bwithmguθ :
(A, B, C) θ
SLD-derivationstep:
(A, B, C) θc (A, B, C) θ
inputclause: variant H Bofc,
“clausecisapplicabletoatomB”
12 October 2006 Foundations of Logic and Constraint Programming 11
SLD-Derivations (example)
Example:Givenprogram
1. add(X,0,X).2. add(X,s(Y),s(Z)) :- add(X,Y,Z).
thefollowingisanSLD-derivationofquery?-add(s(0),W, s(s(s(0)))).
?-add(s(0),W, s(s(s(0)))).
(Variant a of Clause 2) {Xa/s(0),W/s(Ya),Za/s(s(0))}
?-add(s(0),Ya, s(s(0))).
(Variant b of Clause 2) {Xb/s(0),Ya/s(Yb),Zb/s(0))}
?-add(s(0),Yb, s(0)).
(Variant c of Clause 1) {Xc/s(0) ,Yb/0}
?- □
12 October 2006 Foundations of Logic and Constraint Programming 12
SLD-Derivations (example)
?-add(s(0),W, s(s(s(0)))). (2a) {Xa/s(0),W/s(Ya),Za/s(s(0))}
?-add(s(0),Ya, s(s(0))). (2b) {Xb/s(0),Ya/s(Yb),Zb/s(0))}
?-add(s(0),Yb, s(0)). (1c) {Xc/s(0) ,Yb/0}
?- □
Composingthesubstitutions,
(θ2a)(θ2b)=
{ Xa/s(0), W/s(s(Yb)), Za/s(s(0)),
Xb/s(0), Ya/s(Yb), Zb/s(0)) }
(θ2a)(θ2b)(θ1c)=
{Xa/s(0),W/s(s(0)),Za/s(s(0)),
Xb/s(0),Ya/s(0),Zb/s(0)),
Xc/s(0) ,Yb/0 }
theanswerW = s(s(0))isobtained.
12 October 2006 Foundations of Logic and Constraint Programming 13
SLD-Derivations Steps
1. Selection:
Selectanatominthequery
2. Renaming:
Rename(ifnecessary)theclause
3. Instantiation
Instantiatequeryandclausebyanmguoftheselectedatomandthehead
oftheclause
4. Replacement
Replacetheinstanceoftheselectedatombytheinstanceofthebodyof
theclause
12 October 2006 Foundations of Logic and Constraint Programming 14
SLD-Derivations
AmaximalsequenceofSLD-derivationsteps
Q0 θ1c1
Q1 θ2c2
Q2 ... Qn θn+1cn+1
Qn+1 ...
isan
SLD-derivationofP{Q0}:
Q0, Q1, ... , Qn+1, ...arequeries,eachemptyorwithoneatomselectedinit;
θ1, θ2, ... , θn+1, ...aresubstitutions;
c1, c2, ... , cn+1, ...areclausesofP;
ForeverySLD-derivationstep,standardisation apartholds.
12 October 2006 Foundations of Logic and Constraint Programming 15
Standardisation Apart
Variablesinaclauseareuniversallyquantified.Itisthusimportantthatwhena
clause isusedavariantof theclause isusedwith“fresh”variablesthathave
notbeenpreviously“used”.
Thisguarantees that the inputclause isvariabledisjoint fromthe initialquery
andfromthesubstitutionsandinputclausesusedatearliersteps.
Formally:
Var(c’i ) (Var(Q0) j=1..i-1 (Var (θj Var(c´j )) =
fori 1,where c’i(variantofclauseCi P)istheinputclauseusedinthei-th
SLD-derivationstepQi θi+1ci+1 Qi+1
12 October 2006 Foundations of Logic and Constraint Programming 16
Result of a Derivation
Letξ =Q0 θ1c1 Q1 θ2
c2 Q2 ... Qn-1 θncn Qn beafiniteSLD-derivation.
ξ is successful:Qn = □
ξ failed:Qn □ andnoclauseisapplicabletoanyatomofQn.
Letξbesuccessful.
ComputedAnswerSubstitution (CAS) ofQ0(w.r.t.ξ)
: ( θ1 θ2 ... θn ) | Var(Q0)
ComputedInstanceofQ0(w.r.t.ξ)
: Q0 θ1 θ2 ... θn
12 October 2006 Foundations of Logic and Constraint Programming 17
Choices
AnumberofchoicescanbemadeinanySLD-derivation,namely
1. Choiceoftherenaming
2. Choiceofthemgu
3. Choiceoftheselectedatom
4. Choiceoftheprogramclause
Howdotheyinfluencetheresult?
12 October 2006 Foundations of Logic and Constraint Programming 18
Resultants: What is Proved after a Step
To assess the impact of the choices, it is convenient to assess what is
“logically”computedateachderivationstep.
Informally, toansweraqueryQ1, requiresansweringasubsequentqueryQ2.
HenceifQ2istrueforsomesubstitutionθsoisQ1.Moreformally,
ResultantAssociatedwithQ1 θ1 Q2 :Implication Q1θ1 Q2
Consider Aprogram P Aresultant R = Q A, B, C Aclause c AvariantH Bofc,variabledisjointwithR AnmguθofB andH
SLD – Resolvent ofresultantRandcwrtBwithmguθ:
Q (A, B, C) θ
SLD – Resultant step :
Q A, B, C θc Q θ (A, B, C) θ
12 October 2006 Foundations of Logic and Constraint Programming 19
Propagation
ConsideranSLD–Derivation
ξ =Q0 θ1c1 Q1 ... Qn θn+1
cn+1Qn+1 ...
Resultantantofleveliofξ, Ri :
Q0 θ1 θ2 ... θi Qi fori0
The resultant Ri describes what is proved wrt to the initial query Q0, after iderivationsteps.Inparticular
Nothinghasbeenproveninthebegining
R0:Q0 Q0
Thequeryhasbeenanswered,ifthederivationissuccessful
Rn :Q0 θ1 θ2 ... θn ifQn=□(since□=true)
12 October 2006 Foundations of Logic and Constraint Programming 20
Resultants: SLD - derivations
TheselectedatomofaresultantQ1 Qi isdefinedastheatomselectedinQi.
Lemma 3.12
AssumethatR θc R1andR’ θ’
c R’1aretwoSLD–resultantstepssuchthat
RisaninstanceofR’
InRandR’atomsinthesamepositionsareselected
thenR1isaninstanceofR’1.
Proof:See[Apt97],page55.
Sketch:IfRisaninstanceofR’,thenθ = θ’σ (forsomeσ)andR = R’ σ.
12 October 2006 Foundations of Logic and Constraint Programming 21
Resultants: SLD - derivations
Example:
Letusconsider
clausec:q(W,b):- s(W).
resultantR:p(a,Y) :- q(a,Z), r(Z,Y).
resultantR’:p(X,Y) :- q(X,Z), r(Z,Y).
atomq/2isselectedinbothresultants.
Now,ifatomq/2isselectedinbothresultants,itmustbe
θ = mgu(q(a,Z), q(W,b)) = {W/a, Z/b},andfromR,candθ
resultantR1:p(a,Y) :- s(a), r(b,Y).
θ’ = mgu(q(X,Z), q(W,b)) = {W/X, Y/b},andfromR’,candθ’
resultantR’1:p(X,Y) :- s(X), r(b,Y).
thusconfirmingthatR1isaninstanceofR’1.Moreover,itis
θ = θ’ {X/a} andso R1=R’1 {X/a}
12 October 2006 Foundations of Logic and Constraint Programming 22
Resultants: SLD-derivations
AsimilarresultcanbeobtainedforSLD-resolvents
Corollary 3.13
WhenQ θc Q1andQ’ θ’
c Q’1aretwoSLD–resultantstepssuchthat
QisaninstanceofQ’
InRandR’,atomsinthesamepositionsareselected
thenQ1isaninstanceofQ’1.
Example:
From Q: q(a,Z), r(Z,Y) and Q’: q(X,Z), r(Z,Y) if atom q/2 is
selectedinbothresolvents,togetherwithclausec:q(W,b):- s(W),then
Q1:s(a), r(b,Y)andQ’1:s(X), r(b,Y)
12 October 2006 Foundations of Logic and Constraint Programming 23
Similar SLD-derivations
Considertwo(initialfragments)ofSLD–derivations
ξ =Q0 θ1c1 Q1 ... Qn θn+1
cn+1Qn+1 ...
ξ’ =Q’0 θ’1c1 Q’1 ... Q’n θ’n+1
cn+1Q’n+1 ...
ξ and ξ’ are similar :
lenght(ξ ) = lenght(ξ’ );
Q0 andQ’0 arevariants,
inQi andQ’i atomsinthesamepositionsareselected(foriin0..n)
12 October 2006 Foundations of Logic and Constraint Programming 24
A Theorem on Variants
Theorem 3.18:
Consider two similar SLD – derivations ξ and ξ’ . Then for every i 0, the
resultants Ri and R’i of level i of ξ and ξ’, respectively, are variants of each
other.
Proof(byinduction):
Base case (i=0) :
R0:Q0 Q0 and R’0:Q’0 Q’0 .ButQ0 is a variant of Q’0 (bydefinitionof
similarderivations),henceQ0 Q0isavariantofQ’0 Q’0 .
Induction case (i > i+1) :
LetRi θi+1ci+1
Ri+1 and R’ i θ’i+1ci+1
R’i+1.Then
Ri isavariantofR’i(inductionhypothesis)
Ri isaninstanceofR’iandvice-versa(definitionofvariant)
Ri+1 isaninstanceofR’i+1andvice-versa(lemma3.12)
Ri+1 isavariantofR’i+1(definitionofvariant)
12 October 2006 Foundations of Logic and Constraint Programming 25
Answer Substitutions of Similar SLD-derivations
Corollary 3.19:
Consider two similar SLD – derivations of Q0 with computed answers
substitutionsθ and σ . ThenQ0θ andQ0 σarevariantsofeachother.
Proof:
Ifthederivationsaresuccessful,thentheirfinalresultantsare
ξ :Q0θ □ and ξ ‘:Q0 σ □ .
Then,bytheorem3.18,Q0θ and Q0 σ arevariants.
Hence,
Choices of type 1 (choice of a renaming)
Choices of type 2 (choice of an mgu)
do not influence - modulo renaming – on the statement proved by a
successful SLD–derivation.
12 October 2006 Foundations of Logic and Constraint Programming 26
Atom Selection in Queries
Let INIT be the set of all initial fragments of all possible SLD-derivations in
whichthelastqueryisnon-empty.
Aselectionrule,isafunctionwhichforeveryξ< INIT yieldsanoccurrenceof
anatominthelastqueryof ξ<
AnSLD-derivationξisviaaselectionruleRifforeveryinitialfragmentξ< ofξ
endingwithanon-emptyqueryQ,R (ξ< ) istheselectedatomofQ.
Example:
InProlog,theruleusedis“select the leftmost atom”
12 October 2006 Foundations of Logic and Constraint Programming 27
Switching Lemma
Lemma 3.32
ConsideranSLD-derivation
ξ =Q0 θ1c1 Q1 ... Qn θn+1
cn+1 Qn+1 θn+2cn+2 Qn+2 ...
where Qn includesatoms A1 and A2
A1 istheselectedatomofQn
A2θn+1 istheselectedatomofQn+1
Thenforsome Q’n+1, θ’n+1 and θ’n+2 thereisanSLD-derivation
ξ’ =Q0 θ1c1 Q1 ... Qn θ’n+1
cn+1Q’n+1 θ’n+2cn+2Qn+2 ...
where A2 istheselectedatomofatoms Qn
A1 θ’n+1 istheselectedatomofQ’n+1
θ’n+1 θ’n+2 =θn+1 θn+2
Proof:see[Apt97,page65].
12 October 2006 Foundations of Logic and Constraint Programming 28
Independence of Selection Rule
Theorem 3.33
Let ξ be a successful SLD-derivation of P {Q0}. Then for every selection
ruleR , thereexistsasuccessfulSLD-derivation ξ’ofP {Q0} viaR such
that
CAS ofQ0 (wrt ξ) = CAS ofQ0 (wrt ξ’)
ξ andξ’ areofthesamelength
Hence, choices of type 3 (choice of a selected atom) have no influence in
case of successful queries.
12 October 2006 Foundations of Logic and Constraint Programming 29
Independence of Selection Rule
Proof Sketch (of Theorem 3.33):(By induction on the number of derivation steps)
Base case (i=1) : If thederivationhasonlyonestep, thanQ0 hasasingleatomandany
rulemustselectit.
Induction case (i > i+1) : Assume that the first i stepsof thesuccessfulderivationξ aresimilar to the
firsti stepsobtainedbyusingruleR..
Assume further that in the i+1th stepξ selectsatomA inQi, whereas ruleR
selectsatomB. SinceBmustbeselectedinsomesubsequentstepjofξ(i +1 j n),itis
ξ =Q0 ... Qi[A] θi+1
ci+1 Qi+1 ... Qj[B] θj+1
cj+1 Qj+1 ... Qn-1 Qn = □
Applying the switching lemma j-i times to ξ the successful derivation ξ’ is
obtained
ξ’ =Q0 ... Qi[B] σi+1
c’i+1 Q’i+1[A] ... Q’n -1Qn = □.
forwhichthefirsti+1stepsaresimilartothoseobtainedbyusingruleR.
12 October 2006 Foundations of Logic and Constraint Programming 30
SLD-Trees and Search Space
Selectionrule Rvariant independent:
For all initial fragments of SLD-derivations that are similar, R chooses the
atominthesamepositionofthelastquery.
Examples:
Prolog’s rule “select leftmost atom” in the query is variant
independent.
Aselection rulesuchas “select the rightmost atom with predicate
symbol p with arity k, otherwise select the leftmost atom” isalso
variantindependent
Aselectionrulesuchas“select leftmost atom if variable X appears
in the query, otherwise select rightmost atom” is not variant
independent
12 October 2006 Foundations of Logic and Constraint Programming 31
SLD-Trees and Search Space
SLD-Tree forP {Q} viaselectionrule R:
ThebranchesareSLD-derivationsofP {Q} viaR
Every node Q with selected atom A has exactly one descendant for everyclausecofP,whichisapplicabletoA.
ThisdescendantisaresolventofQandcwrtA.
SLD-Tree successful:
AnSLD-treethatcontainsthesuccessleaf□.
SLD-Tree finitelyfailed:
AnSLD-treethatisfiniteandnotsuccessfully.
TheSLD-Tree via“leftmost selection rule”correspondstoProlog’sstrategyforsearchingforsolutions..
12 October 2006 Foundations of Logic and Constraint Programming 32
The Branch Theorem
Theorem 3.38
ConsideranSLD-treeT forP {Q0}viaavariantdependentselectionrule
R.TheneverySLD-derivationofP {Q0}viaRissimilartoabranchinT.
Hence, choices of type 4 (choice of a program clause) have no influence
on the search space as a whole.
Nevertheless,thesamesearchspacecanbesearcheddifferentlybydifferent
strategies.Forexample,
Ifaninfinitebranchistothe“left”ofanysuccessnode,the“left to right
depth first search”ofPrologdoesnotsucceed(notermination).
Iftheorderoftheclausesischanged,thanitisasifPrologwere“right to
left depth first search”, thus finding the solution (before entering the
endlessloop).
12 October 2006 Foundations of Logic and Constraint Programming 33
The Branch Theorem
Proof Sketch (of Theorem 3.38):
Letξ =Q0 Q1 Q2 ... beanSLDderivationof P {Q0}viaR
Byinductiononi0,abranch(withnodesQ’0, Q’1, Q’2, ...)inTsimilartoξ should be found:
Base case (i=0) :
Q’0 = Q0(inparticulartheyarevariants).
Induction case (i > i+1) :
Byinductionhypothesis,Q0 ... Qi issimilartoQ’0 ... Q’i
BydefinitionofT ,theexistenceofQ’iimpliestheexistenceofQ’i+1(applythesameclauseastoQi).
By variant independence, in Qi and Q’i atoms in the same position areselected,soQ0 ... Qi+1 isalsosimilartoQ’0 ... Q’i+1..