I have code with a construct that appears repeatedly with only a small difference consisting of a different predicate call each time. I want to create a predicate that can take a predicate as an argument.
Here is the generic predicate:
array_def (_,[],[]) :- !. array_def(Type,[H|T],[H0|T0]) :- !,Type(H,H0), array_def(T,T0). array_def(Type,H,[H0]) :- Type(H,H0).
Type is a variable predicate where H is an input and H0 is an output. An example is:
value_literal(H,H0) :- number_literal(H,H0), !. value_literal(H,H0) :- string_literal(H,H0), !. value_literal(H,H0) :- boolean_literal(H,H0).
So I want to declare something like this:
value_literal_array(Arr,Arr0) :- array_def(value_literal(...,...),Arr,Arr0), ...
I don't understand the description of call/2 in the manual. Is there a way to use that? If so, how? If not, how else can I do this? How would I include it in array_def and how would I
Thanks, John
On Sun, May 5, 2013 at 3:29 PM, John McCulloch <mccullochj(a)att.net> wrote:
I have code with a construct that appears repeatedly with only a small difference consisting of a different predicate call each time. I want to create a predicate that can take a predicate as an argument.
Here is the generic predicate:
array_def (_,[],[]) :- !. array_def(Type,[H|T],[H0|T0]) :- !,Type(H,H0), array_def(T,T0). array_def(Type,H,[H0]) :- Type(H,H0).
Type is a variable predicate where H is an input and H0 is an output. An example is:
value_literal(H,H0) :- number_literal(H,H0), !. value_literal(H,H0) :- string_literal(H,H0), !. value_literal(H,H0) :- boolean_literal(H,H0).
So I want to declare something like this:
value_literal_array(Arr,Arr0) :- array_def(value_literal(...,...),Arr,Arr0), ...
I don't understand the description of call/2 in the manual. Is there a way to use that? If so, how? If not, how else can I do this? How would I include it in array_def and how would I
Hi John,
This may solve your problem:
:- meta_predicate array_def(pred(2),?,?). array_def(_,[],[]) :- !. array_def(Type,[H|T],[H0|T0]) :- !,Type(H,H0), array_def(T,T0). array_def(Type,H,[H0]) :- Type(H,H0).
The meta_predicate declaration specifies that the first argument is a predicate of arity 2. It does some precomputation that is necessary in order to run modular programs without ambiguities (otherwise the module system is confused about where the predicate in Type comes from).
You also need to use the 'hiord' package in your module in order to make Type(H,H0) syntax valid.
I hope it helps.
Cheers,