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.
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
TrueifTerm1is equivalent toTerm2. A variable is only identical to a sharing variable.
+Expr1 =:= +Expr2
Trueif expressionExpr1evaluates to a number equal toExpr2.
Explanation are taken from swi-prolog.org documentation, but since these are ISO features, a ISO-complient implementation should not have any difference.
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.
H = (X > Y). Can you try on your side? - nhahtdh?- (1 > 2) = (X > Y). X = 1, Y = 2.- UserX = (a > b).thenX = (_ > _)will be true, and can be used to check for the formX > Y. You don't even needXandY(unless you want to know their values). To check ifXis just an atom (likea) useatom(X). - lurker