1 Basic Prolog ?- prolog prompt A :- B IMPLIES in rules; if B then A, logical AND ; logical OR -> if-then-else. END of predicate switch query/database.

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

Logic: The Big Picture Propositional logic: atomic statements are facts –Inference via resolution is sound and complete (though likely computationally.
Chapter 11 :: Logic Languages
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
8 Queens. Problem: Placing 8 queens on a chessboard such that they don’t attack each other Three different Prolog programs are suggessted as solutions.
Prolog.
Declarative Programming Lists in PROLOG Autumn 2014.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
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).
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
Declarative Programming Autumn 2014 Basic syntax and sample programs.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
CSE (c) S. Tanimoto, 2005 Logic Programming 1 Logic Programming Outline: Motivation Examples: The Grandmother relation Formulation in Prolog Logic,
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
Backtracking.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
LISP and PROLOG AI Programming Language Submitted To: Dr. Hesham El-Zoka Submitted By: Eng. Ismail Fathalla El-Gayar.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
Formal Models of Computation Part II The Logic Model
CS 321 Programming Languages and Compilers Prolog part 2.
Evolution of Programming Languages Generations of PLs.
programmation en logique
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.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
Cs7120 (Prasad)L16-Meaning1 Procedural and Declarative Meaning of Prolog
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
Lecture on Programming Languages
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.
Introduction Prolog program consists of clauses which are facts and rules. Predicate is the name given to the relation likes(bill, tennis) predicate name.
Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
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.
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.
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.
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.
Programming Languages Third Edition Chapter 4 Logic Programming.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Knowledge Based Information System
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
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.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Prolog Concepts.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Data Structures and Algorithms
Tests, Backtracking, and Recursion
Logic Programming Languages
Logic Programming Language
Programming Techniques
Programming Languages 2nd edition Tucker and Noonan
Prolog Concepts.
Chapter 2: Prolog (Introduction and Basic Concepts)
Chapter 5 Recursion.
Prolog Concepts.
Presentation transcript:

1 Basic Prolog ?- prolog prompt A :- B IMPLIES in rules; if B then A, logical AND ; logical OR -> if-then-else. END of predicate switch query/database mode [ ] Beginning and end of a list ( ) Grouping (arguments) anything123 constant Anything456 variable _anything anonymous variable halt. stops the Prolog interpreter trace. shows entry/exit/redo/fail for each call. ! CUT (see later, used in not )

2 Structure of Programs Programs consist of procedures. Procedures consist of clauses. Each clause is a fact or a rule. Programs are executed by posing queries.

3 Clauses: Facts and Rules "if" "provided that" "turnstile" Head :- Body. This is a rule. Head. This is a fact. Full stop at the end.

4 Body of a (Rule) Clause Contains Goals HeadBody Goals :- likes(mary, X)human(X), honest(X).

5 Example Program elephant(george). elephant(mary). elephant(X) :- grey(X), mammal(X), hasTrunk(X). Predicate Procedure for elephant Facts Clauses Rule

6 Clauses can be interpreted in a declarative or a procedural way Clause: Declarative reading: Procedural reading: H :- G1, G2, …, Gn. That H is provable follows from goals G 1, G 2, …, G n being provable To execute procedure H, the procedures called by goals G 1, G 2, …, G n are executed first Interpretation of Clauses

7 Clauses can be interpreted in a declarative, or in a procedural way Clause: Declarative reading: Procedural reading: H :- G1, G2, …, Gn. That H is provable follows from goals G 1, G 2, …, G n being provable To execute procedure H, the procedures called by goals G 1, G 2, …, G n are executed first

8 Clauses Clauses provide a convenient way to express case analysis and non-determinism. Program clauses and data have the same form. A Prolog program can also be seen as a relational database containing rules and facts. The relational form of procedures makes it possible to define ‘reversible’ procedures.

9 Unification Term-manipulation that –passes parameters, –returns results, –selects and constructs data structures Basic control flow model is backtracking Sometimes it is necessary to use control features that are not part of ‘logic’

10 Computation via Queries Facts and rules are stored in a database –They can be queried ?- likes(joe, fish). yes ?- likes(joe, chocolate) no –Important: no –means “I know it’s false, or I don’t know” yes –means “I can prove it” asymmetric

11 Example Queries Queries Replies ?- elephant(george). Yes ?- elephant(jane). no

12 Prolog’s Style State the facts and rules Pose queries Prolog finds the answers by –searching the facts –deducing new facts from the rules Answers depend on the search algorithm

13 Imperative/Functional/Declarative Styles – Example - Concatenate lists a and b concat([], Z, Z). concat([H|T], L, [H|Z]) :- concat(T, L, Z). list procedure concat(list a, list b) { list t = list u = copylist(a); while (t.tail != nil) t = t.tail; t.tail = b; return u; } cat(a,b) ≡ if b = nil then a else cons(head(a), concat(tail(a),b)) In an imperative PL In a functional PL In a declarative PL

14 Example: Zoo /* Facts */ elephant(gigi). elephant(mary). elephant(joe). panda(chi_chi). panda(ming_ming). stripey(tigger). stripey(mrED). big_teeth(tigger). isaCat(tigger). isaHorse(mrED). venomous(rattler). venomous(grass_snake). /* Rules */ dangerous(X) :- big_teeth(X). dangerous(X) :- venomous(X). swims(Y) :- isaFish(Y). guess(X, tiger) :- stripey(X), big_teeth(X), isaCat(X). guess(X, koala) :- arboreal(X), sleepy(X). guess(X, zebra) :- stripey(X), isaHorse(X). /* Sample Queries */ ?- dangerous(ming_ming). ?- dangerous(X). ?- guess(X,Y). www2.hawaii.edu/~janst/313/prolog/zoo.pl

15 Colored ball problem Find the correct sequence of colored balls to satisfy the given rules: 1.One red, one black and one white ball Solution will be a permutation of {red,black,white} 2.white is just to the right of black If black is in pos1, white is in pos2, If black is in pos2, white is in pos3 3.black is not in the center Black can’t be in pos2 www2.hawaii.edu/~janst/313/prolog/colors.pl

16 Cryptarithmatic Problem Find the unique digit that will replace each character to solve a puzzle S E N D + M O R E M O N E Y www2.hawaii.edu/~janst/313/prolog/crypto.pl

17 Eight Queens Problem Given an 8-by-8 chessboard, place 8 queens so that they do not threaten each other along a row, column, or a diagonal Mathematically determine diagonal –two queens are on the same / (UR->BL) diagonal iff the sum of the row and the column is the same for each –two queens are on the same \ (UL->BR) diagonal iff the difference between the row and the column are the same for each Generalize: N queens (N >=4) www2.hawaii.edu/~janst/313/prolog/queens.pl

18 N-Queens Without Searching A solution to the N-Queens problem for any N >= 4 Placing queens takes constant time per queen, O(N) Drawing the board takes O(N^2) time Published in ACM SIGART Bulletin, 2(2), page 7 See example at – now unfortunately broken