Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prolog passing a function as a variable, how to add arguments?

I have this arbitrary function that I need to call many times with different variables. btw, this is SWI-Prolog

perform(V1,V2,V3,Function,Result):-
    % 
    % do little stuf.
    %
    Function(Arg1,Arg2,Result).

This gives a syntax error.

But passing a function as a variable without adding arguments works fine as in the following code:

perform(Function):-
    Function.

sayHello:-
    write('hello').

:-perform(sayHello).

So how to add arguments to a variable function?

like image 479
Ihmahr Avatar asked May 12 '12 12:05

Ihmahr


People also ask

How do you declare a passing argument?

Passing arguments by value When an argument is passed by value, the C function receives a copy of the actual value of the argument. To specify that the argument should always be passed by value, use the keyword ByVal preceding the parameter declaration for that argument in the Declare statement for the C function.

How do you represent a variable in Prolog?

A variable in Prolog is a string of letters, digits, and underscores ( _ ) beginning either with a capital letter or with an underscore. Examples: X , Sister , _ , _thing , _y47 , First_name , Z2 The variable _ is used as a "don't-care" variable, when we don't mind what value the variable has.

Which argument is passed to a function?

Arguments are passed by value; that is, when a function is called, the parameter receives a copy of the argument's value, not its address. This rule applies to all scalar values, structures, and unions passed as arguments. Modifying a parameter does not modify the corresponding argument passed by the function call.

Can you use a variable more than once in Prolog?

New! Save questions or answers and organize your favorite content. Learn more.


1 Answers

Specifically in SWI-Prolog you can use call . Quoting the manual:

call(:Goal, +ExtraArg1, ...)

Append ExtraArg1, ExtraArg2, ... to the argument list of Goal and call the result. For example, call(plus(1), 2, X) will call plus(1, 2, X), binding X to 3. The call/[2..] construct is handled by the compiler. The predicates call/[2-8] are defined as real (meta-)predicates and are available to inspection through current_predicate/1, predicate_property/2, etc. Higher arities are handled by the compiler and runtime system, but the predicates are not accessible for inspection.

where the plus indicates that the argument must be fully instantiated to a term that satisfies the required argument type, and the colon indicates that the agument is a meta-argument (this also implies "+").

like image 88
Alexander Serebrenik Avatar answered Sep 29 '22 19:09

Alexander Serebrenik