Ralf Hemmecke wrote:
>
> Oh... I first thought that I do something wrong, but that looks more
> like a bug.
>
> I now extend FreeModuleCategory by
>
> if S has with (coerce: S -> Vector NonNegativeInteger) then
> totalDegree(p: %): NonNegativeInteger ==
> V ==> Vector NonNegativeInteger
> zero? p => 0
> "max"/[reduce("+", s::V, 0) for s in support p]
>
> However, the correct way should be
>
> if S has with (coerce: % -> Vector NonNegativeInteger) then
>
> There is a % instead of S right after "coerce".
> Am I wrong?
>
> The version with the S compiles find, but I wonder whether it really
> does what I intend.
Well, both version mean the same, but IMO the one using S is much
clearer. In particular compiler may miss the fact that both
are equivalent. Also, version using % is more likely to hit
compiler bugs (note that normally % means domain which is
currently compiled, while here % means S, so compiler have
to correcly change view...).
Also, you should use the same condition in category part
and in implementation part -- that way compiler needs to
chack that conditions are the same and correctly implement
conditions at runtime. With different conditions compiler
have to prove that condtion in category part implies one
in implementation part. Even if such implication is true
compiler may be unable to do the proof.
> The version with the % gives the error below. I wonder where the
> compiler gets the "A" from, because in the code of FreeModuleCategory
> there is no A.
>
> augmenting S: (SIGNATURE S coerce ((Vector (NonNegativeInteger)) A))
> compiling exported totalDegree : A -> NonNegativeInteger
> processing macro definition V ==> Vector NonNegativeInteger
> ****** comp fails at level 7 with expression: ******
> error in function totalDegree
>
> (SEQ (MDEF (V) (NIL) (NIL) (|Vector| (|NonNegativeInteger|)))
> (LET (|:| #1=#:G2594 (|Boolean|))
> (|zero?| |p|))
> (|exit| 1
> (IF #1#
> 0
> (REDUCE |max| 0
> (COLLECT (IN |s| (|support| |p|))
> (|reduce| "+" (|::| |s| V) 0))))))
> ****** level 7 ******
> $x:= (:: s (Vector (NonNegativeInteger)))
> $m:= $EmptyMode
> $f:=
> ((((|s| # #) (|last| #) (|rest| #) (|first| #) ...)))
>
> >> Apparent user error:
> Cannot coerce s
> of mode S
> to mode (Vector (NonNegativeInteger))
>
Executable code for categories needs extra parameter to
represent current domain. More precisely, inside code
variable named $ represents categpry. However, code from
default part executes in behalf of some domain. This
domain is passed as an extra hidden argument to the
category constructor. Spad compiler has special logic
to choose name of this extra argument to avoid conflict
with existing name. In this case Spad compiler uses A
as name of this extra parameter.
Actully the messages above show possible problem with %:
apparently Spad compiler replaced % in
if S has with (coerce: % -> Vector NonNegativeInteger) then
by A. In other words, Spad compiler thinks that this % is
the same as % in
totalDegree(p: %): NonNegativeInteger
which is wrong. Writing
if S has with (coerce: S -> Vector NonNegativeInteger) then
avoids such problems.
--
Waldek Hebisch
--
You received this message because you are subscribed to the Google Groups
"FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.