Bases: sage.categories.category_singleton.Category_singleton
The category of facade sets
A facade set is a parent P whose elements actually belong to some other parent:
sage: P = Sets().example(); P
Set of prime numbers (basic implementation)
sage: p = Sets().example().an_element(); p
47
sage: p in P
True
sage: p.parent()
Integer Ring
Typical use cases include modeling a subset of an existing parent:
sage: Sets().Facades().example()
An example of facade set: the monoid of positive integers
or the union of several parents:
sage: Sets().Facades().example("union")
An example of a facade set: the integers completed by +-infinity
or endowing a parent with more (or less!) structure:
sage: Posets().example("facade")
An example of a facade poset: the positive integers ordered by divisibility
Let us consider one of the examples above in detail: the partially ordered set \(P\) of positive integers w.r.t. divisibility order. There are two options for representing its elements:
- as plain integers
- as integers, modified to be aware that their parent is \(P\)
The advantage of 1. is that one needs not to do conversions back and forth between \(P\) and \(\ZZ\). The disadvantage is that this introduces an ambiguity when writing \(2 < 3\):
sage: 2 < 3
True
To raise this ambiguity, one needs to explicitely specify the order as in \(2 <_P 3\):
sage: P = Posets().example("facade")
sage: P.lt(2,3)
False
In short \(P\) being a facade parent is one of the programmatic counterpart (with e.g. coercions) of the usual mathematical idiom: “for ease of notation, we identify an element of \(P\) with the corresponding integer”. Too many identifications lead to confusion; the lack thereof leads to heavy, if not obfuscated, notations. Finding the right balance is an art, and even though there are common guidelines, it is ultimately up to the writer to choose which identifications to do. This is no different in code.
See also
sage: Sets().example("facade")
Set of prime numbers (facade implementation)
sage: Sets().example("inherits")
Set of prime numbers
sage: Sets().example("wrapper")
Set of prime numbers (wrapper implementation)
Specifications
A parent which is a facade must either:
Note
the concept of facade parents was originally introduced in the computer algebra system MuPAD.
TESTS:
Check that multiple categories initialisation works (trac ticket #13801):
sage: class A(Parent):
... def __init__(self):
... Parent.__init__(self, category=(FiniteEnumeratedSets(),Monoids()), facade=True)
sage: a = A()
Returns the parents this set is a facade for
This default implementation assumes that self has an attribute _facade_for, typically initialized by Parent.__init__(). If the attribute is not present, the method raises a NotImplementedError.
EXAMPLES:
sage: S = Sets().Facades().example(); S
An example of facade set: the monoid of positive integers
sage: S.facade_for()
(Integer Ring,)
Check that trac ticket #13801 is corrected:
sage: class A(Parent):
... def __init__(self):
... Parent.__init__(self, category=Sets(), facade=True)
sage: a = A()
sage: a.facade_for()
Traceback (most recent call last):
...
NotImplementedError: this parent did not specify which parents it is a facade for
Returns whether self is the parent of element
INPUT:
Since self is a facade domain, this actually tests whether the parent of element is any of the parent self is a facade for.
EXAMPLES:
sage: S = Sets().Facades().example(); S
An example of facade set: the monoid of positive integers
sage: S.is_parent_of(1)
True
sage: S.is_parent_of(1/2)
False
This method differs from __contains__() in two ways. First, this does not take into account the fact that self may be a strict subset of the parent(s) it is a facade for:
sage: -1 in S, S.is_parent_of(-1)
(False, True)
Furthermore, there is no coercion attempted:
sage: int(1) in S, S.is_parent_of(int(1))
(True, False)
Warning
this implementation does not handle facade parents of facade parents. Is this a feature we want generically?
Returns an example of facade set, as per Category.example().
INPUT:
EXAMPLES:
sage: Sets().Facades().example()
An example of facade set: the monoid of positive integers
sage: Sets().Facades().example(choice='union')
An example of a facade set: the integers completed by +-infinity
sage: Sets().Facades().example(choice='subset')
An example of facade set: the monoid of positive integers
Returns the super categories of self, as per Category.super_categories().
EXAMPLES:
sage: Sets().Facades().super_categories()
[Category of sets]