[Aldor-l] [fricas-devel] Re: "has" in the interpreter
Ralf Hemmecke
ralf at hemmecke.de
Wed Nov 19 12:31:56 EST 2008
> I do not see any immediate need for reflection (self description) in a
> mathematical library [snip]
Good.
> Programmers might use reflection in order to avoid having to consider
> the most general cases. Look at the following code in
> 'src/algebra/numsolve.spad.pamphlet':
I'd say that is not the best piece of code.
> numeric(r:K):F ==
> K is I =>
> F is Float => r::I::Float
> F is RN => r::I::RN
> F is CF => r::I::CF
> F is GRN => r::I::GRN
> K is GI =>
> gr:GI := r::GI
> F is GRN => complex(real(gr)::RN,imag(gr)::RN)$GRN
> F is CF => convert(gr)
> error "case not handled"
K is I => r::I::F
for the first part would probably have worked fine and even covers more
cases.
But it shows that 'is'/'has' might lead to lots of branches and thus the
code doesn't scale very well.
But look at
mygcd(R: EuclideanDomain)(a: R, b: R): R == {
R has Field => if zero? a and zero? b then 0 else 1;
while not zero? b repeat { (a, b) := (b, a rem b) }
return a;
}
That I consider a case where 'has' is useful.
I actually wonder what will happen if I define two functions:
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;
}
and then try to say:
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?)
> So my conclusion in the end is that the use of reflection is (almost
> always) undesirable and usually indicates the presence of some other
> deficiency in the language.
+1
Ralf
More information about the Aldor-l
mailing list