Created
December 11, 2010 23:38
-
-
Save igrigorik/737715 to your computer and use it in GitHub Desktop.
Fun with ruby-prolog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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