Page 214 - DCAP506_ARTIFICIAL_INTELLIGENCE
P. 214

Artificial Intelligence




                    Notes
                                     Did u know?  The similar variable has to be instantiated with the similar value all through
                                     an expression. The only  exception to this rule is the  unidentified variable  _, which is
                                     measured to be unique whenever it appears.


                                          Example:  We  provide  some  examples.  The  terms  is_bigger(X,  dog)  and
                                   is_bigger(elephant, dog) match, since the variable X can be instantiated with the atom elephant.
                                   We could check this in the  Prolog interpreter  by submitting the equivalent query to which
                                   Prolog would respond by listing the suitable variable instantiations:
                                   ?- is_bigger(X, dog) = is_bigger(elephant, dog).

                                   X = elephant
                                   Yes

                                   The following is an example for a query that doesn’t succeed, because X cannot match with 1 and
                                   2 at the same time.

                                   ?- p(X, 2, 2) = p(1, Y, X).
                                   No
                                   If, however, rather than X we use the anonymous variable _, matching is possible, since every
                                   occurrence of _ represents a different variable. All through matching Y is instantiated with 2:
                                   ?- p(_, 2, 2) = p(1, Y, _).

                                   Y = 2
                                   Yes

                                   Another example for matching:
                                   ?- f(a, g(X, Y)) = f(X, Z), Z = g(W, h(X)).
                                   X = a

                                   Y = h(a)
                                   Z = g(a, h(a))

                                   W = a
                                   Yes

                                   Until now so good but what happens, if matching is probable even though no specific variable
                                   instantiation has to be imposed. Consider the following query:

                                   ?- X = my_functor(Y).
                                   X = my_functor(_G177)

                                   Y = _G177
                                   Yes
                                   In this  example matching succeeds, since  X could  be a  compound term  with the  function
                                   my_function and a non-specified single argument. Y could be any valid Prolog term, but it has




          208                               LOVELY PROFESSIONAL UNIVERSITY
   209   210   211   212   213   214   215   216   217   218   219