# When are functional notation of dict compile time expanded?

**URL:** <https://swi-prolog.discourse.group/t/when-are-functional-notation-of-dict-compile-time-expanded/9146>\
**Category:** General\
**Created:** [August 1, 2025, 8:08pm UTC](https://swi-prolog.discourse.group/t/when-are-functional-notation-of-dict-compile-time-expanded/9146 "2025-08-01T20:08:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kwon-young](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/kwon-young/32/4841_2.png) [@kwon-young](https://swi-prolog.discourse.group/u/kwon-young)\
**Post date:** [August 1, 2025, 8:08pm UTC](https://swi-prolog.discourse.group/t/when-are-functional-notation-of-dict-compile-time-expanded/9146/1 "2025-08-01T20:08:26Z")

</div>

Hello,

I tried to use the library `arithmetic_types` which does compile time expansion of user defined arithmetic predicates with dicts but it doesn’t work.  
Basically, compile time expansion of dicts apply earlier than arithmetic\_types, which blocks arithmetic\_types to do its own compile time expansion.

Here is a more detailed example: [Arithmetic type and swi-prolog dicts mix badly · Issue #8 · ridgeworks/arithmetic\_types · GitHub](https://github.com/ridgeworks/arithmetic_types/issues/8)

```prolog
:- use_module(library(arithmetic_types)).
:- arithmetic_function('.+'/2).

test_pred(Y) :-
   X = array{}.arange(10),
   Y is (X .+ X).to_list().

```

And the resulting program:

```prolog
?- listing(test_pred).
array:test_pred(Y) :-
    '.'(array{}, arange(10), A),
    X=A,
    '.'(X.+X, to_list(), B), % X .+ X should have expanded into its own goal
    Y is B.

```

Would this problem be fixable ?

---

<div class="post-metadata">

**Author:** ![ridgeworks](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ridgeworks/32/886_2.png) [@ridgeworks](https://swi-prolog.discourse.group/u/ridgeworks)\
**Post date:** [August 4, 2025, 6:42pm UTC](https://swi-prolog.discourse.group/t/when-are-functional-notation-of-dict-compile-time-expanded/9146/2 "2025-08-04T18:42:43Z")

</div>

I answered this on the referenced GitHub issue, but in case there’s a wider community of interest:

`library(arithmetic)` uses goal expansion to support user defined arithmetic functions. In general terms it lifts any user defined functions out of the `is` (or related arithmetic comparisons) but leaves things it deems “evaluable”, like numbers, character codes, etc. alone. The ‘`.`’ functional notation is deemed evaluable, so no further internal expansion of any arguments is done. The entire ‘`.`’ sub-expression is actually lifted and expanded by the dictionary support code, which explains the actual code listed after expansion. At least, that’s my understanding.

---

<div class="post-metadata">

**Author:** ![kwon-young](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/kwon-young/32/4841_2.png) [@kwon-young](https://swi-prolog.discourse.group/u/kwon-young)\
**Post date:** [August 5, 2025, 6:08am UTC](https://swi-prolog.discourse.group/t/when-are-functional-notation-of-dict-compile-time-expanded/9146/3 "2025-08-05T06:08:06Z")

</div>

I am responding here instead of in the github issue since this is more of a discussion and so that other people can weigh in.

You are right.  
Trying to comment [these lines](https://github.com/ridgeworks/arithmetic_types/blob/4a2173c19e49601e54ed20d777cfa61c5d9fdd6c/prolog/arithmetic_types.pl#L294-L296):

```prolog
evaluable(Func) :- % Functional notation.
	functor(Func, ., 2),
	!.

```

makes code like this:

```prolog
:- use_module(library(arithmetic_types), []).

main(A, B) :-
   A is B+1.

```

go into a stack overflow at compile time:

```prolog
ERROR: /home/kwon-young/prog/array/test_module.pl:3:
ERROR: Stack limit (1.0Gb) exceeded
ERROR: Stack sizes: local: 6Kb, global: 0.8Gb, trail: 0Kb
ERROR: Stack depth: 35,975,343, last-call: 100%, Choice points: 19
ERROR: In:
ERROR: [35,975,343] arithmetic_types:evaluable(_215852598)
ERROR: [51] '$apply':forall('<garbage_collected>', <compound (:)/2>)
ERROR: [50] arithmetic_types:evaluable('<garbage_collected>')
ERROR: [49] arithmetic_types:do_expand_function(<compound (+)/2>, <compound (+)/2>, true)
ERROR: [48] arithmetic_types:expand_function('<garbage_collected>', '<garbage_collected>', _215852698)

```

The weird thing is I have no idea why the ‘.’ functional notation is even used here ?

---

<div class="post-metadata">

**Author:** ![ridgeworks](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ridgeworks/32/886_2.png) [@ridgeworks](https://swi-prolog.discourse.group/u/ridgeworks)\
**Post date:** [August 5, 2025, 2:01pm UTC](https://swi-prolog.discourse.group/t/when-are-functional-notation-of-dict-compile-time-expanded/9146/4 "2025-08-05T14:01:53Z")

</div>

> [@kwon-young](#):
>
> makes code like this:
> 
> ```prolog
> :- use_module(library(arithmetic_types), []).
> 
> main(A, B) :-
> A is B+1.
> 
> ```
> 
> go into a stack overflow at compile time:

The goal expansion of `is` still has to test for ‘`.`’ even though it may not be present in the source expression. The overflow occurs because the original clause you commented out prevented reaching a subsequent clause with an unexpected argument type, and I should close that loophole.

Of course you’re free to modify the code but you should be prepared to debug the result.

---

<div class="post-metadata">

**Author:** ![kwon-young](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/kwon-young/32/4841_2.png) [@kwon-young](https://swi-prolog.discourse.group/u/kwon-young)\
**Post date:** [August 5, 2025, 6:31pm UTC](https://swi-prolog.discourse.group/t/when-are-functional-notation-of-dict-compile-time-expanded/9146/5 "2025-08-05T18:31:59Z")

</div>

Ah, thank you.  
I didn’t know if I was hitting a bug in arithmetic\_types or if it was by design.

I’ll try to experiment on my side to see if I can correctly integrate dict functional notation with arithmetic\_types.  
If I manage to find something useable, I will let you know.
