# Getting parentheses to indicate precedence when interpreting clause bodies

Hi,

I don’t understand how to preserve parentheses that indicate precedence when interpreting clause bodies. The query `test` below yields `[a, ∧ ,b, ∨ ,c]` but i was expecting something like `[a, ∧ , '(', b, ∨ ,c, ')']`; i.e. the parentheses in `BODY` are kept. Can anybody give me a hint? I understand that conjunction has higher precedence than disjunction, so to indicate precedence in a disjunction i would have to use parentheses.

Cheers/JCR

``````test:- BODY = (a, (b; c)), parseToFol(BODY, O), writeln(O).
parseToFol(I, O):- I = ','(I1, I2), parseToFol(I1, O1), parseToFol(I2, O2), flatten([O1, ' ∧ ', O2], O).
parseToFol(I, O):- I = ';'(I1, I2), parseToFol(I1, O1), parseToFol(I2, O2), flatten([O1, ' ∨ ', O2], O).
parseToFol(I, O):- I \= ','(_, _), I \= ';'(_, _), O = [I].``````

Since BODY is a Prolog term/tree, and not a Prolog atom/string. Your input is already parsed. Basically you are handing this Prolog term/tree to parseToFol/2:

``````                      ,
/   \
a     ;
/ \
b   c
``````

In as far it would be better to call your predicate transformToFol/2 instead of parseToFol/2. It will be possibly a Prolog term/tree transformer and not a Prolog atom/string parser.

In case your intention is a Prolog term/tree transformer you can use (=)/2
to destruct and construct Prolog terms/trees. So you could write:

``````transformToFol(I, O):-
I = ;(I1, I2),
transformToFol(I1, O1),
transformToFol(I2, O2),
O = ∨(O1, O2).
``````

Or move it into the head and use operators:

``````transformToFol((I1; I2), (O1 ∨ O2):-
transformToFol(I1, O1),
transformToFol(I2, O2).
``````

Or maybe you want yet something else, unparsing?

1 Like

Thanks @j4n_bur53 ! This solved my problem.