Exercises
A few exercises to practice the evaluation model. Each has a worked solution you can run in this engine (paste it into a .metta file and run metta-ts file.metta, or pass it to runProgram). Try them before peeking.
A reminder on the list encoding used below: the empty list is () and a cons cell is (:: head tail), so [A, B, C] is (:: A (:: B (:: C ()))).
1. Store facts, then query them
Store that Ada likes coffee and chocolate and Turing likes tea, then ask what Ada likes.
match searches the stored atoms (not sub-expressions) for the pattern and returns the template $thing for each match.
2. inc, dec, square
Define (inc $x), (dec $x), and (square $x) with grounded arithmetic.
3. classify by sign
Define (classify $x) returning Positive, Negative, or Zero. Nested if does it:
4. factorial
Grounded numbers cannot be deconstructed by pattern matching, so use if for the base case:
5. length of a list
Two rules, a base case and a recursive case:
Doing the same from TypeScript
Every exercise above can be written with the typed eDSL instead of source strings. Factorial, for example:
import { mettaDB, v, rel, iff, gt, mul, sub } from "@metta-ts/edsl";
const db = mettaDB();
const x = v<number>("x");
db.rule(rel("fact")(x), iff(gt(x, 0), mul(x, rel("fact")(sub(x, 1))), 1));
db.evalJs(rel("fact")(5)); // [120]