このページは https://speakerdeck.com/peteris/logic-programming の内容を掲載しています。

掲載を希望されないスライド著者の方は、こちらよりご連絡下さい。

- Logic Programming

Peteris Erins - p_

@

e - Logic
- Logic

Formal models of human reasoning - Logic

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

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

≡

There are infinitely many primes - Prolog (1972)
- Relations are relationships

between stuff - relation name

lower case for constants

human(you)

less_than(2, 3)

prefix(‘ban’, ‘banana’)

even(X)

upper case for variables - Functions are easily turned into relations

add(2, 3) = 5

add(2, 3, 5)

sorted([3, 1, 2], [1, 2, 3]) - Horn clauses
- Horn clauses

If four legs and has a tail, then a dog

many “if”s

one “then” - Horn clauses

If four legs and has a tail, then a dog

D is a dog if has four legs, has a tail - 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) - 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)

“if”

“and”

dog(D) :-

legs(D, 4),

has_tail(D). - Prolog, how does it work?
- Prolog, how does it work?

1. Write facts

even(2).

prolog, 2 is even

even(4).

prolog, 4 is even

even(6).

prolog, 6 is even - Prolog, how does it work?

1. Write facts

even(2).

prolog, 2 is even

even(4).

prolog, 4 is even

even(6).

prolog, 6 is even

2. Ask a question with a variable

?- even(X).

prolog, can you find an

even X? - Prolog, how does it work?

1. Write facts

even(2).

prolog, 2 is even

even(4).

prolog, 4 is even

even(6).

prolog, 6 is even

2. Ask a question with a variable

?- even(X).

prolog, can you find an

even X?

3. Prolog solves for the variable

X = 2 ;

X = 4 ;

X = 6. - Example

Meta compiler?

NLP grammar parser?

Bayesian expert system? - Example

Meta compiler

NLP grammar parser

Bayesian expert system

Todo list - Todo list

% source code file

% [= fact/relation database]

code

repl

% interactive console for queries - Todo list

todo(Item)

homework

laundry

dishes

talk

the relation we want

to construct

code

repl - Todo list

todo(homework).

todo(Item)

todo(laundry).

homework

todo(dishes).

laundry

todo(talk).

dishes

talk

code

repl

the construction - Todo list

todo(homework).

todo(Item)

todo(laundry).

homework

todo(dishes).

laundry

todo(talk).

dishes

talk

code

?- todo(X).

repl

X = homework ;

X = laundry ;

X = dishes ;

testing

X = talk. - Todo list

just_before(laundry, homework).

just_before(dishes, laundry).

code

?- just_before(X, homework).

repl

X = laundry.

just_before

Before

After

laundry homework

dishes

laundry - Todo list

cannot_do(X) :-

cannot_do(Item)

just_before(Y, X),

homework

todo(Y).

laundry

code

?- cannot_do(X).

repl

X = homework ;

X = laundry. - Todo list

just_before

Before

After

laundry homework

dishes

laundry

code

repl

before

Before

After

laundry homework

dishes

laundry

dishes

homework - Todo list

before(X, Y) :- just_before(X, Y).

before(X, Y) :-

just_before(X, T),

before(T, Y).

code

?- before(X, homework).

repl

X = laundry ;

before

X = dishes.

Before

After

laundry homework

dishes

laundry

dishes

homework - Todo list

tag(homework, work).

tag(laundry, home).

tag(dishes, home).

tag(talk, work).

code

?- tag(dishes, T).

repl

T = home.

tag

Task

Tag

?- tag(X, work).

homework

work

X = homework ;

laundry

home

X = talk.

dishes

home

talk

work - 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).

code

?- agenda_tagged(home,Y).

repl

Y = [dishes].

agenda_tagged

Tag

Agenda

?- agenda_tagged(work,Y).

home

[dishes]

Y = [talk].

work

[talk] - How to think about logic programming?
- How to think about logic programming?

1. functional = imperative – mutable state

logic = functional – manual search - How to think about logic programming?

1. functional = imperative – mutable state

logic = functional – manual search

2. combining database and program - 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 - 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 - Here’s the catch:

io

flow

scoping

numerics - Logic in the wild
- IBM Watson
- Wolfram Mathematica
- Datomic
- Type systems
- Scala shapeless library

/**

* 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]] {}

}

Try finding the logic - Thank you!

p_

@

e

github.com/Pet3ris/talk-logic-programming

thanks to reviewers:

special thanks:

@DARowlands

@swannodette

@Sedols

@zoltanvarju

@Springcoil

@richlitt

Panagiotis Charalampopolous

Peteris Erins