Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part 2
Some Prolog Prolog is a logic programming language
Page 1 Nov 09, 2004CAS 701, McMaster Logic Programming and Prolog Presenter: John Xu Date: Nov 09, 2004.
Cs7120 (Prasad)L22-MetaPgm1 Meta-Programming
Chapter 11 :: Logic Languages
Prolog for Dummies Ulf Nilsson Dept of Computer and Information Science Linköping University.
Declarative Programming Lists in PROLOG Autumn 2014.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
Introduction to Prolog, cont’d Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash 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 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).
Lecture 10 Logic programming, PROLOG (PROgramming in LOGic)
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Chapter 12 - Logic Programming
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Comp 205: Comparative Programming Languages Declarative Programming Languages Logic Programming Horn-Clause Logic Prolog Lecture notes, exercises, etc.,
Programming Languages Third Edition
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for Ch.1 of Bratko For CSCE 580 Sp03 Marco Valtorta.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for CSCE 330 Based on Bratko and Van Emden Marco.
Multiplexer as a Universal Function Generator
CSE (c) S. Tanimoto, 2005 Logic Programming 1 Logic Programming Outline: Motivation Examples: The Grandmother relation Formulation in Prolog Logic,
Reflexive -- First sentence of proof is: (1) Let x  Z (2) Let (x,x)  R. (3) Let (x,x)  I (4) Let x  R.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
P-Phunck - Prolog1 Introduction to Prolog Brian Paden.
Formal Models of Computation Part II The Logic Model
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
CS 321 Programming Languages and Compilers Prolog part 2.
COSC 2P93 Logic Programming Instructor: Brian Ross Instructor: Brian Ross Texts: Texts: Prolog Programming for Artificial Intelligence,4e, Ivan Bratko,
F = ∑m(1,4,5,6,7) F = A’B’C+ (AB’C’+AB’C) + (ABC’+ABC) Use X’ + X = 1.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
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.
1CS3754 Class Notes 14B, John Shieh, Figure 2.5: Further steps in the unification of (parents X (father X) (mother bill)) and (parents bill.
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.
CS 603: Programming Languages Lecture 25 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
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?
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
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.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Kenneth C. Louden, Chapter 12 - Logic Programming Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Programming Languages Third Edition Chapter 4 Logic Programming.
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 CS Programming Languages Class 19 November 2, 2000.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Logic Programming and Declarative Paradigm. Declarative Programming Declare goals, not actions Query by Example HTML Prolog –AI work –Natural Language.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
For Friday No reading Prolog Handout 2. Homework.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Logic Programming: Prolog Notes for CSCE 190 Based on Bratko,
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
The portion of a Prolog interpreter that executes queries (goals) is known as the inference engine. An inference engine is a kind of theorem prover, using.
Prolog CSC 481 Team Project Winter 2009 ES-Tools Team.
CSE 341, S. Tanimoto Logic Programming -
Knowledge Representation and Inference
Notes 9: Inference in First-order logic
Programming Paradigms and Languages
Programming Languages 2nd edition Tucker and Noonan
CSE 3302 Programming Languages
Presentation transcript:

Prolog The language of logic

History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed early version of Prolog for natural language processing. Warren: mid 70’s First version of Prolog that was efficient.

Characteristics Prolog approximates first-order logic. Every program is a set of Horn clauses. Inference is by resolution. Search is by backtracking with unification. Basic data structure is term or tree. Variables are unknowns not locations. Prolog does not distinguish between inputs and outputs. It solves relations/predicates.

Strawberry Prolog

Example Facts: () –likes(john,mary). –likes(john,X). % Variables begin with capital Queries –?- likes(X,Y). – X=john, y=Mary. % hit “;” for more –?- likes(X,X). –X=john.

Example Rules –likes(john,X) :- likes(X,wine). % :- = if –likes(john,X):- female(X), likes(X,john). Some Facts: –likes(bill,wine). female(mary). female(sue). Query: ? - likes(john,Y). –Y = bill ; –no.

Family father(a,b). father(e,d). mother(c,b). mother(d,f). parent(X,Y) :- father(X,Y). parent(X,Y) :- mother(X,Y). grandfather(X,Y):-father(X,Z),parent(Z,Y).

Finding max in a list max(X, [X|T]):- geq(X,T). max(X, [Y|T]):- Y =< X, max(X, T). geq(X, [Y]):- Y =< X. geq(X, [H|T]):- H =< X, geq(X, T). ?-geq(11, [8, 9, 11, 12]). no ?-max(24, [23, 8, 24, 21, 14]). Yes ?-max(24, [23, 8, 24, 21, 14]). Yes.

A different program for max

Merge sorting in Prolog merge(X,[],X). merge([], X, X). merge([X|Y],[P|Q],[X|Z]):- X =< P, merge(Y,[P|Q],Z). merge([X|Y],[P|Q],[P|Z]):- P =< X, merge([X|Y],Q,Z). % ?-merge([3, 17], [11, 12, 20, 26], X), write(X), nl.

Merge sorting in Prolog split([X,Y], [X], [Y]). split([X],[],[X]). split([X,Y|Z],[X|X1], [Y|X2]):-split(Z, X1, X2). %?-split([12, 6, 7, 11, 9], X, Y), write(X), nl, write(Y), nl.

Merge sorting in Prolog msort([],[]). msort([X], [X]). msort(X,Y):- split(X, X1, X2), msort(X1,Y1), msort(X2, Y2), merge(Y1, Y2, Y). ?-msort([6, 12, 20, 7, 3, 21, 36, 14, -3], X), write(X), nl.

Example involving an arithmetic operation Write a Prolog program to compute the prefix sums of elements in an array. Example: ?-prefix_sum([1, 3, -4, 8, 2], X), write(X), nl. Output: Compiling the file: F:\fall09\cs480fa09\programs\prefix_sum 0 errors, 0 warnings. [1,4,0,8,10] Yes.

Prolog function to implement prefix sum First we create a function add that adds X to each member of a list: add(X,[Y],[Z]):- Z is X + Y. add(X, [H|T], [P|Q]):- P is X + H, add(X,T,Q). % some test cases ?-add(3, [-2, 12, 8, 3], X), write(X), nl. ?-add(3, [-2], X), write(X), nl.

Prolog function to implement prefix sum We can use add to write prefix_sum: prefix_sum([X],[X]). prefix_sum([H|T], [H|T1]):- prefix_sum(T, T2), add(H, T2, T1), nl. %?-prefix_sum([2, 1], X), write(X), nl. ?-prefix_sum([1, 3, -4, 8, 2], X), write(X), nl.

Permutation & Insert Write a program to generate all the permutations of a given sequence. insert(X,L, [X|L]). insert(X,[H|T],[H|T1]):- insert(X,T,T1). ?-insert(1, [3, 2, 4], X), write(X), nl, fail. perm([],[]). perm([H|T],P):-perm(T,T1),insert(H,T1,P). ?-perm([1, 2, 3, 4], X), write(X), nl, fail.