繁体   English   中英

Prolog中的命题逻辑子公式

[英]Propositional Logic Subformula in Prolog

这是一个prolog程序,它定义了命题逻辑的语法

所以我一直试图找到X是Y的子公式。我已经做了以下谓词,但我遇到了这个问题。 不完全确定该怎么做。 我知道我需要检查X和Y是否是公式,但我不明白下一步。

at(a). % Our first atom.
at(b). % Our second atom.
%fmla shows if it is a formula or not
fmla(X):- at(X). % an atom is a formula
fmla(neg(X)) :- fmla(X). % neg(F) is a formula if F is a formula
fmla(or(X,Y)) :- fmla(X), fmla(Y). % or(F,G) is a formula if F and G are formulas
fmla(and(X,Y)) :- fmla(X), fmla(Y). % and(F,G) is a formula if F and G are formulas
fmla(imp(X,Y)) :- fmla(neg(X)), fmla(Y). %imp is a formula when not F and G are formulas
fmla(iff(X,Y)) :- fmla(imp(X,Y)), fmla(imp(Y,X)). %Double implication

sub(X,Y) :- fmla(X), fmla(Y). 

想了解如何解决子公式部分的一些想法。

只需描述符合子公式的条件:

sub(X,X) :-           % any formula X is a trivial subformula of itself
    fmla(X).
sub(neg(X),Y) :-      % the argument of neg is a subformula
    sub(X,Y).
sub(or(X,_Y),Z) :-    % the 1st arg. of or is a subformula
    sub(X,Z).
sub(or(_X,Y),Z) :-    % the 2nd arg. of or is a subformula
    sub(Y,Z).
sub(and(X,_Y),Z) :-   % 1st arg. of and
    sub(X,Z).
sub(and(_X,Y),Z) :-   % 2nd arg. of and
    sub(Y,Z).
sub(imp(X,_Y),Z) :-   % you see the pattern, right?
    sub(X,Z).
sub(imp(_X,Y),Z) :-
    sub(Y,Z).
sub(iff(X,_Y),Z) :-
    sub(X,Z).
sub(iff(_X,Y),Z) :-
    sub(Y,Z).

现在你可以测试一些公式是否是另一个公式的子公式:

   ?- sub(and(a,b),b).
yes

或找到给定公式的所有子公式:

   ?- sub(neg(or(a,and(neg(a),imp(b,iff(b,a))))),X).
X = neg(or(a,and(neg(a),imp(b,iff(b,a))))) ? ;
X = or(a,and(neg(a),imp(b,iff(b,a)))) ? ;
X = a ? ;
X = and(neg(a),imp(b,iff(b,a))) ? ;
X = neg(a) ? ;
X = a ? ;
X = imp(b,iff(b,a)) ? ;
X = b ? ;
X = iff(b,a) ? ;
X = b ? ;
X = a ? ; 
no

注意,原子a和b经常列在给定的公式中。 对于不止一次出现的子公式也会发生同样的情况:

   ?- sub(or(neg(a),neg(a)),X).
X = or(neg(a),neg(a)) ? ;
X = neg(a) ? ;
X = a ? ;
X = neg(a) ? ;
X = a ? ;
no

要获得没有重复的所有子公式的列表,您可以使用setof / 3:

   ?- setof(X,sub(or(neg(a),neg(a)),X),Sub).
Sub = [a,neg(a),or(neg(a),neg(a))]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM