Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing user-defined arithmetic functions

How can I add a function (e.g., hammingweight) and use it in expressions occuring in the right-hand side is some (is)/2 goal?

Could something like goal_expansion or term_expansion help here?

I acknowledge that this is not a big feature, but it could increase readability of some of my Prolog programs.

Writing a custom (is)/2 predicate (implementing a custom expression evaluator) is do-able, but I would like to keep runtime overhead low, as I don't want to sacrifice readability for runtime overhead in this case.

like image 620
repeat Avatar asked Mar 04 '15 12:03

repeat


People also ask

What are the 4 categories of user-defined functions?

Function with no arguments and no return value. Function with no arguments and a return value. Function with arguments and no return value. Function with arguments and with return value.

What is the example of user-defined function?

C allows you to define functions according to your need. These functions are known as user-defined functions. For example: Suppose, you need to create a circle and color it depending upon the radius and color.


1 Answers

There is no such provision in ISO Prolog, neither to extend (is)/2 nor to rely on goal expansion. And, looking at the various implementation specific features that are offered by some implementations to this end, there is no generally agreed upon way to do this. So implementing your own (my_is)/2 seems to be the best way to go.

Also note, that this would not only affect (is)/2 but also all other built-ins that use evaluable functors. In particular, all arithmetic comparison built-ins (8.7 Arithmetic comparison) (see this overview) would be affected.

like image 169
false Avatar answered Sep 22 '22 10:09

false