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. ∀m ∃n ∀a ∀b (n ≥ m) ∧ [(a =

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

    1) ∨ (b = 1) ∨ (ab ≠ n)] There are infinitely many primes ≡
  3. add(2, 3, 5) sorted([3, 1, 2], [1, 2, 3]) Functions

    are easily turned into relations add(2, 3) = 5
  4. Horn clauses If four legs and has a tail, then

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

    a dog D is a dog if has four legs, has a tail
  6. 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)
  7. 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”
  8. 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
  9. 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?
  10. 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.
  11. Todo list % source code file % [= fact/relation database]

    % interactive console for queries code repl
  12. 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
  13. 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
  14. 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
  15. Todo list code repl before before Before After laundry homework

    dishes laundry dishes homework just_before just_before Before After laundry homework dishes laundry
  16. 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
  17. 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
  18. 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]
  19. How to think about logic programming? 1. functional = imperative

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

    – mutable state logic = functional – manual search 2. combining database and program
  21. 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
  22. 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
  23. /** * 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
  24. 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