To do this properly, one must create a custom context for sets of sets. I have done that in the attached file. See the documentation at the top of the file for more details. The context uses the List object to manage the set of sets. It subclasses the List object and uses braces to create lists; when a list of numbers is created, it turns it into a Set object (so that the set of sets is a List of Sets). There is some checking for repeated entries and some fanciness to make the error reports better, but that is the basic setup.
That means you can use
loadMacros(
"contextSetOfSets.pl"
);
Context("SetOfSets");
$showPartialCorrectAnswers = 1;
$S = Compute("{0,1}");
$ans = Compute("{{}, {0}, {1}, {0,1}}");
BEGIN_PGML
The power set of [`[$S]`] is [___________________]{$ans}
END_PGML
to get your power-set question. It is possible to set up a name for the empty set (see the documentation in the file).
It is also possible to use other symbols in the sets, like letters. To do that, you have to set up constants in the context that represent the symbols you want to use. Set these to be constants for distinct integers, for example, as shown below. You may also want to set a NumberCheck
function to disallow entry of numbers rather than letters, and you may want to set the entry_type
in order to make the error messages more meaningful. Here is an example that allows sets to consist of the letters A
, B
, and C
:
loadMacros(
"contextSetOfSets.pl"
);
Context("SetOfSets");
@letters = ('A' .. 'C');
Context()->constants->add(map {$letters[$_] => $_} (0..$#letters));
Context()->flags->set(NumberCheck => sub {
my $self = shift;
$self->Error("An element of a set of letters can't be a number");
});
Context()->{cmpDefaults}{List}{entry_type} = "a set of letters";
$showPartialCorrectAnswers = 1;
$S = Formula("{A,B}");
$ans = Compute("{{}, {A}, {B}, {A,B}}");
BEGIN_PGML
The power set of [`[$S]`] is [___________________]{$ans}
END_PGML
You can get more letters by simply setting the @letters
array to include additional letters, or even make it something like
@letters = ('Red', 'Blue', 'Green');
Note, however, that these are case-sensitive names, and you may wish to add
Context()->constants->set(map {$_ => {TeX => "\text{$_}"}} @letters);
after creating the constants in order to produce better TeX output for them.
A full example is:
loadMacros(
"contextSetOfSets.pl"
);
Context("SetOfSets");
@letters = ('red', 'green', 'blue');
Context()->constants->add(map {$letters[$_] => $_} (0..$#letters));
Context()->constants->set(map {$_ => {TeX => "\text{$_}"}} @letters);
Context()->flags->set(NumberCheck => sub {
my $self = shift;
$self->Error("An element of a set of letters can't be a number");
});
Context()->{cmpDefaults}{List}{entry_type} = "a set of letters";
$showPartialCorrectAnswers = 1;
$S = Formula("{red, green}");
$ans = Compute("{{}, {red}, {green}, {red, green}}");
BEGIN_PGML
The power set of [`[$S]`] is [___________________]{$ans}
END_PGML
I think that should handle your needs (and then some).