def from_propositional_skeleton(skeleton: PropositionalFormula, substitution_map: Mapping[str, Formula]) -> \ Formula: """Computes a first-order formula from a propositional skeleton and a substitution map. Arguments: skeleton: propositional skeleton for the formula to compute. substitution_map: a map from each atomic propositional subformula of the given skeleton to a first-order formula. Returns: A first-order formula obtained from the given propositional skeleton by substituting each atomic propositional subformula with the formula mapped to it by the given map. """ for key in substitution_map: assert is_propositional_variable(key) # Task 9.10 if is_propositional_variable(skeleton.root): if skeleton.root not in substitution_map: assert (False) return substitution_map[skeleton.root] if is_unary(skeleton.root): return Formula( skeleton.root, Formula.from_propositional_skeleton(skeleton.first, substitution_map)) if is_binary(skeleton.root): return Formula( skeleton.root, Formula.from_propositional_skeleton(skeleton.first, substitution_map), Formula.from_propositional_skeleton(skeleton.second, substitution_map))
def from_propositional_skeleton(skeleton: PropositionalFormula, substitution_map: Mapping[str, Formula]) -> \ Formula: """Computes a first-order formula from a propositional skeleton and a substitution map. Arguments: skeleton: propositional skeleton for the formula to compute. substitution_map: a map from each atomic propositional subformula of the given skeleton to a first-order formula. Returns: A first-order formula obtained from the given propositional skeleton by substituting each atomic propositional subformula with the formula mapped to it by the given map. """ for key in substitution_map: assert is_propositional_variable(key) # Task 9.10 # recursively go down the formula and return the predicate formula # base case, we reach a term if is_propositional_variable(skeleton.root): if skeleton.root in substitution_map.keys(): return substitution_map[skeleton.root] # recursive unary case if is_unary(skeleton.root): return Formula(skeleton.root, Formula.from_propositional_skeleton(skeleton.first, substitution_map)) # recursive binary case if is_binary(skeleton.root): return Formula(skeleton.root, Formula.from_propositional_skeleton(skeleton.first, substitution_map), Formula.from_propositional_skeleton(skeleton.second, substitution_map))
def from_propositional_skeleton(skeleton, substitution_map): """ Return a first-order predicate logic formula obtained from the given propositional skeleton by substituting each variable with the formula mapped to it by substitution_map """ assert type(skeleton) is PropositionalFormula assert type(substitution_map) is dict for key in substitution_map: assert is_propositional_variable(key) and \ type(substitution_map[key]) is Formula
def from_propositional_skeleton(skeleton: PropositionalFormula, substitution_map: Mapping[str, Formula]) -> \ Formula: """Computes a first-order formula from a propositional skeleton and a substitution map. Arguments: skeleton: propositional skeleton for the formula to compute. substitution_map: a map from each atomic propositional subformula of the given skeleton to a first-order formula. Returns: A first-order formula obtained from the given propositional skeleton by substituting each atomic propositional subformula with the formula mapped to it by the given map. """ for key in substitution_map: assert is_propositional_variable(key)
def from_propositional_skeleton(skeleton: PropositionalFormula, substitution_map: Mapping[str, Formula]) -> \ Formula: """Computes a predicate-logic formula from a propositional skeleton and a substitution map. Arguments: skeleton: propositional skeleton for the formula to compute, containing no constants or operators beyond ``'~'``, ``'->'``, ``'|'``, and ``'&'``. substitution_map: mapping from each atomic propositional subformula of the given skeleton to a predicate-logic formula. Returns: A predicate-logic formula obtained from the given propositional skeleton by substituting each atomic propositional subformula with the formula mapped to it by the given map. Examples: >>> Formula.from_propositional_skeleton( ... PropositionalFormula.parse('((z1&z2)|(z2->~z3))'), ... {'z1': Formula.parse('Ax[x=7]'), 'z2': Formula.parse('x=7'), ... 'z3': Formula.parse('Q(y)')}) ((Ax[x=7]&x=7)|(x=7->~Q(y))) """ for operator in skeleton.operators(): assert is_unary(operator) or is_binary(operator) for variable in skeleton.variables(): assert variable in substitution_map if is_propositional_variable(skeleton.root): return substitution_map[skeleton.root] formula = None if is_binary(skeleton.root): formula1 = Formula.from_propositional_skeleton(skeleton.first, substitution_map) formula2 = Formula.from_propositional_skeleton(skeleton.second, substitution_map) formula = Formula(skeleton.root, formula1, formula2) if is_unary(skeleton.root): formula1 = Formula.from_propositional_skeleton(skeleton.first, substitution_map) formula = Formula(skeleton.root, formula1) return formula
def from_propositional_skeleton(skeleton: PropositionalFormula, substitution_map: Mapping[str, Formula]) -> \ Formula: """Computes a first-order formula from a propositional skeleton and a substitution map. Arguments: skeleton: propositional skeleton for the formula to compute. substitution_map: a map from each atomic propositional subformula of the given skeleton to a first-order formula. Returns: A first-order formula obtained from the given propositional skeleton by substituting each atomic propositional subformula with the formula mapped to it by the given map. """ for key in substitution_map: assert is_propositional_variable(key) node_type = skeleton.getType() if node_type is PropositionalFormula.NodeType.T_UNARY_OP: # Parse the lhs lhs = Formula.from_propositional_skeleton(skeleton.first, substitution_map) return Formula(skeleton.root, lhs) elif node_type is PropositionalFormula.NodeType.T_BINARY_OP: # Parse the lhs and rhs lhs = Formula.from_propositional_skeleton(skeleton.first, substitution_map) rhs = Formula.from_propositional_skeleton(skeleton.second, substitution_map) return Formula(skeleton.root, lhs, rhs) elif node_type is PropositionalFormula.NodeType.T_VAR: # Return the substituted variable, if we got one if skeleton.root in substitution_map: return substitution_map[skeleton.root] # Use the original value return skeleton