I know Prolog (programming in logic) is all about returning true and false, and that a function is something which returns anything from lists, to numbers, to boolean values. Initially it doesn't seem that Prolog has the concept of functions but instead relies on unification, yet you can do stuff like:
?- X is log(42).
X = 3.7376696182833684.
So it seems that there exist functions? Or is this really just some sort of syntactic sugar hiding the unification part?
And if it really is just syntactic sugar, then how would I go about if I wanted to define a mathematical 'function' like log2?
Of course I can use unification:
log2(X,Result) :- Result is log(X)/log(2).
But say I want to use the 'syntactic sugar function style' so I can write:
?- X is log2(8).
X = 3.0.
How can I do that in Prolog?
arithmetic_function
that will do what you want. Assuming yourlog2/2
predicate exists, the directive:- arithmetic_function(log2/1).
will make your second sample work. I'm not sure why it is deprecated but it seems to still work in 6.2.6. – Daniel Lyonsiz/2
with infix syntax) and construct rules that evaluate whatever additional functions are desired. – hardmath