Getting Started
Install Epsil and run your first symbolic program in five minutes.
Epsil is experimental. Its syntax and behavior may change between releases.
Install
Epsil is included with the Compute Engine package:
npm install @cortex-js/compute-engine
The package installs an epsil command. During development, run the
project-local command through npx.
Try the REPL
Start an interactive session:
npx epsil
Enter a declaration, then use it in another expression:
epsil> let x = 5
5
epsil> x^2
25
The REPL keeps declarations and assignments between inputs. Enter .help for
the available commands and .exit when you are done.
Run a Source File
Save this program as squares.epsil:
square(x) = x^2
map(square, 1..5)
Run it:
npx epsil squares.epsil
The result is:
[1,4,9,16,25]
The conventional file extension is .epsil.
Work Symbolically
Expressions remain exact and symbolic by default:
Use N() when you want a numeric approximation:
Embed Epsil in JavaScript
Import the experimental Epsil entry point, create a ComputeEngine, then
execute source text:
import {
ComputeEngine,
executeEpsil,
} from "@cortex-js/compute-engine/epsil";
const ce = new ComputeEngine();
const { value, diagnostics } = executeEpsil(
ce,
"factorial(n) = 1 if n <= 1 else n * factorial(n - 1)\nfactorial(10)"
);
if (diagnostics.length > 0) console.error(diagnostics);
console.log(value.toString()); // 3628800
Calls made with the same ComputeEngine share its top-level declarations,
which is useful for notebook cells and other stateful sessions. Create a fresh
engine when you want an isolated program.
To check a program without running it, for example after each edit in an
editor, use checkEpsil. It gives the parse errors and the static type errors
that executeEpsil reports before it runs a statement. It assigns no value,
defines no function and declares no type on the engine, so it is safe on a
shared engine, and a type that the engine already knows is not an error:
import { ComputeEngine, checkEpsil } from "@cortex-js/compute-engine/epsil";
const ce = new ComputeEngine();
const { diagnostics } = checkEpsil(ce, 'let b = "x" + 1');
console.log(diagnostics[0].message[0]); // static-type-error
console.log(diagnostics[0].code); // incompatible-type
A static type error has the engine error code in its code field, so a host
can tell one static error from another without reading the message. The
range of a diagnostic is the [start, end) offsets of the source it is
about: for a name that a statement reads before its let, the range of that
name.
The message of a diagnostic is a code and its arguments. To show it to a
user, epsilDiagnosticMessage gives the sentence that the epsil command line
shows, without the code:
import { ComputeEngine, checkEpsil, epsilDiagnosticMessage } from "@cortex-js/compute-engine/epsil";
const ce = new ComputeEngine();
const { diagnostics } = checkEpsil(ce, "let x: notatype = 1");
console.log(diagnostics[0].message[0]); // type-annotation-error
console.log(epsilDiagnosticMessage(diagnostics[0])); // Unknown type "notatype"
An error that only a run finds, such as a division by zero in a loop, is not in these diagnostics.