CSE 415 -- (c) S. Tanimoto, 2005 Logic Programming 1 Logic Programming Outline: Motivation Examples: The Grandmother relation Formulation in Prolog Logic,

Slides:



Advertisements
Similar presentations
CS4026 Formal Models of Computation Part II The Logic Model Lecture 1 – Programming in Logic.
Advertisements

Chapter 11 :: Logic Languages
1 A formula in predicate logic An atom is a formula. If F is a formula then (~F) is a formula. If F and G are Formulae then (F /\ G), (F \/ G), (F → G),
Knowledge & Reasoning Logical Reasoning: to have a computer automatically perform deduction or prove theorems Knowledge Representations: modern ways of.
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.
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.
For Friday No reading Homework: –Chapter 9, exercise 4 (This is VERY short – do it while you’re running your tests) Make sure you keep variables and constants.
Chapter 12 - Logic Programming
Outline Recap Knowledge Representation I Textbook: Chapters 6, 7, 9 and 10.
Computability and Complexity 9-1 Computability and Complexity Andrei Bulatov Logic Reminder (Cnt’d)
CSE (c) S. Tanimoto, 2008 Propositional Logic
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Inference and Resolution for Problem Solving
CSE (c) S. Tanimoto, 2007 Unification 1 Unification Predicate calculus rules are sometimes so general that we need to create specializations of.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
Knowledge & Reasoning Logical Reasoning: to have a computer automatically perform deduction or prove theorems Knowledge Representations: modern ways of.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
INFERENCE IN FIRST-ORDER LOGIC IES 503 ARTIFICIAL INTELLIGENCE İPEK SÜĞÜT.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Conjunctive normal form: any formula of the predicate calculus can be transformed into a conjunctive normal form. Def. A formula is said to be in conjunctive.
1 Chapter 8 Inference and Resolution for Problem Solving.
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
CSE S. Tanimoto Horn Clauses and Unification 1 Horn Clauses and Unification Propositional Logic Clauses Resolution Predicate Logic Horn Clauses.
Logic Logic can be traced back to Aristotle, and more recently to Frege who introduced predicate logic as a language for representing and reasoning about.
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.
CS 603: Programming Languages Lecture 25 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
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.
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)
Reasoning using First-Order Logic
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
CSE (c) S. Tanimoto, 2008 Predicate Calculus II 1 Predicate Calculus 2 Outline: Unification: definitions, algorithm Formal interpretations and satisfiability.
CSE 341, S. Tanimoto Logic Programming Intro - 1 Logic Programming Introduction Motivation. Sample logic programs.
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”
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.
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
The portion of a Prolog interpreter that executes queries (goals) is known as the inference engine. An inference engine is a kind of theorem prover, using.
Logical Agents. Outline Knowledge-based agents Logic in general - models and entailment Propositional (Boolean) logic Equivalence, validity, satisfiability.
CSE (c) S. Tanimoto, 2001 Logic Programming
CS 3304 Comparative Languages
CSE 341, S. Tanimoto Logic Programming -
Prolog Concepts.
Horn Clauses and Unification
List Manipulation in Prolog
Introduction to Logic Programming and Prolog
Horn Clauses and Unification
Horn Clauses and Unification
Introduction to Logic Programming and Prolog
CSE (c) S. Tanimoto, 2004 Unification
CSE S. Tanimoto Unification
Horn Clauses and Unification
List Manipulation in Prolog
Introduction to Prolog
CSE (c) S. Tanimoto, 2002 Unification
CSE (c) S. Tanimoto, 2004 Logic Programming
Introduction to Prolog
CSE (c) S. Tanimoto, 2002 Logic Programming
Horn Clauses and Unification
Encoding Knowledge with First Order Predicate Logic
RESOLUTION.
Prolog Concepts.
Introduction to Logic Programming and Prolog
Introduction to Logic Programming and Prolog
Prolog Concepts.
Presentation transcript:

CSE (c) S. Tanimoto, 2005 Logic Programming 1 Logic Programming Outline: Motivation Examples: The Grandmother relation Formulation in Prolog Logic, clauses, and Horn clauses Unification Backtracking Search Horn-Clause Resolver List manipulation using logic

CSE (c) S. Tanimoto, 2005 Logic Programming 2 Motivation 1. Take advantage of automated logical inference to solve problems. 2. Reduce the programming burden. 3. System should simply accept the necessary information and the objective (goal), and then figure out its own solution. 4. Have a program that looks more like its own specification.

CSE (c) S. Tanimoto, 2005 Logic Programming 3 Sample Problem For someone (call him or her X) to be the grandmother of someone else (call him or her Y), X must be the mother of someone (call him or her Z) who is a parent of Y. Someone is a parent of another person, if that someone is either the mother or the father of the other person. Mary is the mother of Stan. Gwen is the mother of Alice. Valery is the mother of Gwen. Stan is the father of Alice. The question: Who is a grandmother of Alice?

CSE (c) S. Tanimoto, 2005 Logic Programming 4 Sample Prolog Program: grandmother.pl grandmother(X, Y) :- mother(X, Z), parent(Z, Y). parent(X, Y) :- mother(X, Y). parent(X, Y) :- father(X, Y). mother(mary, stan). mother(gwen, alice). mother(valery, gwen). father(stan, alice).

CSE (c) S. Tanimoto, 2005 Logic Programming 5 Sample Prolog Session Welcome to SWI-Prolog (Version 3.3.2) Copyright (c) University of Amsterdam. All rights reserved. For help, use ?- help(Topic). or ?- apropos(Word). ?- [grandmother]. % grandmother compiled 0.00 sec, 1,312 bytes Yes ?- grandmother(X, alice). X = mary ; X = valery ; No ?-

CSE (c) S. Tanimoto, 2005 Logic Programming 6 Steps in Writing a Logic Program 1. Formulate assertions in English and translate them into Logic, then into Prolog or 2. Program directly in Prolog or Mock Prolog.

CSE (c) S. Tanimoto, 2005 Logic Programming 7 Predicate Logic “Every Macintosh computer uses electricity.” (all x) (Macintosh(x) implies UsesElectricity(x)) variables: x, y, z, etc. constants: a, b, c, etc. function symbols: f, g, etc. Predicate symbols: P, Q, Macintosh, UsesElectricity quantifiers: forall, exists Logical connectives: not, implies, and, or. ~, ->, &, v.

CSE (c) S. Tanimoto, 2005 Logic Programming 8 Clause Form (all x) (Macintosh(x) -> UsesElectricity(x) UsesElectricity(x) v Not Macintosh(x). Any boolean formula can be put into conjunctive normal form (CNF). If not Y then X and not Z. Y or (X & not Z) (Y or X & (Y or not Z) (X v Y) & (Y v ~Z) clauses: (X v Y), (Y v ~Z) X, Y, and ~Z are called literals. Each clause is a disjunction of literals.

CSE (c) S. Tanimoto, 2005 Logic Programming 9 Horn Clauses Horn clause: at most one unnegated literal (X v Y) is not a Horn clause. (Y v ~Z) is a Horn clause. “If X is the mother of Y and Y is a parent of Z, then X is a grandmother of Z.” (m(X,Y) & p(Y, Z)) -> g(X,Z). grandmother(X, Z) provided mother(X, Y) and parent (Y, Z) g(X,Z) :- m(X,Y), p(Y,Z). ; Edinburgh Prolog syntax grandmother(X, Z) :- % head mother(X, Y), % subgoal 1 parent(Y, Z). % subgoal 2

CSE (c) S. Tanimoto, 2005 Logic Programming 10 How Does Matching Work in Prolog? Literals are matched using a method called unification. The scope of a variable in Prolog is a single clause. Unification involves substituting “terms” for variables.

CSE (c) S. Tanimoto, 2005 Logic Programming 11 Unification of Literals A substitution is a set of term/variable pairs. { f(a)/x, b/y, z/w } A unifier for a pair of literals is a substitution that when applied to both literals, makes them identical. P(x, a), P(f(a), y) have the unifier  = { f(a)/x, a/y } because P(x, a)  = P(f(a), a) P(f(a), y)  = P(f(a), a) Here L  means the result of applying all of the elementary substitutions in  to the literal L.

CSE (c) S. Tanimoto, 2005 Logic Programming 12 Unification of Literals (cont) P(x), P(y) have the unifier { a/x, a/y }, but they also have the unifier { x/y }. The latter is more general because after unifying with { x/y} we get P(x) whereas with the other it is P(a), yet we can obtain the latter from the former with an additional substitution { a/x }.

CSE (c) S. Tanimoto, 2005 Logic Programming 13 Horn-Clause Resolution Example of resolution with the grandmother rules: g(X, Z) :- m(X, Y), p(Y, Z). m(X, Y) :- p(X, Y), f(X). g(X, Z) :- p(X, Y), f(X), p(Y, Z). more generally, from: P :- Q1, Q2. Q1 :- R1, R2. we obtain the resolvent: P :- R1, R2, Q2

CSE (c) S. Tanimoto, 2005 Logic Programming 14 Backtracking Horn-Clause Resolver Maintain the rules in a database of rules. Accept a query (goal) as a positive unit clause, e.g., P(a, b). Put this goal on the subgoal list. Repeatedly try to satisfy the next clause on the subgoal list as follows: Find a rule in the database whose head unifies with the subgoal. Apply the same unifier to the literals in the body of that rule and replace the subgoal by these new literals. If the subgoal list is empty, stop. The combined set of unifiers includes the solution.

CSE (c) S. Tanimoto, 2005 Logic Programming 15 Lists in Prolog: Transformations can be Expressed Declaratively colors([r, g, b]). ?- colors(L). L = [r, g, b] ?- colors([X|Y]). X = r, Y = [g, b] % X is the head. Y is the tail. ?- mylist([a, [b, c]]).

CSE (c) S. Tanimoto, 2005 Logic Programming 16 Defining Predicates on Lists car([X|L], X). cdr([X|L], L). cons(X, L, [X|L]). ?- car([a, b, c], X). X = a ?- cdr([a, b, c], X). X = [b, c] ?- cons(a, [b, c], X). X = [a, b, c]

CSE (c) S. Tanimoto, 2005 Logic Programming 17 concat a.k.a. append concat([], L, L). concat([X|L1], L2, [X|L3]) :- concat(L1, L2, L3). ?- concat([a, b, c], [d, e], X). X = [a, b, c, d, e] ?- concat(X, [d, e], [a, b, c, d, e]). X = [a, b, c]

CSE (c) S. Tanimoto, 2005 Logic Programming 18 concat used backwards ?- concat(X, Y, [x, y, z]). X = [] Y = [x, y, z] ; X = [x] Y = [y, z] ; X = [x, y] Y = [z] ; X = [x, y, z] Y = [] ; No

CSE (c) S. Tanimoto, 2005 Logic Programming 19 Logic Prog. vs Functional Prog. Functional programming: evaluation is one-way. Logic programming: evaluation can be two-way.