I wrote:
>
> Ralf Hemmecke wrote:
> >
> > My intention is to write two functions whose signatures differ only in
> > the type of the second argument.
> >
> > I surely could use another function name, but I think it should also
> > work as I have coded it below. But I couln't make that work with SPAD.
> > While the Aldor compiler obviously produces code that seems to work, see
> > below.
> > ======================================================================
> > )abbrev package FOO Foo
> > N ==> NonNegativeInteger
> > Z ==> Integer
> > Foo(R: Ring): with
> > foo: (R, N) -> R
> > if R has Field then foo: (R, Z) -> R
> > == add
> > import from N
> > foo(r: R, n: N): R == r^n
> > if R has Field then
> > foo(r: R, n: Z): R ==
> > n < 0 => inv foo(r, qcoerce(-n)@N)
> > foo(r, qcoerce(n)@N)
> >
> >
> >
> > (1) -> foo(2,3)
> >
> > (1) 8
> > Type:
> > PositiveInteger
> > (2) -> foo(2,-3)
> > Compiling function G732 with type Integer -> Boolean
> > C-c C-c
> > >> System error:
> > Interactive interrupt at #x100759B7E8.
> >
> > I had to interrupt the function compilation. OK, without qualification,
> > I could live with it. But in a fresh FriCAS I then get
> >
> > (1) -> foo(2, -3)$Foo(Fraction Integer)
> >
> > and my CPU runs crazy. Looks like an endless loop.
> >
> > Does that mean that SPAD forces me to choose two different names for the
> > two exported functions? That must be a bug.
>
> AFAICS you hit Spad feature called operation subsumption.
I looked deeper and it seems that the problem has nothing
to do with operation subsumption. Simply, there are two
availble signatures for 'foo', one is '(R, R, NNI) -> R'
another is '(R, R, Z) -> R'. When compiling call to 'foo'
Spad compiler takes first applicable signature. Compiler
data structures work in such a way that the second
signature, that is '(R, R, Z) -> R' is tried first.
Since it applies compiler does not look at the other
signature...
In this case we could check all signatures and look for
best match. But in case of subtyping, overload resolution
always will be error prone. Conider call bar(n, n, n)
with all three arguments of type NNI. Now, assume that
we have:
bar : (NNI, Z, Z) -> Z
bar : (Z, NNI, NNI) -> Z
bar : (NNI, Z, NNI) -> Z
You could claim that the last two are better match than the
first one, but it is hard to give reason to prefer one of
the last two to another. And if we use lexicographic
order, than suddenly first one is be better than the
second one. Of course, it is natural to prefer exact
match to matching a supertype. But partial matches are
a can of worms...
--
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 http://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.