def variable_declaration(self) -> List[VarDeclaration]: """Assembles a list of all declaration of variables by TYPE_SPEC. Grammar: <variable> ::= ID (COMMA ID)* COLON <type_spec> Returns: List[VarDeclaration]: List with all declaration of variables by type """ var_nodes = [Var(self.current_token)] self.consume_token(TokenType.ID) while self.current_token.type == TokenType.COMMA: self.consume_token(TokenType.COMMA) var_nodes.append(Var(self.current_token)) self.consume_token(TokenType.ID) self.consume_token(TokenType.COLON) type_node = self.type_spec() var_declarations = [ VarDeclaration(var_node, type_node) for var_node in var_nodes ] return var_declarations
def forall_comm(): return forallI( assume(Var("a")), forallI( assume(Var("b")), forallE( forallE(premise(parse("FA x. FA y. P(x, y)")), "b", parse("FA y. P(b, y)")), "a", parse("P(b, a)")), parse("FA x. P(x, a)")), parse("FA y. FA x. P(x, y)"))
def orComm(): A = Var("a") B = Var("b") p1 = premise(Or(A, B)) return orE( p1, arrowI(assume(A), orIR(assumed(A), Or(B, A)), Arrow(A, Or(B, A))), arrowI(assume(B), orIL(assumed(B), Or(B, A)), Arrow(B, Or(B, A))), Or(B, A))
def disSyl(): A = Var("a") B = Var("b") p1 = premise(Or(A, B)) p2 = premise(Not(B)) return orE( p1, arrowI(assume(A), assumed(A), Arrow(A, A)), arrowI(assume(B), FE(notE(assumed(B), p2, false()), A), Arrow(B, A)), A)
def contra(): return arrowI( assume(parse("EX x. P(x)")), arrowE( existsE( assumed(parse("EX x. P(x)")), "a", arrowI(assume(parse("P(a)")), assumed(parse("P(a)")), Arrow(parse("P(a)"), parse("P(a)"))), parse("P(a)")), forallE(premise(parse("FA x. (P(x)->Q)")), "a", parse("P(a)->Q")), Var("Q")), Arrow(parse("EX x. P(x)"), Var("Q")))
def DL2(p1): a = Var("a") b = Var("b") return doubleNeg( notI( arrowI( assume(Not(b)), notE(orIR(assumed(Not(b)), Or(Not(a), Not(b))), p1, false()), Arrow(Not(b), false())), Not(Not(b))), b)
def DM2(): a = Var("a") b = Var("b") prem = premise(Not(And(a, b))) #The original premise, ~(A && B) d1 = premise( Not(Or(Not(a), Not(b))) ) #d1 is the phrase required to run DL1 and DL2. I ran it as a premise, but it is assumed, NOT a premise return doubleNeg( notI( arrowI(assume(Not(Or(Not(a), Not(b)))), notE(andI(DL1(d1), DL2(d1), And(a, b)), prem, false()), Arrow(Not(Or(Not(a), Not(b))), false())), Not(Not(Or(Not(a), Not(b))))), Or(Not(a), Not(b)))
def andComm(): clear() a = Var("A") b = Var("B") p1 = premise(And(a, b)) # The proof itself looks different, but it's really just on it's side. # # each proof rule is written as rule(support, expression) # since the andI rule B && A needs two pieces (B and A), we need the two proofs # the proof for B is andER, and the proof of A is andEL return andI(andER(p1, b), \ andEL(p1, a), \ And(b,a))
def forall_comm(): return forallI( assume(Var("c")), forallI( assume(Var("d")), forallE( forallE( premise(parse("FA x. FA y. P(x, y)")), "d", parse("FA y. P(d, y)"), ), "c", parse("P(d, c)"), ), parse("FA x. P(x, c)")), parse("FA y. FA x. P(x, y)"))
def ren_forall(): return forallI(assume(Var("x")), forallE( premise(parse("FA x. P(x)")), "x", parse("P(x)"), ), parse("FA z. P(z)"))
def pred(tokens): follow = [TType.TEOF, TType.TRPAREN, TType.TARROW, TType.TOR, TType.TAND] e = None # initial name name = tokens.pop(0).val # P(v {, v} ) if tokens[0].ttype == TType.TLPAREN: tokens.pop(0) vs = [] if tokens[0].ttype == TType.TVAR: vs = [tokens.pop(0).val] # P() elif tokens[0].ttype == TType.TRPAREN: pass else: raise ParseException(tokens[0].pos, [TType.TVAR], tokens[0].val) # {, v} while tokens[0].ttype != TType.TRPAREN: if tokens[0].ttype == TType.TCOMMA and \ tokens[1].ttype == TType.TVAR: tokens.pop(0) vs.append(tokens.pop(0).val) else: raise ParseException(tokens[0].pos, [TType.TCOMMA], tokens[0].val) tokens.pop(0) e = Pred(name, vs) # v else: e = Var(name) if tokens[0].ttype not in follow: raise ParseException(tokens[0].pos, follow, tokens[0].val) return e
def term(tokens): first = [TType.TTRUE, TType.TFALSE, TType.TVAR, TType.TLPAREN] follow = [TType.TEOF, TType.TRPAREN, TType.TARROW, TType.TOR, TType.TAND] e = None if tokens[0].ttype == TType.TVAR: e = Var(tokens[0].val) tokens.pop(0) elif tokens[0].ttype == TType.TTRUE: e = true() tokens.pop(0) elif tokens[0].ttype == TType.TFALSE: e = false() tokens.pop(0) elif tokens[0].ttype == TType.TLPAREN: tokens.pop(0) e = expr(tokens) if tokens[0].ttype != TType.TRPAREN: raise ParseException(tokens[0].pos, [TType.TRPAREN], tokens[0].val) tokens.pop(0) else: raise ParseException(tokens[0].pos, first, tokens[0].val) if tokens[0].ttype not in follow: raise ParseException(tokens[0].pos, follow, tokens[0].val) return e
def arrTrans(): a = Var("A") b = Var("B") c = Var("C") p1 = premise(Arrow(a, b)) p2 = premise(Arrow(b, c)) a1 = assume(a) return arrowI(a1, \ arrowE(arrowE(assumed(a), \ p1, \ b), \ p2, \ c), \ Arrow(a,c))
def DM3(): return forallI( assume(Var("a")), notI( arrowI( assume(parse("P(a)")), notE(existsI(assumed(parse("P(a)")), "a", parse("EX x. P(x)")), premise(Not(parse("EX x. P(x)"))), false()), Arrow(parse("P(a)"), false())), Not(parse("P(a)"))), parse("FA x. ~P(x)"))
def main(): try: clear() orComm().print_proof() clear() disSyl().print_proof() clear() DM1().print_proof() clear() a = Var("a") b = Var("b") dlPrem = premise(Not(Or(Not(a), Not(b)))) DL1(dlPrem).print_proof() clear() dlPrem = premise(Not(Or(Not(a), Not(b)))) DL2(dlPrem).print_proof() clear() DM2().print_proof() except (ProofException) as e: e.print()
def variable(self) -> Var: """A token labeled with type ID (identifies uniqueness) representing the variables and the program name (things that must be unique). Grammar: <variable> ::= ID. Returns: Var: a token object containing the type and value """ node = Var(self.current_token) self.consume_token(TokenType.ID) return node
def example(): clear() p1 = premise(parse("EX x. FA y. P(x,y)")) a1 = assume(parse("FA y. P(u,y)")) a2 = assume(Var("v")) l1 = assumed(parse("FA y. P(u,y)")) l2 = forallE(l1, "v", parse("P(u,v)")) l3 = existsI(l2, "u", parse("EX x. P(x,v)")) l4 = forallI(a2, l3, parse("FA y. EX x. P(x,y)")) l5 = arrowI(a1, l4, parse("(FA y. P(u,y)) -> (FA y. EX x. P(x,y))")) l6 = existsE(p1, "u", l5, parse("FA y. EX x. P(x,y)")) return l6
def DM1(): A = Var("a") B = Var("b") p1 = premise(Or(Not(A), Not(B))) end = Not(And(A, B)) A_B = And(A, B) #I condensed parts of the proofs so I could substitute in things that made sense to me, otherwise I was going nuts return orE( p1, arrowI( assume(Not(A)), notI( arrowI(assume(A_B), notE(andEL(assumed(A_B), A), assumed(Not(A)), false()), Arrow(A_B, false())), Not(A_B)), Arrow(Not(A), Not(A_B))), arrowI( assume(Not(B)), notI( arrowI(assume(A_B), notE(andER(assumed(A_B), B), assumed(Not(B)), false()), Arrow(A_B, false())), Not(A_B)), Arrow(Not(B), Not(A_B))), end)
def variable(self): node = Var(self.current_token) self.eat(ID) return node