CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring 20081 CSE3302 Programming Languages, UT-Arlington.

Slides:



Advertisements
Similar presentations
© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Advertisements

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Standard Logical Equivalences
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Logic Languages Source: Scott, Michael Programming Language Pragmatics 3rd Ed.
Chapter 12 - Logic Programming
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog (II) Lecture 22 – Prolog (II), Spring CSE3302 Programming Languages,
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
Programming Languages Third Edition
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Comp 205: Comparative Programming Languages Declarative Programming Languages Unification Backtracking Lecture notes, exercises, etc., can be found at:
Logic Programming Languages. Objective To introduce the concepts of logic programming and logic programming languages To introduce a brief description.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical.
Inference and Resolution for Problem Solving
LISP and PROLOG AI Programming Language Submitted To: Dr. Hesham El-Zoka Submitted By: Eng. Ismail Fathalla El-Gayar.
INFERENCE IN FIRST-ORDER LOGIC IES 503 ARTIFICIAL INTELLIGENCE İPEK SÜĞÜT.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
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.
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
1-1 Introduction Logic programming languages, sometimes called declarative programming languages Express programs in a form of symbolic logic Use a logical.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
Declarative vs Procedural Programming  Procedural programming requires that – the programmer tell the computer what to do. That is, how to get the output.
1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
CSE S. Tanimoto Horn Clauses and Unification 1 Horn Clauses and Unification Propositional Logic Clauses Resolution Predicate Logic Horn Clauses.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CS Introduction to AI Tutorial 8 Resolution Tutorial 8 Resolution.
Programming Languages Tucker and Noonan – 2e Chapter 15 – Part 1 Logic Programming “Q: How many legs does a dog have if you call its tail a leg? A: Four.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
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?
Dr. Muhammed Al-Mulhem ICS An Introduction to Logical Programming.
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.
Automated Reasoning Early AI explored how to automated several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Automated Reasoning Early AI explored how to automate several reasoning tasks – these were solved by what we might call weak problem solving methods as.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
First-Order Logic and Inductive Logic Programming.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
C. Varela1 Logic Programming (PLP 11) Predicate Calculus, Horn Clauses, Clocksin-Mellish Procedure Carlos Varela Rennselaer Polytechnic Institute November.
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Propositional Logic Predicate Logic
Programming Languages Third Edition Chapter 4 Logic Programming.
ISBN Chapter 16 Logic Programming Languages.
Knowledge Based Information System
First-Order Logic Reading: C. 8 and C. 9 Pente specifications handed back at end of class.
1-1 An Introduction to Logical Programming Sept
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Done By :- -Nesreen Basem -Sara nabil -Rawan Prolog Program.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
Horn Clauses and Unification
Logic Programming Languages
Prolog a declarative language
Prolog a declarative language
Horn Clauses and Unification
Horn Clauses and Unification
Horn Clauses and Unification
Logic Programming Language
Horn Clauses and Unification
CSE 3302 Programming Languages
Chapter 2: Prolog (Introduction and Basic Concepts)
Presentation transcript:

CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008

SWI-Prolog Available for: Linux, Windows, MacOS Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Prolog Prolog: “Programming in Logic” (PROgrammation en LOgique) One (and maybe the only one) successful logic programming languages Useful in AI applications, expert systems, natural language processing, database query languages Declarative instead of procedural: “What” instead of “How” Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, 2008

History of Prolog s/1990s 2005 first Prolog interpreter by Colmerauer and Roussel 4 © Patrick Blackburn, Johan Bos & Kristina Striegnitz

History of Prolog s/1990s 2005 implementation of DEC10 compiler by Warren 5 © Patrick Blackburn, Johan Bos & Kristina Striegnitz

History of Prolog s/1990s 2005 Definite Clause Grammars implementation by Pereira and Warren 6 © Patrick Blackburn, Johan Bos & Kristina Striegnitz

History of Prolog s/1990s 2005 Prolog grows in popularity especially in Europe and Japan 7 © Patrick Blackburn, Johan Bos & Kristina Striegnitz

History of Prolog s/1990s 2005 Prolog used to program natural language interface in International Space Station by NASA 8 © Patrick Blackburn, Johan Bos & Kristina Striegnitz

Logic Programming Program Axioms (facts): true statements Input to Program query (goal): statement true (theorems) or false? Thus Logic programming systems = deductive databases datalog Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Example Axioms: 0 is a natural number. (Facts) For all x, if x is a natural number, then so is the successor of x. Query (goal). Is 2 natural number?(can be proved by facts) Is -1 a natural number?(cannot be proved) Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Another example Axioms: The factorial of 0 is 1. (Facts) If m is the factorial of n - 1, then n * m is the factorial of n. Query: The factorial of 2 is 3? Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

First-Order Predicate Calculus Logic used in logic programming: First-order predicate calculus First-order predicate logic Predicate logic First-order logic  x (x  x+1) Second-order logic  S  x (x  S  x  S) Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

First-Order Predicate Calculus: Example natural(0)  X, natural(X)  natural(successor(x))  X and Y, parent(X,Y)  ancestor(X,Y).  A, B, and C, ancestor(A,B) and ancestor(B,C)  ancestor(A,C).  X and Y, mother(X,Y)  parent(X,Y).  X and Y, father(X,Y)  parent(X,Y). father(bill,jill). mother(jill,sam). father(bob,sam). factorial(0,1).  N and M, factorial(N-1,M)  factorial(N,N*M). Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

First-Order Predicate Calculus: statements Symbols in statements: Constants (a.k.a. atoms) numbers (e.g., 0 ) or names (e.g., bill ). Predicates Boolean functions (true/false). Can have arguments. (e.g. parent(X,Y) ). Functions non-Boolean functions ( successor(X) ). Variables e.g., X. Connectives (operations) and, or, not implication (  ):a  b (b or not a) equivalence (  ) : a  b (a  b and b  a) Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

First-Order Predicate Calculus: statements (cont’d) Quantifiers universal quantifier "for all“  existential quantifier "there exists"  bound variable (a variable introduced by a quantifier) free variable Punctuation symbols parentheses (for changing associativity and precedence.) comma period Arguments to predicates and functions can only be terms: – Contain constants, variables, and functions. – Cannot have predicates, qualifiers, or connectives. Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Problem Solving Program = Data + Algorithms Program = Object.Message(Object) Program = Functions Functions Algorithm = Logic + Control The holy grail: we specify the logic itself, the system proves. – Not totally realized by logic programming languages. Programmers must be aware of how the system proves, in order to write efficient, or even correct programs. Prove goals from facts: – Resolution and Unification Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Programmers: facts/axioms/statements Logic programming systems: prove goals from axioms

Horn Clause First-order logic too complicated for an effective logic programming system. Horn Clause: a fragment of first-order logic b  a1 and a2 and a3 … and an. b .  b. Variables in head: universally quantified Variables in body only: existentially quantified Need “or” in head? Multiple clauses Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, head bodyno “or” and no quantifier fact query

Horn Clauses: Example First-Order Logic: natural(0).  X, natural(X)  natural(successor(x)). Horn Clause: natural(0). natural(successor(x))  natural(X). Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Horn Clauses: Example First-Order Logic: factorial(0,1).  N and  M, factorial(N-1,M)  factorial(N,N*M). Horn Clause: factorial(0,1). factorial(N,N*M)  factorial(N-1,M). Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Horn Clauses: Example Horn Clause: ancestor(X,Y)  parent(X,Y). ancestor(A,C)  ancestor(A,B) and ancestor(B,C). parent(X,Y)  mother(X,Y). parent(X,Y)  father(X,Y). father(bill,jill). mother(jill,sam). father(bob,sam). Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Horn Clauses: Example First-Order Logic:  X, mammal(X)  legs(X,2) or legs(X,4). Horn Clause: legs(X,4)  mammal(X) and not legs(X,2). legs(X,2)  mammal(X) and not legs(X,4). Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Prolog syntax :- for , for and ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- ancestor(X,Z), ancestor(Z,Y). parent(X,Y) :- mother(X,Y). parent(X,Y) :- father(X,Y). father(bill,jill). mother(jill,sam). father(bob,sam). Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, Resolution and Unification

Resolution Resolution: Using a clause, replace its head in the second clause by its body, if they “match”. a  a 1, …, a n. b  b 1, …, b i, …, b m. if b i matches a; b  b 1, …, a 1, …, a n, …, b m. Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Resolution: Another view Resolution: Combine two clauses, and cancel matching statements on both sides. a  a 1, …, a n. b  b 1, …, b i, …, b m. a, b  a 1, …, a n,b 1, …, b i, …, b m. Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Problem solving in logic programming systems Program: – Statements/Facts (clauses). Goals: – Headless clauses, with a list of subgoals. Problem solving by resolution: – Matching subgoals with the heads in the facts, and replacing the subgoals by the corresponding bodies. – Cancelling matching statements. – Recursively do this, till we eliminate all goals. (Thus original goals proved.) Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Example Program: mammal(human). Goal:  mammal(human). Proving: mammal(human)  mammal(human). . Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Example Program: legs(X,2)  mammal(X), arms(X,2). legs(X,4)  mammal(X), arms(X,0). mammal(horse). arms(horse,0). Goal:  legs(horse,4). Proving: ? Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Unification Unification: Pattern matching to make statements identical (when there are variables). Set variables equal to patterns: instantiated. In previous example: legs(X,4) and legs(horse,4) are unified. (X is instantiated with horse.) Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Unification: Example Euclid’s algorithm for greatest common divisor Program: gcd(U,0,U). gcd(U,V,W)  not zero(V), gcd(V, U mod V, W). Goals:  gcd(15,10,X). Goald Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Things unspecified The order to resolve subgoals. The order to use clauses to resolve subgoals. Possible to implement systems that don’t depend on the order, but too inefficient. Thus programmers must know the orders used by the language implementations. (Search Strategies) Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,

Example Program: ancestor(X,Y) :- ancestor(X,Z), parent(Z,Y). ancestor(X,Y) :- parent(X,Y). parent(X,Y) :- mother(X,Y). parent(X,Y) :- father(X,Y). father(bill,jill). mother(jill,sam). father(bob,sam). Goals:  ancestor(bill,sam).  ancestor(X,bob). Lecture 21 – Prolog, Spring 2008 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li,