实现基于谓词逻辑的归结原理
- 格式:doc
- 大小:134.50 KB
- 文档页数:24
⼈⼯智能技术导论——基于谓词逻辑的机器推理⼀、⼀阶谓词逻辑1、谓词、函数、量词设a1, a2, …, an表⽰个体对象, A表⽰它们的属性、状态或关系, 则表达式A(a1, a2, …, an)在谓词逻辑中就表⽰⼀个(原⼦)命题。
例如,(1) 素数(2), 就表⽰命题“2是个素数”。
(2) 好朋友(张三, 李四), 就表⽰命题“张三和李四是好朋友”。
⼀般地, 表达式P(x1,x2,…,xn)在谓词逻辑中称为n元谓词。
其中P是谓词符号,也称谓词,代表⼀个确定的特征或关系(名)。
x1,x2,…,xn称为谓词的参量或者项,⼀般表⽰个体。
个体变元的变化范围称为个体域(或论述域),包揽⼀切事物的集合称为全总个体域。
为了表达个体之间的对应关系,我们引⼊通常数学中函数的概念和记法。
例如我们⽤father(x)表⽰x的⽗亲,⽤sum(x,y)表⽰数x和y之和,⼀般地,我们⽤如下形式:f(x1,x2,…,xn)表⽰个体变元x1,x2,…,xn所对应的个体y,并称之为n元个体函数,简称函数(或函词、函词命名式)。
其中f是函数符号,有了函数的概念和记法,谓词的表达能⼒就更强了。
例如,我们⽤Doctor(father(Li))表⽰“⼩李的⽗亲是医⽣”,⽤E(sq(x),y))表⽰“x的平⽅等于y”。
以后我们约定⽤⼤写英⽂字母作为谓词符号,⽤⼩写字母f,g, h等表⽰函数符号,⽤⼩写字母x, y, z等作为个体变元符号, ⽤⼩写字母a, b, c等作为个体常元符号。
我们把“所有”、“⼀切”、“任⼀”、“全体”、“凡是”等词统称为全称量词, 记为∀x; 把“存在”、“有些”、“⾄少有⼀个”、 “有的”等词统称为存在量词,记为∃ x。
其中M(x)表⽰“x是⼈”, N(x)表⽰“x有名字”, 该式可读作“对于任意的x, 如果x是⼈, 则x有名字”。
这⾥的个体域取为全总个体域。
如果把个体域取为⼈类集合, 则该命题就可以表⽰为同理, 我们可以把命题“存在不是偶数的整数”表⽰为其中G(x)表⽰“x是整数”, E(x)表⽰“x是偶数”。
人工智能第3章谓词逻辑与归结原理
1、谓词逻辑是什么?
谓词逻辑(Predicate Logic)是一种通用的符号化语言,用来表达
和分析各种谓词命题(Propositional Statements)的逻辑关系。
它可以
用来表达抽象概念和客观真理,并以精确的形式描述这些概念和真理。
谓
词逻辑最重要的功能是,它能够发现和解决各种类型的逻辑问题,这在人
工智能中显得尤为重要。
2、归结原理是什么?
归结原理是一种认识论。
它提出的基本原则是,如果要获得B给定A,应当给出一个充分陈述,即必须提供一系列真实可信的参数,以及由此产
生B的能力证明,在这种情况下A必须是正确的。
因此,归结原理会被用
来推理。
例如,通过归结原理,如果一个具体的概念被认为是正确的,那
么人们可以得出结论,即所有概念的结果也是正确的。
2.3 谓词逻辑归结法基础由于谓词逻辑与命题逻辑不同,有量词、变量和函数,所以在生成子句集之前要对逻辑公式做处理,具体的说就是要将其转化为Skolem 标准形,然后在子句集的基础上再进行归结,虽然基本的归结的基本方法都相同,但是其过程较之命题公式的归结过程要复杂得多。
过程要复杂得多。
本节针对谓词逻辑归结法介绍了Skolem 标准形、子句集等一些必要的概念和定理。
一些必要的概念和定理。
2.3.1 Skolem 标准形Skolem 标准形的定义:标准形的定义: 前束范式中消去所有的存在量词,则称这种形式的谓词公式为Skolem 标准形,任何一个谓词公式都可以化为与之对应的Skolem 标准形。
但是,Skolem 标准形不唯一。
标准形不唯一。
前束范式:A 是一个前束范式,如果A 中的一切量词都位于该公式的最左边(不含否定词),且这些量词的辖域都延伸到公式的末端。
式的末端。
Skolem 标准形的转化过程为,依据约束变量换名规则,首先把公式变型为前束范式,然后依照量词消去原则消去或者略去所有量词。
具体步骤如下:所有量词。
具体步骤如下:将谓词公式G 转换成为前束范式转换成为前束范式前束范式的形式为:前束范式的形式为:(Q 1x 1)(Q 2x 2)…(Q n x n )M(x 1,x 2,…,x n )即:即: 把所有的量词都提到前面去。
把所有的量词都提到前面去。
注意:由于所有的量词的辖域都延伸到公式的末端,即,最左边量词将约束表达式中的所有同名变量。
所以将量词提到公式最前端时存在约束变量换名问题。
要严守规则。
最前端时存在约束变量换名问题。
要严守规则。
约束变量换名规则:约束变量换名规则:(Qx ) M (x ) (Qy ) M (y )(Qx ) M (x,z ) (Qy )M (y,z ) 量词否定等值式:量词否定等值式:~(x ) M (x ) (y ) ~ M (y )~(x ) M (x ) (y ) ~M (y ) 量词分配等值式:量词分配等值式:(x )( P (x ) ∧Q (x ))(x ) P (x ) ∧ (x ) Q (x ) (x )( P (x ) ∨ Q (x )) (x ) P (x ) ∨ (x ) Q (x )消去量词等值式:设个体域为有穷集合(a1, a2, …an )(x ) P (x ) P (a1) ∧ P (a2) ∧…∧ P (an ) (x ) P (x ) P (a1) ∨ P (a2) ∨… ∨ P (an ) 量词辖域收缩与扩张等值式:量词辖域收缩与扩张等值式:( x )( P (x ) ∨Q) ( x ) P (x ) ∨ Q (x )( P (x ) ∧Q) ( x ) P (x ) ∧ Q (x )( P (x )→ Q) (x ) P (x ) → Q (x )( Q → P (x ) ) Q → (x ) P (x )(x )( P (x ) ∨Q) (x ) P (x ) ∨ Q (x )( P (x ) ∧Q) (x ) P (x ) ∧ Q (x )( P (x )→ Q) (x ) P (x ) → Q (x )( Q → P (x )) Q → (x ) P (x )消去量词量词消去原则: 1) 消去存在量词"",即,将该量词约束的变量用任意常量(a, b 等)、或全称变量的函数(f(x), g(y)等)代替。
离散数学基础2017-11-19•一些基本定义:−谓词公式中原子或原子的否定形式称为文字。
−文字的析取式称为子句。
−不包含任何文字的子句称为空子句。
»空子句是不可满足的。
−若干相互形成合取关系的子句以集合元素的形式构成集合,称为子句集。
•定理:谓词公式的子句集化归−任何谓词公式都可应用谓词逻辑等值式及推理规则化成相应的子句集。
−过程(构造性证明):(1)蕴涵消去:消去条件蕴涵符号;(2)否定词深入:否定词直接作用在原子上;(3)变量标准化:处于不同量词辖域的约束变量根据易名规则使用不同的变量名;(4)消去存在量词:对不受约束的存在量词,使用常量符号例化;对被约束的存在量词,引入Skolem函数建立依赖;(5)化为前束形: (前缀)(母式),前缀包含全称量词串,母式中不包含任何量词;(6)将母式化为合取范式;(7)消去全称量词(自由变量默认全称量化);(8)由(6)中各极大项构成子句;(9)变量分离:使各子句不含同名变量。
•例:∀xP(x)→∀x∃y((P(x)∨Q(x))→R(x, y))¬ ∀xP(x) ∨ ∀x∃y(¬(P(x) ∨ Q(x)) ∨ R(x, y)) 蕴涵消去∃x¬P(x) ∨ ∀x∃y ((¬P(x) ˄ ¬Q(x)) ∨ R(x, y))否定词深入∃x¬P(x) ∨ ∀z∃y ((¬P(z) ˄ ¬Q(z)) ∨ R(z, y))变量标准化¬P(c) ∨ ∀z((¬P(z) ˄ ¬Q(z)) ∨ R(z, f Skolem(z))消去存在量词∀z(¬P(c) ∨ ((¬P(z) ˄ ¬Q(z)) ∨ R(z, f Skolem(z))) 化为前束形∀z((¬P(c) ∨ ¬P(z) ∨ R(z, f Skolem(z)) ˄(¬P(c) ∨ ¬Q(z) ∨ R(z, f Skolem(z)))将母式化为合取范式¬P(c) ∨ ¬P(z) ∨ R(z, f Skolem(z), ¬P(c) ∨ ¬Q(z) ∨ R(z, f Skolem(z) 消去全称量词 {¬P(c) ∨ ¬P(u) ∨ R(u, f Skolem(u), ¬P(c) ∨ ¬Q(v) ∨ R(v, f Skolem(v)} 变量分离−说明:»子句中的变量总是被默认为全称量化的;»化归得到的子句集不等价于原公式;»考虑到量词消去和引入规则的应用,若公式 A 在逻辑上遵循公式集 S,则也遵循由 S 变换成的子句集。
基于谓词逻辑的归结原理研究引言:谓词逻辑是一种常用的逻辑推理方法,用于描述和推理关于对象和关系的命题。
在谓词逻辑中,通过定义谓词和量词来表达命题,通过逻辑运算符来进行推理。
归结是一种基于谓词逻辑的推理方法,通过将两个命题进行合并和化简,得到一个新的命题,从而推导出结论。
一、谓词逻辑的基本概念1. 谓词:谓词是用于描述对象或关系的符号,可以是一个简单的关系,也可以是一个复杂的命题。
例如,P(x)表示x具有性质P,R(x, y)表示x与y之间存在关系R。
2. 量词:量词用于限定谓词的范围,包括全称量词和存在量词。
全称量词表示谓词对于所有对象都成立,存在量词表示谓词对于存在的对象成立。
3. 逻辑运算符:逻辑运算符包括与、或、非等,用于连接和操作命题。
例如,∧表示与运算,∨表示或运算,¬表示非运算。
二、归结原理的基本思想归结是一种基于谓词逻辑的推理方法,通过将两个命题进行合并和化简,得到一个新的命题,从而推导出结论。
归结原理的基本思想是将待证命题与已知命题进行归结,即通过合并和化简来消除冗余信息,从而推导出结论。
三、归结的基本步骤1. 子句化:将命题转化为子句的形式,即将谓词逻辑的公式化简为子句集合的形式。
子句是由谓词和其参数组成的一个命题,可以表示为谓词与其参数的析取。
2. 归结操作:选择两个子句进行归结操作,即将两个子句通过合一操作找到归结项,并进行合一替换,得到一个新的子句。
3. 消解规则:根据不同的合一替换规则,对归结项进行化简,消除冗余的信息,得到一个更简化的子句。
4. 重复归结:重复进行归结操作,直到得到一个空子句或无法进行归结为止。
5. 结论推导:如果得到一个空子句,则说明原始命题成立,可以推导出结论;如果无法进行归结,则说明原始命题不成立。
四、归结原理的应用领域1. 自动推理:归结原理可以应用于自动推理系统中,通过将待证命题与已知命题进行归结,从而自动推导出结论。
这在人工智能领域中具有广泛的应用。
河南城建学院《人工智能》实验报告实验名称:实现基于谓词逻辑的归结原理成绩:____专业班级:学号:姓名:实验日期:20 14 年 05 月 13日实验器材:一台装PC机。
一、实验目的熟练掌握使用归结原理进行定理证明的过程,掌握基于谓词逻辑的归结过程中,子句变换过程、替换与合一算法、归结过程及简单归结策略等重要环节,进一步了解机器自动定理证明的实现过程。
二、实验要求对于任意给定的一阶谓词逻辑所描述的定理,要求实现如下过程:(1) 谓词公式到子句集变换;(2) 替换与合一算法;(3) 在某简单归结策略下的归结。
三、实验步骤步1 设计谓词公式及自居的存储结构,即内部表示。
注意对全称量词∀x和存在量词∃x可采用其他符号代替;步2 实现谓词公式到子句集变换过程;步3 实现替换与合一算法;步4 实现某简单归结策略;步5 设计输出,动态演示归结过程,可以以归结树的形式给出;步6 实现谓词逻辑中的归结过程,其中要调用替换与合一算法和归结策略。
四、代码谓词公式到子句集变换的源代码:#include<iostream>#include<sstream>#include<stack>#include<queue>using namespace std;//一些函数的定义void initString(string &ini);//初始化string del_inlclue(string temp);//消去蕴涵符号string dec_neg_rand(string temp);//减少否定符号的辖域string standard_var(string temp);//对变量标准化string del_exists(string temp);//消去存在量词string convert_to_front(string temp);//化为前束形string convert_to_and(string temp);//把母式化为合取范式string del_all(string temp);//消去全称量词string del_and(string temp);//消去连接符号合取%string change_name(string temp);//更换变量名称//辅助函数定义bool isAlbum(char temp);//是字母string del_null_bracket(string temp);//删除多余的括号string del_blank(string temp);//删除多余的空格void checkLegal(string temp);//检查合法性char numAfectChar(int temp);//数字显示为字符//主函数void main(){cout<<"------------------求子句集九步法演示-----------------------"<<endl;system("color 0A");//orign = "Q(x,y)%~(P(y)";//orign = "(@x)(P(y)>P)";//orign = "~(#x)y(x)";//orign = "~((@x)x!b(x))";//orign = "~(x!y)";//orign = "~(~a(b))";string orign,temp;char command,command0,command1,command2,command3,command4,command5, command6,command7,command8,command9,command10;//================================================================= ============cout<<"请输入(Y/y)初始化谓词演算公式"<<endl;cin>>command;if(command == 'y' || command == 'Y')initString(orign);elseexit(0);//================================================================= ============cout<<"请输入(Y/y)消除空格"<<endl;cin>>command0;if(command0 == 'y' || command0 == 'Y'){//del_blank(orign);//undonecout<<"消除空格后是"<<endl<<orign<<endl;}elseexit(0);//================================================================= ============cout<<"请输入(Y/y)消去蕴涵项"<<endl;cin>>command1;if(command1 == 'y' || command1 == 'Y'){orign =del_inlclue(orign);cout<<"消去蕴涵项后是"<<endl<<orign<<endl;}elseexit(0);//================================================================= ============cout<<"请输入(Y/y)减少否定符号的辖域"<<endl;cin>>command2;if(command2 == 'y' || command2 == 'Y'){do{temp = orign;orign = dec_neg_rand(orign);}while(temp != orign);cout<<"减少否定符号的辖域后是"<<endl<<orign<<endl;}elseexit(0);//================================================================= ============cout<<"请输入(Y/y)对变量进行标准化"<<endl;cin>>command3;if(command3 == 'y' || command3 == 'Y'){orign = standard_var(orign);cout<<"对变量进行标准化后是"<<endl<<orign<<endl;}elseexit(0);//================================================================= ============cout<<"请输入(Y/y)消去存在量词"<<endl;cin>>command4;if(command4 == 'y' || command4 == 'Y'){orign = del_exists(orign);cout<<"消去存在量词后是(w = g(x)是一个Skolem函数)"<<endl<<orign<<endl;}elseexit(0);//================================================================= ============cout<<"请输入(Y/y)化为前束形"<<endl;cin>>command5;if(command5 == 'y' || command5== 'Y'){orign = convert_to_front(orign);cout<<"化为前束形后是"<<endl<<orign<<endl;}elseexit(0);//================================================================= ============cout<<"请输入(Y/y)把母式化为合取方式"<<endl;cin>>command6;if(command6 == 'y' || command6 == 'Y'){orign = convert_to_and(orign);cout<<"把母式化为合取方式后是"<<endl<<orign<<endl;}elseexit(0);//================================================================= ============cout<<"请输入(Y/y)消去全称量词"<<endl;cin>>command7;if(command7 == 'y' || command7 == 'Y'){orign= del_all(orign);cout<<"消去全称量词后是"<<endl<<orign<<endl;}elseexit(0);//================================================================= ============cout<<"请输入(Y/y)消去连接符号"<<endl;cin>>command8;if(command8 == 'y' || command8 == 'Y'){orign = del_and(orign);cout<<"消去连接符号后是"<<endl<<orign<<endl;}elseexit(0);//================================================================= ============cout<<"请输入(Y/y)变量分离标准化"<<endl;cin>>command9;if(command9 == 'y' || command9 == 'Y'){orign = change_name(orign);cout<<"变量分离标准化后是(x1,x2,x3代替变量x)"<<endl<<orign<<endl;}elseexit(0);//================================================================= ===========cout<<"-------------------------完毕-----------------------------------"<<endl;cout<<"(请输入Y/y)结束"<<endl;do{}while('y' == getchar() || 'Y'==getchar());exit(0);}void initString(string &ini){char commanda,commandb;cout<<"请输入您所需要转换的谓词公式"<<endl;cout<<"需要查看输入帮助(Y/N)? "<<endl;cin>>commanda;if(commanda == 'Y' || commanda == 'y')cout<<"本例程规定输入时蕴涵符号为>,全称量词为@,存在量词为#,"<<endl <<"取反为~,吸取为!,合取为%,左右括号分别为( 、),函数名请用一个字母"<<endl;cout<<"请输入(y/n)选择是否用户自定义"<<endl;cin>>commandb;if(commandb =='Y'|| commandb=='y')cin>>ini;elseini = "(@x)(P(x)>((@y)(P(y)>P(f(x, y)))%~(@y)(Q(x,y)>P(y))))";cout<<"原始命题是"<<endl<<ini<<endl;}string del_inlclue(string temp)//消去>蕴涵项{//a>b变为~a!bchar ctemp[100]={""};string output;int length = temp.length();int i = 0,right_bracket = 0,falg= 0;stack<char> stack1,stack2,stack3;strcpy(ctemp,temp.c_str());while(ctemp[i] != '\0' && i <= length-1){stack1.push(ctemp[i]);if('>' == ctemp[i+1])//如果是a>b则用~a!b替代{falg = 1;if(isAlbum(ctemp[i]))//如果是字母则把ctemp[i]弹出{stack1.pop();stack1.push('~');stack1.push(ctemp[i]);stack1.push('!');i = i + 1;}else if(')' == ctemp[i]){right_bracket++;do{if('(' == stack1.top())right_bracket--;stack3.push(stack1.top());stack1.pop();}while((right_bracket != 0));stack3.push(stack1.top());stack1.pop();stack1.push('~');while(!stack3.empty()){stack1.push(stack3.top());stack3.pop();}stack1.push('!');i = i + 1;}}i++;}while(!stack1.empty()){stack2.push(stack1.top());stack1.pop();}while(!stack2.empty()){output += stack2.top();stack2.pop();}if(falg == 1)return output;elsereturn temp;}string dec_neg_rand(string temp)//减少否定符号的辖域{char ctemp[100],tempc;string output;int flag2 = 0;int i = 0,left_bracket = 0,length = temp.length();stack <char> stack1,stack2;queue <char> queue1;strcpy(ctemp,temp.c_str());//复制到字符数组中while(ctemp[i] != '\0' && i <= length - 1){stack1.push(ctemp[i]);if(ctemp[i] == '~')//如果是~否则什么都不做{char fo = ctemp[i+2];if(ctemp[i+1] == '(')//如果是(,否则什么都不做{if(fo == '@' || fo =='#')//如果是全称量词{flag2 = 1;i++;stack1.pop();stack1.push(ctemp[i]);if(fo == '@')stack1.push('#');elsestack1.push('@');stack1.push(ctemp[i+2]);stack1.push(ctemp[i+3]);stack1.push('(');stack1.push('~');if(isAlbum(ctemp[i+4])){stack1.push(ctemp[i+4]);i = i + 5;}elsei = i + 4;do{queue1.push(temp[i]);if(temp[i] == '(')left_bracket ++;else if(temp[i] == ')')left_bracket --;i ++;}while(left_bracket != 0 && left_bracket >=0);queue1.push(')');while(!queue1.empty()){tempc = queue1.front();queue1.pop();stack1.push(tempc);}}}}i ++;}while(!stack1.empty()){stack2.push(stack1.top());stack1.pop();}while(!stack2.empty()){output += stack2.top();stack2.pop();}if(flag2 == 1)temp = output;/************************************************************/ char ctemp1[100];string output1;stack<char> stack11,stack22;int falg1 = 0;int times = 0;int length1 = temp.length(),inleftbackets = 1,j = 0;strcpy(ctemp1,temp.c_str());while(ctemp1[j] != '\0' && j <= (length1 -1)){stack11.push(ctemp1[j]);if(ctemp1[j] == '~'){if(ctemp1[j+1] == '(' /*&& ctemp1[j + 2] != '~'*/){j = j + 2;stack11.push('(');////////////////while(inleftbackets != 0 && inleftbackets >=0 && times <= (length1 - j) && times >= 0){stack11.push(ctemp1[j]);if(ctemp1[j] == '(')inleftbackets ++;else if(ctemp1[j] == ')')inleftbackets --;if(inleftbackets == 1 && ctemp1[j+1] == '!' && ctemp1[j+2] != '@' && ctemp1[j+2] != '#'){falg1 =1;stack11.push(')');//////////stack11.push('%');stack11.push('~');stack11.push('(');//////////j = j+1;}if(inleftbackets == 1 && ctemp1[j+1] == '%' && ctemp1[j+2] != '@' && ctemp1[j+2] != '#'){falg1 =1;stack11.push(')');//////////stack11.push('!');stack11.push('~');stack11.push('(');//////////j = j+1;}j = j +1;}if(falg1 == 1)stack11.push(')');stack11.pop();stack11.push(')');//此处有bugstack11.push(')');//此处有bug}}j ++;}while(!stack11.empty()){stack22.push(stack11.top());stack11.pop();}while(!stack22.empty()){output1 += stack22.top();stack22.pop();}if(falg1 == 1)temp = output1;/************************************************************/ char ctemp3[100];string output3;int k = 0,left_bracket3 = 1,length3 = temp.length();stack <char> stack13,stack23;int flag = 0,bflag = 0;strcpy(ctemp3,temp.c_str());//复制到字符数组中while(ctemp3[k] != '\0' && k <= length3-1){stack13.push(ctemp3[k]);if(ctemp3[k] == '~'){if(ctemp3[k+1] == '('){if(ctemp3[k + 2] == '~'){flag = 1;stack13.pop();k =k + 2;while(left_bracket3 != 0 && left_bracket3 >=0){stack13.push(ctemp3[k+1]);if(ctemp3[k+1] == '(')left_bracket3 ++;if(ctemp3[k+1] == ')')left_bracket3 --;if(ctemp3[k+1] == '!' || ctemp3[k+1] == '%')bflag = 1;k ++;}stack13.pop();}}}k ++;}while(!stack13.empty()){stack23.push(stack13.top());stack13.pop();}while(!stack23.empty()){output3 += stack23.top();stack23.pop();}if(flag == 1 && bflag == 0)temp = output3;return temp;}string standard_var(string temp)//对变量标准化,简化,不考虑多层嵌套{char ctemp[100],des[10]={" "};strcpy(ctemp,temp.c_str());stack <char> stack1,stack2;int l_bracket = 1,falg = 0,bracket = 1;int i = 0,j = 0;string output;while(ctemp[i] != '\0' && i < temp.length()){stack1.push(ctemp[i]);if(ctemp[i] == '@' || ctemp[i] == '#'){stack1.push(ctemp[i+1]);des[j] = ctemp[i+1];j++;stack1.push(ctemp[i+2]);i = i + 3;stack1.push(ctemp[i]);i++;if(ctemp[i-1] == '('){while(ctemp[i] != '\0' && l_bracket != 0){if(ctemp[i] == '(')l_bracket ++;if(ctemp[i] == ')')l_bracket --;if(ctemp[i] == '(' && ctemp[i+1] == '@' ){des[j] = ctemp[i+2];j++;}if(ctemp[i+1] == '(' && ctemp[i+2] == '#' ){falg = 1;int kk = 1;stack1.push(ctemp[i]);stack1.push('(');stack1.push(ctemp[i+2]);i = i+3;if(ctemp[i] == 'y')ctemp[i] ='w';stack1.push(ctemp[i]);stack1.push(')');stack1.push('(');i = i+3;while(kk != 0){if(ctemp[i]=='(')kk++;if(ctemp[i] ==')')kk--;if(ctemp[i] == 'y')ctemp[i] ='w';stack1.push(ctemp[i]);i++;}}stack1.push(ctemp[i]);i ++;}}}i ++;}while(!stack1.empty()){stack2.push(stack1.top());stack1.pop();}while(!stack2.empty()){output += stack2.top();stack2.pop();}if(falg == 1)return output;elsereturn temp;}string del_exists(string temp)//消去存在量词{char ctemp[100],unknow;strcpy(ctemp,temp.c_str());int left_brackets = 0,i = 0,falg = 0;queue<char> queue1;string output;while(ctemp[i] != '\0' && i < temp.length()){if(ctemp[i] =='(' && ctemp[i+1] =='#'){falg = 1;unknow = ctemp[i+2];i = i+4;do{if(ctemp[i] == '(')left_brackets ++;if(ctemp[i] == ')')left_brackets --;if(ctemp[i] == unknow){queue1.push('g');queue1.push('(');queue1.push('x');queue1.push(')');}if(ctemp[i] != unknow)queue1.push(ctemp[i]);i++;}while(left_brackets != 0);}queue1.push(ctemp[i]);i++;}while(!queue1.empty()){output+= queue1.front();queue1.pop();}if(falg == 1)return output;elsereturn temp;}string convert_to_front(string temp)//化为前束形{char ctemp[100];strcpy(ctemp,temp.c_str());int i = 0;string out_var = "",output = "";while(ctemp[i] != '\0' && i < temp.length()){if(ctemp[i] == '(' && ctemp[i+1] == '@'){out_var = out_var + ctemp[i] ;//(@)out_var = out_var + ctemp[i+1] ;out_var = out_var +ctemp[i+2];out_var = out_var +ctemp[i+3];i = i + 4;}output = output + ctemp[i];i++;}output = out_var + output;return output;}string convert_to_and(string temp)//把母式化为合取范式,Q/A?{temp = "(@x)(@y)((~P(x)!(~P(y))!P(f(x,y)))%((~P(x)!Q(x,g(x)))%((~P(x))!(~P(g(x)))))";return temp;}string del_all(string temp)//消去全称量词{char ctemp[100];strcpy(ctemp,temp.c_str());int i = 0,flag = 0;string output = "";while(ctemp[i] != '\0' && i < temp.length()){if(ctemp[i] == '(' && ctemp[i+1] == '@'){i = i + 4;flag = 1;}else{output = output + ctemp[i];i ++;}}return output;}string del_and(string temp)//消去连接符号合取% {char ctemp[100];strcpy(ctemp,temp.c_str());int i = 0,flag = 0;string output = "";while(ctemp[i] != '\0' && i < temp.length()){if(ctemp[i] == '%' ){ctemp[i] = '\n';}output = output +ctemp[i];i++;}return output;}string change_name(string temp)//更换变量名称{char ctemp[100];strcpy(ctemp,temp.c_str());string output = "";int i = 0,j = 0,falg = 0;while(ctemp[i] != '\0' && i < temp.length()){falg++;while('\n' != ctemp[i] && i < temp.length()){if('x' == ctemp[i]){output = output + ctemp[i] ;output = output + numAfectChar(falg);}elseoutput = output + ctemp[i] ;i++;}output = output + ctemp[i] ;i ++;}return output;}bool isAlbum(char temp){if(temp <= 'Z' && temp >= 'A' || temp <= 'z' && temp >= 'a') return true;return false;}char numAfectChar(int temp)//数字显示为字符{int t;switch (temp){case 1:t = 1;break;case 2:t = 2;break;case 3:t = 3;break;case 4:t = 4;break;default:t = 89;break;}return t;}五、结果分析(1)非用户自定义时:六、心得体会通过这次实验,熟悉了谓词公式转换成子句集的步骤,理解消解(谓词公式化为子句集)规则,能把任意谓词公式转换成子句集。