Prolog program for returning atoms in a sentence formula

I am new to the prologue and trying to write a program that returns atomsin a well-formed sentence formula. For example, a query ats(and(q, imp(or(p, q), neg(p))), As).should return [p,q]for As. Below is my code that returns the formula as As. I don’t know what to do to divide single Fby atson F1and F2in wff, so it wff/2will never be called. Please, I need help to get out of here. Thank.

CODE

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).                            
+4
source share
3 answers

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

, , " ", ( DCG ).

" ". , , , , - :

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 arguments
  .                              %
expression_atom( [_|Ts] , A ) :- % Finally, on backtracking,
  expression_atom(Ts,A)          % - we simply discard the head and recurse down on the tail
  .                              %

, - [] , setof/3:

expression_atoms( T , As ) :-       % To get the set of unique atoms in an arbitrary term,
  compound(T) ,                     % - ensure that 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!
+1

: . , , , a(Atom).

-, DCG , :

wff_atoms(a(A))       --> [A].
wff_atoms(neg(F))     --> wff_atoms(F).
wff_atoms(or(F1,F2))  --> wff_atoms(F1), wff_atoms(F2).
wff_atoms(and(F1,F2)) --> wff_atoms(F1), wff_atoms(F2).
wff_atoms(imp(F1,F2)) --> wff_atoms(F1), wff_atoms(F2).

:

?- phrase(wff_atoms(and(a(q), imp(or(a(p), a(q)), neg(a(p))))), As).
As = [q, p, q, p].
+3

, "univ" =../2 . , " " , , , . . @mat .

, ; . , , , , .

Univ , Prolog , s- Lisp: . Prolog , , , . .

atoms_of_prop(Prop, Atoms) :-
    % discard the head of the term ('and', 'imp', etc.)
    Prop =.. [_|PropItems],

    collect_atoms(PropItems, AtomsUnsorted),

    % sorting makes the list unique in Prolog
    sort(AtomsUnsorted, Atoms).  

collect_atoms/2 (univ ) atoms_of_prop/2, . , .

% base case
collect_atoms([], []).

% handle atoms
collect_atoms([A|Ps], [A|Rest]) :-
    % you could replace the next test with logical_atom/1
    atom(A), !,
    collect_atoms(Ps, Rest).

% handle terms
collect_atoms([P|Ps], Rest) :-
    compound(P), !,                % compound/1 tests for terms
    atoms_of_prop(P, PAtoms),
    collect_atoms(Ps, PsAtoms),
    append(PAtoms, PsAtoms, Rest).

% ignore everything else
collect_atoms([_|Ps], Rest) :- atoms_of_prop(Ps, Rest).

as-is:

?- atoms_of_prop(ats(and(q, imp(or(p, q), neg(p))), As), Atoms).
Atoms = [p, q].
+1

All Articles