Dice

Let's play with dice in this example. A Dice object shall represent the outcome of a series of rolling a dice with six faces, e.g.:

Like in most games, the ordering of the individual throws does not matter. We can express this by making Dice Orderless:

A dice object shall be displayed as a rectangle with the given number of points in it, positioned like on a traditional dice:

The empty series of dice shall be displayed as an empty dice:

Any non-empty series of dice shall be displayed as a row of individual dice:

Note that Mathics will automatically sort the given format rules according to their “generality”, so the rule for the empty dice does not get overridden by the rule for a series of dice. We can still see the original form by using InputForm:

We want to combine Dice objects using the + operator:

The ^:= (UpSetDelayed) tells Mathics to associate this rule with Dice instead of Plus, which is protected—we would have to unprotect it first:

We can now combine dice:

Let's write a function that returns the sum of the rolled dice:

And now let's put some dice into a table:

It is not very sophisticated from a mathematical point of view, but it's beautiful.