1

I have defined some custom constraints like that:

constraint(a,something).
constraint(a,something1).
constraint(a,something2).

and i need this logical conjunction of them all as a result. ( if one constraint fails, the result should fail )

result(X) :-
    constraint(X,something),
    constraint(X,something1),
    constraint(X,somethingElse).

I'm looking for a more convenient way to avoid this explicit coding of all constraints.

result(X) :- ????
false
  • 10,264
  • 13
  • 101
  • 209
SdSdsdsd
  • 123
  • 10
  • 1
    Predicates in prolog must start with a lower case letter. And the proper `if` operator is `:-` not `:=`. – lurker Jan 03 '14 at 14:40

2 Answers2

3

Consider using maplist/2:

all_true(X) :- maplist(constraint(X), [something, something1, something2]).
mat
  • 40,498
  • 3
  • 51
  • 78
2

At some point, you need a predicate somewhere to actually list all the constraints you wish to apply. You could do something like this:

result(X) :-
    constraints(X, [something, something1, something2]).

constraints(X, [H|T]) :-
    constraint(X, H),
    constraints(X, T).
constraints(_, []).

This mechanism allows you to generate the constraints dynamically as a list, if desired. You could also have the list of constraints be a fact:

constraint_list(a, [something, something1, something2]).

And then use that in the result predicate:

result(X) :-
    constraint_list(X, List),
    constraints(X, List).
lurker
  • 56,987
  • 9
  • 69
  • 103