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

Logic Programming

Logic Programming

A quick exploration on Logic Programming... with Ruby and Ruby-Prolog

Mario Alberto Chávez

October 14, 2014
Tweet

More Decks by Mario Alberto Chávez

Other Decks in Programming

Transcript

  1. Logic Programming • We do not express what a program

    should do • We do express data and queries in a specific format
  2. Main Blocks • Facts, assertions about our domain • Rules,

    express inferences about our facts • Queries, questions to be answered
  3. likes['wallace', 'cheese'].fact ! likes['grommit', 'cheese'].fact! likes['wendolene', 'sheep'].fact! friend[:X, :Y] <<=

    [noteq[:X,:Y], likes[:X, :Z], likes[:Y, :Z]] query(friend['wallace', 'grommit']) rp = RubyProlog::Core.new! rp.instance_eval do! end
  4. class Family < RubyProlog::Core! def initialize! super! facts! rules! end!

    ! def siblings(person)! query sibling[:X, person]! end! ! def parents(child)! query parent[:X, child]! end! ! def children(parent)! query parent[parent, :Y]! end! ! private! def facts! mother["Trude", "Sally"].fact! father["Tom", "Sally"].fact! father["Tom", "Erica"].fact! father["Mike", "Tom"].fact! end! ! def rules! sibling[:X,:Y] <<= [ parent[:Z,:X], parent[:Z,:Y], noteq[:X,:Y] ]! parent[:X,:Y] <<= father[:X,:Y]! parent[:X,:Y] <<= mother[:X,:Y]! end! end! ! s = Family.new! s.siblings 'Sally'!
  5. Not as complete as Prolog But good enough to explore

    a different way to solve problems