prolog substitutions unification

Upload: kanibalo

Post on 02-Jun-2018

249 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Prolog Substitutions Unification

    1/19

    PROLOG.

    Substitutions and Unification

    Antoni Ligeza

    Katedra Automatyki, AGH w Krakowie

    2011

    Antoni Ligeza Prolog 1/1

  • 8/10/2019 Prolog Substitutions Unification

    2/19

    References

    [1] Ulf Nilsson, Jan Mauszynski:Logic, Programming and Prolog, John Wiley &

    Sons Ltd., pdf,http://www.ida.liu.se/ ulfni/lpp

    [2] Dennis Merritt: Adventure in Prolog, Amzi, 2004

    http://www.amzi.com/AdventureInProlog

    [3] Quick Prolog:

    http://www.dai.ed.ac.uk/groups/ssp/bookpages/quickprolog/quickprolog.html

    [4] W. F. Clocksin, C. S. Mellish: Prolog. Programowanie. Helion, 2003

    [5] SWI-Prologs home:http://www.swi-prolog.org

    [6] Learn Prolog Now!: http://www.learnprolognow.org

    [7] http://home.agh.edu.pl/ ligeza/wiki/prolog

    [8] http://www.im.pwr.wroc.pl/ przemko/prolog

    Antoni Ligeza Prolog 2/1

  • 8/10/2019 Prolog Substitutions Unification

    3/19

    Substitutions

    The role of substitutions

    Substitutionis an operation allowing to replace some variables occurring in

    a formula with terms.

    The goal of applying a substitution is to make a certain formula more specific so

    that it matches another formula. Substitutions allow forunificationof formulae

    (or terms).

    Definition

    A substitution is any finite mapping of variables into terms of the form

    : VTER.

    Notation of substitutions

    Any (finite) substitution can be presented as

    ={X1/t1,X2/t2, . . . ,Xn/tn},

    whereti is a term to be substituted for variable Xi ,i = 1, 2, . . . , n.

    is the formula (or term) resulting fromsimultaneousreplacement of thevariables of with the appropriate terms of.

    Antoni Ligeza Prolog 3/1

  • 8/10/2019 Prolog Substitutions Unification

    4/19

    Extending substitutions over Terms and Formulae

    Any substitution ( : V TER) is extended to operate on terms and formulae sothat a finite mapping of the form

    : TER FOR TER FOR

    satisfying the following conditions is induced:

    (c) =cfor anyc C;

    (X

    )TER, and

    (X

    )=Xfor a certain finite number of variables only;

    iff(t1, t2, . . . , tn) TER, then

    (f(t1, t2, . . . , tn)) =f((t1), (t2), . . . , (tn));

    ifp(t1, t2, . . . , tn) ATOM, then

    (p(t1, t2, . . . , tn)) =p((t1), (t2), . . . , (tn));

    ( ) =() ()for any two formulae, FORand for {, , , };

    Antoni Ligeza Prolog 4/1

  • 8/10/2019 Prolog Substitutions Unification

    5/19

    Instances, Ground Terms, Ground Formulae

    An Instance

    Any formula()resulting from application of substitution to the variables ofwill be denoted as

    and it will be called asubstitution instanceor simply aninstanceof.

    A Ground Instance, Term FormulaIf no variables occur in, it will be called aground instance(aground formulaor aground term, respectively).

    Example

    Let ={X/a, Y/f(b)}, and let =p(X, Y, g(X)). Then

    =p(a,f(b), g(a))

    and it is a ground formula.

    Antoni Ligeza Prolog 5/1

  • 8/10/2019 Prolog Substitutions Unification

    6/19

    Composition of Substitutions

    Since substitutions are mappings, acompositionof substitutions is well defined.

    Having two substitutions, say and, the composed substitutioncan be obtained

    from by: simultaneousapplication of to all the terms of,

    deletion of any pairs of the formX/twheret=X(identity substitutions), and

    enclosing all the pairsX/tof, such that does not substitute for (operate on)X.

    More formally:

    Let ={X1/t1,X2/t2, . . . ,Xn/tn} and let ={Y1/s1, Y2/s2, . . . , Ym/sm}. Thecomposition of the above substitutions is obtained from the set

    {X1/t1,X2/t2 , . . . ,Xn/tn, Y1/s1, Y2/s2, . . . , Ym/sm}

    by:

    removing all the pairsXi/tiwhereXi =ti, and

    removing all the pairsYj/sj whereYj {X1,X2, . . . ,Xn}.

    Antoni Ligeza Prolog 6/1

  • 8/10/2019 Prolog Substitutions Unification

    7/19

    An Example

    Example

    Consider the following substitutions:

    ={X/g(U), Y/f(Z), V/W,Z/c}

    and

    ={Z/f(U), W/V, U/b}.

    The composition of them is defined as: ???

    Antoni Ligeza Prolog 7/1

    A E l

  • 8/10/2019 Prolog Substitutions Unification

    8/19

    An Example

    Example

    Consider the following substitutions:

    ={X/g(U), Y/f(Z), V/W,Z/c}

    and={Z/f(U), W/V, U/b}.

    The composition of them is defined as:

    ={X/g(b), Y/f(f(U)),Z/c, W/V, U/b}.

    Antoni Ligeza Prolog 8/1

    R i d I S b tit ti

  • 8/10/2019 Prolog Substitutions Unification

    9/19

    Renaming and Inverse Substitutions

    A Renaming Substitution

    Substitution

    is arenaming substitutioniff it is off the form

    ={X1/Y1,X2/Y2, . . . ,Xn/Yn} (1)

    Moreover, it is a one-to-one mapping ifYi =Yj fori =j,i,j {1, 2, . . . , n}.

    An Inverse SubstitutionAssumeis a renaming, one-to-one substitution . The inverse substitution for it isgiven by

    1 ={Y1/X1, Y2/X2, . . . , Yn/Xn, }.

    Composition of inverse substitutions

    The composition of a renaming substitution and the inverse one leads to an empty

    substitution, traditionally denoted with; we have

    1 =.

    Antoni Ligeza Prolog 9/1

    S ti

  • 8/10/2019 Prolog Substitutions Unification

    10/19

    Some properties

    An Instance

    LetEdenote an expression (formula or term), denote an empty substitution, and letbe a one-to-one renaming substitution; and denote any substitutions.The following properties are satisfied for any substitutions:

    E() = (E), () = ()(associativity),

    E= E,

    = = .

    Note that, in general, the composition of substitutions is not commutative.

    Antoni Ligeza Prolog 10/1

    Unification

  • 8/10/2019 Prolog Substitutions Unification

    11/19

    Unification

    Substitutions are applied tounifyterms and formulae. Unification is a process of

    determining and applying a certain substitution to a set of expressions (terms or

    formulae) in order to make them identical. We have the following definition of

    unification.

    Definition

    LetE1,E2, . . . ,En TER FORare certain expressions. We shall say thatexpressionsE1,E2, . . . ,En areunifiableif and only if there exists a substitution,such that

    {E1,E2, . . . ,En} ={E1,E2 , . . . ,En}

    is a single-element set.

    Substitution satisfying the above condition is called a unifier(or aunifyingsubstitution) for expressionsE1,E2, . . . ,En.

    Note that if there exists a unifying substitution for some two or more expressions

    (terms or formulae), then there usually exists more than one such substitution (or

    even infinitely many unifiers).

    Antoni Ligeza Prolog 11/1

    The Most General Unifier (mgu)

  • 8/10/2019 Prolog Substitutions Unification

    12/19

    The Most General Unifier (mgu)

    It is useful to define the so-called most general unifier(mgu, for short), which,

    roughly speaking, substitutes terms for variables only if it is necessary, leaving as

    much place for possible further substitutions, as possible.

    Definition

    A substitution is amost general unifierfor a certain set of expressions if and onlyif, for any other unifier of this set of expressions, there exists a substitution , suchthat =.

    The meaning of the above definition is obvious. Substitution is not a most generalunifier, since it is a composition of some simpler substitution with an auxiliarysubstitution.In general, for arbitrary expressions there may exist an infinite number of unifying

    substitutions. However, it can be proved that any two most general unifiers can differ

    only with respect to variable names. This is stated with the following theorem.

    A Theorem

    Let1and2 be two most general unifiers for a certain set of expressions. Then, thereexists a one-to-one renaming substitution such that1 =2and 2 =1

    1.

    Antoni Ligeza Prolog 12/1

    An example

  • 8/10/2019 Prolog Substitutions Unification

    13/19

    An example

    Example

    As an example consider atomic formulaep(X,f(Y))and p(Z,f(Z)). The followingsubstitutions are all most general unifiers:

    ={X/U, Y/U,Z/U},

    1 ={Z/X, Y/X},

    2 ={X/Y,Z/Y},

    3 ={X/Z, Y/Z}.

    All of the above unifiers are equivalent each of them can be obtained from another

    one by applying a renaming substitution. For example, =1for = {X/U}; on

    the other hand obviously1 =1

    .

    Antoni Ligeza Prolog 13/1

    Unification Algorithm An Idea

  • 8/10/2019 Prolog Substitutions Unification

    14/19

    Unification Algorithm An Idea

    It can be proved that if the analyzed expressions are terms or formulae, then

    there exists an algorithm for efficient generating the most general unifier,provided that there exists one; in the other case the algorithm terminates after

    finite number of steps . Hence, the unification problem is decidable.

    The basic idea of the unification algorithm can be explained as a subsequent

    search through the structure of the expressions to be unified for inconsistent

    relative components and replacing one of them, hopefully being a variable, with

    the other.

    In order to find inconsistent components it is useful to define the so-called

    disagreementset.

    LetWTER FORbe a set of expressions to be unified. A disagreement setD(W)for a nonempty set Wis the set of terms obtained through parallel search

    of all the expressions ofW(from left to right), which are different with respectto the first symbol. Hence, the set D(W)specifies all the inconsistent relativeelements met first during the search.

    Antoni Ligeza Prolog 14/1

    Unification Algorithm

  • 8/10/2019 Prolog Substitutions Unification

    15/19

    Unification Algorithm

    Algorithm for Unification

    1 Seti = 0,Wi =W,i =.

    2 IfWi is a singleton, then stop;i is the most general unifier forW.

    3 FindD(Wi).

    4 If there are a variableXD(Wi)and a term t D(Wi), such thatXdoes notoccur int, then proceed; otherwise stop Wis not unifiable.

    5 Seti+1 ={X/t},Wi+1 =Wi{X/t}.

    6 Seti = i+1 and go to 2.

    Antoni Ligeza Prolog 15/1

    Application Example

  • 8/10/2019 Prolog Substitutions Unification

    16/19

    Application Example

    Consider two atomic formulae p(X,f(X, Y), g(f(Y,X)))and p(c,Z, g(Z)). The

    following steps illustrate the application of the unification algorithm to these atomicformulae.

    1 i= 0,W0 ={p(X,f(X, Y), g(f(Y,X))),p(c,Z, g(Z))},0 ={}.

    2 D(W0) ={X, c}.

    3 1 ={X/c},W1 ={p(c,f(c, Y), g(f(Y, c))),p(c,Z, g(Z))}.

    4 D(W1) ={f(c, Y),Z}.

    5 2 ={X/c}{Z/f(c, Y)}= {X/c,Z/f(c, Y)},W2 ={p(c,f(c, Y), g(f(Y, c))),p(c,f(c, Y), g(f(c, Y)))}.

    6 D(W2) ={Y, c}.

    7 3 ={X/c,Z/f(c, Y)}{Y/c}= {X/c,Z/f(c, c), Y/c},

    W3 ={p(c,f(c, c), g(f(c, c))),p(c,f(c, c), g(f(c, c)))}.

    8 Stop; the most general unifier is 3 ={X/c,Z/f(c, c), Y/c}.

    Antoni Ligeza Prolog 16/1

    Properties of the Unification Algorithm

  • 8/10/2019 Prolog Substitutions Unification

    17/19

    Properties of the Unification Algorithm

    Theorem

    1 IfWis a finite set of unifiable expressions, then

    1 the Unification Algorithmalways terminatesat step 2 and

    2 itproduces the most general unifierfor W.

    2 Moreover, if the expressions ofWare not unifiable, then the algorithm

    terminates at step 4.

    Antoni Ligeza Prolog 17/1

    Unification in Prolog

  • 8/10/2019 Prolog Substitutions Unification

    18/19

    Unification in Prolog

    From: R. Bartak:

    http://kti.mff.cuni.cz/~bartak/prolog/data_struct.html

    Unification Algorithm defined in Prolog

    1 unify(A,B):-

    2 atomic(A),atomic(B),A=B.

    3 unify(A,B):-

    4 var(A),A=B. % without occurs check

    5 unify(A,B):-6 nonvar(A),var(B),A=B. % without occurs check

    7 unify(A,B):-

    8 compound(A),compound(B),

    9 A=..[F|ArgsA],B=..[F|ArgsB],

    10 unify_args(ArgsA,ArgsB).

    11

    12 unify_args([A|TA],[B|TB]):-13 unify(A,B),

    14 unify_args(TA,TB).

    15 unify_args([],[]).

    Antoni Ligeza Prolog 18/1

    A final problem

    http://kti.mff.cuni.cz/~bartak/prolog/data_struct.htmlhttp://kti.mff.cuni.cz/~bartak/prolog/data_struct.html
  • 8/10/2019 Prolog Substitutions Unification

    19/19

    p

    Question

    AreXandf(X)unifiable?

    Example

    What is/should be the result of:

    ?- X=f(X).

    ?- X=a, X=f(X).

    ?- X=f(X), write(X).

    Antoni Ligeza Prolog 19/1