CS 2104 – Prog. Lang. Concepts Final wrap-up Dr. Abhik Roychoudhury School of Computing.

Slides:



Advertisements
Similar presentations
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Advertisements

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Prolog.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Logic Programming (cont’d): Lists Lists in Prolog are represented by a functor (similar to cons in Scheme): 1.The empty list is represented by the constant.
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).
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
Prolog OR (disjunction) “;” is same as a logical OR “;” is same as a logical OR It is also equivalent to using separate clauses... It is also equivalent.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
1 Prolog II. 2 The Notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification is kind of like.
Part 1 The Prolog Language Chapter 5 Controlling Backtracking
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10 Exercises –Solutions to Exercises of LPN chapter 9 Theory –Explain how to control Prolog`s.
LING 388 Language and Computers Lecture 2 9/04/03 Sandiway FONG.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
11-Jun-15 Prolog II Unification and clause order.
1 Non-determinism and Cut  Non-determinism  Backtracking  Controlling Backtracking.
 x (x 2  0) 1. True2. False.  x (3x + 2 = 12) 1. True2. False.
Prolog Ross (Tate). Filling in the Blanks Rather than reverse((a,b)) returns (b,a) Rather than reverse((a,b)) returns (b,a) What X makes reverse((a,b),
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 28: Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
Instructor: Dr. Sahar Shabanah Fall Lectures ST, 9:30 pm-11:00 pm Text book: M. T. Goodrich and R. Tamassia, “Data Structures and Algorithms in.
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 7 Implementing Prolog unification, backtracking with coroutines Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction.
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.
CS 2104 – Prog. Lang. Concepts Logic Programming - II Dr. Abhik Roychoudhury School of Computing.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 7 Implementing Prolog unification, backtracking with coroutines Ras Bodik with Mangpo and Ali Hack Your Language! CS164: Introduction to Programming.
Logic Programming (cont’d): Lists Lists in Prolog are represented by a functor (similar to cons in Scheme): 1.The empty list is represented by the constant.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
Prolog Programming. 2 DATA STRUCTURES IN PROLOG PROGRAMMING TECHNIQUES CONTROL IN PROLOG CUTS.
1 What we will learn today…  How to divide polynomials and relate the result to the remainder and factor theorems  How to use polynomial division.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
CSE S. Tanimoto Horn Clauses and Unification 1 Horn Clauses and Unification Propositional Logic Clauses Resolution Predicate Logic Horn Clauses.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Logic Programming (cont’d): Lists Lists in Prolog are represented by a functor (similar to cons in Scheme): 1.The empty list is represented by the constant.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
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.
Lab Lecture#4 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
Principles of Intelligent Systems Constraint Satisfaction + Local Search Written by Dr John Thornton School of IT, Griffith University Gold Coast.
Cs774 (Prasad)L6Backtracking1 Controlling Backtracking : Cuts
1 Week 5 Questions / Concerns What’s due: Lab2 part a due on Sunday Lab1 check-off by appointment Test#1 HW#5 next Thursday Coming up: Lab3 Posted. Discuss.
Prolog Unification and clause order. The notion of Unification Unification is when two things “become one” Unification is kind of like assignment Unification.
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.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
23-Feb-16 Prolog II Unification and clause order.
From Conventional Languages to Prolog –What we can do in conventional languages but not in Prolog –What we can do in Prolog but not in conventional languages.
COP4020 Programming Languages Logic Programming Prof. Xin Yuan.
Logic Programming Lecture 2: Unification and proof search.
Prolog Programming.
CS 315 Data Structures B. Ravikumar Office: 116 I Darwin Hall Phone:
Logic Programming (cont’d): Lists
Prolog: cut & fail © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Unification and clause order
Programming Paradigms and Languages
Homework Reading Machine Projects Labs
Merge Sort Procedure MergeSort (L = a1, a2, , an) if (n > 1)
Logic Programming Part 3: Control Flow
Controlling Backtracking : Cuts
Presentation transcript:

CS 2104 – Prog. Lang. Concepts Final wrap-up Dr. Abhik Roychoudhury School of Computing

Cuts A cut prunes an explored part of a Prolog search tree. a(1):-b. a(2):-e. b:- c. b:-d. d. e. ?- a(X). X = 1 ; X = 2 ; no Cut in a clause commits to the use of that clause. Cut has the effect of making b fails if c fails. X=2 X=1 a(X) b e c d X=1 X=2 !, c ?- a(X). X = 2 ; no

a(X):-b(X). a(X) :-f(X). b(X):-g(X), v(X). b(X):-X = 4, v(X). g(1). g(2). g(3). v(1). v(X) :- f(X). f(5). ?- a(Z). Z = 1 ; Z = 5 ; no v(3) a(Z) b(Z)f(Z) g(Z), v(Z) Z=4,v(4) backtrack v(1)f(1) Z = 1 v(1)v(2) f(2) backtrack f(3) backtrack f(4) backtrack f(5) Z = 5 !, ?- a(Z). Z = 1 ; Z = 5 ; no

Green Cuts, Red Cuts Green Cut a cut the prunes part of a Prolog search tree that cannot possibly reach a solution used mainly for efficiency sake. Red Cut a cut that alters the set of possible solutions reachable. Powerful tool, yet fatal and can be confusing Handle with care

Merging two lists merge([X|Xs],[Y|Ys],[X|Zs]) :- X <Y, merge(Xs,[Y|Ys],Zs). merge([X|Xs],[Y|Ys],[X,Y|Zs]) :- X = Y, merge(Xs,Ys,Zs). merge([X|Xs],[Y|Ys],[Y|Zs]) :- X > Y, merge([X|Xs], Ys, Zs). merge(X, [], X). merge([], Y, Y). First three clauses mutually exclusive No need to try the others, if one of them succeeds. This is made explicit by a green cut.

Example: Green cut merge([X|Xs],[Y|Ys],[X|Zs]) :- X <Y, !, merge(Xs,[Y|Ys],Zs). merge([X|Xs],[Y|Ys],[X,Y|Zs]) :- X = Y, !, merge(Xs,Ys,Zs). merge([X|Xs],[Y|Ys],[Y|Zs]) :- X > Y, !, merge([X|Xs], Ys, Zs). merge(X, [], X) :- !. merge([], Y, Y). Inserting these cuts does not change the answers to any merge query.

Example: Red Cut member(X,[X|Xs]) :- !. member(X,[Y|Ys]) :- member(X, Ys). member(1, [1,2,1,1,3]) is more efficient. But member(X, [1,2,3]) produces only one answer X = 1

Summary Prolog is a procedural language with: Assign once variables Nondeterminism As a result of having assign once variables Assignment is symmetric Test and assignment represented by same operator. Unification combines the concepts of test, assignment and pattern matching.

Summary As a result of having nondeterminism Control issues for the search Cuts (allows the programmer explcit control) Meaning of Prolog program given by queries that can be evaluated to true. Applications of Prolog Database query language Grammar Processing, Natural Language Processing

Homework 10 mergesort([], []) :- !. mergesort([A], [A]) :- !. mergesort(L, L1) :- split(L, M, N), mergesort(M, M1), mergesort(N, N1), merge(M1, N1, L1). split([], [], []). split([A], [A], []). split([A,B|Rest], [A|M], [B|N]) :- split(Rest, M, N).

Homework 10 Replacing an element in a list replace(X, Y, [X|Rest], [Y|Rest]) :- !. replace(X, Y, [Z|Rest], [Z|List]) :- replace(X, Y, Rest, List).

Homework 10 Removing duplicates in a list remdup(L, L1) :- remdup(L, [], L1). remdup([], L, L). remdup([X|Xs], SoFar, List) :- member(X, SoFar), !, remdup(Xs, SoFar, List). remdup([X|Xs], SoFar, List) :- remdup(Xs, [X|SoFar], List).

Homework 10 What happens for query find(X) find(X) :- not(p(X)). p(X) :- q(X), r(X). q(f(X)) :- q(X), X \= b. r(X) :- X \= a

Homework 10 find(X) not(p(X)) p(X) q(X), r(X) q(X1), r(f(X1)) q(X2), r(f(f(X2))) Query eval. Does not terminate …

Answers for last year’s final exam posted in course web-site. Look under -> Materials -> Other Resources

Thank You