Download presentation
Presentation is loading. Please wait.
Published byDamian Day Modified over 9 years ago
1
COSC 2P93 Logic Programming Instructor: Brian Ross Instructor: Brian Ross Texts: Texts: Prolog Programming for Artificial Intelligence,4e, Ivan Bratko, Addison-Wesley 2011. Prolog Programming for Artificial Intelligence,4e, Ivan Bratko, Addison-Wesley 2011. (recommended): Programming in Prolog,5e, Clocksin & Mellish, Springer-Verlag, 2003. (recommended): Programming in Prolog,5e, Clocksin & Mellish, Springer-Verlag, 2003. Lots of free Prolog books (see 2P93 web site) Lots of free Prolog books (see 2P93 web site) Goal: An introduction logic programming and Prolog Goal: An introduction logic programming and Prolog Lots of programming assignments Lots of programming assignments only way to learn a new language and programming paradigm. only way to learn a new language and programming paradigm. 1COSC 2P93 Prolog: Intro
2
Systems available Linux, Windows: Sicstus Prolog 4.2 /usr/local/bin/sicstus Linux, Windows: Sicstus Prolog 4.2 /usr/local/bin/sicstus Note: If you find another system (eg. SWI Prolog), feel free to use it. Note: If you find another system (eg. SWI Prolog), feel free to use it. BUT... Beware of incompatibility of libraries, I/O,... BUT... Beware of incompatibility of libraries, I/O,... you must port your assignments to Sicstus, and have it run on Sandcastle before submitting it you must port your assignments to Sicstus, and have it run on Sandcastle before submitting it if marker cannot run your code on sandcastle, then it won’t be marked! if marker cannot run your code on sandcastle, then it won’t be marked! 2COSC 2P93 Prolog: Intro
3
Prolog: brief history born in the mid-70’s in Marseilles, France born in the mid-70’s in Marseilles, France researchers in Edinburgh made it into a comprehensive language: “Edinburgh syntax” researchers in Edinburgh made it into a comprehensive language: “Edinburgh syntax” derived from research in automatic theorem proving. derived from research in automatic theorem proving. Adopted by Japanese 5th Generation Computer Technology initiative in 1980’s Adopted by Japanese 5th Generation Computer Technology initiative in 1980’s Wide acceptance from AI community Wide acceptance from AI community Lisp is a competitor; however, Lisp is lower-level Lisp is a competitor; however, Lisp is lower-level 3COSC 2P93 Prolog: Intro
4
Main Characteristics High-level: foundation in logic permits “declarative programming” High-level: foundation in logic permits “declarative programming” user specifies what to compute at a high level. user specifies what to compute at a high level. Prolog interpreter searches for (computes) a solution Prolog interpreter searches for (computes) a solution can write powerful applications in much less code than in C can write powerful applications in much less code than in C Symbolic: program code can be treated as data Symbolic: program code can be treated as data self-modifying programs self-modifying programs great for... great for... artificial intelligence apps: Expert systems, planning, reasoning,... artificial intelligence apps: Expert systems, planning, reasoning,... natural language processing natural language processing fast prototyping: systems programming, programming languages fast prototyping: systems programming, programming languages parallelism parallelism Implementation characteristics: Implementation characteristics: Prolog is usually interpreted → slow! Prolog is usually interpreted → slow! compilers exist; Prolog performance acceptable compilers exist; Prolog performance acceptable 4COSC 2P93 Prolog: Intro
5
Starting Prolog 1. write your programs into a text file. Helpful to use “.pl” or “. pro” as your Prolog code extension Helpful to use “.pl” or “. pro” as your Prolog code extension beware: “.pl” also used for Perl! beware: “.pl” also used for Perl! 2. Unix prompt: “sicstus” get the prompt: | ?- get the prompt: | ?- Prolog interpreter is awaiting your commands Prolog interpreter is awaiting your commands 3. When in Sicstus, load your file: ?- consult(‘myfile.pro’). 1. no blank between consult and “(“ 2. the period at end of line (ends every Prolog statement) 3. Single quotes around filename required if it uses an extension. But can leave out “pro” and “pl” extensions. If you edit “myfile.pro” in another window, and want to re-load it, then type: reconsult(‘myfile.pro’). If you edit “myfile.pro” in another window, and want to re-load it, then type: reconsult(‘myfile.pro’). this will replace existing program code with the reloaded code this will replace existing program code with the reloaded code abbreviations: ?- consult(‘file’). ↔ ?- [‘file’]. abbreviations: ?- consult(‘file’). ↔ ?- [‘file’]. ?- reconsult(‘file’). ↔ ?- [-’file’]. ?- reconsult(‘file’). ↔ ?- [-’file’]. 5COSC 2P93 Prolog: Intro
6
Prolog To exit, type: ?- halt. (or CTRL D) To exit, type: ?- halt. (or CTRL D) Between loading and exiting, you run your program. Consider the following Prolog program: Between loading and exiting, you run your program. Consider the following Prolog program: /* my date program */ likes(jane, sushi). likes(jane, salad). likes(jane, lobster). likes(mary, haggis). likes(steve, bbq). likes(steve, lobster). likes(steve, sushi). good_date(Guy, Gal) :- likes(Guy, Food), likes(Gal, Food). Assume this code is put in the file: date.pro 6COSC 2P93 Prolog: Intro
7
Prolog there are two subroutines or predicates: there are two subroutines or predicates: good_date/2 good_date/2 likes/2 likes/2 predicates defined by: predicates defined by: a) a) b) b) a predicate is composed of one or more Prolog statements or clauses a predicate is composed of one or more Prolog statements or clauses there are 3 kinds of Prolog clauses: there are 3 kinds of Prolog clauses: 1. 1. 2 7COSC 2P93 Prolog: Intro
8
Prolog 3. 3. 8COSC 2P93 Prolog: Intro
9
Prolog A predicate can have a mixture of facts and rules. A predicate can have a mixture of facts and rules. consider this fact from likes: consider this fact from likes: likes(jane, salad). likes(jane, salad). good_date(Guy, Gal) :- likes(Guy, Food), likes(Gal, Food). 9COSC 2P93 Prolog: Intro
10
Prolog execution Unlike “conventional” languages, there are many ways to execute this program. Unlike “conventional” languages, there are many ways to execute this program. To interactively execute the program you issue a program call or query to the interpreter... To interactively execute the program you issue a program call or query to the interpreter... ?- likes(jane, lobster). yes ?- likes(jane, kraft_dinner). no ?- likes(jane, ferraris). no ?- likes(steve, Food). Food = bbq ;% Note: typing ‘;’ tells Prolog to find next soln. Food = lobster ; Food = sushi; no 10COSC 2P93 Prolog: Intro
11
Prolog ?- likes(Who, sushi). Who = jane ; Who = steve ; no ?- good_date(A, B). A = jane, B = jane ; % jane likes sushi A = jane, B = steve ;% jane and steve like sushi A = jane, B = jane ;% jane likes salad A = jane, B = jane ;% jane likes lobster A = jane, B = steve ;% jane and steve like lobster A = mary, B = mary ;% mary likes haggis A = steve, B = steve ;% steve likes bbq A = steve, B = jane ;% steve and mary like lobster A = steve, B = steve ;% steve likes lobster A = steve, B = jane ;% steve and jane like sushi A = steve, B = steve ;% steve likes sushi no 11COSC 2P93 Prolog: Intro
12
Prolog ?- good_date(jane, steve). yes ?- good_date(steve, jane). yes ?- good_date(jane, jane). yes ?- good_date(mary, steve). no ?- likes(jane, X), likes(steve, X). X = sushi yes Comments: Comments: exhaustive matching of answer combinations exhaustive matching of answer combinations matching process uses order of statements in program matching process uses order of statements in program Different variables can match to same constant (person is their own date) Different variables can match to same constant (person is their own date) if a solution is computed, then that same solution given as a query should result in ‘yes’; vice versa for ‘no’ if a solution is computed, then that same solution given as a query should result in ‘yes’; vice versa for ‘no’ 12COSC 2P93 Prolog: Intro
13
Prolog Possible enhancements: Possible enhancements: 1. How to stop someone from being their own date? 2. How to see the food two people like? % 1. smarter good_date: good_date2(Guy, Gal) :- likes(Guy, Food), likes(Gal, Food), \+ (Guy = Gal). % 2. more informative good_date: good_date3(Guy, Gal, Food) :- likes(Guy, Food), likes(Gal, Food). note: \+ is “not”: succeeds if the goal beside it fails note: \+ is “not”: succeeds if the goal beside it fails = is ‘unification’: succeeds if terms match (more later) = is ‘unification’: succeeds if terms match (more later) could replace 3rd goal in good_date2 with: Guy \= Gal could replace 3rd goal in good_date2 with: Guy \= Gal 13COSC 2P93 Prolog: Intro
14
Prolog interpreter each predicate call in the body of a rule, and each call in a query, is called a goal. each predicate call in the body of a rule, and each call in a query, is called a goal. the order you put goals in a query or rule is crucial for standard Prolog interpreters the order you put goals in a query or rule is crucial for standard Prolog interpreters scheme: solve each goal in the query from left to right scheme: solve each goal in the query from left to right clauses order important too clauses order important too scheme: test clauses in the order given: first to last scheme: test clauses in the order given: first to last predicate order does not matter predicate order does not matter 14COSC 2P93 Prolog: Intro
15
Backtracking hitting “ ; “ when a solution is given causes interpreter to find yet another solution: called backtracking hitting “ ; “ when a solution is given causes interpreter to find yet another solution: called backtracking backtracking causes execution to revert back to last place a clause was successful, and move on to the next one backtracking causes execution to revert back to last place a clause was successful, and move on to the next one interpreter keeps track of these places in program, as well as the values given to logic variables interpreter keeps track of these places in program, as well as the values given to logic variables if you keep hitting “;”, all solutions possible will be given (including duplicate ones!) if you keep hitting “;”, all solutions possible will be given (including duplicate ones!) 15COSC 2P93 Prolog: Intro
16
Some comments As in other languages (Java, C,...), the Prolog interpreter does not understand the meaning of the predicate names, atom names, variables,... As in other languages (Java, C,...), the Prolog interpreter does not understand the meaning of the predicate names, atom names, variables,... The answer “no” can mean a number of things: The answer “no” can mean a number of things: the information queried does not exist in the program, and is therefore assumed “false” (eg. likes(harvey, porridge). may not exist ) the information queried does not exist in the program, and is therefore assumed “false” (eg. likes(harvey, porridge). may not exist ) there is a logical error in the program, ie. the program is erroneously written there is a logical error in the program, ie. the program is erroneously written eg. good_date(Guy, Gal) :- likes(Guy, Food), likes(Food, Gal). eg. good_date(Guy, Gal) :- likes(Guy, Food), likes(Food, Gal). there is a spelling error: there is a spelling error: eg. likes(jane, stake). eg. likes(jane, stake). Likewise, a “yes” (with computed results if they exist) can mean: Likewise, a “yes” (with computed results if they exist) can mean: a solution was obtained or verified (logical “true”) a solution was obtained or verified (logical “true”) but a solution is only as correct as the program that generated it! but a solution is only as correct as the program that generated it! queries can be complex; if they are too big, it is better to write a program predicate for them (case in point: “good_date”) queries can be complex; if they are too big, it is better to write a program predicate for them (case in point: “good_date”) 16COSC 2P93 Prolog: Intro
17
Another example: family Bratko text (Section 1.1) % Figure 1.8 The family program. parent( pam, bob). % Pam is a parent of Bob parent( tom, bob). parent( tom, liz). parent( bob, ann). parent( bob, pat). parent( pat, jim). female( pam). % Pam is female male( tom). % Tom is male male( bob). female( liz). female( ann). female( pat). male( jim). 17COSC 2P93 Prolog: Intro pam tom jim annpat bobliz
18
Family (cont.) offspring( Y, X) :- % Y is an offspring of X if parent( X, Y). % X is a parent of Y parent( X, Y). % X is a parent of Y mother( X, Y) :- % X is the mother of Y if parent( X, Y), % X is a parent of Y and parent( X, Y), % X is a parent of Y and female( X). % X is female female( X). % X is female grandparent( X, Z) :- % X is a grandparent of Z if parent( X, Y), % X is a parent of Y and parent( X, Y), % X is a parent of Y and parent( Y, Z). % Y is a parent of Z parent( Y, Z). % Y is a parent of Z sister( X, Y) :- % X is a sister of Y if parent( Z, X), parent( Z, X), parent( Z, Y), % X and Y have the same parent and parent( Z, Y), % X and Y have the same parent and female( X), % X is female and female( X), % X is female and different( X, Y). % X and Y are different different( X, Y). % X and Y are different predecessor( X, Z) :- % Rule prl: X is a predecessor of Z parent( X, Z). parent( X, Z). predecessor( X, Z) :- % Rule pr2: X is a predecessor of Z parent( X, Y), parent( X, Y), predecessor( Y, Z). predecessor( Y, Z). 18COSC 2P93 Prolog: Intro
19
Prolog Note that we can understand each program predicate at a high level Note that we can understand each program predicate at a high level “what to” programming “what to” programming almost like talking directly in English almost like talking directly in English this is called “declarative programming” this is called “declarative programming” Can also understand program in terms of goals to solve, and the order in which to solve them Can also understand program in terms of goals to solve, and the order in which to solve them “how to” programming “how to” programming like conventional programming in Java like conventional programming in Java called “procedural programming” called “procedural programming” Recommended to split large predicates into multiple lines Recommended to split large predicates into multiple lines makes program more readable & modifiable makes program more readable & modifiable use meaningful names for predicates, constants, variables use meaningful names for predicates, constants, variables Put all predicate clauses together in a single file Put all predicate clauses together in a single file 19COSC 2P93 Prolog: Intro
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.