Using "Univ" / "=.." meta-predicate to return boolean result of a function?
I am trying to use a meta-predicate "=.." to return a value.
Now I have two examples below, the first one works perfectly, however I cannot figure out how to do something similar using the second example:
Example 1:
?- Expression=..[+,2,3], Value is Expres开发者_如何转开发sion.
Expression = 2 + 3
Value = 5
yes
Example 2:
?- test(N):-N>=0.
Term asserted
?- Term=..[test,-5], Value is Term.
This is producing an error, and i want Value to be yes or true or 1, so that I can use it in an IF statement.
How can this be accomplished using the "univ" "=.." operators?
You can't use is/2
to get the boolean value of a predicate call. Use call/1
instead:
?- Term =.. [test, -5], call(Term).
false.
?- Term =.. [test, 5], call(Term).
Term = test(5).
Or just:
?- call(test, 5).
true.
?- call(test, -5).
false.
Then your if-then-else just becomes:
(Term =.. [test, -5], call(Term) ->
% then-part
;
% else-part
)
If you want to reify the result value for some other reason, the usual advice applies:
(Term =.. [test, -5], call(Term) ->
Value = true % or 1
;
Value = false % or 0, or fail
)
精彩评论