Introduction to Logic Programming with Prolog (Section 11.3)

Slides:



Advertisements
Similar presentations
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.
Advertisements

Inference Rules Universal Instantiation Existential Generalization
Chapter 11 :: Logic Languages
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming with Prolog: Resolution, Unification, Backtracking COMP 144 Programming Language Concepts Spring 2003 Stotts, Hernandez-Campos Modified.
Logic Languages Source: Scott, Michael Programming Language Pragmatics 3rd Ed.
COP4020 Programming Languages Logic programming and Prolog Prof. Xin Yuan.
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.
Programming Languages Third Edition
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 26: Introduction to Logic Programming with Prolog COMP 144 Programming Language.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
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.
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.
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.
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.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
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 Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
The AI War LISP and Prolog Basic Concepts of Logic Programming
By: Cory Canter CSC 415 Programming Languages. History  Created by Alain Colmerauer, Phillipe Roussel and Robert Kowalski in 1971  Started as a natural.
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.
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)
© 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.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Logic Programming: Prolog Lecture 21 – Prolog, Spring CSE3302 Programming Languages, UT-Arlington.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
Knowledge Based Information System
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
1 Announcements We are almost done grading HW1 HW2 due today Download SWI Prolog! HW3 (Prolog) will be posted today, due on February 29 th Spring 16 CSCI.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
Logic Programming Lecture 2: Unification and proof search.
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Carlos Varela Rennselaer Polytechnic Institute November 11, 2016
Knowledge Representation and Reasoning
CS 3304 Comparative Languages
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Carlos Varela Rensselaer Polytechnic Institute November 14, 2017
Logic Programming with Prolog: Resolution, Unification, Backtracking
Tests, Backtracking, and Recursion
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Logic Programming Languages
Chapter 11 :: Logic Languages
Back to “Serious” Topics…
Chapter 12 :: Logic Languages
Logic Programming Language
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Presentation transcript:

Introduction to Logic Programming with Prolog (Section 11.3) CSCI 431 Programming Languages Fall 2003 Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill

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

Resolution Horn clauses can capture most logical statements But not all The derivation of new statements is known as resolution The logic programming system combines existing statements to find new statements For instance, C  A, B D  C D  A, B A and B imply C If we know that A and B imply C, and that C implies D, then we can deduce that A and B imply D

Predicate Applied to a Variable Predicate Applied to an Atom Example flowery(X)  rainy(X) rainy(Rochester) flowery(Rochester) Variable Predicate Applied to a Variable Free Variable X acquired value Rochester during the resolution This is known as Unification Predicate Applied to an Atom

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,…

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.

Asks the interpreter to 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(sam, curry). No ?- likes(sam, X). X = dahl ; X = tandoori ; X = kurma ; 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 Program Query 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. curry, 4.5, +, ‘Hi, Mom’ Variables (identifiers starting with an uppercase letter) E.g. Food Structures (predicates or data structures) E.g. indian(Food), 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)) Functors 3 2 5

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,Food) :- indian(Food), mild(Food). C2: indian(dahl). C3: mild(dahl). We can replace the first and the second terms in C1 by C2 and C3 using the principle of resolution (after instantiating variable Food to dahl) Therefore, likes(sam, dahl) can be proved

Another Example takes(jane_doe, his201). takes(jane_doe, cs245). takes(ajit_chandra, art302). takes(ajit_chandra, cs254). classmates(X, Y) :- takes(X, Z), takes(Y, Z). via resolution, yields the new rule: classmates(jane_doe, Y) :- takes(Y, cs254).

Unification Prolog associates variables and values using a process known as unification Variable that receive a value are said to be instantiated Unification rules A constant unifies only with itself Two structures unify if and only if they have the same functor and the same number of arguments, and the corresponding arguments unify recursively A variable unifies to with anything

Equality Equality is defined as unifiability For instance, An equality goal is using an infix predicate = For instance, ?- dahl = dahl. Yes ?- dahl = curry. No ?- likes(Person, dahl) = likes(sam, Food). Person = sam Food = dahl ; ?- likes(Person, curry) = likes(sam, Food). Food = curry ;

Equality What is the results of Internal Representation for an ?- likes(Person, Food) = likes(sam, Food). Person = sam Food = _G158 ; No Internal Representation for an uninstantiated variable Any instantiation proves the equality

Execution Order Prolog searches for a resolution sequence that satisfies the goal In order to satisfy the logical predicate, we can imagine two search strategies: Forward chaining, derived the goal from the axioms Backward chaining, start with the goal and attempt to resolve them working backwards Backward chaining is usually more efficient, so it is the mechanism underlying the execution of Prolog programs Forward chaining is more efficient when the number of facts is small and the number of rules is very large

Backward Chaining in Prolog Backward chaining follows a classic depth-first backtracking algorithm Example Goal: Snowy(C)

Depth-first backtracking The search for a resolution is ordered and depth-first The behavior of the interpreter is predictable Ordering is fundamental in recursion Recursion is again the basic computational technique, as it was in functional languages Inappropriate ordering of the terms may result in non-terminating resolutions (infinite regression) For example: Graph edge(a,b). edge(b, c). edge(c, d). edge(d,e). edge(b, e). edge(d, f). path(X, X). path(X, Y) :- edge(Z, Y), path(X, Z). Correct

Depth-first backtracking The search for a resolution is ordered and depth-first The behavior of the interpreter is predictable Ordering is fundamental in recursion Recursion is again the basic computational technique, as it was in functional languages Inappropriate ordering of the terms may result in non-terminating resolutions (infinite regression) For example: Graph edge(a,b). edge(b, c). edge(c, d). edge(d,e). edge(b, e). edge(d, f). path(X, Y) :- path(X, Z), edge(Z, Y). path(X, X). Incorrect

Infinite Regression Goal

Examples Genealogy Data structures and arithmetic http://ktiml.mff.cuni.cz/~bartak/prolog/genealogy.html Data structures and arithmetic Prolog has an arithmetic functor is that unifies arithmetic values E.g. is (X, 1+2), X is 1+2 Dates example http://ktiml.mff.cuni.cz/~bartak/prolog/basic_struct.html