Chapter 8: The Logical Paradigm Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
Chapter 11 :: Logic Languages
Prolog.
Introduction to Prolog, cont’d Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
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).
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Side effects in Prolog Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Chapter 12 - Logic Programming
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
Logic-based, data-driven enterprise network security analysis Xinming (Simon) Ou Assistant Professor CIS Department Kansas State University COS 598D: Formal.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
CS190/295 Programming in Python for Life Sciences: Lecture 1 Instructor: Xiaohui Xie University of California, Irvine.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
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.
Introduction to Logic Programming with Prolog (Section 11.3)
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
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.
1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
Lecture on Programming Languages
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
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.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
CS 363 Comparative Programming Languages Logic Programming Languages.
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)
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Dr. Muhammed Al-Mulhem ICS An Introduction to Prolog.
Knowledge Based Information System
Artificial Intelligence CS370D
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.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Answer Extraction To use resolution to answer questions, for example a query of the form  X C(X), we must keep track of the substitutions made during.
1-1 An Introduction to Prolog Sept Prolog statements Like other programming languages, Prolog consists of collection of statements. Prolog.
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.
Review for Test 2 Chapters 5 (start at 5.4), 6.1, , 12, 13, 15.1, Python.
Section 16.5, 16.6 plus other references
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
CS190/295 Programming in Python for Life Sciences: Lecture 1
Prolog a declarative language
Prolog a declarative language
Logic: Top-down proof procedure and Datalog
Prolog a declarative language
Chapter 12 :: Logic Languages
Logic Programming Language
Programming Languages 2nd edition Tucker and Noonan
Chapter 2: Prolog (Introduction and Basic Concepts)
Presentation transcript:

Chapter 8: The Logical Paradigm Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1

What we have covered so far: Imperative paradigm – Computes effects Functional paradigm – Computes values – Used ML as an example Logical paradigm – Computes relations – Use Prolog as an example 2

Example relations Parent relation: – parent(A, B) means A is B’s parent – e.g. parent(bill, mary). parent(mary, john). Ancestor relation – Can be defined inductively: A is B’s ancestor if A is B’s parent. A is B’s ancestor if A is C’s ancestor, and C is B’s ancestor – The resulting relation is the smallest one that satisfies the above two rules. 3

Some definitions for Prolog Atoms: – Any sequence of alpha-numeric characters that starts with a lower- case letter, or a single-quoted string, or a number e.g. mary, john01, ‘Mary Doe’, 100 Variables: – Any sequence of alpha-numeric characters that starts with an upper- case letter, or an underscore “_” – e.g. Mary, _mary, _ Literal – predicate(t 1, …, t k ), where t i is either an atom, a variable, or a data- structure (function applied to parameters). – e.g. parent(mary, john). parent(mother(john), john). ancestor(mother(father(john)), john). 4

Horn Clauses A Horn clause is a logical clause with a single positive literal: L 0 ∨ L 1 ∨ … L n This is equivalent to L 1 ∧ … ∧ L n => L 0 In Prolog, we use “,” to mean logical and, and write implication “backward”. Each clause is concluded with a “.” L 0 :- L 1, …, L n. – Example: ancestor(A, B) :- parent(A, B). ancestor(A, B) :- parent(A, C), ancestor(C, B). We call the left-hand side of the clause its head, and the right-hand side of the clause its body. – A clause may have an empty body. e.g. parent(mother(X), X). 5

Variables in Clauses All variables are implicitly universally bound at the beginning of the clause – e.g. ancestor(A, B) :- parent(A, B). Logically it is equivalent to: Forall A, B. parent(A,B) => ancestor(A, B) Thus A and B can be instantiated with any term. An underscore “_” is a wild card and can match anything. – e.g. isParent(A) :- ancestor(A, _). 6

Query in Prolog A query is in the form of a literal. The answer to the query is all the instantiations of the variables that make the literal true. – e.g. ? - ancestor(X,Y). X = bill Y = mary; X = mary Y = john; X = bill Y = john; no – Logically it is equivalent to “exists X, Y. ancestor(X,Y)?” 7

Execution Semantics of Prolog When a query is issued, it is “compared” against the head of all the clauses one by one. – If a “match” is found, the body of the clause becomes the new goals – This process will iterate and may either succeed or fail. – In either case the execution will backtrack to the first “choice point”, and try another match. This is called “SLD resolution” 8

Z2=john X=mary Y=john X=bill Y=mary Example SLD resolution ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y). parent(bill,mary). parent(mary,john). ?- parent(X,Y). ?- Success ?- Success ?- parent(X,Z), ancestor(Z,Y). ?- ancestor(X, Y). X=bill Z=mary ?- ancestor(mary,Y). ?- parent(mary,Y). ?- Success ?- parent(mary,Z2), ancestor(Z2,Y). … Failure … Failure ?- ancestor(john,Y). X=mary Z=john ?- ancestor(john,Y). 9

Logic deduction as a program The advantage of Prolog is that it has both a logic meaning, and an execution semantics – Ideally you do not need to think about the SLD resolution process when writing Prolog code – A Prolog program is simply a collection of logical statements. A query is simply asking whether a fact can be derived as a logical consequence of the statements. However… – When the result does not match your expectation, knowing the SLD resolution process will help in debugging. – Moreover, Prolog is not always declarative, which we will see in the next lecture. 10

Practice XSB We will be using the XSB Prolog system – Is installed on all the departmental Linux machines – Can be downloaded from: Installation is relatively hassle-free. However, if you need to compile XSB under Mac OS X Snow Leopard, please let me know and there will be special instructions. 11

The first simple Prolog program Put the following Prolog statements in a text file named “ancestor.P” parent(bill, mary). parent(mary, john). ancestor(A, B) :- parent(A, B). ancestor(A, B) :- parent(A, C), ancestor(C, B). Load the file in XSB: bash-3.2$ xsb [xsb_configuration loaded] [sysinitrc loaded] XSB Version 3.2 (Kopi Lewak) of March 15, 2009 [i386-apple-darwin10.4.0; mode: optimal; engine: slg-wam; scheduling: local; word size: 64] | ?- [ancestor]. 12

Experiment with it Issue various queries: e.g. ?- ancestor(X,Y). ?- ancestor(bill, X). ?- ancestor(john, X). … Change the order of the clauses and see what will happen: parent(bill, mary). parent(mary, john). ancestor(A, B) :- parent(A, C), ancestor(C, B). ancestor(A, B) :- parent(A, B). 13