0
votes

If I have a term H which is either of the form a > b or a where a and b are constants (although not necessarily named a and b), how can I check the form of H?

Neither H == (X > Y) nor H =:= (X > Y) doesn't work.

2
In SWI-Prolog, I can do H = (X > Y). Can you try on your side? - nhahtdh
@nhahtdh this the answer, I think. ?- (1 > 2) = (X > Y). X = 1, Y = 2. - User
X = (a > b). then X = (_ > _) will be true, and can be used to check for the form X > Y. You don't even need X and Y (unless you want to know their values). To check if X is just an atom (like a) use atom(X). - lurker

2 Answers

1
votes

What you need here is unification =/2:

H = (X > Y)

As for the 2 alternatives that you have tried, they are not what you want:

@Term1 == @Term2

True if Term1 is equivalent to Term2. A variable is only identical to a sharing variable.


+Expr1 =:= +Expr2

True if expression Expr1 evaluates to a number equal to Expr2.

Explanation are taken from swi-prolog.org documentation, but since these are ISO features, a ISO-complient implementation should not have any difference.

1
votes

I prefer to match patterns using subsumes_term/2, since it matches patterns without binding variables. One pattern can match several different terms, so the pattern can be re-used:

:- initialization(main).
:- set_prolog_flag('double_quotes','chars').

main :-
    Pattern = (X>1),
    subsumes_term(Pattern,A>1),
    subsumes_term(Pattern,5>1),
    subsumes_term(Pattern,6>1).

In this example, the variables X or A are unchanged.