Skip to content

Commit ee57e09

Browse files
help with more logging
1 parent d24b4d7 commit ee57e09

5 files changed

Lines changed: 70 additions & 53 deletions

File tree

src/scratch/trace-scratch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const program = (input: string) =>
2424
ManagedRuntime.make(
2525
Layer.mergeAll(Parser.Default, Evaluator.Default),
2626
).runPromise(
27-
program("diff(fn (x) { 1 / (2 * x + 3) })(3)").pipe(
27+
program("diff(fn(x) { sin(3 * x + 2) })(0)").pipe(
2828
Effect.provide(NodeSdkLive),
2929
Effect.catchAllCause(Effect.logInfo),
3030
),

src/services/diff/obj.ts

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
expectIdentEquivalence,
2626
} from "../../schemas/nodes/exps/ident";
2727
import { TokenType } from "../../schemas/token-types/union";
28+
import { objInspect } from "../object";
2829
import { makeLambda } from "./helper";
2930

3031
const baseBuiltInDiffFunc = (x: IdentExp) =>
@@ -70,12 +71,15 @@ const baseBuiltInDiffFunc = (x: IdentExp) =>
7071
x,
7172
);
7273

73-
yield* Effect.log("chain", chain);
74+
yield* Effect.log("chain", objInspect(chain));
7475

7576
return chain;
7677
}).pipe(
7778
Effect.tap((x) =>
78-
Effect.annotateCurrentSpan("chain", x),
79+
Effect.annotateCurrentSpan(
80+
"chain",
81+
objInspect(x),
82+
),
7983
),
8084
),
8185
),
@@ -226,58 +230,61 @@ const processTerm = Effect.fn("diff.process-term")(
226230

227231
// powerRule(ONE, ONE, x)
228232

229-
export const diffPolynomial = (
230-
obj: PolynomialObj,
231-
x: IdentExp,
232-
): Effect.Effect<PolynomialObj, ParseError | KennethParseError, never> =>
233-
Match.value(obj).pipe(
234-
Match.tag("IntegerObj", () => constantRule()), // leaf
235-
Match.tag("CallObj", baseBuiltInDiffFunc(x)),
236-
Match.tag("IdentObj", () => powerRule(ONE, ONE, x)), // leaf
237-
Match.tag("InfixObj", ({ left, operator, right }) =>
238-
Effect.all([
239-
Schema.decodeUnknown(PolynomialObj)(left),
240-
Schema.decodeUnknown(
241-
Schema.Literal(
242-
TokenType.MINUS,
243-
TokenType.PLUS,
244-
TokenType.ASTERISK,
245-
TokenType.SLASH,
246-
TokenType.EXPONENT,
247-
),
248-
)(operator),
249-
Schema.decodeUnknown(PolynomialObj)(right),
250-
]).pipe(
251-
Effect.flatMap(([left, operator, right]) =>
252-
Match.value(operator).pipe(
253-
Match.when(TokenType.ASTERISK, () => productRule(left, right, x)),
254-
Match.when(TokenType.SLASH, () => quotientRule(left, right, x)),
255-
Match.when(TokenType.EXPONENT, (operator) =>
256-
Match.value(left).pipe(
257-
Match.tag("InfixObj", () =>
258-
chainRule(
259-
InfixObj.make({
260-
left: IdentObj.make({ identExp: x }),
261-
operator,
262-
right,
263-
}),
264-
left,
265-
x,
233+
export const diffPolynomial = Effect.fn("diff.outer")(
234+
(
235+
obj: PolynomialObj,
236+
x: IdentExp,
237+
): Effect.Effect<PolynomialObj, ParseError | KennethParseError, never> =>
238+
Match.value(obj).pipe(
239+
Match.tag("IntegerObj", () => constantRule()), // leaf
240+
Match.tag("CallObj", baseBuiltInDiffFunc(x)),
241+
Match.tag("IdentObj", () => powerRule(ONE, ONE, x)), // leaf
242+
Match.tag("InfixObj", ({ left, operator, right }) =>
243+
Effect.all([
244+
Schema.decodeUnknown(PolynomialObj)(left),
245+
Schema.decodeUnknown(
246+
Schema.Literal(
247+
TokenType.MINUS,
248+
TokenType.PLUS,
249+
TokenType.ASTERISK,
250+
TokenType.SLASH,
251+
TokenType.EXPONENT,
252+
),
253+
)(operator),
254+
Schema.decodeUnknown(PolynomialObj)(right),
255+
]).pipe(
256+
Effect.flatMap(([left, operator, right]) =>
257+
Match.value(operator).pipe(
258+
Match.when(TokenType.ASTERISK, () => productRule(left, right, x)),
259+
Match.when(TokenType.SLASH, () => quotientRule(left, right, x)),
260+
Match.when(TokenType.EXPONENT, (operator) =>
261+
Match.value(left).pipe(
262+
Match.tag("InfixObj", () =>
263+
chainRule(
264+
InfixObj.make({
265+
left: IdentObj.make({ identExp: x }),
266+
operator,
267+
right,
268+
}),
269+
left,
270+
x,
271+
),
266272
),
273+
Match.orElse(() => processTerm(obj, x)),
267274
),
268-
Match.orElse(() => processTerm(obj, x)),
269275
),
276+
Match.when(TokenType.PLUS, (plus) =>
277+
sumAndDifferenceRule(left, right, x, plus),
278+
),
279+
Match.when(TokenType.MINUS, (minus) =>
280+
sumAndDifferenceRule(left, right, x, minus),
281+
),
282+
Match.exhaustive,
270283
),
271-
Match.when(TokenType.PLUS, (plus) =>
272-
sumAndDifferenceRule(left, right, x, plus),
273-
),
274-
Match.when(TokenType.MINUS, (minus) =>
275-
sumAndDifferenceRule(left, right, x, minus),
276-
),
277-
Match.exhaustive,
278284
),
279285
),
280286
),
287+
Match.exhaustive,
288+
Effect.tap((x) => Effect.log("outer obj", objInspect(obj))),
281289
),
282-
Match.exhaustive,
283-
);
290+
);

src/services/evaluator/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ export const evalDiff = (diffExp: DiffExp) =>
287287

288288
const expResult = yield* convertToExp(diffSoftEval);
289289

290+
yield* Effect.log("after convert", nodeString(expResult));
291+
290292
// yield* logDebug('exp', expResult)
291293
// yield* logDebug('--------------------------')
292294

src/services/object/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,17 @@ export const objMatch = $match;
2121
// compiler won't scream at you to remind you to handle that specific case.
2222
export const objInspect = (obj: Obj): string =>
2323
Match.value(obj).pipe(
24-
Match.tag("InfixObj", () => "infix obj"),
24+
Match.tag(
25+
"InfixObj",
26+
({ left, operator, right }) =>
27+
`(${objInspect(left)} ${operator} ${objInspect(right)})`,
28+
),
29+
Match.tag(
30+
"CallObj",
31+
({ args, fn }) => `${objInspect(fn)}(${args.map(objInspect).join(", ")})`,
32+
),
2533
Match.tag("IdentObj", ({ identExp: { value } }) => value),
26-
Match.tag("BuiltInObj", () => "builtin function"),
34+
Match.tag("BuiltInObj", ({ fn }) => fn),
2735
Match.tag(
2836
"FunctionObj",
2937
({ params, body }) => `

src/tests/vitest/eval/eval.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ describe("eval", () => {
446446
});
447447
describe("trig chain", () => {
448448
const tests = [
449-
["diff(fn(x) { sin(3 * x + 2) })(0)", 3 * Math.cos(3 * 0 + 2)],
449+
["diff(fn(x) { sin(3 * x + 2) })(1)", 3 * Math.cos(3 * 1 + 2)],
450450
["diff(fn(x) { 3 * x + 2 })(0)", 3],
451451
] as const;
452452

0 commit comments

Comments
 (0)