jweinst1
9/15/2015 - 7:54 PM

propositional logic, ruby

propositional logic, ruby

def checkcontainer(elem, container)
    for thing in container
        if thing == elem
            return true
        end
    end
    return false
end



class Proposition
    def initialize(statement)
        @statement = statement
        @prop = eval statement
    end
    def bool
        return @prop
    end
    def statement
        return @statement
    end
end

class Conjunction
    def initialize(*statements)
        @statelist = statements.collect{|x| x}
        @prop = true
        for elem in @statelist
            if (eval elem) == false
                @prop = false
            end
        end
    end
    def bool
        return @prop
    end
    def statement
        return @statelist
    end
end

class Disjunction
    def initialize(*statements)
        @statelist = statements.collect{|x| x}
        @prop = false
        for elem in @statelist
            if (eval elem)
                @prop = true
            end
        end
    end
    def bool
        return @prop
    end
    def statement
        return @statelist
    end
end

class Negation
    def initialize(statement)
        @statement = statement
        if (eval statement)
            @prop = false
        else
            @prop = true
        end
    end
    def bool
        return @prop
    end
    def statement
        return @statement
    end
end

class Implication
    def initialize(s1, s2)
        @s1 = s1
        @s2 = s2
        @statement = "If " + s1 + " then" + s2 + "."
        if (eval s2)
            @prop = true
        else
            @prop = false
        end
    end
    def bool
        return @prop
    end
    def statement
        return @statement
    end
    def checkconverse
        if (eval @s1)
            return true
        else
            return false
        end
    end
end

#Quantifiers

class Condition
    #must use x as a variable
    def initialize(term)
        @term = term
        @condition = lambda do |x|
            if (eval @term)
                return true
            else
                return false
            end
        end
    end
    def check(elem)
        return @condition.call(elem)
    end
end

class Condition2
    #must use x and y as variables, uses two variables
    def initialize(term)
        @term = term
        @condition = lambda do |x, y|
            if (eval @term)
                return true
            else
                return false
            end
        end
    end
    def check(elem1, elem2)
        return @condition.call(elem1, elem2)
    end
end
#universal quantifier
class Univ_quant
    def initialize(condition, container)
        @condition = condition
        @container = container
        @prop = true
        for elem in container
            if @condition.check(elem) == false
                @prop = false
            end
        end
    end
    def bool
        return @prop
    end
end
#existiential quantifier
class Exist_quant
    def initialize(condition, container)
        @condition = condition
        @container = container
        @prop = false
        for elem in container
            if @condition.check(elem)
                @prop = true
            end
        end
    end
    def bool
        return @prop
    end
end

def quant_sorter(condition, container, option=nil)
    false_bin = []
    true_bin = []
    for elem in container
        if condition.check(elem)
            true_bin << elem
        else
            false_bin << elem
        end
    end
    if option == 't'
        return true_bin
    else
        return false_bin
    end
end

def check_implication(prop1, prop2)
    if prop2.bool
        return true
    else
        return false
    end
end
#Equation statements

#summation

class Summation
    def initialize(term)
        @term = term
        @sum = lambda do |i|
            n = 0
            total = 0
            until n == i do
                total += (eval @term)
                n += 1
            end
            return total
        end
    end
    def sum(num)
        return @sum.call(num)
    end
end