# Arithmetic\_function/1

**URL:** <https://swi-prolog.discourse.group/t/arithmetic-function-1/2268>\
**Category:** Predicate\
**Created:** [May 3, 2020, 1:36pm UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268 "2020-05-03T13:36:31Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcculloch](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@mcculloch](https://swi-prolog.discourse.group/u/mcculloch)\
**Post date:** [May 3, 2020, 1:36pm UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/1 "2020-05-03T13:36:32Z")

</div>

I found [arithmetic\_function/1](https://www.swi-prolog.org/pldoc/man?predicate=arithmetic_function/1) but it is deprecated. If [arithmetic\_expression\_value/2](https://www.swi-prolog.org/pldoc/doc_for?object=arithmetic%3Aarithmetic_expression_value/2) is supposed to accomplish the same thing, I don’t understand how. Is there a way to create custom arithmetic expression functions that isn’t deprecated? If not, I’d like to use arithmetic\_function/1 but I’m not sure how to use it. Do I just create a predicate with an extra argument for the return value and declare it in arithmetic\_function/1 as a directive?

I’d also like to define functions that can take list arguments but I’ve seen some discussion suggesting that this was never possible. Any ideas?

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [May 3, 2020, 2:17pm UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/2 "2020-05-03T14:17:11Z")

</div>

If I understand your problem correctly you have run into the problem many new to Prolog have run into with regards to using math expressions; I even had this problem.

You often here the saying that Prolog doesn’t have functions, only predicates, but that is not really true, see: [Arithmetic Functions](https://www.swi-prolog.org/pldoc/man?section=functions)

The use of these is a bit deceptive in that [is/2](https://www.swi-prolog.org/pldoc/man?predicate=is/2) has to be used, e.g.

```prolog
?- X is 2 + 2.
X = 4.

```

Over the years I have come to think of Prolog not as a Turing complete programming language but a language to create models to solve problems and as such there are certain parts of Prolog that are used for modeling and there are parts that are embedded modeling with island grammars within Prolog and the arithmetic functions are one such island grammar that uses _ **is** _ as both an escape syntax into the island grammar and/or as a predicate to return the value of the arithmetic functions.

is/2 used as a predicate.

```prolog
?- is(X,2+2).
X = 4.

```

Some other embedded modeling are constraint satisfaction, term rewriting and DCGs.

Also to expand ones thinking on this is other types of operators. ([How Operators: Infix, Prefix, Postfix, or Mixfix (Distfix)](http://myworkandstudy.blogspot.com/2013/08/how-operators-infix-prefix-postfix-or.html))

HTH

* * *

> [@mcculloch](#):
>
> I’d also like to define functions that can take list arguments but I’ve seen some discussion suggesting that this was never possible.

Can you give us some concrete examples. When you say **list arguments** I think of list ([List Manipulation](https://www.swi-prolog.org/pldoc/man?section=lists)) but I also think of arguments ([Option list processing](https://www.swi-prolog.org/pldoc/man?section=option))

---

<div class="post-metadata">

**Author:** ![mcculloch](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@mcculloch](https://swi-prolog.discourse.group/u/mcculloch)\
**Post date:** [May 3, 2020, 8:49pm UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/3 "2020-05-03T20:49:00Z")

</div>

By list argument I meant something like sum(L) where L is a list of numbers. e.g.

> ?- S is sum([1,2,3]).  
> S = 6.

Pretty simple but I don’t know how to define the sum function and I’m not sure it’s even possible. But to clarify, I want to be able to define arithmetic expression functions in general, not just ones with list arguments.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [May 3, 2020, 9:34pm UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/4 "2020-05-03T21:34:29Z")

</div>

Perhaps using foldl/4. See: [library(apply): Apply predicates on a list](https://www.swi-prolog.org/pldoc/man?section=apply)

```prolog
?- foldl(add,[1,2,3],0,Sum).
Sum = 6.

```

For more details see: [Adding elements of a list in Prolog](https://stackoverflow.com/questions/43327910/adding-elements-of-a-list-in-prolog)

Since this notes using a functional form in Prolog, I expect David will make an appearance.

---

<div class="post-metadata">

**Author:** ![mcculloch](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@mcculloch](https://swi-prolog.discourse.group/u/mcculloch)\
**Post date:** [May 3, 2020, 11:40pm UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/5 "2020-05-03T23:40:24Z")

</div>

The goal for me is custom arithmetic expression functions.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [May 4, 2020, 8:03am UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/7 "2020-05-04T08:03:22Z")

</div>

The arithmetic function interface was introduced when is/2 dropped support for Prolog-defined functions. The reason for that is that it requires (de)serialization of arithmetic objects to the Prolog stacks for intermediate results and requires the arithmetic code to be prepared to deal with stack shifts and garbage collection. This slows down expression evaluation quite a bit. As Prolog systems I viewed as most closely related also did not provide Prolog defined functions I decided to drop them. The arithmetic function declaration performs source code rewriting to get something comparable. In any case, functions are always from a set of numbers to a single output number.

If you want functional notation in Prolog, use one of the various functional notation extensions. If you want it below a predicate like is, define your own eval/2 or something similar.

Although arithmetic\_function is deprecated, It won’t disappear and even if it would, you can copy the library from an old version and add it to your project. I mostly think this is not a technique that should be promoted.

---

<div class="post-metadata">

**Author:** ![mcculloch](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@mcculloch](https://swi-prolog.discourse.group/u/mcculloch)\
**Post date:** [May 4, 2020, 11:42am UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/8 "2020-05-04T11:42:29Z")

</div>

I’m not going to pursue arithmetic\_function/1 anymore. I can’t figure out how to use it anyway.

I do have a predicate which works, but it requires explicitly listing in the code all term names that should be considered as operators . I’ll just add calls to current\_op/3 instead of listing each operator.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [May 5, 2020, 7:34am UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/9 "2020-05-05T07:34:20Z")

</div>

This answer hints at some misconceptions. First of all, _operators_ have nothing to do with arithmetic evaluation. I can write `A is 1+B`, but this is just syntactic sugar using operators for `is(A,+(1,B))`. Arithmetic _evaluation_ applies to the right argument of is/2 and both arguments of =:=/2, \>/2, etc. _applies_ simply mean that executing e.g., is/2 causes the implementation of is/2 to examine the right argument.  
If it finds a term (e.g., +(1,B)) it performs arithmetic evaluation on the arguments, calls some `(C)` function to do the right operation (addition), etc. If the whole thing successfully evaluates to number is/2 _unifies_ this number to whatever appears on the left side and succeeds or fails.

Arithmetic functions are terms that are recognized by the evaluation logic of is/2, etc.

The library(arithmetic) allows defining new arithmetic functions. So, we can define squared(X) to be the same as X^2, This means we define

```
squared(X,Y) :- Y is X^2.

```

Now the library rewrites `A is sqrt(squared(X1-X2)+squared(Y1-Y2)` as

```prolog
Xd is X1-X2,
squared(Xd,X2),
Yd is Y1-Y2,
squared(Yd,Y2),
A is sqrt(X2+Y2).

```

That is all …

---

<div class="post-metadata">

**Author:** ![mcculloch](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@mcculloch](https://swi-prolog.discourse.group/u/mcculloch)\
**Post date:** [May 5, 2020, 11:27am UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/10 "2020-05-05T11:27:17Z")

</div>

Not really a misconception, but a generalization. In my code, all of arithmetic functions that I want to support with is/2 are operators. I realize that not all arithmetic expression functions have corresponding operators and that not all operators have corresponding arithmetic functions. In my case though, making the assumption allows me to use all binary arithmetic operators without explicitly listing the ones to be used with is/2 instead of my numeric predicates.

I did figure out what I was doing wrong with library(arithmetic). I was declaring the arity of the predicate instead of the arity of the function. But it still won’t take a list with more than one member as an argument.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [May 5, 2020, 6:23pm UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/11 "2020-05-05T18:23:24Z")

</div>

FWIW, IBM Prolog allowed calling arbitrary predicates, something like this (I forget the syntax):

```prolog
range(X, Limit, []) :- X >= Limit, !.
range(X, Limit, [X|Xs]) :- range(?X+1, Limit, Xs]).

```

which would expand to

```prolog
range(X, Limit, []) :- X >= Limit, !.
range(X, Limit, [X|Xs]) :-
    X2 is X+1,
    range(X2, Limit, Xs]).

```

It could also handle arbitrary predicates, by taking their last argument as the “result”:

```prolog
incr(X, Incr) :- X + 1.

range(X, Limit, []) :- X >= Limit, !.
range(X, Limit, [X|Xs]) :- range(?incr(X), Limit, Xs]).

```

We already have something similar in SWI-Prolog with the “.” operator for dicts … would it make sense to make this more general?

---

<div class="post-metadata">

**Author:** ![mcculloch](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@mcculloch](https://swi-prolog.discourse.group/u/mcculloch)\
**Post date:** [May 6, 2020, 11:19am UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/12 "2020-05-06T11:19:55Z")

</div>

So, since arithmetic\_functions/1 doesn’t allow functions that take lists as arguments, I still have to use a predicate I wrote that I call resolve/2. It uses the same approach where it adds a variable argument term and then calls it. But since a “resolvable” term still can be an arithmetic function, the code has to determine which way to “resolve” the term. Here’s the code as it stand now:

```prolog
resolve(N,N) :- integer(N), !.
resolve(T,N) :-	
	\+ is_list(T),					
	compound(T), !,				
	T =.. [Head | Args],
	resolve_compound(Head,Args,N).

resolve_compound(Op,Args,N) :-	
	length(Args,2),
	current_op(_,_,Op), !,   
	Args = [Arg1,Arg2],
	resolve(Arg1,N1),
	resolve(Arg2,N2),
	Callable =.. [Op, N1, N2],
	N is Callable.

resolve_compound(Head,Args,N) :-		
	append(Args,[N],CallArgs),			
	Callable =.. [Head | CallArgs],
	Callable.

```

Sorry, I’m still not good at formatting in this forum. It doesn’t show my indentation. I had to remove my comments. They made quite a mess.

Yes, I agree that checking for any binary operator is not the best way to determine if is/2 should be used or if it should use a custom predicate, but I couldn’t think of a better way. This is where I thought someone might have a better idea.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [May 6, 2020, 11:37am UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/13 "2020-05-06T11:37:35Z")

</div>

> [@mcculloch](#):
>
> if is/2 should be used or if it should use a custom predicate

One way is to know which things you define yourself and hand the rest to is/2 as it will complain if some function isn’t known. The other is the reverse using [current\_arithmetic\_function/1](https://www.swi-prolog.org/pldoc/doc_for?object=current_arithmetic_function/1)

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [May 6, 2020, 11:55am UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/14 "2020-05-06T11:55:00Z")

</div>

> [@mcculloch](#):
>
> Sorry, I’m still not good at formatting in this forum.

FYI

I changed your code so it formats correctly.

Instead of using the quote button `"` which puts a `>`at the start of each line, use the preformatted text button `</>` which bookends the selected text with `````.

```
```
resolve(N,N) :- integer(N), !.
```
```

which renders as

```prolog
resolve(N,N) :- integer(N), !.

```

NB The default programming language for preformatted text is Prolog, you can also manually chose another language that is configured for this site, see [Language highlighting](https://swi-prolog.discourse.group/t/language-highlighting/2260)

Don’t worry many new users are not aware of these features. 🙂

Also take a look at  
[Discourse New User Guide](https://meta.discourse.org/t/discourse-new-user-guide/96331)  
[Markdown help](https://superuser.com/editing-help)  
[Markdown tutorial](https://commonmark.org/help/tutorial/)  
[Language highlighting](https://swi-prolog.discourse.group/t/language-highlighting/2260)

---

<div class="post-metadata">

**Author:** ![mcculloch](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@mcculloch](https://swi-prolog.discourse.group/u/mcculloch)\
**Post date:** [May 6, 2020, 12:07pm UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/17 "2020-05-06T12:07:11Z")

</div>

Thanks Jan, current\_arithmetic\_function/1 is exactly what I need.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [May 6, 2020, 12:15pm UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/18 "2020-05-06T12:15:44Z")

</div>

> [@EricGT](#):
>
> manually append `prolog`

I thought `prolog` was the default? If not (and there can be a default), that should be the case. After all we are a Prolog forum 🙂

A little related, it is great we can now attach .pl file, only they are marked as Perl by the browser ☹ I think we need mime type `application/x-prolog`, but that may be for our self-hosted server …

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [May 6, 2020, 1:09pm UTC](https://swi-prolog.discourse.group/t/arithmetic-function-1/2268/19 "2020-05-06T13:09:18Z")

</div>

> [@jan](#):
>
> I thought `prolog` was the default?

Yes, Prolog is the default.

Had you not asked I would not have known this, thanks.

To verify this I did a little test and created the same preformatted text, one without `prolog` and one with `prolog` and then used the inspect HTML feature of an Internet browser, e.g. Chrome.

For both variations they modify the HMTL with

```
<code class="lang-prolog hljs">
</code>
```

Also checked that this was a Discourse option that can be set by admins.  
It is `Settings` → `Postings` → `default code lang`

> Default programming language syntax highlighting applied to GitHub code blocks (lang-auto, ruby, python etc.)

and it is set to `prolog`.
