Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Logic Programming

Peteris Erins
November 22, 2012

Logic Programming

Given at #HNLondon on Nov 22, 2012

Peteris Erins

November 22, 2012
Tweet

More Decks by Peteris Erins

Other Decks in Programming

Transcript

  1. Logic Programming
    Peteris Erins

    View full-size slide

  2. Logic
    Formal models of human reasoning

    View full-size slide

  3. Logic
    is expressive

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  6. Prolog (1972)

    View full-size slide

  7. Relations are relationships
    between stuff

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  10. Horn clauses

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  14. 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”

    View full-size slide

  15. Prolog, how does it work?

    View full-size slide

  16. 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

    View full-size slide

  17. 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?

    View full-size slide

  18. 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.

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  24. 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

    View full-size slide

  25. 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

    View full-size slide

  26. 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

    View full-size slide

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

    View full-size slide

  28. 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

    View full-size slide

  29. 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

    View full-size slide

  30. 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]

    View full-size slide

  31. How to think about logic programming?

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  34. 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

    View full-size slide

  35. 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

    View full-size slide

  36. Here’s the catch:
    io
    flow
    scoping
    numerics

    View full-size slide

  37. Logic in the wild

    View full-size slide

  38. Wolfram Mathematica

    View full-size slide

  39. Type systems

    View full-size slide

  40. /**
    * 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

    View full-size slide

  41. 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

    View full-size slide