COP4020 Programming Languages Logic Programming Prof. Xin Yuan.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
SLD-resolution Introduction Most general unifiers SLD-resolution
Chapter 11 :: Logic Languages
Prolog for Dummies Ulf Nilsson Dept of Computer and Information Science Linköping University.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 CMSC 471 Fall 2002 Class #6 – Wednesday, September 18.
Prolog Programming (Volume 5) Dr W.F. Clocksin. List cells + Unification = Great Idea Normally we use ‘proper lists’, which are defined according to the.
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.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Automated Reasoning Systems For first order Predicate Logic.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
1 Knowledge Based Systems (CM0377) Lecture 8 (Last modified 5th March 2001)
Bahar Pamuk. Outline Introduction Syntaxa Clauses, Programs and Queries List Manipulation Operators Backtracking, Cuts References.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
COP4020 Programming Languages Logic programming and Prolog Prof. Xin Yuan.
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Inference and Resolution for Problem Solving
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
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.
LING 388 Language and Computers Lecture 16 10/23/03 Sandiway FONG.
Prolog Ross (Tate). Filling in the Blanks Rather than reverse((a,b)) returns (b,a) Rather than reverse((a,b)) returns (b,a) What X makes reverse((a,b),
Artificial Intelligence Chapter 17 Knowledge-Based Systems Biointelligence Lab School of Computer Sci. & Eng. Seoul National University.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Discrete Maths Objective to show the close connection between recursive definitions and recursive functions , Semester 2, Recursion.
P-Phunck - Prolog1 Introduction to Prolog Brian Paden.
Formal Models of Computation Part II The Logic Model
CS 321 Programming Languages and Compilers Prolog part 2.
Logic Programming Two possible work modes: 1.At the lab: Use SICstus Prolog. To load a prolog file (*.pl or *.pro extension) to the interpreter, use: ?-
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Introduction to AgentSpeak and Jason for Programming Multi-agent Systems (1) Dr Fuhua (Oscar) Lin SCIS Athabasca University June 19, 2009.
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.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Prolog Programming. 2 DATA STRUCTURES IN PROLOG PROGRAMMING TECHNIQUES CONTROL IN PROLOG CUTS.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
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 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
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 and Inductive Logic Programming.
© 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 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
COP4020 Programming Languages Prolog Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
For Friday No reading Prolog Handout 2. Homework.
LECTURE 21 Logic Programming. LOGIC PROGRAMMING RESOLUTION STRATEGIES.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
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),
Logic Programming Lecture 2: Unification and proof search.
Decision Trees DEFINITION: DECISION TREE A decision tree is a tree in which the internal nodes represent actions, the arcs represent outcomes of an action,
Binary search tree. Removing a node
Carlos Varela Rensselaer Polytechnic Institute November 17, 2017
COP4020 Programming Languages
First-Order Logic and Inductive Logic Programming
Tests, Backtracking, and Recursion
Chapter 12 :: Logic Languages
Carlos Varela Rennselaer Polytechnic Institute November 15, 2016
Presentation transcript:

COP4020 Programming Languages Logic Programming Prof. Xin Yuan

COP4020 Spring /13/2016 Topics More on Prolob Prolog by examples

COP4020 Spring /13/2016 Control Features Prolog offers built-in constructs to support a form of control-flow  \+ G negates a (sub-)goal G  ! (cut) terminates backtracking for a predicate  fail always fails (so to trigger backtracking) Examples  ?- \+ member(b, [a,b,c]). no  ?- \+ member(b, []). yes  We can (re)define: if(Cond, Then, Else) :- Cond, !, Then. if(Cond, Then, Else) :- Else.  ?- if(true, X=a, X=b). X = a ; % type ';' to try to find more solutions no  ?- if(fail, X=a, X=b). X = b ; % type ';' to try to find more solutions no

COP4020 Spring /13/2016 Control Features Prolog offers built-in constructs to support a form of control-flow  G 1 ; G 2 forms an “or”: try G 1 then G 2 if G 1 failed  G 1 -> G 2 ; G 3 forms an if-then-else: if G 1 then G 2 else G 3  true is a true goal and acts like a no-op  repeat always succeeds (for looping) Examples:  We can use an “or” instead of two clauses: rainy(C) :- ( C = rochester ; C = seattle ).  Using an if-then-else: guess(X) :- ( X = heads -> format(“~s~n”, [“Guessed it!”]) ; format(“~s~n”, [“Sorry, no luck”]) ).  Repeat drawing a random X until X=1 (and then cuts backtracking): do_until_one :- repeat, X is random(10), X = 1, !.

Meta-Logical Meta-logical predicates perform operations that require reasoning about terms  X == Y true if X is identical to Y (vice versa X \= Y )  var(X) true if X is uninstantiated (vice versa nonvar(Y) )  ground(X) true if X is a ground term, has no variables  atom(X) true if X is an atom  functor(X, F, N) true if X is a functor F with N arguments  X =.. [F,A1,A2,…,An] true if X a functor F with arguments Impact:  Pure logic has no ordering constraints, e.g. X “or” Y = Y “or” X X=3, var(X).  var(X), X=3.  Meta-logical predicates are order sensitive and influence control  Theoretically, cannot be expressed using predicate definitions with a finite number of clauses COP4020 Spring /13/2016

COP4020 Spring /13/2016 Example 1 fact(n) = 1*2*…*n fact(1, 1). fact(N, X) :- M is N-1, fact(M, Y), X is Y*N.

COP4020 Spring /13/2016 Example 2 Sum the elements of a list sum([1,2,3], X). X=6

COP4020 Spring /13/2016 Example 3 Sum the elements of a list sum([1,2,3], X). X=6 sum([], 0). sum([X|T], Y) :- sum(T, Y1), Y is X + Y1.

COP4020 Spring /13/2016 Example 4 Generate a list of n copies of x Fill(3, x, Y) Y=[x,x,x]

COP4020 Spring /13/2016 Example 4 Generate a list of n copies of x Fill(3, x, Y) Y=[x,x,x] fill(0, _, []). fill(N, X, [X|T]):- M is N-1, fill(M, X, T).

COP4020 Spring /13/2016 Example 5 Replace x with y in list xs subst(3, 0, [8,2,3,4,3,5], X). X=[8,2,0,4,0,5]

COP4020 Spring /13/2016 Example 5 Replace x with y in list xs subst(3, 0, [8,2,3,4,3,5], X). X=[8,2,0,4,0,5] replace1(_, _, [], []). replace1(X, Y, [X|T], [Y|T1]) :- replace1(X, Y, T, T1). replace1(X, Y, [Z|T], [Z|T1]) :- replace1(X, Y, T, T1).

COP4020 Spring /13/2016 Example 6 Search on binary search tree, where [] are leaves and [val, left, right] is a node bsearch[4, [3, [], [4, [],[]]], X). X=1

COP4020 Spring /13/2016 Example 6 Search on binary search tree, where [] are leaves and [val, left, right] is a node bsearch[4, [3, [], [4, [],[]]], X). X=1 bsearch(_, [], 0). bsearch(X, [X | _], 1). bsearch(X, [Y, L, _], Z) :- X < Y, bsearch(X, L, Z). bsearch(X, [Y, _, R], Z) :- X > Y, bsearch(X, R, Z).

COP4020 Spring /13/2016 Example 7 Binary tree insertion, where [] are leaves and [val, left, right] is a node binsert(X, [], [X, [], []]). binsert(X, [X | T], [X | T]). binsert(X, [Y, L, R], [Y, L1, R]) :- X < Y, binsert(X, L, L1). binsert(X, [Y, L, R], [Y, L, R1]) :- X > Y, bsearch(X, R, R1).

Example 8 Scheme: (genlist ‘( )) => (() (1) (1 2) (1 2 3) ( )) Prolog removelast([_], []). removelast([X|T], [X|T1]) :- removelast(T, T1). append(X, [], [X]). append(X, [Y|T], [Y|T1]):- append(X, T, T1). genlist([], [[]]). genlist(X, Y) :- removelast(X, T), genlist(T, Y1), append(X, Y1, Y). COP4020 Spring /13/2016

Example 9 Search an item in list, if found, return original list; otherwise, insert at the beginning of the list. Search: search(_, [], 0). search(X, [X|_], 1). search(X, [_|T], C) :- search(X, T, C). COP4020 Spring /13/2016

Example 9 Search an item in list, if found, return original list; otherwise, insert at the beginning of the list. Searchinsert searchinsert(X, Y, Y):- search(X, Y, 1). searchinsert(X, Y, [X|Y]) :- search(X, Y, 0). COP4020 Spring /13/2016

Example 10 Remove all items in a list equal to x  (remove 1 ‘( )) => ( ) remove(_, [], []). remove(X, [X|T], T1):- remove(X, T, T1). remove(X, [Y|T], [Y|T1]) :- remove(X, T, T1). COP4020 Spring /13/2016

Example 11 Insert into a sorted list (insertsort 14 ‘( )) => ( ) insertsorted(X, [], [X]). insertsorted(X, [Y|T], [X, Y|T]) :- X =< Y. insertsorted(X, [Y|T], [Y|T1]) :- X > Y, insertsorted(X, T, T1). COP4020 Spring /13/2016

Example 12 Insertion sort isort([], []). isort([X|T], Y):- isort(T, T1), insertsorted(X, T1, Y). COP4020 Spring /13/2016