About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines
Chapter 11 :: Logic Languages
1. An Overview of Prolog.
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.
Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2.
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.
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 COMP313A Programming Languages Logic Programming (3)
Prolog Programming for Artificial Intelligence Three edition 2001
Prolog programming Introduction to Prolog
4. PROLOG Data Objects And PROLOG Arithmetic
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
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.
© 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 CILOG User Manual Bayesian Networks Seminar Sep 7th, 2006.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
1 Artificial Intelligence CSC 361 Prof. Mohamed Batouche Department of Computer Science CCIS – King Saud University Riyadh, Saudi Arabia
(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
CS 321 Programming Languages and Compilers Prolog part 2.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
1 Lecture 6 Logic Programming introduction to Prolog, facts, rules Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
For Wednesday No new reading Prolog handout 2 Chapter 9, exercise 4.
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.
MB: 5 March 2001CS360 Lecture 41 Programming in Logic: Prolog Lists and List Operations Readings: Sections 3.1 & 3.2.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
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 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.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 9: A closer look at terms Theory –Introduce the == predicate –Take a closer look at term structure.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Prolog for Linguists Symbolic Systems 139P/239P John Dowding Week 3, October 22, 2001
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
ISBN Chapter 16 Logic Programming Languages.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Knowledge Based Information System
Artificial Intelligence CS370D
For Friday No reading Prolog Handout 2. Homework.
History Automatic Theorem Proving Robinson 1965 “PROgramming in LOGic” Kowalski, 1974 Deduction as computation “Prolog”, Colmerauer, 1973 Edinburgh Prolog,
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.
For Wednesday No reading Prolog handout 2 Chapter 9, exercise 4.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Logic Programming: Prolog Notes for CSCE 190 Based on Bratko,
1 Artificial Intelligence CS370D Prolog programming Declarative meaning of Prolog programs and Lists representation.
Section 16.5, 16.6 plus other references
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog programming Introduction to Prolog
Prolog programming Introduction to Prolog (part2)
Prolog programming Introduction to Prolog (part3)
Chapter Two: Syntax and Meaning of Prolog Programs
Programming Techniques
Prolog programming Introduction to Prolog (part4)
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language

Prolog Program  A Prolog program is defined by a set of Predicates  Each Predicate has a unique Functor and Arity parent(Parent, Child) a predicate whose functor is “parent” with arity 2. Each Predicate is defined by a sequence of Clauses: A Clause is either a Factor a Rule

Defining relations by facts parent( pam, bob). % Pam is a parent of Bob parent( tom, bob). parent( tom, liz). parent( bob, ann). parent( bob, pat). parent( pat, jim). pam lizbob jim patann tom

Term A Term is either: Atomic, Variable, or Compound  Atomic terms can be either atoms or numbers Atoms can be a sequence of alphanumeric (including ‘_’) characters starting with a lower case letter, or a sequence of any characters embedded in single quote marks  Variables start with a capital letter or _ character Variables that occur with the same name in the same clause represent the same variable, except for “_” ( The anonymous variable, which always represents a unique variable ).  A compound term is a structure with a functor and N arguments

Defining relations by rules Rules have: A condition part (body) the right-hand side of the rule A conclusion part (head) the left-hand side of the rule Syntax: concl(…) :- cond1(…), …, condN(…).

Rules Define the “ offspring ” relation: Fact: offspring( liz, tom). Rule: offspring( Y, X) :- parent( X, Y). pam lizbob jim patann tom X Y parentoffspring

mother( X, Y) :- parent( X, Y), female( X). grandparent( X, Z) :- parent( X, Y), parent( Y, Z). parent X Y mother female Z parent X Y grandparent

Facts: declare things that are always true facts are clauses that have a head and the empty body Rules: declare things that are true depending on a given condition rules have the head and the (non-empty) body Questions: the user can ask the program what things are true questions only have the body ?- parent( bob, pat). ?- parent( bob, pat), parent( pat, jim). pam lizbob jim patann tom

BUILT-IN Predicate: consult/1 consult(‘code.pl’). reconsult(‘code.pl’). [‘code.pl’].

Structural Induction and Recursion e.g. simple arithmetic using compound terms to represent integers: s(0).% 1 s(s(0)).% 2 s(s(s(0))).% 3 …  is_number(0).  Is_number(s(N)):- number(N).

predecessor( X, Z):- parent( X, Z). predecessor( X, Z):- parent( X, Y), predecessor( Y, Z). parent X Y predecessor Z parent X Y predecessor Y2 parent X Y1 predecessor Z parent

How Prolog works To answer a question, Prolog tries to satisfy all the goals. To satisfy a goal means to demonstrate that the goal is true, assuming that the relations in the program is true. Prolog accepts facts and rules as a set of axioms, and the user ’ s question as a conjectured theorem. For now; lets have a pragmatic view…

To find out when our question can be true; Prolog tries to prove it; to satisfy all the goals. To satisfy a goal means to demonstrate that the goal is true, assuming that the relations in the program are true. So our program -facts and rules we defined- would be axioms, and the user ’ s question is the conjectured theorem. A rough sketch of prolog’s algorithm would be: prove(Query): if Query is empty succeed else choose it’s first goal, G. for each clause C of the program whose head matches G: make NewQuery from Query by replacing G with C’s body. prove(NewQuery).

And-Or Tree Note that in each step we are free to choose which clause to use (OR). When we choose the clause; we must satisfy all of it’s conditions (AND).

parent( pam, bob). parent( tom, bob). parent( tom, liz). parent( bob, ann). parent( bob, pat). parent( pat, jim). predecessor( X, Z) :- parent( X, Z). % Rule pr1 predecessor( X, Z) :- parent( X, Y), % Rule pr2 predecessor( Y, Z). predecessor( tom, pat) predecessor( bob, pat) parent( tom, Y) predecessor( Y, pat) parent( tom, pat) parent( bob, pat) no By rule pr1 By rule pr2 By fact parent( tom, bob) Y = bob By rule pr1 yes ?- predecessor( tom, pat).

Trace & Notrace | ?- trace. The debugger will first creep -- showing everything (trace) (15 ms) ture. {trace} | ?- predecessor( tom, pat). 1 1 Call: predecessor(tom,pat) ? 2 2 Call: parent(tom,pat) ? 2 2 Fail: parent(tom,pat) ? 2 2 Call: parent(tom,_79) ? 2 2 Exit: parent(tom,bob) ? 3 2 Call: predecessor(bob,pat) ? 4 3 Call: parent(bob,pat) ? 4 3 Exit: parent(bob,pat) ? 3 2 Exit: predecessor(bob,pat) ? 1 1 Exit: predecessor(tom,pat) ? true ? | ?- notrace. The debugger is switched off true.

Linked Lists Prolog allows a special syntax for lists: [a,b,c] is a list of 3 elements [ ] is a special atom indicating a list with 0 elements Internally, Prolog lists are regular Prolog terms with the functor ‘.’ (so called “dotted pairs”) [a,b,c] = ‘.’(a, ‘.’(b, ‘.’(c, []))). The symbol | in a list indicates “rest of list”, or the term that is the 2 nd argument of a dotted pair. [a,b,c] = [a|[b,c]]. [Head|Tail] is a common expression for dividing a list into

% list(?List) list([]). list([_Head|Tail]):- list(Tail).  Since Prolog is untyped, we don’t have to know anything about Head except that it is a term. Example: list/1

% member(?Element, ?List) member(Element, [Element|_Tail]). member(Element, [_Head|Tail]):- member(Element, Tail). Example: member/2

% delete(+Element, +List, -NewList) % delete/3 succeeds if NewList results from % removing one occurrence of Element from List. delete(Element, [Element|Tail], Tail). delete(Element, [Head|Tail], [Head|NewTail]):-delete(Element, Tail, NewTail). Example: delete/3

% append(+List1, +List2, -List3) % append/3 succeds if List3 contains all the % elements of List1, followed by all the elements % of List2. append([], List2, List2). append([Head|List1], List2, [Head|List3]):- append(List1, List2, List3). Example: append/3

% "naive reverse": nreverse(+List, - ReversedList). nreverse([], []). nreverse([Head|Tail], ReversedList):- nreverse(Tail, ReversedTail), append(ReversedTail, [Head], ReversedList). Example: “naïve” reverse

pure Prolog vs non-logical built-ins All the examples so far have been “pure Prolog”; Contain no built-ins with non-logical side-effects Prolog has many built-in predicates:  Type checking of terms  Arithmetic  Control execution  Input and output  Modify the program during execution  Perform aggregation operations Use of non-logical built-in predicates usually effects the reversibility of your program.

Type-checking Built-in Predicates var(X)– is true when X is an uninstantiated variable. nonvar(X)– is true when X is not a variable. atom(X)– is true when X is a symbolic constant. number(X)- is true when X is a number atomic(X)– is true when atom(X) or number(X). compound(X)- is true when X is a compound term.

Term constructor/selectors: functor/3, arg/3 functor(+Term, ?Functor, ?Arity) % Find the Functor and Arity of Term functor(?Term, +Functor, +Arity) % Constructs a new Term with Functor and Arity arg(+N, +Term, ?SubTerm) % Unifies SubTerm with the Nth argument of Term