[sage-support] Re: Element vs UniqueRepresentation

2016-10-11 Thread Simon King
["Followup-To:" nach gmane.comp.mathematics.sage.devel gesetzt.]
On 2016-10-11, Simon King  wrote:
> Hi Jeroen,
>
> On 2016-10-10, Jeroen Demeyer  wrote:
>> Maybe I'm misunderstanding you, but what I wanted to say is: Python 3 
>> doesn't have any place to hook a custom metaclass.
>
> Is there a way to have a metaclass similar to our ClasscallMetaclass at
> all, in Python3?? I just tested, having
>
> class Classcall(type):
> def __call__(cls, *args, **opts):
> try:
> classcall = cls.__classcall__
> except AttributeError:
> return type.__call__(cls, *args, **opts)
> return classcall(cls, *args, **opts)
>
> class MyUniqueRepresentation:
> __metaclass__ = Classcall

Found it! In Python3, one has to write
  class MyUniqueRepresentation(metaclass=Classcall):
  ...

and then it works. The same is a syntax error in Python2.

Anyway. Since SageMath meanwhile makes use of more and more metaclasses,
I created trac ticket #21681 for the introduction of a metaclass
framework.

Best regards,
Simon


-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.


[sage-support] Re: Element vs UniqueRepresentation

2016-10-11 Thread Simon King
Hi Jeroen,

On 2016-10-10, Jeroen Demeyer  wrote:
> Maybe I'm misunderstanding you, but what I wanted to say is: Python 3 
> doesn't have any place to hook a custom metaclass.

Is there a way to have a metaclass similar to our ClasscallMetaclass at
all, in Python3?? I just tested, having

class Classcall(type):
def __call__(cls, *args, **opts):
try:
classcall = cls.__classcall__
except AttributeError:
return type.__call__(cls, *args, **opts)
return classcall(cls, *args, **opts)

class MyUniqueRepresentation:
__metaclass__ = Classcall
@staticmethod
def __classcall__(cls, *args, **opts):
out = super(cls,cls).__new__(cls, *args, **opts)
print("classcall got",type(out), cls)
cls.__init__(out,*args,**opts)
out._reduction = (type(out), args, opts)
return out

Loading the above into python2, I get
>>> class Foo(MyUniqueRepresentation):
... def __init__(self, a,b):
... self.a = a
... self.b = b
... 
>>> A = Foo("A","B")
('classcall got', , )
>>> A._reduction
(, ('A', 'B'), {})

But doing the same in python3, it gives
>>> class Foo(MyUniqueRepresentation):
... def __init__(self, a,b):
... self.a = a
... self.b = b
... 
>>> A = Foo("A","B")
>>> A._reduction
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'Foo' object has no attribute '_reduction'

So, in python2, the __classcall__ is called, in python3 it isn't. What
needs to be done to make the above example work in python3?

It makes me wonder: IIRC there are people trying to build SageMath with
python3 (that's why I put sage-devel on Cc). Does UniqueRepresentation
actually work with python3, when the metaclass apparently is not called
during creation of a class?

Best regards,
Simon


-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.


Re: [sage-support] Re: Element vs UniqueRepresentation

2016-10-10 Thread Jeroen Demeyer

On 2016-10-10 17:31, Simon King wrote:

I just tested that in the current Sage development version my
proof-of-concept would still work.


But still Python 2, right?


And looking at
_PyType_CalculateMetaclass, I don't see why it wouldn't work in Python3.


Maybe I'm misunderstanding you, but what I wanted to say is: Python 3 
doesn't have any place to hook a custom metaclass. In Python 3, with


class X(B1, ..., Bn)

the metaclass of X must be one of the metaclasses of the Bi, there is no 
way to override it (except of course when you explicitly specify the 
metaclass). Of course, I can always be wrong...


--
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.


[sage-support] Re: Element vs UniqueRepresentation

2016-10-10 Thread Simon King
Hi Jeroen,

On 2016-10-10, Jeroen Demeyer  wrote:
> On 2016-10-10 10:47, Simon King wrote:
>> The meta-metaclass would take the
>> atomic metaclasses appearing in the bases of a class definition "Foo",
>> and would automatically/dynamically create a composed metaclass, that
>> combines all the features of the given atomic metaclasses and would
>> serve as the metaclass of the resulting class "Foo".
>
> I have tried that. It is possible in Python 2 but (as far as I know) not 
> in Python 3. In Python 2, when doing
>
> class X(B1, ..., Bn)
>
> The metaclass of B1 is responsible for constructing the class X (in 
> particular, the metaclass of B1 can decide the metaclass of X). In 
> Python 3, the metaclass of X is resolved by a hard-coded algorithm 
> _PyType_CalculateMetaclass and then that metaclass constructs the class X.
>
> Of course, all this is completely undocumented.

Seriously? That's a regression. Having single-purpose metaclasses that
can smoothly blend would have been a nice feature.

Best regards,
Simon

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.


[sage-support] Re: Element vs UniqueRepresentation

2016-10-10 Thread Simon King
Hi Jeroen,

On 2016-10-10, Jeroen Demeyer  wrote:
> Try this:
>
> sage: from sage.misc.inherit_comparison import 
> InheritComparisonClasscallMetaclass
> sage: class A(Element, UniqueRepresentation):
> : __metaclass__ = InheritComparisonClasscallMetaclass

In other words, we have custom made metaclasses that combine various
"basic" metaclasses. Such as
DynamicInheritComparisonClasscallMetaclass
found in sage.structure.dynamic_class: It combines DynamicMetaclass,
InheritComparisonMetaclass and ClasscallMetaclass, and if I see that
correctly it fails to include NestedClassMetaclass (which is a
sub-metaclass of ClasscallMetaclass for a mere technical reason).

Several years ago, I played with the idea to have certain atomic
metaclasses for Sage, each of which implementing just a single feature.
And to be able to automatically combine them, the atomic metaclasses are
instances of a common meta-metaclass: The meta-metaclass would take the
atomic metaclasses appearing in the bases of a class definition "Foo",
and would automatically/dynamically create a composed metaclass, that
combines all the features of the given atomic metaclasses and would
serve as the metaclass of the resulting class "Foo".

In other words, there would be no need to hard-code
DynamicInheritComparisonClasscallMetaclass in SageMath: The
meta-metaclass would dynamically create it from DynamicMetaclass,
InheritComarisonMetaclass, and ClasscallMetaclass.

I had a proof of concept, at a time when SageMath used less metaclasses
than it does now. Do you think it would find support from the SageMath
community to revive my project to sanitise metaclass usage? The more
SageMath is relying on metaclasses, the more I feel it makes sense to
structure it by means of a meta-metaclass.

Best regards,
Simon


-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.


[sage-support] Re: Element vs UniqueRepresentation

2016-10-09 Thread Nils Bruin
On Saturday, October 8, 2016 at 12:12:30 PM UTC-7, Nils Bruin wrote:
>
> On Saturday, October 8, 2016 at 8:43:33 AM UTC-7, John H Palmieri wrote:
>>
>>
>> What are all of the drawbacks?
>>
>> The ones I am aware of:
>  - the construction parameters need to be cached. That might have lifetime 
> implications for them.
>
 
A particularly devious one is this:

class A(...):
  ...
  @cached_method
  def B(self):
return Bclass(self)

class Bclass(UniqueRepresentation):
   ...

a=A(...)
b=a.B()
del a,b

This creates a memory leak (a and b will never be deallocated).

 Thanks to UniqueRepresentation, b lives in a global WeakValueDict, keyed 
by a (so a will live as long as b will live). Conversely, because a.B is 
cached, it will have a reference to b, so a will keep b alive. Hence, a 
reference loop anchored in a global object.

As soon as UniqueRepresentation is involved anywhere, you have to be very 
careful about caching. The weak global reference that exists for 
UniqueRepresentation to function is very easy to short-circuit into a 
reference cycle that prevents deletion.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.


Re: [sage-support] Re: Element vs UniqueRepresentation

2016-10-08 Thread Jori Mäntysalo

On Sat, 8 Oct 2016, Nils Bruin wrote:


  What are all of the drawbacks?

The ones I am aware of:


Also random testing for a hypotheses is hard. I can generate infinitely 
many random graphs in a loop and try to found a counterexample, but the 
same does not apply to posets.


--
Jori Mäntysalo


[sage-support] Re: Element vs UniqueRepresentation

2016-10-08 Thread Nils Bruin
On Saturday, October 8, 2016 at 8:43:33 AM UTC-7, John H Palmieri wrote:
>
>
> What are all of the drawbacks?
>
> The ones I am aware of:
 - the construction parameters need to be cached. That might have lifetime 
implications for them.
 - construction parameters need to be processed for hashing etc. So 
construction takes more time the first time. (If you're trying to construct 
an already cached object you might get better performance)
 - you end up with objects that are really global: someone else can get a 
hold of your object without either of you knowing. That means you have to 
be very careful to make your objects immutable. 

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.


[sage-support] Re: Element vs UniqueRepresentation

2016-10-08 Thread John H Palmieri


On Friday, October 7, 2016 at 6:21:45 PM UTC-7, Nils Bruin wrote:
>
> On Friday, October 7, 2016 at 10:47:55 AM UTC-7, John H Palmieri wrote:
>>
>> TypeError: Error when calling the metaclass bases
>> metaclass conflict: the metaclass of a derived class must be a 
>> (non-strict) subclass of the metaclasses of all its bases
>>
> Yes, python doesn't support multiple metaclass inheritance (its design 
> really prevents it), so I'd think this is a hard obstruction.
>
> sage: M=UniqueRepresentation.__metaclass__
> sage: M.mro(M)
> [,
>  ,
>  ,
>  ]
> sage: E=Element.__getmetaclass__(1)
> sage: E.mro(E)
> [,
>  ,
>  ]
>
> prior to https://trac.sagemath.org/ticket/18329 your example worked.
>
> Of course, if you want UniqueRepresentation (do you really want that? it's 
> got a lot of drawbacks) you wouldn't really care about inheriting 
> comparison, but that doesn't help you.
>

What are all of the drawbacks?

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.


[sage-support] Re: Element vs UniqueRepresentation

2016-10-07 Thread Nils Bruin
On Friday, October 7, 2016 at 10:47:55 AM UTC-7, John H Palmieri wrote:
>
> TypeError: Error when calling the metaclass bases
> metaclass conflict: the metaclass of a derived class must be a 
> (non-strict) subclass of the metaclasses of all its bases
>
Yes, python doesn't support multiple metaclass inheritance (its design 
really prevents it), so I'd think this is a hard obstruction.

sage: M=UniqueRepresentation.__metaclass__
sage: M.mro(M)
[,
 ,
 ,
 ]
sage: E=Element.__getmetaclass__(1)
sage: E.mro(E)
[,
 ,
 ]

prior to https://trac.sagemath.org/ticket/18329 your example worked.

Of course, if you want UniqueRepresentation (do you really want that? it's 
got a lot of drawbacks) you wouldn't really care about inheriting 
comparison, but that doesn't help you.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/d/optout.