Skip to content

Instantly share code, notes, and snippets.

@igrigorik
Created December 11, 2010 23:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igrigorik/737715 to your computer and use it in GitHub Desktop.
Save igrigorik/737715 to your computer and use it in GitHub Desktop.
Fun with ruby-prolog
# gem install ruby-prolog
# https://github.com/preston/ruby-prolog/
# Prolog code:
#
# mother_child(trude, sally).
#
# father_child(tom, sally).
# father_child(tom, erica).
# father_child(mike, tom).
#
# sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).
#
# parent_child(X, Y) :- father_child(X, Y).
# parent_child(X, Y) :- mother_child(X, Y).
#
# sibling(sally, erica).
# => true
require 'ruby-prolog'
c = RubyProlog::Core.new
c.instance_eval do
father_child['mike', 'tom'].fact
father_child['tom', 'sally'].fact
father_child['tom', 'erica'].fact
mother_child['trude', 'sally'].fact
parent_child[:X, :Y] <<= [father_child[:X, :Y]]
parent_child[:X, :Y] <<= [mother_child[:X, :Y]]
sibling[:X, :Y] <<= [parent_child[:Z, :X], parent_child[:Z, :Y]]
p query(sibling['sally', 'erica']) # => [[sibling["sally", "erica"]]]
p query(sibling['trude', 'erica']) # => []
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment