Please add sets in Doenet

This came up in another thread: The need for “sets”.

I am proposing a new tag: <mathSet> or something like that. The current <numberList> and <mathList> don’t quite do the same things because they care about order and allow repeats.

Someone else can chime in if there is a need for multisets (unordered lists where duplicates are allowed).

I would like it to work with sets of numbers, words, letters, (and maybe even algebraic expressions?). Here is what I would like to be able to accomplish:

  1. Remove duplicates from a user provided list. This is already possible using <numberList><convertSetToList>{$user_in}</convertSetToList></numberList> but the syntax is weird and we are actually converting a list to a set, so the naming feels off.
  2. Check the cardinality of a finite set using something like $s.numComponents (again, already possible, but requires the above tag wrapping to make it happen).
  3. Check whether one set is a subset (or proper subset) of another.
  4. Find set intersections
  5. Find set unions
  6. Find set complements when a Universal set is defined and relative set complements when two sets are defined.
  7. Determine if a set is a subset of the natural numbers, whole numbers, integers, rational numbers, etc.

I’ve already manually coded a lot of this using modules, but I’d like to be able to do this with the semantic language rather than huge setup blocks at the start of my activity. I have not adapted it for a user inputted set. I’ve only tested it for very small sets, but it works so far. It feels very “hacky” though and uses a lot of RepeatForSequence which I know can be computationally expensive.

Carly’s Attempt at Sets

Thanks, Carly, for the request.

Suggestions on the name of the potential new component? Carly suggested one candidate so far <mathSet>. Other suggestions?

Other suggestions or comments on how this component should work?

Looking at the component source code, there is already an “unordered” property for lists. Wouldn’t we just need to also remove duplicates, and lists would act like sets?

Also, I looked but it wasn’t obvious, what does convertSetToList act on? It is doing what it says (because the outer object in Carly’s example IS a list, so the inside {$user_in} is begin treated as a set and being placed in the outer numberList, but this just seems to be a shortcut to remove duplicates?)

Either way, the question of what the component should be called and how it should work I think depends a lot on what already exists in the Doenet back-end, but without doing a deep dive into the source, I don’t have that context. Duane, could you expand on how works? That might provide some insight already.

This seems like a good addition. finiteSet might be another possible name. @Carly_Vollet , what kind of syntax were you envisioning for 3-7?

We already have elementOf, so maybe $A subsetOf $B?

For intersections and unions, maybe just tags? So

<union> $A $B </union>

Right now I’m doing it in modules. This is still a work in progress, but here is what I have: Set Operations Draft

We already have these operators parsed by <math>

<ul>
  <li><math>x elementof A</math></li>
  <li><math>x notelementof A</math></li>
  <li><math>A containselement x</math></li>
  <li><math>A notcontainselement x</math></li>
  <li><math>A subset B</math></li>
  <li><math>A notsubset B</math></li>
  <li><math>A subseteq B</math></li>
  <li><math>A notsubseteq B</math></li>
  <li><math>A superset B</math></li>
  <li><math>A notsuperset B</math></li>
  <li><math>A superseteq B</math></li>
  <li><math>A notsuperseteq B</math></li>
  <li><math>A union B</math></li>
  <li><math>A intersect B</math></li>
</ul>

I know that containselement does not work with boolean logic; we created an issue to fix that.

It seems like the superset/subset operators work, though superseteq is the same as superset, etc.

I couldn’t get the union to work, i.e., <boolean>x elementof {x} union {y}</boolean> returns false.

Also, Doenet turns x into {x} if needed, so <boolean>x subset {x}</boolean> is true…

If you can create scenarios on how you want to use these components, that would help me to understand what needs to be built.