The Jest Programming Language
README.md

Jest Examples #

Files #

rosetta.jst — Core Language #

Classic programming problems demonstrating Jest's evaluation model:

  • Arithmetic (factorial, GCD, hypotenuse)
  • Lists (sum, reverse, range, quicksort)
  • Higher-order functions (map, filter)
  • Closures (make-adder, compose)
  • Macros (when — conditional with unevaluated args)
  • Mutual recursion via defrec (even?/odd?)

pattern-matching.jst — Pattern Matching & Inverse Evaluation #

Jest's most distinctive feature: patterns that invert computations.

  • Literal, variable, wildcard matching
  • Array destructuring with {"rest": ...} patterns
  • Object subset matching (multi-key patterns)
  • Guard patterns with predicate functions
  • Inverse evaluation: match {"+": [{"var": "a"}, 3]} against 10 to get a = 7
  • Inversions for cons, str, assoc, and all arithmetic ops
  • Pattern combinators (and-pattern, or-pattern, guard-pattern)

generic-dispatch.jst — Types & Multiple Dispatch #

User-defined types and CLOS-style generic functions:

  • deftype for self-evaluating types (values that survive eval unchanged)
  • defgeneric / defmethod for multiple dispatch on argument types
  • Specificity ranking (more specific type signatures win)
  • Rational arithmetic via generic dispatch (1/3 + 1/6 = 1/2)

kanren-demo.jst — Relational Programming (miniKanren) #

Logic programming with semantic unification:

  • Logic variables (lvar), unification, occurs check
  • Semantic unification: {"cons": [h, t]} understood as "array constructible via cons"
  • Arithmetic, string, and assoc inversions in relational context
  • appendo — the canonical test: runs forward, backward, and sideways

example_tests.jst — Test Harness #

Basic self-testing pattern using Jest's match-result and ==.

Running #

cargo run --release -- examples/rosetta.jst
cargo run --release -- examples/pattern-matching.jst
cargo run --release -- examples/generic-dispatch.jst
cargo run --release -- examples/kanren-demo.jst

Each file returns its last expression's value. Intermediate computations are evaluated but only the final result is printed (Jest's do semantics).

Key Syntax #

  • Functions: {"fn": [[params], body]} — creates an applicative (evaluates args)
  • Macros: {"macro": [[params], body]} — creates an operative (args unevaluated)
  • Recursion: {"defrec": [["name", [params], body], ...]} — mutually recursive group
  • Pattern match: {"match": [expr, [[pattern, body], ...]]} — first matching clause wins
  • Variables: {"var": "name"} — look up a name in scope
  • Definition: {"def": ["name", expr]} — bind a value; {"defn": ["name", [params], body]}