Prolog 程序返回命题公式中的原子 [英] Prolog program to return atoms in a proposition formula

查看:25
本文介绍了Prolog 程序返回命题公式中的原子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 prolog 的新手,正在尝试编写一个程序,该程序以格式良好的命题公式返回 atoms.例如查询 ats(and(q, imp(or(p, q), neg(p))), As). 应该返回 [p,q]对于 As.下面是我的代码,它将公式返回为 As.我不知道如何拆分 F1 中的 ats 中的 single F 中的 F2wff 所以 wff/2 永远不会被调用.请我需要帮助才能从这里开始.谢谢.

I am a newbie to prolog and am trying to write a program which returns the atoms in a well formed propositional formula. For instance the query ats(and(q, imp(or(p, q), neg(p))), As). should return [p,q] for As. Below is my code which returns the formula as As. I dont know what to do to split the single F in ats in the F1 and F2 in wff so wff/2 never gets called. Please I need help to proceed from here. Thanks.

代码

logical_atom( A ) :-
       atom( A ),     
       atom_codes( A, [AH|_] ),
       AH >= 97,
       AH =< 122.

wff(A):- ground(A),
         logical_atom(A).

wff(neg(A)) :- ground(A),wff(A).

wff(or(F1,F2)) :-
    wff(F1),
    wff(F2).
wff(and(F1,F2)) :-
    wff(F1),
    wff(F2).

wff(imp(F1,F2)) :-
    wff(F1),
    wff(F2).
ats(F, As):- wff(F), setof(F, logical_atom(F), As).                            

推荐答案

这应该可以满足您的需求.它提取在任意序言项中找到的唯一原子集.

This should do what you want. It extracts the unique set of atoms found in any arbitrary prolog term.

不过,我会让你来决定什么构成格式良好的命题公式",正如你在问题陈述中所说的那样(你可能想看看DCG 用于解析和验证).

I'll leave it up to you, though, to determine what constitutes a "well formed propositional formula", as you put it in your problem statement (You might want to take a look at DCG's for parsing and validation).

大部分工作都是由这个工人谓词"完成的.它只是通过回溯一次一个地提取解析树中找到的任何原子并丢弃其他任何内容:

The bulk of the work is done by this "worker predicate". It simply extracts, one at a time via backtracking, any atoms found in the parse tree and discards anything else:

expression_atom( [T|_] , T ) :-  % Case #1: head of list is an ordinary atom
  atom(T) ,                      % - verify that the head of the list is an atom.
  T \= []                        % - and not an empty list
  .                              % 
expression_atom( [T|_] , A ) :-  % Case #2: head of listl is a compound term
  compound(T) ,                  % - verify that the head of the list is a compound term
  T =.. [_|Ts] ,                 % - decompose it, discarding the functor and keeping the arguments
  expression_atom(Ts,A)          % - recurse down on the term's arguments
  .                              %
expression_atom( [_|Ts] , A ) :- % Finally, on backtracking,
  expression_atom(Ts,A)          % - we simply discard the head and recurse down on the tail
  .                              %

然后,在顶层,我们有这个简单的谓词,它接受任何 [复合] prolog 术语,并通过 setof/3 提取工作谓词在其中找到的唯一原子集:

Then, at the top level, we have this simple predicate that accepts any [compound] prolog term and extracts the unique set of atoms found within by the worker predicate via setof/3:

expression_atoms( T , As ) :-       % To get the set of unique atoms in an arbitrary term,
  compound(T) ,                     % - ensure that's its a compound term,
  T =.. [_|Ts] ,                    % - decompose it, discarding the functor and keeping the arguments
  setof(A,expression_atom(Ts,A),As) % - invoke the worker predicate via setof/3
  .                                 % Easy!

这篇关于Prolog 程序返回命题公式中的原子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆