Skip to content

Commit 29f856f

Browse files
committed
Adds set algebra laws
1 parent a88d50d commit 29f856f

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

doc/Language/math.pod6

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,62 @@
11
=begin pod :tag<perl6>
22
3-
=TITLE Interacting with the system
3+
=TITLE Doing math with Perl 6
44
5-
=SUBTITLE How to work with other applications and the operating system at large
5+
=SUBTITLE Different mathematical paradigms and how they are implemented in this language.
66
7-
=head1 Running external programs.
7+
=head1 Sets.
88
9-
TBD
9+
Perl 6 includes the L<Set> data type, as well as support for L<most set operations|/language/setbagmix#Set/Bag_Operators>. L<Union and intersection|https://en.wikipedia.org/wiki/Algebra_of_sets> are not only native operations, they use their I<natural> symbols, ∩ and ∪. For instace, this code would check the fundamental laws of the arithmetic of sets for a limited number of sets:
1010
11-
=head1 Interacting with the system in an asynchronous way
11+
=begin code
12+
my @arbitrary-numbers = ^100;
13+
my \U = @arbitrary-numbers.Set;
14+
my \Ø = ().Set;
1215
13-
TBD
16+
my @sets;
17+
18+
@sets.push: Set.new( @arbitrary-numbers.pick( @arbitrary-numbers.elems.rand)) for @arbitrary-numbers;
19+
20+
my (@union, @intersection);
21+
22+
for @sets -> $set {
23+
@union.push: $set ∩ $set === $set;
24+
@intersection.push: $set ∪ $set === $set;
25+
}
26+
27+
say "Idempotent union is ", so @union.all;
28+
# OUTPUT: «Idempotent union is True»
29+
say "Idempotent intersection is ", so @intersection.all;
30+
# OUTPUT: «Idempotent intersection is True»
31+
my (@universe, @empty-set, @id-universe, @id-empty);
1432
15-
=head1 Using the native interface
33+
for @sets -> \A {
34+
@universe.push: A ∪ U === U;
35+
@id-universe.push: A ∩ U === A;
36+
@empty-set.push: A ∩ Ø === Ø;
37+
@id-empty.push: A ∪ Ø === A;
38+
}
39+
40+
say "Universe dominates ", so @universe.all; # OUTPUT: «Universe dominates True»
41+
say "Empty set dominates ", so @empty-set.all; # OUTPUT: «Empty set dominates True»
42+
43+
say "Identity with U ", so @id-universe.all; # OUTPUT: «Identity with U True»
44+
say "Identity with Ø ", so @id-empty.all; # OUTPUT: «Identity with Ø True»
45+
=end code
46+
47+
In this code, not only we check if the equalities in the algebra of sets hold, we also use, via L<sigilless variables|/language/variables#index-entry-\_(sigilless_variables)> and the Unicode form of the set operators, expressions that are as close as possible to the original form; C<A ∪ U === U>, for example, except for the use of the L<value identity operator <===>|/routine/===> is very close to the actual mathematical expression in the L<Wikipedia entry|https://en.wikipedia.org/wiki/Algebra_of_sets>.
48+
49+
=head1 Arithmetic.
1650
1751
TBD
1852
19-
=head1 Defining your own associative structures
53+
=head1 Sequences
2054
2155
TBD
2256
57+
=head1 Mathematical constants
2358
59+
TBD
2460
2561
2662
=end pod

0 commit comments

Comments
 (0)