Download presentation
Presentation is loading. Please wait.
1
Section 16.5, 16.6 plus other references
Prolog Language for programming in logic Programmer expresses facts and rules User expresses queries (goals) Prolog inferencing engine tries to prove the goal using the existing facts and rules Section 16.5, plus other references
2
Section 16.5, 16.6 plus other references
The basics Getting into prolog % gprolog /usr/bin/gprolog | ? prolog prompt Loading a prolog file | ?- [file] File should end with a .pl extension Interpreter is now sitting waiting for you to ask questions Exiting prolog – type ctrl-D Comments – put inside of /* …. */ Section 16.5, plus other references
3
Section 16.5, 16.6 plus other references
Facts Simple predicate statement followed by a period Predicates and constants should begin with a lower case letter and can consist of any alphanumeric character or underscore Section 16.5, plus other references
4
Section 16.5, 16.6 plus other references
Examples of facts likes(jim, mary). likes(jim, marshmellows). likes(jim, books). likes(fred, wine). likes(mary, wine). likes(mary, food). owns(jim, car). owns(jim, marshmellows). Section 16.5, plus other references
5
Section 16.5, 16.6 plus other references
Rules head :- body. Head is a single predicate Body can be a conjunction of predicates, separated by commas Rule states that head is true if body is true Section 16.5, plus other references
6
Section 16.5, 16.6 plus other references
Rule examples likes(jim, X) :- likes(X, wine), likes(X, food). X represents a variable that can be instantiated during the inferencing process Variables begin with a capital letter and can consist of any alphanumeric characters or underscore Section 16.5, plus other references
7
Prolog queries (goals)
Looks like a prolog fact, but when entered at the prolog prompt, prolog interprets it as a question Prolog attempts to use the facts and rules to prove the query Prolog uses a closed world assumption – if something can't be proven to be true than it must be false Section 16.5, plus other references
8
Section 16.5, 16.6 plus other references
Query examples | ?- likes(jim, marshmellows). Yes | ?- owns(jim, X). X = car; X = marshmellows; No Section 16.5, plus other references
9
Section 16.5, 16.6 plus other references
Query example | ?- likes(jim, X). X = mary; X = marshmellows; X = books; No Section 16.5, plus other references
10
Section 16.5, 16.6 plus other references
Exciting life problem Express the following as prolog facts and rules All people that are wealthy and are smart are happy. Those people that read are smart. Kate is wealthy. John can read and is wealthy. Cindy can read. Happy people have exciting lives. Section 16.5, plus other references
11
Section 16.5, 16.6 plus other references
Exciting life problem Express the following as a query Can anyone be found with an exciting life? Section 16.5, plus other references
12
Inferencing process of prolog
To prove a goal: Q Either Q matches a fact Inferencing process finds a sequence of rules to prove Q P2 :- P (P1 is true so P2 is true) P3 :- P2 . Q :- Pn (Pn is true so Q is true) Section 16.5, plus other references
13
Section 16.5, 16.6 plus other references
Forward chaining Start with the facts and use rules to find sequence of matching propositions that lead up to the goal P1 P2 P3 . Q Section 16.5, plus other references
14
Section 16.5, 16.6 plus other references
Backward chaining Start with goal and use propositions to get to a fact Q Pn . P2 P1 This is the typical approach in Prolog implementations Section 16.5, plus other references
15
Using exciting life example:
goal: smart(john) | smart(X) (smart(john) matches smart(X)) canread(X) (try canread(X) for X = john; | this matches fact) canread(john)) Section 16.5, plus other references
16
Using exciting life example
Rules like: happy(x) :- wealthy(x), smart(x) Mean that both wealthy(x) and smart(x) need to be solved in order to get happy(x) Section 16.5, plus other references
17
Section 16.5, 16.6 plus other references
Resolution process Depth-first – solves first (leftmost) subgoal before working on other subgoals (wealthy(x) before smart(x)) Backtracking – reconsider a subgoal and try to solve it in another way (with possibly a different instantiation of variables) Section 16.5, plus other references
18
Section 16.5, 16.6 plus other references
List data structure Items separated by commas, enclosed in brackets [a, b, c] [this, and, that] [1, and, 2] [[jay, baron, grayson, ethan], [cindy]] Bar operator can be used to separate list into components [X | Y] /* X is the first element in the list, Y is tail */ [X, Y | Z] /* X is first element, Y is second element and Z is the rest of the list */ Section 16.5, plus other references
19
Section 16.5, 16.6 plus other references
Example Suppose or prolog program consists of p([1, 2, 3]). p([this, that, [and, all, this]]). p([[inside, out], and, more]). Queries - how does prolog respond? | ?- p([X|Y]). | ?- p([X, Y | Z]). Section 16.5, plus other references
20
Section 16.5, 16.6 plus other references
Example continued More queries | ?- p([X, Y, Z | _]). /* _ is an anonymous placeholder */ | ?- p([_, _, [_|X]]). Section 16.5, plus other references
21
A First Prolog function
mylast(X, Y) – evaluates to Yes if X is a list and Y is the last element of the list X /* [X] is a list containing one element (X), thus X is the last */ mylast([X], X). /* [_|Y] is a list containing more than one element. */ mylast([_|Y], Z) :- mylast(Y, Z). Section 16.5, plus other references
22
Section 16.5, 16.6 plus other references
Mymember function Lets write a mymember function that takes 2 arguments, an element and a list, and returns yes if the element is in the list Base case: the element is at the head of the list Recursive case: an element is a member of a list if the element is a member of the tail Example query: mymember(a, [b, a, d]). Section 16.5, plus other references
23
Evenelements function
Takes as input 2 arguments. The first argument is a list containing an even number of elements. The second argument is a list containing the even numbered elements of the first list. (Numbering starts at 1.) Example queries: | ?- evenelements([a, b, c, d], [b, d]). | ?- evenelements([a, b, c, d], X). Section 16.5, plus other references
24
Section 16.5, 16.6 plus other references
Myappend function Takes three arguments, all lists, and returns yes if the third argument is equal to the append of the first argument to the second. Example queries | ?- myappend([1, 2], [3, 4], X). | ?- myappend([1, 2], [3, 4], [1, 2, 3, 4]). | ?- myappend(X, [3, 4], [1, 2, 3, 4]). | ?- myappend(X, Y, [1, 2, 3, 4]). Section 16.5, plus other references
25
Section 16.5, 16.6 plus other references
Myappend function Base cases An empty list appended to a list is that list A list appended to the empty list is that list Need recursive rule that moves closer to base cases Section 16.5, plus other references
26
Section 16.5, 16.6 plus other references
Prefix function Takes 2 arguments and evaluates to yes if the first argument is a prefix of the second Example queries | ?- prefix([a, b], [a, b, c, d]). | ?- prefix(X, [a, b, c]). Section 16.5, plus other references
27
Section 16.5, 16.6 plus other references
Sublist function Takes two arguments and returns yes if the first is a sublist of the second Example queries: | ?- sublist([2, 3, 4], [1, 2, 3, 4, 5]). | ?- sublist(X, [1, 2, 3]). Section 16.5, plus other references
28
Debugging prolog programs
spy(prefix). Every call to prefix is traced Type <ret> to step through calls to the function nospy(prefix). Turns off trace of function prefix Section 16.5, plus other references
29
Some Prolog Predicates
atom(X) – evaluates to Yes if X is bound to an atom | ?- atom(a). Yes | ?- atom([a]). No integer(X) – evaluates to Yes if X is bound to an integer | ?- integer(a). No | ?- integer(3). Yes Similar functions: float, number Section 16.5, plus other references
30
More Prolog Predicates
\+(X) - evaluates to Yes if X evaluates to No (can not be satisfied). This function used to be called not. example rule: sibling(X, Y) :- parent(M, Y), parent(M, X), \+(X = Y). length(X, Y) – evaluates to Yes if X is a list whose length is Y | ?- length([a, b], X). X = 2 | ?- length(a, X) No Section 16.5, plus other references
31
Section 16.5, 16.6 plus other references
Arithmetic in prolog Is operator v is expression. Expression is an arithmetic expression that is evaluated If the value of expression can be unified with v then the query succeeds Examples X is Yes, by setting X to 8 8 is Yes. 3 + 5 is No, right side is evaluated, left is not Section 16.5, plus other references
32
Section 16.5, 16.6 plus other references
Arithmetic in Prolog Arithmetic operators +, -, *, / with usual precedence Comparison operators =:= is equal to (only works on numbers) =\= is not equal to =< less than or equal to >= greater than or equal to > < = is equal for non-numeric constants \= is not equal for non-numeric constants Section 16.5, plus other references
33
Section 16.5, 16.6 plus other references
Examples | ?- 3 =:= Yes | ?- 3 < Yes | ?- 3 * 4 < 4 * Yes | ?- baron =:= grayson. Prolog error | ?- 3 = No, because isn’t evaluated. Section 16.5, plus other references
34
Section 16.5, 16.6 plus other references
Problem. Given the following facts: birthyear(baron, 1994). birthyear(grayson, 1996). birthyear(ethan, 1999). birthyear(ben, 1994). birthyear(eli, 1994). year(2008). Section 16.5, plus other references
35
Section 16.5, 16.6 plus other references
Problem continued Add the following rules. age(X, Y) which returns yes if the age of X is Y younger(X, Y) which returns yes if X is younger than Y older(X, Y). sameage(X, Y) which returns yes if X and Y are the same age differentage(X, Y) fourteenyearold(X) which returns true if X is fourteen years old Section 16.5, plus other references
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.