Presentation is loading. Please wait.

Presentation is loading. Please wait.

INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.

Similar presentations


Presentation on theme: "INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside."— Presentation transcript:

1 INTRODUCTION TO PROLOG

2 PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside single quotes’) Examples: red, apple, cs, ‘abc def’, hello_world 2

3 PROLOG BASICS Numbers – signed integers are always available floating point in available in some implementations Variables - The goal of most prolog programs is to find values for variables Variables start with a capital letter or an underscore Variables that are “unbound” have not current value Variables that are “bound” have been assigned a value Once a variable is bound, it stays bound, unless the binding is undone through backtracking. 3

4 PROLOG BASICS Lists – Lists contain sequences of prolog terms Lists contain comma separated terms,within square brackets. For example: [ this, is, a, list, of, atoms] The empty list is referred to as null [] Lists may contain any terms, including other lists [[this, is], [a, list], [of, lists], and, atoms] 4

5 PROLOG BASICS Assertions Assertions typically specify a fact. They are created using an atomic name and one or more terms in a comma separated list inside parenthesis. Assertions are terminated with a period. For example, /* red is a color */ primaryColor(red). /* so is blue */ primaryColor(blue). /* Andrea and Anna are sisters sister(andrea, anna). 5

6 PROLOG BASICS Prolog Rules A head (a predicate – the goal state) A body (a sequence of predicates that can prove the head predicate.) For example: // An atom is a color if it is a primary color color(X) :- primaryColor(X). 6

7 PROLOG BASICS Prolog Queries Uses can make queries to prolog by presenting a goal with bound or on bound components. ?- color(red). yes. ?- color(A) A = red /* here the variable A is “bound” to red. */ /* If we type a semicolon, prolog will try to find another way to answer this. */ 7

8 PROLOG BASICS Rule processing Prolog will try to satisfy each of the predicates in the body of a goal, binding variables as it goes. If it can satisfy all the predicates in the body, the goal is considered true, and the bindings are reported. If it fails to prove a predicate, it will “backtrack” to the most recent goal with an alternate solution, and try that. For example: person(sally). person(tom). female(sally). male(tom). man(X) :- person (X), male(X). 8 7 ?- trace. true. [trace] 7 ?- man(X). Call: (6) man(_G1025) ? creep Call: (7) person(_G1025) ? creep Exit: (7) person(sally) ? creep Call: (7) male(sally) ? creep Fail: (7) male(sally) ? creep Redo: (7) person(_G1025) ? creep Exit: (7) person(tom) ? creep Call: (7) male(tom) ? creep Exit: (7) male(tom) ? creep Exit: (6) man(tom) ? creep X = tom.

9 PROLOG BASICS List operations [A | B] binds the first term with A and the rest of the list to B A simple tail-recursion to write out the elements in a list: writeList([]). writeList([A|B]) :- write(A), nl, writeList(B). 9

10 APPEND /* append the second list to the first */ append([], SecondList, SecondList). /* copy the first list item by item */ append([Head|Tail, SecondList, [Head | TheRest]) :- append(Tail, L, Rest). 10 Call: (6) append([a, b, c], [d, e], _G1174) ? creep Call: (7) append([b, c], [d, e], _G1256) ? creep Call: (8) append([c], [d, e], _G1259) ? creep Call: (9) append([], [d, e], _G1262) ? creep Exit: (9) append([], [d, e], [d, e]) ? creep Exit: (8) append([c], [d, e], [c, d, e]) ? creep Exit: (7) append([b, c], [d, e], [b, c, d, e]) ? creep Exit: (6) append([a, b, c], [d, e], [a, b, c, d, e]) ? creep

11 REMOVING THE POSSIBILITY OF BACKTRACKING The “cut” operator (!) The cut operator prevents the prolog interpreter from backtracking to subgoals that were satisfied prior to encountering the cut operator. Example: notEqual(X, X) :- !, fail. notEqual(_, _). 11

12 MORE DETAILS ABOUT LISTS Prolog lists are usually null-terminated, but they don’t have to be. [a, b] is really the list of the items a, b, and null ([]). The list of just a and b is specified as [a | b]. This is similar to the “dotted notation” in LISP. 12

13 SOME BUILT-IN PREDICATES Determining Type atom(X) - True if X is bound to an atom. compound(X) – True if X is bound to a compound object. var(X) – True if X is currently a free variable. nonvar(X) – True if X is currently a non-free variable. 13

14 SOME BUILT-IN PREDICATES fail – explicitly force backtracking repeat – always succeeds in a new way. 14

15 MORE BUILT-IN PREDICATES Assert and Retract Assert lets you create new assertions. assert(apart(apt202, 3)). /* in swiProlog apart must be a new goal, not already part of the program unless you declare the predicate as “dynamic” */ Retract lets you remove assertions from the program. retract(apart(apt202, 3)). /* in swiProlog apart must be a goal created with assert unless you declare the predicate as “dynamic” */ To specify a predicate as dynamic use a statement of the form: :- dynamic predicateName/arity /* arity is the number of parameters */ Example: :- dynamic apartment/1 15

16 MORE BUILT-IN PREDICATES Comparison operators X = Y True if unification succeeds on X and Y X \= Y True if unification fails on X and Y. Y True if X is less than Y in the standard order of terms b is true. Similar definitions exist for: Y Y Y Y 16

17 SOME ADDITIONAL EXAMPLES Check to see if an element is in a list member([a, b, c, d], c). /* true */ Remove an element from a list removeElement([a, b, c, d], c, NewList). /* NewList = [a, b, d] */ Interleave elements in two lists interleaveLists([a, b, c], [d, e., f], X). /* X = [a, d, b, e, c, f] */ countOccurances([a, b, c, a, a, d], a, X). /* X = 3 */ Intersection([a, b, c, d, e], [e, c, j], X). /* X = [e,c] */ 17


Download ppt "INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside."

Similar presentations


Ads by Google