doubly executed code

14 views
Skip to first unread message

Ralf Hemmecke

unread,
Nov 13, 2024, 6:11:54 AM11/13/24
to fricas-devel
The output of the following function is strange.
Why is dbgPrint executed twice?

%%% (9) -> fooo(2)
Compiling function bar with type (Integer, Integer) -> List(Integer)

Compiling function fooo with type Integer -> Integer
[:> , ab, [1, 2]]
[:> , ab, [1, 2]]

(9) 2
Type: PositiveInteger

NB: If I replace the definition of fooo by

fooo(n: ZZ): ZZ == (_
n < 2 => error "argument must be > 2";_
l := bar(1, n);_
l.2)

the double execution goes away.

Anyway, maybe my assignment of variables inside a list "construct"
expression on the left-hand side is not conforming SPAD rules, but I
would expect the interpreter either to shout at me or not do the work
(function calling) twice. (Or maybe it is only output twice.)

Actually, why is it two times and not three times? It is a list of length 3.

Ralf

foo.input

Waldek Hebisch

unread,
Nov 13, 2024, 7:17:57 PM11/13/24
to 'Ralf Hemmecke' via FriCAS - computer algebra system
On Wed, Nov 13, 2024 at 12:11:50PM +0100, 'Ralf Hemmecke' via FriCAS - computer algebra system wrote:
> The output of the following function is strange.
> Why is dbgPrint executed twice?
>
> %%% (9) -> fooo(2)
> Compiling function bar with type (Integer, Integer) -> List(Integer)
>
> Compiling function fooo with type Integer -> Integer
> [:> , ab, [1, 2]]
> [:> , ab, [1, 2]]
>
> (9) 2
> Type: PositiveInteger

> fooo(n: ZZ): ZZ == (_
> n < 2 => error "argument must be > 2";_
> [P1n, Q1n, R1n] := bar(1, n);_
> Q1n)

Concerning reasons, in interpreter such assignment uses pattern
matching. 'bar' is evaluated once to get argument to pattern
matching function. But assignment also produces a value and
'bar' is evaluated second time to get (ignored) return value
of the assignment.

ATM I am not sure if it is easy or difficult to fix, just I am
answering the question ('why').

--
Waldek Hebisch

Ralf Hemmecke

unread,
Nov 14, 2024, 12:36:50 PM11/14/24
to fricas...@googlegroups.com
Dear Waldek,

On 11/14/24 01:17, Waldek Hebisch wrote:
> ATM I am not sure if it is easy or difficult to fix, just I am
> answering the question ('why').

Thank you for this explanation. It helps already.

Ralf

Waldek Hebisch

unread,
May 16, 2025, 10:37:06 PMMay 16
to fricas...@googlegroups.com
On Thu, Nov 14, 2024 at 01:17:54AM +0100, Waldek Hebisch wrote:
> On Wed, Nov 13, 2024 at 12:11:50PM +0100, 'Ralf Hemmecke' via FriCAS - computer algebra system wrote:
> > The output of the following function is strange.
> > Why is dbgPrint executed twice?
> >
> > %%% (9) -> fooo(2)
> > Compiling function bar with type (Integer, Integer) -> List(Integer)
> >
> > Compiling function fooo with type Integer -> Integer
> > [:> , ab, [1, 2]]
> > [:> , ab, [1, 2]]
> >
> > (9) 2
> > Type: PositiveInteger
>
> > fooo(n: ZZ): ZZ == (_
> > n < 2 => error "argument must be > 2";_
> > [P1n, Q1n, R1n] := bar(1, n);_
> > Q1n)
>
> Concerning reasons, in interpreter such assignment uses pattern
> matching. 'bar' is evaluated once to get argument to pattern
> matching function. But assignment also produces a value and
> 'bar' is evaluated second time to get (ignored) return value
> of the assignment.

I have now commited fix for this to the trunk.

--
Waldek Hebisch
Reply all
Reply to author
Forward
0 new messages