Is there any way to define constants in prolog?
I’d like to write something like
list1 :- [1, 2, 3].
list2 :- [4, 5, 6].
predicate(L) :- append(list1, list2, L).
The work-around I’m using now is
list1([1, 2, 3]).
list2([4, 5, 6]).
predicate(L) :-
list1(L1),
list2(L2),
append(L1, L2, L).
but it’s a bit clumsy to bind a “useless” variable like this every time I need to access the constant.
Another (even uglier) work around I suppose, would be to include cpp in the build-chain.
(In my actual application, the list is a large LUT used in many places.)
I don’t think you can do that in ‘pure’ Prolog (though some implementations may let you do something close, for example ECLiPSe has shelves).
The reason is:
1) You can’t write things like
or
Because right hand side and left hand side are both grounds terms which don’t match.
2) You can’t write things like
or
because the left hand side is now a variable, but variables are only allowed in predicate heads/bodies.
What you could do is to define a multi-option predicate like:
and then retrieve all its values with bagof (or similar predicates):
MLsis the list of allMLvalues that satisfymyList(ML)and of courseconcatconcatenates a list of lists.