[Aldor-l] [fricas-devel] Re: "has" in the interpreter
Ralf Hemmecke
ralf at hemmecke.de
Wed Nov 19 16:32:54 EST 2008
>> Q ==> Fraction Integer;
>> g: (Q, Q) -> Q := mygcd Q
>>
>> Should a compiler reject it or simply take the most specific code? Note,
>> that the compiler already knows that 'Q has Field'.
>>
>> Even for Aldor I don't think there is a specification for that. (Can
>> somebody prove me wrong?)
> The code is bad for two reasons:
>
> 1) it is hard to check statically due to overloading on type argument
> 2) the overloading is ambigous
>
> In current Spad the code is illegal because of 1 (Spad compiler
> can not handle overloading on type arguments). I believe that
> in Aldor it is illegal because of 2.
Now it depends on what you meant by 'code' above. So let me repeat:
---BEGIN aaa.as
#include "algebra"
#include "aldorio"
mygcd(R: Field)(a: R, b: R): R == {
if zero? a and zero? b then 0 else 1;
}
mygcd(R: EuclideanDomain)(a: R, b: R): R == {
while not zero? b repeat { (a, b) := (b, a rem b) }
return a;
}
#if Ambiguity
Q ==> Fraction Integer;
g: (Q, Q) -> Q := mygcd Q;
#elseif Field
Q: Field == Fraction Integer add;
g: (Q, Q) -> Q := mygcd(Q at Field);
#elseif ED
Q: EuclideanDomain == Fraction Integer add;
g: (Q, Q) -> Q := mygcd(Q at EuclideanDomain);
#else
Q ==> Fraction Integer;
g: (Q, Q) -> Q := +;
#endif
import from Integer, Q;
q4: Q := 4::Q;
q6: Q := 6::Q;
stdout << g(q4, q6) << newline;
---END aaa.as
So let's go through the different options.
>aldor -lalgebra -laldor -grun aaa.as
10
That is fine. The two mygcd have different types so there should be no
problem with defining two constants with different types.
>aldor -lalgebra -laldor -grun -D Ambiguity aaa.as
"aaa.as", line 12: g: (Q, Q) -> Q := mygcd Q;
..................^
[L12 C19] #1 (Error) There are 2 meanings for the operator `mygcd'.
Meaning 1: (R: Field) -> (a: R, b: R) -> R
Meaning 2: (R: EuclideanDomain) -> (a: R, b: R) -> R
Also clear. Q is at the same time a Field and a EuclideanDomain, so the
compiler does not have enough information to uniquely choose one of the
two functions.
>aldor -lalgebra -laldor -grun -D ED aaa.as
6
The interpretation of the result is a bit questionable, but that it
works could be agreed upon. I pass Q considered as a EuclideanDomain to
the function. There is only one mygcd that accepts a ED. The other one
requires Field.
>aldor -lalgebra -laldor -grun -D Field aaa.as
"aaa.as", line 15: g: (Q, Q) -> Q := mygcd(Q at Field);
..................^
[L15 C19] #1 (Error) There are 2 meanings for the operator `mygcd'.
Meaning 1: (R: Field) -> (a: R, b: R) -> R
Meaning 2: (R: EuclideanDomain) -> (a: R, b: R) -> R
At first I didn't like that so much, but if Q is considered to be a
Field then it is still a EuclideanDomain. So selection is ambiguous.
I've tried many more versions like
g: (Q, Q) -> Q := (mygcd@((R:Field)->(R,R)->R))(Q at Field);
fgcd(R: Field): (R, R)-> R == mygcd R;
fgcd(R: Field)(a: R, b: R): R == mygcd(R)(a,b);
All lead to the same ambiguity.
Conclusion must be: The definition of the two mygcd functions is OK, but
bad in the sense that I don't see any language construct to
unambiguously select the appropriate function.
Ralf
More information about the Aldor-l
mailing list