1 / 18

Basic Prolog

Basic Prolog. 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. Clauses: Facts and Rules. "if" "provided that" "turnstile". Head :- Body . This is a rule. Head .

matsu
Download Presentation

Basic 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. Basic Prolog

  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. 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. Body of a (Rule) Clause Contains Goals Head Body likes(mary, X) human(X), honest(X). :- Goals

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

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

  7. Interpretation of Clauses • 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 G1, G2, …, Gn being provable To execute procedure H, the procedures called by goals G1, G2, …, Gn are executed first

  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. 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. 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. Example Queries • ?- elephant(george). • Yes • ?- elephant(jane). • no Queries Replies

  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. Imperative/Functional/Declarative Styles –Example - Concatenate lists a and b 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; } In an imperative PL cat(a,b) ≡ if b = nil then a else cons(head(a), concat(tail(a),b)) In a functional PL concat([], Z, Z). concat([H|T], L, [H|Z]) :- concat(T, L, Z). In a declarative PL

  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. Colored ball problem • Find the correct sequence of colored balls to satisfy the given rules: • One red, one black and one white ball • Solution will be a permutation of {red,black,white} • 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 • black is not in the center • Black can’t be in pos2 • www2.hawaii.edu/~janst/313/prolog/colors.pl

  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. 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. 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.html • now unfortunately broken

More Related