public formula make_SSF() { if (this.is_PNF) { formula ssf = new formula(); if (this.top_operation is quantifier) { int all_quantors = 0; int existance_quantors = -1; ArrayList all_variables = new ArrayList(); ArrayList quantifiers = new ArrayList(); while (this.is_quantum_expression && top_operation is quantifier) { quantifier K = new quantifier((quantifier)this.top_operation); if (K.is_universality) { all_quantors++; quantifiers.Add(K); this.logical_exp.RemoveAt(0); all_variables.Add(K.variable); } else // if (K.is_existance) { existance_quantors++; if (all_quantors == 0) { this.logical_exp.RemoveAt(0); this.rename_variable(K.variable, "const"); } else { this.logical_exp.RemoveAt(0); function scolem = new function("s" + existance_quantors, all_variables); ssf = this.change_variable(K.variable, scolem); } } } ssf.logical_exp.InsertRange(0, quantifiers); } else { return(ssf); } return(ssf); } else { MessageBox.Show("Для сколемизации формула должна быть в ПНФ!", "Ошибка"); return(this); } }
//взять связанную переменную из квантора public string get_bounded_variable(operation this_operation) { string var = ""; if (this_operation is quantifier) { quantifier K = new quantifier((quantifier)this_operation); var = K.variable; } else { throw new Exception("Нельзя взять связанную переменную, так как операция - не квантор"); } return(var); }
public formula get_basic() { if (this.is_basic) { return(this); } formula func = new formula(); //если вдруг импликация if (top_operation is imp) { func = (~(first_operand.get_basic())).get_basic() | second_operand.get_basic(); } else { if (top_operation.dimension == 1) { func = first_operand.get_basic(); func.logical_exp.Insert(0, top_operation); } else { if (top_operation is dis) { func = first_operand.get_basic() | second_operand.get_basic(); } else { func = first_operand.get_basic() & second_operand.get_basic(); } } } //отрицание перед скобками if (top_operation is neg) { formula negation_trick = new formula(first_operand.logical_exp); if (negation_trick.top_operation is neg) { negation_trick.logical_exp.RemoveAt(0); //убираем второе отрицание } if (negation_trick.top_operation is quantifier) { quantifier K = new quantifier((quantifier)negation_trick.top_operation); K.change_quant(); negation_trick.logical_exp.RemoveAt(0); negation_trick = (~negation_trick).get_basic(); negation_trick.logical_exp.Insert(0, K); } //применяем правила ДеМоргана: if (negation_trick.top_operation is kon) { negation_trick = (~(negation_trick.first_operand.get_basic())).get_basic() | (~(negation_trick.second_operand.get_basic())).get_basic(); } else if (negation_trick.top_operation is dis) { negation_trick = (~(negation_trick.first_operand.get_basic())).get_basic() & (~(negation_trick.second_operand.get_basic())).get_basic(); } return(negation_trick); } return(func); }
public formula make_PNF() { if (this.is_PNF) { return(this); } formula pnf = new formula(); if (top_operation is quantifier) { quantifier K1 = new quantifier((quantifier)top_operation); //bounded_variables.Add(K1.variable); pnf = first_operand.make_PNF(); pnf.logical_exp.Insert(0, top_operation); return(pnf); } else { //1 правило вноса атомов под квантор if (top_operation is dis) { if (first_operand.is_free_from_quntifiers && second_operand.is_free_from_quntifiers) //два атома => итак уже пнф { return(first_operand | second_operand); } else if (first_operand.is_free_from_quntifiers && second_operand.is_quantum_expression) //атом и кванторное выражение => записываем атом под квантор { pnf = first_operand | second_operand.first_operand.make_PNF(); pnf.logical_exp.Insert(0, second_operand.top_operation); pnf = pnf.make_PNF(); } else if (!first_operand.is_free_from_quntifiers && first_operand.top_operation is quantifier) //два кванторных выражения { string variable = get_bounded_variable(first_operand.top_operation); if (second_operand.is_free_from(variable)) { pnf = first_operand.first_operand.make_PNF() | second_operand.make_PNF(); pnf.logical_exp.Insert(0, first_operand.top_operation); pnf = pnf.make_PNF(); } } else if (!second_operand.is_free_from_quntifiers && second_operand.top_operation is quantifier) { string variable = get_bounded_variable(second_operand.top_operation); if (first_operand.is_free_from(variable)) { pnf = first_operand.make_PNF() | second_operand.first_operand.make_PNF(); pnf.logical_exp.Insert(0, second_operand.top_operation); pnf = pnf.make_PNF(); } } } else if (top_operation is kon) { if (first_operand.is_free_from_quntifiers && second_operand.is_free_from_quntifiers) //два атома => итак уже пнф { return(first_operand & second_operand); } else if (first_operand.is_free_from_quntifiers && second_operand.is_quantum_expression) //атом и кванторное выражение => записываем атом под квантор { pnf = first_operand & second_operand.first_operand.make_PNF(); pnf.logical_exp.Insert(0, second_operand.top_operation); pnf = pnf.make_PNF(); } else if (second_operand.is_free_from_quntifiers && first_operand.is_quantum_expression) //кванторное выражение и атом { pnf = first_operand.first_operand.make_PNF() & second_operand; pnf.logical_exp.Insert(0, first_operand.top_operation); pnf = pnf.make_PNF(); } if (first_operand.is_quantum_expression && first_operand.top_operation is quantifier) { string variable = get_bounded_variable(first_operand.top_operation); bounded_variables.Add(variable); if (second_operand.is_free_from(variable)) { pnf = first_operand.first_operand.make_PNF() & second_operand.make_PNF(); pnf.logical_exp.Insert(0, first_operand.top_operation); pnf = pnf.make_PNF(); } } else if (second_operand.is_quantum_expression && second_operand.top_operation is quantifier) { string variable = get_bounded_variable(second_operand.top_operation); //bounded_variables.Add(variable); if (first_operand.is_free_from(variable)) { pnf = first_operand.make_PNF() & second_operand.first_operand.make_PNF(); pnf.logical_exp.Insert(0, second_operand.top_operation); pnf = pnf.make_PNF(); } } } //2 - правила объединения кванторов //2.1 - всеобщность if (top_operation is kon) { if (first_operand.is_quantum_expression && second_operand.is_quantum_expression) { if (first_operand.top_operation is quantifier && second_operand.top_operation is quantifier) { quantifier K1 = new quantifier((quantifier)first_operand.top_operation); //bounded_variables.Add(K1.variable); quantifier K2 = new quantifier((quantifier)second_operand.top_operation); if (K1.is_universality && K2.is_universality && K1.variable == K2.variable) { pnf = first_operand.first_operand & second_operand.first_operand; pnf.logical_exp.Insert(0, first_operand.top_operation); } } } } //2.2 - существование if (top_operation is dis) { if (first_operand.is_quantum_expression && second_operand.is_quantum_expression) { if (first_operand.top_operation is quantifier && second_operand.top_operation is quantifier) { quantifier K1 = new quantifier((quantifier)first_operand.top_operation); //bounded_variables.Add(K1.variable); quantifier K2 = new quantifier((quantifier)second_operand.top_operation); if (K1.is_existance && K2.is_existance && K1.variable == K2.variable) { pnf = first_operand.first_operand | second_operand.first_operand; pnf.logical_exp.Insert(0, first_operand.top_operation); } } } } //3 - правило выноса квантора за скобки (с переименованием связанных переменных) if (top_operation is dis) { if (first_operand.is_quantum_expression && second_operand.is_quantum_expression) { quantifier K1 = new quantifier((quantifier)first_operand.top_operation); //bounded_variables.Add(K1.variable); quantifier K2 = new quantifier((quantifier)second_operand.top_operation); if (K1.variable == K2.variable && bounded_variables.Contains(K2.variable)) { pnf = first_operand.first_operand | second_operand.first_operand.rename_variable(K2.variable, K2.variable + "_renamed"); K2.variable = K2.variable + "1"; pnf.logical_exp.Insert(0, K2); pnf.logical_exp.Insert(0, K1); } else if (!second_operand.is_free_from(K1.variable)) { pnf = first_operand.first_operand | second_operand.first_operand.rename_variable(K1.variable, K1.variable + "_renamed"); pnf.logical_exp.Insert(0, K2); pnf.logical_exp.Insert(0, K1); } else { pnf = first_operand.first_operand | second_operand.first_operand; pnf.logical_exp.Insert(0, K2); pnf.logical_exp.Insert(0, K1); } } } else if (top_operation is kon) { if (first_operand.is_quantum_expression && second_operand.is_quantum_expression) { quantifier K1 = new quantifier((quantifier)first_operand.top_operation); quantifier K2 = new quantifier((quantifier)second_operand.top_operation); if (K1.variable == K2.variable) { pnf = first_operand.first_operand & second_operand.first_operand.rename_variable(K2.variable, K2.variable + "_renamed"); K2.variable = K2.variable + "1"; pnf.logical_exp.Insert(0, K2); pnf.logical_exp.Insert(0, K1); } } } return(pnf); } }
//конструктор копирования: public quantifier(quantifier copy) { _quant = copy._quant; _var = copy._var; }