Prolog Based on: Chapter 12 of PLP “Seven languages in seven weeks”

Slides:



Advertisements
Similar presentations
CS4026 Formal Models of Computation Part II The Logic Model Lecture 6 – Arithmetic, fail and the cut.
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Chapter 11 :: Logic Languages
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
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.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 6: More Lists Theory –Define append/3, a predicate for concatenating two lists, and illustrate.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Hierarchies & Trees in SQL by Joe Celko copyright 2008.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 3: Recursion Theory –Introduce recursive definitions in Prolog –Four examples –Show that there.
Prolog: Unification & Recursion © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 27: Prolog’s Resolution and Programming Techniques COMP 144 Programming Language.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
Trees. 2 Definition of a tree A tree is like a binary tree, except that a node may have any number of children Depending on the needs of the program,
Trees. Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program, the.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
LING 388: Language and Computers Sandiway Fong Lecture 5.
ITEC 380 Organization of programming languages Lecture 9 – Prolog.
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
Introduction to Logic Programming with Prolog (Section 11.3)
CS 320 Assignment 1 Rewriting the MISC Osystem class to support loading machine language programs at addresses other than 0 1.
CS 415 – A.I. Slide Set 5. Chapter 3 Structures and Strategies for State Space Search – Predicate Calculus: provides a means of describing objects and.
N-space Snakes are special maximal length loops through an N-space cube. They ’ re full of intriguing symmetries, puzzles and surprises. They ’ re simple.
Logic Programming CSC 358/ Outline Pattern matching Unification Logic programming.
Review: computing list results Many programs require list results to be computed, built and returned Many programs require list results to be computed,
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
CS 554: Knowledge base systems Part-4: Prolog- 2 By Dr. Syed Noman Hasany.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
1 TP #2: How does Prolog answer questions? n Miscellaneous info; n Last TP exercises solved; n How does Prolog answer questions? n Recursive Prolog programs;
Copyright © 2009 Elsevier Chapter 11 :: Logic Languages And other online sources (see links on schedule page) Michael L. Scott Based on Programming Language.
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
String is a synonym for the type [Char].
Integration by Inspection & Substitution
COSC160: Data Structures Binary Trees
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Chapter 12 :: Logic Languages
Unification, Recursion and Lists
Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Tests, Backtracking, and Recursion
3.5 Programming paradigms
PROGRAMMING IN HASKELL
Prolog Recursion Pepper Major portions credited to :
Chapter 11 :: Logic Languages
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete.
Instructor: Shengyu Zhang
Problems With Assistance Module 1 – Problem 3
PROGRAMMING IN HASKELL
Prolog a declarative language
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
LING/C SC/PSYC 438/538 Lecture 21 Sandiway Fong.
Prolog programming Introduction to Prolog (part3)
Chapter 12 :: Logic Languages
PROGRAMMING IN HASKELL
Chapter 12 :: Logic Languages
Trees.
CE 221 Data Structures and Algorithms
RECURSION Haskell.
Programming Techniques
A General Backtracking Algorithm
Recursion Taken from notes by Dr. Neil Moore
CSC 143 Java Applications of Trees.
And the Final Subject is…
Prolog Based on: Chapter 12 of PLP “Seven languages in seven weeks”
Chapter 12 :: Logic Languages
Presentation transcript:

Prolog Based on: Chapter 12 of PLP “Seven languages in seven weeks” Various links (see schedule page)

Last time Saw a bit more syntax First bit of lists Today: try to get to some more complex and recursive structures Note: again, some examples for you to work out, so email me AT THE END OF CLASS with some proof of your work Working with a partner is fine if you don’t have a computer, but be sure to give these a try yourself later! No substitute for actual programming…

Math in Prolog Arithmetic: The '=' operator determines whether its operands can be unified ?- A = 37. A = 37 yes ?- 2 = 2. yes Math operators are functors (structure names), not functions ?- (2+3) = 5 no

Math (cont) For math we use the built-in operator is ?- is(X, 1+2). X = 3 yes ?- X is 1+2. X = 3 yes % LHS of 'is' must be as-yet uninstantiated ?- 1+2 is 4-1. no % RHS of 'is' must already be instantiated ?- X is Y. <error>

Starting with an exercise I want you each to start a genealogical database to use for the in class exercises today. My file: lists my kids, me, my parents, and my grandparents Some queries to try: Confirm a parent relationship such as parent(erin, grace) Find someone's parent such as parent(X, jesse) Find someone's children such as parent(susan, X) List all parent-children such as parent(X,Y)

Back to Recursion We actually saw some recursive definitions last time, but let’s look a little closer at one. child(anne,bridget). child(bridget,caroline). child(caroline,donna). child(donna,emily). descend(X,Y) :- child(X,Y). descend(X,Y) :- child(X,Z), descend(Z,Y). This final rule allows the “child” relationship to chain indefinitely when unifying – which is the correct notion to use for descendants!

Recursion (again) How the solution is determined depends on order in the database, as well as order in which you list the constraints. So – order matters!!

How does order matter? I have the two rules: descend(X,Y) :- child(X,Y). descend(X,Y) :- child(X,Z), descend(Z,Y). If I swap the two rules, it will still work correctly (if less efficiently). However, if I change the second to be: descend(X,Y) :- descend(Z,Y), child(X,Z). Then infinite loops will abound, since Prolog will attempt to go down an infinitely long tree of descends. (Look back at that tree again…)

Another example Be careful of ordering! Consider this example describing paths in graphs: edge(a, b). edge(b,c). edge(c,d). edge(d, e). edge(b, e). edge(d, f). path(X, X). path(X, Y):- edge(Z, Y), path(X, Z). If the two terms on the last clause were reversed, the program would be less efficient. Why?

Same path example If we were to flip order of the last 2 clauses, things get even worse!

Exercise Pull up your genealogy database, and add the predicate to find descendants (see previous slide), but using the parent predicate (since we didn’t make a child one). Then, test your predicate. Do they work, or do you see infinite loops even for simple queries like: descend(erin,grace).? If so, check your rules and re-order some things!

Lists We also saw lists last time: Structure: [] is the empty list, and . is a built in concatenation (like : in Haskell). So a list is: .(a, .(b, .(c, []))) ?- append([a, b, c], [d, e], L). L = [a, b, c, d, e]

List exercise Now, add the following to your genealogy file: ancestor(A,D,[A]) :- parent(A,D). ancestor(A,D,[X|Z]) :- parent(X,D), ancestor(A,X,Z). Now test a few ways. I did: ancestor(jesse,erin,X). ancestor(erin,jesse,X). ancestor(clarice,jesse,X). ancestor(X,jesse,Y).

Takeaway PROLOG IS NOT PURELY DECLARATIVE The ordering of the database and the left-to-right pursuit of sub- goals gives a deterministic imperative semantics to searching and backtracking Changing the order of statements in the database can give you different results It can lead to infinite loops It can certainly result in inefficiency