Spad issue: can't coerce Symbol to Expression(Integer)

5 views
Skip to first unread message

Neven Sajko

unread,
Aug 5, 2021, 10:01:07 AM8/5/21
to fricas...@googlegroups.com
I have the following package:


)abbrev package GGLC GeographicLibCoefficients
GeographicLibCoefficients() : Exports == Implementation where

L ==> List
I ==> Integer
P ==> PositiveInteger

EXPR ==> Expression(I)

RAT ==> Fraction(I)
PLY ==> Polynomial(RAT)
UPLY(var) ==> UnivariatePolynomial(var, RAT)
RATFUNC ==> Fraction(PLY)
TAY(var) ==> UnivariateTaylorSeries(EXPR, var, 0)

Exports ==> with

tau1_m_sigma: () -> EXPR

Implementation ==> add

maxpow(): P ==
8

tau1_m_sigma(): EXPR ==
integrand: EXPR := sqrt(1 + 'k2 * sin('sigma)^2)
I1: TAY('eps) := integrate(taylor(
subst(integrand,
'k2 = 4 * 'eps
/ (1 - 'eps)^2) *
(1 - 'eps),
'eps = 0),
'sigma)
I1_truncated: EXPR := reduce(_+,
[coefficient(I1, n) *
'eps^n for
n in 0..maxpow()])
A1: UPLY('eps) := simplify(subst(I1_truncated,
'sigma = 2*%pi) /
(2*%pi))
tau1: EXPR := I1_truncated / A1
tau1 - sigma


This is me trying to compile the package:


(1) -> )compile test.spad
Compiling FriCAS source code from file /tmp/spad_test/test.spad
using old system compiler.
GGLC abbreviates package GeographicLibCoefficients
------------------------------------------------------------------------
initializing NRLIB GGLC for GeographicLibCoefficients
compiling into NRLIB GGLC
compiling local maxpow : () -> PositiveInteger
GGLC;maxpow is replaced by 8
Time: 0.00 SEC.

compiling exported tau1_m_sigma : () -> Expression Integer
****** comp fails at level 7 with expression: ******
error in function tau1_m_sigma

(SEQ
(|:=| (|:| |integrand| (|Expression| (|Integer|)))
(|sqrt| (+ 1 (* '|k2| (^ | << | (|sin| '|sigma|) | >> | 2)))))
(|:=| (|:| I1 (|UnivariateTaylorSeries| (|Expression| (|Integer|)) '|eps| 0))
(|integrate|
(|taylor|
(* (|subst| |integrand| (= '|k2| (/ (* 4 '|eps|) (^ (- 1 '|eps|) 2))))
(- 1 '|eps|))
(= '|eps| 0))
'|sigma|))
(|:=| (|:| |I1_truncated| (|Expression| (|Integer|)))
(|reduce| +
(COLLECT (IN |n| (SEGMENT 0 (|maxpow|)))
(* (|coefficient| I1 |n|) (^ '|eps| |n|)))))
(|:=| (|:| A1 (|UnivariatePolynomial| '|eps| (|Fraction| (|Integer|))))
(|simplify|
(/ (|subst| |I1_truncated| (= '|sigma| (* 2 |%pi|))) (* 2 |%pi|))))
(|:=| (|:| |tau1| (|Expression| (|Integer|))) (/ |I1_truncated| A1))
(|exit| 1 (- |tau1| |sigma|)))
****** level 7 ******
$x:= (sin (QUOTE sigma))
$m:= $EmptyMode
$f:=
((((|integrand| #) (|maxpow| #) (* #) (+ #) ...)))

>> Apparent user error:
Cannot coerce (QUOTE sigma)
of mode (Symbol)
to mode (Expression (Integer))


I notice that Fricas says that it can't coerce a Symbol to an
Expression. Is that a bug, or do I really have to convert symbols into
expressions explicitly?


Thanks,
Neven

Ralf Hemmecke

unread,
Aug 5, 2021, 10:39:14 AM8/5/21
to Neven Sajko, fricas-devel
> I notice that Fricas says that it can't coerce a Symbol to an
> Expression. Is that a bug, or do I really have to convert symbols into
> expressions explicitly?

A difference between interpreter and compiler is that the interpreter
tries to find some coercion or retraction function and applies them
implicitly. That makes the life of the user usually easier, but the
interpreter can make wrong guesses of your intentions.

The compiler does such an coercion only in very simple cases otherwise
YOU must provide the right conversion of the object into the respective
type. And this is a good thing to require programmers to be explicit.
Suppose in the future there would be a little change in the FriCAS
library so that (if you were not explicit) the compiler would choose
another coercion function. That shouldn't actually matter much since
coercions are supposed to be injections, but being an injection is just
a convention and is not forced by the compiler.

My suggestion is that you should always be as explicit as possible. You
will appreciate it when you read you program after 5 years.

Ralf

Neven Sajko

unread,
Aug 5, 2021, 11:17:58 AM8/5/21
to Ralf Hemmecke, fricas-devel
(That last sigma in my code should be 'sigma, instead. But that seems
unrelated to the issue I'm currently facing.)

Well, I can't seem to solve this. I tried replacing sin('sigma) with both
sin(('sigma)@EXPR)
and
sin(('sigma)::EXPR)
but nothing seems to help, I still get about the same diagnostic error.

Any pointers, please?


Thanks,
Neven

Ralf Hemmecke

unread,
Aug 5, 2021, 1:52:03 PM8/5/21
to Neven Sajko, fricas-devel
Hi Neven,

as a general remark, I suggest to you that you make simpler expressions.
Then you have better chances to find the problem, see attachment.

Maybe the two lines

error "ENDE"
)fin

in that file may help you in the future to nail down where exactly the
error is.

As you have seen, I have split up the expressions into simpler pieces
and added the respective type information.

But when it comes to

I1: TAY(eps) := integrate(t, sigma)

I must stop, because I have also other things to do and do not know an
easy solution for it. Maybe, since t is a Taylor series over EXPR, you
can simply first truncate and after that integrate wrt. sigma.

I hope, with that bit of code you may be able to continue yourself.

Good luck.

Ralf
neven.spad

Waldek Hebisch

unread,
Aug 5, 2021, 2:10:14 PM8/5/21
to fricas...@googlegroups.com
FriCAS library uses:

kernel(s)::F

where s is symbol (in library it is variable) and F is appropriate
expression domain (usually Expression(Integer)).

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