The Universal Set

Some people like to think you can’t have a universal set. I say otherwise. The difference is what you can do about it.

Perl 6 has the empty set, βˆ… and nothing is a member of that set:

if $a ∈ βˆ… { put 'Never executes!' } # always False

This is easy to construct with a set of no elements:

if $a ∈ set() { put 'Never executes!' } # always False

Perl 6 can construct sets from finite sequences but I wondered about infinite ones. The universal set, π•Œ, contains everything (although that causes some problems the set theorists haven’t figured out other than to say “Don’t do that”).

But if I can figure out set membership without enumerating the members of the set I can make it work:

            # Double struck letters
class π•Œ {}; # Universal, U+1D54C but wordpress don't like that

multi infix:<∈> ( Any:_ $c, π•Œ:U $ --> Bool ) { True }

use Test;

ok any(1,2) ∈ π•Œ, '(Junction) is part of the universal set';
ok   Any    ∈ π•Œ, '(Any) is part of the universal set';
ok   37     ∈ π•Œ, 'Int:D is part of the universal set';
ok  '37'    ∈ π•Œ, 'Integer string is part of the universal set';
ok   π•Œ      ∈ π•Œ, 'Universal set contains itself? Not so sure...';
ok   βˆ…      ∈ π•Œ, 'Universal set contains the empty set';


The trick is to figure out how to represent a set difference with a universal set. I know what the complement of that set so I need to flip around the operators. I’m still working out some other things (see Where did my Perl 6 operator go after I defined a more specific multi? for one hiccup) to make the set operators work, though.


    1. Any:_ says that the container accepts a type that is Any or inherits from Any. The _ says it can be defined (:D) or undefined (:U). This is the same thing as no type constraint really.

      The $ is merely a scalar container I don’t give a name to since the name doesn’t matter.

      I could have also written both as anonymous scalar containers since I don’t really care about either name:

      multi infix:<∈> ( $, π•Œ:U $ -–> Bool ) { True }
  1. I know you’re just showing off Perl6, but it has to be said that the class π•Œ isn’t really a set, since π•Œ !~~ Set.

    1. I don’t define a set by the class tree. If it looks like a set and acts like a set, it’s a set. In mathematics that’s a collection of distinct items that’s considered as a whole.

      In object orientation we should never care what something is as long as it does the things that we need. Don’t check for types of objectsβ€”check for abilities. If it follows the same interface it doesn’t matter what its lineage is.

Leave a Reply

Your email address will not be published. Required fields are marked *