COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.

Slides:



Advertisements
Similar presentations
Formal Models of Computation Part II The Logic Model
Advertisements

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.
Prolog.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
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.
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.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Logic programming and Prolog Prof. Xin Yuan.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Programming Languages Third Edition
(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
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
Formal Models of Computation Part II The Logic Model
The ACL2 Proof Assistant Formal Methods Jeremy Johnson.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
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.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
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?
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.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
© 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.
Lists in Prolog Sections 3.1, 3.2. Lists n List = sequence of values –[1, 2, 3, 4, 5] –[bob, brian, cathy, mark, david, loretta] –[birds(4, calling),
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
ISBN Chapter 16 Logic Programming Languages.
Knowledge Based Information System
COP4020 Programming Languages Prolog Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
1 Artificial Intelligence CS370D Prolog programming List operations.
For Friday No reading Prolog Handout 2. Homework.
LECTURE 21 Logic Programming. LOGIC PROGRAMMING RESOLUTION STRATEGIES.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
C H A P T E R N I N E Logic Programming Part 2 Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
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.
COP4020 Programming Languages Logic Programming Prof. Xin Yuan.
C. Varela1 Logic Programming (PLP 11, CTM 9.3) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not Lists, Append Carlos Varela Rennselaer Polytechnic.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Copyright © 2009 Elsevier Chapter 11 :: Logic Languages And other online sources (see links on schedule page) Michael L. Scott Based on Programming Language.
Prolog a declarative language
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 17, 2017
Tests, Backtracking, and Recursion
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Chapter 11 :: Logic Languages
COP4020 Programming Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Chapter 12 :: Logic Languages
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Chapter 12 :: Logic Languages
PROLOG.
Presentation transcript:

COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan

COP4020 Spring /12/2015 Topics Logic programming with Prolog

COP4020 Spring /12/2015 Definitions: Prolog Terms Terms are symbolic expressions that are Prolog’s building blocks A Prolog program consists of Horn clauses (axioms) that consist of terms Data structures processed by a Prolog program are terms A term is either  a variable: a name beginning with an upper case letter  a constant: a number or string  an atom: a symbol or a name beginning with a lower case letter  a structure of the form: functor(arg 1, arg 2,..., arg n ) where functor is an atom and arg i are sub-terms  a list (also a structure with a functor) of the form [term 1, term 2, …, term n ] Examples:  X, Y, ABC, and Alice are variables  7, 3.14, and ”hello” are constants  foo, barFly, and + are atoms  bin_tree(foo, bin_tree(bar, glarch)) and +(3,4) are structures

COP4020 Spring /12/2015 Term Manipulation Terms can be analyzed and constructed  Built-in predicates functor and arg, for example: ?- functor(foo(a,b,c), foo, 3). yes ?- functor(bar(a,b,c), F, N). F = bar N = 3 ?- functor(T, bee, 2). T = bee(_G1,_G2) ?- functor(T, bee, 2), arg(1, T, a), arg(2, T, b). T = bee(a,b)  The “univ” operator =.. Break up a term into a list or form a term from a list.  ?- foo(a,b,c) =.. L L = [foo,a,b,c]  ?- T =.. [bee,a,b] T = bee(a,b)

COP4020 Spring /12/2015 Unification and Variable Instantiation Variables are used in clauses: A variable is instantiated to a term as a result of unification, which takes place when goals are matched to head predicates  Goal in query: rainy(C)  Fact: rainy(seattle)  Unification is the result of the goal-fact match: C=seattle Unification is recursive:  An uninstantiated variable unifies with anything, even with other variables which makes them identical (aliases)  An atom unifies with an identical atom  A constant unifies with an identical constant  A structure unifies with another structure if the functor and number of arguments are the same and the arguments unify recursively Once a variable is instantiated to a non-variable term, it cannot be changed: “proofs cannot be tampered with”

COP4020 Spring /12/2015 Examples of Unification The built-in predicate = (A,B) succeeds if and only if A and B can be unified, where the goal = (A,B) may be written as A = B  ?- a = a. yes  ?- a = 5. No  ?- 5 = 5.0. No  ?- a = X. X = a  ?- foo(a,b) = foo(a,b). Yes  ?- foo(a,b) = foo(X,b). X = a  ?- foo(X,b) = Y. Y = foo(X,b)  ?- foo(Z,Z) = foo(a,b). no

COP4020 Spring /12/2015 Prolog Lists A list is of the form: [elt 1,elt 2,..., elt n ] where elt i are terms The special list form [elt 1,elt 2,..., elt n | tail] denotes a list whose tail list is tail Examples  ?- [a,b,c] = [a|T]. T = [b,c]  ?- [a,b,c] = [a,b|T]. T = [c]  ?- [a,b,c] = [a,b,c|T]. T = []

COP4020 Spring /12/2015 List Operations: List Membership List membership definitions: member(X, [X|T]). member(X, [H|T]) :- member(X, T). ?- member(b, [a,b,c]).  Execution: member(b,[a,b,c]) does not match member(X,[X|T])  member(b,[a,b,c]) matches predicate member(X 1,[H 1 |T 1 ]) with X 1 =b, H 1 =a, and T 1 =[b,c]  Sub-goal to prove: member(b, [b,c])  member(b,[b,c]) matches predicate member(X 2,[X 2 |T 2 ]) with X 2 =b and T 2 =[c]  The sub-goal is proven, so member(b,[a,b,c]) is proven (deduced)  Note: variables can be "local" to a clause (like the formal arguments of a function)  Local variables such as X 1 and X 2 are used to indicate a match of a (sub)-goal and a head predicate of a clause

COP4020 Spring /12/2015 Predicates and Relations Predicates are not functions with distinct inputs and outputs Predicates are more general and define relationships between objects (terms)  member(b,[a,b,c]) relates term b to the list that contains b  ?- member(X, [a,b,c]). X = a ; % type ';' to try to find more solutions X = b ; %... try to find more solutions X = c ; %... try to find more solutions no  ?- member(b, [a,Y,c]). Y = b  ?- member(b, L). L = [b|_G255] where L is a list with b as head and _G255 as tail, where _G255 is a new variable

COP4020 Spring /12/2015 Predicates and Relations ?- member(b, L). L = [b|_G545] ; L = [_G544, b|_G548] ; L = [_G544, _G547, b|_G551] ; L = [_G544, _G547, _G550, b|_G554] ; L = [_G544, _G547, _G550, _G553, b|_G557] ; L = [_G544, _G547, _G550, _G553, _G556, b|_G560] ; L = [_G544, _G547, _G550, _G553, _G556, _G559, b|_G563].

COP4020 Spring /12/2015 Example: List Append L3 = L1 || L2. How to write this in Prolog? Predicates do not have return value  Implication? L1, L2, L3 must be arguments to a predicate, the programming will then specify the relation.  Append(L1, L2, L3). Define this recursively  Case 1: when L1 is empty  Case 2: when L1 is not empty  Prolog has not if constructs, how to do two cases?

COP4020 Spring /12/2015 Example: List Append Append(L1, L2, L3). %L3 = L1 || L2 Define this recursively  Case 1: when L1 is empty append([], L2, L2).  Case 2: when L1 is not empty append([H | T], L2, [H | append(T, L2) ]) – of course this is incorrect, append does not have a return value. Solution:  append ([H | T], L2, [H | L]) :- append(T, L2, L). Final solution: append([], A, A). append([H|T], A, [H|L]) :- append(T, A, L).

COP4020 Spring /12/2015 Example: List Append List append predicate definitions: append([], A, A). append([H|T], A, [H|L]) :- append(T, A, L). Prolog append is more power then the append function in other languages ?- append([a,b,c], [d,e], X). X = [a,b,c,d,e] ?- append(Y, [d,e], [a,b,c,d,e]). Y = [a,b,c] ?- append([a,b,c], Z, [a,b,c,d,e]). Z = [d,e] ?- append([a,b],[],[a,b,c]). No ?- append([a,b],[X|Y],[a,b,c]). X = c Y = []

Example: take out one element from a list predicate prototype: takeout(Item, SrcL, DstL) Three cases:  SrcL is empty  SrcL is not empty: the first element is Item (take out this item)  SrcL is not empty: the first element is not Item (Keep this item) COP4020 Spring /12/2015

Example: take out one element from a list Example6.pl Item may or may not be in SrcL  Three cases: SrcL is empty:  takeout(Item, [], []). SrcL is not empty: the first element is Item (take out this item)  takeout(Item, [Item | L], L). SrcL is not empty: the first element is not Item (Keep this item)  Takeout(Item, [X | L], [X | L1]) :- takeout(Item, L, L1). Item must be in SrcL? COP4020 Spring /12/2015

Permutation of a list perm(L1, L2). %L2 is a permutation of L1 perm([], []). perm([X|Y], Z) :- perm(Y, W), takeout(X, Z, W). Example7.pl COP4020 Spring /12/2015

Example: sorting Interface: sort(List, Sortedlist).  All variables must start with a capital letter. Naïve_sort (very inefficient sorting) naive_sort(List,Sorted):-perm(List,Sorted),is_sorted(Sorted). How to write is_sorted? COP4020 Spring /12/2015

Example: sorting How to write is_sorted? is_sorted([]). is_sorted([_]). is_sorted([X, Y | L]) :- X < Y, is_sorted([Y|L]). COP4020 Spring /12/2015