190 likes | 216 Views
CS 361: Artificial Intelligence. Spring_2015. What is Prolog ?. Developed by Alain Colmerauer and Philippe Roussel in 1972 . Prolog is acronym of PROgramming in LOGic. Prolog program is sequence of rules and facts .
E N D
CS 361: Artificial Intelligence Spring_2015
What is Prolog ? Developed by Alain Colmerauer and Philippe Roussel in 1972. Prolog is acronym of PROgramming in LOGic. Prolog program is sequence of rules and facts. Uses first-order predicate calculus to express specification. Elegant syntax and well-defined semantics.
Logic Programming • So far programming has been algorithmic. • Procedural: statements (C, C++, FORTRAN, Pascal) • Functional: expressions (Postscript, ML, LISP) • Now we will look at declarative languages - They state what the solution should look like; not how to compute such a solution. • Example, BNF. Give a description of all even length palindromes over 0 and 1: • Can list them: 00, 11, 0000, 0110, 1001, 1111, ... • Can give a specification rule: S 0S0 | 1S1 | 00 | 11 • The BNF rule does not say how to generate such strings or how to recognize a given string, only a description of what all such strings look like.
Logic Programming (Cont …) • Logical predicates: • P :- a, b. • P :- c,d. • P is true if a and b are true or if c and d are true, or (a b) (c d) • This is where the term logic programming comes from.
Basic for Prolog • We can specify either all the strings that obey the relationship we want, or we can specify a pattern (the context free production in this case) that is matched with the input string. • This model occurs frequently in database applications. Consider a relation X: • X(a,b). is a fact that states that a and b have relation X. • Such relations and facts are the basis for Prolog execution.
Prolog Execution • So Prolog is: • A database of facts. • A database of queries. • A sequential execution model: Search facts in order looking for matches. If failure, back up to last match and try next entry in database. • Also, unlike previous languages studied, Prolog is not complete. Not every program function can be specified as a Prolog program.
Syntax Program logic is expressed in terms of relations Computations are queries over these relations Single data type: TERM Relations defined by clauses
TERM • Terms are divided amongst many different sub-types • Atom • Numbers • Variables • Compound Term
Complete Syntax of Terms Term Variable Constant Compound Term Names an individual Stands for an individual unable to be named when program is written Names an individual that has parts Atom Number likes(john, ‘mary)’ book(dickens, Z, cricket) f(x) [1, 3, g(a), 7, 9] -(+(15, 17), t) 15 + 17 - t X Gross_pay Diagnosis _257 _ alpha17 gross_pay john_smith dyspepsia + =/= ’12Q&A’ 0 1 57 1.618 2.04e-27 -13.6
General Syntax of Prolog The rules and facts in Prolog are terminated by full stop (.). Goal can be included in the program or given at the prompt. Goal can be single or conjunction of sub-goal(s) terminated by full stop. Constants are numerals and symbols such as, 4, mary, etc. String is a sequence of characters and is enclosed within single quotes e.g., 'This is a string'. Predicate names must start with alphabet and are formed by using lower case letters, numerals and underscore ( _ ). Variable names are formed similar to predicate names but it must start with upper case letter. Normally, we use X, Y, Z, ... for variables.
Prolog Data • Data: • Integers: 1, 2, 3, 4 • Reals: 1.2, 3.4, 6.7 • Strings: 'abc', '123' • Facts: lower case names • Variables: Upper case names • Lists: [a, b, c, d]
Syntax for predicate calculus programming To represent facts and rules
Structure of Programs An example… Programs consist of procedures. Procedures consist of clauses. Each clause is a fact or a rule. Programs are executed by posing queries.
Example Predicate Procedure for elephant Facts elephant(george). elephant(mary). elephant(X) :- grey(X), mammal(X), hasTrunk(X). Clauses Rule
Example (Cont …) ?- elephant(george). true ?- elephant(jane). false Queries Replies
Facts • Facts are like relations in database systems, representing the known data. • Bill likes Cindy. • Cindy likes Bill. • Bill likes dog. • In Prolog, • likes (bill, cindy). • likes (cindy, bill). • likes (bill, dogs).
Facts (Cont …) • A database of facts: • inclass(john, cs330). • inclass(mary, cs330). • inclass(george, cs330). • inclass(jennifer, cs330). • inclass(john, cs311). • inclass(george, cs420). • inclass(susan, cs420). • Queries: Prolog can confirm these facts: • ?- inclass(john, cs330). Prolog responds “yes” • ?-inclass(susan, cs420). Prolog responds “yes” • ?-inclass(susan, cs330). Prolog responds “no”