Prolog a declarative language

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 11 :: Logic Languages
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Inference and Reasoning. Basic Idea Given a set of statements, does a new statement logically follow from this. For example If an animal has wings and.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
ISBN Chapter 16 Logic Programming Languages.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Prolog Language Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 16 Logic Programming Languages.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
CS 330 Programming Languages 12 / 05 / 2006 Instructor: Michael Eckmann.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Logic Programming Tasanawan Soonklang. Programming paradigms Imperative Object-oriented Functional Logic Procedural programming Non-procedural programming.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
CPS 506 Comparative Programming Languages
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 330 Programming Languages 12 / 04 / 2007 Instructor: Michael Eckmann.
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
1-1 Introduction Logic programming languages, sometimes called declarative programming languages Express programs in a form of symbolic logic Use a logical.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
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.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
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.
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.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Prolog Harry R. Erwin, PhD COMM2M University of Sunderland.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
© 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 330 Programming Languages 11 / 25 / 2008 Instructor: Michael Eckmann.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
Dr. Muhammed Al-Mulhem ICS An Introduction to Prolog.
Knowledge Based Information System
1-1 An Introduction to Logical Programming Sept
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”
1-1 An Introduction to Prolog Sept Prolog statements Like other programming languages, Prolog consists of collection of statements. Prolog.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Copyright 1999Paul F. Reynolds, Jr. Foundations of Logic Programming.
CS 330 Programming Languages 12 / 06 / 2007 Instructor: Michael Eckmann.
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
Logic Programming Languages
CS 3304 Comparative Languages
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Logic Programming Languages
Logic Programming Languages
Prolog a declarative language
Prolog a declarative language
Logic Programming Language
Programming Languages 2nd edition Tucker and Noonan
Logic Programming Languages
Logic Programming & Prolog
Chapter 2: Prolog (Introduction and Basic Concepts)
PROLOG.
Presentation transcript:

Prolog a declarative language A program in a declarative programming language consists of assertions rather than assignments and control flow statements. These declarations are actually statements or propositions in symbolic logic. Program doesn’t state how a result is to be computed but what the result is. I’m using the word assertions to disambiguate the meaning of declaration as used in imperative languages.

Prolog program composition Collections of statements Kinds of statements Facts Rules Statements are constructed from terms A term is a constant, a variable, or a structure

Prolog program elements Constants are Atoms or integers Atoms are Symbolic values of Prolog String of letters, digits, and underscores that begins with a lowercase letter OR String of any printable ASCII characters delimited by apostrophes

Prolog program elements Variables are any string of letters, digits, and underscores that begins with an upper case letter not bound to types by declarations bound to values and types by instantiations Instantiation occurs only in resolution process not like variables in imperative languages p. 627 – instantiations last only as long as it takes to satisfy one complete goal, which involves the proof or disproof of one proposition.

Prolog program elements Structures have the general form functor(parameter_list) functor is any atom & it identifies the structure parameter_list can be any list of atoms variables or other structures. are the way to specify facts in Prolog can be thought of as objects are relations are predicates Objects because they allow facts to be stated in terms of several related atoms Relations because they state relationships among terms Predicates – when its context specifies it to be a query

Prolog facts Facts state relations explicitly man(paul). rich(joan). SWI-Prolog doesn’t like spaces between the name of the relation and the left parenthesis. PDProlog doesn’t care. are propositions that are assumed to be true are the statements from which new information can be inferred are headless Horn clauses have no intrinsic semantics

Prolog rules Rules are headed Horn clauses right side is the antecedent or if part left side is the consequent or then part consequent must be a single term, antecedent may be either a single term or a conjunction rich(joan) :- has_money(joan). rich(joan) :- has_health(joan),has_job(joan). Conjunctions contain multiple terms separated by logical and :- is read as if , means and Rules can be related to a known theorm in mathematics from which a conclusion can be drawn if the set of conditions is satisfied. IF the antecendent is true then the consequent must be true. Headed Horn clauses are called rules because they state ruls of implications

Example parent(X,Y) :- mother(X,Y). parent(X,Y) :- father(X,Y). grandparent(X,Z) :- parent(X,Y), parent(Y,Z). sibling(X,Y) :- mother(M,X),mother(M,Y),father (F,X), father(F,Y).

Prolog as a theorem proving model proposition is the form of the theorem that we want to prove or disprove is called a goal is called a query syntactic form is that of a headless Horn clause man(fred). returns yes OR returns no yes means that system proved goal was true under given database of facts and relationships no means either the goal was proved false OR system was simply unable to prove it. How does system know whether your headless Horn clause is a fact or a query (goal)? By whether you type asserta or assertz before the headless Horn clause

Sidebar 1 The process of determining useful values for variables is called unification. The temporary assigning of values to variables to allow unification is called instantiation. Resolution is an inference rule that allows inferred propositions to be computed from given propositions.

Sidebar 2 Horn clauses come in two forms Single atomic proposition on the left side OR Empty left side Left side is called the head Horn clauses with left sides are called headed Horn clauses Horn clauses with empty left sides are called headless Horn clauses.

Example Database: p(X) :- q(X), not (r(X)). r(X) :- w(X), not (s(X)). q(a). q(b). q(c). s(a). w(a). w(b). Queries p(a). p(b). p(c).

Example Database: bachelor(P) :- male(P), not (married(P)). male(henry). male(tom). married(tom). Queries: bachelor(henry). bachelor(tom). bachelor(Who). married(Who). not(married(Who)).

Prolog operators relational operators Assignment operator \= = >= <= > < Assignment operator is is peculiar - not like ordinary assignment operator

Behavior of Prolog “is” takes an arithmetic expression as its right operand takes a variable as its left operand all variables in the arithmetic expression must already be instantiated left-side variable cannot be previously instantiated Discussion Examples: A is B / 17 + C X is X + 1

Prolog Arithmetic Example speed(ford,100). speed(chevy,105). speed(dodge,95). speed(volvo,80). time(ford,20). time(chevy,21). time(dodge,24). time(volvo,24). distance (X,Y) :- speed(X,Speed), time(X,Time), Y is Speed * Time.

Short Prolog backtracking example /* facts */ likes(jake,chocolate). likes(jake,apricots). likes(darcie,licorice). likes(darcie,apricots). /* query or goal */ likes(jake,What), likes(darcie,What).

Miscellanea parameters , arguments arity – number of parameters/arguments in a parameter list anonymous variable – used when don’t care mother (X, _). likes (_, What). logical operators , means and ; means or \+ means not in SWI - Prolog not means not in PD Prolog

Miscellania retract( ). - can be used to delete a single fact or relation forget( ). – can be used to remove a file you have consulted halt. – exits gracefully from SWI-Prolog differences between listing. dir p. dir.

Prolog lists Lists are written in square brackets with commas between the list’s element [condor,whooping_crane, dusky_seaside_sparrow] can be used as the argument of a relation rarebird([condor,whooping_crane, dusky_seaside_sparrow]). can be queried can take advantage of anonymous variables have a head and a tail can be processed recursively

List Example rarebird(condor). rarebird(What). Database: Queries: rarebird([condor,whooping_crane, dusky_seaside_sparrow]). Queries: rarebird(condor). rarebird(What).

List Example - continued Database: rarebird([condor,whooping_crane, dusky_seaside_sparrow]). noteworthy(Bird) :- rarebird([Bird, __,__]). noteworthy(Bird) :- rarebird([__,Bird, __]). noteworthy(Bird) :- rarebird([__,__, Bird]). Queries: rarebird(condor). rarebird(What). noteworthy(condor). noteworthy(Who).

List Example - continued Database: rarebird([condor,whooping_crane, dusky_seaside_sparrow]). noteworthy(Bird) :- rarebird([Bird, __,__]). noteworthy(Bird) :- rarebird([__,Bird, __]). noteworthy(Bird) :- rarebird([__,__, Bird]). rarebird(Bird) :- noteworthy(Bird). Queries: rarebird(condor). rarebird(What). noteworthy(condor). noteworthy(Who).

Head | Tail examples Query Database rarebird([H | T]). rarebird([condor,whooping_crane, dusky_seaside_sparrow]). Query rarebird([H | T]).

P638.pro append ([], List, List). append([Head | List_1], List_2, [Head | List_3]) :- append (List_1, List_2, List_3).

P6382.pro list_op_2( [],[]). list_op_2( [Head | Tail], List) :- list_op_2 (Tail, Result), append (Result, [Head], List).

P639.pro member (Element, [Element | _ ] ). member (Element, [_ | List]) :- member (Element, List

Matching a goal to a fact Start with facts and rules and attempt to find a sequence of matches that lead to the goal. Called bottom-up resolution Also called forward chaining Start with the goal and attempt to find a sequence of matching propositions that lead to a set of original facts in the database. Called top-down resolution Also called backward chaining Prolog implementations use backward chaining.

How is solution found? A depth-first search finds a complete sequence of proposition - a proof- for the first subgoal before working on the others. A breadth-first search works on all subgoals of a given goal in parallel. Prolog’s designers chose depth-first approach uses fewer resources

Backtracking When a goal with multiple subgoals is being processed and the system fails to show the truth of one of the subgoals, the system abandons the subgoal it could not prove. It then reconsiders the previous subgoal,if there is one, and attempts to find an alternative solution to it. A new solution is found by beginning the search where the previous search for that subgoal stopped. Multiple solutions to a subgoal result from different instantiations of its variables.