class Syllogism

Constants

VERSION

Attributes

errors[RW]
statements[R]

Public Class Methods

[](*raw_statements) click to toggle source
# File lib/syllogism.rb, line 20
def self.[](*raw_statements)
  parse(*raw_statements)
end
new(statements) click to toggle source
# File lib/syllogism.rb, line 28
def initialize(statements)
  @errors = []
  @statements = statements
end
parse(*raw_statements) click to toggle source
# File lib/syllogism.rb, line 24
def self.parse(*raw_statements)
  new(raw_statements.map { |raw_statement| Statement.parse(raw_statement) })
end

Public Instance Methods

==(other) click to toggle source
# File lib/syllogism.rb, line 33
def ==(other)
  premises.map(&:formula).sort == other.premises.map(&:formula).sort &&
    conclusion.formula == other.conclusion.formula
end
conclusion() click to toggle source
# File lib/syllogism.rb, line 42
def conclusion
  @conclusion ||= statements.last
end
premises() click to toggle source
# File lib/syllogism.rb, line 38
def premises
  @premises ||= statements.take(statements.length - 1)
end
valid?() click to toggle source
# File lib/syllogism.rb, line 46
def valid?
  statements_are_well_formed? &&
    meets_definition_of_syllogism? &&
    distribute_terms &&
    star_premises &&
    star_conclusion &&
    passes_star_test?
end

Private Instance Methods

contains_statements?() click to toggle source
# File lib/syllogism.rb, line 59
def contains_statements?
  if statements.any?
    true
  else
    errors.push("By definition, a syllogism must contain at least one statement")
    false
  end
end
distribute_terms() click to toggle source
# File lib/syllogism.rb, line 68
def distribute_terms
  statements.each(&:distribute) && true
end
every_general_term_is_starred_exactly_once() click to toggle source
# File lib/syllogism.rb, line 84
def every_general_term_is_starred_exactly_once
  statements.each_with_object(Hash.new(0)) do |statement, stars|
    statement.general_terms.each do |general_term|
      stars[general_term.value] += 1 if general_term.starred?
    end
  end.values.all? { |star_count| star_count == 1 }
end
exactly_two_of_each_term?() click to toggle source
# File lib/syllogism.rb, line 72
def exactly_two_of_each_term?
  if term_histogram.values.all? { |count| count == 2 }
    true
  else
    term_histogram.reject { |_term, count| count == 2 }.each do |term, count|
      errors.push("The term '#{term}' occured #{count} time(s), but should occur exactly twice")
    end

    false
  end
end
forms_a_chain?() click to toggle source
# File lib/syllogism.rb, line 92
def forms_a_chain?
  (0...statements.count - 1).map do |index|
    current_statement = statements[index]
    next_statement = statements[index + 1]
    current_terms = current_statement.terms.map(&:value)
    next_terms = next_statement.terms.map(&:value)

    if (current_terms - next_terms).length == 1
      true
    else
      errors.push("'#{current_statement}'' should share exactly one term with '#{next_statement}'")
      false
    end
  end.all?
end
meets_definition_of_syllogism?() click to toggle source
# File lib/syllogism.rb, line 108
def meets_definition_of_syllogism?
  contains_statements? && exactly_two_of_each_term? && forms_a_chain?
end
passes_star_test?() click to toggle source

See Harry Gensler's paper, _A simplified decision procedure for categorical syllogisms._ projecteuclid.org/journals/notre-dame-journal-of-formal-logic/volume-14/issue-4/A-simplified-decision-procedure-for-categorical-syllogisms/10.1305/ndjfl/1093891100.full

# File lib/syllogism.rb, line 114
def passes_star_test?
  every_general_term_is_starred_exactly_once &&
    there_is_exactly_one_star_on_the_right_hand_side
end
star_conclusion() click to toggle source
# File lib/syllogism.rb, line 127
def star_conclusion
  conclusion.terms.each do |term|
    term.starred = !term.distributed?
  end
end
star_premises() click to toggle source
# File lib/syllogism.rb, line 119
def star_premises
  premises.each do |premise|
    premise.terms.each do |term|
      term.starred = term.distributed?
    end
  end
end
statements_are_well_formed?() click to toggle source
# File lib/syllogism.rb, line 133
def statements_are_well_formed?
  statements.map do |statement|
    if statement.wff?
      true
    else
      statement.errors.each { |error| errors.push(error) }
      false
    end
  end.all?
end
term_histogram() click to toggle source
# File lib/syllogism.rb, line 144
def term_histogram
  statements.flat_map(&:terms).map(&:value).each_with_object(Hash.new(0)) do |value, hash|
    hash[value] += 1
  end
end
there_is_exactly_one_star_on_the_right_hand_side() click to toggle source
# File lib/syllogism.rb, line 150
def there_is_exactly_one_star_on_the_right_hand_side
  statements.map(&:predicate).count(&:starred?) == 1
end