1

I'm trying to implement a program that takes a variable with multiple values and evaluates all the values. For instance:

foo(X,R) :-
  X > 2,
  Z is R + 1,
  R = Z.

This program might not be valid, but it will help me ask the question regardless.

My question: If X has multiple values, how would I increment the counter for each value X > 2?

1 Answer 1

1

In order to instantiate X to increasingly larger integers you can use the following:

?- between(0, inf, X).
X = 0 ;
X = 1 ;
X = 2 ;
X = 3 ;
X = 4 ;
<ETC.>

PS1: Notice that you have to instantiate R as well since it is used in the arithmetic expression Z is R + 1.

PS2: Notice that your program fails for all instantiations of X and R since R =\= R + 1 for finite R. The for instance means that the following query will not terminate:

?- between(0, inf, X), foo(X, 1).

Alternatively, the program can be rewritten in library CLP(FD) (created by Markus Triska):

:- use_module(library(clpfd)).

foo(X,R):-
  X #> 2,
  Z #= R + 1,
  R #= Z.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the advice. My question was more centered on if I have a variable with multiple values, would prolog evaluate every value and increment the counter for every value where X > 2?
@user2962883 Ah, I see. I have added an alternative solution to my answer which shows the same program in CLP(FD) where you can can express that a variable belongs to a certain range (e.g., X #> 2 means that X is one of [2,+inf)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.