C. Varela1 Logic Programming Prolog: Arithmetic, Equalities, Operators, I/O. (PLP 11) Databases: assert, retract. (CTM 9.6) Carlos Varela Rennselaer Polytechnic.

Slides:



Advertisements
Similar presentations
Prolog Nonmonotonic logic.
Advertisements

C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Programming Techniques Accumulators, Difference Lists (VRH ) Carlos Varela.
Modern Programming Languages
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.
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
COP4020 Programming Languages
1 Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language Concepts Spring 2003 The University of North Carolina at Chapel Hill.
Tutorial 6 of CSCI2110 Bipartite Matching Tutor: Zhou Hong ( 周宏 )
TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University.
Side effects in Prolog Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
Pure Prolog vs. Extralogical Prolog Pure Prolog programs can be interpreted as logical statements about what they compute. Pure Prolog programs can be.
PROLOG I/O I/O and the relational model. PROLOG I/O operators. –Not really predicates. I/O with characters. I/O with terms. I/O with lists. I/O.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Single assignment store Kernel language syntax Carlos Varela.
Chapter 5: Loops and Files.
Carlos Varela Rennselaer Polytechnic Institute September 10, 2007
1 Lab Session-IV CSIT-120 Spring 2001 Lab 3 Revision and Exercises Rev: Precedence Rules Lab Exercise 4-A Machine Language Programming The “Micro” Machine.
C. Varela1 Logic Programming (PLP 11.3) Prolog Imperative Control Flow: Backtracking Cut, Fail, Not Carlos Varela Rennselaer Polytechnic Institute September.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 28: Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Copyright © 2009 Elsevier Chapter 11 :: Logic Languages Programming Language Pragmatics Michael L. Scott.
P-Phunck - Prolog1 Introduction to Prolog Brian Paden.
Copyright © 2009 Elsevier Chapter 11 :: Logic Languages Programming Language Pragmatics Michael L. Scott.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
1 Lecture Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,
6 Sums of Games.. Lecture6 Admin Plagiarism forms Answer football cup final problem. //Daisy Problem hints e.g. try a smaller size. General problem solving.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
CSCE 121: Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
F28PL1 Programming Languages Lecture 18: Prolog 3.
Operators in Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
C. Varela1 Logic Programming (PLP 11, CTM 9.2, 9.4, ) Constraint Satisfaction Problems, Natural Language Parsing (Definite Clause Grammars) Carlos.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Loops and Files. 5.1 The Increment and Decrement Operators.
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.
COP4020 Programming Languages Prolog Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
C. Varela1 Logic Programming (CTM ) Constraint Programming: Constraints and Computation Spaces Carlos Varela Rennselaer Polytechnic Institute.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
C. Varela1 Logic Programming (PLP 11, CTM 9.3) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not Lists, Append Carlos Varela Rennselaer Polytechnic.
Copyright © 2009 Elsevier Chapter 11 :: Logic Languages And other online sources (see links on schedule page) Michael L. Scott Based on Programming Language.
1 Lecture 16: Prolog’s Lists, Negation and Imperative Control Flow (Section 11.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed.
Carlos Varela Rennselaer Polytechnic Institute November 18, 2016
Chapter 12 :: Logic Languages
Carlos Varela Rennselaer Polytechnic Institute November 11, 2016
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
Chapter 12 :: Logic Languages
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
Carlos Varela Rensselaer Polytechnic Institute December 5, 2017
PROLOG.
Carlos Varela Rensselaer Polytechnic Institute November 14, 2017
Carlos Varela Rensselaer Polytechnic Institute November 28, 2017
Carlos Varela Rennselaer Polytechnic Institute December 6, 2016
Carlos Varela Rensselaer Polytechnic Institute December 8, 2017
CS 3304 Comparative Languages
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Declarative Computation Model Single assignment store (VRH 2
Carlos Varela Rennselaer Polytechnic Institute November 22, 2016
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Presentation transcript:

C. Varela1 Logic Programming Prolog: Arithmetic, Equalities, Operators, I/O. (PLP 11) Databases: assert, retract. (CTM 9.6) Carlos Varela Rennselaer Polytechnic Institute November 24, 2015

C. Varela2 Arithmetic Goals N>M N<M N=<M N>=M N and M must be bound to numbers for these tests to succeed or fail. X is 1+2 is used to assign numeric value of right-hand-side to variable in left-hand-side.

C. Varela3 Loop Revisited natural(1). natural(N) :- natural(M), N is M+1. my_loop(N) :- N>0, natural(I), write(I), nl, I=N, !. my_loop(_). Also called generate-and-test.

C. Varela4 = is not equal to == or =:= X=Y X\=Y test whether X and Y can be or cannot be unified. X==Y X\==Y test whether X and Y are currently co-bound, i.e., have been bound to, or share the same value. X=:=Y X=\=Y test arithmetic equality and inequality.

C. Varela5 More equalities test whether X and Y are structurally identical. is weaker than == but stronger than =. Examples:

C. Varela6 More on equalities X==Y   X=Y but not the other way (  ). If two terms are currently co-bound, they are structurally identical, and therefore they can unify. Examples:

C. Varela7 Prolog Operators :- op(P,T,O) declares an operator symbol O with precedence P and type T. Example: :- op(500,xfx,’has_color’) a has_color red. b has_color blue. then: ?- b has_color C. C = blue. ?- What has_color red. What = a.

C. Varela8 Operator precedence/type Precendence P is an integer: the larger the number, the less the precedence (ability to group). Type T is one of: T PositionAssociativityExamples xfx InfixNon-associative is xfy InfixRight-associative, ; yfx InfixLeft-associative + - * / fx PrefixNon-associative ?- fy PrefixRight-associative xf PostfixNon-associative yf PostfixLeft-associative

C. Varela9 Testing types atom(X) tests whether X is an atom, e.g., ‘foo’, bar. integer(X) tests whether X is an integer; it does not test for complex terms, e.g., integer(4/2) fails. float(X) tests whether X is a float; it matches exact type. string(X) tests whether X is a string, enclosed in `` … ``.

C. Varela10 Prolog Input seeing(X) succeeds if X is (or can be) bound to current read port. X = user is keyboard (standard input.) see(X) opens port for input file bound to X, and makes it current. seen closes current port for input file, and makes user current. read(X) reads Prolog type expression from current port, storing value in X. end-of-file is returned by read at.

C. Varela11 Prolog Output telling(X) succeeds if X is (or can be) bound to current output port. X = user is screen (standard output.) tell(X) opens port for output file bound to X, and makes it current. told closes current output port, and reverses to screen output (makes user current.) write(X) writes Prolog expression bound to X into current output port. nl new line (line feed). tab(N) writes N spaces to current output port.

C. Varela12 I/O Example browse(File) :- seeing(Old),/* save for later */ see(File),/* open this file */ repeat, read(Data),/* read from File */ process(Data), seen,/* close File */ see(Old), /* prev read source */ !./* stop now */ process(end_of_file) :- !. process(Data) :- write(Data), nl, fail.

C. Varela13 First-Class Terms Revisited call(P) Invoke predicate as a goal. assert(P) Adds predicate to database. retract(P) Removes predicate from database. functor(T,F,A) Succeeds if T is a term with functor F and arity A. findall(F,P,L) Returns a list L with all elements F satisfying predicate P clause(H,B) Succeeds if the clause H :- B can be found in the database.

Databases: assert and retract Prolog enables direct modification of its knowledge base using assert and retract. Let us consider a tic-tac-toe game: We can represent a board with facts x(n) and o(n), for n in {1..9} corresponding to each player’s moves. As a player (or the computer) moves, a fact is dynamically added to Prolog’s knowledge base. C. Varela

Databases: assert and retract C. Varela15 % main goal: play :- clear, repeat, getmove, respond. getmove :- repeat, write('Please enter a move: '), read(X), empty(X), assert(o(X)). respond :- makemove, printboard, done. makemove :- move(X), !, assert(x(X)). makemove :- all_full. clear :- retractall(x(_)), retractall(o(_)). Human move Computer move

Tic-tac-toe: Strategy C. Varela16 move(A) :- good(A), empty(A), !. good(A) :- win(A). good(A) :- block_win(A). good(A) :- split(A). good(A) :- strong_build(A). good(A) :- weak_build(A). good(5). good(1). good(3). good(7). good(9). good(2). good(4). good(6). good(8). The strategy is to first try to win, then try to block a win, then try to create a split (forced win in the next move), then try to prevent opponent from building three in a row, and creating a split, finally choose center, corners, and other empty squares. The order of the rules is key to implementing the strategy.

Tic-tac-toe: Strategy(2) C. Varela17 win(A) :- x(B), x(C), line(A,B,C). block_win(A):- o(B), o(C), line(A,B,C). split(A):- x(B), x(C), different(B,C), line(A,B,D), line(A,C,E), empty(D), empty(E). strong_build(A) :- x(B), line(A,B,C), empty(C), not(risky(C)). weak_build(A) :- x(B), line(A,B,C), empty(C), not(double_risky(C)). risky(C):- o(D), line(C,D,E), empty(E). double_risky(C) :- o(D), o(E), different(D,E), line(C,D,F), line(C,E,G), empty(F), empty(G). Moving x(8) produces a split: x(2) or x(7) wins in next move. O XO X

Databases in Oz: RelationClass Oz supports dynamic database modifications using a RelationClass. The initial relation is defined as follows: Rel = {New RelationClass init} Once we have a relation instance, the following operations are possible: –{Rel assert(T)} adds tuple T to Rel. –{Rel assertall(Ts)} adds the list of tuples Ts to Rel. –{Rel query(X)} binds X to one of the tuples in Rel. X can be any partial value. If more than one tuple is compatible with X, then search can enumerate all of them. C. Varela18

Databases in Oz: An example GraphRel = {New RelationClass init} {GraphRel assertall([edge(a b) edge(b c) edge(c d) edge(d e) edge(b e) edge(d f)])} proc {EdgeP A B} {GraphRel query(edge(A B))} end {Browse {Search.base.all proc {$ X} {EdgeP b X} end}} % displays all edges from node b: [c e] C. Varela19 abcdef

Databases in Oz: An example(2) proc {Path X Y} choice X = Y [] Z in {EdgeP Z Y} {Path X Z} end {Browse {Search.base.all proc {$ X} {Path b X} end}} % displays all nodes with a path from node b: [b c e e f d] C. Varela20

C. Varela21 Exercises 87.The Prolog predicate my_loop/1 can succeed or fail as a goal. Explain why you may want a predicate to succeed, or fail depending on its expected calling context. 88.CTM Exercise (page 671). Do it both in Prolog and Oz. 89.PLP Exercise 11.7 (page 571), in Oz. 90.Develop a tic-tac-toe game in Oz.