Facade Sets

class sage.categories.facade_sets.FacadeSets(s=None)

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:

  1. as plain integers
  2. 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:

  • call Parent.__init__() using the facade parameter to specify a parent, or tuple thereof.
  • overload the method facade_for().

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()
class ParentMethods
facade_for()

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
is_parent_of(element)

Returns whether self is the parent of element

INPUT:

  • element – any object

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?

FacadeSets.example(choice='subset')

Returns an example of facade set, as per Category.example().

INPUT:

  • choice – ‘union’ or ‘subset’ (default: ‘subset’).

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
FacadeSets.super_categories()

Returns the super categories of self, as per Category.super_categories().

EXAMPLES:

sage: Sets().Facades().super_categories()
[Category of sets]

Previous topic

Weyl Groups

Next topic

Examples of algebras with basis

This Page