10 october 2006 foundations of logic and constraint programming 1 unification an overview need for...
TRANSCRIPT
10 October 2006 Foundations of Logic and Constraint Programming 1
Unification
Anoverview
• NeedforUnification
• Rankedalfabethsandterms.
• Substitutions
• Unifiersandmgus
• MartelliMontanariAlgorithm
10 October 2006 Foundations of Logic and Constraint Programming 2
Why unification ?
Inpropositionallogictwoclausesmayresolveiftheyhavetwocomplementaryliterals.
In the propositional case, two literals are the same if they have the same proposition(negatedinexactlyoneofthem).
In first order logic, the situation is more complex, due to the existence ofvariables.Wemayassumethatvariablesareuniversallyquantified.
Inthiscase,toapplyresolution,thepropositiondonothavetobethesameinthecomplementaryliteralsbelongingtotwoclauses.
Intuitively,sincethevariablesareuniversallyquantifiedandmaybereplacedbyany“objects”,thepredicatemustrefertothesameobjects.Forexample
A P P BA B
q(X) p(X,b) P(a,Y) r(Y)q(a) r(b)
10 October 2006 Foundations of Logic and Constraint Programming 3
Term Universe
Informally, the purpose of unification is to findwhat the common objects are.Suchgoalmaybeacomplishedbypurelysyntacticmeans,throughprocessingofthetermsappearinginthecommonpredicates.
Atermiscomposedofoneormoresymbolsfrom• Variablesymbols(convention:startingwithuppercase)• AlphabetisafinitesetΣofsymbols(convention:startingwithlowercase)
• Toeverysymbolanaturalnumber0isassigned(itsarityorrank).• Σ(n)denotesthesubsetofΣwitharityn• Asymbolwitharity0(belongingtoΣ(0))isaconstant.
Given a set of variablesV and a ranked alphabet of function symbolsF, thetermuniverseTUF,V(overFandV)isthesmallestsetToftermssuchthat
a)V T
b)f T,iffF(0)
c)f(t1,t2, ... ,tn) Tiff F(n)andt1,t2, ... , tn T
10 October 2006 Foundations of Logic and Constraint Programming 4
Ground terms and Herbrand Universe
ThesetofvariablesappearinginatermtisdenotedbyVar(t) Var(t):setofvariablesintermt
Atermisgroundifitcontainsnovariables tisground:Var(t) =
IfFisarankedalphabetoffunctionsymbols,thenHUF,theHerbrandUniverseoverF,isthesetoftermsthatcanbeformedwithoutvariables
HUF:TUF,
Example:GivenF = {a, f/1, g/2}itisHUF= = { a, f(a), g(a,a),
g(a,f(a)), g(f(a),a), g(g(a,a),a), g(a, g(a,a)), f(f(a)), f(g(a,a)), g(f(a),f(a)), g(f(a),g(a,a)),
g(g(a,a),f(a)), g(g(a,a),g(a,a)),... }
Asubtermsoft,isatermthatisincludedint s=sub_term(t):sisatermthatisasub-stringoft
10 October 2006 Foundations of Logic and Constraint Programming 5
Substitutions
Informally a variable (universally quantified) may be substituted by a term(withsomeconstraints).Moreformally,given
• V,asetofVariables• XV,asubsetofvariablestobesubstituted• F,arankedalphabet
asubtitutionθ:X→TUF,Vwithxθ(x)foranyx X.
Thefollowingnotationiscommonlyusedforasubstitutionθ
θ = {x1/t1, x2/t2, ... xn/tn },where
1. X = {x1, x2, ... xn },
2. θ(xi) = ti for every xi X
Itisalsoconvenienttodefine Emptysubstitutionε:n=0 θisagroundsubstitution:t1, t2, ... , tn aregroundterms.
θisapurevariablesubstitution:t1, t2, ... , tn arevariables.
θisarenaming:{t1, t2, ... , tn }{x1, x2, ... xn }.
10 October 2006 Foundations of Logic and Constraint Programming 6
Substitutions
Forasubtitutionθitisalsoconvenienttoconsider
Thedomainofthesubstitutionisthesetofvariablestobesubstituted
Dom(θ ):{x1, x2, ... xn }.
TheRangeofthesubstitutionisthesetoftermsforwhichthevariablearesubstituted
Range(θ ):{t1, t2, ... , tn }
TheRanofthesubstitutionisthesetofvariablesappearingintheseterms
Ran(θ ):Var(Range(θ ) )
Thevariablesofthesubstitutionarethoseinitsdomainorinitsran.
Var(θ ):Dom(θ) Ran(θ )
TheprojectionofasubstitutiontoasubsetofitsdomainYDom(θ)
θ | Y :{y/t | y/t θ and y Y)
10 October 2006 Foundations of Logic and Constraint Programming 7
Application of Substitutions
A few notational conventions and definitions are commonly used when asubstitutionθisapplied
IfxisavariableandxDom(θ), thenxθ :θ(x)
IfxisavariableandxDom(θ), thenxθ :x
f(t1, t2, ... , tn )θ :f(t1θ, t2θ, ... , tn θ )
t is an instance of s :thereisasubstitutionθ with sθ = t
s is more general than t :t isaninstanceof s
t is a variant of s :thereisarenaming θ with sθ = t
Lemma 2.5
t is a variant of s ifft is an instance of sands is an instance of t.
10 October 2006 Foundations of Logic and Constraint Programming 8
Composition of Substitutions
Duringaresolutionproof,severalsubstitutionsareperformedinsequence,inwhichvariablesaresuccessivelyreplacedbytermswithothervariables.Thenotion of composition captures the intuition of end result of thesesubstitutions.
Definition(2.6) Thecompositionofsubstitutionsθandσ isdefinedas
(θ σ) x :x (θ)σ foreveryvariable x
Lemma (2.3)
Let θ = {x1/t1, x2/t2, ... xn/tn },σ = {y1/s1, y2/s2, ... ym/sm }. Thenθ σcanbeconstructedfromthesequence
x1/t1 σ, x2/t2 σ, ... xn/tn σ, , y1/s1, y2/s2, ... ym/sm
1. Byremovingallbindings xi/ti σ wherexi = ti σ
2. Byremovingallbindings yj/sj whereyj {x1, x2, ... xn}
3. Byformingasubstitutionfromtheresultingsequence.
10 October 2006 Foundations of Logic and Constraint Programming 9
An Ordering of Substitutions
Asubstitutionrestrictsthevaluesavariablemaytake,andthemoreso,whenmore substitutions are applied in sequence. This augmented restriction iscapturedbytheorderingofsubstitutions.
Definition(2.6)
Letθandσbesubstitutions.Then
θismoregeneralthanσ:σ = θ forsomesubstitution.
(equivalently,σ isaspecialisationofθ,orθcanbespecialisedtoσ).
Examples:
θ = {x/y} ismoregeneralthan σ = {x/a, y/a} sincefor = {y/a} itis σ = θ. Since, y = a,then θ = {x/yσ , y/a} = {x/a, y/a} = σ.
θ = {x/y} isnotmoregeneralthan σ = {x/a}, sinceeverysuchthatθ = σ shouldcontainapairy/a, andthatpairshouldappearinθ .
x/a {x/y} y/a y Dom(σ ) = Dom (θ)
Intuitively, σ = {x/a} canbespecialisedto{x/a, y/b}but θ = {x/y} cannot,soθ cannotbemoregeneralthan σ.
10 October 2006 Foundations of Logic and Constraint Programming 10
Unifiers
The resolution of two clauses does not require the occorrence ofcomplementary literals thatareequal (apart from thenegation)but rather thatthecomplementaryliteralsareapplicabletothesameobjects.Hence,thetermsintheliteralsmustbeunifiable,i.e.equalafterperformingsomesubstitution.
Definition(2.9)
Givensubstitution θandtermssandt
θisaunifierofsandt:sθ = tθ.
s and t are unifiable :sθ = tθ forsome θ.
Manyunifiersmayexistbetweentwoterms,butitisspeciallyinteresting(e.g.inresolution)toconsiderthemostgeneralunifier(mgu).
θisamguofsandt:sθ = tθ s σ = t σ σ = θ forsome .
Thedefinitionofunifiersforpairsofterms,maybeextendedforsetsofpairsofterms.
10 October 2006 Foundations of Logic and Constraint Programming 11
Most General Unifiers
• The literals to be unified may have more than one argument. The followingdefinitionsarerelevantinthiscase.
- Lets1, s2, ... , sn, t1, t2, ... , tn,beterms;si ≈ tidenotethe(ordered)pair(si, ti); and E = {s1 ≈ t1, s2 ≈ t2, ... Sn ≈ tn }. Then
θisaunifierofE:siθ = tiθ for all i 1..n.
θisanmguofE:σ isaunifierofE σ = θ forsome .
SetsEandE’areequivalent
:θisaunifierofEθisaunifierofE’
E = {x1 ≈ t1, x2 ≈ t2, ... xn ≈ tn } is solved :
xi, xjarepairwisedistinctvariables(1ij,n)
noxi, occurs in tj (1i,j,n)
10 October 2006 Foundations of Logic and Constraint Programming 12
Most General Unifiers
Lemma (2.15)
IfE = {x1 ≈ t1, x2 ≈ t2, ... xn ≈ tn } issolved,then θ = {x1/t1, x2/t2, ... xn/tn },isanmguofE.
Proof:
1)xi θ = ti= ti θ
2)Foreveryunifier σ of E, it is xiσ = ti σ. But ti= ti θ andso xiσ = xi θ σ; forevery i1..n.Additionally,x σ = x θ σ =x for everyx {x1, x2, ... , xn}. Hence, σ = θ σandθ isthusmoregeneralthan σ.
• Oncetheunificationofpredicatesandtermswithmorethanoneargumentisdefined, it is important toobtainanalgorithmtoperformtheunificationof twoterms.
• Infact,itiseasiertodefineanalgorithmthatunifiesasetofpairsofterms–theMartelli–Montanarialgorithm.
10 October 2006 Foundations of Logic and Constraint Programming 13
Martelli – Montanari algorithm
Martelli-Montaneri algorithm (MM)
Let E be a set of pairs of terms. As long as it is possible, choosenondeterministicallyapairinEandperformtheappropriateaction:
a) f(s1,s2, .. , sn) ≈ f(t1,t2, .. , tn)
→replace,inE,bys1 ≈ t1,s1 ≈ t1,...,sn ≈ tn• f(s1,s2, .. , sn) ≈ g(t1,t2, .. , tn)wheref g
→haltwithfailure(clash)• x ≈ x
→deletethepair• t ≈ x wheretisnotavariable
→replacebyx ≈ t • x ≈ t wherex Var(t) andxoccursinsomeotherpair
→performsubstitution{x / t } inthosepairs • x ≈ t wherex Var(t)andx t
→haltwithfailure(occurs check)
Thealgorithmterminateswhennoactioncanbeperformed.
10 October 2006 Foundations of Logic and Constraint Programming 14
Martelli – Montanari algorithm
Theorem 2.16
Iftheoriginalsethasaunifier,thealgorithmMMsuccessfullyterminatesandproduces a solved set E´ that is equivalent to E; otherwise the algorithmterminateswithfailure.
Proof Steps:
1. Provethatthealgorithmterminates.
2. Provethateachactionreplacesthesetofpairsbyanequivalentone.
3. Prove that the algorithm terminateswith failure, then the set of pairs at themomentoffailurehasnounifiers*.• Given2,thesetE’isequivalenttoE,soEalsohasnounifiers
4. Prove that if the algorithm terminates successfully, the final set of pairs issolved**.• Bylemma2.15,ifthefinalsetofpairsissolved,thenitisanmgu.
10 October 2006 Foundations of Logic and Constraint Programming 15
Substitution Ordering
Beforegoing througheachof thesteps, thenotionofwell-foundedorderings mustbedefinedforrelationsonsets.
Intuitively,evenifthesetisinfinite,anwellfoundedrelationguaranteesthatthesetscanbeorderedsothattheyhavean“initial”element.
Firstsomedefinitionsonrelations
Definitions
RrelationonsetA :R A A
R reflexive :(a,a) R forall a A
R irreflexive :(a,a) R forall a A
R antisymmetric :(a,b) R and(b,a) R impliesthat a = b.
R transitive :(a,b) R and(b,c) R impliesthat(a,c) R .
10 October 2006 Foundations of Logic and Constraint Programming 16
Lexicographic Ordering
The lexicographic ordering<n (n>1) is defined inductively on the set Nn of n-tuplesofnaturalnumbers.
BaseClause
(a1) <1 (b1) :a1 < b1
InductiveClause
(a1, a2, ... , an+1) <n+1 (b1, b2, ... , bn+1) :
(a1, a2, ... , an) <n+1 (b1, b2, ... , bn) ;or
(a1, a2, ... , an) = (b1, b2, ... , bn) and an+1 <n bn+1
Examples:(5,3,9)<3(5,3,11),(1,4,8)<3(2,3,5).
Thelexicographicordering(Nn,<n)iswell-founded
Irreflexivepartialordering(irreflexibleandtransitive)with Lowestelement:(0,0,...,0)
10 October 2006 Foundations of Logic and Constraint Programming 17
Well founded orderings
Oncedefinedthebasicpropertiesofrelationssomepropertiesmaybedefinedontheorderingstheyinduceinparticularsets..
Definitions
(A, R)is a (reflexive) partial ordering:R isareflexive,antisymmetric,andtransitiverelationon A
(A, R)is a irreflexive partial ordering:R isairreflexiveandtransitiverelationon A
(A, R)is well-founded ordering:R isairreflexivepartialordering;and
thereisnoinfinitedescendingchain ... a2 R a1 R a0.
ExamplesPartialOrderings: (N, ), (Z, ), (({1,2,3,4}), )IrreflexivepartialOrderings: (N, <), (Z, <), (({1,2,3,4}), )WellFounded: (N, <), (({1,2,3,4}), )NotWellFounded: (Z, <),
10 October 2006 Foundations of Logic and Constraint Programming 18
Algorithm MM terminates
1. MM terminates (1)
Letthefollowingdenotationsapply
VariablexsolvedinE:
x ≈ t E,andthisistheonlyoccurrenceof x in E
Uns(E) :
numberofvariablesinEthatareunsolved
lfun(E):
number of occurrences of function symbols in the first components ofpairsinE
card(E):
numberofpairsinE
Letusconsidertuple(Uns(E) , lfun(E), card(E)).ToproveterminationofMM,itissufficient toprovethateachaction inMMreducesthistuple.Fromthewell-foundnessof3tuples,therecursionmustterminate(intheworstcase,itcannotgobelowthelowestelement).
10 October 2006 Foundations of Logic and Constraint Programming 19
Algorithm MM terminates
1. MM terminates (2) Letusconsidertuple(u,l,c)=(Uns(E) , lfun(E), card(E)) toshowthattheMM
operationsexceptb)andf),leadingtofailure,alwaysleadtoalowertuple.
a) f(s1,s2, .. , sn) ≈ f(t1,t2, .. , tn)→replace,inE,bys1 ≈ t1,s2 ≈ t2,...,sn ≈ tn
(u,l,c) → (u-k, l-1, c+n-1)forsomek 1..n
a) x ≈ x→deletethepair
(u,l,c) → (u-k, l, c-1)forsomek 0..1
• t ≈ x wheretisnotavariable→replacebyx ≈ t
(u,l,c)→(u-k1, l-k2, c)forsomek1 0..1 and k2 1
a) x ≈ t wherex Var(t) andxoccursinsomeotherpair→performsubstitution{x / t } inthosepairs
(u,l,c)→(u-1, l+k, c)forsomek 1
10 October 2006 Foundations of Logic and Constraint Programming 20
Algorithm MM is correct
2. MM replaces the set of pairs by an equivalents set
a) f(s1,s2, .. , sn) ≈ f(t1,t2, .. , tn)→replace,inE,bys1 ≈ t1,s1≈ t1,...,sn ≈ tn
c) x ≈ x→deletethepair
d) t ≈ x wheretisnotavariable→replacebyx ≈ t
• Thisisclearlytrueforactionsa),c)andd)above.Foractione)
e)whenx ≈ t wherex Var(t) andxoccursinsomeotherpair→performsubstitution{x/t } inthosepairs
consider E {x ≈ t } and E{ x/t } {x ≈ t } then
θ is a unifier of E {x ≈ t } iff (θ is a unifier of E) and xθ = tθ iff (θ is a unifier of E {x/t} ) and xθ = tθ iff θ is a unifier of E {x/t} {x ≈ t }
10 October 2006 Foundations of Logic and Constraint Programming 21
Algorithm MM Result
3. If MM terminates with failure, then the set of pairs at the moment of failure has no unifiers
• InMMterminateswithfailure,thelastactiontakeninE’waseitherb)ord).
b) f(s1,s2, .. , sn) ≈ g(t1,t2, .. , tn)wheref g
→haltwithfailure(clash)
f) x ≈ t wherex Var(t)andx t→haltwithfailure(occurs check)
• Giventhedefinitionofunification,theremustbenounifierofE’.
Any substitution θ applied to f(s1,s2, .. , sn) θ would result in a term withfunctorf,notg,sonounifierθexists.
Nosubstitutionθmaycontainapair x/t wherex Var(t)andx t,aswouldbeneededtounifyxandt.
10 October 2006 Foundations of Logic and Constraint Programming 22
Algorithm MM Result
4. If the algorithm terminates successfully, the final set of pairs is solved
• WhenalgorithmMMterminates,withsuccess,noleftcomponentsofthepairsinE’arefunctions(otherwise,actionsa)orb)ord)wouldapply).
a) f(s1,s2, .. , sn) ≈ f(t1,t2, .. , tn)→replace,inE,bys1 ≈ t1,s1≈ t1,...,sn ≈ tnd) t ≈ x wheretisnotavariable→replacebyx ≈ t
• AllpairsinE’areintheformx ≈ t,wherexisavariable.Furthermoret x(otherwiseactionc)wouldapply)
c) x ≈ x→deletethepair
• Nopairotherthanx ≈ tcontainsvariablex,otherwiseactione)wouldapply
e) x ≈ t wherex Var(t) andxoccursinsomeotherpair→performsubstitution{x / t } inthosepairs
• Hence,E’hastheformE’ = {x1 ≈ t1, x2 ≈ t2, ... xn ≈ tn },wherexi Var(tj), for 1 i j n, andisthusinsolvedform.
10 October 2006 Foundations of Logic and Constraint Programming 23
Examples
Somenon-trivialexamples(fromSICStusProlog,wherevariablesarewritteninuppercase)
| ?- f(X,b) = f(a,Y).
X = a,Y = b ? ; no
|?- f(X,f(b)) = f(g(a,Y),Y).
X = g(a,f(b)), Y = f(b) ? ; no
| ?- f(X,f(b,Z)) = f(g(a,Y),Y).
X = g(a,f(b,Z)), Y = f(b,Z) ? ; no
| ?- X = f(Y), Y = f(a).
X = f(f(a)), Y = f(a) ? ; no
| ?- X = f(Y,Z), g(a,Y) = g(Z,b).
X = f(b,a),Y = b,Z = a ? ; no
10 October 2006 Foundations of Logic and Constraint Programming 24
Unifiers Size Can Be Exponential
Example:
| ?- f(X1) = f(g(X0,X0)).X1 = g(X0,X0) ? ; no
| ?- f(X1,X2) = f(g(X0,X0),g(X1,X1)).X1 = g(X0,X0),X2 = g(g(X0,X0),g(X0,X0)) ? ; no
| ?- f(X1,X2,X3) = f(g(X0,X0),g(X1,X1),g(X2,X2)).X1 = g(X0,X0),X2 = g(g(X0,X0),g(X0,X0)),X3 = g(g(g(X0,X0),g(X0,X0)),g(g(X0,X0),g(X0,X0))) ?
; no. ...
Inthiscase,the“size”ofvariableXi,intermsofoccurrencesofX0,is:
size(X1)=2size(X2)=22=4size(X3)=23=8...size(Xn)=2n
10 October 2006 Foundations of Logic and Constraint Programming 25
Strong MGUs
Definition
SubstitutionθisIdempotent
:θθ=θ :Mguθoftermssandtisstrong
:σismguofsandtσ=θσ
Theorem 2.16
Anmguisstrongiffitisidempotent.
Proof :
Letθbeastrongmgu.Sinceθisastrongmgu,foranyunifierσitisσ=θσ.Inparticular,forunifierθ,wehaveθ=θθ.Henceθisidempotent.
Letθbeanidempotentmgu.Then,foranyunifierσthereissomeλsuchthatσ=θλ.Butsinceθisidempotentitisσ=θλ=(θθ)λ=θ(θλ)=θσ.Hence,θisstrong.
10 October 2006 Foundations of Logic and Constraint Programming 26
Strong MGUs
Examples
Themguθ={x/y,y/x}oftwoidenticaltermsisnotstrong.Indeed,θθ={x/y,y/x}°{x/y,y/x}={x/x,y/y,x/y,y/x}=ε
Substitutionθ1={x/u,z/f(u,v),y/v}isIdempotent{x/u,z/f(u,v),y/v}°{x/u,z/f(u,v),y/v}=
{x/u,z/f(u,v),y/v,x/u,z/f(u,v),y/v}={x/u,z/f(u,v),y/v}=θ1
Substitutionθ2={x/u,z/f(u,v),v/y}isnotIdempotent{x/u,z/f(u,v),v/y}°{x/u,z/f(u,v),v/y}=
{x/u,z/f(u,y),v/y,x/u,z/f(u,v),y/v}={x/u,z/f(u,y),y/v}θ2.
Thefollowinglemmajustifiesasimpletesttocheckwhetherasubstitutionmguisidempotent.
Lemma:AsubstitutionθisidempotentiffDom(θ)Ran(θ)1=
10 October 2006 Foundations of Logic and Constraint Programming 27
Relevant MGUs
Definition Mguθoftermssandtisrelevant
:Var(θ)Var(s)Var(t)
Informaly,arelevantmgudoesnotintroducenewvariables.AllmgusproducedbytheMartelli-Montanarialgorithmarerelevant.Moreover
Theorem 2.22 (Relevance)
Everyidempotentmguisrelevant.
Proof : in [Apt, 1997]
Notethattheconverseisnottrue,i.e.Notallrelevantmgusareidempotent.Forexampleθ={x/y,y/x},whichisarelevantmguoftermss=f(x,y)andt=f(x,y),isnotidempotent.
Asobservedbefore,θθ=ε,andε is themgubtainedfromtheMMalgorithmwhenpresentedwithE={f(x,y)≈f(x,y)}.
10 October 2006 Foundations of Logic and Constraint Programming 28
Properties of MGUs
Thefollowingtheoremscanalsobeprovedandareusefullater(see[Apt97])
Lemma 2.23 (Equivalence):
Letθ1beanmguofasetofequationsE.Thenforeverysubstitutionθ2,θ2isanmguofEiffθ2=θ1ρ,whereρisarenamingsuchthat
Var(ρ)Var(θ1)Var(θ2).
Lemma 2.24 (Iteration) :
LetE1andE2betwosetsofequations.Supposethatθ1isanmguofE1,andθ2is anmguofE2 θ1. Thenθ1θ2 is anmguofE1E2.Moreover, ifE1E2 isunifiable,thenanmguθ1ofE1 existsandforanymguθ1ofE1therealsoexistsanmguθ2ofE2 θ1.
Corollary 2.25 (Switching) :
LetE1andE2betwosetsofequations.Supposethatθ1isanmguofE1,andθ2isanmguofE2θ1.ThenE2isunifiable,andforeverymguθ1’ofE2 thereexistsanmguθ2’ofE2 θ1’such thatθ1’θ2 ’=θ1θ2 .Moreover,θ2’canbesochosensuchthatVar(θ2’)Var(E1)Var(θ1’)Var(θ1θ2).
10 October 2006 Foundations of Logic and Constraint Programming 29
Occurs Check in Prolog
Theoccurs checkisusuallynotimplemented(bydefault)inProlog,since Itmakesunificationsignificantlyharder Ithappensquiterarely
Ofcourse,unificationbehavesstrangelywhentheoccurscheckisneeded.Forexample,inSICStus,wemayobserve
| ?- X = f(X).X = f(f(f(f(f(f(f(f(f(f(...)))))))))) ? ; no
Evenworse
| ?- X = f(X).X = f(f(f(f(f(f(f(f(f(f(f(... % LOOP <CTRL-C>
Toavoid this incorrectbeheviour,mostPrologsystemsprovidethepossibilityofusingcorrect,ifinnefficientunification.
10 October 2006 Foundations of Logic and Constraint Programming 30
Occurs Check in Prolog
InSICStus,theexplicitunificationbetweentwotermsSandTcanbeexpressedeitheras
S = T,thedefaultmode,incorrectimplementationofunification;or unify_with_occurs_check(S,T),correctimplementation,inneficient.
Now,itis
| ?- unify_with_occurs_check(X,f(X)).no
| ?- unify_with_occurs_check(X,f(Y)).X = f(Y) ? ; no
It isup to theuser toselect theappropriate form.Forexample, the testoranemptydifference listLcouldbesimplyexpressedasT-T.But if the list isnotempty,andcontaintsnelements,thenittakestheform
[a1,a2,...,an|T]-T
in which case the default unification (without occurs check) would not workproperly,sinceitwouldtrytounifyTwith[a1,a2,...,an|T].
10 October 2006 Foundations of Logic and Constraint Programming 31
Occurs Check in Prolog
This behaviour can be observed with the rev_diff/2 predicate to reverse adifferencelist
rev_diff(L-L,L-L). % incorrect versionrev_diff([H|T]-Z,X-W):- % according to diff_cat Z \== [H|T], % C=X, D=W if Y = [H|D] rev_diff(T-Z,X-[H|W]).
leadingtothefollowinginteraction
| ?- rev_diff0([1,2|T]-T,L).L = [1,2,1,2,1,2,1,2,1|...]-[1,2,1,2,1,2,1,2,1|...],T = [1,2,1,2,1,2,1,2,1,2|...] ? ;L = [2,1|_A]-_A,T = [2,1|_A] ? ;no
Ifthefirstclauseiswritteninsteadas
rev_diff(L-T,L-L):- unify_with_occurs_check(L,T).
thenthecorrectbehaviourisobserved
| ?- rev_diff([1,2|T]-T,L). L = [2,1|_A]-_A,T = [2,1|_A] ? ;no