( ce: IComputeEngine, name: string, def: SequenceDefinition )
| 28 | } from './oeis.js'; |
| 29 | |
| 30 | export function declareSequence( |
| 31 | ce: IComputeEngine, |
| 32 | name: string, |
| 33 | def: SequenceDefinition |
| 34 | ): IComputeEngine { |
| 35 | // Validate basic requirements (without parsing) |
| 36 | if (!def.base || Object.keys(def.base).length === 0) { |
| 37 | throw new Error(`Sequence "${name}" requires at least one base case`); |
| 38 | } |
| 39 | if (!def.recurrence) { |
| 40 | throw new Error(`Sequence "${name}" requires a recurrence relation`); |
| 41 | } |
| 42 | |
| 43 | // Declare the symbol first with a placeholder handler |
| 44 | // This ensures the symbol exists when we parse the recurrence |
| 45 | ce.declare(name, { |
| 46 | subscriptEvaluate: () => undefined, |
| 47 | }); |
| 48 | |
| 49 | // Now validate and create the actual handler |
| 50 | const validation = validateSequenceDefinition(ce, name, def); |
| 51 | if (!validation.valid) { |
| 52 | throw new Error(validation.error); |
| 53 | } |
| 54 | |
| 55 | // Create the full subscriptEvaluate handler |
| 56 | const handler = createSequenceHandler(ce, name, def); |
| 57 | |
| 58 | // Update the symbol's subscriptEvaluate handler |
| 59 | // We need to access the internal definition to update it |
| 60 | const boxedDef = ce.lookupDefinition(name); |
| 61 | if (boxedDef && isValueDef(boxedDef)) { |
| 62 | boxedDef.value.subscriptEvaluate = handler; |
| 63 | } |
| 64 | |
| 65 | return ce; |
| 66 | } |
| 67 | |
| 68 | export function getSequenceStatus( |
| 69 | ce: IComputeEngine, |
nothing calls this directly
no test coverage detected