Polymorphism

Suppose we have a CertSAFE project with two diagrams in it. The first diagram, named “My Component”, performs some simple conditional arithmetic depending upon its inputs:

ALT

Because “My Component” does not explicitly specify any of its numeric data types, it is inferred to be polymorphic in these types. This means that different instances of the component can have different data types for their inputs and outputs, subject to automatically-inferred constraints.

The second diagram, named “Component User”, contains two instances of the “My Component” diagram as custom components, named “My Component 1” and “My Component 2”:

ALT

When you mouse over My Component 1 in the user interface, CertSAFE displays this description of the component’s data type:

ALT

This is called the type signature of the component, and is read as follows: * The first line says the unit is named “My Component 1” and is of type “My Component”. * The “Type variables” line says that there are three types that must be filled in to simulate a unit of type “My Component”, which CertSAFE has arbitrarily named with the Greek letters α, β, and γ. It also says that the types α, β, and γ are constrained to all be numeric data types (since My Component performs arithmetic operations on values of those types), and γ is also constrained to be an ordered data type (since an ordering comparison is being performed on a value of that type). * The table in the middle lists the data types of each input and output. From the table, we know that input y and output u must have the same data type α, and input z and output v must have the same data type β. (The “pure” keywords have to do with constness inference, a somewhat different topic.* The “With substitutions” line at the bottom says that, in the My Component 1 instance, α and β are instantiated to the type UInt32, and γ to the type Int16. These are the actual types that will be used to calculate values for this instance during simulation.

In the “My Component 2” instance, on the other hand, the data types are Float32 for α and β and Float64 for γ. This means that integer-valued constants, arithmetic, and comparisons are used in My Component 1, while floating-point constants, arithmetic, and comparisons are used in My Component 2. This information is all inferred from the structure of the two diagrams, without most of the typing information being specified by the user or the file on disk.

This demonstrates yet another reason why a diagram by itself should not be thought of as a model: polymorphic types. Occasionally when trying to create a simulation, CertSAFE may generate an error message at the bottom of the timeline like “This root is not simulatable. The root unit type ‘[Diagram] My Component’ contains type variables.” This is because CertSAFE does not have enough information to simulate the instance hierarchy instantiated from My Component. Specifically, it does not know which data types to use for some of its input and output variables, because more than one choice is valid.