
6.3 The Fundamental Questions 6.3.1 Name Binding and Scope Names in Prolog are used to represent predicates and terms. Predicates are used to express a relationship between terms. For example, the statement below could represent the relationship that Jay is the father of Baron. (It could just as well represent the relationship that Baron is the father of Jay since the father predicate has no intrinsic meaning in Prolog.) This is the simplest type of statement and is known as a fact. The name father is the predicate used in this fact. father(jay, baron). Predicates are also used in rules, which define new relationships in terms of existing relationships. For example, the statement below defines the sibling relationship: X and Y are siblings if they have the same father and X is not equal to Y. The predicates in this example are sibling , father, \+ (not) and = (equal). The left side of the :- (pronounced “if”) operator is called the head of the rule; the right side is called the body. The comma separated terms on the right side are called the conditions of the rule. sibling(X, Y) :- father(Z, X), father(Z, Y), \+(X = Y). The Prolog term is the single data structure in the language. Terms are either atoms, numbers, variables or compound terms. An atom is a name with no intrinsic meaning that is used to represent a constant value in the domain. An atom is a sequence of alphanumeric characters beginning with a lower case letter or a sequence of characters enclosed in single quotes. Examples of atoms include: jay ‘Jay Fenwick’ x ‘some $’ Notice that enclosing a sequence of characters in single quotes allows us to have constants in our domain that start with an upper case letter and/or include non-alphanumeric characters. Numbers can also be the terms of predicates. For example, a multiplication table can be expressed with a series of statements of the following form: multiply(1, 1, 1). multiply(1, 2, 2). multiply(1, 3, 3). Variables are represented by a sequence of letters, numbers and underscores, starting with either an upper case letter or an underscore. A single underscore is a special type of variable, known as the anonymous variable. As seen in the previous section, variables are used in rules or queries and are instantiated during the inferencing process except for the anonymous variable that is used in the pattern matching, but not instantiated. A variable can be instantiated to any term, including atoms, numbers, compound terms and other variables. Example of variables include: X Thefather _father _ A compound term is either a list or a functor term. A Prolog list consists of elements separated by commas enclosed in square brackets. Examples of Prolog lists can be seen below; the first list is the empty list or nil. [] [a, b, c] [a, [b]] [1, a, [‘hello’]] A string, enclosed in double quotes rather than single quotes, is actually equivalent to a list of the encoded value of the characters within the string. This can be seen by the example below. The = operator attempts to unify the term on the left with the term on the right and does so by instantiating X, Y, Z to the ascii codes for a, b, and c, respectively. | ?- "abc" = [X, Y, Z]. X = 97 Y = 98 Z = 99 yes A functor term is composed of an atom, known as a functor, and a number of argument terms enclosed within parenthesis. A functor term looks like a function call, however, no transfer of control occurs; rather a functor term, like an atom, is used to represent some value in the domain. Consider the example below. The first three facts represent father-son relationships between the first argument of the predicate and the second. fatherof(‘Baron’) does not evaluate to another value; it is simply a value in the domain, much like ‘Baron’, ‘Grayson’ and ‘Ethan’ are values in the domain. Note that the single quotes are used with ‘Baron’, ‘Grayson’, and ‘Ethan’ to prevent these atoms from being treated as variables. The rule can be used to identify sibling relationships, for example, determining that ‘Baron’ and ‘Grayson’ are siblings. father('Baron', fatherof('Baron')). father('Grayson', fatherof('Baron')). father('Ethan', fatherof('Baron')). sibling(X, Y) :- father(X, Z), father(Y, Z), \+(X = Y). A procedure in Prolog is a set of statements whose head begins with the same predicate. For example, the set of statements below is a Prolog procedure. myappend([], L, L). myappend([H|T], L, [H|R]) :- myappend(T, L, R). Prolog performs static scoping and the scope of a Prolog variable is the statement in which the variable appears. For example, the scope of the variable L in the first myappend statement is only that myappend statement. The variable L in the second statement is unrelated to the variable L in the first statement. (If you want to relate this to the block scoping of imperative languages, each statement would be equivalent to a block.) As noted earlier, each call to the procedure will result in a set of variables allocated for that procedure call. The second myappend rule may be used multiple times in the attempt to solve a myappend query. Each use of the rule will result in the creation of space for the rule variables. Thus, the instantiation of L in one use of the rule will be different than the instantiation of L in a second use of the rule. Prolog is dynamically typed, which means that the type errors are not detected until runtime. If a function is applied to a term with an invalid type, then Prolog will report the error when the function is applied. Unlike most languages, Prolog does not detect the use of a predicate with the wrong number of parameters as a type error. Instead, Prolog searches for a predicate with the given arity and reports that a predicate with that arity is undefined. Current implementations of Prolog compilers typically target the Warren Abstract Machine (WAM). Similar to the implementation of imperative languages, the WAM uses one or more stacks to store variables and control information, a heap area for data values and an area called the trail to hold information for restoring values when backtracking occurs. These data areas grow while the resolution process creates more subgoals to prove and shrink during backtracking. Space is automatically allocated as needed and garbage collection is performed by the interpreter to reclaim space that is no longer needed. 6.3.2 Expressions Prolog statements, or expressions, are the equivalent of Horn clauses. A Horn clause is a clause in one of the following two forms (P1 ^ P2 ^ … ^ Pn) -> Q (R1 ^ R2 ^ … ^ Rn) These would be written in Prolog in the form below. The Q predicate expression is known as a rule. The Ri predicates are called facts. Q :- P1, P2, …, Pn. R1. R2. Rn. Thus the Prolog :- operator is logically equivalent to implication and the comma is equivalent to logical and. Prolog also defines the ; operator to be equivalent to logical or. For example, the expression below is equivalent to Q if P1 or P2 Q :- P1; P2. The statement above is equivalent to two below. Q :- P1. Q :- P2. The , operator has higher precedence than the; operator, thus the statement below: Q :- P1, P2; P3, P4. Is equivalent to: Q :- P1, P2. Q :- P3, P4. Parenthesis can be used to change the precedence. For example, Q :- P1, (P2 ; P3), P4. is logically equivalent to Q :- P1, P2, P4. Q :- P1, P3, P4. Prolog expressions can also consist of arithmetic operators and comparison operators that have the expected precedence (for example, comparison operators have a higher precedence than arithmetic operators, / and * have higher precedence than + and -). In addition, Prolog provides a set of equality and inequality operators and an is operator that evaluates its right operand and attempts to unify the value with the left operand. The is operator can be used to instantiate variables, somewhat similar to assignment statements in imperative languages. Descriptions and example uses of these operators can be seen in the table below. Operator Description Examples is Evaluates the expression on the | ?- X is 3. right and determines whether it can X = 3 be unified with the expression on yes the left. The expression on the left | ?- 3 is 3. is not evaluated, thus the last yes example evaluates to no. | ?- 3 + 4 is 3 + 4. no =, \= Determines whether the left operand | ?- [X, Y] = [a, b]. and the right operand can or can not X = a be unified Y = b Yes | ?- X = 3. X = 3 yes | ?- X = Y. Y = X yes ==, \== Determines whether the left and | ?- X == 3. right operands evaluate to the same no values. In the first example, X is | ?- 3 * 4 == 3 * 4. unbound and thus has no value. yes | ?- X is 3, X == 3. X = 3 yes <, >, Comparison operators. Notice the | ?- 5 >= 4. =<, >= less than or equal to operator is =< yes | ?- X is 4, 3 =< X. X = 4 yes /, * Operators for division and | ?- X is 2 * 3, 10 > X. multiplication X = 6 yes | ?- X is 3, Y is 10/X. X = 3 Y = 3.3333333333333335 yes +, - Operators for addition and | ?- X is 3, Y is 4 - X + subtraction 7. X = 3 Y = 8 yes Prolog also supplies a number of built-in predicates. For example, the atom predicate evaluates to true if its argument is an atom. | ?- atom(hello). yes | ?- atom('hello'). yes | ?- atom("hello"). no | ?- atom(3). no In fact, the operators described earlier are simply predicates that have been invoked using infix, rather than prefix, notation.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages15 Page
-
File Size-