28-4-2008P-Phunck - Prolog1 Introduction to Prolog Brian Paden.

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.
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
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
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.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
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).
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
4. PROLOG Data Objects And PROLOG Arithmetic
Logic Languages Source: Scott, Michael Programming Language Pragmatics 3rd Ed.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
CPS 506 Comparative Programming Languages
Formal Models of Computation Part II The Logic Model
CS 321 Programming Languages and Compilers Prolog part 2.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
ProLog PROgramming in LOGic. Description of Prolog Prolog stands for PROgramming in LOGic. It is a nonprocedural and declarative language.
1-1 Introduction Logic programming languages, sometimes called declarative programming languages Express programs in a form of symbolic logic Use a logical.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
F28PL1 Programming Languages Lecture 16: Prolog 1.
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.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
Lecture 9b: Prolog Heshaam Faili University of Tehran The language of logic Concepts Examples.
By: Cory Canter CSC 415 Programming Languages. History  Created by Alain Colmerauer, Phillipe Roussel and Robert Kowalski in 1971  Started as a natural.
Dr. Muhammed Al-Mulhem ICS An Introduction to Logical Programming.
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
CS 363 Comparative Programming Languages Logic Programming Languages.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Prolog Harry R. Erwin, PhD COMM2M University of Sunderland.
Programming Languages Third Edition Chapter 4 Logic Programming.
Logic Programming (LP) expresses code using of a restricted form of symbolic logic. LP programs are declarative, rather than algorithmic. An LP program.
COMP307 Artificial Intelligence Xiaoying Gao Victoria University of Wellington Lecture 2:1 
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
Knowledge Based Information System
1-1 An Introduction to Logical Programming Sept
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.
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.
The Prolog Language by Piyabut Thavilthicakul
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Prolog Overview Syntax Mechanism Summary. Overview PROLOG : Programming with Logic Uses predicate (first-order) calculus History: Roots: J.A. Robinson.
Section 16.5, 16.6 plus other references
Logic Programming Languages
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
PROLOG.
Logic Programming Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Chapter 12 :: Logic Languages
Logic Programming Language
PROLOG.
Presentation transcript:

P-Phunck - Prolog1 Introduction to Prolog Brian Paden

P-Phunck - Prolog2 History Creators: Alain Colmerauer & Phillipe Roussel  University of Aix-Marseille 1972 Robert Kowalski  University of Edinburgh Named by: Philippe Roussel  programmation en logique First compiler: David H. D. Warren

P-Phunck - Prolog3 More History 1984 – Began working on standard 1987 – ISO Standard Group Formed 1995 – ISO Standard published  Most current New version expected “by early 2000”

P-Phunck - Prolog4 Predicate Logic First order predicate calculus Propositions  Atomic propositions & compound terms Resolution  Inference rule Horn clauses  Single atomic proposition on left side  Empty left side

P-Phunck - Prolog5 Data Types Atom  Letters or numbers'x', 'longConstant17', '“Var”' Variable  Start with uppercase 'X', 'NotanAtom'  Anonymous variable - '_' Fact  'doesntKnow( “Prolog”, brian ).'

P-Phunck - Prolog6 Variables Two states  Instantiated Variable has a set value  Uninstantiated Has not been assigned a value Closed-world assumption  Only true if it can be proven

P-Phunck - Prolog7 Data Structures Compound term  Functor and arguments  'weird( prolog, yes ).' Queries  '?- good( presentation, brian )'  Only returns 'yes' or 'no' Conjunction  Multiply queries and facts

P-Phunck - Prolog8 Special Compound Terms List  Head First element in list  Tail List containing everything else in list except the head Last element is empty list - '[]'  '[]', '.' - '[a,[b,[c]]]' == 'a.b.c.[]' String  Special list – all elements are atoms '[“This”,is,a,string,in,”Prolog”]'

P-Phunck - Prolog9 More Lists Special list operators  '[]' - Empty list  '[X|Y]' – X is head, Y is tail  '””' - convert list to list containing ascii values Recursive search for element of list member(X,[X|_]). member(X,[_|Y]) :- member(X,Y). ?- member(c,[a,b,c,d,e,f]). yes

P-Phunck - Prolog10 Built in Operators Compound construction  ',' - conjunction, ';' - disjunction Comparison  'X = Y' – equality, 'X \= 2' – inequality  'X == Y' – strict equality, 'X \== 2' – strict inequality  'X < Y', 'X =< Y', - less than, less than equal to  'X > Y', 'X >= Y' – greater than, greater than equal to Arithmetic  'X + Y', 'X - Y', 'X * Y', 'X / Y', 'X mod Y'

P-Phunck - Prolog11 Arithmetic Operators that stand out  '\' - not, '\=' - not equal  '= =' - greater equal Perform arithmetic  'is' ' Y = Y/X' – tests equality 'Y is Y/X' – assigns numeric value to Y Right hand side of 'is' must be algebraic

P-Phunck - Prolog12 Looping No iteration, only recursion factorial(0,1). factorial(N,F) :- N>0, N1 is N-1, factorial(N1,F1), F is N * F1. ?- factorial(3,X). X=6

P-Phunck - Prolog13 I/O Special file  'user' Output  'tell(X).' - creates/opens file  'telling(X).' - gets open file name  'told' – closes file Input  see, seeing, seen Misc  'tab(X)', 'nl'

P-Phunck - Prolog14 Backtracking User query  Searches database for fact declaring it true Matching fact (head rule) found  Mark place in database, move on to next goal No matching fact found  Attempt to re-satisfy goal Undo last change and try again Cut  Shortcuts backtracking '!' symbol

P-Phunck - Prolog15 Backtracking in Action father(mary,george). father(john,george). father(sue,harry). father(george,edward). father(X) :- father(_,X). Database Query ?- father(Y). Result Y=george; Y=harry; Y=edward; no

P-Phunck - Prolog16 Lists and Backtracking reverse([],[]). reverse([Head|Tail],List) :- reverse(Tail,Result), append(Result,[Head],List). trace. ?- reverse([a,b,c],Q).

P-Phunck - Prolog17 Reverse Example (1) 1 Call: reverse([a,b,c],_6)? (2) 2 Call: reverse([b,c],_65636)? (3) 3 Call: reverse([c],_65646)? (4) 4 Call: reverse([],_65656)? (4) 4 Call: Exit: reverse([],[])‏ (5) 4 Call: append([],[c],_65646)? (5) 4 Exit: append([],[c],[c])‏ (3) 3 Exit: reverse([c],[c])‏ (6) 3 Call: append([c],[b],_65636)? (7) 4 Call: append([],[b],_25)? (7) 4 Exit: append([],[b],[b])‏ (6) 3 Exit: append([c],[b],[c,b])‏ (2) 2 Exit: reverse([b,c],[c,b)‏ (8) 2 Call: append([c,b],[a],_6)? (9) 3 Call: append([b],[a],_32)? (10) 4 Call: append([],[a],_39)? (10) 4 Exit: append([],[a],[a])‏ (9) 3 Exit: append([b],[a],[b,a])‏ (8) 2 Exit: append([c,b],[a],[c,b,a])‏ (1) Exit: reverse([a,b,c],[c,b,a])‏

P-Phunck - Prolog18 How to Cut sum_to(1,1) :- !. sum_to(N,Res) :- N1 is N – 1, sum_to(N1,Res), Res is Res + N. Database Query ?- sum_to(5,X). Result X = 15; no

P-Phunck - Prolog19 Useful Built in Predicates Conditional  'true', 'fail' – Always their namesakes  'atom(X)', 'integer(X)' – true if X is an atom or int  'atomic(X)' – X is not a variable  'asserta(X)' – adds clause X to start of database  'assertz(X)' – adds clause to end of database  'X =.. L' - Univ X is variable L is list where head is functor and tail is arguments

P-Phunck - Prolog20 Example: Binary Tree lookup(H,w(H,G,_,__,G) :- !. lookup(H,w(H1,_,Before,_),G) :- aless(H,H1), lookup(H,Before,G). lookup(H,w(H1,_,_,After),G) :- not(aless(H,H1)), lookup(H,After,G). ?- lookup(key,X,582), lookup(val,X,356). ImplementationUsage

P-Phunck - Prolog21 Example: Quicksort partition([], _, [], []). partition([X|Xs], Pivot, Smalls, Bigs) :- ( Smalls = [X|Rest], partition(Xs, Pivot, Rest, Bigs)‏ ; Bigs = [X|Rest], partition(Xs, Pivot, Smalls, Rest)‏ ). quicksort([]) --> []. quicksort([X|Xs]) --> { partition(Xs, X, Smaller, Bigger) }, quicksort(Smaller), [X], quicksort(Bigger).

P-Phunck - Prolog22 Example: Turing Machine turing(Tape0, Tape) :- perform(q0, [], Ls, Tape0, Rs), reverse(Ls, Ls1), append(Ls1, Rs, Tape). perform(qf, Ls, Ls, Rs, Rs) :- !. perform(Q0, Ls0, Ls, Rs0, Rs) :- symbol(Rs0, Sym, RsRest), once(rule(Q0, Sym, Q1, NewSym, Action)), action(Action, Ls0, Ls1, [NewSym|RsRest], Rs1), perform(Q1, Ls1, Ls, Rs1, Rs). symbol([], b, []). symbol([Sym|Rs], Sym, Rs). action(left, Ls0, Ls, Rs0, Rs) :- left(Ls0, Ls, Rs0, Rs). action(stay, Ls, Ls, Rs, Rs). action(right, Ls0, [Sym|Ls0], [Sym|Rs], Rs). left([], [], Rs0, [b|Rs0]). left([L|Ls], Ls, Rs, [L|Rs]).

P-Phunck - Prolog23 Compilers ISO Compliant  GNU Prolog  SWI-Prolig  YAP Visual Prolog (Turbo Prolog)‏ 

P-Phunck - Prolog24 Sources Clocksin, W.F and Mellish, C.S. Programming in Prolog. Berlin. Springer-Verlag: 1984 Sebesta, Robert W. Concepts of Programming Languages: Eighth Edition