1Lecture 12 Introduction to Prolog SWI-Prolog. 2Lecture 12 Introduction to Prolog Welcome to SWI-Prolog (Version 4.0.9) Copyright (c) 1990-2000 University.

Slides:



Advertisements
Similar presentations
PROLOG 8 QUEENS PROBLEM.
Advertisements

Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Modern Programming Languages
Prolog.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
”assert” and ”retract”
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).
Logic Programming Chapter 15 Part 2. Prolog Lists The list is Prolog’s basic data structure Lists are a series of Prolog terms, separated by commas Each.
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
1 Prolog III. 2 Lists [ ] is the empty list. [x, 2+2, [a, b, c]] is a list of three elements. The first element in the list is its “head”. The list with.
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
For Monday Take home exam due Exam 1. For Wednesday Read chapter 10, sections 1-2 Prolog Handout 4.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
”assert” and ”retract”  Modifying Programs Dynamically  Prolog’s Execution Tree.
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/29.
LING 364: Introduction to Formal Semantics Lecture 3 January 19th.
CSE (c) S. Tanimoto, 2005 Logic Programming 1 Logic Programming Outline: Motivation Examples: The Grandmother relation Formulation in Prolog Logic,
1 LING 438/538 Computational Linguistics Sandiway Fong Lecture 4: 8/31.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
P-Phunck - Prolog1 Introduction to Prolog Brian Paden.
Formal Models of Computation Part II The Logic Model
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.
Tutorial 6 CSCI3230 ( First Term) Hands on
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
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 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1Lecture 12 Introduction to Prolog Logic Programming Prolog.
CS 603: Programming Languages Lecture 25 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
Summary. likes(tom,jerry). likes(mary,john). likes(tom,mouse). likes(tom,jerry). likes(jerry,cheeze). likes(mary,fruit). likes(john,book). likes(mary,book).
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Lists in Prolog Sections 3.1, 3.2. Lists n List = sequence of values –[1, 2, 3, 4, 5] –[bob, brian, cathy, mark, david, loretta] –[birds(4, calling),
Lecture course: Heuristics & Prolog for Artificial Intelligence Applications Notes on Prolog from Professor Bruce Batchelor, DSc.
CSE 341, S. Tanimoto Logic Programming Intro - 1 Logic Programming Introduction Motivation. Sample logic programs.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Knowledge Based Information System
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
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.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Chapter Three: Operators, Arithmetic 1. Chapter three: 3.3Operator notation 3.4Arithmetic 2.
1 member(X,[Y| _ ] ) :- X = Y. member(X, [ _ | Y]) :- member(X, Y). It would be easier to write this as: member(X,[X| _ ]). member(X, [ _ | Y]) :- member(X,
More on Prolog syntax Already seen program statement types: Already seen program statement types: rules: p(X) :- q(X,Y), r(Y,z). rules: p(X) :- q(X,Y),
Section 16.5, 16.6 plus other references
COSC 2P93 Prolog: Debugging
Prolog a declarative language
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Mathematical Induction Recursion
Prolog Lists.
Introduction to Logic Programming and Prolog
Prolog a declarative language
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Prolog III.
Prolog Lists.
Introduction to Logic Programming and Prolog
Chapter 2: Prolog (Introduction and Basic Concepts)
Prolog III Lists 8-Jul-19.
Presentation transcript:

1Lecture 12 Introduction to Prolog SWI-Prolog

2Lecture 12 Introduction to Prolog Welcome to SWI-Prolog (Version 4.0.9) Copyright (c) University of Amsterdam. Copy policy: GPL-2 (see For help, use ?- help(Topic). or ?- apropos(Word). ?- SWI-Prolog You may query Prolog now. Use the built-in Predicate “halt” to exit Prolog.

3Lecture 12 Introduction to Prolog ?- append([abc,def],[ghi,lmn],L). L = [abc, def, ghi, lmn] Yes ?- append([abc,def],L,[abc,def,ghi,lmn]). L = [ghi, lmn] Yes ?- Query mode

4Lecture 12 Introduction to Prolog ?- [user]. |: Do not forget the period at the end of your input. At the “|:” prompt, type in what you want entered into your database. male(john). female(mary). father(john,mary). great(massey). has_a_job(mary). After the last fact is entered and at the “|:” prompt, enter a ctrl-D to exit the consult mode. An example is shown on the next slide. Consult mode You can enter consult mode by typing in “[user]” at the “?-” Prompt:

5Lecture 12 Introduction to Prolog ?- [user]. |: likes(tom,jerry). |: likes(mary,john). |: likes(tom,mouse). |: likes(tom,jerry). |: likes(jerry,cheeze). |: likes(mary,fruit). |: likes(john,book). |: knows(mary,book). |: knows(tom,john). |: % user compiled 0.01 sec, 64 bytes Yes ?- listing. likes(tom, jerry). likes(mary, john). likes(tom, mouse). likes(tom, jerry). likes(jerry, cheeze). likes(mary, fruit). likes(john, book). knows(mary,book). knows(tom,john). Yes

6Lecture 12 Introduction to Prolog listing. listing(likes/2). Listing your predicates

7Lecture 12 Introduction to Prolog ?- listing(likes/2). /* we could have said “listing(likes).” in this case*/ likes(tom, jerry). likes(mary, john). likes(tom, mouse). likes(tom, jerry). likes(jerry, cheeze). likes(john,book). ?- listing(knows). knows(mary,book). knows(tom,john).

8Lecture 12 Introduction to Prolog trace. notrace. Or simply n. spy(likes/2). nospy Tracing your program

9Lecture 12 Introduction to Prolog Call: (7) likes(_G332, _G333) ? creep Exit: (7) likes(tom, jerry) ? creep X = tom Y = jerry ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(mary, john) ? creep X = mary Y = john ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(tom, mouse) ? creep X = tom Y = mouse ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(tom, jerry) ? creep X = tom Y = jerry ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(jerry, cheeze) ? creep ?- trace. Yes [trace] ?- likes(X,Y).

10Lecture 12 Introduction to Prolog X = jerry Y = cheeze ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(mary, fruit) ? creep X = mary Y = fruit ; Redo: (7) likes(_G332, _G333) ? creep Exit: (7) likes(john, book) ? creep X = john Y = book ; Fail: (7) likes(_G332, _G333) ? creep No

11Lecture 12 Introduction to Prolog Structures

12Lecture 12 Introduction to Prolog ?- owns(john,book(prolog,author( _ ),year( X ),edition( _ ))),X >1990. owns(john,book(prolog,author(clocksin_and_mellish),year(1994),edition(4))). owns(victoria,book(prolog,author(bratko),year(2001),edition(3))). owns(john,book(prolog,clocksin_and_mellish)). owns(victoria,book(prolog,bratko)). owns(john, book). owns(victoria, book). owns(george,book).

13Lecture 12 Introduction to Prolog Arithmetic

14Lecture 12 Introduction to Prolog ?- A is 2+3, B is A-1, C is A*2, D is C/B, E is C // B. A = 5 B = 4 C = 10 D = 2.5 E = 2 +additionX + Y - subtractionX - Y *multiplicationX * Y /divisionX / Y //division (integer)X // Y modmodulo (remainder)X mod Y **powerX ** Y isequalsZ is X + Y Arithmetic Operators

15Lecture 12 Introduction to Prolog population(us,275000). population(china, ). population(nz,4000). Population(india, ). land(us,3000). land(china,4000). land(nz,250). land(india,3288). concen(X,Y):- population(X,P), land(X,L), Y is P / L.

16Lecture 12 Introduction to Prolog ?- concen(X,Y). X = us Y = Yes ?- concen(X,Y). X = us Y = ; X = china Y = ; X = nz Y = 16 ; X = india Y = ; No

17Lecture 12 Introduction to Prolog Comparison

18Lecture 12 Introduction to Prolog X \= Y True if X not equal to Y X < Y True if X is less than Y X > Y True if X is greater than Y X =< Y True if X is less than or equal to Y X >= Y True if X is greater than or equal to Y Example: Given two positive integers X and Y, their greatest common devisor “D” is found according to the following: if X = Y then D = X if X < Y then D = greatest common devisor of X and Y – X if X > Y then D = do the same as above with X and Y interchanged greatest common devisor of 6 and 6 is 6 greatest common devisor of 27 and 9 is 9 greatest common devisor of 20 and 15 is 5 X = Y True if X equals to Y

19Lecture 12 Introduction to Prolog gcd(X,Y,D) :- X < Y, Z is Y – X, gcd(X, Z, D). gcd(X,Y,D) :- Y < X, gcd(Y, X, D). We could have written the last clause above, as gcd(X,Y,D) :- Y < X, Z is X – Y, gcd(Z, Y, D). ?- gcd(20,15,D). D = 5 Yes ?- gcd(0,15,D). ERROR: Out of local stack gcd(X,X,X).

20Lecture 12 Introduction to Prolog fact(0,1). fact(N,F) :- N > 0, N1 is N – 1, fact(N1, F1), F is N * F1. ?- edit(fact). % d:/prolog/fact.pl compiled 0.00 sec, 64 bytes Yes ?- fact(6,X). X = 720 Yes ?- fact(3,X). X = 6 Yes Another Example n*(n-1)…2*1 n=1,2,3… n!= 1 n=0

21Lecture 12 Introduction to Prolog Lists

22Lecture 12 Introduction to Prolog The list is the main data structure in Prolog. A list may be empty: [ ] or it may contain one or more terms (constants, variables or structures). [a,b,c,d,e] [5,8,3,9,7] [the, boy, run] A list can be split into a head and a tail: [H|T]. grades(john, [70,87,90,58]). ?- grades(john, [H|T]). H = 70 T = [87,90,58]

23Lecture 12 Introduction to Prolog Heads and tails of lists List Head Tail [1,2,3]1[2,3] [red,blue,[green,yellow]]red[blue,[green,yellow]] [ X+Y, a-b] X+Y[a-b] [the,[boy,run]]the[[boy,run]] [[a],b,c,d][a][b,c,d] [ ] ____

24Lecture 12 Introduction to Prolog ?- [a, b, c] = [Head | Tail]. Head = a Tail = [b, c] ?- [the,[boy,run]]=[Head|Tail]. Head = the Tail = [[boy, run]] ?- [a, b, c] = [X, Y | Tail]. X = a Y = b Tail = [c] ?- [a, b, c] = [X|Y,Z]. No

25Lecture 12 Introduction to Prolog Converting a structure to a list ?- likes(john,mary) =.. X. X = [likes, john, mary] Anything in double is equivalent to a list of ASCII values: ?- "abc"=X. X = [97, 98, 99]

26Lecture 12 Introduction to Prolog member(X,[Y| _ ] ) :- X = Y. member(X, [ _ | Y]) :- member(X, Y). It would be easier to write this as: member(X,[X| _ ]). member(X, [ _ | Y]) :- member(X, Y). ?- member(1, [3,4,5,8,1,9]). Yes ?- member(X, [prolog, c, ada, haskell]). X= prolog; X= c X= ada; X= haskell; No Recursion and Lists

27Lecture 12 Introduction to Prolog Other Examples change(you, i). change(are, [am, not]). change(french, australian). change(do, no). change(X, X). /* catchall */ alter([ ], [ ]). alter([H|T], [X|Y]) :- change(H, X), alter(T,Y). ?- alter([you,are,french],R). R = [i, [am, not], australian] Yes ?- alter([you,are,a,computer],R). R = [i, [am, not], a, computer] Yes ?-

28Lecture 12 Introduction to Prolog asserta/1. assertz/1 assert/1 retract/1 retractall/1 Example: assertz(fib(N,F)).

29Lecture 12 Introduction to Prolog :-dynamic fib fib(1,1). fib(2,1). fib(N,F) :- N > 2, N1 is N-1, fib(N1,F1), N2 is N-2, fib(N2,F2), F is F1 + F2, asserta(fib(N,F)). ?- fib(8, F). F = 21 ?- fib(6,F). F = 8

30Lecture 12 Introduction to Prolog F(6) + f(5)f(4) + f(3) f(2) f(2) f(1) f(4) f(3) + f(3) f(2) 1 + f(2) f(1)