1 / 17

Introduction to Prolog

Introduction to Prolog. 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. Prolog Basics. Numbers –

artan
Download Presentation

Introduction to Prolog

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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, ‘abcdef’, hello_world

  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.

  4. Prolog Basics • Lists – • Lists contain sequences of prolog terms • Lists contain comma separated terms,withinsquare 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]

  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).

  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).

  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. */

  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). ఀ 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).

  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). 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(_, _).

  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.

  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.

  14. Some Built-In predicates • fail – explicitly force backtracking • repeat – always succeeds in a new way.

  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

  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. • X @< Y True if X is less than Y in the standard order of terms • a @< b is true. • Similar definitions exist for: • X @=< Y • X @>= Y • X @< Y • X @> Y

  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] */

More Related