SlideShare ist ein Scribd-Unternehmen logo
1 von 12
ruby-prolog
    Logical programming with Ruby declarations.

    2009.02.09 - Preston Lee <preston.lee@openrain.com> - Http://prestonlee.com


Tuesday, February 10, 2009
Logical programming paradigm.


         Declarative.
    ✤
         We define the rules for the world, but not how to process them.



         Inherently recursive.
    ✤

         The interpreter will automatically recurse to evaluate a query behind the scenes.



         Turing complete.
    ✤
         You can program without objects or functions, if you so choose.



         Artificial intelligence.
    ✤

         It’s easier to define and resolve complex logical problems when we think mathematically.




Tuesday, February 10, 2009
Core Prolog concepts.


         Rules
    ✤


               Generic semantic definitions (predicates) of how your world works via clauses.
          ✤


               Declarations are formal mathematics (predicate logic) in programming syntax.
          ✤


         Facts
    ✤


               Assertions of truth about the world.
          ✤


               Bob is Charlie’s father.
          ✤


               Bob is Dale’s father.
          ✤


         Queries
    ✤


               Attempts to resolve an unknown logical statement using the rule given the facts.
          ✤




Tuesday, February 10, 2009
Project history.


         tiny_prolog resolver/unification implementation from teh
    ✤

         internets. Various small additional patches.

         Refactored to be object-oriented, not mess with
    ✤

         method_missing globally, play nice with garbage
         collection and support multiple simultaneous contexts.

         Test cases from scratch, and various snippets ported from
    ✤

         Prolog.


Tuesday, February 10, 2009
Simple family tree, in Prolog.

         Rules
    ✤


               We are siblings if we share a parent.
          ✤


               A father is a parent.
          ✤
                                                       sibling(X, Y)      :- parent_child(Z, X), parent_child(Z, Y).

               A mother is a parent.
          ✤
                                                       parent_child(X, Y) :- father_child(X, Y).
                                                       parent_child(X, Y) :- mother_child(X, Y).
         Facts
    ✤
                                                       mother_child(alice, charlie).
               Alice is Charlie’s mother.              father_child(bob, charlie).
          ✤
                                                       father_child(bob, dale).
               Bob is Charlie’s father.
          ✤
                                                       /*
                                                       * Who are Charle's siblings?
               Bob is Dale’s father.
          ✤
                                                       * sibling(charlie, X).
                                                       *
         Queries
    ✤                                                  * Who are Charlie's parents?
                                                       * parent_child(X, sally).
               Who are Alice’s siblings?               */
          ✤




Tuesday, February 10, 2009
Imperative programming support.

                             /*******************************************************
                              * 99 Bottles of Beer
                              * Paul J. DeMarco 9/20/2002
                              * beer.pro
                              * To execute start gprolog (others may work)
                              * consult('beer.pro').
                              * drink(beer,99).
                              *******************************************************/
                             english(beer,0):-
                                                     write('no more bottle of beer').
                             english(beer,1):-
                                                     write('1 bottle of beer').
                             english(beer,X):-
                                                     X >= 2,
                                                     write( X ) ,
                                                     write(' bottles of beer').

                             drink(beer,X):- X >= 1,
                                                       english(beer,X),
                                                       write(' on the wall, '),
                                                       english(beer,X),
                                                       write(', take one down, pass it aroundn'),
                                                       X1 is X - 1,
                                                       english(beer,X1),
                                                       write(' on the wall.n'),
                                                       drink(beer, X1).




Tuesday, February 10, 2009
ruby-prolog
                                      c = RubyProlog::Core.new
                                      c.instance_eval do

                                        vendor['dell'].fact
                                        vendor['apple'].fact

                                        model['ultrasharp'].fact
                                        model['xps'].fact
                                        model['macbook'].fact
                                        model['iphone'].fact

                                        manufactures['dell', 'ultrasharp'].fact
                                        manufactures['dell', 'xps'].fact
                                        manufactures['apple', 'macbook'].fact

         Prolog-like DSL.               manufactures['apple', 'iphone'].fact
    ✤
                                        is_a['xps', 'laptop'].fact
                                        is_a['macbook', 'laptop'].fact
                                        is_a['ultrasharp', 'monitor'].fact
         Object-oriented wrapper.
    ✤                                   is_a['iphone', 'phone'].fact

                                        kind['laptop']
                                        kind['monitor']
         Not as complete as Prolog.     kind['phone']
    ✤

                                        model[:M] <<= [manufactures[:V, :M]]

                                        vendor_of[:V, :K] <<= [vendor[:V],
                                                     manufactures[:V, :M], is_a[:M, :K]]

                                        p query(is_a[:K, 'laptop'])
                                        p query(vendor_of[:V, 'phone'])

                                      end


Tuesday, February 10, 2009
Complex logical reasoning.


                             The Towers of Hanoi




Tuesday, February 10, 2009
Two implementations.

                     Prolog                                     ruby-prolog

                                                 c = RubyProlog::Core.new
                                                 c.instance_eval do

             move(1,X,Y,_) :-                      move[0,:X,:Y,:Z] <<= :CUT
                 write('Move top disk from '),     move[:N,:A,:B,:C] <<= [
                 write(X),                           is(:M,:N){|n| n - 1}, # reads as quot;M IS N - 1quot;
                 write(' to '),                      move[:M,:A,:C,:B],
                 write(Y),                           write_info[:A,:B],
                 nl.                                 move[:M,:C,:B,:A]
                                                   ]
             move(N,X,Y,Z) :-                      write_info[:X,:Y] <<= [
                 N>1,                                write[quot;move a disc from the quot;],
                 M is N-1,                           write[:X], write[quot; pole to the quot;],
                 move(M,X,Z,Y),                      write[:Y], writenl[quot; pole quot;]
                 move(1,X,Y,_),                    ]
                 move(M,Z,Y,X).
                                                   move[3,quot;leftquot;,quot;rightquot;,quot;centerquot;]
             /* move(3,left,right,center). */
                                                 end




Tuesday, February 10, 2009
ACL Example



                             examples/acls.rb




Tuesday, February 10, 2009
Ideas for the future.


         active_prolog - Logical interface to relational ActiveRecord objects.
    ✤




         logical_authentication - Easy custom ACL creation and enforcement.
    ✤




         logical_search - Custom DB search query builder using English-like
    ✤

         predicates.

         ...
    ✤




Tuesday, February 10, 2009
Thanks!




         Code: http://github.com/preston/ruby-prolog/tree/master
    ✤




         Releases: http://rubyforge.org/projects/ruby-prolog/
    ✤




Tuesday, February 10, 2009

Weitere ähnliche Inhalte

Andere mochten auch

Part 8 add,update,delete records using records operation buttons in vb.net
Part 8 add,update,delete records using records operation buttons in vb.netPart 8 add,update,delete records using records operation buttons in vb.net
Part 8 add,update,delete records using records operation buttons in vb.net
Girija Muscut
 
Python Tools for Visual Studio: Python na Microsoftovom .NET-u
Python Tools for Visual Studio: Python na Microsoftovom .NET-uPython Tools for Visual Studio: Python na Microsoftovom .NET-u
Python Tools for Visual Studio: Python na Microsoftovom .NET-u
Nikola Plejic
 

Andere mochten auch (15)

Part 8 add,update,delete records using records operation buttons in vb.net
Part 8 add,update,delete records using records operation buttons in vb.netPart 8 add,update,delete records using records operation buttons in vb.net
Part 8 add,update,delete records using records operation buttons in vb.net
 
Part2 database connection service based using vb.net
Part2 database connection service based using vb.netPart2 database connection service based using vb.net
Part2 database connection service based using vb.net
 
Part 1 picturebox using vb.net
Part 1 picturebox using vb.netPart 1 picturebox using vb.net
Part 1 picturebox using vb.net
 
Presentation1
Presentation1Presentation1
Presentation1
 
Making Information Usable: The Art & Science of Information Design
Making Information Usable: The Art & Science of Information DesignMaking Information Usable: The Art & Science of Information Design
Making Information Usable: The Art & Science of Information Design
 
Python Tools for Visual Studio: Python na Microsoftovom .NET-u
Python Tools for Visual Studio: Python na Microsoftovom .NET-uPython Tools for Visual Studio: Python na Microsoftovom .NET-u
Python Tools for Visual Studio: Python na Microsoftovom .NET-u
 
Part 5 create sequence increment value using negative value
Part 5 create sequence increment value using negative valuePart 5 create sequence increment value using negative value
Part 5 create sequence increment value using negative value
 
Part 3 binding navigator vb.net
Part 3 binding navigator vb.netPart 3 binding navigator vb.net
Part 3 binding navigator vb.net
 
What&rsquo;s new in Visual C++
What&rsquo;s new in Visual C++What&rsquo;s new in Visual C++
What&rsquo;s new in Visual C++
 
Debugging in visual studio (basic level)
Debugging in visual studio (basic level)Debugging in visual studio (basic level)
Debugging in visual studio (basic level)
 
Science Information Literacy Tutorials and Pedagogy
Science Information Literacy Tutorials and Pedagogy Science Information Literacy Tutorials and Pedagogy
Science Information Literacy Tutorials and Pedagogy
 
Cpp lab 13_pres
Cpp lab 13_presCpp lab 13_pres
Cpp lab 13_pres
 
RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...
RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...
RuleML2015: Explanation of proofs of regulatory (non-)complianceusing semanti...
 
Offline Omni Font Arabic Optical Text Recognition System using Prolog Classif...
Offline Omni Font Arabic Optical Text Recognition System using Prolog Classif...Offline Omni Font Arabic Optical Text Recognition System using Prolog Classif...
Offline Omni Font Arabic Optical Text Recognition System using Prolog Classif...
 
Forensic Science Information Literacy
Forensic Science Information LiteracyForensic Science Information Literacy
Forensic Science Information Literacy
 

Ähnlich wie Logical Programming With ruby-prolog

Ähnlich wie Logical Programming With ruby-prolog (8)

PowerPoint Presentation
PowerPoint PresentationPowerPoint Presentation
PowerPoint Presentation
 
NUS iOS Swift Talk
NUS iOS Swift TalkNUS iOS Swift Talk
NUS iOS Swift Talk
 
Curious case of Dust
Curious case of DustCurious case of Dust
Curious case of Dust
 
Crystal Rocks
Crystal RocksCrystal Rocks
Crystal Rocks
 
Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009
 
groovy & grails - lecture 2
groovy & grails - lecture 2groovy & grails - lecture 2
groovy & grails - lecture 2
 
03 introduction to graph databases
03   introduction to graph databases03   introduction to graph databases
03 introduction to graph databases
 
Quines—Programming your way back to where you were
Quines—Programming your way back to where you wereQuines—Programming your way back to where you were
Quines—Programming your way back to where you were
 

Kürzlich hochgeladen

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Logical Programming With ruby-prolog

  • 1. ruby-prolog Logical programming with Ruby declarations. 2009.02.09 - Preston Lee <preston.lee@openrain.com> - Http://prestonlee.com Tuesday, February 10, 2009
  • 2. Logical programming paradigm. Declarative. ✤ We define the rules for the world, but not how to process them. Inherently recursive. ✤ The interpreter will automatically recurse to evaluate a query behind the scenes. Turing complete. ✤ You can program without objects or functions, if you so choose. Artificial intelligence. ✤ It’s easier to define and resolve complex logical problems when we think mathematically. Tuesday, February 10, 2009
  • 3. Core Prolog concepts. Rules ✤ Generic semantic definitions (predicates) of how your world works via clauses. ✤ Declarations are formal mathematics (predicate logic) in programming syntax. ✤ Facts ✤ Assertions of truth about the world. ✤ Bob is Charlie’s father. ✤ Bob is Dale’s father. ✤ Queries ✤ Attempts to resolve an unknown logical statement using the rule given the facts. ✤ Tuesday, February 10, 2009
  • 4. Project history. tiny_prolog resolver/unification implementation from teh ✤ internets. Various small additional patches. Refactored to be object-oriented, not mess with ✤ method_missing globally, play nice with garbage collection and support multiple simultaneous contexts. Test cases from scratch, and various snippets ported from ✤ Prolog. Tuesday, February 10, 2009
  • 5. Simple family tree, in Prolog. Rules ✤ We are siblings if we share a parent. ✤ A father is a parent. ✤ sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y). A mother is a parent. ✤ parent_child(X, Y) :- father_child(X, Y). parent_child(X, Y) :- mother_child(X, Y). Facts ✤ mother_child(alice, charlie). Alice is Charlie’s mother. father_child(bob, charlie). ✤ father_child(bob, dale). Bob is Charlie’s father. ✤ /* * Who are Charle's siblings? Bob is Dale’s father. ✤ * sibling(charlie, X). * Queries ✤ * Who are Charlie's parents? * parent_child(X, sally). Who are Alice’s siblings? */ ✤ Tuesday, February 10, 2009
  • 6. Imperative programming support. /******************************************************* * 99 Bottles of Beer * Paul J. DeMarco 9/20/2002 * beer.pro * To execute start gprolog (others may work) * consult('beer.pro'). * drink(beer,99). *******************************************************/ english(beer,0):- write('no more bottle of beer'). english(beer,1):- write('1 bottle of beer'). english(beer,X):- X >= 2, write( X ) , write(' bottles of beer'). drink(beer,X):- X >= 1, english(beer,X), write(' on the wall, '), english(beer,X), write(', take one down, pass it aroundn'), X1 is X - 1, english(beer,X1), write(' on the wall.n'), drink(beer, X1). Tuesday, February 10, 2009
  • 7. ruby-prolog c = RubyProlog::Core.new c.instance_eval do vendor['dell'].fact vendor['apple'].fact model['ultrasharp'].fact model['xps'].fact model['macbook'].fact model['iphone'].fact manufactures['dell', 'ultrasharp'].fact manufactures['dell', 'xps'].fact manufactures['apple', 'macbook'].fact Prolog-like DSL. manufactures['apple', 'iphone'].fact ✤ is_a['xps', 'laptop'].fact is_a['macbook', 'laptop'].fact is_a['ultrasharp', 'monitor'].fact Object-oriented wrapper. ✤ is_a['iphone', 'phone'].fact kind['laptop'] kind['monitor'] Not as complete as Prolog. kind['phone'] ✤ model[:M] <<= [manufactures[:V, :M]] vendor_of[:V, :K] <<= [vendor[:V], manufactures[:V, :M], is_a[:M, :K]] p query(is_a[:K, 'laptop']) p query(vendor_of[:V, 'phone']) end Tuesday, February 10, 2009
  • 8. Complex logical reasoning. The Towers of Hanoi Tuesday, February 10, 2009
  • 9. Two implementations. Prolog ruby-prolog c = RubyProlog::Core.new c.instance_eval do move(1,X,Y,_) :- move[0,:X,:Y,:Z] <<= :CUT write('Move top disk from '), move[:N,:A,:B,:C] <<= [ write(X), is(:M,:N){|n| n - 1}, # reads as quot;M IS N - 1quot; write(' to '), move[:M,:A,:C,:B], write(Y), write_info[:A,:B], nl. move[:M,:C,:B,:A] ] move(N,X,Y,Z) :- write_info[:X,:Y] <<= [ N>1, write[quot;move a disc from the quot;], M is N-1, write[:X], write[quot; pole to the quot;], move(M,X,Z,Y), write[:Y], writenl[quot; pole quot;] move(1,X,Y,_), ] move(M,Z,Y,X). move[3,quot;leftquot;,quot;rightquot;,quot;centerquot;] /* move(3,left,right,center). */ end Tuesday, February 10, 2009
  • 10. ACL Example examples/acls.rb Tuesday, February 10, 2009
  • 11. Ideas for the future. active_prolog - Logical interface to relational ActiveRecord objects. ✤ logical_authentication - Easy custom ACL creation and enforcement. ✤ logical_search - Custom DB search query builder using English-like ✤ predicates. ... ✤ Tuesday, February 10, 2009
  • 12. Thanks! Code: http://github.com/preston/ruby-prolog/tree/master ✤ Releases: http://rubyforge.org/projects/ruby-prolog/ ✤ Tuesday, February 10, 2009

Hinweis der Redaktion

  1. announce: rubyology content and google group.
  2. Two people are siblings if and only if they share a common parent. A widow is a living married female with a deceased spouse. Mary is a female. Mary is married to Nick. Nick is deceased. Who are the widows?
  3. [sibling]. sibling(charlie, X). parent_child(X, sally).
  4. You *can* execute multiple statements in order.