Variables for Prolog Variables Using DCG

I want to parse a boolean expression using DCG in Prolog.

Logical terms are presented in the form of lists, for example. ['x','&&','y']for the x ∧ yresult should be a parse tree and(X,Y)(there were Xand Y- unassigned Prolog variables).

I implemented it, and everything works as expected, but I have one problem:
I can’t figure out how to analyze the variables 'x'and 'y'to get the real Prolog variables Xand Yfor the subsequent assignment of truth values.

I tried the following rule options:

  • v(X) --> [X].:
    This, of course, does not work, it only returns and('x','y').
    But can I possibly uniformly replace the logical variables in this term with Prolog variables? I know the predicate term_to_atom(which is proposed as a solution to a similar problem ), but I do not think that it can be used here to achieve the desired result.

  • v(Y) --> [X], {nonvar(Y)}.:
    This returns a returned variable, but, of course, a new one, every time, even if the logical variable ('x', 'y', ...) was already in terms of ['X','&&','X']receiving an estimate and(X,Y), which is also not the desired result.

Is there any elegant or idiomatic solution to this problem?

Thank you very much in advance!


EDIT:

, Prolog DPLL-. , Prolog, Back- Prolog:

  • : , T = [x,'&&',y]
  • : [G_123,'&&',G_456] ( Prolog)
  • {boolean (t), boolean (f)} T.
  • .
  • ... , v, v(T) = t .

Prolog . ! ( , , ;-) ...)

+4
2

, x ( 'x') . , . , .

[x, &&, x] ? , , - . , . , , . ,

power(P) --> factor(F), power_r(F, P).

power(P, D0,D) --> factor(F, D0,D1), power_r(F, P, D1,D).
%        ^^^^                ^^^^^                 ^^^^

.

. ​​ . .

:

1mo Name=Variable :

v(N-V, [N-V|D],D) --> [N], {maybesometest(N)}.

unify_nvs(NVs) :-
   keysort(NVs, NVs2),
   uniq(NVs2).

uniq([]).
uniq([NV|NVs]) :-
   head_eq(NVs, NV).
   uniq(NVs).

head_eq([], _).
head_eq([N-V|_],N-V).
head_eq([N1-_|_],N2-_) :-
   dif(N1,N2).

2do , .

- .

+3

, , . , , , .

, && ||:

parse(Exp, Bindings, NBindings)-->
  parseLeaf(LExp, Bindings, MBindings),
  parse_cont(Exp, LExp, MBindings, NBindings).

parse_cont(Exp, LExp, Bindings, NBindings)-->
  parse_op(Op, LExp, RExp),
  {!},
  parseLeaf(RExp, Bindings, MBindings),
  parse_cont(Exp, Op, MBindings, NBindings).
parse_cont(Exp, Exp, Bindings, Bindings)-->[].

parse_op(and(LExp, RExp), LExp, RExp)--> ['&&'].
parse_op(or(LExp, RExp), LExp, RExp)--> ['||'].

parseLeaf(Y, Bindings, NBindings)-->
  [X],
  {
    (member(bind(X, Var), Bindings)-> Y-NBindings=Var-Bindings ; Y-NBindings=Var-[bind(X, Var)|Bindings])
  }.

.

:

?- phrase(parse(Exp, [], Bindings), ['x', '&&', 'y']).
Exp = and(_G683, _G696),
Bindings = [bind(y, _G696), bind(x, _G683)].

?- phrase(parse(Exp, [], Bindings), ['x', '&&', 'x']).
Exp = and(_G683, _G683),
Bindings = [bind(x, _G683)].

?- phrase(parse(Exp, [], Bindings), ['x', '&&', 'y', '&&', 'x', '||', 'z']).
Exp = or(and(and(_G839, _G852), _G839), _G879),
Bindings = [bind(z, _G879), bind(y, _G852), bind(x, _G839)].
+1

All Articles