Home > database >  SWI-Prolog: Generalize a predicate to calcluate the power of some function
SWI-Prolog: Generalize a predicate to calcluate the power of some function

Time:08-05

I want to generalize some predicate written in swi-prolog to calculate the power of some function. My predicate so far is:

% calculates the  Power and the  Argument of some function  Function with value  Value.
calc_power(Value, Argument, Function, Power) :-
    not(Power is 0),
    Power is Power_m1   1,
    Value =..[Function, Buffer],
    calc_power(Buffer, Argument, Function, Power_m1), !.
calc_power(Argument, Argument, _, 0).

The call calc_power((g(a)),A,f,POW). gives so far:

A = g(a),
POW = 0.

My generalization should also solve calls like that:

calc_power(A1, a, f, 3).

the solution should be in that special calse A1 = f(f(f(a))). But for some reason it doesn't work. I get the error:

ERROR: Arguments are not sufficiently instantiated

in line

Power is Power_m1   1

it means probably in swi prolog it is not possible to take plus with two variables. How can I solve this problem?

CodePudding user response:

Found some solution

:- use_module(library(clpfd)).

% calculates the  Power and the  Argument of some function  Function with value  Value.
calc_power(Argument, Argument, _, 0).
calc_power(Value, Argument, Function, Power) :-
    Power #\= 0,
    Power #= Power_m1   1,
    Value =..[Function, Buffer],
    calc_power(Buffer, Argument, Function, Power_m1).
  • Related