Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.95 KB | None | 0 0
  1. def pull_out_quantifications_across_binary_operator(formula: Formula) -> \
  2.         Tuple[Formula, Proof]:
  3.     """Converts the given formula with uniquely named variables of the form
  4.    ``'(``\ `Q1`\ `x1`\ ``[``\ `Q2`\ `x2`\ ``[``...\ `Qn`\ `xn`\ ``[``\ `inner_first`\ ``]``...\ ``]]``\ `*`\ `P1`\ `y1`\ ``[``\ `P2`\ `y2`\ ``[``...\ `Pm`\ `ym`\ ``[``\ `inner_second`\ ``]``...\ ``]])'``
  5.    to an equivalent formula of the form
  6.    ``'``\ `Q'1`\ `x1`\ ``[``\ `Q'2`\ `x2`\ ``[``...\ `Q'n`\ `xn`\ ``[``\ `P'1`\ `y1`\ ``[``\ `P'2`\ `y2`\ ``[``...\ `P'm`\ `ym`\ ``[(``\ `inner_first`\ `*`\ `inner_second`\ ``)]``...\ ``]]]``...\ ``]]'``
  7.    and proves the equivalence of these two formulas.
  8.    Parameters:
  9.        formula: formula with uniquely named variables to convert, whose root
  10.            is a binary operator, i.e., which is of the form
  11.            ``'(``\ `Q1`\ `x1`\ ``[``\ `Q2`\ `x2`\ ``[``...\ `Qn`\ `xn`\ ``[``\ `inner_first`\ ``]``...\ ``]]``\ `*`\ `P1`\ `y1`\ ``[``\ `P2`\ `y2`\ ``[``...\ `Pm`\ `ym`\ ``[``\ `inner_second`\ ``]``...\ ``]])'``
  12.            where `*` is a binary operator, `n`>=0, `m`>=0, each `Qi` and `Pi`
  13.            is a quantifier, each `xi` and `yi` is a variable name, and neither
  14.            `inner_first` nor `inner_second` starts with a quantifier.
  15.    Returns:
  16.        A pair. The first element of the pair is a formula equivalent to the
  17.        given formula, but of the form
  18.        ``'``\ `Q'1`\ `x1`\ ``[``\ `Q'2`\ `x2`\ ``[``...\ `Q'n`\ `xn`\ ``[``\ `P'1`\ `y1`\ ``[``\ `P'2`\ `y2`\ ``[``...\ `P'm`\ `ym`\ ``[(``\ `inner_first`\ `*`\ `inner_second`\ ``)]``...\ ``]]]``...\ ``]]'``
  19.        where each `Q'i` and `P'i` is a quantifier, and where the operator `*`,
  20.        the `xi` and `yi` variable names, `inner_first`, and `inner_second` are
  21.        the same as in the given formula. The second element of the pair is a
  22.        proof of the equivalence of the given formula and the returned formula
  23.        (i.e., a proof of
  24.        `equivalence_of`\ ``(``\ `formula`\ ``,``\ `returned_formula`\ ``)``)
  25.        via `~predicates.prover.Prover.AXIOMS` and
  26.        `ADDITIONAL_QUANTIFICATION_AXIOMS`.
  27.    Examples:
  28.        >>> formula = Formula.parse('(Ax[Ey[R(x,y)]]->Ez[P(1,z)])')
  29.        >>> returned_formula, proof = pull_out_quantifications_across_binary_operator(
  30.        ...     formula)
  31.        >>> returned_formula
  32.        Ex[Ay[Ez[(R(x,y)->P(1,z))]]]
  33.        >>> proof.is_valid()
  34.        True
  35.        >>> proof.conclusion == equivalence_of(formula, returned_formula)
  36.        True
  37.        >>> proof.assumptions == Prover.AXIOMS.union(
  38.        ...     ADDITIONAL_QUANTIFICATION_AXIOMS)
  39.        True
  40.    """
  41.     assert has_uniquely_named_variables(formula)
  42.     assert is_binary(formula.root)
  43.     prover = Prover(ADDITIONAL_QUANTIFICATION_AXIOMS)
  44.     f1, p1 = pull_out_quantifications_from_left_across_binary_operator(formula)
  45.     curr = f1
  46.     lst_of_tuples = []
  47.     while is_quantifier(curr.root):
  48.         lst_of_tuples.append((curr.root, curr.variable))
  49.         curr = curr.predicate
  50.     f2, p2 = pull_out_quantifications_from_right_across_binary_operator(curr)
  51.     step1 = prover.add_proof(p1.conclusion, p1)
  52.     step2 = prover.add_proof(p2.conclusion, p2)
  53.     dct = {'A': ADDITIONAL_QUANTIFICATION_AXIOMS[14], 'E': ADDITIONAL_QUANTIFICATION_AXIOMS[15]}
  54.     inst = p2.conclusion
  55.     for quantifier, variable in reversed(lst_of_tuples):
  56.         map1 = {'R': inst.first.first, 'Q': inst.first.second, 'x': variable, 'y': variable}
  57.         axiom = dct[quantifier]
  58.         inst = axiom.instantiate(map1)
  59.         step3 = prover.add_instantiated_assumption(inst, axiom, map1)
  60.         inst = inst.second
  61.         step2 = prover.add_mp(inst, step2, step3)
  62.     prover.add_tautological_implication(equivalence_of(p1.conclusion.second.second,
  63.                                                        inst.first.second),
  64.                                         {step1, step2})
  65.     f = inst.first.second
  66.     return f, prover.qed()
  67. # Task 11.8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement