Inside Cortex

A Cortex program is an expression that gets transformed into an IR (Intermediate Representation) expressed in MathJSON, compiled and evaluated by the Cortex Compute Engine.

The process to convert a Cortex program into a MathJSON expression is pretty straightforward:

  • Function calls gets converted into MathJSON functions:
Print("x =", x)
["Print", "'x ='", "x"]
  • String get converted into MathJSON string. Interpolated strings get converted into MathJSON String functions:
"The solution is \(x)"
["String", "'The solution is '", "x"]
  • Operators get converted into equivalent MathJSON functions:
2x + 1
["Add", ["Multiply", 2, "x"], 1]
  • Collections (List, Set, Tuple, Sequence, Dicitionary) get converted into a corresponding MathJSON expression:
set =  {2, 5, 7, 11, 13}
list = [2, 7, 2, 4, 2]
tuple = (1.5, 0.5)
sequence = 2, 5, 7
["Assign", "set", ["Set", 2, 5, 7, 11, 13]]
["Assign", "list", ["List", 2, 7, 2, 4, 2])]
["Assign", "tuple", ["Tuple", 1.5, 0.5])]
["Assign", "sequence", ["Sequence", 2, 5, 7])]
  • Control structures get converted to an appropriate expression:
if (x in PrimeNumber) {
  Print(x);
   x += 1;
} else {
  x += 2;
}
[
  "If",
  ["Element", "x", "PrimeNumber"],
  ["Do", ["Print", "x"], ["Equal", "x", ["Add", "x", 1]]],
  ["Add", "x", 2]
]