Skip to content

Commit a84d1d6

Browse files
even more fn use
1 parent 7796c1c commit a84d1d6

4 files changed

Lines changed: 73 additions & 65 deletions

File tree

src/schemas/objs/unions/polynomials.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,20 @@ export const sumAndDifferenceRule = Effect.fn("diff.sumAndDifferenceRule")(
9090
),
9191
);
9292

93-
export const powerRule = (coeff: IntegerObj, power: IntegerObj, x: IdentExp) =>
94-
newTerm(
95-
coeff.value * power.value,
96-
IdentObj.make({ identExp: x }),
97-
power.value - 1,
98-
);
93+
export const powerRule = Effect.fn("diff.powerRule")(
94+
(coeff: IntegerObj, power: IntegerObj, x: IdentExp) =>
95+
Effect.succeed(
96+
newTerm(
97+
coeff.value * power.value,
98+
IdentObj.make({ identExp: x }),
99+
power.value - 1,
100+
),
101+
),
102+
);
99103

100-
export const constantRule = () => Effect.succeed(IntegerObj.make({ value: 0 }));
104+
export const constantRule = Effect.fn("diff.constantRule")(() =>
105+
Effect.succeed(IntegerObj.make({ value: 0 })),
106+
);
101107

102108
export const recursivelySubstitute = Effect.fn("diff.recursivelySubstitute")(
103109
(

src/scratch/trace-scratch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ ManagedRuntime.make(
2626
).runPromise(
2727
program("diff(fn (x) { 1 / (2 * x + 3) })(3)").pipe(
2828
Effect.provide(NodeSdkLive),
29-
Effect.catchAllCause(Effect.logError),
29+
Effect.catchAllCause(Effect.logInfo),
3030
),
3131
);

src/services/diff/obj.ts

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ const baseBuiltInDiffFunc =
7171
x,
7272
);
7373

74+
yield* Effect.log("chain", chain);
75+
7476
return chain;
7577
}).pipe(
7678
Effect.tap((x) =>
@@ -146,7 +148,7 @@ const baseBuiltInDiffFunc =
146148
const processTerm = (exp: PolynomialObj, x: IdentExp) =>
147149
Match.value(exp).pipe(
148150
Match.tag("IntegerObj", () => constantRule()),
149-
Match.tag("IdentObj", () => Effect.succeed(powerRule(ONE, ONE, x))),
151+
Match.tag("IdentObj", () => powerRule(ONE, ONE, x)),
150152
Match.tag("InfixObj", ({ left, operator, right }) =>
151153
Schema.decodeUnknown(
152154
Schema.Literal(TokenType.ASTERISK, TokenType.EXPONENT),
@@ -172,7 +174,7 @@ const processTerm = (exp: PolynomialObj, x: IdentExp) =>
172174
Schema.decodeUnknown(IntegerObj)(secondRight),
173175
]).pipe(
174176
Effect.flatMap(([operator, power]) =>
175-
Effect.succeed(powerRule(coeff, power, x)),
177+
powerRule(coeff, power, x),
176178
),
177179
),
178180
),
@@ -192,7 +194,7 @@ const processTerm = (exp: PolynomialObj, x: IdentExp) =>
192194
const integerObj =
193195
yield* Schema.decodeUnknown(IntegerObj)(right);
194196

195-
return powerRule(ONE, integerObj, x);
197+
return yield* powerRule(ONE, integerObj, x);
196198
}),
197199
),
198200
),
@@ -207,59 +209,60 @@ const processTerm = (exp: PolynomialObj, x: IdentExp) =>
207209
Effect.withSpan("diff.process_term"),
208210
);
209211

210-
export const diffPolynomial = (
211-
obj: PolynomialObj,
212-
x: IdentExp,
213-
): Effect.Effect<PolynomialObj, ParseError | KennethParseError, never> =>
214-
Match.value(obj).pipe(
215-
Match.tag("IntegerObj", () => constantRule()), // leaf
216-
Match.tag("CallObj", baseBuiltInDiffFunc(x)),
217-
Match.tag("IdentObj", () => Effect.succeed(powerRule(ONE, ONE, x))), // leaf
218-
Match.tag("InfixObj", ({ left, operator, right }) =>
219-
Effect.all([
220-
Schema.decodeUnknown(PolynomialObj)(left),
221-
Schema.decodeUnknown(
222-
Schema.Literal(
223-
TokenType.MINUS,
224-
TokenType.PLUS,
225-
TokenType.ASTERISK,
226-
TokenType.SLASH,
227-
TokenType.EXPONENT,
228-
),
229-
)(operator),
230-
Schema.decodeUnknown(PolynomialObj)(right),
231-
]).pipe(
232-
Effect.flatMap(([left, operator, right]) =>
233-
Match.value(operator).pipe(
234-
Match.when(TokenType.ASTERISK, () => productRule(left, right, x)),
235-
Match.when(TokenType.SLASH, () => quotientRule(left, right, x)),
236-
Match.when(TokenType.EXPONENT, (operator) =>
237-
Match.value(left).pipe(
238-
Match.tag("InfixObj", () =>
239-
chainRule(
240-
InfixObj.make({
241-
left: IdentObj.make({ identExp: x }),
242-
operator,
243-
right,
244-
}),
245-
left,
246-
x,
212+
export const diffPolynomial = Effect.fn("diff.outer")(
213+
(
214+
obj: PolynomialObj,
215+
x: IdentExp,
216+
): Effect.Effect<PolynomialObj, ParseError | KennethParseError, never> =>
217+
Match.value(obj).pipe(
218+
Match.tag("IntegerObj", () => constantRule()), // leaf
219+
Match.tag("CallObj", baseBuiltInDiffFunc(x)),
220+
Match.tag("IdentObj", () => powerRule(ONE, ONE, x)), // leaf
221+
Match.tag("InfixObj", ({ left, operator, right }) =>
222+
Effect.all([
223+
Schema.decodeUnknown(PolynomialObj)(left),
224+
Schema.decodeUnknown(
225+
Schema.Literal(
226+
TokenType.MINUS,
227+
TokenType.PLUS,
228+
TokenType.ASTERISK,
229+
TokenType.SLASH,
230+
TokenType.EXPONENT,
231+
),
232+
)(operator),
233+
Schema.decodeUnknown(PolynomialObj)(right),
234+
]).pipe(
235+
Effect.flatMap(([left, operator, right]) =>
236+
Match.value(operator).pipe(
237+
Match.when(TokenType.ASTERISK, () => productRule(left, right, x)),
238+
Match.when(TokenType.SLASH, () => quotientRule(left, right, x)),
239+
Match.when(TokenType.EXPONENT, (operator) =>
240+
Match.value(left).pipe(
241+
Match.tag("InfixObj", () =>
242+
chainRule(
243+
InfixObj.make({
244+
left: IdentObj.make({ identExp: x }),
245+
operator,
246+
right,
247+
}),
248+
left,
249+
x,
250+
),
247251
),
252+
Match.orElse(() => processTerm(obj, x)),
248253
),
249-
Match.orElse(() => processTerm(obj, x)),
250254
),
255+
Match.when(TokenType.PLUS, (plus) =>
256+
sumAndDifferenceRule(left, right, x, plus),
257+
),
258+
Match.when(TokenType.MINUS, (minus) =>
259+
sumAndDifferenceRule(left, right, x, minus),
260+
),
261+
Match.exhaustive,
251262
),
252-
Match.when(TokenType.PLUS, (plus) =>
253-
sumAndDifferenceRule(left, right, x, plus),
254-
),
255-
Match.when(TokenType.MINUS, (minus) =>
256-
sumAndDifferenceRule(left, right, x, minus),
257-
),
258-
Match.exhaustive,
259263
),
260264
),
261265
),
266+
Match.exhaustive,
262267
),
263-
Match.exhaustive,
264-
Effect.withSpan("diff.outer"),
265-
);
268+
);

src/services/evaluator/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ export const Eval =
189189
> =>
190190
nodeEvalMatch(env)(node).pipe(Effect.withSpan("eval.Eval"));
191191

192-
export const evalDiff = (diffExp: DiffExp) => (env: Environment) =>
193-
Effect.gen(function* () {
192+
export const evalDiff = (diffExp: DiffExp) =>
193+
Effect.fn("eval.Diff")(function* (env: Environment) {
194194
// yield* logDebug('evalDiff')
195195
// this is during running this function. the diff function to be sure! so it will return a number
196196
// NEED to do a soft eval of all the expressions here.
@@ -199,15 +199,12 @@ export const evalDiff = (diffExp: DiffExp) => (env: Environment) =>
199199
outer: env.outer,
200200
idents: [...env.idents, ...diffExp.params],
201201
});
202-
yield* Effect.log("soft eval:");
203202
const softEval = yield* Eval(diffExp.exp)(newEnv).pipe(
204203
Effect.flatMap(Schema.decodeUnknown(PolynomialObj)),
205204
);
206205

207-
yield* Effect.log("diff:");
208206
const diffSoftEval = yield* diffPolynomial(softEval, diffExp.params[0]);
209207

210-
yield* Effect.log("diff-eval", diffSoftEval);
211208
// maybe simplest will be to convert back to exp and Eval.
212209
const convertToExp = (obj: PolynomialObj): Effect.Effect<Exp, ParseError> =>
213210
Match.value(obj).pipe(
@@ -265,7 +262,9 @@ export const evalDiff = (diffExp: DiffExp) => (env: Environment) =>
265262
),
266263
Match.tag("FunctionObj", ({ params, body }) =>
267264
FuncExp.make({
268-
token: fnTokenSchema.make({ literal: "fn" }),
265+
token: fnTokenSchema.make({
266+
literal: "fn",
267+
}),
269268
parameters: params,
270269
body,
271270
}),

0 commit comments

Comments
 (0)