7.1.5. Another ordering of Role types

There is, however, another base for comparing roles and that is to consider instances as ‘tagged individuals’. An instance is an instance of a role R by declamation, as it were; not because it has a particular structure (in terms of properties).

Let’s briefly diverse into a discussion of Aspects to underline this (See the chapter Aspects below for a brief explanation). Consider a rather universal role Driver, that can be used in many contexts, e.g. as the driver of an Ambulance or the driver of a Taxi. Driver-in-an-Ambulance has a different meaning than Driver-in-a-Taxi. A model may, for example, contain a query to count the number of hospitals that the Driver-in-an-Ambulance has visited in a period of time. Clearly, if the same person drives both Taxi’s and Ambulances, we want to separate out both ‘contextualized’ roles when performing that query.

7.1.5.1. Product types

There is no natural ordering of simple role types. But there is a way to consider compound roles to be ordered. Take, for example, the Product of two roles A and B and compare it to A itself. Surely, A is less specific than AxB. Any instance of AxB is, by construction, also an instance of A, but not the other way round.

From this we derive the notion that, for Product types, when we consider the products as the sets of their terms, subset means: less specific.

7.1.5.2. Sum types

What about a Sum of A and B? How does that compare to A? Well, an instance of A is always an instance of A | B, but not the other way round (after all an instance of B is an instance of A+B as well – but clearly not an instance of A!). So, A+B is less specific than A. Paraphrasing it may be easier to understand: A is more specific than A+B (“I will talk not to just any Parent, only to the Mother!”).

So, for Sum types, considering them as sets of terms, subset means: more specific.

7.1.5.3. Arbitrary types

Taking this as the base of our alternative definition of ordering, how can we generalize it to any role defined in terms of Sums and Products?

Any construction in terms of Sums and Products can be converted into Disjunctive Normal Form (DNF). A formula is in DNF iff it is a disjunction of products, where the term of each product is atomic.

Let’s work out how we can compare two types written in DNF. We’ll investigate: SUB is more specific than SUPER.

For Sum types, more specific equals subset. SUB should be a subset of SUPER. The definition of subset is: each element of SUB should occur in SUPER.

What if the terms (elements) are Product types? Instead of using simple equality, we must now ask of each element in SUB if it is a superset of an element in SUPER. Remember that the elements are Products and that for them more specific means: superset.

In short, our entire algorithm is: is each element in SUB the superset of some element in SUPER?

7.1.5.4. Aspects

In the previous paragraph we used reduction to Disjunctive Normal Form (DNF) to prove the soundness of the function equalsOrGeneralisesADT. This function does not take Aspects into account, because we treat terms in Products and Sums as atomic. However, this is insufficient to capture the semantics of Perspectives types, as the following example shows. Let T have aspect A, then clearly A equalsOrGeneralises T. However, our function will return false! This is because union’ will be {A}, while intersection’ will be {T} and {A} is not a subset of {T}. Luckily, we can easily improve the function to cover aspects, too.

When we give a type T an aspect A, we actually state that any instance of T is an instance of A as well. We can think of a type-T-with-aspect-A as the Product TxA. Imagine that we expand all terms to the full set of their aspects (i.e. the type itself and its aspects). Then we would have:

  • union’ = {A}

  • intersection’ = \{A, T}

and now the first is a subset of the latter, hence clearly A equalsOrGeneralises T returns true! In other words, we can capture the semantics of Perspectives types in the function equalsOrGeneralises by expanding all types to their transitive closure over the aspect relation.