Prolog Primarily for Symbolic (nonnumeric) Computation

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
Semantics Static semantics Dynamic semantics attribute grammars
1. An Overview of Prolog.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
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).
Prolog OR (disjunction) “;” is same as a logical OR “;” is same as a logical OR It is also equivalent to using separate clauses... It is also equivalent.
Prolog Programming for Artificial Intelligence Three edition 2001
Prolog programming Introduction to Prolog
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
MB: 2 March 2001CS360 Lecture 31 Programming in Logic: Prolog Prolog’s Declarative & Procedural Semantics Readings: Sections
Introduction to Prolog Proof Procedures. Exercise parent(mark, alex). parent(di, alex). brother(brian, mark). sister(cathy, di). wife(susan, brian). husband(brad,
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.
Copyright © 2003 Bolton Institute Logical Analysis and Problem Solving, (LAPS) Programming in Prolog.
Prolog programming Introduction to Prolog CS370d - CS 461.
The Evolution of Programming Languages Day 3 Lecturer: Xiao Jia The Evolution of PLs1.
F28PL1 Programming Languages Lecture 16: Prolog 1.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
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.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
Introduction to Prolog Facts, Questions & Rules Atoms & Variables.
CS 603: Programming Languages Lecture 25 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
Basics of Most C++ Programs // Programmer: Clayton Price date: 9/4/ // File: fahr2celc.cpp 03. // Purpose:
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
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/
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Negation Chapter 5. Stating Negative Conditions n Sometimes you want to say that some condition does not hold n Prolog allows this –not/1this is a predicate.
COMP307 Artificial Intelligence Xiaoying Gao Victoria University of Wellington Lecture 2:1 
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Artificial Intelligence CS370D
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.
For Wednesday No reading Prolog handout 2 Chapter 9, exercise 4.
Solving Problems with Repetition Version 1.0. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C#
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Chapter 2 Writing Simple Programs
Recursive stack-based version of Back-chaining using Propositional Logic
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
TEMPERATURE CONVERSION
Tests, Backtracking, and Recursion
3.5 Programming paradigms
Prolog programming Introduction to Prolog (part2)
Prolog programming Introduction to Prolog
Prolog programming Introduction to Prolog (part3)
3. Decision Structures Rocky K. C. Chang 19 September 2018
Chapter 12 :: Logic Languages
Programming Techniques
Life is Full of Alternatives
Objectives You should be able to describe: The while Statement
CSE 3302 Programming Languages
How To Think Like a Prolog ?
Life is Full of Alternatives
Chapter 2 - Algorithms and Design
Clauses and Predicates
Chapter 2: Prolog (Introduction and Basic Concepts)
ICS103: Programming in C 5: Repetition and Loop Statements
Prolog Based on: Chapter 12 of PLP “Seven languages in seven weeks”
Module 3 Selection Structures 12/7/2019 CSE 1321 Module 3.
Presentation transcript:

Prolog Primarily for Symbolic (nonnumeric) Computation Suited to Problems Involving Objects and Relationships A Step Toward Declarative Programming.

Example: Family Tree always hold. parent(pam, bob). parent(tom, bob). ann jim liz pat parent(pam, bob). parent(tom, bob). parent(tom, liz). parent(bob, ann). parent(bob, pat). parent(pat, jim). Facts: Relations that always hold.

Yes/No Queries ?- parent(bob, pat). yes ?- parent(liz, pat). no ?-

Queries with Variables ?- parent(X, bob). X=pam; X=tom; no ?- parent(X, Y). X=pam Y=bob yes

Compound Queries ?- parent(Y, pat), parent(X,Y). X=pam Y=bob; X=tom yes

Exercises 1. Who is Pat's parent? 2. Does Liz have a child? 3. Who is Pat’s grandparent? 4. Find a great-grandparent.

Adding Inference Rules For all X and Y Y is an offspring of X if X is a parent of Y offspring(Y,X):- parent(X,Y). Rules: Conclusion is True if Conditions Satisfied. All Rules Have Two Parts: Conclusion, Consequent or Head (Left-hand side). Antecedents, Condtions or Body (Right-hand side). A Fact is a Rule with no Conditions.

Exs: Sibling, Sister, Grandparent

Exs: Sibling, Sister, Grandparent sibling(X,Y) :- parent(P,X), parent(P,Y), X \= Y.

Exs: Sibling, Sister, Grandparent sister(X,Y) :- sibling(X,Y), female(X).

Exs: Sibling, Sister, Grandparent grandparent(X,Y) :- parent(P,Y), parent(X,P).

Summary So Far Prolog Program is a Set of Clauses. Prolog Clauses: Facts, Rules, Queries. Facts declare unconditional truths. Rules declare truths based on conditions. Queries are used to ask Prolog what is true. Prolog Clauses have a head and body.

How Prolog Works p1(foo). ?- p1(X). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz). ?- p1(X).

p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz). goal: p1(X) return: X=foo;

How Prolog Works p1(foo). goal: p1(X). p1(X) :- return: X=foo; p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz). goal: p1(X). return: X=foo; REDO

How Prolog Works p1(foo). p1(X) :- goal: p1(X) goal: p2(X,Y) p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works p1(foo). goal: p1(X) p1(X) :- goal: p2(X,Y) p2(X,Y), X=foo, Y=bleen p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works p1(foo). goal: p1(X) p1(X) :- goal: p2(X,Y) p2(X,Y), X=foo, Y=bleen goal: p3(bleen) p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works p1(foo). goal: p1(X) p1(X) :- goal: p2(X,Y) p2(X,Y), X=foo, Y=bleen goal: p3(bleen) FAIL p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works p1(foo). goal: p1(X) p1(X) :- goal: p2(X,Y) p2(X,Y), X=foo, Y=bleen REDO p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works p1(foo). goal: p1(X) p1(X) :- goal: p2(X,Y) p2(X,Y), X=bar, Y=baz p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works p1(foo). goal: p1(X) p1(X) :- goal: p2(X,Y) p2(X,Y), X=bar, Y=baz goal: p3(baz) p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

goal: p1(X) goal: p2(X,Y) X=bar, Y=baz goal: p3(baz) yes return: X=bar ; p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works goal: p1(X) p1(foo). goal: p2(X,Y) p1(X) :- X=bar, Y=baz goal: p3(baz) REDO p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works goal: p1(X) p1(foo). goal: p2(X,Y) p1(X) :- X=bar, Y=baz goal: p3(baz) FAIL p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works goal: p1(X) p1(foo). goal: p2(X,Y) p1(X) :- X=bar, Y=baz REDO p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works goal: p1(X) p1(foo). goal: p2(X,Y) p1(X) :- FAIL p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works goal: p1(X) p1(foo). REDO p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works goal: p1(X) p1(foo). return: X=snork ; p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works goal: p1(X) p1(foo). return: X=snork p1(X) :- REDO p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works goal: p1(X) p1(foo). FAIL p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works goal: p1(X) p1(foo). return: no p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

How Prolog Works ?- p1(X). p1(foo). X=foo; p1(X) :- p2(X,Y), X=bar; X=snork; no ?- p1(foo). p1(X) :- p2(X,Y), p3(Y). p1(snork). p2(foo,bleen). p2(bar,baz). p3(baz).

Is That All There is to It? Yes. But there is more to know: Built-in predicates Designing Algorithms (programming) Predicates are sub-programs Decisions (ifs) are Predicates with multiple clauses Loops via recursion or Repeat/Fail Variables don't vary. Data Structures: Complex terms

Ex: Temperature Conversion Conversion “Function” % convert.pl convert(Cel, Fahr) :- Fahr is 9/5*Cel+32.

Interactive Conversion ?- [convert]. (Loads the code) % convert compiled ?- convert(100, X). X = 212 yes ?-

Adding Input/Output run :- write('Enter a Celsius temp: '), read(C), convert(C, F), write('The temp is '), write(F), write(' degrees Fahrenheit.').

Running the Enhanced Version Enter a Celsius temp: 100. The temp is 212 degrees Fahrenheit. Yes ?-

Adding a Decision If temp is greater than 90: print a heat warning if temp is less than 30: print a cold warning warn(T, 'It''s really hot') :- T > 90. warn(T,'Brass monkey danger') :- T < 30. warn(T, '') :- T >= 30, T <= 90.

Enhanced main program run :- write('Enter a Celsius temp: '), read(C), convert(C, F), write('The temp is '), write(F), write(' degrees Fahrenheit.'), nl, warn(F, Warning), write(Warning).

Version with Decision ?- run. Enter a Celsius temp: 100. The temp is 212 degrees Fahrenheit. It's really hot out! Yes ?-

Retricting Backtracking warn(T, 'It''s really hot') :- T > 90. warn(T,'Brass monkey danger') :- T < 30. warn(T, '') :- T >= 30, T <= 90. ! (called "cut") causes Prolog to commit to choices. warn(T, 'It''s really hot) :- T > 90, !. warn(T, 'Brass monkey danger') :- T < 30, !. warn(_,'').

Adding a Loop Sentinel Loop As a Decision Get an Input While input is valid number Convert to Fahrenheit Output temp and warnings Get next input Get an Input if input is a number: Do the conversion Get another input Do the loop again if input is 'quit' just do it. 11

Using Recusion to Loop run2 :- getInput(C), convert_loop(C). number(C), convert(C,F), ... getInput(C1), convert_loop(C1). convert_loop(quit). If input is a number: Do the conversion Get another input Do the loop again if input is 'quit' just do it.

Repeat-Fail Loop run3 :- repeat, write('Enter a Celsius Temp: '), read(C), process(C), C = quit. process(C) :- number(C), convert(C,F), write('The temp is '), write(F), warn(F, W), write(W), nl. process(quit).

Structured Data