1 / 24

Prolog Programming Concepts

Learn the basics of Prolog, a declarative logic programming language. Explore predicate definitions, facts, rules, and more. Discover applications like expert systems, NLP, and databases.

Download Presentation

Prolog Programming Concepts

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. DM552: Part 2 ProgramingLogic Chapter 2: Prolog (Introduction and Basic Concepts) Dr Youcef Djenouri djenouri@imada.sdu.dk 2017-2018

  2. Introduction (1/2) • (imperative, functional, object-oriented, declarative) • Prolog is the logic declarative language. • Alain Colmeraeur & Philippe Roussel, 1971-1973 • With help from theorem proving folks such as Robert Kowalski • Original project: Type in French statements & question • Computer needed NLP and deductive reasoning

  3. Introduction (2/2) • Assists thinking in terms of objects and entities • Not good for number crunching • Useful applications of Prolog in • Expert Systems (Knowledge Representation and Reasoning) • Natural Language Processing • Relational Databases

  4. Basic Concepts (1/10) • Prolog programs are made up of facts and rules. • A fact asserts some property of an object, or relation between two or more objects. e.g. parent(jane,alan). Can be read as “Jane is the parent of Alan.” • Rules allow us to infer that a property or relationship holds based on preconditions. e.g. parent(X,Y) :- mother(X,Y). = “Person X is the parent of person Y if X is the mother of Y.”

  5. Basic Concepts (2/10)

  6. Basic Concepts (3/10) • Names of relationship and objects must begin with a lower-case letter. • Variables must begin with a higher-case letter. • Relationship is written first (typically the predicate of the sentence). • Objects are written separated by commas and are enclosed by a pair of parenthesis. • The full stop character ‘.’ must come at the end of fact and rules.

  7. Basic Concepts (4/10) • Both facts and rules are predicate definitions. • ‘Predicate’is the name given to the word occurring before the bracket in a fact or rule: parent(jane,alan). • By defining a predicate you are specifying which information needs to be known for the property denoted by the predicate to be true. Predicate name

  8. Basic Concepts (5/10) • Predicate definitions consist of clauses. = An individual definition (whether it be a fact or rule). e.g. mother(jane,alan).= Fact parent(P1,P2):- mother(P1,P2).= Rule • A clause consists of a head • and sometimes abody. • Facts don’t have a body because they are always true. body head

  9. Basic Concepts (6/10) • The symbol ‘,’ use as conjunction operator. • Example • Facts: • likes(mary,food). • likes(mary,tea). • likes(john,tea). • likes(john,mary) • likes(mary,X),likes(john,X).

  10. Basic Concepts (7/10) • And: «, » • OR: A new line, • Not: \+ • Equality: == • Anonymous variable: _

  11. Basic Concepts (8/10) • A Prolog program consists of predicate definitions. • A predicate denotes a property or relationship between objects. • Definitions consist of clauses. • A clause has a head and a body (Rule) or just a head (Fact). • A head consists of a predicate name and arguments. • A clause body consists of a conjunction of terms. • Terms can be constants, variables, or compound terms.

  12. Basic Concepts (9/10) mother_of(cathy,maria) An atom alone is a structure too. arguments atom Closing paren. Opening parenthesis Atom at the beginning is called functor

  13. Basic Concepts (10/10) • Clauses from the same predicate must be put in a group: How prolog reacts depends on the implementation mother(…). mother(…). father(…). father(…). mother(…). father(…). mother(…). father(…).

  14. Prolog reasoning • If we have this fact and rule • rainy(london). • rainy(bangkok). • dull(X):- rainy(X). • We can ask (or query) prolog on its command prompt • ?- dull(C). (is there a C that makes this predicate true?) • It will automatically try to substitute atoms in its fact into its rule such that our question gives the answer true • in this example, we begin with dull(X), so the program first chooses an atom for X, that is london (our first atom in this example) • The program looks to see if there is rainy(london). There is! • So the substitution gives the result “true” • The Prolog will answer • C= london • To find an alternative answer, type “;” and “Enter” • It’ll give C= bangkok • If it cannot find any more answer, it will answer “no”

  15. How to ask question • First, write a prolog program in a .pl file. • Then load the file, using a prolog interpreter. Or use the consult command: ?- consult(‘file.pl’). • To exit, use command: halt.

  16. Example (1\8) /* Clause 1 */ located_in(atlanta,georgia). /* Clause 2 */ located_in(houston,texas). /* Clause 3 */ located_in(austin,texas). /* Clause 4 */ located_in(toronto,ontario). /* Clause 5 */ located_in(X,usa) :- located_in(X,georgia). /* Clause 6 */ located_in(X,usa) :- located_in(X,texas). /* Clause 7 */ located_in(X,canada) :- located_in(X,ontario). /* Clause 8 */ located_in(X,north_america) :- located_in(X,usa). /* Clause 9 */ located_in(X,north_america) :- located_in(X,canada).

  17. Example (2/8) • To ask whether atlanta is in georgia: ?- located_in(atlanta,georgia). • This query matches clause 1. So prolog replies “yes”. ?- located_in(atlanta,usa). This query can be solve by calling clause 5, and then clause 1. So prolog replies “yes”.

  18. Example (3/8) ?-located_in(atlanta,texas). this query gets “no” as its answer because this fact cannot be deduced from the knowledge base. The query succeeds if it gets a “yes” and fails if it gets a “no”.

  19. Example (4/8) ?- located_in(X, texas). This is a query for prolog to find X that make the above query true. • This query can have multiple solutions: both houston and austin are in texas. • What prolog does is: find one solution and asks you whether to look for another. • ->

  20. Example (5/8) • The process will look like X = houston More (y/n)? y X = austin More (y/n)? y no Some implementations let you type semicolon without asking any question. Cannot find any more solution

  21. Example (6/8) • To get all the cities in texas: ?-located_in(X,texas), write(X), nl, fail. New line Rejects the current solution. Forcing prolog to go back and substitutes other alternatives for X.

  22. Example (7/8) ?- located_in(austin,X). ?- located_in(X, texas). ?- located_in(X,Y). Gets the names of regions that contain austin. Gets the names of the cities that are in texas. Gets all the pairs that of located_in that it can find or deduce.

  23. Example (8/8) ?-located_in(X,X). Forces the two arguments to have the same value, which will result in a fail.

  24. Classwork parent(michael, cathy). parent(melody, cathy). parent(charles_gordon, michael). parent(hazel, michael). male(michael). male(charles_gordon). female(cathy). female(melody). female(hazel). father(X,Y):- parent(X,Y), male(X). mother(X,Y):- parent(X,Y), female(X).

More Related