Announcements Rainbow grades up HW1, Quiz 1-2 in HW Server Deadline for HW3 (Prolog) is extended to Monday, March 7 th. Submit in HW Server A few notes.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
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.
Chapter 11 :: Logic Languages
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
8 Queens. Problem: Placing 8 queens on a chessboard such that they don’t attack each other Three different Prolog programs are suggessted as solutions.
PROLOG 8 QUEENS PROBLEM.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Reasoning About Code; Hoare Logic, continued
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
Logic Programming Lecture 3: Recursion, lists, and data structures.
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/
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
C. Varela1 Programming Languages (CSCI 4430/6969) History, Syntax, Semantics, Essentials, Paradigms Carlos Varela Rennselaer Polytechnic Institute August.
C. Varela1 Logic Programming (PLP 11.3) Prolog Imperative Control Flow: Backtracking Cut, Fail, Not Carlos Varela Rennselaer Polytechnic Institute September.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 28: Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language.
CSE115/ENGR160 Discrete Mathematics 01/20/11 Ming-Hsuan Yang UC Merced 1.
Data Structures Using C++ 2E Chapter 6 Recursion.
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.
Data Structures Using C++ 2E Chapter 6 Recursion.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Programming Languages (ICE 1341) Lecture #4 Programming Languages (ICE 1341)
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.
1 Topics Covers everything –1) in slides –2) from the very beginning. Language classification, history, and comparison Programming paradigms –Structured.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
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.
First Order Logic Lecture 3: Sep 13 (chapter 2 of the book)
Cs774 (Prasad)L6Backtracking1 Controlling Backtracking : Cuts
CSE (c) S. Tanimoto, 2008 Predicate Calculus II 1 Predicate Calculus 2 Outline: Unification: definitions, algorithm Formal interpretations and satisfiability.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
C. Varela1 Logic Programming (PLP 11, CTM 9.1) Terms, Resolution, Unification, Search, Backtracking (Prolog) Relational Computation Model (Oz) Carlos Varela.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
1 Announcements HW2 due Thursday, 2/18 in class and in Homework Server Note on Problem 3: Show means prove Note on 4&7: Will cover material today Download.
PREDICATES AND QUANTIFIERS COSC-1321 Discrete Structures 1.
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.
C. Varela1 Logic Programming (PLP 11, CTM 9.3) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not Lists, Append Carlos Varela Rennselaer Polytechnic.
Logic Programming Lecture 2: Unification and proof search.
Copyright © 2009 Elsevier Chapter 11 :: Logic Languages And other online sources (see links on schedule page) Michael L. Scott Based on Programming Language.
Section 16.5, 16.6 plus other references
COSC 2P93 Prolog: Debugging
Prolog a declarative language
Carlos Varela Rennselaer Polytechnic Institute November 11, 2016
CS 3304 Comparative Languages
Chapter 3 The Logic of Quantified Statements
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
Carlos Varela Rensselaer Polytechnic Institute November 14, 2017
Logic Programming with Prolog: Resolution, Unification, Backtracking
Chapter 11 :: Logic Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Chapter 11 :: Logic Languages
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
CS Software Studio Assignment 1
Announcements Quiz 3 HW2 should be graded by Friday/Saturday
Programming Techniques
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Chapter 12 :: Logic Languages
Chapter 12 :: Logic Languages
Presentation transcript:

Announcements Rainbow grades up HW1, Quiz 1-2 in HW Server Deadline for HW3 (Prolog) is extended to Monday, March 7 th. Submit in HW Server A few notes on Prolog trace. turns on tracing mode notrace. turns it off HW Server inputs and outputs Strongly, strongly encourage you to solve (a good chunk of) HW3 before the test 1

Announcements Exam on March 3 rd Closed-book, only 2 “cheat” pages, that is, you can have (i) 2 8.5x11 sheets, filled on one side each, or (ii) 1 8.5x11 sheet, filled on both sides Can be typed, handwritten, as you wish Programming Language Syntax and Prolog Lectures 1-9 Chapters 1, 2.1, 2.2, , 11 Practice tests available on Announcements page 2

Spring 16 CSCI 4430, A Milanova 3 Last Class Prolog Lists Arithmetic Imperative control flow

Spring 16 CSCI 4430, A Milanova 4 Today’s Lecture Outline Prolog Imperative control flow Negation by failure The generate and test paradigm

5 Logic Programming and Prolog Finish reading: Scott, Chapter

6 Imperative Control Flow Programmer has explicit control on backtracking process cut (!) ! is a subgoal As a goal it succeeds, but with a side effect: Commits interpreter to all bindings made since unifying left-hand side of current rule with parent goal

Spring 16 CSCI 4430, A Milanova 7 Cut (!) Example rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), !, cold(X). ?- snowy(C).

Spring 16 CSCI 4430, A Milanova 8 Cut (!) Example rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), !, cold(X). snowy(C) snowy(X) AND OR rainy(X) cold(X) rainy(seattle)rainy(rochester) cold(rochester) _C = _X X = seattle cold(seattle) fails; no backtracking to rainy(X). GOAL FAILS. !

Spring 16 CSCI 4430, A Milanova 9 Cut (!) Example 2 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), !, cold(X). snowy(troy). ?- snowy(C).

10 Cut (!) Example 2 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), !, cold(X). snowy(troy). snowy(C) snowy(X) AND OR rainy(X) cold(X) rainy(seattle)rainy(rochester) cold(rochester) _C = _X X = seattle 2 committed OR bindings: _C = _X and X = seattle GOAL FAILS. ! OR snowy(troy) How about query ?- snowy(troy)? Spring 16 CSCI 4430, A Milanova

11 Cut (!) Example 3 rainy(seattle) :- !. rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X). snowy(troy). ?- snowy(C).

12 Cut (!) Example 3 rainy(seattle) :- !. rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X). snowy(troy). snowy(C) snowy(X) AND OR rainy(X) cold(X) rainy(seattle)rainy(rochester) cold(rochester) _C = _X X = seattle C = troy SUCCEEDS Only rainy(X) is committed to bindings (X = seattle). ! OR snowy(troy) C = troy How about query ? - snowy(rochester)?

Spring 16 CSCI 4430, A Milanova 13 Cut (!) Example 4 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- !, rainy(X), cold(X). snowy(troy). ?- snowy(C).

Spring 16 CSCI 4430, A Milanova 14 Cut (!) Example 4 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- !, rainy(X), cold(X). snowy(C) snowy(X) AND OR rainy(X) cold(X) rainy(seattle)rainy(rochester) cold(rochester) _C = _X X = seattle cold(seattle) fails; backtrack. X = rochester success !

Spring 16 CSCI 4430, A Milanova 15 Cut (!) Example 5 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X), !. ?- snowy(C).

Spring 16 CSCI 4430, A Milanova 16 Cut (!) Example 5 rainy(seattle). rainy(rochester). cold(rochester). snowy(X) :- rainy(X), cold(X), !. snowy(C) snowy(X) AND OR rainy(X) cold(X) rainy(seattle)rainy(rochester) cold(rochester) _C = _X X = seattle X = rochester success !

17 Negation by Failure: not(X), \+(X) not(X) succeeds when X fails Called negation by failure, defined: not(X):- X,!,fail. not(_). Not the same as negation in logic ¬X ! In Prolog, we can assert that something is true, but we cannot assert that something is false! Spring 16 CSCI 4430, A Milanova

18 Questions takes(jane, his). takes(jane, cs). takes(ajit, art). takes(ajit, cs). classmates(X,Y) :- takes(X,Z), takes(Y,Z). ?- classmates(jane,Y). What are the bindings of Y? How can we change rule classmates(X,Y) to prevent binding Y=jane ?

Spring 16 CSCI 4430, A Milanova 19 Example p(X) :- q(X), not(r(X)). r(X) :- w(X), not(s(X)). q(a). q(b). q(c). s(a). s(c). w(a). w(b). Evaluate: ?- p(a). ?- p(b). ?- p(c).

Spring 16 CSCI 4430, A Milanova 20 Today’s Lecture Outline Prolog Imperative control flow Negation by failure The generate and test paradigm

Spring 16 CSCI 4430, A Milanova 21 Generate and Test Paradigm Search in space Prolog rules to generate potential solutions Prolog rules to test potential solutions for desired properties Easy prototyping of search

Spring 16 CSCI 4430, A Milanova 22 A Classic Example: n Queens Given an n by n chessboard, place each of n queens on the board so that no queen can attack another in one move Queens can move either vertically, horizontally, or diagonally. A classic generate and test problem queens(P) :- … placement(P),ok_place(P).

23 n Queens my_not(X):- X, !, fail. %same as not my_not(_). in(H,[H|_]). %same as member in(H,[_|T]):- in(H,T). nums(H,H,[H]). nums(L,H,[L|R]):- L<H, N is L+1, nums(N,H,R). %%nums generates a list of integers between two other numbers, L,H by putting the first number at the front of the list returned by a recursive call with a number 1 greater than the first. It only works when the first two arguments are bound to integers. It stops when it gets to the higher number queen_no(4). %%The number of queens/size of board - use 4

Spring 16 CSCI 4430, A Milanova 24 n Queens ranks(L):- queen_no(N), nums(1,N,L). files(L):- queen_no(N), nums(1,N,L). %%ranks and files generate the x and y axes of the chess board. Both are lists of numbers up to the number of queens; that is, ranks(L) binds L to the list [1,2,3,…,#queens]. rank(R):- ranks(L), in(R,L). %% R is a rank on the board; selects a particular rank R from the list of all ranks L. file(F):- files(L), in(F,L). %% F is a file on the board; selects a particular file F from the list of all files L.

25 n Queens %% Squares on the board are (rank,file) coordinates. attacks decides if a queen on the square at rank R1, file F1 attacks the square at rank R2, file F2 or vice versa. A queen attacks every square on the same rank, the same file, or the same diagonal. attacks((R,_),(R,_)). attacks((_,F),(_,F)). %a Prolog tuple attacks((R1,F1),(R2,F2)):- diagonal((R1,F1),(R2,F2)). %%can decompose a Prolog tuple by unification (X,Y)=(1,2) results in X=1,Y=2; tuples have fixed size and there is not head-tail type construct for tuples q same diagonal same rank same file What is safe placement for next queen on board?

Spring 16 CSCI 4430, A Milanova 26 n Queens %% Two squares are on the same diagonal if the slope of the line between them is 1 or -1. diagonal((X,Y),(X,Y)). %degenerate case diagonal((X1,Y1),(X2,Y2)):-N is Y2-Y1,D is X2-X1, Q is N/D, Q is 1. %diagonal needs bound arguments! diagonal((X1,Y1),(X2,Y2)):-N is Y2-Y1,D is X2-X1, Q is N/D, Q is -1. %%because of use of “is”, diagonal is NOT invertible.

Spring 16 CSCI 4430, A Milanova 27 n Queens %% This solution works by generating every list of squares, such that the length of the list is the same as the number of queens, and then checks every list generated to see if it represents a valid placementof queens to solve the N queens problem; assume list length function queens(P):- queen_no(N), length(P,N), placement(P), ok_place(P). “generate” code given first“test” code follows

Spring 16 CSCI 4430, A Milanova 28 n Queens placement([]). placement([(R,F)|P]):- placement(P), rank(R), file(F). %%placement can be used as a generator. If placement is called with a free variable, it will construct every possible list of squares on the chess board. The first predicate will allow it to establish the empty list as a list of squares on the board. The second predicate will allow it to add any (R,F) pair onto the front of a list of squares if R is a rank of the board and F is a file of the board. placement first generates all 1 element lists, then all 2 element lists, etc. Switching the order of predicates in the second clause will cause it to try varying the length of the list before it varies the squares added to the list

Spring 16 CSCI 4430, A Milanova 29 n Queens %%these two routines check the placement of the next queen %%Checks a list of squares to see that no queen on any of them would attack any other. does by checking that position j doesn’t conflict with positions (j+1),(j+2) etc. ok_place([]). ok_place([(R,F)|P]):- no_attacks((R,F),P),ok_place(P). %% Checks that a queen at square (R,F) doesn't attack any square (rank,file pair) in list L; uses attacks predicate defined previously no_attacks(_,[]). no_attacks((R,F),[(R2,F2)|P]):- my_not(attacks((R,F),(R2,F2))), no_attacks((R,F),P).

Next class New topic: Binding, naming and scoping Review for Exam 1 Spring 16 CSCI 4430, A Milanova 30

Spring 16 CSCI 4430, A Milanova 31

32 Exam 1 Regular Expressions CFGs Derivation, Parsing, Ambiguity, Precedence LL(1) grammars and parsing FIRST and FOLLOW sets, LL(1) parsing table Obstacles to LL(1)-ness SLR(1) grammars and parsing CFSM and SLR(1) parsing tables Conflicts in SLR(1) Spring 16 CSCI 4430, A Milanova

33 Exam 1 Prolog Concepts: Search trees, Unification, Rule ordering, Backtracking, Backward chaining One type of question: Given a Prolog predicate and a query, “Find the first binding” or “Find all bindings” Second type of question: Given a description, write a predicate. 3-4 Lines of Code. Spring 16 CSCI 4430, A Milanova