eyelang 1.3.2 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/guide.md CHANGED
@@ -579,4 +579,4 @@ For large programs, keep helper predicates selective, bind arguments early, docu
579
579
 
580
580
  ## Implementation limits
581
581
 
582
- Eyelang is intentionally smaller than ISO Prolog. It has no operators, zero-arity compound syntax, cut, modules, dynamic database updates, DCGs, or complete ISO library. Negation is negation-as-failure through `not/1`. Search is goal-directed and expected to be finite for the selected output goals. Output explanations are non-normative proof printouts and do not change answer semantics.
582
+ Eyelang is intentionally smaller than ISO Prolog. It has no operators, zero-arity compound syntax, cut, modules, dynamic database updates, DCGs, or complete ISO library. Arity-zero data is always written and read back as an atom, such as `nil`, never `nil()`. Negation is negation-as-failure through `not/1`. Search is goal-directed and expected to be finite for the selected output goals. Output explanations are non-normative proof printouts and do not change answer semantics.
@@ -222,7 +222,7 @@ Arity-zero data is written as an atom constant, not as a zero-arity compound:
222
222
  value(example, nil).
223
223
  ```
224
224
 
225
- The syntax `nil()` is intentionally rejected so eyelang source and read-back output use one representation for arity-zero data.
225
+ The syntax `nil()` is intentionally rejected so eyelang source and read-back output use one representation for arity-zero data. Host APIs SHOULD follow the same rule: constructing a term with an atom name and an empty argument list is canonicalized to the atom constant itself.
226
226
 
227
227
  ## 5. Terms
228
228
 
@@ -473,7 +473,7 @@ Context terms are data representations of atomic formulas and comma conjunctions
473
473
  | `holds(?context, ?name, ?args)` | Enumerates context members of any arity, exposing each member as atom constant `?name` plus a proper argument list `?args`. |
474
474
  | `functor(?term, ?name, ?arity)` | Decomposes a non-variable term into its name and arity. |
475
475
  | `arg(?index, ?term, ?arg)` | Extracts the 1-based argument of a compound term. |
476
- | `compound_name_arguments(?term, ?name, ?args)` | Decomposes a compound term or constructs one from an atom name and proper argument list. |
476
+ | `compound_name_arguments(?term, ?name, ?args)` | Decomposes a compound term, treats an atom as a zero-argument term, or constructs a term from an atom name and proper argument list. Empty `?args` constructs an atom. |
477
477
 
478
478
  Example:
479
479
 
@@ -483,6 +483,7 @@ holds((ready, name(alice, "Alice"), route(alice, bob, 7)), ?name, ?args).
483
483
  functor(route(alice, bob, 7), route, 3).
484
484
  arg(2, route(alice, bob, 7), bob).
485
485
  compound_name_arguments(?term, route, [alice, bob, 7]).
486
+ compound_name_arguments(nil, nil, []).
486
487
  ```
487
488
 
488
489
  The first goal can yield `holds((name(alice, "Alice"), knows(alice, bob)), name(alice, "Alice")).` The second can yield `holds((ready, name(alice, "Alice"), route(alice, bob, 7)), ready, []).`, `holds((ready, name(alice, "Alice"), route(alice, bob, 7)), name, [alice, "Alice"]).`, and `holds((ready, name(alice, "Alice"), route(alice, bob, 7)), route, [alice, bob, 7]).`
package/index.d.ts CHANGED
@@ -128,6 +128,7 @@ export function variable(name: string): Term;
128
128
  export function atom(name: string): Term;
129
129
  export function stringTerm(value: string): Term;
130
130
  export function numberTerm(value: string | number): Term;
131
+ /** Construct a compound term; an empty argument list is canonicalized to atom(name). */
131
132
  export function compound(name: string, args?: EyelangTerm[]): Term;
132
133
  export function emptyList(): Term;
133
134
  export function cons(head: EyelangTerm, tail: EyelangTerm): Term;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eyelang",
3
- "version": "1.3.2",
3
+ "version": "1.3.3",
4
4
  "description": "A small Prolog-like logic programming language for rules, goals, answers, and proofs.",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -20,7 +20,7 @@ function argReady(goal, env) {
20
20
 
21
21
  function compoundNameArgumentsReady(goal, env) {
22
22
  const term = deref(goal.args[0], env);
23
- if (term.type === 'compound') return true;
23
+ if (term.type === 'compound' || term.type === 'atom') return true;
24
24
  return term.type === 'var' && lexicalValue(goal.args[1], env) !== null && properListItems(goal.args[2], env) !== null;
25
25
  }
26
26
 
@@ -45,9 +45,10 @@ function* argBuiltin({ goal, env }) {
45
45
 
46
46
  function* compoundNameArguments({ goal, env }) {
47
47
  const term = deref(goal.args[0], env);
48
- if (term.type === 'compound') {
48
+ if (term.type === 'compound' || term.type === 'atom') {
49
49
  const next = env.clone();
50
- if (unify(goal.args[1], atom(term.name), next) && unify(goal.args[2], listFromItems(term.args), next)) yield next;
50
+ const args = term.type === 'compound' ? term.args : [];
51
+ if (unify(goal.args[1], atom(term.name), next) && unify(goal.args[2], listFromItems(args), next)) yield next;
51
52
  return;
52
53
  }
53
54
  if (term.type !== 'var') return;
package/src/term.js CHANGED
@@ -21,7 +21,7 @@ export const variable = (name) => new Term(VAR, name);
21
21
  export const atom = (name) => new Term(ATOM, name);
22
22
  export const stringTerm = (value) => new Term(STRING, value);
23
23
  export const numberTerm = (value) => new Term(NUMBER, value);
24
- export const compound = (name, args = []) => new Term(COMPOUND, name, args);
24
+ export const compound = (name, args = []) => args.length === 0 ? atom(name) : new Term(COMPOUND, name, args);
25
25
  export const emptyList = () => atom('[]');
26
26
  export const cons = (head, tail) => compound('.', [head, tail]);
27
27
 
@@ -113,17 +113,20 @@ export function unify(left, right, env) {
113
113
  }
114
114
 
115
115
  export function cloneTerm(term) {
116
+ if (term.type === COMPOUND && term.arity === 0) return atom(term.name);
116
117
  return new Term(term.type, term.name, term.args.map(cloneTerm));
117
118
  }
118
119
 
119
120
  export function freshTerm(term, suffix) {
120
121
  if (term.type === VAR) return variable(`${term.name}#${suffix}`);
122
+ if (term.type === COMPOUND && term.arity === 0) return atom(term.name);
121
123
  return new Term(term.type, term.name, term.args.map((arg) => freshTerm(arg, suffix)));
122
124
  }
123
125
 
124
126
  export function copyResolved(term, env) {
125
127
  const resolved = deref(term, env);
126
128
  if (resolved.type === VAR) return variable(resolved.name);
129
+ if (resolved.type === COMPOUND && resolved.arity === 0) return atom(resolved.name);
127
130
  return new Term(resolved.type, resolved.name, resolved.args.map((arg) => copyResolved(arg, env)));
128
131
  }
129
132
 
@@ -6,5 +6,6 @@ answer(functor_string, pair(?name, ?arity)) :- functor("hi", ?name, ?arity).
6
6
  answer(arg_nested, ?x) :- arg(1, path(edge(a, b), c), ?x).
7
7
  answer(compose_nested, ?x) :- compound_name_arguments(?x, outer, [inner(a), [b, c]]).
8
8
  answer(compose_atom_empty_args, ?x) :- compound_name_arguments(?x, z, []).
9
+ answer(decompose_atom_empty_args, pair(?name, ?args)) :- compound_name_arguments(z, ?name, ?args).
9
10
  answer(arg_zero_rejected, ok) :- not(arg(0, edge(a, b), ?x)).
10
11
  answer(arg_too_large_rejected, ok) :- not(arg(3, edge(a, b), ?x)).
@@ -4,5 +4,6 @@ answer(functor_string, pair("hi", 0)).
4
4
  answer(arg_nested, edge(a, b)).
5
5
  answer(compose_nested, outer(inner(a), [b, c])).
6
6
  answer(compose_atom_empty_args, z).
7
+ answer(decompose_atom_empty_args, pair(z, [])).
7
8
  answer(arg_zero_rejected, ok).
8
9
  answer(arg_too_large_rejected, ok).
@@ -337,6 +337,18 @@ function apiCases() {
337
337
  },
338
338
  },
339
339
 
340
+ {
341
+ name: 'compound factory canonicalizes zero arity to atoms',
342
+ run: () => {
343
+ const nil = compound('nil', []);
344
+ assertEqual(nil.type, 'atom', 'type');
345
+ assertEqual(nil.name, 'nil', 'name');
346
+ assertEqual(nil.arity, 0, 'arity');
347
+ assertEqual(termToString(nil, new Env(), true), 'nil', 'readback');
348
+ assertEqual(unify(nil, atom('nil'), new Env()), true, 'unifies with atom');
349
+ },
350
+ },
351
+
340
352
  {
341
353
  name: 'portable hash helpers match standard vectors',
342
354
  run: () => {