JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.47k stars 5.46k forks source link

v0.5 "cannot add methods to an abstract type" when overriding call #14919

Closed dlfivefifty closed 5 years ago

dlfivefifty commented 8 years ago

I'm updating ApproxFun and this caught me by surprise:

abstract Foo
julia> (f::Foo)(x)=x
ERROR: cannot add methods to an abstract type
 in eval(::Module, ::Any) at ./boot.jl:267

Is this intentional? Is there a way to get around this other than define for each subtype:

julia> immutable CFoo <: Foo end
julia> (f::CFoo)(x)=x
JeffBezanson commented 8 years ago

Yes, unfortunately this is the only capability I could not figure out how to cleanly preserve in #13412. How bad is this for you?

dlfivefifty commented 8 years ago

Not bad actually (only 3 concrete subtypes), but thought I’d double check before working around it.

vtjnash commented 8 years ago

grepping the source, it looks like the (only) problematic definition was: https://github.com/ApproxFun/ApproxFun.jl/blob/cb062d6714412bd342cd7e4682ba7ff9f788b40a/src/Multivariate/Multivariate.jl#L7

wbhart commented 8 years ago

We are hitting this. What is excluded here?

Previously, to define a function for all types that are subtypes of a given abstract type, this would be precisely the syntax used. That seems like a rather fundamental part of Julia.

Or is it only when this syntax is used in combination with an object call overload that is excluded?

Losing that would be rather a blow to us. As you may know, we cannot store all the information we need to define mathematical rings in the type, so we create objects that stand in as types, e.g.

R, x = PolynomialRing(ZZ, "x")

where R is now an object that contains information about the ring of all polynomials in ZZ[x].

However, the mathematical abstraction of a polynomial ring is implemented via a bunch of Julia types, all of which are subtypes of some abstract type.

So it's natural to define methods for all of them at once by overloading the call syntax, e.g.

R() returns 0 in the given ring, or R(1) coerces the integer 1 into the ring.

Before panicking I'd like to understand the change a bit better, and what workarounds there are. But this seems to be a bit of a worrisome and unexpected change.

KristofferC commented 8 years ago

I thought call just changed syntax: https://github.com/JuliaLang/julia/commit/0778a89ddfdbb3098cc42b15e86f4cc4c610f449#diff-5cb043c25dee87c0787a9e1bbbf935baL17

wbhart commented 8 years ago

I think I understand what is happening now. Rather than someone replying in detail, perhaps I can ask if my understanding is correct. I might also take the opportunity to discuss a very related issue which we are hitting, in case it is useful to know about (see further below).

I think call is changing syntax and rather than only being defined for types, it is still defined for objects. The comment in the PR about deprecating call and putting method tables inside types instead refers to implementing that which is formerly known as "call" by putting method tables inside types. This doesn't mean call will be restricted to types, but that objects of a given type will have their call syntax implemented by putting method tables inside the corresponding types. The advantage will be faster anonymous methods. Is this correct?

The only thing disappearing is the former ability to define a method for a given class of types, i.e. all types which are subtypes of a given abstract type. Is this because "call" is now a method and not a generic function?

One place where this will definitely affect us badly is when we want to write a single method that overloads call for all elements of all polynomial rings. E.g. the user may create at runtime R = ZZ[x], S = R[y], T = S[z]. The user may then create polynomials f, g, h each in one of these rings R, S or T. Each of the polynomials f, g, h necessarily has a different type, though all the types belong to the abstract type PolyElem{T} (where T is the type of the coefficients).

We use call to enable things like f(123) to give the value of the polynomial at 123, or f(M) to substitute a matrix M into the polynomial f, etc. It has to be a generic function because the user could in theory substitute absolutely anything into f, g or h.

It's therefore a very serious breakage that we can't overload call for all of these at once using call(R::PolyElem{T}, a).

I would also be quite concerned if a similar thing happened for overloading the array syntax []. We currently write generic functions to overload [] for our objects R, S, T above so that for example U = R["x"] is possible. This creates a polynomial ring over R with variable "x". We use it similarly for matrices.

On a related note, I'd like to take the opportunity to discuss an issue which we hit which we have been surprised has not hit anyone else yet. We accept there is likely nothing that can be done about this, but I want to mention it here in case it helps with future design decisions.

Consider the example of matrices over Z/nZ for multiprecision integer n. The initial thought is to have objects of type Z/nZ. As you know, putting n as a parameter of the type is not really possible, nor desirable (otherwise it would trigger recompilation again and again in multimodular algorithms that used many different values of n).

Therefore, the only option is to have the n in an object, not in the type. Let's call the type in question modint. So objects of type modint will somehow have the residue r associated with them, but also in some way the modulus n.

But now consider Julia's generic algorithms for matrices over a given type. Many of these algorithms require one to be able to create a zero object to insert into the matrix. But the zero function in Julia only takes the type modint as a parameter.

The problem is, the type modint contains no information about n, so it is in fact not able to create the zero object of this type, since it cannot set n.

I'm actually really surprised no one has hit this issue anywhere else. For this reason it would be really useful if functions like zero and one that are used so pervasively in Julia matrix algorithms could take more information from somewhere.

Note that the n really can't be stored in the type itself, since then things would need recompiling for every n.

As things are, we are not able to use any of the Julia generic matrix functionality in Nemo.

The only solution that I can see is to explicitly support using objects in the place of types in Julia, so that things like zero(R) will be used by Julia when constructing arrays over "parents" [1] like R which aren't implemented as types but as objects, e.g. arrays over R = IntegersMod(7).

I mention this only because it directly relates to the call syntax and calling of objects as though they were types.

In fact, in computer algebra, the distinction becomes blurred. Consider ideals A and B of a ring R. We can certainly perform arithmetic operations on A and B such as A*B. So in one sense they behave like objects or values. But we can also think of ideals as sets of elements in the same way that ZZ or a polynomial ring is. So they can also act as "parents". For example, the element 0 of an ideal could be obtained by zero(A) or A().

Of course we can define zero(A) and A() no problems. But this isn't the way the Julia generic matrix algorithms generate a zero object. They will use zero(typeof(A)) which is not sufficient to construct the zero object of "type" (actually parent) A.

As I say, I'm just mentioning it here since it seems like an opportune moment to do so. I'm not expecting anything, just hoping that it might be of use in future planning.

[1] "parents" is the name computer algebraists give to the objects that represent mathematical types such as polynomial rings or Z/nZ that can't be fully modeled using actual types in the programming language of choice.

tknopp commented 8 years ago

Is there a workaround for this? I was so happy to see this happening in 0.4 and now its gone again. In Gtk.jl this would have allowed to remove a lot of macros that are currently used to create widgets. https://github.com/JuliaLang/Gtk.jl/issues/134

vtjnash commented 8 years ago

Gtk doesn't need this. In particular, the limitation is that you can't call an instance of a widget, but you can still define constructors for abstract subclasses.

JeffreySarnoff commented 8 years ago

Many things not yet done in Julia need to add methods to an abstract type.

If this capability were to become absent, fundamental expressiveness would be curtailed. It is already too hard to gather together multifaceted realization as an abstraction, and to project from intrinsically coherent concept through abstraction into specific entityhoods.

Moreover, it is a substantial boon to clear human communication about Julia code.

JeffreySarnoff commented 8 years ago

@JeffBezanson @wbhart @vtjnash For example, it has been an essential strength of Julia that one could think and code:

The norm of a number that belongs to a division algebra is the square root of the product of that number with the conjugate of that number.

import Base: conj, *, sqrt

abstract DivisionAlgebraNum <: Number
norm{T<:DivisionAlgebraNum}(x::T) = sqrt( x * conj(x) )

and then define specific types, knowing norm(x) does 'just work'.


immutable QuaternionNum{T<:Real} <: DivisionAlgebraNumber
   r::T;   i::T;   j::T; k::T
end
conj{T<:Real}(x::QuaternionNum{T}) = ...
*{T<:Real}(x::QuaternionNum{T}, y::QuaternionNum{T}) = ...
sqrt{T<:Real}(x::QuaternionNum{T}) = ...
...

immutable OctonionNum{T<:Real} <: DivisionAlgebraNumber ...
immutable ComplexNum{T<:Real} <: DivisionAlgebraNumber ...
immutable RealNum{T<:Real} <: DivisionAlgebraNumber ...
wbhart commented 8 years ago

@JeffreySarnoff There's no evidence ordinary generic functions like your norm function will stop working is there?

JeffBezanson commented 8 years ago

Yes, definitions like that still work fine. The change only affects what used to be the first argument of call.

JeffreySarnoff commented 8 years ago

So does this mean that I would not run into this using abstract types and generics on them to code some category theory as an abstraction and then use that to define some basic categories as working types?

wbhart commented 8 years ago

I suspect you would definitely hit it if you tried to define functors using (the replacement of) the call syntax. This could happen if your functors were treated as objects that had properties, which as you know happens. This is actually a very good example.

wbhart commented 8 years ago

One other area where this functionality is really needed is homological algebra. I'd be surprised if it wasn't also extremely valuable to homotopy theory and to group actions in representation theory.

dlfivefifty commented 8 years ago

I've changed the title of the issue as it may have been confusing people

wbhart commented 8 years ago

Just to clarify, my concerns are not theoretical, but practical. Our code broke because of the syntax change, but some of the call overloads we had are no longer possible at all. We have lost actual features because of this change.

StefanKarpinski commented 8 years ago

Can you post the definitions that no longer work?

wbhart commented 8 years ago

The first one we noticed was:

call{T <: RingElem}(f::PolyElem{T}, a) = subst(f, a)

This is for evaluating polynomials at arbitrary things, e.g. elements of other rings or at matrices, etc.

Note PolyElem is an abstract type acting as a type class for all polynomial types in Nemo.

There are various specialisations of this where "a" is a specific type. I'll omit these since they are of the same kind.

(There will be similar things for PowerSeriesElem instead of PolyElem.)

Here are some specific examples from Hecke.jl:

call(O::GenNfOrd, a::nf_elem, check::Bool = true)

This function is for coercing a number field element into the ambient number field of an order.

There's about a dozen similar examples for coercing various things into such. This is done because there are multiple different number field order types.

There is also:

call(M::Map, a::Any)

This is for applying any kind of map in Hecke to anything. Map is an abstract type to which many types belong. Our map objects contain data about the maps.

The guy writing Singular.jl says he has a problem with this change too, but I don't have explicit examples from his code right now. He basically said he used this generic call thing to reduce code duplication. He's modelling a system written in C that uses dynamic types, and instead of duplicating everything for each individual type, he has a generic implementation that he specialises per type.

My biggest concern actually is the project I was about to work on which was an object model for group theory and commutative algebra. I produced a very small prototype some months back which relies heavily on call. I just had a look now and miraculously the prototype doesn't yet make use of abstract types for the first argument. But there's no doubt it will. And this is very worrisome as I just convinced some colleagues to use Julia for this precisely on this basis. I'm visiting them next week as it happens to thrash out the details.

Consider homomorphisms between many different types of groups. The homomorphisms are modeled as objects that can be called. All the homomorphism types will belong to an abstract type, and a fundamental part of the mechanism that allows propagation of new knowledge along the homomorphisms requires generic overloading of call for all homomorphisms (similar to the Map example above).

yuyichao commented 8 years ago

Hmm, so, what was it?

wbhart commented 8 years ago

Sorry, I accidentally pressed enter whilst writing the post. If you look at the post on GitHub itself you will see I've filled in what I wanted to write now.

StefanKarpinski commented 8 years ago

The post is empty.

wbhart commented 8 years ago

Well I see it, but here it is again:

The first one we noticed was:

call{T <: RingElem}(f::PolyElem{T}, a) = subst(f, a)

This is for evaluating polynomials at arbitrary things, e.g. elements of other rings or at matrices, etc.

Note PolyElem is an abstract type acting as a type class for all polynomial types in Nemo.

There are various specialisations of this where "a" is a specific type. I'll omit these since they are of the same kind.

(There will be similar things for PowerSeriesElem instead of PolyElem.)

Here are some specific examples from Hecke.jl:

call(O::GenNfOrd, a::nf_elem, check::Bool = true)

This function is for coercing a number field element into the ambient number field of an order.

There's about a dozen similar examples for coercing various things into such. This is done because there are multiple different number field order types.

There is also:

call(M::Map, a::Any)

This is for applying any kind of map in Hecke to anything. Map is an abstract type to which many types belong. Our map objects contain data about the maps.

The guy writing Singular.jl says he has a problem with this change too, but I don't have explicit examples from his code right now. He basically said he used this generic call thing to reduce code duplication. He's modelling a system written in C that uses dynamic types, and instead of duplicating everything for each individual type, he has a generic implementation that he specialises per type.

My biggest concern actually is the project I was about to work on which was an object model for group theory and commutative algebra. I produced a very small prototype some months back which relies heavily on call. I just had a look now and miraculously the prototype doesn't yet make use of abstract types for the first argument. But there's no doubt it will. And this is very worrisome as I just convinced some colleagues to use Julia for this precisely on this basis. I'm visiting them next week as it happens to thrash out the details.

Consider homomorphisms between many different types of groups. The homomorphisms are modeled as objects that can be called. All the homomorphism types will belong to an abstract type, and a fundamental part of the mechanism that allows propagation of new knowledge along the homomorphisms requires generic overloading of call for all homomorphisms (similar to the Map example above).

thofma commented 8 years ago

I guess as there is no equivalent of call(x::Integer, args...) there is also no equivalent of call{T <: Integer}(x::T, args...)?

I see that this can be overcome by defining call(x::T, args...) for every concrete subtype of T. But what if there are infintely many of them you don't know at parse time? More concretely I am thinking of a "recursive" type as follows:

type A{T <: Integer} <: Integer
  x::T

Then you can construct A{Int}, A{A{...{A{Int}}} but with the new behavior of call I don't know how to overload an object of this type. Before the change I could just do

call{T <: Integer}(a::A{T}, args...) = ...

One solution now would be

call(a, args...) = ...

But this cannot be used as soon as there is another type with the same behavior.

Note that this is not an artificial problem: Think of polynomials f whose coefficients are polynomials whose coefficients are polynomials whose coefficients are polynomials whose coefficients are of type Int. And now I would like to evalute this polynomial using the using the syntax f(a).

I hope I could properly communicate my concerns.

mbauman commented 8 years ago

To be clear: the only restriction here is with defining call for all objects that belong to an abstract supertype with just one definition. You are still able to define call for non-concrete parametric types.

These are ok (I'll use the old 0.4 syntax for clarity):

abstract AbstractFoo
type Bar <: AbstractFoo end
type Baz{T} <: AbstractFoo end

call(::Bar,x) = 1
call{T}(::Baz{T},x) = 2
call{T<:Integer}(::Baz{T},x) = 3
call(::Type{AbstractFoo},x) = 4
call{T<:Union{Bar,Baz}}(::Type{T},x) = 5

These are the cases that are not supported:

call(::AbstractFoo, x)
call(::Union{Bar, Baz}, x)
call{T<:AbstractFoo}(::T, x)
call(a, x)

So your A{T} example is just fine, @thofma.

thofma commented 8 years ago

Thanks for the clarification @mbauman. Very helpful.

I guess one of my problems was that I could not find out how to do this with the new synatx. The following gives me a syntax error:

{T<:Integer}(::Baz{T},x) = 3

As a user it is quite unfortunate to have all cool features of parametric types except for this one. In particular, since it was possible in 0.4. While I don't mind the change of syntax with new versions, but the removing of features from the language (and not providing equivalent functionality) is hard to cope with.

mbauman commented 8 years ago

It does feel a little backwards right now, but you can do it:

(::Baz{T}){T<:Integer}(x) = 4
toivoh commented 8 years ago

The way that I see it, the fundamental problem here is that, given a large number of methods definitions, a data structure is needed to find the most specific match to dispatch a given function call.

Method tables stored per type of the first (implicit) argument are one implementation of such a data structure, but it's not the only possibility. (E.g. in PatternDispatch.jl I built a graph based on the partial order, but I'm not thinking of such major rewrites here. )

How bad would it be to visit the method tables of both a type and it's super types when looking for the most specific match for dispatch?

JeffBezanson commented 8 years ago

Yes, we could do that. It seemed kind of ugly to me but I might just have to deal with it.

dlfivefifty commented 8 years ago

A work around is to define a macro for extending an abstract type that defines the necessary call:

abstract AbstractFoo macro subFoo(F) return quote immutable $F <: AbstractFoo end (::$F)(x)=x end end

@subFoo(Foo)

f=Foo() f(5) # returns 5

Not sure how to include fields in the subtype, however.

On 7 Feb 2016, at 8:07 AM, Jeff Bezanson notifications@github.com wrote:

Yes, we could do that. It seemed kind of ugly to me but I might just have to deal with it.

— Reply to this email directly or view it on GitHub https://github.com/JuliaLang/julia/issues/14919#issuecomment-180863860.

JeffreySarnoff commented 8 years ago

Thank you to all who are devoting some of their focus on this.

JeffreySarnoff commented 8 years ago

This issue is relevant to some design effort underway. Is there a decision to continue supporting this use of Abstract?

lostanlen commented 8 years ago

@vtjnash sad to see this "won't fix" tag. This bug affects my package, and I regret that call is not going to be a first-class function. Yet I appreciate the massive overhaul of #13412. If the issue turns out to be a "won't fix" indeed, then we need to embrace it and document it.

JeffBezanson commented 8 years ago

I admit, I don't love losing this functionality. We could certainly try the approach here https://github.com/JuliaLang/julia/issues/14919#issuecomment-180863659

vtjnash commented 8 years ago

Agreed. I put the "won't fix" flag mostly as a sign that this may not be fixed soon

nalimilan commented 8 years ago

IMHO this is a blocker for 0.5. Else some packages will be forced to keep using the old call overloading syntax.

StefanKarpinski commented 8 years ago

That's not what "won't fix" means.

Jeffrey-Sarnoff commented 8 years ago

toivoh's approach: to visit the method tables of both a type and it's super types when looking for the most specific match for dispatch

Julia wakes up early to do the dance of dispatch resolution, letting some of us sleep in a bit before running dispatch resolved methods -- right? for most user's uses and also for the internal stuff (sufficiently advancing on Arthur C. Clarke's indistinguishability).

We may be onto more good by looking up dispatches' types by looking up types' types: a refinement of this mechanism likely contributes to Julia's expressive ease by bringing traitful entrainings, stackable protocols, inheritable multimelds™ or other waves of hello.

jiahao commented 8 years ago

I think I just encountered this issue in CurveFit.jl:

abstract LeastSquares

#define a bunch of concrete types <: LeastSquares

Base.call{T<:LeastSquares}(f::T, x) = apply_fit(f, x) #gates of hell

now produces

WARNING: deprecated syntax "call(x::T, ...)".
Use "(x::T)(...)" instead.
ERROR: function type in method definition is not a type
 in eval(::Module, ::Any) at ./boot.jl:225
 in macro expansion at ./REPL.jl:92 [inlined]
 in (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:46
vtjnash commented 8 years ago

TypeMap is pretty close to being able to handle this:

julia> typeof(+).name.mt |> length
20305 # there is only one method table
julia> methods(+) |> length
165

julia> abstract Foo
julia> type Bar <: Foo; end
julia> (::Foo)(x) = x
julia> Bar()("it's alive!")
"it's alive!"

which is fun, even though it still has a few serious performance bugs (e.g. core test takes 174.07s instead of 20s)

daanhb commented 8 years ago

It would be cool if eventually this functionality was restored. In a code with use case similar to ApproxFun, I've used the simple workaround of adding a method to every subtype: that involved changing a single line of code into 20+ lines spread over 15 files in two packages - I would love to take those out again :-)

On the other hand, some new helpful warnings in 0.5 pointed to lots of redundant code so we still had a net gain. Deprecation warnings were quickly dealt with and this issue was the only one in moving to 0.5, so :thumbsup:

daanhb commented 8 years ago

On second thought, never mind my previous comment: our use of the calling operator was not semantically consistent, and in the alternative we won't have this problem of adding functions to abstract types anymore. Even better.

wbhart commented 8 years ago

I want to mention another serious issue that has arisen from removal of this functionality. I recently went through our package Nemo to document all our functionality. We now take the approach that as much as possible in Nemo is implemented generically (I am using this word in the sense of generic programming, not in the sense of generic function in Julia), for generic rings/fields/groups/modules, etc. and then overloaded by more specific methods where available.

Documenting the same functions over and over again for each individual ring was proving to be cumbersome and not useful to the user. So we decided to document the most generic implementation exactly once and say that it works for all types that belong to the abstract type for which the function is implemented (this fits in nicely with the Documenter.jl package we are using, and the Julia doc""" ... """ syntax).

The problem is, now there is no "generic" version of "call" for our abstract types. So there's no "generic" function to actually document. And moreover, we can't tell the user that this functionality is automatically available for them if they implement a type that belongs to one of our abstract types.

To work around this, we've basically had to add some fake documentation directly in our .md files for call (or its replacement), along with a longwinded explanation of why this symmetry in Julia is broken.

mauro3 commented 8 years ago

This, oddly, seems to work:

abstract A

"Some doc"
function (::A) end
help?> A
search: A ANY Any any all abs ARGS ans atan asin asec any! all! airy acsc acot acos abs2 Array atanh atand atan2 asinh asind asech asecd ascii angle

  Some doc

So, an empty generic function for an abstract type can be created but it's not possible to add methods to it.

MichaelHatherly commented 8 years ago

Attaching the docstring to A is expected there. I felt that allowing call, or (::?), to have additional documentation added to it wasn't really that useful.

What I mean here is that call syntax is for calling an object and it's documentation should reflect that. Adding dozens of docstrings describing how it works for each callable object isn't going to scale too well I believe. In my view specifics about how a type can be used should always be added to the type's docstring, i.e. supports calling, iteration, etc. Docstrings for "generic" functions should stay generic.

I believe that structuring docs in that way will be more discoverable than having them spread out over a multitude of different functions.

(Apologies for going slightly off-topic here.)

JeffreySarnoff commented 8 years ago

+1,2,many for restoring this underlying capability to abstract types:

defining call for all objects that belong to an abstract supertype with just one definition - mbauman

This is essential to designing software in the abstract that will run correctly when coded as conceived.

If that is not sufficiently compelling, this technology would give Julia an immediate way to specify API's.

mauro3 commented 8 years ago

Should this get a milestone?

cortner commented 8 years ago

I also just stumbled over this issue. Should I assume that this won't be restored for v0.5?

cortner commented 8 years ago

@dlfivefifty as a temporary work-around, instead of a macro to create the subtype I created a macro to annotate a new type declaration; for me this is sufficient for now, maybe it helps?

macro pot(fsig::Expr)
   @assert fsig.head == :type
   eval(fig)    # creates the new type
   eval(
   quote
      (x::$(fsig.args[2]))(args...) = dosomething(x, args...)
   end
   )
end
vtjnash commented 8 years ago

Correct versions of above macro:

function pot(fsig::Expr)
   @assert fsig.head == :type
   eval(fig)    # creates the new type
   @eval begin
       (x::$(fsig.args[2]))(args...) = dosomething(x, args...)
   end
end
macro pot(fsig)
    return :(pot($(esc(fsig))))
end

or

macro pot(fsig)
   @assert fsig.head == :type
   return quote
       $(esc(fsig))   # creates the new type
       (x::$(esc(fsig.args[2])))(args...) = dosomething(x, args...)
   end
end