6
votes

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

The logical terms are represented as lists e.g. ['x','&&','y'] for x ∧ y the result should be the parse tree and(X,Y) (were X and Y are unassigned Prolog variables).

I implemented it and everything works as expected but I have one problem:
I can't figure out how to parse the variable 'x' and 'y' to get real Prolog variables X and Y for the later assignment of truth values.

I tried the following rule variations:

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

  • v(Y) --> [X], {nonvar(Y)}.:
    This does return an unbound variable but of course a new one every time even if the logical variable ('x','y',...) was already in the term so ['X','&&','X'] gets evaluated to and(X,Y) which is not the desired result, either.

Is there any elegant or idiomatic solution to this problem?

Many thanks in advance!


EDIT:

The background to this question is that I'm trying to implement the DPLL-algorithm in Prolog. I thought it would by clever to directly parse the logical term to a Prolog-term to make easy use of the Prolog backtracking facility:

  • Input: some logical term, e.g T = [x,'&&',y]
  • Term after parsing: [G_123,'&&',G_456] (now featuring "real" Prolog variables)
  • Assign a value from { boolean(t), boolean(f) } to the first unbound variable in T.
  • simplify the term.
  • ... repeat or backtrack until a assignment v is found so that v(T) = t or the search space is depleted.

I'm pretty new to Prolog and honestly couldn't figure out a better approach. I'm very interested in better alternatives! (So I'm kinda half-shure that this is what I want ;-) and thank you very much for your support so far ...)

2
At what point do you want to actually look at the X and the Y? Do you insist on having an 'x' map to an X? Or is it simply that you want to have all your 'x' refer to the same variable in the final parse tree?user1812457
@Boris I edited the question to give more context - I don't care for the variable name as long as always refers to the same variable .jules

2 Answers

3
votes

You want to associate ground terms like x (no need to write 'x') with uninstantiated variables. Certainly that does not constitute a pure relation. So it is not that clear to me that you actually want this.

And where do you get the list [x, &&, x] in the first place? You probably have some kind of tokenizer. If possible, try to associate variable names to variables prior to the actual parsing. If you insist to perform that association during parsing you will have to thread a pair of variables throughout your entire grammar. That is, instead of a clean grammar like

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

you will now have to write

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

since you are introducing context into an otherwise context free grammar.

When parsing Prolog text, the same problem occurs. The association between a variable name and a concrete variable is already established during tokenizing. The actual parser does not have to deal with it.

There are essentially two ways to perform this during tokenization:

1mo collect all occurrences Name=Variable in a list and unify them later:

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 use some explicit dictionary to merge them early on.

Somewhat related is this question.

1
votes

Not sure if you really want to do what you asked. You might do it by keeping a list of variable associations so that you would know when to reuse a variable and when to use a fresh one.

This is an example of a greedy descent parser which would parse expressions with && and ||:

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])
  }.

It parses the expression and returns also the variable bindings.

Sample outputs:

?- 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)].