0 Prolog Unification

Embed Size (px)

Citation preview

  • 7/24/2019 0 Prolog Unification

    1/4

    1

    Introduction to Prolog. Unification. Sicstus Prolog

    In this introductory session you will get acquainted with the Prolog data types and

    unification rules.

    0.1 Prolog Data Types

    Prolog's single data type is the term. Terms are either: atoms, numbers, variables

    or compound terms (structures). The figure below presents a classification of the data

    types in Prolog:

    Figure 1 Prolog data types

    0.1.1 Atoms and numbers

    An atom is a general-purpose name with no inherent meaning. It is composed ofa sequence of characters that is parsed by the Prolog reader as a single unit. Atoms are

    usually bare words in Prolog code, written with no special syntax. However, atoms

    containing spaces or certain other special characters must be surrounded by single

    quotes. Atoms beginning with a capital letter must also be quoted, to distinguish them

    from variables. The empty list, written [], is also an atom. Other examples of atoms

    include:

    x, blue, 'Taco', and 'some atom'.

    Numbers can be integers or real numbers (not used very much in typical Prolog

    programming).

    0.1.2 Variables

    Variables are denoted by a string consisting of letters, numbers and underscore

    characters, and beginning with an upper-case letter or underscore. Variables closely

    1Ivan Bratko Prolog Programming for Artificial Intelligence

  • 7/24/2019 0 Prolog Unification

    2/4

    2

    resemble variables in logic in that they are placeholders for arbitrary terms. A variable

    can become instantiated (bound to equal a specific term) via unification. A single

    underscore (_) denotes an anonymous variable and means "any term". Unlike other

    variables, the underscore does not represent the same value everywhere it occurs within

    a predicate definition.

    0.1.3 Compound Terms

    A compound term is composed of an atom called a "functor" and a number of

    "arguments", which are again terms. Compound terms are ordinarily written as a functor

    followed by a comma-separated list of argument terms, which is contained in

    parentheses. The number of arguments is called the term's arity. An atom can be

    regarded as a compound term with arity zero.

    Examples of compound terms are: truck_year('Mazda', 1986) and

    'Person_Friends'(zelda,[tom,jim]). Users can declare arbitrary functors as operators with

    different precedence to allow for domain-specific notations. The notation f/n is commonlyused to denote a term with functor f and arity n.

    Special cases of compound terms:

    Listsare defined inductively. The list [1, 2, 3] would be represented internally as

    '.'(1, '.'(2, '.'(3, []))). A syntactic shortcut is [H | T], which is mostly used to construct

    rules. A list can be processed by processing the first element, and then the rest of

    the list, in a recursive manner.

    Lists can be constructed and deconstructed in a variety of ways:

    o Element enumeration: [abc, 1, f(x), Y, g(A,rst)]

    o Prepending single element: [abc | L1]

    o Prepending multiple elements: [abc, 1, f(x) | L2]

    o Term expansion: '.'(abc, '.'(1, '.'(f(x), '.'(Y, '.'(g(A,rst), [])))))

    Strings

    0.2 Prolog Unification

    Atomsunify if and only if they are the same atom.

    Numbersunify if and only if they are the same number.

    Stringsunify if and only if they are the same string. Listsunify if and only if

    1. their heads unify, and

    2. their tails unify.

    Structuresunify if and only if

    1. their names unify,

    2. they have the same number of arguments, and

    3. their arguments unify.

  • 7/24/2019 0 Prolog Unification

    3/4

    3

    Variable V unifies with Term T just in case one of the following conditions is

    satisfied:

    o Vis an instantiated variable.

    If T is not a variable, then V and T unify if and only if the term

    instantiated on Vunifies with T.

    If Tis an instantiated variable, then Vand Tunify if and only if the

    term instantiated on Vunifies with the term instantiated on T.

    If T is an uninstantiated variable, then V and T are unified by

    instantiating on Tthe term that is instantiated on V.

    o Vis an uninstantiated variable.

    If Tis not a variable, then Vand Tare unified by instantiating Ton

    V.

    If T is an instantiated variable, then V and T are unified by

    instantiating on Vthe term that is instantiated on T.

    If T is an uninstantiated variable, then Vand Tunify and become

    synonyms for the same variable.

    Exercise 1.1: Check Sicstus Prolog manual for:

    a. Terms (4.1.2)

    b. Compound Terms (4.1.3)

    c. Unification (4.8.1.2)

  • 7/24/2019 0 Prolog Unification

    4/4