CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries.

Slides:



Advertisements
Similar presentations
© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Advertisements

Logic Programming Lecture 1: Getting started. Getting started We’ll use SICStus Prolog Free for UofE students Available on all DICE machines
Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
Prolog.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
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).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 1 Introduction to Prolog.
4. PROLOG Data Objects And PROLOG Arithmetic
Lecture 2 Introduction to C Programming
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 2 Theory –Unification –Unification in Prolog –Proof search Exercises –Correction exercises.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Discrete Math Lecture 5: Counting
DEDUCTIVE DATABASE.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
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
LING 388: Language and Computers Sandiway Fong Lecture 4.
Agile Software Development Lab 2008 Dr. Günter Kniesel, Daniel Speicher, Tobias Rho, Pascal Bihler Spring 2008 R O O T S Prolog - Part 1 Alexis Raptarchis.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
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.
CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search.
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.
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
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?
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
By: Cory Canter CSC 415 Programming Languages. History  Created by Alain Colmerauer, Phillipe Roussel and Robert Kowalski in 1971  Started as a natural.
Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain).
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)
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Introduction to Prolog
Introduction to Prolog © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Knowledge Based Information System
Artificial Intelligence CS370D
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
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.
Instructor :Bayan Ghozlan الاستاذه : بيان غزلان.  البرولوغ Prolog هي لغة برمجة منطقية. وتعني برمجه المنطق programing language  تم اختراع اللغة بواسطة.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
علمتني الثلوج أن أكون كبياض الثلج لا يحمل قلبي غير الحب والعطف والرحمة لا أسيئ الظن بالأخرين ولا أحكم عليهم من مظهرهم فإذا لم تعاشرهم فأنت تجهلهم !
Section 16.5, 16.6 plus other references
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
Prolog a declarative language
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Revision Lecture
PROLOG.
ARTIFICIAL INTELLIGENCE LAB UCEL601 ( Second Term) Presented by Nirmala 1.
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Chapter 4 Procedural Methods.
PHP.
Prolog a declarative language
Chapter Two: Syntax and Meaning of Prolog Programs
Chapter 7 Procedural Methods.
Introduction to Prolog
Logic Programming Language
Introduction to PROLOG
Lexical Elements & Operators
PROLOG.
Presentation transcript:

CSC 270 – Survey of Programming Languages Prolog Lecture 1 – Facts, Rules, and Queries

Basic Constructs of Prolog Prolog has only three basic constructs: – Facts – statements that are unconditionally true – Rules – statements that are conditionally true – Queries – questions pertaining to the information stored in a knowledge base A knowledge base is a collection of facts and rules Writing a program in Prolog is all about creating the knowledge base.

Knowledge Base #1 woman(mia). woman(jody). woman(yolanda). playsAirGuitar(yolanda). party.

Our Prolog Environment Panther – swipl – Create files using notepad. – Consult the file using [filename without.pl]. For kb1.pl use [kb1]. – Ctrl d to exit OR exit command SWI-Prolog on windows or mac –

Querying Knowledge Base #1 1 ?- % c:/documents and settings/adelphi/my documents/prolog/kb1 compiled 0.00 sec, 6 clauses 1 ?- woman(mia). true. 2 ?- playsAirGuitar(jody). false. 3 ?- playsAirGuitar(mia). false. 4 ?- playsAirGuitar(vincent). false.

5 ?- tatooed(jody). ERROR: toplevel: Undefined procedure: tatooed/1 (DWIM could not correct goal) 6 ?- party. true. 7 ?- rockConcert. ERROR: toplevel: Undefined procedure: rockConcert/0 (DWIM could not correct goal) 8 ?-

Infer (or Deduce) Prolog determines that Vincent doesn’t play Air Guitar because there is no fact from which it can infer (or deduce). The error messages for tatooed and rockConcert are due to the fact that SWI Prolog did not find these terms when compiling the knowledge base.

Knowledge Base #2 Add 3 rules: happy(yolanda). listens2Music(mia). listens2Music(yolanda) :- happy(yolanda). playsAirGuitar(mia) :- listens2Music(mia). playsAirGuitar(yolanda) :- listens2Music(yolanda). listens2Music(amy) :- happy(amy). If Yolanda is happy then she listens to music Left is head, right is body Prolog can infer the head from the body being true 3 rules, 2 facts, 5 clauses Predicates or procedures: happy, listens2Music and playsAirGuitar

Querying Knowledge Base #2 9 ?- playsAirGuitar(mia). true. 10 ?- playsAirGuitar(yolanda). true. 11 ?- listens2Music(amy). False.

Knowledge Base #3 This knowledge base consists of 2 facts and 3 rules. happy(vincent). listens2Music(butch). playsAirGuitar(vincent):- listens2Music(vincent),happy(vincent). playsAirGuitar(butch):-happy(butch). playsAirGuitar(butch):-listens2Music(butch). Translation: If vincent is happy and vincent listens2music, then vincent plays air guitar.

Running Knowledge Base #3 11 ?- "playsAirGuitar(vincent)"? false. 12 ?- This is because while KB3 contains happy(vincent), it does not explicitly contain the information listens2Music(vincent), and this fact cannot be deduced either.

Running Knowledge Base #3 (continued) 12 ?- playsAirGuitar(butch). true. 13 ?- The knowledge base gives us two ways of deducing this through two different rules: playsAirGuitar(butch):-happy(butch). playsAirGuitar(butch):-listens2Music(butch). This is effectively equivalent to an OR.

Knowledge Base #4 7 facts woman(mia). woman(jody). woman(yolanda). loves(vincent,mia). loves(marsellus,mia). loves(pumpkin,honey_bunny). loves(honey_bunny,pumpkin). -- read as vincent loves mia

Prolog Variables Capital letters start a variable name – Actually a placeholder to lookup a match – Ex : woman(X). Tell me which individual you know to be a woman – Returns the first occurrence – Press ; for each additional match X = mia ; X = jody ; X = yolanda ; X = honey-bumpkin ;

Querying Knowledge Base #4 17 ?- loves(marsellus, X). -- who loves marsellus? X = mia. 18 ?- loves(marsellus, X), woman(X). -- who loves marsellus and is a woman?; requires unification of two facts with the matching X X = mia.

Knowledge Base #5 A rule with a variable: loves(vincent,mia). loves(marsellus,mia). loves(pumpkin,honey_bunny). loves(honey_bunny,pumpkin). jealous(X,Y):- loves(X,Z), loves(Y,Z).

Running Knowledge Base #5 20 ?- jealous(marsellus, W). W = vincent -- any other jealous people? -- trace. -- stop with notrace.

Prolog Syntax Facts, rules and queries are built from terms. There are four types of terms in Prolog: – atoms – numbers – variables – complex terms (or structures) Atoms and numbers are constants and constants and variables make up the simple terms of Prolog.

Atoms An atom is either: 1.A string of uppercase letters, lowercase letters, underscores and digits that begin with a lowercase letter. (E.g., butch, big_kahuna, etc.) 2.An arbitrary sequence of characters inside single quotes (E.g., ‘ Vincent ’, ‘ The Gimp ’, ‘ ((*&^*&^*&^ ’) 3.A string of special characters ===>, ;, and :- )

Numbers The typical Prolog application does not make heavy use of numbers. Most Prolog implmentations support floating point numbers and integers.

Variables A variable is a string of uppercase letters, lowercase letters, digits and underscores that starts with either an uppercase letter or an underscore. (E.g., X, Y, Variable, _tag, X_526, List, List24, etc.) _ is a special variable and it is called the anonymous variable.

Complex Terms We can combine atoms, numbers and variables to form complex terms (also called structures). Complex terms are built out of a functor followed by a sequence of arguments. Examples – playsAirGuitar(jody). – loves(vincent, mia). – hide(X, father(father(father(butch)))).

Recursive Structure Nesting complex terms inside complex terms indefinitely leads to recursive structures.

Arity Arity refers to the number of arguments that a functor has. It becomes important when the same functor can have more than one different count of arguments (or different arities).