Prolog CS 171/271. Before we begin… Download and install SWI-Prolog, version 5.6.4: bin/nph-download/SWI- Prolog/w32pl564.exe.

Slides:



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

Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
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).
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for CSCE 330 Based on Bratko and Van Emden Marco.
Chapter 12 - Logic Programming
Chapter 8: The Logical Paradigm Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
CPSC 322 Introduction to Artificial Intelligence September 15, 2004.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
Programming Languages Third Edition
Logic Programming Language (Natural Language Processing)
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.
INFERENCE IN FIRST-ORDER LOGIC IES 503 ARTIFICIAL INTELLIGENCE İPEK SÜĞÜT.
P-Phunck - Prolog1 Introduction to Prolog Brian Paden.
1 Software Development Topic 2 Software Development Languages and Environments.
PROLOG KAIST Gunwoo Park 1. What’s Prolog? 2  General purpose logic programming language  Declarative, which means that the program logic is.
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.
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
Introduction to Logic Programming with Prolog (Section 11.3)
Declarative vs Procedural Programming  Procedural programming requires that – the programmer tell the computer what to do. That is, how to get the output.
Getting Started with Visual Prolog
1 Artificial Intelligence Prolog for. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux It's licensed under GPL, therefore free.
Artificial Intelligence LECTURE 2 ARTIFICIAL INTELLIGENCE LECTURES BY ENGR. QAZI ZIA 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.
ARTIFICIAL INTELLIGENCE DR. ABRAHAM AI a field of computer science that is concerned with mechanizing things people do that require intelligent.
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Programming Languages Tucker and Noonan – 2e Chapter 15 – Part 1 Logic Programming “Q: How many legs does a dog have if you call its tail a leg? A: Four.
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?
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
The AI War LISP and Prolog Basic Concepts of Logic Programming
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).
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 Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Logical and Functional Programming
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
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.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
Functional Programming Languages Logic Programming Languages
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Prolog Notes for CSCE 330 Based on Bratko, Poole, and Van Emden.
Knowledge Based Information System
Artificial Intelligence CS370D
First-Order Logic Reading: C. 8 and C. 9 Pente specifications handed back at end of class.
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
For Friday No reading Prolog Handout 2. Homework.
For Wednesday No reading Prolog handout 2 Chapter 9, exercise 4.
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,
Done By :- -Nesreen Basem -Sara nabil -Rawan Prolog Program.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
3.5 Programming paradigms
CPSC 322 Introduction to Artificial Intelligence
Logic Programming Language
Chapter 2: Prolog (Introduction and Basic Concepts)
PROLOG.
Presentation transcript:

Prolog CS 171/271

Before we begin… Download and install SWI-Prolog, version 5.6.4: bin/nph-download/SWI- Prolog/w32pl564.exe bin/nph-download/SWI- Prolog/w32pl564.exe

What is Prolog? A logic programming language “Prolog” stands for “Programming in Logic” (or “Programmation en Logique” in French) Designed in 1972 by Alain Colmerauer and Robert Kowalski (much later than Lisp) Used in AI programming, expert systems, and computational linguistics Functional programming (ala Lisp) is possible, but very cumbersome

Logic Programming Creates logical models that describe the world in which a problem exists Uses first-order logic (but can also be used with propositional logic) A subset of declarative programming Creates a set of conditions that describe a solution space Two phases: declaration and interpretation

Step 1: Declaration Creating a knowledge base (KB) of sentences describing the world Declaring facts fun(ai). (AI is fun!) likes(mark,bacon). (Mark likes bacon) Defining rules heartbroken(X) :- loves(X,Y), not(loves(Y,X)). (X is heartbroken if X loves Y and Y does not love X… *sniff*) Sentences end with a period

A simple Knowledge Base orbits(mercury, sun). orbits(venus, sun). orbits(earth, sun). orbits(mars, sun). orbits(moon, earth). orbits(phobos, mars). orbits(deimos, mars). planet(P) :- orbits(P,sun). satellite(S) :- orbits(S,P), planet(P).

Step 2: Interpretation Deriving new sentences from the sentences in the KB by making queries Uses a Prolog interpreter SWI-Prolog, GNU Prolog, etc. Knowledge bases must first be loaded/consulted using consult(‘filename.pl’).

Some simple queries ?- consult(‘solar.pl’). % Is the moon a satellite? ?- satellite(moon). % Is the sun a planet? ?- planet(sun). % Is Uranus a planet? ?- planet(uranus). % What are the planets? ?- planet(Planet). % What objects orbit Mars? ?- orbits(X, mars). % Is the moon made of cheese? ?- made_of_cheese(moon).

Another KB example parents(william, diana, charles). parents(henry, diana, charles). parents(charles, elizabeth, philip). parents(diana, frances, edward). parents(anne, elizabeth, philip). parents(andrew, elizabeth, philip). parents(edwardW, elizabeth, philip). married(diana, charles). married(elizabeth, philip). married(frances, edward). married(anne, mark). parent(C,M) :- parents(C,M,D). parent(C,D) :- parents(C,M,D). sibling(X,Y) :- parents(X,M,D), parents(Y,M,D). % What’s wrong with this? aORuDirect(C, A) :- parent(C,P), sibling(P,A). aORuMarr(C, A) :- aORuDirect(C,X), married(X,A). aORuMarr(C, A) :- aORuDirect(C,X), married(A,X). aORu(C,A) :- aORuDirect(C,A). aORu(C,A) :- aORuMarr(C,A).

Predicates Used to express facts or statements about objects and their relationships Examples: likes heartbroken orbits Have arguments teaches(jpv,cs171). (2 arguments) Names/atoms are alphanumeric, can contain underscores, begin with a lowercase letter Meanings of predicates, arguments, and order of arguments are arbitrary (but should be descriptive and consistent!)

Variables Can be used to stand for any object Are instantiated during matching Examples: Planet X Scope is statement-wide Usually used in rules, but can also be used in facts goes_to_heaven(Dog) :- dog(Dog). (All dogs go to heaven.) must_perish(Thing). (All things must perish.) Variable names are alphanumeric, can contain underscores, must begin with an uppercase letter Use the underscore to indicate an anonymous variable female(X) :- mother(X,_). (All mothers are female, regardless of who the child is.)

Queries Describe problems to be solved Consist of goals to be satisfied Example: father(X) :- male(X), parent(X,_). Goals are male(X) and parent (X,_). Goals are checked against facts in the KB Rules are satisfied if all the goals in the “if” part (in the body, separated by commas) are satisfied Variables can take on any value Press semi-colon to look for further matches Uses backtracking to satisfy goals in all possible ways (“brute-forcing” it)

Prolog and FOL Rules are usually in the form of Horn clauses :- is  reversed Backtracking in Prolog is known in FOL terms as backward chaining Begins with a goal (the query) Recursively builds a set of substitutions that satisfy the goals necessary to conclude the goal All variables in Prolog are assumed to be universally quantified (though skolemization can still be done)

Recursion and Functions % Make sure they’re not left-recursive ancestor(Person, Ancestor) :- parent(Person, Ancestor). ancestor(Person, Ancestor) :- parent(Person, P1), ancestor(P1, Ancestor). % Functions are defined as relations fac(0,1). fac(N,F) :- N > 0, M is N - 1, fac(M,Fm), F is N * Fm.

Other Prolog features Support for tree structures and lists (similar to Haskell) Many built-in predicates (arithmetic, logic, I/O, etc.) and operators Can parse context-free grammars through definite clause grammars (DCGs)

Towers of Hanoi in Prolog hanoi(N) :- move(N,a,b,c). move(0,_,_,_). move(N,From,To,Using) :- M is N-1, move(M,From,Using,To), print_move(From,To), move(M,Using,To,From). print_move(From,To) :- write([move,disk,from,From,to,To]), nl.

Resources SWI-Prolog Visual Prolog GNU Prolog InterProlog (Java) P# (.NET) /dlpsharp.html /dlpsharp.html