Operator Registry

MathJSON.jl uses a JSON-based registry system based on the Cortex Compute Engine standard. The registry defines operators, their categories, and Julia function mappings.

Cortex Compute Engine Compatibility

The operator definitions are sourced from the Cortex Compute Engine's OPERATORS.json, providing full compatibility with the MathJSON standard library.

File Structure

The registry consists of three JSON files in the data/ directory:

data/
├── categories.json        # Category definitions
├── operators.json         # Operator definitions (Cortex format)
├── julia_functions.json   # Julia function mappings
└── schemas/               # JSON Schema files for validation
    ├── categories.schema.json
    ├── operators.schema.json
    └── julia_functions.schema.json

Registry File Formats

categories.json

Defines operator categories matching the Cortex Compute Engine standard.

{
  "categories": [
    {
      "id": "Arithmetic",
      "name": "Arithmetic",
      "description": "Basic arithmetic operations and functions"
    }
  ]
}
FieldTypeRequiredDescription
idStringYesCategory identifier (e.g., "Arithmetic", "Trigonometry")
nameStringYesHuman-readable display name
descriptionStringYesBrief description of the category

operators.json

Defines operators using the Cortex Compute Engine format with full metadata.

{
  "operators": [
    {
      "name": "Add",
      "category": "Arithmetic",
      "arity": "variadic",
      "signature": "(value+) -> value",
      "associative": true,
      "commutative": true,
      "idempotent": true,
      "lazy": true,
      "broadcastable": true,
      "description": "Sum of two or more values.",
      "wikidata": "Q32043"
    }
  ]
}
FieldTypeRequiredDescription
nameStringYesMathJSON operator name
categoryStringYesCategory ID (must exist in categories.json)
arityStringNo"1", "2", "variadic", "1+", "2+", etc.
signatureStringNoType signature (e.g., "(number, number) -> number")
associativeBooleanNoWhether the operator is associative
commutativeBooleanNoWhether the operator is commutative
idempotentBooleanNoWhether the operator is idempotent
lazyBooleanNoWhether the operator uses lazy evaluation
broadcastableBooleanNoWhether the operator can be broadcast
descriptionStringNoBrief description
wikidataStringNoWikidata identifier (e.g., "Q32043")
examplesArrayNoExample expressions as strings

julia_functions.json

Maps operators to Julia functions for expression evaluation.

{
  "mappings": [
    {
      "operator": "Add",
      "julia_function": "+",
      "module": "Base"
    },
    {
      "operator": "And",
      "julia_function": null,
      "expression": "logical_and"
    }
  ]
}
FieldTypeRequiredDescription
operatorStringYesOperator name (must exist in operators.json)
julia_functionString/nullYesJulia function name or null if unmapped
moduleStringNoModule containing the function (default: "Base")
expressionStringNoKey for special anonymous functions

Special Functions: Some operators like And and Or require anonymous functions. These use the expression field to reference entries in the SPECIAL_FUNCTIONS dictionary:

const SPECIAL_FUNCTIONS = Dict{String,Function}(
    "logical_and" => (a, b) -> a && b,
    "logical_or" => (a, b) -> a || b,
    "logical_nand" => (a, b) -> !(a && b),
    "logical_nor" => (a, b) -> !(a || b),
    "logical_implies" => (a, b) -> !a || b,
    "logical_equivalent" => (a, b) -> a == b,
    "square" => x -> x^2,
    "nth_root" => (x, n) -> x^(1/n),
    # ... and more
)

Categories

MathJSON.jl supports all 15 Cortex Compute Engine categories:

CategoryDescription
ArithmeticBasic arithmetic operations and functions
CalculusCalculus operations (derivatives, integrals, limits)
CollectionsOperations on collections (lists, sequences, sets)
ColorsColor manipulation and conversion operations
CombinatoricsCombinatorial functions (factorial, binomial, permutations)
Control StructuresControl flow structures (if, loop, block)
CoreCore language constructs and meta-operations
Linear AlgebraLinear algebra operations (matrix, vector, decompositions)
LogicLogical operators and predicates
Number TheoryNumber theory functions (gcd, lcm, prime, divisibility)
PolynomialsPolynomial arithmetic and manipulation
Relational OperatorsComparison and relational operators
StatisticsStatistical functions (mean, variance, distributions)
TrigonometryTrigonometric and hyperbolic functions
UnitsPhysical units and quantity operations

Selected Operators by Category

Arithmetic (Sample)

OperatorJulia FunctionArityDescription
Add+variadicSum of two or more values
Subtract-2Subtraction
Multiply*variadicMultiplication
Divide/2Division
Power^2Exponentiation
Negate-1Negation
Sqrtsqrt1Square root
Absabs1Absolute value
Expexp1Exponential (e^x)
Lnlog1Natural logarithm
Loglog1-2Logarithm
Log10log101Base-10 logarithm
Log2log21Base-2 logarithm

Trigonometry (Sample)

OperatorJulia FunctionArityDescription
Sinsin1Sine
Coscos1Cosine
Tantan1Tangent
Arcsinasin1Inverse sine
Arccosacos1Inverse cosine
Arctanatan1Inverse tangent
Sinhsinh1Hyperbolic sine
Coshcosh1Hyperbolic cosine
Tanhtanh1Hyperbolic tangent
Arsinhasinh1Inverse hyperbolic sine
Arcoshacosh1Inverse hyperbolic cosine
Artanhatanh1Inverse hyperbolic tangent

Relational Operators

OperatorJulia FunctionArityDescription
Equal==2Equality
NotEqual!=2Inequality
Less<2Less than
Greater>2Greater than
LessEqual<=2Less than or equal
GreaterEqual>=2Greater than or equal
ApproxEqualisapprox2Approximate equality

Logic

OperatorJulia FunctionArityDescription
And(a, b) -> a && bvariadicLogical AND
Or(a, b) -> a || bvariadicLogical OR
Not!1Logical NOT
Xorxor2Exclusive OR
Nand(a, b) -> !(a && b)2NOT AND
Nor(a, b) -> !(a || b)2NOT OR
Implies(a, b) -> !a || b2Logical implication
Equivalent(a, b) -> a == b2Logical equivalence

Collections (Sample)

OperatorJulia FunctionArityDescription
UnionunionvariadicSet union
IntersectionintersectvariadicSet intersection
SetMinussetdiff2Set difference
Firstfirst1First element
Lastlast1Last element
Reversereverse1Reverse collection
Sortsort1Sort collection
Uniqueunique1Unique elements

Calculus

OperatorJulia FunctionArityDescription
D-1-2Derivative
ND-1+Numerical derivative
Integrate-1+Integral
NIntegrate-1+Numerical integration
Limit-2+Limit
Sumsum1+Summation
Productprod1+Product

Linear Algebra (Sample)

OperatorJulia FunctionArityDescription
Determinantdet1Matrix determinant
Transposetranspose1Matrix transpose
Inverseinv1Matrix inverse
Tracetr1Matrix trace
Normnorm1Vector/matrix norm
Rankrank1Matrix rank
Eigenvalueseigvals1Eigenvalues
Eigenvectorseigvecs1Eigenvectors

Statistics (Sample)

OperatorJulia FunctionArityDescription
Meanmean1Arithmetic mean
Medianmedian1Median value
Variancevar1Variance
StandardDeviationstd1Standard deviation
MinminvariadicMinimum
MaxmaxvariadicMaximum

Combinatorics (Sample)

OperatorJulia FunctionArityDescription
Factorialfactorial1Factorial
Binomialbinomial2Binomial coefficient
GCDgcd2+Greatest common divisor
LCMlcm2+Least common multiple

Adding New Operators

Step 1: Add Category (if needed)

Add a new entry to data/categories.json:

{
  "id": "MyCategory",
  "name": "My Category",
  "description": "Description of the category"
}

Note: You must also add the category to the OperatorCategory.T enum in src/operators.jl and the CATEGORY_ENUM_MAP dictionary.

Step 2: Add Operator

Add a new entry to data/operators.json:

{
  "name": "MyOperator",
  "category": "MyCategory",
  "arity": "2",
  "signature": "(number, number) -> number",
  "associative": false,
  "commutative": true,
  "idempotent": false,
  "lazy": false,
  "broadcastable": true,
  "description": "What this operator does"
}

Step 3: Add Julia Function Mapping

Add a new entry to data/julia_functions.json:

{
  "operator": "MyOperator",
  "julia_function": "my_julia_function",
  "module": "Base"
}

For operators without a direct Julia equivalent:

{
  "operator": "MyOperator",
  "julia_function": null
}

For operators requiring custom anonymous functions:

  1. Add to SPECIAL_FUNCTIONS in src/registry_loader.jl:

    "my_special" => (a, b) -> custom_logic(a, b)
  2. Reference in JSON:

    {
      "operator": "MyOperator",
      "julia_function": null,
      "expression": "my_special"
    }

Step 4: Run Tests

Validate your changes pass schema validation and all tests:

julia --project=. -e 'using Pkg; Pkg.test()'

API Reference

See the API Reference page for documentation of operator-related functions (get_category, get_julia_function, is_known_operator) and types (OperatorCategory).

Registry Types

MathJSON.CategoryInfoType
CategoryInfo

Stores information about an operator category loaded from JSON.

Fields

  • id::String: Category identifier (e.g., "Arithmetic")
  • name::String: Human-readable display name
  • description::String: Brief description of the category
source
MathJSON.OperatorInfoType
OperatorInfo

Stores information about an operator loaded from JSON. Compatible with Cortex Compute Engine OPERATORS.json format.

Fields

  • name::Symbol: Operator name as Symbol
  • category::String: Category name reference
  • arity::Union{String,Nothing}: Operator arity (e.g., "1", "2", "variadic")
  • signature::Union{String,Nothing}: Type signature (e.g., "(number, number) -> number")
  • associative::Bool: Whether the operator is associative
  • commutative::Bool: Whether the operator is commutative
  • idempotent::Bool: Whether the operator is idempotent
  • lazy::Bool: Whether the operator uses lazy evaluation
  • broadcastable::Bool: Whether the operator can be broadcast
  • description::Union{String,Nothing}: Operator description
  • wikidata::Union{String,Nothing}: Wikidata identifier
source
MathJSON.RegistryLoadErrorType
RegistryLoadError <: Exception

Exception raised when loading registry files fails.

Fields

  • path::String: Path to the file that caused the error
  • details::String: Detailed error message
source