How to check if a variable is being created in Prolog?

I need to know if a variable is being created in a given rule, but I am not allowed to use var (X) and have no idea how to do this.
To be specific, my rule receives 4 parameters (P, A, B, C).
If P, A, B, C are created, then my rule should "return" the true iff (A + B) mod (P) = C (mod (P)). If one of AB and C is not isntantiated, I must return what value this ensures that (A + B) mod (P) = C (mod (P)). so, for example, if C is not created, the rule should “return” (A + B) mod (P) in the form C and similar behavior if A or B are not created. Writing each rule is easy, but how can I find out which of the cases I am in if I don’t know if the variable was created or not? as mentioned earlier, I cannot use var (X) or number (X), etc., I can only assume that P is always created. Thanks in advance!

+4
source share
2 answers

, , -, , . , .

, : , , , .

, CLP (FD), :

:- use_module(library(clpfd)).

same_sum_mod(A, B, C, P) :-
    (A+B) mod P #= C mod P.

, :

?- same_sum_mod(1, 2, 3, 3).
true.

?- same_sum_mod(1, B, 3, 2).
1+B#=_G823,
_G823 mod 2#=1.

?- same_sum_mod(1, 2, 3, P).
P in inf..-1\/1..sup,
3 mod P#=_G855,
3 mod P#=_G855.

, B , ,

?- B in 0..1, same_sum_mod(1, B, 3, 2).
B = 0.

, .

. CLP (FD).

+4

, @mat - .

, , var/1, (- - , , ), , , :

not_inst(Var):-
  \+(\+(Var=0)),
  \+(\+(Var=1)).

:

?- not_inst(X).
true.
?- not_inst(a).
false.
+4

Source: https://habr.com/ru/post/1624086/


All Articles