Created
December 1, 2010 20:47
-
-
Save nileshtrivedi/724193 to your computer and use it in GitHub Desktop.
syllogisms in ruby (incomplete)
This file contains hidden or 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
class Syllogism | |
attr_accessor :first, :second, :relation, :matrix | |
def initialize(first_term,relation,second_term) | |
@first = first_term | |
@second = second_term | |
@relation = relation | |
@matrix = { @first => :unknown, @second => :unknown, :both => :unknown, :neither => :unknown } | |
@matrix = { @first => :absent, @second => :unknown, :both => :unknown, :neither => :unknown } if @relation == :all_are | |
@matrix = { @first => :unknown, @second => :unknown, :both => :present, :neither => :unknown } if @relation == :some_are | |
@matrix = { @first => :present, @second => :unknown, :both => :unknown, :neither => :unknown } if @relation == :some_are_not | |
@matrix = { @first => :unknown, @second => :unknown, :both => :absent, :neither => :unknown } if @relation == :none_are | |
end | |
def to_s | |
return "All #{@first} are #{@second}." if @relation == :all_are | |
return "Some #{@first} are #{@second}." if @relation == :some_are | |
return "Some #{@first} are not #{@second}." if @relation == :some_are_not | |
return "No #{@first} are #{@second}." if @relation == :none_are | |
return "No relation exists between #{@first} and #{@second}" | |
end | |
def equal?(other) | |
@matrix == other.matrix | |
end | |
def combine(other) | |
#draw a conclusion from self and other | |
big_matrix = {} | |
intersection = [self.first,self.second] & [other.first, other.second] | |
raise "no common term found in these two syllogisms" if intersection.size == 0 | |
raise "only one term should be common in these two syllogisms" if intersection.size > 1 | |
common_term = intersection.first | |
new_first = ([self.first,self.second] - intersection).first | |
new_second = ([other.first,other.second] - intersection).first | |
big_matrix = { new_first => :unknown, new_second => :unknown, :both => :unknown, :neither => :unknown } | |
big_matrix[new_first] = :absent if self.matrix[new_first] == :absent && other.matrix[common_term] == :absent | |
big_matrix[new_second] = :absent if self.matrix[common_term] == :absent && other.matrix[new_second] == :absent | |
big_matrix[:both] = :absent if self.matrix[:both] == :absent && other.matrix[new_second] == :absent | |
big_matrix[:both] = :absent if self.matrix[new_first] == :absent && other.matrix[:both] == :absent | |
big_matrix[:both] = :present if self.matrix[:both] == :present && other.matrix[common_term] == :absent | |
big_matrix[:both] = :present if self.matrix[common_term] == :absent && other.matrix[:both] == :present | |
big_matrix[new_first] = :present if self.matrix[:both] == :present && other.matrix[:both] == :absent | |
big_matrix[new_second] = :present if self.matrix[:both] == :absent && other.matrix[:both] == :present | |
big_matrix[new_first] = :present if self.matrix[new_first] == :present && other.matrix[new_second] == :absent | |
big_matrix[new_second] = :present if self.matrix[new_first] == :absent && other.matrix[new_second] == :present | |
big_matrix[new_first] = :present if self.matrix[common_term] == :absent && other.matrix[:both] == :absent | |
big_matrix[new_second] = :present if self.matrix[:both] == :absent && other.matrix[common_term] == :absent | |
return Syllogism.from_matrix(big_matrix) | |
end | |
def self.from_matrix(matrix) | |
keys = (matrix.keys - [:both, :neither]) | |
first = keys.first | |
second = keys.last | |
relation = :all_are if matrix[first] == :absent | |
relation = :some_are if matrix[:both] == :present | |
relation = :some_are_not if matrix[first] == :present | |
return Syllogism.new(second,:some_are_not,first) if matrix[second] == :present | |
relation = :none_are if matrix[:both] == :absent | |
return Syllogism.new(first,relation,second) | |
end | |
end | |
s1 = Syllogism.new(:animals, :all_are, :mortals) | |
s2 = Syllogism.new(:men, :all_are, :animals) | |
s1.combine(s2).to_s | |
s2.combine(s1).to_s | |
s1 = Syllogism.new(:reptiles, :none_are, :furry) | |
s2 = Syllogism.new(:snakes, :all_are, :reptiles) | |
s1.combine(s2).to_s | |
s2.combine(s1).to_s | |
s1 = Syllogism.new(:kittens, :all_are, :playful) | |
s2 = Syllogism.new(:pets, :some_are, :kittens) | |
s1.combine(s2).to_s | |
s2.combine(s1).to_s | |
s1 = Syllogism.new(:homework, :none_are, :fun) | |
s2 = Syllogism.new(:reading, :some_are, :homework) | |
s1.combine(s2).to_s | |
s2.combine(s1).to_s | |
s1 = Syllogism.new(:healthy_food, :none_are, :fattening_food) | |
s2 = Syllogism.new(:cakes, :all_are, :fattening_food) | |
s1.combine(s2).to_s | |
s2.combine(s1).to_s | |
s1 = Syllogism.new(:horses, :all_are, :animals_with_hooves) | |
s2 = Syllogism.new(:humans, :none_are, :animals_with_hooves) | |
s1.combine(s2).to_s | |
s2.combine(s1).to_s | |
s1 = Syllogism.new(:lazy_students, :none_are, :pass_exams) | |
s2 = Syllogism.new(:students, :some_are, :pass_exams) | |
s1.combine(s2).to_s | |
s2.combine(s1).to_s | |
s1 = Syllogism.new(:informative_things, :all_are, :useful_things) | |
s2 = Syllogism.new(:websites, :some_are_not, :useful_things) | |
s1.combine(s2).to_s | |
s2.combine(s1).to_s | |
#Skipping the Darapati form of syllogism as it uses existential import | |
s1 = Syllogism.new(:mugs, :some_are, :beautiful) | |
s2 = Syllogism.new(:mugs, :all_are, :useful_things) | |
s1.combine(s2).to_s | |
s2.combine(s1).to_s | |
s1 = Syllogism.new(:boys, :all_are, :readheads) | |
s2 = Syllogism.new(:boys, :some_are, :boarders) | |
s1.combine(s2).to_s | |
s2.combine(s1).to_s | |
#skippping felapton | |
s1 = Syllogism.new(:cats, :some_are, :without_tails) | |
s2 = Syllogism.new(:cats, :all_are, :mammals) | |
s1.combine(s2).to_s | |
s2.combine(s1).to_s | |
s1 = Syllogism.new(:trees, :none_are, :edible) | |
s2 = Syllogism.new(:trees, :some_are, :green_things) | |
s1.combine(s2).to_s | |
s2.combine(s1).to_s | |
#skipping bramantip | |
s1 = Syllogism.new(:coloured_flowers, :all_are, :scented_flowers) | |
s2 = Syllogism.new(:scented_flowers, :none_are, :grown_indoors) | |
s1.combine(s2).to_s | |
s2.combine(s1).to_s | |
s1 = Syllogism.new(:small_birds, :some_are, :birds_that_live_on_honey) | |
s2 = Syllogism.new(:birds_that_live_on_honey, :all_are, :colourful) | |
s1.combine(s2).to_s | |
s2.combine(s1).to_s | |
#skipping fesapo | |
s1 = Syllogism.new(:competents, :none_are, :messy_people) | |
s2 = Syllogism.new(:messy_people, :all_are, :working_here) | |
s1.combine(s2).to_s | |
s2.combine(s1).to_s | |
class Syllogism | |
attr_accessor :kmatrix | |
def initialize(kmatrix) | |
@kmatrix = kmatrix #knowledge matrix of presence/absence of elements in each of the 4 regions | |
end | |
def to_s | |
#there can be multiple string representations of a syllogism. Should we prefer one over other? | |
end | |
def stype | |
end | |
def terms | |
(@kmatrix.keys - [:both, :neither]).to_set | |
end | |
def +(other) | |
#combine this syllogism with "other" | |
middle_term = (other.terms & self.terms).first | |
self_term = (self.terms.to_a - [middle_term]).first | |
other_term = (other.terms.to_a - [middle_term]).first | |
end | |
end | |
class Hash | |
def to_syllogism | |
Syllogism.new(self) | |
end | |
end | |
class String | |
def to_syllogism | |
#Need to parse the string to figure out the terms and relation and create appropriate Syllogism instance | |
end | |
end | |
syllogism_kmatrix = {:a => :present, :b => :unknown, :both => :unknown, :neither => :unknown} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
only a only b a and b neither a nor b
unknown unknown unknown unknown
present unknown unknown unknown some a are notb some notb are a
unknown present unknown unknown some b are nota some nota are b
unknown unknown present unknown some a are b some b are a
unknown unknown unknown present some nota are notb some notb are nota
absent unknown unknown unknown no a is notb all a are b no notb is a all notb are nota
unknown absent unknown unknown no b is nota all b are a no nota is b all nota are notb
unknown unknown absent unknown no a is b no b is a all a are notb all b are nota
unknown unknown unknown absent no nota is notb no notb is nota all nota are b all notb are a