Types, Sets and Characteristics

### Sets as Types

We can, somewhat informally, say that sets are types. Although this doesn't capture the nuance of types, it allows us to capture a lot of power in a simple idea. We looked at defining the string type as a set of values. What this really means is, strings are a certain set of values within all values assignable. If we begin our sets by considering all values assignable and available in within Javascript, we can refer to that set as our "universe." Within that universe, we could choose a variety of different sets, but regardless of which set we choose, the new set will be contained completely within the original universe. Using this universal definition, we can consider our strings again and consider how we might describe our set of all strings. First of all, we can ask if a value is contained within our universe. A good example of a value which is distinctly NOT in the Javascript universe is 1000! (1000 factorial). Although this is an integer which actually exists, Javascript will simply evaluate it as infinity. This is not something we will need to test for, it is simply the indication of an upper bound in Javascript. We could, however, define a set of numbers we declare as our domain set. We can call this an explicit set definition. By turns, we can define a set by way of excluding items which are not in the set. This inverse set can, equally, be declared explicitly, or we can define a function which will simply tell us whether something is in the set or not. This implicit method of creating a set can be referred to as an induced set. Let's take a look at a meaningful question we could ask. Let's ask if a value is a number. This means we are going to call a function which accepts type of * and returns a boolean. This kind of function is called a characteristic function.1 | function isNumber (value) { |

### Propositions and Predicates

It has been shown in the academic community that propositions are types. What this means is we can actually consider propositions such as A and B in the expression A ∨ B as types unto themselves. Any proposition can either resolve to a theorem (⊤) or an antitheorem (⊥) which roughly equates to the idea of true or false. In other words, we can ask a question and the answer will either be "yes" or "no." Although this seems non-obvious, at its face, this is the foundation we will use to construct a richer set of types within Javascript. We have already seen type construction where we use a predicate to manage inclusion in a type set. We are stating that a value α must be in the set of numbers if α is a number. Even stronger than that, we can say, since our set contains ONLY values which are numbers that if α is in the set of numbers it has a type of number. This relationship between sets and type implementations is important for capturing greater amounts of information about a value as we construct subsets from sets we have defined. Let's have a look at the logical notation to see predicates in action, so we can tie that together with our predicate notation. N_{js}= the set of all values which are Javascript numbers A: α is a Javascript number B: α ∈ N

_{js}; A → B B → A Of course in our implementation we really only worry about the second relation, i.e. that if a value is in the set of values which conform to the number type, the value must also conform to the number type. Given the definition of B, α ∈ N

_{js};, we can actually conclude that the first relation is true given the definition of N

_{js}. We can actually reformulate this to express the type-set relation more generally. If we simply replace our specific number set with a set of any type Τ we get a new, very useful formulation we can use to extend our type reach well beyond the specifics of the language. Τ

_{js}= the set of all values expressible in Javascript of type τ A: α

_{τ}is a Javascript value of type τ B: α

_{τ}∈ Τ

_{js}A → B B → A That's a lot of symbols, words and relations. What this really means is we can identify and define any arbitrary type logically and, in turn, define a set containing all values of that type, which will induce an "if and only if" (iff) relationship. That's a lot of words and symbols. Let's take a look at how we might use this to implement our own type.

### Defining a New Javascript Type

Clearly we won't be able to build our type into the core language without getting on TC39, issuing a new standard and waiting for several years while everyone adopts it, but we can induce our type through a new predicate function. Let's suppose we want to define a new type, Integer. We could express our type in the following way: Int_{js}= the set of all values expressible in Javascript of type number which are integers α

_{int}∈ Int

_{js}With this, we can define a function expressing this relationship, which we can use to verify whether a value is in our set Int

_{js}or not. With regard to the relation between types, expressible values in Javascript and our integer set, we can guarantee the stability of our type and the correctness of our verification.

1 | function isInt (value) { |

1 | function isInt (value) { |

_{js}, and our integer set, Int

_{js}. In other words, what we can see expressed here is the typical inheritance property of the is-a relationship.

### The Is-A Relationship of Types

As is true for objects in classical object oriented programming, types can also have an inheritance relationship where one type is a subtype of another. This is what we mean by is-a relationship. We can say an integer is a number, or a name is a string. Although an integer can be a type in its own right, we know the number type is the foundation type in Javascript for any numeric representation. This means, for any function which requires a number, an integer is an acceptable value. Our isInt function demonstrates the is-a relationship by using the number set definition as the first requirement of our check for set inclusion. Let's continue the chain and create a characteristic function to defining our natural number. Our natural number set will be a strict subset of our integer set.1 | function isNatural (value) { |

_{js}⊂ Int

_{js}⊂ Number

_{js}With the repeated behavior of including a function call from the superset, we can start looking for a way to uniformly describe our sets and their relationships. Let's create a new function, subtype, to help us create set relationships in order to streamline the process of defining type relationships.

1 | function subtype(parentCharacteristic) { |

1 | function isNaturalType (value) { |