Logic Programming CSC 358/458 5.13.2003. Outline Pattern matching Unification Logic programming.

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.
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.
Some Prolog Prolog is a logic programming language
First-Order Logic.
Russell and Norvig Chapter 7
SLD-resolution Introduction Most general unifiers SLD-resolution
Chapter 11 :: Logic Languages
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
Lecture 11: Datalog Tuesday, February 6, Outline Datalog syntax Examples Semantics: –Minimal model –Least fixpoint –They are equivalent Naive evaluation.
Standard Logical Equivalences
Inference in first-order logic Chapter 9. Outline Reducing first-order inference to propositional inference Unification Generalized Modus Ponens Forward.
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.
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.
Methods of Proof Chapter 7, second half.. Proof methods Proof methods divide into (roughly) two kinds: Application of inference rules: Legitimate (sound)
Methods of Proof Chapter 7, Part II. Proof methods Proof methods divide into (roughly) two kinds: Application of inference rules: Legitimate (sound) generation.
Logic CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
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/
Outline Recap Knowledge Representation I Textbook: Chapters 6, 7, 9 and 10.
CSE (c) S. Tanimoto, 2008 Propositional Logic
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Inference and Resolution for Problem Solving
Methods of Proof Chapter 7, second half.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Logic Programming Languages
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
CS 321 Programming Languages and Compilers Prolog part 2.
ICAPRG301A Week 4Buggy Programming ICAPRG301A Apply introductory programming techniques Program Bugs US Navy Admiral Grace Hopper is often credited with.
1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Advanced Topics in Propositional Logic Chapter 17 Language, Proof and Logic.
Logical Agents Logic Propositional Logic Summary
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.
ARTIFICIAL INTELLIGENCE DR. ABRAHAM AI a field of computer science that is concerned with mechanizing things people do that require intelligent.
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
The AI War LISP and Prolog Basic Concepts of Logic Programming
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Computing & Information Sciences Kansas State University Lecture 14 of 42 CIS 530 / 730 Artificial Intelligence Lecture 14 of 42 William H. Hsu Department.
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.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
PROGRAMMING LANGUAGES: PROLOG, CLOJURE, F# Jared Wheeler.
1 The Wumpus Game StenchBreeze Stench Gold Breeze StenchBreeze Start  Breeze.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Programming Languages Third Edition Chapter 4 Logic Programming.
© Copyright 2008 STI INNSBRUCK Intelligent Systems Propositional Logic.
CSE (c) S. Tanimoto, 2008 Predicate Calculus II 1 Predicate Calculus 2 Outline: Unification: definitions, algorithm Formal interpretations and satisfiability.
CSCI 5582 Fall 2006 CSCI 5582 Artificial Intelligence Lecture 11 Jim Martin.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
1 Propositional Logic Limits The expressive power of propositional logic is limited. The assumption is that everything can be expressed by simple facts.
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.
Logical Agents Chapter 7. Outline Knowledge-based agents Propositional (Boolean) logic Equivalence, validity, satisfiability Inference rules and theorem.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Lecture 2: Unification and proof search.
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.
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Logic: Top-down proof procedure and Datalog
Chapter 11 :: Logic Languages
CS2136: Paradigms of Computation
Methods of Proof Chapter 7, second half.
Presentation transcript:

Logic Programming CSC 358/

Outline Pattern matching Unification Logic programming

Pattern matching Slade's version (A *WILD* B) matches (A B C B) return (B C) More general (A ?x B ?y B) matches (A B B C B) ?X => B ?Y => C (A ?x B ?x B) matches (A B B B B)

Bindings Pattern matcher maintains a binding list ((?X. B) (?Y. C)) Once bound a pattern variable never changes in value The rest of the target must be consistent with the bindings Matching against a variable extends bindings, or confirms consistency, or fails

Norvig code Pattern matcher variables for single values not segments different syntax for extended patterns patmatch.lsp

Unification Variables on both sides (foo ?a x) matches (foo x ?a) There is a binding that unifies the expressions ?a = x

Unify function Tree-based matches both car and cdr of pattern Attempts to unify variables regardless of where they appear

Issues Must allow variables bound to variables ?X -> ?Y Must not have circular evaluation ?X -> ?Y ?Y -> ?X Must not have recursive bindings ?X -> (F ?X) "occurs in" check

Examples

Prolog Assertions statements Rules implications Queries statements of unknown truth

Example (<- (mortal ?x) (human ?x)) (<- (human socrates)) (?- (mortal socrates)) or (? (mortal ?y))

Brief detour: macros What are <- ?- Obviously not a function arguments are not evaluated

Macro (defmacro <- (&rest clause) "add a clause to the data base." `(add-clause ',(replace-?-vars clause))) Arguments are not evaluated Can be used to build code to be evaluated

Macro expansion A macro is an arbitrary piece of Lisp code result is an expression to be evaluated Make your own syntax Examples expand <- expand OR More about macros next week

Logic Review Predicates has-professor(csc358, burke) Variables if has-professor(course, professor) and subject-of (course, topic) then knows(professor, topic) Quantification for all x > 0, there exists s such that s * s = x Implication Equivalence daughter (A, B) iff mother (B, A) or father (B, A) and female (A)

Terminology Left-hand side IF part Right-hand side THEN part Clause a statement headed by a predicate Assertion a true statement

Prolog Handles only implication no equivalence Handles only a single implicated clause No A => B and C Handles only AND clauses no OR no NOT Handles only universally-quantified variables no "there exists" Called "Horn clauses"

Backward chaining If we want to prove something is true we want to prove that is follows from what we already know Is socrates mortal? follows from rules and assertions We follow the rules backwards

Algorithm Start with item to prove if matching assertion exists, bind variables and return if not, select rules that could prove it try to prove each of these

Proof Tree Root S = statement to prove S is the consequent of some rule The rule may have several precendent parts each of these can be considered something to prove

Search problem Horn clause theorem proving is effectively search we are searching the tree of rules and assertions for something that will prove what we want to know Prolog does this depth-first order of rule definition Interestingly Horn clause theorem proving is Turing- complete

Backtracking Involves making choices testing them when they fail, re-doing the choice differently Must keep track of what choices have been tried what to do differently where the last choice was

In Prolog To prove Q we may have several rules with Q is a consequent if one rule fails to resolve, we try the next Recursive formulation since resolve a rule requires more clauses to satisfy etc.

Complexity Large rule base = more stuff to search Crucial "fan out" factor how many ways to prove something

Programming with Prolog Many basic Lisp functions can be defined member length Relational rather than functional

Knowledge Representation Prolog requires that we be declarative We have to have a language for representing our problem Prolog doesn't help you write a good language Good language = difference between computable and not We have to have rules that will answer our queries Easy to write bad rules

How to build a language Select primitives the most basic facts to be represented from which other useful things can be inferred Select more complex predicates more like the queries you want to answer Write rules that relate complex to simple

Exercise Knowing Prof. Burke teaches CSC 358 CSC 358 is a CTI course Professors teach courses associated with their home department Prove Prof. Burke has CTI as his home department

Example I married a widow who has a grown-up daughter. My father, who visited us often, fell in love with my step- daughter and married her. Hence my father became my son-in-law and my step-daughter became my mother. Some months later, my wife gave birth to a son, who became the brother-in-law of my father, as well as my uncle. The wife of father, that is, my step- daughter also had a son. I'm my own grandpa.

Frustrations Numbers pure Prolog doesn't have math operations no relational operators like >. Control pure Prolog only has one programmer- defined control over evaluation: order of definition badly written rules can create infinite loops Evaluation pure Prolog has no notion of value beyond variable binding

Hard to derive primitives (abduction) Knowing that Beatrice is someone's aunt can't infer that she is female even though this information is in the rules, sort of

Only backward chaining Sometime it is desirable to make inference in both directions Example Mammals

Real Prolog Is a modern programming language Has mechanisms for math operations Has data structures Has some control structures Cut (max example)

Constraint Logic Programming Next-generation logic programming paradigm allows the assertion of constraints a solution must satisfy Example Sudoku puzzle 1..9 can only appear once in each row very cumbersome in Prolog simple as a constraint

Homework #5 1. Solve a logic puzzle 2. Reason about cards