242-203 Comp. Eng. II: Intro. to Prolog1 CoE Software Lab II (2SBo6) 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison WiG Lab Office

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Comp Eng Lab II: Intro. to Prolog 1 Computer Engineering Lab II , Semester 2, Who I am: Andrew Davison CoE, Info. Eng. Research.
Introduction to C Programming
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.
Prolog.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
Logic Programming – Part 2 Lists Backtracking Optimization (via the cut operator) Meta-Circular Interpreters.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
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).
Bahar Pamuk. Outline Introduction Syntaxa Clauses, Programs and Queries List Manipulation Operators Backtracking, Cuts References.
CSI 3120, Prolog recap, page 1 Prolog recap Prolog was invented in 1970, first implemented in 1972, first implemented efficiently in Excellent commercial-
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
CS 330 Programming Languages 12 / 02 / 2008 Instructor: Michael Eckmann.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
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.
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Discrete Maths Objective to show the close connection between recursive definitions and recursive functions , Semester 2, Recursion.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
Formal Models of Computation Part II The Logic Model
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
LING 388: Language and Computers Sandiway Fong Lecture 4.
CS 321 Programming Languages and Compilers Prolog part 2.
Introduction of C++ language. C++ Predecessors Early high level languages or programming languages were written to address a particular kind of computing.
CS 2104 – Prog. Lang. Concepts Logic Programming - II Dr. Abhik Roychoudhury School of Computing.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
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.
F28PL1 Programming Languages Lecture 16: Prolog 1.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
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.
Working with arrays (we will use an array of double as example)
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CISB434: VisiRule 1.
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.
Logical and Functional Programming
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
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.
Lecture course: Heuristics & Prolog for Artificial Intelligence Applications Notes on Prolog from Professor Bruce Batchelor, DSc.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
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.
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.
1. Starting 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Section 16.5, 16.6 plus other references
CS314 – Section 5 Recitation 9
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Tests, Backtracking, and Recursion
Chapter 11 :: Logic Languages
Chapter 12 :: Logic Languages
Programming Paradigms and Languages
CSE 3302 Programming Languages
Chapter 12 :: Logic Languages
Presentation transcript:

Comp. Eng. II: Intro. to Prolog1 CoE Software Lab II (2SBo6) , Semester 2, Who I am: Andrew Davison WiG Lab Office Introduction to Prolog Please ask questions Version 2

1. Why Learn Prolog? Very popular in Artificial Intelligence (AI) Unique features include: −unification (more powerful assignment) −backtracking −close links to predicate logic Very different from C, Java −an engineer must know more than 1 tool −Prolog is a logic programming language Comp Eng Lab II: Intro. to Prolog2

C Program Compared to Prolog  void foo(...) {... } int bar(...) {... } void main() {... }  Prolog predicate -- made up of facts and rules Prolog predicate -- more facts and rules ?- Prolog query Comp Eng Lab II: Intro. to Prolog3 Lots of data types: int, char, struct, pointer,... Data types: term, list

Example (parents.pro) % parentOf/2 predicate (made of 6 facts, 0 rules) parentOf(kim,holly). % e.g. kim is the parent of holly parentOf(margaret,kim). parentOf(margaret,kent). parentOf(esther,margaret). parentOf(herbert,margaret). parentOf(herbert,jean). % livesAt/3 predicate (made of 8 facts, 0 rules) livesAt(margaret, 9, "Bar Lane"). livesAt(kim, 37, "Foo Street"). livesAt(holly, 37, "Foo Street"). livesAt(esther, 9, "Bar Lane"). livesAt(herbert, 23, "PSU Village"). livesAt(kent, 9, "Bar Lane"). livesAt(bill, 23, "PSU Village"). livesAt(john, 9, "Bar Lane"). ?- parentOf(margaret, kent). 4 terms are the data inside facts (and rules) terms are the data inside facts (and rules) a query

5 Facts using compound terms A staff/3 predicate: staff( name(mickey, mouse), address(123, "Fantasy Way"), 73). staff( name(bat, man), address(321, "Cavern Ave"), 54). staff( name(wonder, woman), address(987, "Truth Way"), 39). A compound term (e.g. name(wonder, woman)) is a bit like a C struct Software Lab II: Intro. to Prolog

6 3. Executing Prolog more details in section 11 a query result only 1 query must be uncommented only 1 query must be uncommented

7 Use a query to execute Prolog code. −a query starts with "?-" In SB Prolog: −use F5 (or Run|Run) to execute the query −the output appears in the Output window Software Lab II: Intro. to Prolog

Simple Queries ?- parentOf(margaret, kent). Yes // printed in the Output window ?- parentOf(fred, pebbles). No Software Lab II: Intro. to Prolog

Executing a Query Match the query against a fact. Prolog tries each fact in top-down order, until one matches, or it runs out of facts Comp Eng Lab II: Intro. to Prolog9 ?- parentOf(margaret, kent) parentOf(margaret, kent).

Queries With Variables ?- parentOf(P, jean), writeln(P). herbert Yes ?- parentOf(P, esther), writeln(P). No Software Lab II: Intro. to Prolog the ',' means "and"

Executing a Query Match query (e.g. ?- parentOf(P, jean)) against a fact −also match (unify, bind) the variables Comp Eng Lab II: Intro. to Prolog11 ?- parentOf(P, jean) parentOf(herbert, jean). P = herbert

Conjunctions A conjunction is a series of queries. Prolog works left to right, trying to match each query. ?- parentOf(margaret,X), parentOf(X,holly), writeln(X). kim Yes the ',' means "and" Software Lab II: Intro. to Prolog

As Prolog executes the queries in a conjunction, it remembers variable matches (bindings). −e.g. the first query sets X = kim −this value for X is passed to the second query in the conjunction Comp Eng Lab II: Intro. to Prolog13

Multiple Solutions the user types 'F8' to make SB Prolog look for another answer

How Multiple Solns Work? When a query matches a fact, Prolog remembers which fact was chosen −called a choice point If the user types 'F8', Prolog goes back to the choice point (backtracks) and tries to find a different match Comp Eng Lab II: Intro. to Prolog15

16 4. Rules 1. Match query (e.g. ?- livesWith(bill,herbert) ) against the head of a rule. −match (unify) the variables 2. Create new query conjunction from the body goals. livesWith(X, Y) :- livesAt(X, No, Addr), livesAt(Y, No, Addr). body goals head Software Lab II: Intro. to Prolog read this as "if"

17 Example ?- livesWith(kim, X), writeln(X). kim Yes. holly Yes. No Software Lab II: Intro. to Prolog I pressed F8 why?

More Rules livesWithParent(X, Y) :- livesWith(X, Y), parentOf(Y, X). ?- livesWithParent(holly, kim). ?- livesWithParent(kim, holly) Comp Eng Lab II: Intro. to Prolog18

19 5. Lists List notationMeaning []an empty list [andrew]list with one element [1,2,"hello"]3-element list [1,name(X,Y)]2-element list Software Lab II: Intro. to Prolog

20 Examples ?- X = [1, 2, 3]. X = [1, 2, 3] ?- [X, Y, Z] = [1, 2, 3]. X = 1 Y = 2 Z = Software Lab II: Intro. to Prolog From now on, I'm going to leave out calls to writeln(). From now on, I'm going to leave out calls to writeln().

21 List Notation With Tail [1,2|X] matches with a list that starts with 1,2 and binds X to the rest (tail) of the list. ?- [1,2|X] = [1,2,3,4,5]. X = [3, 4, 5] Software Lab II: Intro. to Prolog

Comp Eng Lab II: Intro. to Prolog22 ?- [X|Y] = [a, b, dc, st, fg]. X = a Y = [b, dc, st, fg]

23 6. member/2 member(X, [X|_]). member(X, [Y|Rest]) :- member(X, Rest). member(X, [X|_]). member(X, [Y|Rest]) :- member(X, Rest). member(X,L) succeeds if X is an element in the list L ?- member(1, [2, 1,3]). yes ?- member(j, [a,n,d,y]). no ?- member(X,[j,i,m]),writeln(X). j i // F8 m // F8 no // F Software Lab II: Intro. to Prolog

24 7. length/2 length(X,Y) succeeds if Y is the length of the list X. length([], 0). length([X|Rest], Len) :- length(Rest, LenRest), Len is LenRest + 1. length([], 0). length([X|Rest], Len) :- length(Rest, LenRest), Len is LenRest + 1. ?- length([a,b,c,d], L), writeln(L). 4 ?- length([1,2,3], 4). no Software Lab II: Intro. to Prolog

25 Evaluating Arithmetic The general format is: Variable is expression ?- X is 1+2*3, writeln(X). 7 yes Software Lab II: Intro. to Prolog

26 8. The append/3 Predicate append(X,Y,Z) means that the X list 'stuck onto' the Y list == the Z list ?- append([1,2],[3,4],Z), writeln(Z). [1, 2, 3, 4] Yes append([], B, B). append([Head|TailA], B, [Head|TailC]) :- append(TailA, B, TailC) Software Lab II: Intro. to Prolog

27 Other Uses of append/3 append/3 can be called with variables in any of its argument positions. ?- append(X,[3,4],[1,2,3,4]), writeln(X). [1, 2] Yes Software Lab II: Intro. to Prolog

28 Multiple Answers ?- append(X,Y,[1,2,3]). X = [] Y = [1, 2, 3] X = [1] Y = [2, 3] X = [1, 2] Y = [3] X = [1, 2, 3] Y = [] No By using F Software Lab II: Intro. to Prolog

29 9. The not/1 Predicate not(X) succeeds when the X goal fails. Only use not/1 when its goal contains no variables. −use not/1 as a yes/no test ?- not( member(4,[1,2,3]) ). Yes ?- not( member(1,[2,1,3]) ). No Software Lab II: Intro. to Prolog

Using Strawberry Prolog Download SB Prolog system from: Software.coe/LAB/Prolog/ The filename: StrawberryProlog_3_0_Beta4.exe Read the readme.txt file Software Lab II: Intro. to Prolog

More Information The Help menu in SB Prolog leads to: −a tutorial −a Prolog language guide −examples continued Software Lab II: Intro. to Prolog

32 SB Prolog’s Web Site: − −v.3.0 beta 4, and other versions "Learn Prolog Now" (chs 1-6): − Online Prolog tutorials list at − onlineprolog.shtml Software Lab II: Intro. to Prolog