Peteris Erins
November 22, 2012
3.2k

Logic Programming

Given at #HNLondon on Nov 22, 2012

Peteris Erins

November 22, 2012

Transcript

1. Logic Programming
Peteris Erins

2. p_e
@

3. Logic

4. Logic
Formal models of human reasoning

5. Logic
is expressive

6. ∀m ∃n ∀a ∀b (n ≥ m) ∧ [(a = 1) ∨ (b = 1) ∨ (ab ≠ n)]
?

7. ∀m ∃n ∀a ∀b (n ≥ m) ∧ [(a = 1) ∨ (b = 1) ∨ (ab ≠ n)]
There are inﬁnitely many primes

8. Prolog (1972)

9. Relations are relationships
between stuff

10. human(you)
less_than(2, 3)
prefix(‘ban’, ‘banana’)
even(X)
relation name
upper case for variables
lower case for constants

sorted([3, 1, 2], [1, 2, 3])
Functions are easily turned into relations

12. Horn clauses

13. Horn clauses
If four legs and has a tail, then a dog
many “if”s one “then”

14. Horn clauses
If four legs and has a tail, then a dog
D is a dog if has four legs, has a tail

15. Horn clauses
If four legs and has a tail, then a dog
D is a dog if has four legs, has a tail
dog(D) if legs(D, 4) & has_tail(D)

16. Horn clauses
If four legs and has a tail, then a dog
D is a dog if has four legs, has a tail
dog(D) if legs(D, 4) & has_tail(D)
dog(D) :-
legs(D, 4),
has_tail(D).
“if”
“and”

17. Prolog, how does it work?

18. even(2).
even(4).
even(6).
Prolog, how does it work?
1. Write facts
prolog, 2 is even
prolog, 4 is even
prolog, 6 is even

19. even(2).
even(4).
even(6).
Prolog, how does it work?
1. Write facts
2. Ask a question with a variable
?- even(X).
prolog, 2 is even
prolog, 4 is even
prolog, 6 is even
prolog, can you ﬁnd an
even X?

20. even(2).
even(4).
even(6).
Prolog, how does it work?
1. Write facts
2. Ask a question with a variable
?- even(X).
3. Prolog solves for the variable
prolog, 2 is even
prolog, 4 is even
prolog, 6 is even
prolog, can you ﬁnd an
even X?
X = 2 ;
X = 4 ;
X = 6.

21. Example
Meta compiler?
NLP grammar parser?
Bayesian expert system?

22. Example
Meta compiler
NLP grammar parser
Bayesian expert system
Todo list

23. Todo list
% source code file
% [= fact/relation database]
% interactive console for queries
code
repl

24. Todo list
code
repl
todo(Item)
homework
laundry
dishes
talk
the relation we want
to construct

25. Todo list
todo(homework).
todo(laundry).
todo(dishes).
todo(talk).
code
repl
todo(Item)
homework
laundry
dishes
talk
the construction

26. Todo list
todo(homework).
todo(laundry).
todo(dishes).
todo(talk).
?- todo(X).
X = homework ;
X = laundry ;
X = dishes ;
X = talk.
code
repl
todo(Item)
homework
laundry
dishes
talk
testing

27. Todo list
just_before(laundry, homework).
just_before(dishes, laundry).
?- just_before(X, homework).
X = laundry.
code
repl
just_before
just_before
Before After
laundry homework
dishes laundry

28. Todo list
cannot_do(X) :-
just_before(Y, X),
todo(Y).
?- cannot_do(X).
X = homework ;
X = laundry.
code
repl
cannot_do(Item)
homework
laundry

29. Todo list
code
repl
before
before
Before After
laundry homework
dishes laundry
dishes homework
just_before
just_before
Before After
laundry homework
dishes laundry

30. Todo list
before(X, Y) :- just_before(X, Y).
before(X, Y) :-
just_before(X, T),
before(T, Y).
?- before(X, homework).
X = laundry ;
X = dishes.
code
repl
before
before
Before After
laundry homework
dishes laundry
dishes homework

31. Todo list
tag(homework, work).
tag(laundry, home).
tag(dishes, home).
tag(talk, work).
?- tag(dishes, T).
T = home.
?- tag(X, work).
X = homework ;
X = talk.
code
repl
tag
tag
homework work
laundry home
dishes home
talk work

32. Todo list
agenda_tagged(Tag, Agenda) :-
findall(X, tag(X, Tag) , Need_Doing),
findall(X, cannot_do(X), Cant_Do),
subtract(Need_Doing, Cant_Do, Agenda).
?- agenda_tagged(home,Y).
Y = [dishes].
?- agenda_tagged(work,Y).
Y = [talk].
code
repl
agenda_tagged
agenda_tagged
Tag Agenda
home [dishes]
work [talk]

33. How to think about logic programming?

34. How to think about logic programming?
1. functional = imperative – mutable state
logic = functional – manual search

35. How to think about logic programming?
1. functional = imperative – mutable state
logic = functional – manual search
2. combining database and program

36. How to think about logic programming?
1. functional = imperative – mutable state
logic = functional – manual search
2. combining database and program
3. an implementation of mathematical logic

37. How to think about logic programming?
1. functional = imperative – mutable state
logic = functional – manual search
2. combining database and program
3. an implementation of mathematical logic
not really

38. Here’s the catch:
io
ﬂow
scoping
numerics

39. Logic in the wild

40. IBM Watson

41. Wolfram Mathematica

42. Datomic

43. Type systems

44. /**
* Type class witnessing that `A` is less than or equal to `B`.
*
* @author Miles Sabin
*/
trait LTEq[A <: Nat, B <: Nat]
object LTEq {
import Nat._0
type <=[A <: Nat, B <: Nat] = LTEq[A, B]
implicit def ltEq1 = new <=[_0, _0] {}
implicit def ltEq2[B <: Nat] = new <=[_0, Succ[B]] {}
implicit def ltEq3[A <: Nat, B <: Nat](implicit lt : A <= B) =
new <=[Succ[A], Succ[B]] {}
}
Scala shapeless library
Try ﬁnding the logic

45. p_e
@
Thank you!
github.com/Pet3ris/talk-logic-programming
Peteris Erins
thanks to reviewers:
@DARowlands
@Sedols
@Springcoil
Panagiotis Charalampopolous
special thanks:
@swannodette
@zoltanvarju
@richlitt