quick 'n ez way to get allsupertypes(x), allsubtypes(x)?

153 views
Skip to first unread message

Jeffrey Sarnoff

unread,
Feb 9, 2016, 3:24:44 PM2/9/16
to julia-users
Any advice on quick 'n EZ coding of something like these?

allsupertypes(Irrational) == ( Real, Number, Any )

allsubtypes(Integer) == ( BigInt, Bool,  Signed, Int128,Int16,Int32,Int64,Int8, Unsigned, UInt128,UInt16,UInt32,UInt64,UInt8 )
abstractsubtypes(Integer) == ( Signed, Unsigned )
concretesubtypes(Integer) == ( BigInt,Bool,UInt128,UInt16,UInt32,UInt64,UInt8,UInt16,UInt32,UInt64,UInt8)



Milan Bouchet-Valat

unread,
Feb 9, 2016, 3:43:48 PM2/9/16
to julia...@googlegroups.com
Here's a way to get all concretes ubtypes:
subtypestree(x) = length(subtypes(x)) > 1 ? map(subtypestree, subtypes(x)) : x
[subtypestree(AbstractArray)...;]

You should be able to adapt this to return all abstract types instead
by using isleaftype() (which would better be called isconcretetype()?).
But note there's the special case of parametric types, which aren't
leaf types.


Regards

Jeffrey Sarnoff

unread,
Feb 9, 2016, 6:50:43 PM2/9/16
to julia-users
I see that your definition pours the subtypes from a pitcher of the poured subtypes.  The note about parametric types is well pointed. -- Jeffrey

Clearly, the answer is therein.  Cloudily, I'm looking.

Tommy Hofmann

unread,
Feb 10, 2016, 2:32:35 AM2/10/16
to julia-users
You implicitly assume that a type has only finitely many sub/supertypes, which for arbitrary types is clearly not the case. The simplest example is Any but you can also get this behavior when defining recursive types. More generally, given types TL, TU there is no way of returning all types T with TL <: T <: TU. You can describe this set using TypeVar, but you cannot just write it down.

Tommy

Jeffrey Sarnoff

unread,
Feb 10, 2016, 3:10:16 AM2/10/16
to julia-users
Hello Tommy,

OK, putting off inclusion of recursive types ...
and ignoring all possible values for the parameters of a parameterized type unless already explicitly defined (present in memory) ...

    allsupertypes(T) should be a short list from T to supertype(T) to supertype(supertype(T)) .. to Any

    allsubtypes(T) seems obtainable 
    I can throw things into a tree until the leaves have no subtypes, then traverse it; is there a nice way to do that implicitly within a function?

Tommy Hofmann

unread,
Feb 10, 2016, 3:26:14 AM2/10/16
to julia-users
I think the recursive solution of Milan will give you a finite list of all subtypes. But the list will contain things like Array{T, N}, that is, a type with a parameter. How do you handle those? Do you want to count them as abstract or concrete?

Jeffrey Sarnoff

unread,
Feb 10, 2016, 3:47:30 AM2/10/16
to julia-users
I want to Typename{T,N} as Abstract onlyif Typename.abstract==true, otherwise Typename.abstract==false and I want to treat it as Concrete

Milan's solution displays the subtypes immediately. The way it is set  up is neat -- but how to pull the targeted type out of the body?

I need this to be a callable function that yields a manipulable tuple or vector or tuple of subtuples or  etc.

Milan Bouchet-Valat

unread,
Feb 10, 2016, 4:12:42 AM2/10/16
to julia...@googlegroups.com
Le mercredi 10 février 2016 à 00:47 -0800, Jeffrey Sarnoff a écrit :
> I want to Typename{T,N} as Abstract onlyif Typename.abstract==true,
> otherwise Typename.abstract==false and I want to treat it as Concrete
>
> Milan's solution displays the subtypes immediately. The way it is set
>  up is neat -- but how to pull the targeted type out of the body?
>
> I need this to be a callable function that yields a manipulable tuple
> or vector or tuple of subtuples or  etc.
Could you elaborate a bit? Do you need a tuple, or a tuple type?


Regards
Message has been deleted

Jeffrey Sarnoff

unread,
Feb 10, 2016, 5:12:04 PM2/10/16
to julia-users

I need a tuple -- it could have internal subtuples to organize subtypes of a subtype.

The use is to investigate more expressively elaborated and more flexibly organized alternatives to the current Numeric hierarchy.
I would call allsubtypes(T) on some of my own Abstract types and then compare/contrast the sets of tuples yielded, and maybe reorganizing a few.

Milan Bouchet-Valat

unread,
Feb 11, 2016, 8:41:50 AM2/11/16
to julia...@googlegroups.com
Le mercredi 10 février 2016 à 14:12 -0800, Jeffrey Sarnoff a écrit :
>
> I need a tuple -- it could have internal subtuples to organize
> subtypes of a subtype.
>
> The use is to investigate more expressively elaborated and more
> flexibly organized alternatives to the current Numeric hierarchy.
> I would call allsubtypes(T) on some of my own Abstract types and then
> compare/contrast the sets of tuples yielded, and maybe reorganizing a
> few.
Sorry, I don't really get it. It's easy to transform the array that my
code returns into a tuple by doing (a...,), but I'm not sure what's the
point of using a tuple instead of an array.

Regards

Jeffrey Sarnoff

unread,
Feb 11, 2016, 12:24:02 PM2/11/16
to julia-users
stop the presses .. I had misread your initial solution, entering this:

subtypestree(x) = length(subtypes(x)) > 1 ? map(subtypestree, subtypes(x)) : x[subtypestree(AbstractArray)...;] 

... rather than this:

concreteSubtypesTree(x) = length(subtypes(x)) > 1 ? map(concreteSubtypesTree, subtypes(x)) : x
concreteSubtypes(x) = [concreteSubtypesTree(x)...;]

thanks for the assist
Reply all
Reply to author
Forward
0 new messages