@orkestrel/reason 0.0.3 → 0.0.5

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.
@@ -3287,7 +3287,7 @@ export declare interface AggregatorOptions {
3287
3287
  * operation('abs', variable('x')) // unary — no right operand
3288
3288
  * ```
3289
3289
  */
3290
- export declare const operation: (operator: MathOperation, left: SymbolicExpression, right?: SymbolicExpression) => SymbolicExpression;
3290
+ export declare function operation(operator: MathOperation, left: SymbolicExpression, right?: SymbolicExpression): SymbolicExpression;
3291
3291
 
3292
3292
  /**
3293
3293
  * Parse a JSON string into a {@link Definition}, failing safe to `undefined`.
@@ -4693,7 +4693,7 @@ export declare interface AggregatorOptions {
4693
4693
  * transform stays exact-record valid; the transformer then applies its
4694
4694
  * per-operation default (`1` for `multiply` / `divide` / `power`, `0` otherwise).
4695
4695
  *
4696
- * @param operation - The math operation to apply
4696
+ * @param operator - The math operation to apply
4697
4697
  * @param operand - The operand (ignored by the unary operations)
4698
4698
  * @returns A fresh transform
4699
4699
  *
@@ -4705,7 +4705,7 @@ export declare interface AggregatorOptions {
4705
4705
  * transform('round') // { operation: 'round' }
4706
4706
  * ```
4707
4707
  */
4708
- export declare function transform(operation: MathOperation, operand?: number): Transform;
4708
+ export declare function transform(operator: MathOperation, operand?: number): Transform;
4709
4709
 
4710
4710
  /**
4711
4711
  * Applies math {@link Transform}s to numbers — the quantitative reasoner's
@@ -4720,13 +4720,14 @@ export declare interface AggregatorOptions {
4720
4720
  * a strict left fold — `NaN` flows through untouched. Stateless and
4721
4721
  * deterministic.
4722
4722
  */
4723
- export declare class Transformer implements TransformerInterface {
4723
+ declare class Transformer_2 implements TransformerInterface {
4724
4724
  #private;
4725
4725
  constructor(options?: TransformerOptions);
4726
4726
  get id(): string;
4727
4727
  apply(value: number, transform: Transform): number;
4728
4728
  chain(value: number, transforms: readonly Transform[]): number;
4729
4729
  }
4730
+ export { Transformer_2 as Transformer }
4730
4731
 
4731
4732
  /** Default `id` for a `Transformer`. */
4732
4733
  export declare const TRANSFORMER_ID = "transformer";
@@ -3287,7 +3287,7 @@ export declare interface AggregatorOptions {
3287
3287
  * operation('abs', variable('x')) // unary — no right operand
3288
3288
  * ```
3289
3289
  */
3290
- export declare const operation: (operator: MathOperation, left: SymbolicExpression, right?: SymbolicExpression) => SymbolicExpression;
3290
+ export declare function operation(operator: MathOperation, left: SymbolicExpression, right?: SymbolicExpression): SymbolicExpression;
3291
3291
 
3292
3292
  /**
3293
3293
  * Parse a JSON string into a {@link Definition}, failing safe to `undefined`.
@@ -4693,7 +4693,7 @@ export declare interface AggregatorOptions {
4693
4693
  * transform stays exact-record valid; the transformer then applies its
4694
4694
  * per-operation default (`1` for `multiply` / `divide` / `power`, `0` otherwise).
4695
4695
  *
4696
- * @param operation - The math operation to apply
4696
+ * @param operator - The math operation to apply
4697
4697
  * @param operand - The operand (ignored by the unary operations)
4698
4698
  * @returns A fresh transform
4699
4699
  *
@@ -4705,7 +4705,7 @@ export declare interface AggregatorOptions {
4705
4705
  * transform('round') // { operation: 'round' }
4706
4706
  * ```
4707
4707
  */
4708
- export declare function transform(operation: MathOperation, operand?: number): Transform;
4708
+ export declare function transform(operator: MathOperation, operand?: number): Transform;
4709
4709
 
4710
4710
  /**
4711
4711
  * Applies math {@link Transform}s to numbers — the quantitative reasoner's
@@ -4720,13 +4720,14 @@ export declare interface AggregatorOptions {
4720
4720
  * a strict left fold — `NaN` flows through untouched. Stateless and
4721
4721
  * deterministic.
4722
4722
  */
4723
- export declare class Transformer implements TransformerInterface {
4723
+ declare class Transformer_2 implements TransformerInterface {
4724
4724
  #private;
4725
4725
  constructor(options?: TransformerOptions);
4726
4726
  get id(): string;
4727
4727
  apply(value: number, transform: Transform): number;
4728
4728
  chain(value: number, transforms: readonly Transform[]): number;
4729
4729
  }
4730
+ export { Transformer_2 as Transformer }
4730
4731
 
4731
4732
  /** Default `id` for a `Transformer`. */
4732
4733
  export declare const TRANSFORMER_ID = "transformer";
@@ -104,7 +104,7 @@ var ReasonError = class extends Error {
104
104
  super(message);
105
105
  this.name = "ReasonError";
106
106
  this.code = code;
107
- this.context = context;
107
+ if (context !== void 0) this.context = context;
108
108
  }
109
109
  };
110
110
  /**
@@ -948,7 +948,7 @@ function rule(id, premises, conclusion, overrides) {
948
948
  * transform stays exact-record valid; the transformer then applies its
949
949
  * per-operation default (`1` for `multiply` / `divide` / `power`, `0` otherwise).
950
950
  *
951
- * @param operation - The math operation to apply
951
+ * @param operator - The math operation to apply
952
952
  * @param operand - The operand (ignored by the unary operations)
953
953
  * @returns A fresh transform
954
954
  *
@@ -960,9 +960,9 @@ function rule(id, premises, conclusion, overrides) {
960
960
  * transform('round') // { operation: 'round' }
961
961
  * ```
962
962
  */
963
- function transform(operation, operand) {
964
- return operand === void 0 ? { operation } : {
965
- operation,
963
+ function transform(operator, operand) {
964
+ return operand === void 0 ? { operation: operator } : {
965
+ operation: operator,
966
966
  operand
967
967
  };
968
968
  }
@@ -1050,7 +1050,7 @@ function constant(value) {
1050
1050
  * operation('abs', variable('x')) // unary — no right operand
1051
1051
  * ```
1052
1052
  */
1053
- var operation = (operator, left, right) => {
1053
+ function operation(operator, left, right) {
1054
1054
  return right === void 0 ? {
1055
1055
  form: "operation",
1056
1056
  operator,
@@ -1061,7 +1061,7 @@ var operation = (operator, left, right) => {
1061
1061
  left,
1062
1062
  right
1063
1063
  };
1064
- };
1064
+ }
1065
1065
  /**
1066
1066
  * Build an {@link Equation} — `left = right`, solved for `target`.
1067
1067
  *
@@ -2100,7 +2100,10 @@ function extractAtoms(expression) {
2100
2100
  continue;
2101
2101
  }
2102
2102
  const operands = node.operands;
2103
- for (let index = operands.length - 1; index >= 0; index--) if (index in operands) stack.push(operands[index]);
2103
+ for (let index = operands.length - 1; index >= 0; index--) {
2104
+ const operand = operands[index];
2105
+ if (operand !== void 0) stack.push(operand);
2106
+ }
2104
2107
  }
2105
2108
  return atoms;
2106
2109
  }
@@ -3428,10 +3431,7 @@ var Reason = class {
3428
3431
  constructor(options) {
3429
3432
  this.#bail = options?.bail ?? true;
3430
3433
  this.#validate = options?.validate ?? false;
3431
- this.#emitter = new Emitter({
3432
- on: options?.on,
3433
- error: options?.error
3434
- });
3434
+ this.#emitter = new Emitter(options);
3435
3435
  for (const reasoner of options?.reasoners ?? []) this.#reasoners.set(reasoner.reasoning, reasoner);
3436
3436
  }
3437
3437
  get emitter() {
@@ -4103,34 +4103,7 @@ var LogicalReasoner = class {
4103
4103
  if (typeof rule !== "object" || rule === null) continue;
4104
4104
  if (rule.enabled === false) trace.push(`Skipped rule "${rule.id}" (disabled)`);
4105
4105
  }
4106
- const prove = (rule, depth, visited) => {
4107
- if (depth > maxDepth) return false;
4108
- if (visited.has(rule.id)) return false;
4109
- const nextVisited = new Set(visited);
4110
- nextVisited.add(rule.id);
4111
- const currentSubject = {
4112
- ...subject,
4113
- ...derived
4114
- };
4115
- const premiseResults = [];
4116
- for (const premise of rule.premises) premiseResults.push(this.#establish(premise, currentSubject, sortedRules, depth + 1, nextVisited, derived, subject, trace, ruleResults, maxDepth));
4117
- const allMet = premiseResults.every(Boolean);
4118
- ruleResults.set(rule.id, {
4119
- id: rule.id,
4120
- applied: allMet,
4121
- premises: premiseResults,
4122
- conclusion: allMet
4123
- });
4124
- if (allMet) {
4125
- const conclusions = extractConclusions(rule.conclusion);
4126
- for (const [key, value] of Object.entries(conclusions)) if (!equalValues(derived[key], value)) {
4127
- derived[key] = value;
4128
- trace.push(`Rule "${rule.id}" derived: ${key}=${String(value)} (backward, depth ${depth})`);
4129
- }
4130
- } else trace.push(`Rule "${rule.id}": does not hold (backward, depth ${depth})`);
4131
- return allMet;
4132
- };
4133
- for (const rule of sortedRules) prove(rule, 0, /* @__PURE__ */ new Set());
4106
+ for (const rule of sortedRules) this.#proveRule(rule, 0, /* @__PURE__ */ new Set(), sortedRules, derived, subject, trace, ruleResults, maxDepth);
4134
4107
  const finalResults = [];
4135
4108
  for (const rule of sortedRules) {
4136
4109
  const existing = ruleResults.get(rule.id);
@@ -4145,6 +4118,33 @@ var LogicalReasoner = class {
4145
4118
  rules: finalResults
4146
4119
  };
4147
4120
  }
4121
+ #proveRule(rule, depth, visited, rules, derived, subject, trace, ruleResults, maxDepth) {
4122
+ if (depth > maxDepth) return false;
4123
+ if (visited.has(rule.id)) return false;
4124
+ const nextVisited = new Set(visited);
4125
+ nextVisited.add(rule.id);
4126
+ const currentSubject = {
4127
+ ...subject,
4128
+ ...derived
4129
+ };
4130
+ const premiseResults = [];
4131
+ for (const premise of rule.premises) premiseResults.push(this.#establish(premise, currentSubject, rules, depth + 1, nextVisited, derived, subject, trace, ruleResults, maxDepth));
4132
+ const allMet = premiseResults.every(Boolean);
4133
+ ruleResults.set(rule.id, {
4134
+ id: rule.id,
4135
+ applied: allMet,
4136
+ premises: premiseResults,
4137
+ conclusion: allMet
4138
+ });
4139
+ if (allMet) {
4140
+ const conclusions = extractConclusions(rule.conclusion);
4141
+ for (const [key, value] of Object.entries(conclusions)) if (!equalValues(derived[key], value)) {
4142
+ derived[key] = value;
4143
+ trace.push(`Rule "${rule.id}" derived: ${key}=${String(value)} (backward, depth ${depth})`);
4144
+ }
4145
+ } else trace.push(`Rule "${rule.id}": does not hold (backward, depth ${depth})`);
4146
+ return allMet;
4147
+ }
4148
4148
  #establish(expression, currentSubject, rules, depth, visited, derived, subject, trace, ruleResults, maxDepth) {
4149
4149
  if (this.#evaluateExpression(expression, currentSubject)) return true;
4150
4150
  return this.#proveExpression(expression, rules, depth, visited, derived, subject, trace, ruleResults, maxDepth);
@@ -4532,7 +4532,7 @@ var InferentialReasoner = class {
4532
4532
  return {
4533
4533
  reasoning: "inferential",
4534
4534
  derived: result.derived,
4535
- proof: result.proof,
4535
+ ...result.proof === void 0 ? {} : { proof: result.proof },
4536
4536
  success: errors.length === 0,
4537
4537
  trace,
4538
4538
  errors
@@ -4721,10 +4721,7 @@ var EquationManager = class {
4721
4721
  #destroyed = false;
4722
4722
  constructor(options) {
4723
4723
  this.#equations = options?.equations ?? [];
4724
- this.#emitter = new Emitter({
4725
- on: options?.on,
4726
- error: options?.error
4727
- });
4724
+ this.#emitter = new Emitter(options);
4728
4725
  }
4729
4726
  get emitter() {
4730
4727
  return this.#emitter;
@@ -4791,10 +4788,7 @@ var FactManager = class {
4791
4788
  #destroyed = false;
4792
4789
  constructor(options) {
4793
4790
  this.#facts = options?.facts ?? [];
4794
- this.#emitter = new Emitter({
4795
- on: options?.on,
4796
- error: options?.error
4797
- });
4791
+ this.#emitter = new Emitter(options);
4798
4792
  }
4799
4793
  get emitter() {
4800
4794
  return this.#emitter;
@@ -4863,10 +4857,7 @@ var FactorManager = class {
4863
4857
  #destroyed = false;
4864
4858
  constructor(groups, options) {
4865
4859
  this.#groups = groups;
4866
- this.#emitter = new Emitter({
4867
- on: options?.on,
4868
- error: options?.error
4869
- });
4860
+ this.#emitter = new Emitter(options);
4870
4861
  }
4871
4862
  get emitter() {
4872
4863
  return this.#emitter;
@@ -4935,10 +4926,7 @@ var GroupManager = class {
4935
4926
  #destroyed = false;
4936
4927
  constructor(options) {
4937
4928
  this.#groups = options?.groups ?? [];
4938
- this.#emitter = new Emitter({
4939
- on: options?.on,
4940
- error: options?.error
4941
- });
4929
+ this.#emitter = new Emitter(options);
4942
4930
  }
4943
4931
  get emitter() {
4944
4932
  return this.#emitter;
@@ -5005,10 +4993,7 @@ var InferenceManager = class {
5005
4993
  #destroyed = false;
5006
4994
  constructor(options) {
5007
4995
  this.#inferences = options?.inferences ?? [];
5008
- this.#emitter = new Emitter({
5009
- on: options?.on,
5010
- error: options?.error
5011
- });
4996
+ this.#emitter = new Emitter(options);
5012
4997
  }
5013
4998
  get emitter() {
5014
4999
  return this.#emitter;
@@ -5075,10 +5060,7 @@ var RuleManager = class {
5075
5060
  #destroyed = false;
5076
5061
  constructor(options) {
5077
5062
  this.#rules = options?.rules ?? [];
5078
- this.#emitter = new Emitter({
5079
- on: options?.on,
5080
- error: options?.error
5081
- });
5063
+ this.#emitter = new Emitter(options);
5082
5064
  }
5083
5065
  get emitter() {
5084
5066
  return this.#emitter;
@@ -5147,10 +5129,7 @@ var VariableManager = class {
5147
5129
  #destroyed = false;
5148
5130
  constructor(options) {
5149
5131
  this.#variables = options?.variables ?? {};
5150
- this.#emitter = new Emitter({
5151
- on: options?.on,
5152
- error: options?.error
5153
- });
5132
+ this.#emitter = new Emitter(options);
5154
5133
  }
5155
5134
  get emitter() {
5156
5135
  return this.#emitter;
@@ -5232,10 +5211,7 @@ var DefinitionBuilder = class {
5232
5211
  id: this.#id
5233
5212
  };
5234
5213
  this.#envelope = this.#strip(seeded);
5235
- this.#emitter = new Emitter({
5236
- on: options?.on,
5237
- error: options?.error
5238
- });
5214
+ this.#emitter = new Emitter(options);
5239
5215
  this.groups = options?.groups ?? new GroupManager({ groups: seeded.reasoning === "quantitative" ? seeded.groups : [] });
5240
5216
  this.factors = options?.factors ?? new FactorManager(this.groups);
5241
5217
  this.rules = options?.rules ?? new RuleManager({ rules: seeded.reasoning === "logical" ? seeded.rules : [] });
@@ -5422,10 +5398,7 @@ var SubjectBuilder = class {
5422
5398
  const { id: _id, ...rest } = seed;
5423
5399
  this.#subject = rest;
5424
5400
  }
5425
- this.#emitter = new Emitter({
5426
- on: options?.on,
5427
- error: options?.error
5428
- });
5401
+ this.#emitter = new Emitter(options);
5429
5402
  }
5430
5403
  get id() {
5431
5404
  return this.#id;