COSC 2P93 Logic Programming Instructor: Brian Ross Instructor: Brian Ross Texts: Texts: Prolog Programming for Artificial Intelligence,4e, Ivan Bratko,

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Getting started with Prolog
Logic Programming Lecture 1: Course orientation; Getting started with Prolog.
1 Logic Programming School of Informatics, University of Edinburgh Logic Programming in 50 Minutes The purpose of this lecture is to explain why logic.
Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines
Software Development Languages and Environments. Programming languages High level languages are problem orientated contain many English words are easier.
1. An Overview of Prolog.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Prolog.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
Prolog Programming for Artificial Intelligence Three edition 2001
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Introduction to Prolog What is Prolog? Application Areas of Prolog How does Prolog work? (Syntax of Prolog) Program Structure.
Programming Languages Language Design Issues Why study programming languages Language development Software architectures Design goals Attributes of a good.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Declarative Languages. Declarative languages In an imperative programming language, a program specifies how to solve a problem In a declarative language,
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 26: Introduction to Logic Programming with Prolog COMP 144 Programming Language.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for Ch.1 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
1 Software Development Topic 2 Software Development Languages and Environments.
High-Level Programming Languages: C++
Formal Models of Computation Part II The Logic Model
Introduction to Shell Script Programming
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Evolution of Programming Languages Generations of PLs.
Logic Programming Programming Lecture 1: Getting started with Prolog.
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
Expert System Development Tools Sean Lydon Daniel Nelson Emily Schwarz.
Copyright © 2007 Addison-Wesley. All rights reserved.1-1 Reasons for Studying Concepts of Programming Languages Increased ability to express ideas Improved.
F28PL1 Programming Languages Lecture 16: Prolog 1.
Artificial Intelligence Programming in Prolog Lecture 1: An Introduction 23/09/04.
For Wednesday No new reading Prolog handout 2 Chapter 9, exercise 4.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.
Introduction to Prolog Facts, Questions & Rules Atoms & Variables.
B. Ross Cosc 4f79 1 Inference mechanisms Backward chaining - goal-driven reasoning which gathers data as needed - Prolog's default mechanism - good for.
Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic structures and functions of the logic programming language:
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Programming Languages Third Edition Chapter 4 Logic Programming.
Logic Programming Dr. Yasser Nada Fall 2010/2011 Lecture 1 1 Logic Programming.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Course Instructor: Hira Farman Course : BY:HIRA FARMAN.
Knowledge Based Information System
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
For Friday No reading Prolog Handout 2. Homework.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Logic Programming: Prolog Notes for CSCE 190 Based on Bratko,
Done By :- -Nesreen Basem -Sara nabil -Rawan Prolog Program.
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog programming Introduction to Prolog
CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Prolog programming Introduction to Prolog
Dr. Yasser Nada Fall 2010/2011 Lecture 1
Chapter 2: Prolog (Introduction and Basic Concepts)
Presentation transcript:

COSC 2P93 Logic Programming Instructor: Brian Ross Instructor: Brian Ross Texts: Texts: Prolog Programming for Artificial Intelligence,4e, Ivan Bratko, Addison-Wesley Prolog Programming for Artificial Intelligence,4e, Ivan Bratko, Addison-Wesley (recommended): Programming in Prolog,5e, Clocksin & Mellish, Springer-Verlag, (recommended): Programming in Prolog,5e, Clocksin & Mellish, Springer-Verlag, 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

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

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

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

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

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

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: COSC 2P93 Prolog: Intro

Prolog COSC 2P93 Prolog: Intro

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

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

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

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

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

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

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

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

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

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

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