Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Basic Prolog ?- prolog prompt A :- B IMPLIES in rules; if B then A, logical AND ; logical OR -> if-then-else. END of predicate switch query/database.

Similar presentations


Presentation on theme: "1 Basic Prolog ?- prolog prompt A :- B IMPLIES in rules; if B then A, logical AND ; logical OR -> if-then-else. END of predicate switch query/database."— Presentation transcript:

1 1 Basic Prolog ?- prolog prompt A :- B IMPLIES in rules; if B then A, logical AND ; logical OR -> if-then-else. END of predicate switch query/database mode [ ] Beginning and end of a list ( ) Grouping (arguments) anything123 constant Anything456 variable _anything anonymous variable halt. stops the Prolog interpreter trace. shows entry/exit/redo/fail for each call. ! CUT (see later, used in not )

2 2 Structure of Programs Programs consist of procedures. Procedures consist of clauses. Each clause is a fact or a rule. Programs are executed by posing queries.

3 3 Clauses: Facts and Rules "if" "provided that" "turnstile" Head :- Body. This is a rule. Head. This is a fact. Full stop at the end.

4 4 Body of a (Rule) Clause Contains Goals HeadBody Goals :- likes(mary, X)human(X), honest(X).

5 5 Example Program elephant(george). elephant(mary). elephant(X) :- grey(X), mammal(X), hasTrunk(X). Predicate Procedure for elephant Facts Clauses Rule

6 6 Clauses can be interpreted in a declarative or a procedural way Clause: Declarative reading: Procedural reading: H :- G1, G2, …, Gn. That H is provable follows from goals G 1, G 2, …, G n being provable To execute procedure H, the procedures called by goals G 1, G 2, …, G n are executed first Interpretation of Clauses

7 7 Clauses can be interpreted in a declarative, or in a procedural way Clause: Declarative reading: Procedural reading: H :- G1, G2, …, Gn. That H is provable follows from goals G 1, G 2, …, G n being provable To execute procedure H, the procedures called by goals G 1, G 2, …, G n are executed first

8 8 Clauses Clauses provide a convenient way to express case analysis and non-determinism. Program clauses and data have the same form. A Prolog program can also be seen as a relational database containing rules and facts. The relational form of procedures makes it possible to define ‘reversible’ procedures.

9 9 Unification Term-manipulation that –passes parameters, –returns results, –selects and constructs data structures Basic control flow model is backtracking Sometimes it is necessary to use control features that are not part of ‘logic’

10 10 Computation via Queries Facts and rules are stored in a database –They can be queried ?- likes(joe, fish). yes ?- likes(joe, chocolate) no –Important: no –means “I know it’s false, or I don’t know” yes –means “I can prove it” asymmetric

11 11 Example Queries Queries Replies ?- elephant(george). Yes ?- elephant(jane). no

12 12 Prolog’s Style State the facts and rules Pose queries Prolog finds the answers by –searching the facts –deducing new facts from the rules Answers depend on the search algorithm

13 13 Imperative/Functional/Declarative Styles – Example - Concatenate lists a and b concat([], Z, Z). concat([H|T], L, [H|Z]) :- concat(T, L, Z). list procedure concat(list a, list b) { list t = list u = copylist(a); while (t.tail != nil) t = t.tail; t.tail = b; return u; } cat(a,b) ≡ if b = nil then a else cons(head(a), concat(tail(a),b)) In an imperative PL In a functional PL In a declarative PL

14 14 Example: Zoo /* Facts */ elephant(gigi). elephant(mary). elephant(joe). panda(chi_chi). panda(ming_ming). stripey(tigger). stripey(mrED). big_teeth(tigger). isaCat(tigger). isaHorse(mrED). venomous(rattler). venomous(grass_snake). /* Rules */ dangerous(X) :- big_teeth(X). dangerous(X) :- venomous(X). swims(Y) :- isaFish(Y). guess(X, tiger) :- stripey(X), big_teeth(X), isaCat(X). guess(X, koala) :- arboreal(X), sleepy(X). guess(X, zebra) :- stripey(X), isaHorse(X). /* Sample Queries */ ?- dangerous(ming_ming). ?- dangerous(X). ?- guess(X,Y). www2.hawaii.edu/~janst/313/prolog/zoo.pl

15 15 Colored ball problem Find the correct sequence of colored balls to satisfy the given rules: 1.One red, one black and one white ball Solution will be a permutation of {red,black,white} 2.white is just to the right of black If black is in pos1, white is in pos2, If black is in pos2, white is in pos3 3.black is not in the center Black can’t be in pos2 www2.hawaii.edu/~janst/313/prolog/colors.pl

16 16 Cryptarithmatic Problem Find the unique digit that will replace each character to solve a puzzle S E N D + M O R E M O N E Y www2.hawaii.edu/~janst/313/prolog/crypto.pl

17 17 Eight Queens Problem Given an 8-by-8 chessboard, place 8 queens so that they do not threaten each other along a row, column, or a diagonal Mathematically determine diagonal –two queens are on the same / (UR->BL) diagonal iff the sum of the row and the column is the same for each –two queens are on the same \ (UL->BR) diagonal iff the difference between the row and the column are the same for each Generalize: N queens (N >=4) www2.hawaii.edu/~janst/313/prolog/queens.pl

18 18 N-Queens Without Searching A solution to the N-Queens problem for any N >= 4 Placing queens takes constant time per queen, O(N) Drawing the board takes O(N^2) time Published in ACM SIGART Bulletin, 2(2), page 7 See example at –www.apl.jhu.edu/~hall/NQueens.htmlwww.apl.jhu.edu/~hall/NQueens.html now unfortunately broken


Download ppt "1 Basic Prolog ?- prolog prompt A :- B IMPLIES in rules; if B then A, logical AND ; logical OR -> if-then-else. END of predicate switch query/database."

Similar presentations


Ads by Google