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.

Slides:



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

SLD-resolution Introduction Most general unifiers SLD-resolution
Prolog.
CSA2050: DCG I1 CSA2050 Introduction to Computational Linguistics Lecture 8 Definite Clause Grammars.
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.
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.
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.
Closure Properties of CFL's
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Answer Set Programming Overview Dr. Rogelio Dávila Pérez Profesor-Investigador División de Posgrado Universidad Autónoma de Guadalajara
CS 2104 – Prog. Lang. Concepts Final wrap-up Dr. Abhik Roychoudhury School of Computing.
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
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.
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
Ofer Strichman, Technion Deciding Combined Theories.
Context-Free Grammar CSCI-GA.2590 – Lecture 3 Ralph Grishman NYU.
Induction and recursion
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
Formal Models of Computation Part II The Logic Model
The Theory of NP-Completeness 1. What is NP-completeness? Consider the circuit satisfiability problem Difficult to answer the decision problem in polynomial.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
The ACL2 Proof Assistant Formal Methods Jeremy Johnson.
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.
DECIDABILITY OF PRESBURGER ARITHMETIC USING FINITE AUTOMATA Presented by : Shubha Jain Reference : Paper by Alexandre Boudet and Hubert Comon.
1st-order Predicate Logic (FOL)
CSCI 2670 Introduction to Theory of Computing September 20, 2005.
Declarative vs Procedural Programming  Procedural programming requires that – the programmer tell the computer what to do. That is, how to get the output.
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.
1 Functional Programming Lecture 6 - Algebraic Data Types.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures defining.
1 The Theory of NP-Completeness 2 Cook ’ s Theorem (1971) Prof. Cook Toronto U. Receiving Turing Award (1982) Discussing difficult problems: worst case.
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.
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.
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.
Full Logic Programming. Data structures Pure LP allows only to represent relations (=predicates) To obtain full LP we will add functors (=function symbols)
Curry A Tasty dish? Haskell Curry!. Curried Functions Currying is a functional programming technique that takes a function of N arguments and produces.
1 First order theories (Chapter 1, Sections 1.4 – 1.5) From the slides for the book “Decision procedures” by D.Kroening and O.Strichman.
1 Section 13.1 Turing Machines A Turing machine (TM) is a simple computer that has an infinite amount of storage in the form of cells on an infinite tape.
1 Turing Machines and Equivalent Models Section 13.1 Turing Machines.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
Chapter Nine Predicate Logic Proofs. 1. Proving Validity The eighteen valid argument forms plus CP and IP that are the proof machinery of sentential logic.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
C. Varela1 Logic Programming (PLP 11, CTM 9.3) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not Lists, Append Carlos Varela Rennselaer Polytechnic.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures (predicates),
 2004 SDU Lecture8 NON-Context-free languages.  2004 SDU 2 Are all languages context free? Ans: No. # of PDAs on  < # of languages on  Pumping lemma:
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Logic Programming: Introduction
Introduction to Logic for Artificial Intelligence Lecture 2
Logic Programming (cont’d): Lists
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
Logics for Data and Knowledge Representation
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
PROGRAMMING IN HASKELL
Curry A Tasty dish? Haskell Curry!.
Lecture 7: Definite Clause Grammars
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Presentation transcript:

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 - [ ]. 2.The term.(X, Xs) represents a list of which X is the 1 st element and Xs is the rest of its elements. 3.The functor ‘. ’ has an infix representation by the pipe symbol ‘|’ : [X | Xs] For example: [3 | [5 | [] ] ] 4.Lists of known length can be represented as a list of comma-separated expressions: For example: [ X, 2, f(Y) ] or [3, 5]

Logic Programming (cont’d): Lists Example 1: Implementing a Context Free Grammar in PROLOG: NP VP Det N V NP | V a | the woman | man saw S NP VP Det N V Q: What language does this CFG represent? s(Z) :- np(X),vp(Y),append(X,Y,Z). np(Z):- det(X),n(Y),append(X,Y,Z). vp(Z):- v(X),np(Y),append(X,Y,Z). vp(Z):- v(Z). det([the]). det([a]). n([woman]). n([man]). v([saw]). s(Z) :- np(X),vp(Y),append(X,Y,Z). np(Z):- det(X),n(Y),append(X,Y,Z). vp(Z):- v(X),np(Y),append(X,Y,Z). vp(Z):- v(Z). det([the]). det([a]). n([woman]). n([man]). v([saw]). Representation in PROLOG: ?- s([a,woman,saw,a,man]). true ?-s(X). X = [the,woman,saw,the,woman]; X = [the,woman,saw,the,man]; X = [the,woman,saw,a,woman]; X = [the,woman,saw,a,man]; X = [the,woman,saw] … ?-s([the,man|X]). X = [saw,the,woman]; X = [saw,the,man]; X = [saw,a,woman] …

Logic Programming (cont’d): Lists Example 1: Implementing a Context Free Grammar in PROLOG: NP VP Det N | Det Adj N vicious | marvelous V NP | V a | the woman | man saw S NP Adj VP Det N V Representation in PROLOG: Q: How would we implement the modifications above? s(Z) :- np(X),vp(Y),append(X,Y,Z). np(Z):- det(X),n(Y),append(X,Y,Z). vp(Z):- v(X),np(Y),append(X,Y,Z). vp(Z):- v(Z). det([the]). det([a]). n([woman]). n([man]). v([shoots]). s(Z) :- np(X),vp(Y),append(X,Y,Z). np(Z):- det(X),n(Y),append(X,Y,Z). vp(Z):- v(X),np(Y),append(X,Y,Z). vp(Z):- v(Z). det([the]). det([a]). n([woman]). n([man]). v([shoots]). s(Z) :- np(X),vp(Y),append(X,Y,Z). np(Z):- det(X),n(Y),append(X,Y,Z). np(Z):- det(X),adj(W),n(Y),append([X,W,Y],Z). adj([vicious]). adj([marvelous]). vp(Z):- v(X),np(Y),append(X,Y,Z). vp(Z):- v(Z). det([the]). det([a]). n([woman]). n([man]). v([shoots]). s(Z) :- np(X),vp(Y),append(X,Y,Z). np(Z):- det(X),n(Y),append(X,Y,Z). np(Z):- det(X),adj(W),n(Y),append([X,W,Y],Z). adj([vicious]). adj([marvelous]). vp(Z):- v(X),np(Y),append(X,Y,Z). vp(Z):- v(Z). det([the]). det([a]). n([woman]). n([man]). v([shoots]).

Logic Programming (cont’d): Lists Example 2: Determining the order of specific times o In logic programming, symbols have no values: ‘2’ and ‘9’ are names and not numbers. o We cannot use the < relation to compare the order of numbers (it is infinite!). o However, a finite ordered relation may be represented by positions in lists. We would like to implement an ordered relation between pairs of the form (hour, weekday). % Type: Hour. % Constructor: functor h/1. % Signature: h(Hour)/1 % Example: h(18). % Identifier: is_hour/1 % Set of elements: hour_list/1 % Order: hour_order/2 % Type: Hour. % Constructor: functor h/1. % Signature: h(Hour)/1 % Example: h(18). % Identifier: is_hour/1 % Set of elements: hour_list/1 % Order: hour_order/2 % Type: Weekday. % Constructor: functor d/1. % Signature: d(Weekday)/1 % Example: d(Tue). % Identifier: is_weekday/1 % Set of elements: weekday_list/1 % Order: weekday_order/2 % Type: Weekday. % Constructor: functor d/1. % Signature: d(Weekday)/1 % Example: d(Tue). % Identifier: is_weekday/1 % Set of elements: weekday_list/1 % Order: weekday_order/2

Logic Programming (cont’d): Lists Example 2: Determining the order of specific times o In logic programming, symbols have no values: ‘2’ and ‘9’ are names and not numbers. o We cannot use the < relation to compare the order of numbers (it is infinite!). o However, a finite ordered relation may be represented by positions in lists. We would like to implement an ordered relation between pairs of the form (hour, weekday). % Signature: hour_list(List)/1 % Purpose: Holds the ordered list of days hours. hour_list([h(0),h(1),h(2),h(3),h(4),h(5),h(6),h(7),h(8),h(9),h(10),h(11),h(12), h(13),h(14),h(15),h(16),h(17),h(18),h(19),h(20),h(21),h(22),h(23)]). % Signature: hour_list(List)/1 % Purpose: Holds the ordered list of days hours. hour_list([h(0),h(1),h(2),h(3),h(4),h(5),h(6),h(7),h(8),h(9),h(10),h(11),h(12), h(13),h(14),h(15),h(16),h(17),h(18),h(19),h(20),h(21),h(22),h(23)]). % Signature: weekday_list(List)/1 % Purpose: Holds the ordered list of week days. weekday_list([d('Sun'),d('Mon'),d('Tue'),d('Wed'),d('Thu'),d('Fri'),d('Sat')]). % Signature: weekday_list(List)/1 % Purpose: Holds the ordered list of week days. weekday_list([d('Sun'),d('Mon'),d('Tue'),d('Wed'),d('Thu'),d('Fri'),d('Sat')]).

Logic Programming (cont’d): Lists Example 2: Determining the order of specific times weekday_list([d('Sun'),d('Mon'),d('Tue'),d('Wed'),d('Thu'),d('Fri'),d('Sat')]). hour_list([h(0),h(1),h(2),h(3),h(4),h(5),h(6),h(7),h(8),h(9),h(10),h(11),h(12), h(13),h(14),h(15),h(16),h(17),h(18),h(19),h(20),h(21),h(22),h(23)]). % Signature: is_hour?(Hour)/1 % Purpose: Succeeds iff Hour is an hour of the weekday. is_hour(h(H)) :- hour_list(Hour_list), member(h(H),Hour_list). % Signature: is_hour?(Hour)/1 % Purpose: Succeeds iff Hour is an hour of the weekday. is_hour(h(H)) :- hour_list(Hour_list), member(h(H),Hour_list). A predicate to identify hours: % Signature: hour_order(H1,H2)/2 % Purpose: hour H1 precedes the hour H2 in some weekday. hour_order(h(H1),h(H2)) :- is_hour(h(H1)), is_hour(h(H2)), hour_list(Hour_list), precedes(h(H1),h(H2),Hour_list). % Signature: hour_order(H1,H2)/2 % Purpose: hour H1 precedes the hour H2 in some weekday. hour_order(h(H1),h(H2)) :- is_hour(h(H1)), is_hour(h(H2)), hour_list(Hour_list), precedes(h(H1),h(H2),Hour_list). An order relation between hours:

Logic Programming (cont’d): Lists Example 2: Determining the order of specific times % Signature: is_weekday?(Day)/1 % Purpose: Success iff Weekday is a weekday of the week. is_weekday?(d(D)) :- weekday_list(Weekday_list), member(d(D),Weekday_list). % Signature: is_weekday?(Day)/1 % Purpose: Success iff Weekday is a weekday of the week. is_weekday?(d(D)) :- weekday_list(Weekday_list), member(d(D),Weekday_list). A predicate to identify weekdays: % Signature: weekday_order(Weekday1,Weekday2)/2 % Purpose: Weekday1 precedes weekday2 % in some week. weekday_LT(d(D1),d(D2)) :- is_weekday(d(D1)), is_weekday(d(D2)), weekday_list(Weekday_list), precedes(d(D1),d(D2),Weekday_list). % Signature: weekday_order(Weekday1,Weekday2)/2 % Purpose: Weekday1 precedes weekday2 % in some week. weekday_LT(d(D1),d(D2)) :- is_weekday(d(D1)), is_weekday(d(D2)), weekday_list(Weekday_list), precedes(d(D1),d(D2),Weekday_list). An order relation between weekdays: weekday_list([d('Sun'),d('Mon'),d('Tue'),d('Wed'),d('Thu'),d('Fri'),d('Sat')]). hour_list([h(0),h(1),h(2),h(3),h(4),h(5),h(6),h(7),h(8),h(9),h(10),h(11),h(12), h(13),h(14),h(15),h(16),h(17),h(18),h(19),h(20),h(21),h(22),h(23)]).

Logic Programming (cont’d): Lists Example 2: Determining the order of specific times % Signature: time_LT(T1,T2)/2 % Purpose: The time T1 precedes the time T2 in the week. % Example: ?- time_order(time(h(5),d(‘Mon’)), time(h(1),d(‘Tue’)). % true. time_order(time(h(H1),d(D1)),time(h(H2),d(D2))) :- is_time(time(h(H1),d(D1))), is_time(time(h(H2),d(D2))), weekday_order(d(D1),d(D2)). time_order(time(h(H1),d(D)),time(h(H2),d(D))) :- is_time(time(h(H1),d(D))), is_time(time(h(H2),d(D))), hour_order(h(H1),h(H2)). % Signature: time_LT(T1,T2)/2 % Purpose: The time T1 precedes the time T2 in the week. % Example: ?- time_order(time(h(5),d(‘Mon’)), time(h(1),d(‘Tue’)). % true. time_order(time(h(H1),d(D1)),time(h(H2),d(D2))) :- is_time(time(h(H1),d(D1))), is_time(time(h(H2),d(D2))), weekday_order(d(D1),d(D2)). time_order(time(h(H1),d(D)),time(h(H2),d(D))) :- is_time(time(h(H1),d(D))), is_time(time(h(H2),d(D))), hour_order(h(H1),h(H2)). An order relation between times: % Signature: is_time?(Time)/1 % Purpose: Succeeds iff Time is an hour of some weekday. % Example: ? – is_time? (time(h(1),d(‘Sun’))). true. is_time?(time(h(H),d(D))) :- is_hour?(h(H)), is_weekday?(d(D)). % Signature: is_time?(Time)/1 % Purpose: Succeeds iff Time is an hour of some weekday. % Example: ? – is_time? (time(h(1),d(‘Sun’))). true. is_time?(time(h(H),d(D))) :- is_hour?(h(H)), is_weekday?(d(D)). A predicate to identify times: % Type: Time. % Signature: time(Hour,WeekDay)/2 % Example: time(h(18),d(Tue)). % Type: Time. % Signature: time(Hour,WeekDay)/2 % Example: time(h(18),d(Tue)).

Logic Programming (cont’d): Lists Example 3: Deleting one occurrence of an element from a list. % Signature: select(X,HasXs,OneLessXs)/3 % Purpose: The list OneLessXs is the result of removing % one occurrence of X from the list HasXs. select(Element, HasXs, OneLessXs) :- append([Prefix, [Element], Suffix], HasXs), append(Prefix, Suffix, OneLessXs). % Signature: select(X,HasXs,OneLessXs)/3 % Purpose: The list OneLessXs is the result of removing % one occurrence of X from the list HasXs. select(Element, HasXs, OneLessXs) :- append([Prefix, [Element], Suffix], HasXs), append(Prefix, Suffix, OneLessXs). ?- select(4,[2,3,2,4,5,2,4],X). X = [2, 3, 2, 5, 2, 4]; X = [2, 3, 2, 4, 5, 2]; false

Logic Programming (cont’d): Lists Example 4: Merging sorted lists % Signature: merge(Xs,Ys,Zs)/3 % Purpose: Zs is the sorted merge of the sorted lists Xs and Ys. Assumes % a predicate "lt" of order between elements of Xs and Ys. merge([X|Xs],[Y|Ys],[X|Zs]) :- lt(X,Y), merge(Xs,[Y|Ys],Zs).%1 merge([X|Xs],[X|Ys],[X,X|Zs]) :- merge(Xs,Ys,Zs).%2 merge([X|Xs],[Y|Ys],[Y|Zs]) :- lt(Y,X), merge([X|Xs],Ys,Zs).%3 merge(Xs,[],Xs).%4 merge([],Ys,Ys).%5 % Signature: merge(Xs,Ys,Zs)/3 % Purpose: Zs is the sorted merge of the sorted lists Xs and Ys. Assumes % a predicate "lt" of order between elements of Xs and Ys. merge([X|Xs],[Y|Ys],[X|Zs]) :- lt(X,Y), merge(Xs,[Y|Ys],Zs).%1 merge([X|Xs],[X|Ys],[X,X|Zs]) :- merge(Xs,Ys,Zs).%2 merge([X|Xs],[Y|Ys],[Y|Zs]) :- lt(Y,X), merge([X|Xs],Ys,Zs).%3 merge(Xs,[],Xs).%4 merge([],Ys,Ys).%5 ?- merge([time(h(5),d('Sun')),time(h(5),d('Mon'))], X, [time(h(2),d('Sun')),time(h(5),d('Sun')),time(h(5),d('Mon'))]). X = [time(h(2),d( 'Sun'))] % Signature: lt(Obj1,Obj2)/2 % Purpose: Obj1 precedes Obj2 by some comparison criteria. lt(Time1,Time2) :- time_LT(Time1,Time2). % Signature: lt(Obj1,Obj2)/2 % Purpose: Obj1 precedes Obj2 by some comparison criteria. lt(Time1,Time2) :- time_LT(Time1,Time2).

Logic Programming (cont’d): Lists Example 5: Merging sorted lists merge([t1,t3,t5],[t2,t3],Xs) { X_1=t1,Xs_1=[t3,t5], Y_1=t2,Ys_1=[t3], Xs=[t1|Zs_1] } 1 merge([t3,t5],[t2,t3],Zs_1) 2 – failure branch… 1 – failure branch… { X_3=t3,Xs_3=[t5], Ys_3=[],Zs_2=[t3,t3|Zs_3] } 2 merge([t3,t5],[t3],Zs_2) 1 – failure branch… merge([t5],[],Zs_3) true { Xs_4=[t5], Zs_3=[t5] } 4 merge([X|Xs],[Y|Ys],[X|Zs]) :- lt(X,Y), merge(Xs,[Y|Ys],Zs). merge([X|Xs],[X|Ys],[X,X|Zs]) :- merge(Xs,Ys,Zs). merge([X|Xs],[Y|Ys],[Y|Zs]) :- lt(Y,X), merge([X|Xs],Ys,Zs). merge(Xs,[],Xs). merge([],Ys,Ys). merge([X|Xs],[Y|Ys],[X|Zs]) :- lt(X,Y), merge(Xs,[Y|Ys],Zs). merge([X|Xs],[X|Ys],[X,X|Zs]) :- merge(Xs,Ys,Zs). merge([X|Xs],[Y|Ys],[Y|Zs]) :- lt(Y,X), merge([X|Xs],Ys,Zs). merge(Xs,[],Xs). merge([],Ys,Ys). ?-merge( [ time(h(1),d('Sun')), time(h(3),d('Wed')), time(h(5), d('Sat'))], [ time(h(2), d('Sun')), time(h(3),d('Wed'))], Xs) lt(t1,t2), merge([t3,t5],[t2,t3],Zs_1) * lt(t2,t3), merge([t3,t5],[t3],Zs_2) * 1 – failure branch… { X_2=t3,Xs_2=[t5], Y_2=t2, Ys_2=[t3], Zs_1=[t2|Zs_2] } 3 2 – failure branch… 3 – failure branch…

Logic Programming (cont’d): Backtracking optimization merge([t1,t3,t5],[t2,t3],Xs) !, merge([t3,t5],[t2,t3],Zs_1) merge([X|Xs],[Y|Ys],[X|Zs]) :- lt(X,Y), !, merge(Xs,[Y|Ys],Zs). merge([X|Xs],[X|Ys],[X,X|Zs]) :- merge(Xs,Ys,Zs). merge([X|Xs],[Y|Ys],[Y|Zs]) :- lt(Y,X), merge([X|Xs],Ys,Zs). merge([X|Xs],[Y|Ys],[X|Zs]) :- lt(X,Y), !, merge(Xs,[Y|Ys],Zs). merge([X|Xs],[X|Ys],[X,X|Zs]) :- merge(Xs,Ys,Zs). merge([X|Xs],[Y|Ys],[Y|Zs]) :- lt(Y,X), merge([X|Xs],Ys,Zs). lt(t1,t2), !, merge([t3,t5],[t2,t3],Zs_1) Example 7: Optimize the procedure merge. o When none of the first two params is [], only one of the rules can be valid, o Since either lt(Y,X), or lt(X,Y) or X==Y holds, exclusively. o Once one of the above holds, we can skip further rule selections. merge([t3,t5],[t2,t3],Zs_1) 3 – failure branch… 2 – failure branch… *

Logic Programming (cont’d): Backtracking optimization Rule k:A :- B 1, …B i, !, B i+1, …, B n. Rule k+1:A :- …. … ?- B 1,…, B i, !, B i+1, …B n,Q 1 ’,…Q m ’ Rule k * ?- !, B i+1 ’, …B n ’,Q 1 ’’,…Q n ’’ ?- B i+1 ’, …B n ’,Q 1 ’’,…Q n ’’ … Following selections of A rules are cut. Further substitutions for goals before ‘!’ are cut. The cut operator (!): Used to avoid redundant computations.

Logic Programming (cont’d): Backtracking optimization Example 8: red VS green cuts. o Green cut: Helps optimizing the program by avoiding redundant computations. o Red cut: Omits possible solutions. o Red cuts are undesired, unless specifically required (E.g., to provide only the first solution). Q: How many possible solutions does the following query have? ?- merge([],[],X). X = []; X = [] The query matches both facts 4 and 5: merge(Xs,[],Xs). merge([],Ys,Ys). merge(Xs,[],Xs). merge([],Ys,Ys). To avoid this, we add ‘!’ to fact 4, rewriting it as a rule: merge(Xs,[],Xs):- !. merge([],Ys,Ys). merge(Xs,[],Xs):- !. merge([],Ys,Ys). A red cut! The number of solutions has changed!

Logic Programming (cont’d): Meta circular interpreter Recall the “waiting list” version of the meta-circular interpreter (seen in class): o Based on goals reduction, with explicit control of selection order (using a stack of goals). o Operates in two phases: 1.Preprocessing: The given program, P, is translated into a new program P’, which has a single predicate, rule, and consists of facts only. 2.Queries are applied to the new program using the procedure solve.

Logic Programming (cont’d): Meta circular interpreter member(X,[X|Xs]). member(X,[Y|Ys]) :- member(X,Ys). member(X,[X|Xs]). member(X,[Y|Ys]) :- member(X,Ys). % Signature: rule(Head,BodyList)/2 rule(member(X,[X|Xs]),true). rule(member(X,[Y|Ys]),[member(X,Ys)]). % Signature: rule(Head,BodyList)/2 rule(member(X,[X|Xs]),true). rule(member(X,[Y|Ys]),[member(X,Ys)]). Example 10: Applying the meta circular interpreter. Preprocessing % Signature: solve(Exp)/1 % Purpose: Interpret with explicit management of a waiting list of goals. solve(Goal) :- solve(Goal,[]). % Signature: solve(Goal,Rest_of_goals)/2 solve(true, Goals) :- solve([],Goals). solve([],[]). % Nothing to prove, empty stack, done. solve([],[G | Goals]) :- solve(G,Goals). % Nothing to prove, non-empty stack, pop. % A list of goals: prove the first and push the rest. solve([A|B],Goals) :- extend_waiting_list([A|B],Goals,NewGoals), solve([],NewGoals). solve(A,Goals) :- rule(A,B), solve(B,Goals). % Signature: solve(Exp)/1 % Purpose: Interpret with explicit management of a waiting list of goals. solve(Goal) :- solve(Goal,[]). % Signature: solve(Goal,Rest_of_goals)/2 solve(true, Goals) :- solve([],Goals). solve([],[]). % Nothing to prove, empty stack, done. solve([],[G | Goals]) :- solve(G,Goals). % Nothing to prove, non-empty stack, pop. % A list of goals: prove the first and push the rest. solve([A|B],Goals) :- extend_waiting_list([A|B],Goals,NewGoals), solve([],NewGoals). solve(A,Goals) :- rule(A,B), solve(B,Goals).

… {NewGoals_4 = [member(X, [b, c])]} {G_5 = member(X, [b, c]), Goals_5 = []} {X = b, X_7 = b, Xs_7 = [c], B_6 = []} solve(member(X, [a, b, c])) solve(member(X, [a, b, c]), []) rule(member(X, [a, b, c], B_2), solve(B_2, [] ) solve([],[]) solve( [member(X, [b, c])], [] ) ext-wait-list([member(X, [b, c])], [], NewGoals_4), solve([], NewGoals_4) solve([], [member(X, [b, c])] ) rule(member(X,[b, c]), B_6), solve(B_6, []) solve(true,[]) true {X = b} {Goal_1 = member(X, [a, b, c])} 1 {A_2 = member(X, [a, b, c]), Goals_2 = []} 5 {X = a, X_3 = a, Xs_3 = [b, c], B_2 = []} 1 true {X = a} 2 {A_4 = member(X, [b, c]), B_4=[] Goals_4 = []} rule(member(X,[X|Xs]),true). rule(member(X,[Y|Ys]),[member(X,Ys)]). rule(member(X,[X|Xs]),true). rule(member(X,[Y|Ys]),[member(X,Ys)]). solve(Goal) :- solve(Goal,[]). solve(true, Goals) :- solve([],Goals). solve([],[]). solve([],[G | Goals]) :- solve(G,Goals). solve([A|B],Goals) :- extend_waiting_list([A|B],Goals,NewGoals), solve([],NewGoals). solve(A,Goals) :- rule(A,B), solve(B,Goals). solve(Goal) :- solve(Goal,[]). solve(true, Goals) :- solve([],Goals). solve([],[]). solve([],[G | Goals]) :- solve(G,Goals). solve([A|B],Goals) :- extend_waiting_list([A|B],Goals,NewGoals), solve([],NewGoals). solve(A,Goals) :- rule(A,B), solve(B,Goals). {X_3 = X, Y_3 = a, Ys_3 = [b, c], B_2 = [member(X, [b, c])]} 2 * solve(member(X, [b, c]), []) {A_6 = member(X, [b, c]), Goals_6 = []} 5 1 solve([],[]) {Goals_8 = []} 1