python模态逻辑K求解器

标签 python modal-logic

我正在研究用 python(2.7.5 版本)实现的模态逻辑画面求解器。 所以我已经有了一个将输入字符串转换为画面格式的函数,即:

输入:

~p ^ q

已解析:

['and',('not', 'p'), 'q']

已解析并应用了 alpha 规则:

[('not', 'p'), 'q']

现在,我处理交集、双重否定等的 alpha 公式。 我现在遇到的问题是 beta 公式,例如 Union。

对于 Union 公式,我需要编写一个将一个列表拆分为两个列表的函数,例如:

输入:

('and', 's', ('or', (not,'r'), 'q'))

输出:

1st list ('s',('not','r'))
2nd list ('s','q')

我可以很容易地做到一次,但是我怎样才能递归地扫描公式并生成这些列表,以便稍后我可以扫描它们并验证它们是否关闭?

这样做的最终目标是创建一个画面求解器,它生成一个模型图或返回一个公式无法满足的答案。

最佳答案

这是一个非常有趣的项目:)。我自己现在正在写关于模态逻辑的论文。

首先,我建议您使用 InToHyLo 输入格式,这是现有求解器中的一个标准。

InToHyLo 格式如下所示:

  file ::= ['begin'] dml ['end']

  fml ::= '(' fml ')'                        (* parentheses *)
        | '1' | 'true' | 'True' | 'TRUE'     (* truth *)
        | '0' | 'false' | 'False' | 'FALSE'  (* falsehood *)
        | '~' fml | '-' fml                  (* negation *)
        | '<>' fml | '<' id '>' fml          (* diamonds *)
        | '[]' fml | '[' id ']' fml          (* boxes *)
        | fml '&' fml                        (* conjunction *)
        | fml '|' fml                        (* disjunction *)
        | fml '->' fml                       (* implication *)
        | fml '<->' fml                      (* equivalence *)
        | id                                 (* prop. var. *)

   where identifiers (id) are arbitrary nonempty alphanumeric sequences: (['A'-'Z' 'a'-'z' '0'-'9']+)

为了简化公式的解析并专注于真正的问题:解决实例。我建议您使用现有的解析器,例如 flex/bison

通过在互联网上查找您的问题,(我远不是 Python 专家)它看起来像库“http://pyparsing.wikispaces.com”是解析的引用。

之后,只需按如下方式使用 Bison,您的文件将被完全解析。

这是我的 Bison 文件(用于在求解器 C++ 中使用 Flex/Bison):

/*
 *
 *  Compile with bison.
 */

/*** Code inserted at the begin of the file. ***/
%{   
  #include <stdlib.h>
  #include <list>
  #include "Formula.h"

  // yylex exists
  extern int yylex();
  extern char yytext[];

  void yyerror(char *msg);
%}


/*** Bison declarations ***/
%union
{
   bool         bval;
   operator_t  opval;
   char        *sval;
   TermPtr     *term;      
}

%token LROUND RROUND 

%left IFF
%left IMP
%left OR
%left AND
%right DIAMOND 
%right BOX 
%right NOT 

%token VALUE
%token IDENTIFIER

%type<bval> VALUE
%type<sval> IDENTIFIER 

%type<term> Formula BooleanValue BooleanFormula ModalFormula PropositionalVariable UnaryFormula
%type<opval> BinaryBoolOperator UnaryBoolOperator ModalOperator

%start Start

%%

Start:  
| Formula  { (Formula::getFormula()).setRoot(*$1); }
;

Formula:   BooleanFormula               { $$ = $1; }
         | ModalFormula                 { $$ = $1; }
         | UnaryFormula                 { $$ = $1; }
         | LROUND Formula RROUND        { $$ = $2; }
;

BooleanValue:   VALUE { $$ = new TermPtr( (Term*) new BooleanValue($1) ); }
;

PropositionalVariable:   IDENTIFIER { $$ = new TermPtr( (Term*) new PropositionalVar($1) ); }
;

BooleanFormula:   Formula BinaryBoolOperator Formula { 

                      $$ = new TermPtr( (Term*) new BooleanOp(*$1, *$3, $2) );  /* can be (A OR B) or (A AND B) */
                      delete($3); 
                      delete($1); 
                  }

|                 Formula IMP Formula {

                      ($1)->Negate();
                      $$ = new TermPtr( (Term*) new BooleanOp(*$1, *$3, O_OR) ); /* A -> B can be written : (¬A v B) */
                      delete($3); 
                      delete($1);
                  }

|                 PropositionalVariable IFF PropositionalVariable {

                      PropositionalVar *Copy1 = new PropositionalVar( *((PropositionalVar*)$1->getPtr()) );
                      PropositionalVar *Copy3 = new PropositionalVar( *((PropositionalVar*)$3->getPtr()) );

                      TermPtr Negated1( (Term*)Copy1, $1->isNegated() ); 
                      TermPtr Negated3( (Term*)Copy3, $3->isNegated() );

                      Negated1.Negate(); 
                      Negated3.Negate();

                      TermPtr Or1( (Term*) new BooleanOp(Negated1, *$3, O_OR) ); /* Or1 = (¬A v B) */
                      TermPtr Or2( (Term*) new BooleanOp(Negated3, *$1, O_OR) ); /* Or2 = (¬B v A) */

                      $$ = new TermPtr( (Term*) new BooleanOp(Or1, Or2, O_AND) ); /* We add : (Or1 AND OrB) */

                      delete($3); 
                      delete($1);
                  }                           
;

ModalFormula:   ModalOperator LROUND Formula RROUND  {

                  $$ = new TermPtr( (Term*) new ModalOp(*$3, $1) );
                  delete($3);
                }
|
                ModalOperator ModalFormula  {

                  $$ = new TermPtr( (Term*) new ModalOp(*$2, $1) );
                  delete($2);
                }        
|
                ModalOperator UnaryFormula  {

                  $$ = new TermPtr( (Term*) new ModalOp(*$2, $1) );
                  delete($2);
                }   
;

UnaryFormula:   BooleanValue                 { $$ = $1; }

|               PropositionalVariable        { $$ = $1; }

|
                UnaryBoolOperator UnaryFormula {

                  if ($1 == O_NOT) {
                    ($2)->Negate(); 
                  }                 

                  $$ = $2; 
                }
|
                UnaryBoolOperator ModalFormula {

                  if ($1 == O_NOT) {
                    ($2)->Negate(); 
                  }                 

                  $$ = $2; 
                }                
|
                UnaryBoolOperator LROUND Formula RROUND {

                  if ($1 == O_NOT) {
                    ($3)->Negate(); 
                  }                 

                  $$ = $3; 
                }
;


ModalOperator:   BOX          { $$ = O_BOX; }
|                DIAMOND      { $$ = O_DIAMOND; }
;

BinaryBoolOperator:   AND     { $$ = O_AND; }
|                     OR      { $$ = O_OR; }
;

UnaryBoolOperator:   NOT      { $$ = O_NOT; }
;


/*** Code inserted at the and of the file ***/
%%

void yyerror(char *msg)
{
  printf("PARSER: %s", msg);
  if (yytext[0] != 0)
    printf(" near token '%s'\n", yytext);
  else 
    printf("\n");
  exit(-1);   
}

通过调整它,您将能够完全递归地解析模态逻辑公式:)。

如果稍后,您想挑战现有画面求解器(例如 Spartacus)的求解器。不要忘记这些求解器几乎总是在回答最大的开放 Tableau,因此如果您想找到解决方案的 Kripke 模型,它们肯定会比您更快 ;)

我希望我能帮助你解决你的问题,我想少理论化,但不幸的是我没有为此精通 python :/。

祝你的求解器一切顺利;

最好的问候。


如果您接受我使用 InToHyLo 的提议,我最近在为模态逻辑 K 开发 Kripke 模型的检查器。您可以在这里找到:http://www.cril.univ-artois.fr/~montmirail/mdk-verifier/

最近发表在 PAAR'2016 上:

检查模态逻辑 K 的 Kripke 模型,Jean-Marie Lagniez、Daniel Le Berre、Tiago de Lima 和 Valentin Montmirail,第五届自动推理实用方面研讨会论文集(PAAR 2016 )

关于python模态逻辑K求解器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33283851/

相关文章:

python - 在 Python 中测试所有组合

Python 在程序退出时修改关闭的文件

python - 在 python 中使用 Web 服务的最佳方式是什么?

python - 使用 http.client 登录网站

模态认知逻辑的求解器

haskell - Haskell 中遵守模态公理的有趣运算符

logic - P暗示Q,英文怎么读

Python 在 fetchone 上运行缓慢,在 fetchall 上挂起