function depending on domain

7 views
Skip to first unread message

Ralf Hemmecke

unread,
Dec 7, 2025, 10:12:26 AM (3 days ago) Dec 7
to fricas-devel
Hi Waldek,

I am about to design a function that depends on a domain and a few other
parameters.

Cat: Category == with
foo?: Integer -> Boolean
..
D1: Cat == add ...
D2: Cat == add ...

Dom(): with
bar?: (Cat, Integer) -> Integer
add
blah(x: %): Integer == ...
bar(D: Cat, n: Integer, x: %): Integer ==
k: Integer := blah(x, n)
if (foo? $ D)(k) then 1 else 0

I am pretty sure that this is possible. I just want to ask whether from
your point of view this causes any speed penalties?

In my current idea I would not want to put that dependency into the
arguments of Dom. It is rather that "bar" counts as an additional
feature that is not so important. I also don't want to move bar to a
separate package.

Thank you in advance
Ralf

Waldek Hebisch

unread,
Dec 7, 2025, 12:33:21 PM (3 days ago) Dec 7
to 'Ralf Hemmecke' via FriCAS - computer algebra system
On Sun, Dec 07, 2025 at 04:12:21PM +0100, 'Ralf Hemmecke' via FriCAS - computer algebra system wrote:
> Hi Waldek,
>
> I am about to design a function that depends on a domain and a few other
> parameters.
>
> Cat: Category == with
> foo?: Integer -> Boolean
> ..
> D1: Cat == add ...
> D2: Cat == add ...
>
> Dom(): with
> bar?: (Cat, Integer) -> Integer
> add
> blah(x: %): Integer == ...
> bar(D: Cat, n: Integer, x: %): Integer ==
> k: Integer := blah(x, n)
> if (foo? $ D)(k) then 1 else 0
>
> I am pretty sure that this is possible. I just want to ask whether from your
> point of view this causes any speed penalties?

Yes. Normally result of lookup for functions are cached. But such
construct means that before each call insted of actually calling
'foo?' FriCAS runtime will first need to do full lookup, which costs
you some milliseconds. or tens of milliseconds. Negligible if you
do a single call. Perfomance killer if done in a tight loop.

Theoretically there is still some possibility for caching, but
currently variable domain is Spad means full lookup for each call.

> In my current idea I would not want to put that dependency into the
> arguments of Dom. It is rather that "bar" counts as an additional feature
> that is not so important. I also don't want to move bar to a separate
> package.
>
> Thank you in advance
> Ralf

--
Waldek Hebisch

Ralf Hemmecke

unread,
Dec 7, 2025, 7:28:16 PM (3 days ago) Dec 7
to fricas...@googlegroups.com
Dear Waldek,

thank you for your answer.

How is the situation for a definition

baz?: Cat -> Integer -> Integer
baz?(D: Cat): Integer -> Integer ==
(n: Integer, x: %): Integer +->
k: Integer := blah(x, n)
if (foo? $ D)(k) then 1 else 0

and in places where I use baz?, I do it like this

fun: (%, Integer) -> Integer := baz?(D1)
for i in 1..10 repeat res := res + fun(x, i)

would that still cost more or do I (with this assignment) prevent the
multiple lookups of foo? $ D1. I think

Ralf

Ralf Hemmecke

unread,
Dec 9, 2025, 2:57:25 AM (yesterday) Dec 9
to fricas...@googlegroups.com
Dear Waldek,

it seems that I was too hopeful.

Unfortunately, the attached program give the following output.
It seems that the interpreter is unable to check a domain-evaluating
function parameter for its correct type. :-(

Can this be easily fixed?

Thank you
Ralf

===================================================

%%% (1) -> d: Dom := 1

(1) 1

Type: Dom
%%% (2) -> [baz1(3)(d), baz2(5)(d)]

(2) [1, 2]
Type: List(PositiveInteger)
%%% (3) -> bar?(D1,3)(d)
There are 1 exposed and 0 unexposed library operations named bar?
having 2 argument(s) but none was determined to be applicable.
Use HyperDoc Browse, or issue
)display op bar?
to learn more about the available operations. Perhaps
package-calling the operation or using coercions on the arguments
will allow you to apply the operation.

Cannot find a definition or applicable library operation named bar?
with argument type(s)
Type
PositiveInteger

Perhaps you should use "@" to indicate the required return type,
or "$" to specify which version of the function you need.

%%% (3) -> (bar?$Dom)

(3) theMap(DOM;bar?;CIM;2,45)
Type: ((Cat, Integer) -> (Dom -> Boolean))
%%% (4) -> (bar?$Dom)(D1,4)
There are 1 exposed and 0 unexposed library operations named bar?
having 2 argument(s) but none was determined to be applicable.
Use HyperDoc Browse, or issue
)display op bar?
to learn more about the available operations. Perhaps
package-calling the operation or using coercions on the arguments
will allow you to apply the operation.

Cannot find a definition or applicable library operation named bar?
with argument type(s)
Type
Integer

Perhaps you should use "@" to indicate the required return type,
or "$" to specify which version of the function you need.

foo1.spad
Reply all
Reply to author
Forward
0 new messages