-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcustom-matching.ts
More file actions
35 lines (31 loc) · 1.39 KB
/
Copy pathcustom-matching.ts
File metadata and controls
35 lines (31 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// SPDX-FileCopyrightText: 2026 MesTTo
//
// SPDX-License-Identifier: MIT
// Custom unification for a TypeScript type: subclass MatchableObject and override match_, then the core
// matcher calls into it. Here a Range value matches any integer within its bounds.
//
// Run it (after `pnpm build`): npx tsx examples/custom-matching.ts
import { MeTTa, G, MatchableObject, type Atom, type GroundedAtom } from "@mettascript/hyperon";
import { sym as coreSym, gint, matchAtoms } from "@mettascript/core";
class Range extends MatchableObject {
constructor(
readonly lo: number,
readonly hi: number,
) {
super({ lo, hi });
}
// Return one (empty) binding to signal a match, or none to signal no match.
override match_(other: Atom): unknown[] {
const g = other as GroundedAtom;
const n = typeof g.object === "function" ? (g.object().content as unknown) : undefined;
return typeof n === "number" && n >= this.lo && n <= this.hi ? [[]] : [];
}
}
// Make a Range atom and match integers against it via the core matcher.
const range = G(new Range(1, 10));
console.log("5 in [1,10]:", matchAtoms(range.catom, gint(5)).length === 1); // true
console.log("20 in [1,10]:", matchAtoms(range.catom, gint(20)).length === 1); // false
// It is just an atom, so it also works inside a runner.
const m = new MeTTa();
void m; // (a Range could be stored in a space and matched by a query)
void coreSym;