|
| 1 | +import { StepperBaseNode } from '../../interface'; |
| 2 | +import { StepperExpression, StepperPattern } from '../index'; |
| 3 | + |
| 4 | +export class StepperFunctionApplication implements StepperBaseNode { |
| 5 | + type = 'FunctionApplication'; |
| 6 | + operator: StepperBaseNode; |
| 7 | + operands: StepperBaseNode[]; |
| 8 | + |
| 9 | + constructor(operator: StepperBaseNode, operands: StepperBaseNode[]) { |
| 10 | + this.operator = operator; |
| 11 | + this.operands = operands; |
| 12 | + } |
| 13 | + |
| 14 | + static create(node: any): StepperFunctionApplication { |
| 15 | + // This will be handled by the convertNode function in generator.ts |
| 16 | + return new StepperFunctionApplication( |
| 17 | + node.operator, |
| 18 | + node.operands |
| 19 | + ); |
| 20 | + } |
| 21 | + |
| 22 | + isContractible(): boolean { |
| 23 | + // Check if operator is a lambda and all operands are literals |
| 24 | + if (this.operator.type === 'LambdaExpression') { |
| 25 | + return this.operands.every(op => op.isContractible()); |
| 26 | + } |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + isOneStepPossible(): boolean { |
| 31 | + // Can step if any operand is not contractible, or if operator is lambda and all operands are contractible |
| 32 | + return this.operands.some(op => !op.isContractible()) || this.isContractible(); |
| 33 | + } |
| 34 | + |
| 35 | + contract(): StepperBaseNode { |
| 36 | + if (!this.isContractible()) { |
| 37 | + throw new Error('Cannot contract non-contractible expression'); |
| 38 | + } |
| 39 | + |
| 40 | + // Perform beta-reduction for lambda applications |
| 41 | + if (this.operator.type === 'LambdaExpression') { |
| 42 | + const lambda = this.operator as any; |
| 43 | + let body = lambda.body; |
| 44 | + |
| 45 | + // Substitute parameters with arguments |
| 46 | + for (let i = 0; i < lambda.params.length && i < this.operands.length; i++) { |
| 47 | + const param = lambda.params[i]; |
| 48 | + const arg = this.operands[i]; |
| 49 | + body = body.substitute(param, arg); |
| 50 | + } |
| 51 | + |
| 52 | + return body; |
| 53 | + } |
| 54 | + |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + oneStep(): StepperBaseNode { |
| 59 | + // First, step any non-contractible operands |
| 60 | + const steppedOperands = this.operands.map(op => |
| 61 | + op.isContractible() ? op : op.oneStep() |
| 62 | + ); |
| 63 | + |
| 64 | + // If all operands are now contractible and operator is lambda, contract |
| 65 | + if (this.operator.type === 'LambdaExpression' && |
| 66 | + steppedOperands.every(op => op.isContractible())) { |
| 67 | + return this.contract(); |
| 68 | + } |
| 69 | + |
| 70 | + return new StepperFunctionApplication(this.operator, steppedOperands); |
| 71 | + } |
| 72 | + |
| 73 | + substitute(id: StepperPattern, value: StepperExpression): StepperBaseNode { |
| 74 | + return new StepperFunctionApplication( |
| 75 | + this.operator.substitute(id, value), |
| 76 | + this.operands.map(op => op.substitute(id, value)) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + freeNames(): string[] { |
| 81 | + return [ |
| 82 | + ...this.operator.freeNames(), |
| 83 | + ...this.operands.flatMap(op => op.freeNames()) |
| 84 | + ]; |
| 85 | + } |
| 86 | + |
| 87 | + allNames(): string[] { |
| 88 | + return [ |
| 89 | + ...this.operator.allNames(), |
| 90 | + ...this.operands.flatMap(op => op.allNames()) |
| 91 | + ]; |
| 92 | + } |
| 93 | + |
| 94 | + rename(before: string, after: string): StepperBaseNode { |
| 95 | + return new StepperFunctionApplication( |
| 96 | + this.operator.rename(before, after), |
| 97 | + this.operands.map(op => op.rename(before, after)) |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + toString(): string { |
| 102 | + return `(${this.operator.toString()} ${this.operands.map(op => op.toString()).join(' ')})`; |
| 103 | + } |
| 104 | +} |
| 105 | + |
0 commit comments