![Page 1: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/1.jpg)
A Taste of PrologAja Hammerly
Thursday, August 2, 12
![Page 2: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/2.jpg)
Basics
• I Like Prolog
• But, I'm not an expert
• This is just an introduction
Thursday, August 2, 12
![Page 3: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/3.jpg)
What Is Prolog
• A logic programing language
• A declarative programming language
• A weird programming language
Thursday, August 2, 12
![Page 4: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/4.jpg)
Uses
• Natural Language Processing
• Grammars
• Theorem Proving
• Expert Systems and other AI
Thursday, August 2, 12
![Page 5: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/5.jpg)
Why Learn Prolog
• Expand your toolbox
• New perspective
• Become a polyglot
Thursday, August 2, 12
![Page 6: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/6.jpg)
Prolog - Weirdness
• “What”, not “How”.
• Programs are expressed as:
• Facts
• Rules
Thursday, August 2, 12
![Page 7: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/7.jpg)
“A computation of a logic program is a deduction of consequences of the
program. A program defines a set of consequences, which is its meaning.
The art of logic programming is constructing concise and elegant programs that have the desired
meaning.”
- The Art of Prolog
Thursday, August 2, 12
![Page 8: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/8.jpg)
Seattle.rb Pairing
Thursday, August 2, 12
![Page 9: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/9.jpg)
editor(zenspider, emacs).editor(drbrain, vim).editor(phiggins, vim).editor(tenderlove, vim).
Facts
Thursday, August 2, 12
![Page 10: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/10.jpg)
?- editor(zenspider, emacs).yes
?- editor(zenspider, vim).no
Questionseditor(zenspider, emacs).editor(drbrain, vim).
Thursday, August 2, 12
![Page 11: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/11.jpg)
?- editor(drbrain, Editor).Editor = vim
Questionseditor(zenspider, emacs).editor(drbrain, vim).
Thursday, August 2, 12
![Page 12: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/12.jpg)
editor(zenspider, emacs).editor(drbrain, vim).
?- editor(Person, Editor).Person = zenspiderEditor = emacs
Questions
Thursday, August 2, 12
![Page 13: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/13.jpg)
?- editor(Person1, vim), editor(Person2, vim),
Person1 \== Person2.Person1 = drbrainPerson2 = tenderlove
Questionseditor(zenspider, emacs).editor(drbrain, vim).editor(tenderlove, vim).
Thursday, August 2, 12
![Page 14: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/14.jpg)
?- editor(Person1, Editor), editor(Person2, Editor),
Person1 \== Person2.Editor = vimPerson1 = drbrainPerson2 = tenderlove
Questionseditor(zenspider, emacs).editor(drbrain, vim).editor(tenderlove, vim).
Thursday, August 2, 12
![Page 15: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/15.jpg)
pair(Person1, Person2) :- editor(Person1, Editor), editor(Person2, Editor), Person1 \== Person2.
Rules
?- pair(Person1, Person2).Person1 = drbrainPerson2 = tenderlove
Thursday, August 2, 12
![Page 16: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/16.jpg)
Questions & Ruleseditor(zenspider, emacs).editor(drbrain, vim).editor(tenderlove, vim).
?- pair(drbrain, Person2).Person1 = tenderlove
Thursday, August 2, 12
![Page 17: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/17.jpg)
?- pair(Person1, Person2).
Person1 = drbrainPerson2 = tenderlove ? ;
Person1 = drbrainPerson2 = phiggins ? ;
Person1 = tenderlovePerson2 = drbrain ?
Questions
Thursday, August 2, 12
![Page 18: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/18.jpg)
pair(Person1, Person2) :- editor(Person1, Editor), editor(Person2, Editor), Person1 @> Person2.
Rules
?- pair(Person1, Person2).Person1 = tenderlovePerson2 = drbrain
Thursday, August 2, 12
![Page 19: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/19.jpg)
?- pair(Person1, Person2).
Person1 = tenderlovePerson2 = drbrain ? ;
Person1 = tenderlovePerson2 = phiggins ? ;
Person1 = phigginsPerson2 = drbrain ?
Questions
Thursday, August 2, 12
![Page 20: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/20.jpg)
keyboard(zenspider, dvorak).keyboard(drbrain, dvorak).keyboard(tenderlove, qwerty).keyboard(phiggins, qwerty).
Facts
Thursday, August 2, 12
![Page 21: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/21.jpg)
?- keyboard(drbrain, Keyboard).Keyboard = dvorak
Questionskeyboard(zenspider, dvorak).keyboard(drbrain, dvorak).
Thursday, August 2, 12
![Page 22: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/22.jpg)
pair(Person1, Person2) :- keyboard(Person1, Keyboard), keyboard(Person2, Keyboard), Person1 @> Person2.
Rules
?- pair(Person1, Person2).Person1 = zenspiderPerson2 = drbrain
Thursday, August 2, 12
![Page 23: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/23.jpg)
pair(P1, P2) :- editor( P1, Editor), editor( P2, Editor), P1 @> P2.pair(P1, P2) :- keyboard(P1, Keyboard), keyboard(P2, Keyboard), P1 @> P2.
Two Rules
Thursday, August 2, 12
![Page 24: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/24.jpg)
?- pair(X, Y).
X = tenderlove, Y = drbrainX = tenderlove, Y = phigginsX = phiggins, Y = drbrainX = zenspider, Y = drbrainX = tenderlove, Y = phiggins
Questions
Thursday, August 2, 12
![Page 25: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/25.jpg)
super_pair(Person1, Person2) :- editor(Person1, Editor), editor(Person2, Editor), keyboard(Person1, Keyboard), keyboard(Person2, Keyboard), Person1 @> Person2.
Rule
Thursday, August 2, 12
![Page 26: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/26.jpg)
?- super_pair(Person1, Person2).
Person1 = tenderlovePerson2 = phiggins
Questionseditor(phiggins, vim).editor(tenderlove, vim).keyboard(tenderlove, qwerty).keyboard(phiggins, qwerty).
Thursday, August 2, 12
![Page 27: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/27.jpg)
Pattern Matching
• In prolog pattern matching is used to pass arguments.
• For example:
• human(X) will match human(bill)
• Pattern matching with variables is called unification
Thursday, August 2, 12
![Page 28: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/28.jpg)
List Basics
Thursday, August 2, 12
![Page 29: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/29.jpg)
Examples
• []
• [1, 2, 3]
• [apples, bananas]
• [1, lemon]
• [[1, lemon], [1, lime], [2, coconuts]]
Thursday, August 2, 12
![Page 30: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/30.jpg)
Heads and Tails
• [1, 2, 3]
• 1 is the head
• [2, 3] is the tail
• [H | T] (read: "H bar T")
• [H | T] matches with [1, 2, 3] as [1|[2,3]]
Thursday, August 2, 12
![Page 31: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/31.jpg)
Don't Care• ‘_’ means I don't care
• [1, _, 3] could be
• [1, 2, 3] or
• [1, pi, 3] or
• [1, [apple, pie], 3]
• 2 don't cares can refer to different values
Thursday, August 2, 12
![Page 32: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/32.jpg)
def member(x, ary) return false if ary == [] return true if ary[0] == x member(x, ary[1..-1])end
Member
Thursday, August 2, 12
![Page 33: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/33.jpg)
def member(x, ary) return false if ary == [] return true if ary[0] == x member(x, ary[1..-1])end
Member
member(H, [H | _]).member(X, [_ | T]):- member(X, T).
Thursday, August 2, 12
![Page 34: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/34.jpg)
?- member(2, [1, 2, 3]).
true
?- member(6, [1, 2, 3]).
no
Thursday, August 2, 12
![Page 35: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/35.jpg)
?- member(X, [1, 2, 3]).
X = 1 ? a
X = 2
X = 3
Thursday, August 2, 12
![Page 36: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/36.jpg)
?- member(6, X).
X = [6|_] ? ;
X = [_,6|_] ? ;
X = [_,_,6|_] ?
Variables Anywhere
Thursday, August 2, 12
![Page 37: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/37.jpg)
def length(ary) return 0 if ary == [] return length(ary[1..-1]) + 1end
Length
Thursday, August 2, 12
![Page 38: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/38.jpg)
def length(ary) return 0 if ary == [] return length(ary[1..-1]) + 1end
Length
length([], 0).length([_ | T], N) :- length(T, N1), N is N1 + 1.
Thursday, August 2, 12
![Page 39: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/39.jpg)
?- length([a, b, c, d], 4).
yes
?- length([1, 2, 3], X).
X = 3
Thursday, August 2, 12
![Page 40: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/40.jpg)
?- length(X, 2).
X = [_,_]
Thursday, August 2, 12
![Page 41: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/41.jpg)
Circuits
Thursday, August 2, 12
![Page 42: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/42.jpg)
In Outinv(0, 1).inv(1, 0).
Thursday, August 2, 12
![Page 43: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/43.jpg)
A B Outor(0, 0, 0).or(1, 0, 1).or(0, 1, 1).or(1, 1, 1).
Thursday, August 2, 12
![Page 44: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/44.jpg)
A B Outand(0, 0, 0).and(0, 1, 0).and(1, 0, 0).and(1, 1, 1).
Thursday, August 2, 12
![Page 45: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/45.jpg)
A B Outxor(0, 0, 0).xor(0, 1, 1).xor(1, 0, 1).xor(1, 1, 0).
Thursday, August 2, 12
![Page 46: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/46.jpg)
A B Outnand(0, 0, 1).nand(0, 1, 1).nand(1, 0, 1).nand(1, 1, 0).
Thursday, August 2, 12
![Page 47: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/47.jpg)
half_adder(A, B, C, S) :- xor(A, B, S), and(A, B, C).
Thursday, August 2, 12
![Page 48: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/48.jpg)
?- half_adder(A, B, C, S).
A = 0, B = 0, C = 0, S = 0
A = 0, B = 1, C = 0, S = 1
A = 1, B = 0, C = 0, S = 1
A = 1, B = 1, C = 1, S = 0
Thursday, August 2, 12
![Page 49: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/49.jpg)
full_adder(A, B, Cin, Cout, S) :- half_adder(A, B, C1, S1), half_adder(Cin, S1, C2, S), or(C1, C2, Cout).
Thursday, August 2, 12
![Page 50: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/50.jpg)
?- full_adder(A, B, 1, Cout, 1).
A = 0, B = 0, Cout = 0A = 1, B = 1, Cout = 1
? - full_adder(A, B, Cin, 1, S).
A = 0, B = 1, Cin = 1, S = 0A = 1, B = 0, Cin = 1, S = 0A = 1, B = 1, Cin = 0, S = 0A = 1, B = 1, Cin = 1, S = 1
Thursday, August 2, 12
![Page 51: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/51.jpg)
mystery(A, B, D) :- nand(A, B, T1), nand(A, T1, T2), nand(B, T1, T3), nand(T2, T3, D).
Thursday, August 2, 12
![Page 52: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/52.jpg)
?- mystery(A, B, D).
A = 0, B = 0, D = 0
A = 0, B = 1, D = 1
A = 1, B = 0, D = 1
A = 1, B = 1, D = 0
Thursday, August 2, 12
![Page 53: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/53.jpg)
Logic PuzzlesThursday, August 2, 12
![Page 54: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/54.jpg)
Apartment Building1. Adam does not live on the top floor.
2. Bill does not live on the bottom floor.
3. Cora does not live on either the top or the bottom floor.
4. Dale lives on a higher floor than does Bill.
5. Erin does not live on a floor adjacent to Cora's.
6. Cora does not live on a floor adjacent to Bill's.
Thursday, August 2, 12
![Page 55: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/55.jpg)
‘Data Structure’
• A list of the people, ordered by floor
• [Top, Floor4, Floor3, Floor2, Bottom]
• [adam, bill, cora, dale, erin]
Thursday, August 2, 12
![Page 56: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/56.jpg)
adam \== Top,
Adam does not live on the top floor.
Thursday, August 2, 12
![Page 57: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/57.jpg)
bill \== Bottom,
Bill does not live on the bottom floor.
Thursday, August 2, 12
![Page 58: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/58.jpg)
cora \== Top,cora \== Bottom,
Cora does not live on either the top or the bottom floor.
Thursday, August 2, 12
![Page 59: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/59.jpg)
higher(dale, bill, L),
Dale lives on a higher floor than does Bill.
Thursday, August 2, 12
![Page 60: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/60.jpg)
higher(X, Y, [X | T]) :- member(Y, T).
higher(X, Y, [_ | T]) :- higher(X, Y, T).
Higher
Thursday, August 2, 12
![Page 61: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/61.jpg)
not_adjacent(erin, cora, L),
Erin does not live on a floor adjacent to Cora's.
Thursday, August 2, 12
![Page 62: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/62.jpg)
not_adjacent
Thursday, August 2, 12
![Page 63: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/63.jpg)
not_adjacentnot_adjacent(X, Y, [X, Z | T]) :- Z \== Y,
member(Y, T).
Thursday, August 2, 12
![Page 64: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/64.jpg)
not_adjacent
not_adjacent(X, Y, [Y, Z | T]) :- Z \== X, member(X, T).
not_adjacent(X, Y, [X, Z | T]) :- Z \== Y,
member(Y, T).
Thursday, August 2, 12
![Page 65: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/65.jpg)
not_adjacent(X, Y, [_ | T]) :- not_adjacent(X, Y, T).
not_adjacent
not_adjacent(X, Y, [Y, Z | T]) :- Z \== X, member(X, T).
not_adjacent(X, Y, [X, Z | T]) :- Z \== Y,
member(Y, T).
Thursday, August 2, 12
![Page 66: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/66.jpg)
not_adjacent(cora, bill, L),
Cora does not live on a floor adjacent to Bill's.
Thursday, August 2, 12
![Page 67: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/67.jpg)
permutation(L, [adam, bill, cora, dale, erin]).
permutation
Thursday, August 2, 12
![Page 68: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/68.jpg)
puzzle(L) :- L = [Top, F4, F3, F2, Bottom],
Puzzle
Thursday, August 2, 12
![Page 69: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/69.jpg)
puzzle(L) :- permutation(L, [adam, bill, cora, dale, erin]), L = [Top, Floor4, Floor3, Floor2, Bottom], adam \== Top, bill \== Bottom, cora \== Top, cora \== Bottom, higher(dale, bill, L), not_adjacent(erin, cora, L), not_adjacent(cora, bill, L).
All Together
Thursday, August 2, 12
![Page 70: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/70.jpg)
| ?- puzzle([A, B, C, D, E]).
A = daleB = coraC = adamD = billE = erin ? ;
no
Running
Thursday, August 2, 12
![Page 71: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/71.jpg)
Learn More
Thursday, August 2, 12
![Page 72: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/72.jpg)
Books• Sterling, Leon & Shapiro, Ehud. The Art of Prolog
• Clocksin, William F. Clause and Effect: Prolog Programming for the Working Programmer
• Bratko, Ivan. Prolog Programming for Artificial Intelligence
• Tate, Bruce A. Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages
Thursday, August 2, 12
![Page 73: A Taste of Prolog - thagomizer.com · • This is just an introduction Thursday, August 2, 12. ... • Natural Language Processing • Grammars • Theorem Proving • Expert Systems](https://reader034.vdocuments.us/reader034/viewer/2022043006/5f8de6cb47dabc4db45e9d8e/html5/thumbnails/73.jpg)
Thank You
Thursday, August 2, 12