Download presentation
Presentation is loading. Please wait.
1
Prolog Ross (Tate)
2
Filling in the Blanks Rather than reverse((a,b)) returns (b,a) Rather than reverse((a,b)) returns (b,a) What X makes reverse((a,b), X) true? What X makes reverse((a,b), X) true? The blank X gets filled with (b,a) The blank X gets filled with (b,a) X = 5. X = 5. Fills X with 5. Fills X with 5. X = 5, X = 6. X = 5, X = 6. Fails since no value can equal both 5 and 6. Fails since no value can equal both 5 and 6. “=” unifies the left and right to the same value “=” unifies the left and right to the same value
3
Database of Rules (and Facts) Prolog is a database of rules (and facts) Prolog is a database of rules (and facts) name(params) :- further requirements. name(params) :- further requirements. A “fact” is simply a rule with no further requirements A “fact” is simply a rule with no further requirements name(params). (name starts with lower case letter) name(params). (name starts with lower case letter) isparent(ross, colleen). isparent(ross, colleen). child(Parent, Child) :- isparent(Child, Parent). child(Parent, Child) :- isparent(Child, Parent). Lower case words denote “atoms” Lower case words denote “atoms” Upper case words are variables or “blanks” Upper case words are variables or “blanks”
4
Backtracking Rules in Prolog can be satisfied many ways Rules in Prolog can be satisfied many ways So, which way should be chosen? So, which way should be chosen? Prolog starts at the top and finds the first way for the rule to be satisfied Prolog starts at the top and finds the first way for the rule to be satisfied It also remembers where it was satisfied It also remembers where it was satisfied Say the results of that fail later Say the results of that fail later Then Prolog simply starts where it left and finds more ways until none are left (fails) Then Prolog simply starts where it left and finds more ways until none are left (fails)
5
Structures Structures are a way to combine things Structures are a way to combine things They take the form: name(parts) They take the form: name(parts) pair(x,y) is one such structure pair(x,y) is one such structure The parts can be anything: atoms, variables, or even structures The parts can be anything: atoms, variables, or even structures Structures are stored as trees in prolog, with the structure name being the name of the node and the parts being the children Structures are stored as trees in prolog, with the structure name being the name of the node and the parts being the children
6
Lists and Recursion The empty list: [] (a special atom) The empty list: [] (a special atom) [head | tail] is how lists are stored (linked list) [head | tail] is how lists are stored (linked list) [a,b,c] is shorthand for [a | [b | [c | [] ] ] ] [a,b,c] is shorthand for [a | [b | [c | [] ] ] ] [a,b|tail] is shorthand for [a | [b | tail] ] [a,b|tail] is shorthand for [a | [b | tail] ] element([X | _], X). (_ is unnamed variable) element([X | _], X). (_ is unnamed variable) element([_ | Tail], X). :- element(Tail, X). element([_ | Tail], X). :- element(Tail, X). The second rule is the recursive call The second rule is the recursive call
7
Redundancy element([a,b,a], a), X = 2. element([a,b,a], a), X = 2. Returns twice! X = 2 AND X = 2. Returns twice! X = 2 AND X = 2. ‘a’ is an element of [a,b,a] twice ‘a’ is an element of [a,b,a] twice element finds every way ‘a’ can be an element of [a,b,a], so with backtracking it finds 2 ways element finds every way ‘a’ can be an element of [a,b,a], so with backtracking it finds 2 ways What if instead of X = 2 there was a really computation intensive goal? What if instead of X = 2 there was a really computation intensive goal? That goal would be calculated for the same input twice, finding the same answer twice, which is a waste of time That goal would be calculated for the same input twice, finding the same answer twice, which is a waste of time
8
Cuts (!) Cuts prevent backtracking Cuts prevent backtracking Cannot be backtracked through Cannot be backtracked through Makes the parent goal’s result final (no searching for more answers for the same question) Makes the parent goal’s result final (no searching for more answers for the same question) contains([X | _], X) :- !. contains([X | _], X) :- !. contains([_ | Tail], X) :- contains(Tail, X). contains([_ | Tail], X) :- contains(Tail, X). contains will only find one answer, unlike element, because of the cut (!) contains will only find one answer, unlike element, because of the cut (!)
9
fail, true, and repeat fail is a special goal which always fails fail is a special goal which always fails often used after a cut: often used after a cut: not(Goal) :- Goal, !, fail; true. not(Goal) :- Goal, !, fail; true. true is a special goal which always passes true is a special goal which always passes repeat is a special goal which always passes and continues to pass when backtracked into: repeat is a special goal which always passes and continues to pass when backtracked into: repeat. repeat. repeat :- repeat. repeat :- repeat.
10
Arithmetic X = 1 + 1. produces X = 1 + 1, not X = 2. X = 1 + 1. produces X = 1 + 1, not X = 2. X is 1 + 1 does produce X = 2. X is 1 + 1 does produce X = 2. “is” evaluates the right then unifies with the left “is” evaluates the right then unifies with the left, … evaluate both sides and then compare, … evaluate both sides and then compare In order to be evaluated there cannot be any unknowns, otherwise an error will be thrown. In order to be evaluated there cannot be any unknowns, otherwise an error will be thrown. length([], 0). length([], 0). length([_ | T], L) :- length(T, TL), L is TL + 1. length([_ | T], L) :- length(T, TL), L is TL + 1.
11
bagof and setof Rather than having to hit ‘;’ to get all the answers, why not have a function that does it for you? Rather than having to hit ‘;’ to get all the answers, why not have a function that does it for you? bagof(Variables, Goal, Results) will repeatedly attempt Goal and add the unification of Variables to Results (fails if Goal not possible) bagof(Variables, Goal, Results) will repeatedly attempt Goal and add the unification of Variables to Results (fails if Goal not possible) setof is like bagof but sorts Results and removes duplicates setof is like bagof but sorts Results and removes duplicates
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.