Overview. Declarative Languages. operation of del. Deleting an element from a list. functions using del. inserting elements with del

Size: px
Start display at page:

Download "Overview. Declarative Languages. operation of del. Deleting an element from a list. functions using del. inserting elements with del"

Transcription

1 Overview Declarative Languages D7012E: Arithmetic and Backtracking Fredrik Bengtsson Some standard functions Operators Arithmetic The eight queens problem Controlling backtracking cut Deleting an element from a list relation del(x, L, L1) delete X from L obtaining L1 If X is head of L, L1 is tail otherwise, delete X from the tail del(x, [X Tail], Tail). del(x, [Y Tail], [Y Tail1]):- del(x, Tail, Tail1). operation of del can delete any occurrence by backtracking?- del(a, [a, b, a, a], L). L=[b,a,a]; L=[a,b,a]; L=[a,b,a]; inserting elements with del What is L such that after deleting a, we obtain L1?- del(a,l,[1,2,3]) L=[a,1,2,3] L=[1,a,2,3] L=[1,2,a,3] L=[1,2,3,a] functions using del insert: insert(x,list, BiggerList):- del(x,biggerlist, List). insert and delete is really the same operation member: member2(x,list):- del(x,list,_). if X can be deleted from List... then X is in List. 1

2 sublist relation sublist([c,d,e], [a,b,c,d,e,f]) true sublist([c,e], [a,b,c,d,e,f]) false S is sublist of L if L decomposed into L1, L2 L2 decomposed into S and L3 sublist(s,l):- conc(l1,l2,l), conc(s,l3,l2). Operators operators are infix atoms 2+5 = +(2,5) No operation associated with operator it just defines a structure exactly like other atoms?- X=2+5 X=2+5 evaluation X is assigned structure, as usual Operators: +, -, *, / ** power // integer division mod modulo is operator force arithmetic evaluation?- X is 2+5. X=7 standard functions sin, cos, tan,... Arithmetic Comparison operators only compares does t instantiate variables! error if variables t instantiated operators: <, >, >=, =< equal: =:= t equal =/= difference between = and =:= = instantiates =:= only compares comparison operators will force evaluation like is List length function define: length(list, N) length([],0). length([ _ Tail], N):- length(tail, N1), N is 1 + N1.?- length([a,b,[c,d],e], N). N=4 ordering important cant evaluate N is 1+N1 before recursive call procedural thinking Ather length function what about length1([],0). length1([ _ Tail], N):- length1(tail, N1), N=1 + N1.?- length1([a,b,[c,d],e], N). N=1+(1+(1+(1+0))). rewritten length1([],0). length1([ _ Tail], 1+N):- length1(tail, N). length1([a,b,c], N), Length is N. N=1+(1+(1+0)) Length=3. 2

3 The eight queens problem Place eight queens on chessboard queen can attack ather queen queens attack horizontally vertically diagonally unary predicate solution(pos) Pos represents correct solution eight queens problem data representation list of pairs X/Y obs! t division position for each queen Simplify solution generalize problem! allow any number of queens Observation two queens in same column start with list [1/Y1, 2/Y2, 3/Y3, 4/Y4, 5/Y5, 6/Y6, 7/Y7, 8/Y8] solution attack Solution list of queens empty solution [X/Y Others] attack in list Others X and Y integers between 1 and 8 X/Y t attack anyone in Others Code: solution([]). solution([x/y Others]):- solution(others), member(y, [1,2,3,4,5,6,7,8]), attack(x/y,others). Remains: define attack attack(q, Qlist): if Qlist empty, then true Qlist t empty: Qlist=[Q1 Qlist1] Q t attack Q1 Q t attack Qlist1 t attack between pair of queens: Y-coordinates different t on same diagonal X coordinate already taken care of start list attack Complete solution the attack relation attack(_,[]). attack(x/y, [X1/Y1 Others]):- Y=/=Y1, %different Y Y1-Y=/=X1-X, %different diagonals Y1-Y=/=X-X1, attack(x/y, Others) solution([]). find solution: solution([x/y Others]):-?- template(s), solution(s). solution(others), member(y, [1,2,3,4,5,6,7,8]), attack(x/y,others). attack(_,[]). attack(x/y, [X1/Y1 Others]):- Y=/=Y1, %different Y Y1-Y=/=X1-X, %different diagonals Y1-Y=/=X-X1, attack(x/y, Others) member(x, [X L]). template([1/y1, 2/Y2, 3/Y3, 4/Y4, 5/Y5, 6/Y6, 7/Y7, 8/Y8]). 3

4 Controlling backtracking Cut Consider function f such that f(x)=0, for x<3 f(x)=2, for 3<=x<6 f(x)=4, for 6<x In prolog: f(x,0):- X<3. f(x,2):- 3=<X, X<6. f(x,4):- 6=<X. Now, consider:?- f(1,y), 2<Y. f(1,y) will bind Y=0 fail on second goal prolog will try second clause f(x,2) third clause f(x,4) before failure of main goal t necessary we kw: both second and third clause will fail! inefficient Tell prolog t to try any more clauses. CUT! Cut, deted exclamation mark "!" pseudo-goal prevent backtracking to earlier point prevent other clause from being used consider: f(x,2):- 3=<X, X<6,!. f(x,4):- 6=<X,!. t try other clause because of cut» more efficient in this case new experiment new program w, try?- f(7,y) Y=4 what happens? first clause: 7<3 fail second clause: 3<=7 succeed 7<6 fail third clause 6<=7 succeed Not efficient either second goal (3<=7) kw that it's true from first goal 3=<X redundant remove 6=<X also redundant remove f(x,2):- 3=<X, X<6,!. f(x,4):- 6=<X,!. so, f(x,2):- X<6,!. f(x,4). same result as first program! what if we w remove cuts? doesn't work correctly we have changed behaviour t only improved efficiency Cut single solution member H :- B1, B2,..., Bm,!, Bm1..., Bn. meaning: don't try alternatives for B1, B2,..., Bm don't try other clauses of H DO try alternatives for Bm1,..., Bn consider: member(x, [X L]). X appears several times any occurrence found t necessary and inefficient insert cut member(x, [X L]) :-!. once first X found don't backtrack?- member(x, [a,a,a,b,c]). X=a; 4

5 negation as failure bagof, setof, findall Next lecture 5

Snakes. Declarative Languages. Relation different. Overview. Negation as failure. Relation different

Snakes. Declarative Languages. Relation different. Overview. Negation as failure. Relation different Snakes Declarative Languages D7012E: Negation and solution gathering Fredrik Bengtsson "Mary like all animals but snakes" likes(mary, X):-snake(X),!,fail. likes(mary, X):-animal(X). observe the cut before

More information

PROLOG. First simple Prolog Program. Basic elements of Prolog. Clause. NSSICT/CH/Nov., 2012.

PROLOG. First simple Prolog Program. Basic elements of Prolog. Clause. NSSICT/CH/Nov., 2012. PROLOG Prolog is a programming language for symbolic, n-numeric computations. It is specially well suited for solving problems that involve objects and relation between objects. First simple Prolog Program

More information

IKI30820 Logic Programming Negation as Failure Slide 06

IKI30820 Logic Programming Negation as Failure Slide 06 IKI30820 Logic Programming Negation as Failure Slide 06 Ari Saptawijaya (slides) Adila A. Krisnadhi (L A T E Xadaptation) Fakultas Ilmu Komputer Universitas Indonesia 2009/2010 Semester Gasal AS,AAK (Fasilkom

More information

Logic Programming and PROLOG (III)

Logic Programming and PROLOG (III) Logic Programming and PROLOG (III) Dr. Antonio L. Bajuelos Note: The most of the information of these slides was extracted and adapted from Bratko s book, Prolog Programming for Artificial Intelligence".

More information

PROLOG programming for artificial intelligence

PROLOG programming for artificial intelligence Prolog Prolog.1 Textbook and Software Title PROLOG programming for artificial intelligence Author Ivan Bratko Get the software windows Home users: download from the course site Install software to preferred

More information

Lecture 9: A closer look at terms

Lecture 9: A closer look at terms Lecture 9: A closer look at terms Theory Introduce the == predicate Take a closer look at term structure Introduce strings in Prolog Introduce operators Exercises Exercises of LPN: 9.1, 9.2, 9.3, 9.4,

More information

Prolog. Textbook and Software. Title PROLOG programming for artificial intelligence

Prolog. Textbook and Software. Title PROLOG programming for artificial intelligence Tutorial : Prolog Prolog Prolog.1 Textbook and Software Title PROLOG programming for artificial intelligence Author Ivan Bratko Get the software windows Download PL.zip from the course site Extract the

More information

Backtracking. Ch. 5 Controlling Backtracking. Backtracking. Backtracking Example. Example. Example

Backtracking. Ch. 5 Controlling Backtracking. Backtracking. Backtracking Example. Example. Example Backtracking Ch. 5 Controlling Backtracking Backtracking is the attempt to (re)satisfy a goal by exploring alternative ways to satisfy it. Chronological backtracking is backtracking in which we always

More information

3 Lists. List Operations (I)

3 Lists. List Operations (I) 3 Lists. List Operations (I) The list is the simplest yet the most useful Prolog structure. A list is a sequence of any number of objects. Example 3.1: L = [1, 2, 3], R = [a, b, c], T = [john, marry, tim,

More information

INTRODUCTION TO PROLOG

INTRODUCTION TO PROLOG INTRODUCTION TO PROLOG PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/44 STRUCTURE OF A PROLOG PROGRAM Where, declaratively, Haskell expresses a computation as a system

More information

What s the problem? fib(1,1). fib(2,1). fib(n,x) :- N>2, N1 is N-1, N2 is N-2, fib(n1,x2), fib(n2,x2).

What s the problem? fib(1,1). fib(2,1). fib(n,x) :- N>2, N1 is N-1, N2 is N-2, fib(n1,x2), fib(n2,x2). 39 What s the problem? A Fibonacci Implementation fib(1,1). fib(2,1). fib(n,x) :- N>2, N1 is N-1, N2 is N-2, fib(n1,x2), fib(n2,x2). List Concatenation conc([],l,l). conc([x L1], L2, [X L3]) :- conc(l1,l2,l3).

More information

Chapter 16. Logic Programming. Topics. Unification. Resolution. Prolog s Search Strategy. Prolog s Search Strategy

Chapter 16. Logic Programming. Topics. Unification. Resolution. Prolog s Search Strategy. Prolog s Search Strategy Topics Chapter 16 Logic Programming Summary (resolution, unification, Prolog search strategy ) Disjoint goals The cut operator Negative goals Predicate fail Debugger / tracer Lists 2 Resolution Resolution

More information

Combining Lists & Built-in Predicates

Combining Lists & Built-in Predicates Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 6 11/10/04 11/10/04 AIPP Lecture 6: Built-in Predicates 1 Collecting Results Last time we

More information

Prolog - 3 Prolog search trees + traces

Prolog - 3 Prolog search trees + traces Prolog - 3 Prolog search trees + traces 1 Review member(a, [A B]). member(a, [B C]) :- member (A,C). goal-oriented semantics: can get value assignment for goal member(a,[b C]) by showing truth of subgoal

More information

A Third Look At Prolog. Chapter Twenty-Two Modern Programming Languages, 2nd ed. 1

A Third Look At Prolog. Chapter Twenty-Two Modern Programming Languages, 2nd ed. 1 A Third Look At Prolog Chapter Twenty-Two Modern Programming Languages, 2nd ed. 1 Outline Numeric computation in Prolog Problem space search Knapsack 8-queens Farewell to Prolog Chapter Twenty-Two Modern

More information

Lecture 9. Exercises. Theory. Solutions to exercises LPN 8.1 & 8.2. Patrick Blackburn, Johan Bos & Kristina Striegnitz

Lecture 9. Exercises. Theory. Solutions to exercises LPN 8.1 & 8.2. Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9 Exercises Solutions to exercises LPN 8.1 & 8.2 Theory Solution to Exercise 8.1 Suppose we add the noun ``men'' (which is plural) and the verb ``shoot''. Then we would want a DCG which says that

More information

Chapter 5. Pure PROLOG. Foundations of Logic Programming

Chapter 5. Pure PROLOG. Foundations of Logic Programming Chapter 5 1 Outline vs. logic programming Lists in Adding Arithmetics to Adding the Cut to 2 Syntax of Pure Prolog p(x,a) :- q(x), r(x,yi). p(x, a) q(x), r(x,y i ) % Comment Ambivalent syntax: p(p(a,b),

More information

Programming Paradigms

Programming Paradigms PP 2017/18 Unit 9 Prolog: Accumulators, Order of Goals/Clauses, and the Cut 1/38 Programming Paradigms Unit 9 Prolog: Accumulators, Order of Goals/Clauses, and the Cut J. Gamper Free University of Bozen-Bolzano

More information

Connection between Lists and Terms. Prolog-Lecture 2. Unifying Lists. Unifying Lists. Steps to a Recursive Predicate. List Predicates.

Connection between Lists and Terms. Prolog-Lecture 2. Unifying Lists. Unifying Lists. Steps to a Recursive Predicate. List Predicates. Prolog-Lecture 2 Iqbal Mohomed Connection between Lists and Terms [H T] is just syntactic sugaring for the term.(h,t)?-.(h,t) = [a,b,c]. H = a T = [b, c] ; The dot operator or functor. corresponds to cons

More information

Recursive Search with Backtracking

Recursive Search with Backtracking CS 311 Data Structures and Algorithms Lecture Slides Friday, October 2, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009 Glenn G.

More information

Logic Programming. fac(n-1,m) fac(n,n m) fac(1,1) ex) factorial program query. cf) in procedural programming. ?- fac(5,120). yes?- fac(5,x).

Logic Programming. fac(n-1,m) fac(n,n m) fac(1,1) ex) factorial program query. cf) in procedural programming. ?- fac(5,120). yes?- fac(5,x). Logic Programming Logic Programming = ex) factorial program query fac(1,1) fac(n-1,m) fac(n,n m)?- fac(5,120). yes?- fac(5,x). X=120 cf) in procedural programming x=1; for (i=1;i

More information

Backtrack control. Chapter 5

Backtrack control. Chapter 5 Chapter 5 Backtrack control Prolog will automatically backtrack to satisfy a goal. This relieves the programmer of the burden of putting in backtrack explicitly. On the negative side, this might lead to

More information

10. Logic Programming With Prolog

10. Logic Programming With Prolog Copyright (C) R.A. van Engelen, FSU Department of Computer Science, 2000 10. Logic Programming With Overview Logic Programming Logic Programming Logic programming is a form of declarative programming A

More information

Lecture Chapter 6 Recursion as a Problem Solving Technique

Lecture Chapter 6 Recursion as a Problem Solving Technique Lecture Chapter 6 Recursion as a Problem Solving Technique Backtracking 1. Select, i.e., guess, a path of steps that could possibly lead to a solution 2. If the path leads to a dead end then retrace steps

More information

Topic B: Backtracking and Lists

Topic B: Backtracking and Lists Topic B: Backtracking and Lists 1 Recommended Exercises and Readings From Programming in Prolog (5 th Ed.) Readings: Chapter 3 2 Searching for the Answer In order for a Prolog program to report the correct

More information

CS 360: Programming Languages Lecture 10: Logic Programming with Prolog

CS 360: Programming Languages Lecture 10: Logic Programming with Prolog CS 360: Programming Languages Lecture 10: Logic Programming with Prolog Geoffrey Mainland Drexel University Section 1 Administrivia Midterm Tuesday Midterm is Tuesday, February 14! Study guide is on the

More information

What are Operators? - 1. Operators. What are Operators? - 2. Properties. » Position» Precedence class» associativity. » x + y * z. » +(x, *(y, z)).

What are Operators? - 1. Operators. What are Operators? - 2. Properties. » Position» Precedence class» associativity. » x + y * z. » +(x, *(y, z)). What are Operators? - 1 Functors Introduce operators to improve the readability of programs Operators syntactic sugar Based on Clocksin & Mellish Sections 2.3, 5.5 O-1 O-2 The arithmetic expression:» x

More information

Operators (2A) Young Won Lim 10/5/13

Operators (2A) Young Won Lim 10/5/13 Operators (2A) Copyright (c) 2013 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version

More information

Overview. Declarative Languages. State-space representation. Block example. Searching the state space. Solution space

Overview. Declarative Languages. State-space representation. Block example. Searching the state space. Solution space Overview Declarative Languages D7012E: Problems solving strategies Fredrik Bengtsson State-space representation Depth-first search Iterative deepening Breadth-first search A few words about complexity

More information

CAP 5602 Summer, Lesson 5: Lists. The topics 1. updating a counter 2. the list structure 3. some useful built-in predicates for lists

CAP 5602 Summer, Lesson 5: Lists. The topics 1. updating a counter 2. the list structure 3. some useful built-in predicates for lists CAP 5602 Summer, 20 Lesson 5: Lists The topics. updating a counter 2. the list structure 3. some useful built-in predicates for lists. Updating a counter Let us try to implement the increse the counter

More information

Example Programming Exercises for Prolog

Example Programming Exercises for Prolog 2007 February 3 3401 Example Prolog Programs Page 1 of 3 Example Programming Exercises for Prolog 1. Write a Prolog predicate countbt(tree, Count) to count the number of des in a binary tree that have

More information

Programming Languages

Programming Languages Programming Languages Jörg Kreiker Chair for Theoretical Computer Science Prof. Esparza TU München winter term 2010/2011 Lecture 13 Databases 3 Organizational Issues assignments corrected today: final

More information

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion simple and/or composition of goals hides complex control patterns not easily represented by traditional

More information

Agenda. CS301 Session 20. A logic programming trick. A simple Prolog program. Introduction to logic programming Examples Semantics

Agenda. CS301 Session 20. A logic programming trick. A simple Prolog program. Introduction to logic programming Examples Semantics CS301 Session 20 Introduction to logic programming Examples Semantics Agenda 1 2 A logic programming trick A two-way translator in two lines of code: translate([],[]). translate([word Words],[Mot Mots])

More information

Operators (2A) Young Won Lim 10/2/13

Operators (2A) Young Won Lim 10/2/13 Operators (2A) Copyright (c) 2013 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version

More information

Prolog Examples. Distributed Systems / Technologies. Sistemi Distribuiti / Tecnologie

Prolog Examples. Distributed Systems / Technologies. Sistemi Distribuiti / Tecnologie Prolog Examples Distributed Systems / Technologies Sistemi Distribuiti / Tecnologie Giovanni Ciatto giovanni.ciatto@unibo.it Andrea Omicini andrea.omicini@unibo.it Dipartimento di Informatica Scienza e

More information

Reviews. Arithmetic Operators (2)

Reviews. Arithmetic Operators (2) Introduction to Prolog Useful references: Clocksin, W.F. and Mellish, C.S., Programming in Prolog: Using the ISO Standard (5th edition), 2003. Bratko, I., Prolog Programming for Artificial Intelligence

More information

The Prolog to Mercury transition guide

The Prolog to Mercury transition guide The Prolog to Mercury transition guide Version 14.01.1 Thomas Conway Zoltan Somogyi Fergus Henderson Copyright c 1995 2014 The University of Melbourne. Permission is granted to make and distribute verbatim

More information

Symbolic Computation Example Programming Exercises for Prolog

Symbolic Computation Example Programming Exercises for Prolog 2001 July 16 3401 Prolog Programming Exercises Page 1 of 8 Symbolic Computation Example Programming Exercises for Prolog 1. Write a Prolog predicate countbt(tree, Count) to count the number of des in a

More information

Logic Programming. Efficiency Issues. Temur Kutsia

Logic Programming. Efficiency Issues. Temur Kutsia Logic Programming Efficiency Issues Temur Kutsia Research Institute for Symbolic Computation Johannes Kepler University of Linz, Austria kutsia@risc.uni-linz.ac.at Efficiency Issues in Prolog Narrow the

More information

Products and Records

Products and Records Products and Records Michael P. Fourman February 2, 2010 1 Simple structured types Tuples Given a value v 1 of type t 1 and a value v 2 of type t 2, we can form a pair, (v 1, v 2 ), containing these values.

More information

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion simple and/or composition of goals hides complex control patterns not easily represented by traditional

More information

Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Functional & Logic Programming - Backtracking October, 01

Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Functional & Logic Programming - Backtracking October, 01 Functional & Logic Programming - October, 01 already seen how to control execution ordering the clauses and goals can affect the speed of execution and the order of evaluation of the clauses now going

More information

815338A Principles of Programming Languages. Exercise 7 answers

815338A Principles of Programming Languages. Exercise 7 answers 815338A Principles of Programming Languages. Exercise 7 answers The topic of this exercise is logic programming in Prolog. Prolog programs can be executed at https://ideone.com/ or http://www.tutorialspoint.com/codingground.htm.

More information

Derived from PROgramming in LOGic (1972) Prolog and LISP - two most popular AI languages. Prolog programs based on predicate logic using Horn clauses

Derived from PROgramming in LOGic (1972) Prolog and LISP - two most popular AI languages. Prolog programs based on predicate logic using Horn clauses Prolog Programming Derived from PROgramming in LOGic (1972) Good at expressing logical relationships between concepts Prolog and LISP - two most popular AI languages Execution of a Prolog program is a

More information

COMP2411 Lecture 20: Logic Programming Examples. (This material not in the book)

COMP2411 Lecture 20: Logic Programming Examples. (This material not in the book) COMP2411 Lecture 20: Logic Programming Examples (This material not in the book) There are several distinct but often equivalent ways to think about logic programs 1. As computing logical consequences of

More information

Lecture 2: List algorithms using recursion and list comprehensions

Lecture 2: List algorithms using recursion and list comprehensions Lecture 2: List algorithms using recursion and list comprehensions Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense September 12, 2017 Expressions, patterns

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #06 Loops: Operators We have seen comparison operators, like less then, equal to, less than or equal. to and

More information

Prolog. Intro to Logic Programming

Prolog. Intro to Logic Programming Prolog Logic programming (declarative) Goals and subgoals Prolog Syntax Database example rule order, subgoal order, argument invertibility, backtracking model of execution, negation by failure, variables

More information

To figure this out we need a more precise understanding of how ML works

To figure this out we need a more precise understanding of how ML works Announcements: What are the following numbers: 52/37/19/6 (2:30,3:35,11:15,7:30) PS2 due Thursday 9/22 11:59PM Quiz #1 back in section Monday Quiz #2 at start of class on Thursday 9/22 o HOP s, and lots

More information

More Planning and Prolog Operators

More Planning and Prolog Operators More Planning and Prolog Operators Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 16 22/11/04 22/11/04 AIPP Lecture 16: More Planning and Operators 1 Planning continued Contents

More information

Wan Hussain Wan Ishak

Wan Hussain Wan Ishak September 1 st Session 2014/2015 (A141) Wan Hussain Wan Ishak School of Computing UUM College of Arts and Sciences Universiti Utara Malaysia (P) 04-9285150 (E) hussain@uum.edu.my (U) http://wanhussain.com

More information

Fall Lecture 3 September 4. Stephen Brookes

Fall Lecture 3 September 4. Stephen Brookes 15-150 Fall 2018 Lecture 3 September 4 Stephen Brookes Today A brief remark about equality types Using patterns Specifying what a function does equality in ML e1 = e2 Only for expressions whose type is

More information

Prolog. Logic Programming vs Prolog

Prolog. Logic Programming vs Prolog Language constructs Prolog Facts, rules, queries through examples Horn clauses Goal-oriented semantics Procedural semantics How computation is performed? Comparison to logic programming 1 Logic Programming

More information

Chapter 16. Logic Programming. Topics. Predicate Calculus and Proving Theorems. Resolution. Resolution: example. Unification and Instantiation

Chapter 16. Logic Programming. Topics. Predicate Calculus and Proving Theorems. Resolution. Resolution: example. Unification and Instantiation Topics Chapter 16 Logic Programming Proving Theorems Resolution Instantiation and Unification Prolog Terms Clauses Inference Process Backtracking 2 Predicate Calculus and Proving Theorems A use of propositions

More information

Lecture Notes on Prolog

Lecture Notes on Prolog Lecture Notes on Prolog 15-317: Constructive Logic Frank Pfenning Lecture 15 October 24, 2017 1 Introduction Prolog is the first and still standard backward-chaining logic programming language. While it

More information

Logic Programming in Prolog

Logic Programming in Prolog Cristian Giumale / Lecture Notes 1 Logic Programming in Prolog There are important advantages of using programming systems based on logic: Data are neatly separated from the inference engine, which is

More information

Scheme Quick Reference

Scheme Quick Reference Scheme Quick Reference COSC 18 Winter 2003 February 10, 2003 1 Introduction This document is a quick reference guide to common features of the Scheme language. It is by no means intended to be a complete

More information

Lecture Notes on Prolog

Lecture Notes on Prolog Lecture Notes on Prolog 15-317: Constructive Logic Frank Pfenning Lecture 14 October 15, 2009 In this lecture we introduce some simple data structures such as lists, and simple algorithms on them such

More information

Constraint (Logic) Programming

Constraint (Logic) Programming Constraint (Logic) Programming Roman Barták Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic bartak@ktiml.mff.cuni.cz Sudoku Combinatorial puzzle, whose goal is to enter

More information

Prolog (cont d) Remark. Using multiple clauses. Intelligent Systems and HCI D7023E

Prolog (cont d) Remark. Using multiple clauses. Intelligent Systems and HCI D7023E Intelligent Systems and HCI D703E Lecture : More Prolog Paweł Pietrzak Prolog (cont d) 1 Remark The recent version of SWI- Prolog displays true and false rather than and no Using multiple clauses Different

More information

- Wikipedia Commons. Intro Problem Solving in Computer Science 2011 McQuain

- Wikipedia Commons. Intro Problem Solving in Computer Science 2011 McQuain Recursion Recursion 1 Around the year 1900 the illustration of the "nurse" appeared on Droste's cocoa tins. This is most probably invented by the commercial artist Jan (Johannes) Musset, who had been inspired

More information

Arithmetic Terms are Symbolic

Arithmetic Terms are Symbolic Arithmetic Terms are Symbolic Evaluation of an arithmetic term into a numeric value must be forced. That is, 1+2 is an infix representation of the relation +(1,2). This term is not an integer! Therefore?-

More information

Lecture 4: Backtracking and Cut 1. Lecture 4: Backtracking and Cut. Ahmed Sallam

Lecture 4: Backtracking and Cut 1. Lecture 4: Backtracking and Cut. Ahmed Sallam LOGIC Lecture 4: Backtracking and Cut 1 Lecture 4: Backtracking and Cut Ahmed Sallam Backtracking and Cut Lecture 2 4: Backtracking and Cut Review Backtracking Cut Cut uses Problems with Cut Backtracking

More information

Problem Solving and Search

Problem Solving and Search Problem Solving and Search Ulle Endriss Institute for Logic, Language and Computation University of Amsterdam [ http://www.illc.uva.nl/~ulle/teaching/pss/ ] Ulle Endriss 1 Table of Contents Lecture 1:

More information

Recursion, Structures, and Lists

Recursion, Structures, and Lists Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 4 04/10/04 30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists 1 The central ideas of Prolog

More information

36 Modular Arithmetic

36 Modular Arithmetic 36 Modular Arithmetic Tom Lewis Fall Term 2010 Tom Lewis () 36 Modular Arithmetic Fall Term 2010 1 / 10 Outline 1 The set Z n 2 Addition and multiplication 3 Modular additive inverse 4 Modular multiplicative

More information

Lecture 11: Feb. 10, 2016

Lecture 11: Feb. 10, 2016 CS323: AI (Hands on with Prolog) Spring 2016 Lecturer: K.R. Chowdhary Lecture 11: Feb. 10, 2016 : Professor of CS (VF) Disclaimer: These notes have not been subjected to the usual scrutiny reserved for

More information

About these slides Prolog programming hints

About these slides Prolog programming hints About these slides Prolog programming hints COMP9414-2008 Semester 1 Ronnie Taib (ronniet@cse.unsw.edu.au)! Practical notes on using Prolog! Some hints for better code! LECTURE NOTES ALWAYS PREVAIL!! The

More information

SML A F unctional Functional Language Language Lecture 19

SML A F unctional Functional Language Language Lecture 19 SML A Functional Language Lecture 19 Introduction to SML SML is a functional programming language and acronym for Standard d Meta Language. SML has basic data objects as expressions, functions and list

More information

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions Greg Plaxton Theory in Programming Practice, Spring 2005 Department of Computer Science University of Texas at Austin

More information

To figure this out we need a more precise understanding of how ML works

To figure this out we need a more precise understanding of how ML works Announcements: What are the following numbers: 74/2/70/17 (2:30,2:30,3:35,7:30) PS2 due Thursday 9/20 11:59PM Guest lecture on Tuesday 9/25 o No RDZ office hours next Friday, I am on travel A brief comment

More information

The object level in Prolog. Meta-level predicates and operators. Contents. The flow of computation. The meta level in Prolog

The object level in Prolog. Meta-level predicates and operators. Contents. The flow of computation. The meta level in Prolog Lecture 8 Meta-level predicates and operators Contents Object level vs. meta level Controlling flow of computation Checking and dismantling expressions Comparison operators The object level in Prolog Prolog

More information

Lecture Notes on Prolog

Lecture Notes on Prolog Lecture Notes on Prolog 15-317: Constructive Logic Frank Pfenning Lecture 14 October 27, 2015 In this lecture we introduce some simple data structures such as lists, and simple algorithms on them such

More information

Logic Programming (PLP 11) Prolog: Arithmetic, Equalities, Operators, I/O, Natural Language Parsing

Logic Programming (PLP 11) Prolog: Arithmetic, Equalities, Operators, I/O, Natural Language Parsing Logic Programming (PLP 11) Prolog: Arithmetic, Equalities, Operators, I/O, Natural Language Parsing Carlos Varela Rennselaer Polytechnic Institute February 9, 2015 C. Varela 1 Arithmetic Goals N>M N

More information

Scheme Quick Reference

Scheme Quick Reference Scheme Quick Reference COSC 18 Fall 2003 This document is a quick reference guide to common features of the Scheme language. It is not intended to be a complete language reference, but it gives terse summaries

More information

Lecture Notes on Dynamic Programming

Lecture Notes on Dynamic Programming Lecture Notes on Dynamic Programming 15-122: Principles of Imperative Computation Frank Pfenning Lecture 23 November 16, 2010 1 Introduction In this lecture we introduce dynamic programming, which is a

More information

IN112 Mathematical Logic

IN112 Mathematical Logic Institut Supérieur de l Aéronautique et de l Espace IN112 Mathematical Logic Lab session on Prolog Christophe Garion DMIA ISAE Christophe Garion IN112 IN112 Mathematical Logic 1/ 31 License CC BY-NC-SA

More information

PROgramming in LOGic PROLOG Recursion, Lists & Predicates

PROgramming in LOGic PROLOG Recursion, Lists & Predicates PROgramming in LOGic PROLOG Recursion, Lists & Predicates CSC9Y4 1 Recursion Recursion in Prolog means placing in the body of a rule a call to the predicate which occurs in the head of the rule. Here is

More information

More Non-logical Features of Prolog

More Non-logical Features of Prolog More Non-logical Features of Prolog Prof. Geraint A. Wiggins Centre for Cognition, Computation and Culture Goldsmiths College, University of London Contents Commit operators Implementing Negation as Failure

More information

CSEN403 Concepts of Programming Languages. Topics: Logic Programming Paradigm: PROLOG Search Tree Recursion Arithmetic

CSEN403 Concepts of Programming Languages. Topics: Logic Programming Paradigm: PROLOG Search Tree Recursion Arithmetic CSEN403 Concepts of Programming Languages Topics: Logic Programming Paradigm: PROLOG Search Tree Recursion Arithmetic Prof. Dr. Slim Abdennadher 8.2.2015 c S. Abdennadher 1 Logic Programming versus Prolog

More information

CSE 115. Introduction to Computer Science I

CSE 115. Introduction to Computer Science I CSE 115 Introduction to Computer Science I Note about posted slides The slides we post will sometimes contain additional slides/content, beyond what was presented in any one lecture. We do this so the

More information

Lecture Notes on Ints

Lecture Notes on Ints Lecture Notes on Ints 15-122: Principles of Imperative Computation Frank Pfenning Lecture 2 August 26, 2010 1 Introduction Two fundamental types in almost any programming language are booleans and integers.

More information

IN112 Mathematical Logic

IN112 Mathematical Logic Institut Supérieur de l Aéronautique et de l Espace IN112 Mathematical Logic Lab session on Prolog Christophe Garion DMIA ISAE Christophe Garion IN112 IN112 Mathematical Logic 1/ 31 License CC BY-NC-SA

More information

CS 381: Programming Language Fundamentals

CS 381: Programming Language Fundamentals CS 381: Programming Language Fundamentals Summer 2017 Introduction to Logic Programming in Prolog Aug 2, 2017 Outline Programming paradigms Logic programming basics Introduction to Prolog Predicates, queries

More information

Shared Variables and Interference

Shared Variables and Interference Illinois Institute of Technology Lecture 24 Shared Variables and Interference CS 536: Science of Programming, Spring 2018 A. Why Parallel programs can coordinate their work using shared variables, but

More information

CSC384: Intro to Artificial Intelligence Prolog Tutorials 3&4. Hojjat Ghaderi, Fall 2006, University of Toronto

CSC384: Intro to Artificial Intelligence Prolog Tutorials 3&4. Hojjat Ghaderi, Fall 2006, University of Toronto CSC384: Intro to Artificial Intelligence Prolog Tutorials 3&4 1 Debugging Programs in Prolog We talked about the graphical debugger under Windows. Now, the text-based debugger: You can put a breakpoint

More information

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator 1 A Closer Look at the / Operator Used for performing numeric calculations C++ has unary, binary, and ternary s: unary (1 operand) - binary ( operands) 13-7 ternary (3 operands) exp1? exp : exp3 / (division)

More information

Exercises on the Fundamentals of Prolog

Exercises on the Fundamentals of Prolog 1 Introduction Exercises on the Fundamentals of Prolog These exercises are intended to help reinforce material taught in the lectures of CIS335 course in Prolog. They do not contribute any marks to the

More information

6.001 Notes: Section 17.5

6.001 Notes: Section 17.5 6.001 Notes: Section 17.5 Slide 17.5.1 Now, let's look at one example in which changing the evaluation model allows us to explore a very different kind of computational problem. Our goal is to show how

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #23 Loops: Precedence of Operators

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #23 Loops: Precedence of Operators Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #23 Loops: Precedence of Operators This one more concept that we have to understand, before we really understand

More information

Arithmetic (12A) Young W. Lim 4/9/14

Arithmetic (12A) Young W. Lim 4/9/14 Copyright (c) 2013-2014. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free

More information

Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg

Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg 1 Logic Programming True facts: If I was born in year B, then in year Y on my birthday I turned Y-B years old I turned

More information

Functional Logic Programming. Kristjan Vedel

Functional Logic Programming. Kristjan Vedel Functional Logic Programming Kristjan Vedel Imperative vs Declarative Algorithm = Logic + Control Imperative How? Explicit Control Sequences of commands for the computer to execute Declarative What? Implicit

More information

Tests, Backtracking, and Recursion

Tests, Backtracking, and Recursion Tests, Backtracking, and Recursion Artificial Intelligence Programming in Prolog Lecture 3 30/09/04 30/09/04 AIPP Lecture 3: Rules, Results, and Backtracking 1 Re-cap A Prolog program consists of predicate

More information

Operational Semantics

Operational Semantics 15-819K: Logic Programming Lecture 4 Operational Semantics Frank Pfenning September 7, 2006 In this lecture we begin in the quest to formally capture the operational semantics in order to prove properties

More information

Recursive Search with Backtracking

Recursive Search with Backtracking continued CS 311 Data Structures and Algorithms Lecture Slides Friday, February 2, 29 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 25 29 Glenn

More information

Fall Lecture 13 Thursday, October 11

Fall Lecture 13 Thursday, October 11 15-150 Fall 2018 Lecture 13 Thursday, October 11 n queens One s favorite ipod app n queens Queens attack on row, column, or diagonal Task: put n queens safely on an n-by-n board British Museum algorithm

More information

An introduction to logic programming with Prolog

An introduction to logic programming with Prolog An introduction to logic programming with Prolog Dr. Constantinos Constantinides Department of Computer Science and Software Engineering Concordia University A running example: A family genealogy tree

More information

This lecture covers: Prolog s execution strategy explained more precisely. Revision of the elementary Prolog data types

This lecture covers: Prolog s execution strategy explained more precisely. Revision of the elementary Prolog data types This lecture covers: Prolog s execution strategy explained more precisely The execution strategy has been presented as simply placing all matching clauses onto the stack. In fact, Prolog is slightly more

More information