# Domains

A domain, such as Integer Boolean, is a set used to represent the possible values of an expression.

Domains are similar to types in programming languages. They are used to select the correct function definition. For example a function Add could operate either on numbers or matrixes. The domain of the arguments indicates the appropriate function definition. Symbolic manipulation algorithms use domains to decide when certain manipulations are applicable. For example, $$\sqrt{x^2} = x$$ only if $$x \geq 0$$

To query the domain of an expression, use the domain() function.

ce.domain('Pi');
// -> "IrrationalNumber"


## Domain Lattice

Domains are defined in a hierarchy (a lattice). The upper bound of the domain lattice is the Anything domain and its lower bound is the Nothing domain.

The parent of a domain represents a is-a/subset-of relationship, for example, a List is-a Collection.    The implementation of the CortexJS domains is based on Weibel, Trudy & Gonnet, Gaston. (1991). An Algebra of Properties… 352-359. 10.1145/120694.120749. .

## Defining New Domains

A new domain can be defined using a domain expression, that is a set expression using any of the set functions: Union Intersection SetMinus…, combined with domains and parametric domain functions.

//  A number or a boolean.
["Union", "Number", "Boolean"]

// Any number except "1".
["SetMinus", "Number", 1]


Parametric domains are functions that define a domain:

// Range of non-negative integers
["Range", 0, "+Infinity"]


The ["Range", <min>, <max>] parametric domain defines a set of integers such that $$\mathord{min} \le n \le \mathord{max}, n \in \N$$.

The ["Interval", <min>, <max>] parametric domain defines a set of real numbers such that $$\mathord{min} \le x \le \mathord{max}, n \in \R$$.

To represent an open interval, use the Open function: ["Interval", ["Open", <min>], <max>] $$\operatorname{min} \lt x \le \operatorname{max}, n \in \R$$ or $$x \in \rbrack \operatorname{min}, \operatorname{max} \rbrack$$.

Parametric Domain Description
Function ["Function", ...<arg-domain>, <co-domain>]
For example, ["Function", "Number", "Boolean"] is the domain of the functions that have a single argument, a number, and return a boolean (has a boolean codomain).
Interval ["Interval", <min>, <max>]
The set of real numbers between <min> and <max>. Use ["Interval", ["Open", <min>], <max>] to indicate an open interval.
Multiple ["Multiple", <factor>, <domain>, <offset>]
The set of numbers that satisfy <factor> * x + <offset> with x in domain. For example, the set of odd numbers is ["Multiple", 2, "Integer", 1]
Range ["Range", <min>, <max>]
The set of integers from <min> to <max> (inclusive).

## Simplifying Domains

To simplify a domain expression, use ce.simplify(<domain>).

ce.simplify(["SetMinus", "Integer", ["Range", "-Infinity", 0]]);
// ➔ ["Range", 1, "+Infinity]]

ce.simplify(
["Union", ["Number", 0, "+Infinity"], ["Number", "-Infinity", 5]],
);
// ➔ "ExtendedRealNumber"