CPSC 3520 - DAY 6 JANUARY 30, 2018 ================================================================================ PROLOG ------ Predicate - A predicate consists of a name and an optional set of arguments. The number of arguments is the arity of the predicate. Clause - Clauses are either rules or facts. Clauses are built using predicates and logical connectives and allowed to contain variables which are assumed universally qualified. -A prolog clause is comprised of a head and an optional body or tail. -If the body is empty, the clause is a fact, which is interpreted to be true. Facts - a fact is a rule with an empty tail. Although this is acceptable notation, in practice facts have the typical form: predicate_name(term1, term2, ..., termn). where the . terminates the clause. The following is an example of a database of Prolog facts: is_fact (arg1, arg2). a_fact_too (Y). equal (X, X). another_fact. wheel_is_round. round (wheel). wheel (round). Rules - a clause with a non-empty head and a tail. Rules have a typical form (logical representation) predicate_name1(term1, term2, ...) :- predicate_name2(term1, term2, ...), Conjunction and Disjunction - The use of comma (',') in prolog to seperate clauses indicates that the conjuction of these clauses (subgoals) must be satisfied: a, b force both subgoals to be satisfied. goal :- a;b. ^ this can be rewritten as the locically equivalent pair of clauses: goal :- a. goal :- b. Variables - All variables in Prolog begin with a capital letter. The name of a predicate may not be a variable. The anonymous variable - is used to represent universal qualification; (when any instantiation of the variable will suffice). It is not necessary to give this variable a name, instead the underscore or '_' is used. Goal - A prolog program is incomplete without a specification of a goal. A goal entered at the Prolog interpreter prompt (?-), is a Prolog clause which may be comprised of subgoals, logical connectives and contain variables. The Prolog system attempts to unify this goal with the database. An example of a simple goal used to query the Prolog database might be: ?-has(What, door). Unification = Solution = Search An understanding of the operation of the Prolog unification mechanism is fundamental to success in developing practical Prolog programs. The rules for unification are: 1. Clauses are tested in the order in which they appear in the database. 2. When a subgoal matches the left side (head) of a rule, the right side (tail) becomes the new set of subgoals to unify. 3. The unifier proceeds from left to right in attempting to unify the predicates in the tail. When a subgoal is spawned, the unification/ search process described in 1. repeats. 4. A goal is satisfied when a matching fact is found in the database for all leaves of the goal tree. 5. When two or more clauses in the database with the same predicate name are identified as possible matches, the first clause in the database is tested for attempted unification. The rest are marked as point for possible backtracking. Running a program in Prolog: ?-consult('hello.pro'). ?-listing(go). ?-go. ***ALSO ACCEPTABLE*** ?-['hello.pro'] factorial description in Prolog factorial(1,1). factorial(N, Result) :- Imin1 is N-1, factorial(Imin1, Fmin1), Result is N*Fmin1.