0

I need a predicate elements(X,L) where X is an operation like neg X, X and Y and returns a list L with all the elements from the operation, like [X,Y].

I already have these:

elements(neg X, [X]).
elements(X and Y, [X,Y]).
elements(X or Y, [X,Y]).
elements(X imp Y, [X,Y]). 

But I don´t know how to make it work with complex operations like X imp (Y or Z).

1 Answers1

2

I'm not great at picking operator precedences but here's what I have for your input:

:- op(100, fx, neg).
:- op(200, xfy, and).
:- op(200, xfy, or).
:- op(300, xfy, imp).

elements(Term, Variables) :- term_variables(Term, Variables).

This seems to do what you specified:

?- elements(X imp (Y and Z), Q).
Q = [X, Y, Z].

Is there more to your problem than this?

Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77
  • I have the opposite problem, I am not good at Prolog op. Your precedence looks right, see: [Order of precedence](https://en.wikipedia.org/wiki/Logical_connective#Order_of_precedence) – Guy Coder Jan 12 '17 at 23:56
  • Thank you and @GuyCoder for your help. That's exactly what I needed. I actually did some research, but I didn't found that predicate. – Miguel Andrade Jan 12 '17 at 23:59
  • @MiguelAndrade Don't worry about not finding it, I should have looked for it first myself. I was hung up writing a parser when the easier answer was right there. You can also give an upvote in addition to the accept vote. – Guy Coder Jan 13 '17 at 00:08
  • @MiguelAndrade if it weren't for the question I asked today I wouldn't have known about `term_variable/2` either. :) – Daniel Lyons Jan 13 '17 at 01:53
  • @GuyCoder thanks for checking, I usually get precedence all monkeyed up, but I use `op/3` _a lot_ :) – Daniel Lyons Jan 13 '17 at 01:53
  • @DanielLyons It doesn't hurt to [link your question](http://stackoverflow.com/q/41621414/1812457). It increases visibility and might help someone else along the way (your question should now be prominently displayed in the right-hand pane) –  Jan 13 '17 at 08:44