Slide 1

Slide 1 text

Logic Programming Peteris Erins

Slide 2

Slide 2 text

p_e @

Slide 3

Slide 3 text

Logic

Slide 4

Slide 4 text

Logic Formal models of human reasoning

Slide 5

Slide 5 text

Logic is expressive

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

∀m ∃n ∀a ∀b (n ≥ m) ∧ [(a = 1) ∨ (b = 1) ∨ (ab ≠ n)] There are infinitely many primes ≡

Slide 8

Slide 8 text

Prolog (1972)

Slide 9

Slide 9 text

Relations are relationships between stuff

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Horn clauses

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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)

Slide 16

Slide 16 text

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”

Slide 17

Slide 17 text

Prolog, how does it work?

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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 find an even X?

Slide 20

Slide 20 text

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 find an even X? X = 2 ; X = 4 ; X = 6.

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Example Meta compiler NLP grammar parser Bayesian expert system Todo list

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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 Task Tag homework work laundry home dishes home talk work

Slide 32

Slide 32 text

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]

Slide 33

Slide 33 text

How to think about logic programming?

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

Here’s the catch: io flow scoping numerics

Slide 39

Slide 39 text

Logic in the wild

Slide 40

Slide 40 text

IBM Watson

Slide 41

Slide 41 text

Wolfram Mathematica

Slide 42

Slide 42 text

Datomic

Slide 43

Slide 43 text

Type systems

Slide 44

Slide 44 text

/** * 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 finding the logic

Slide 45

Slide 45 text

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