Knowledge Based Information System

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
Chapter 11 :: Logic Languages
Prolog.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Inference and Reasoning. Basic Idea Given a set of statements, does a new statement logically follow from this. For example If an animal has wings and.
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).
Answer Set Programming Overview Dr. Rogelio Dávila Pérez Profesor-Investigador División de Posgrado Universidad Autónoma de Guadalajara
Logic Languages Source: Scott, Michael Programming Language Pragmatics 3rd Ed.
COP4020 Programming Languages Logic programming and Prolog Prof. Xin Yuan.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
Chapter 8: The Logical Paradigm Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
CPSC 322 Introduction to Artificial Intelligence September 20, 2004.
Declarative Languages. Declarative languages In an imperative programming language, a program specifies how to solve a problem In a declarative language,
Logic Programming Languages. Objective To introduce the concepts of logic programming and logic programming languages To introduce a brief description.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 26: Introduction to Logic Programming with Prolog COMP 144 Programming Language.
1 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
DEDUCTIVE DATABASE.
PROLOG KAIST Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.
Formal Models of Computation Part II The Logic Model
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Evolution of Programming Languages Generations of PLs.
COSC 2P93 Logic Programming Instructor: Brian Ross Instructor: Brian Ross Texts: Texts: Prolog Programming for Artificial Intelligence,4e, Ivan Bratko,
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.
Introduction to Logic Programming with Prolog (Section 11.3)
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
F28PL1 Programming Languages Lecture 16: Prolog 1.
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.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic structures and functions of the logic programming language:
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
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?
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
Programming Languages Third Edition Chapter 4 Logic Programming.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Dr. Yasser Nada Fall 2010/2011 Lecture 1 1 Logic Programming.
ISBN Chapter 16 Logic Programming Languages.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
CS 3304 Comparative Languages
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
PROLOG.
Logic Programming with Prolog: Resolution, Unification, Backtracking
Logic Programming Languages
Chapter 11 :: Logic Languages
Back to “Serious” Topics…
Dr. Yasser Nada Fall 2010/2011 Lecture 1
Programming Languages 2nd edition Tucker and Noonan
Chapter 2: Prolog (Introduction and Basic Concepts)
Representations & Reasoning Systems (RRS) (2.2)
PROLOG.
Presentation transcript:

Knowledge Based Information System Prolog Programming.

Prolog Prolog is a declarative language A program is a collection of axioms from which theorems can be proven. Rather than describing how to compute a solution, a program consists of a data base of facts and logical relationships (rules). Rather then running a program to obtain a solution, the user asks a question. When asked a question, the run time system searches through the data base of facts and rules to determine (by logical deduction) the answer.

Logic Programming Axiom are written in a standard form known as Horn clauses A Horn clause consists of a consequent (head H) and a body (terms Bi) H  B1, B2,…, Bn Semantics: when all Bi are true, we can deduce that H is true

Application Areas Prolog has been a very important tool in artificial intelligence applications expert systems Updated versions of Prolog are now being used for natural language interfaces smart information management systems

Prolog PROgramming in LOGic It is the most widely used logic programming language Its development started in 1970 and it was result of the collaboration between researchers from Marseille, France, and Edinburgh, Scotland Main applications: Artificial intelligence: expert systems, natural language processing, … Databases: query languages, data mining,… Mathematics: theorem proving, symbolic packages,…

Declarative Language This means that The programmer declares facts defines rules for reasoning with the facts Prolog uses deductive reasoning to determine new facts from old decide whether a proposed fact (goal) can be logically derived from known facts (such a decision is called a conclusion)

Fact and rule Comes from Horn clause HB1,B2,…Bn Which means if all the Bs are true, then H is also true In Prolog, we write fact in the form predicate(atom1,….) Predicate is a name that we give to a relation An atom is a constant value, usually written in lower case Fact is the H part of horn clause Rule is in the form predicate(Var1,…):- predicate1(…), predicate2(…), … Where Var1 is a variable, usually begins with upper case Yes, it’s just a rewriting of Fact is a rule that does not have the right hand side. Means “and”

A Prolog Program The program, sometimes called Database is a text file (*.pl) that contain the facts and rules. It contains all the relations needed to define the problem. When you launch a program you are in query mode represented by the ? – prompt. In query mode you ask questions about the relations described in the program. When Prolog is launched the ?- should appear meaning you are in query mode. In SWI-Prolog you can load a program by typing the command [file]. when the file containing your program is file.pl. When you have done this you can use all the facts and rules that are contained in the program.

SWI-Prolog We will use SWI-Prolog for the Prolog programming assignment http://www.swi-prolog.org/ After the installation, try the example program ?- [likes]. % likes compiled 0.00 sec, 2,148 bytes Yes ?- likes(Mother,baby). No ?- likes(sam, X). X = Steve ; X = Paul ; X = Samuel ; Load example likes.pl This goal cannot be proved, so it assumed to be false (This is the so called Close World Assumption) Asks the interpreter to find more solutions

Example location(desk, office). location(apple, kitchen). Program location(desk, office). location(apple, kitchen). location(flashlight, desk). location('washing machine', cellar). location(nani, 'washing machine'). location(broccoli, kitchen). location(crackers, kitchen). location(computer, office). Query ?- location(apple, kitchen). yes

Prolog Programming Model A program is a database of (Horn) clauses that are assumed to be true. The clauses in a Prolog database are either facts or rules. Each ends with a period. A fact is a clause without a right-hand side. rainy(asheville). A rule has a right-hand side. snowy(X) :- rainy(X), cold(X). The token :- is the implication symbol. The comma (,) indicates “and”

Prolog Clauses A query or goal is a clause with an empty left-hand side. Queries are given to the prolog interpreter to initiate execution of a program. Each clauses is composed of terms: Constants (atoms, that are identifier starting with a lowercase letter, numbers, punctuation, and quoted strings) E.g. baby, 4.5, +, ‘Hi, Mom’ Variables (identifiers starting with an uppercase letter) E.g. sugar Structures (predicates or data structures) E.g. africaan(sugar), date(Year,Month,Day)

Structures Structures consist of an atom called the functor and a list of arguments E.g. date(Year,Month,Day) E.g. T = tree(3, tree(2,nil,nil), tree(5,nil,nil)) 3 2 5

Prolog reasoning If we have this fact and rule rainy(Nairobi). rainy(mombasa). 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 Nairobi(our first atom in this example) The program looks to see if there is rainy(Nairobi). There is! So the substitution gives the result “true” The Prolog will answer C= Nairobi To find an alternative answer, type “;” and “Enter” It’ll give C= Mombasa If it cannot find any more answer, it will answer “no”

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’). If you want to load the same program again, use reconsult. -> prevent two copies in memory. A backslash in the file name may have to be written twice, such as c:\\firstprog.pl Do not forget this.

How to ask question Cont’n Then you can ask question. To exit, use command: halt.

Example 2 . /* Clause 1 */ located_in(Dagoretti,Nairobi). /* Clause 2 */ located_in(Thika,Kiambu). /* Clause 3 */ located_in(kongowea,Mombasa). /* Clause 4 */ located_in(kondele,Kisumu). /* Clause 5 */ located_in(X,Kenya) :- located_in(X,Nairobi). /* Clause 6 */ located_in(X,Kenya) :- located_in(X,Kiambu). /* Clause 7 */ located_in(X,Kenya) :- located_in(X,Mombasa). /* Clause 8 */ located_in(X,Coastal_Kenya) :- located_in(X,Mombasa). /* Clause 9 */ located_in(X,Western-Kenya) :- located_in(X,Kisumu). /* Clause 10 */ located_in(Ruiru,Kiambu). .

Question in Prolog To ask whether Thika is in Kiambu: ?- located_in(Thika,Kiambu). This query matches clause 1. So prolog replies “yes”. ?- located_in(kongowea,Coastal_Kenya). This query can be solve by calling clause 8, and then clause 3. So prolog replies “yes”.

-located_in(Thika,Nairobi) ?-located_in(Thika,Nairobi). 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”.

Prolog can fill in the variables ?- located_in(X, Kiambu). This is a query for prolog to find X that make the above query true. This query can have multiple solutions: both Thika and Ruiru are in Kiambu. What prolog does is: find one solution and asks you whether to look for another. ->

Cannot find any more solution Questioning prolog The process will look like X = Thika More (y/n)? y X = Ruiru no Some implementations let you type semicolon without asking any question. Cannot find any more solution

Sometimes it won’t let you ask for alternatives This is because: Your query prints output, so prolog assumes you do not want any more solutions. For example: ?- located_in(X,Kiambu), write(X). will print only one answer. Your query contains no variable. Prolog will only print “yes” once. Print out

What about printing all solutions To get all the towns in kiambu: ?-located_in(X,Kiambu), write(X), nl, fail. New line Rejects the current solution. Forcing prolog to go back and substitutes other alternatives for X.

located_in is said to be nondeterministic Because there can be more than one answer.

Any of the arguments can be queried ?- located_in(Thika,X). ?- located_in(X, Nairobi). ?- located_in(X,Y). Gets the names of regions that contain Thika. Gets the names of the cities that are in Nairobi. Gets all the pairs that of located_in that it can find or deduce.

Questioning prolog ?-located_in(X,X). Forces the two arguments to have the same value, which will result in a fail.

Principle of Resolution Prolog execution is based on the principle of resolution If C1 and C2 are Horn clauses and the head of C1 matches one of the terms in the body of C2, then we can replace the term in C2 with the body of C1 For example, C1: likes(sam,Sugar) :- africaan(sugar), sweet(sugar). C2: africaan(honey). C3: sweet(honey). We can replace the first and the second terms in C1 by C2 and C3 using the principle of resolution (after instantiating variable sugar to honey) Therefore, likes(sam, honey) can be proved

Another Example takes(john, ics201). takes(john, ics245). takes(Samuel, ics302). Takes(Samuel, ics254). classmates(X, Y) :- takes(X, Z), takes(Y, Z). via resolution, yields the new rule: classmates(john, Y) :- takes(Y, ics254).