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'; done-testing();
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.
multi infix: ( Any:_ $c, :U $ –> Bool ) { True }
What does this mean?
_ $c
:U $
Any:_
says that the container accepts a type that isAny
or inherits fromAny
. 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:
I know you’re just showing off Perl6, but it has to be said that the class π isn’t really a set, since π !~~ Set.
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.