Skip to content

Commit 7796c1c

Browse files
clean up scratch program, start using effect.fn for traces
1 parent b930ec6 commit 7796c1c

3 files changed

Lines changed: 116 additions & 112 deletions

File tree

src/schemas/objs/unions/polynomials.ts

Lines changed: 75 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,40 @@ export const quotientRule = (f: PolynomialObj, g: PolynomialObj, x: IdentExp) =>
5555
),
5656
);
5757

58-
export const productRule = (f: PolynomialObj, g: PolynomialObj, x: IdentExp) =>
59-
diffBoth(f, g, x).pipe(
60-
Effect.flatMap(([df, dg]) =>
61-
Effect.succeed(
62-
InfixObj.make({
63-
left: OpInfixObj(TokenType.ASTERISK)(df, g),
64-
operator: TokenType.PLUS,
65-
right: OpInfixObj(TokenType.ASTERISK)(f, dg),
66-
}),
58+
export const productRule = Effect.fn("diff.productRule")(
59+
(f: PolynomialObj, g: PolynomialObj, x: IdentExp) =>
60+
diffBoth(f, g, x).pipe(
61+
Effect.flatMap(([df, dg]) =>
62+
Effect.succeed(
63+
InfixObj.make({
64+
left: OpInfixObj(TokenType.ASTERISK)(df, g),
65+
operator: TokenType.PLUS,
66+
right: OpInfixObj(TokenType.ASTERISK)(f, dg),
67+
}),
68+
),
6769
),
6870
),
69-
);
71+
);
7072

71-
export const sumAndDifferenceRule = (
72-
f: PolynomialObj,
73-
g: PolynomialObj,
74-
x: IdentExp,
75-
operator: typeof TokenType.PLUS | typeof TokenType.MINUS,
76-
) =>
77-
diffBoth(f, g, x).pipe(
78-
Effect.flatMap(([df, dg]) =>
79-
Effect.succeed(
80-
InfixObj.make({
81-
left: df,
82-
operator,
83-
right: dg,
84-
}),
73+
export const sumAndDifferenceRule = Effect.fn("diff.sumAndDifferenceRule")(
74+
(
75+
f: PolynomialObj,
76+
g: PolynomialObj,
77+
x: IdentExp,
78+
operator: typeof TokenType.PLUS | typeof TokenType.MINUS,
79+
) =>
80+
diffBoth(f, g, x).pipe(
81+
Effect.flatMap(([df, dg]) =>
82+
Effect.succeed(
83+
InfixObj.make({
84+
left: df,
85+
operator,
86+
right: dg,
87+
}),
88+
),
8589
),
8690
),
87-
);
91+
);
8892

8993
export const powerRule = (coeff: IntegerObj, power: IntegerObj, x: IdentExp) =>
9094
newTerm(
@@ -95,56 +99,60 @@ export const powerRule = (coeff: IntegerObj, power: IntegerObj, x: IdentExp) =>
9599

96100
export const constantRule = () => Effect.succeed(IntegerObj.make({ value: 0 }));
97101

98-
export const recursivelySubstitute = (
99-
f: PolynomialObj,
100-
g: PolynomialObj,
101-
x: IdentExp,
102-
): Effect.Effect<PolynomialObj, ParseError, never> =>
103-
Match.value(f).pipe(
104-
Match.tag("IdentObj", () => Effect.succeed(g)),
105-
Match.tag("IntegerObj", (intObj) => Effect.succeed(intObj)),
106-
Match.tag("InfixObj", ({ left, operator, right }) =>
107-
Effect.all([
108-
Schema.decodeUnknown(PolynomialObj)(left),
109-
Schema.decodeUnknown(PolynomialObj)(right),
110-
]).pipe(
111-
Effect.flatMap(([left, right]) =>
112-
Effect.all([
113-
recursivelySubstitute(left, g, x),
114-
recursivelySubstitute(right, g, x),
115-
]).pipe(
116-
Effect.flatMap(([left, right]) =>
117-
Effect.succeed(
118-
InfixObj.make({
119-
left,
120-
operator,
121-
right,
122-
}),
102+
export const recursivelySubstitute = Effect.fn("diff.recursivelySubstitute")(
103+
(
104+
f: PolynomialObj,
105+
g: PolynomialObj,
106+
x: IdentExp,
107+
): Effect.Effect<PolynomialObj, ParseError, never> =>
108+
Match.value(f).pipe(
109+
Match.tag("IdentObj", () => Effect.succeed(g)),
110+
Match.tag("IntegerObj", (intObj) => Effect.succeed(intObj)),
111+
Match.tag("InfixObj", ({ left, operator, right }) =>
112+
Effect.all([
113+
Schema.decodeUnknown(PolynomialObj)(left),
114+
Schema.decodeUnknown(PolynomialObj)(right),
115+
]).pipe(
116+
Effect.flatMap(([left, right]) =>
117+
Effect.all([
118+
recursivelySubstitute(left, g, x),
119+
recursivelySubstitute(right, g, x),
120+
]).pipe(
121+
Effect.flatMap(([left, right]) =>
122+
Effect.succeed(
123+
InfixObj.make({
124+
left,
125+
operator,
126+
right,
127+
}),
128+
),
123129
),
124130
),
125131
),
126132
),
127133
),
134+
Match.tag("CallObj", ({ fn }) =>
135+
Effect.succeed(CallObj.make({ fn, args: [g] })),
136+
),
137+
Match.exhaustive,
128138
),
129-
Match.tag("CallObj", ({ fn }) =>
130-
Effect.succeed(CallObj.make({ fn, args: [g] })),
131-
),
132-
Match.exhaustive,
133-
);
139+
);
134140

135-
export const chainRule = (f: PolynomialObj, g: PolynomialObj, x: IdentExp) =>
136-
Effect.all([diffPolynomial(f, x), diffPolynomial(g, x)]).pipe(
137-
Effect.flatMap(([left, right]) =>
138-
recursivelySubstitute(left, g, x).pipe(
139-
Effect.flatMap((left) =>
140-
Effect.succeed(
141-
InfixObj.make({
142-
left,
143-
operator: TokenType.ASTERISK,
144-
right,
145-
}),
141+
export const chainRule = Effect.fn("diff.chainRule")(
142+
(f: PolynomialObj, g: PolynomialObj, x: IdentExp) =>
143+
Effect.all([diffPolynomial(f, x), diffPolynomial(g, x)]).pipe(
144+
Effect.flatMap(([left, right]) =>
145+
recursivelySubstitute(left, g, x).pipe(
146+
Effect.flatMap((left) =>
147+
Effect.succeed(
148+
InfixObj.make({
149+
left,
150+
operator: TokenType.ASTERISK,
151+
right,
152+
}),
153+
),
146154
),
147155
),
148156
),
149157
),
150-
);
158+
);

src/scratch/trace-scratch.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { defaultLayer } from "@/layers/default";
21
import { Evaluator } from "@/services/evaluator";
32
import { objInspect } from "@/services/object";
43
import { Parser } from "@/services/parser";
@@ -8,23 +7,12 @@ import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base";
87
import { Effect, Layer, ManagedRuntime } from "effect";
98

109
// Create a program with tasks and subtasks
11-
const exampleProgram = Effect.gen(function* () {
12-
const evaluator = yield* Evaluator;
13-
return yield* evaluator.run("diff(fn(x) { sin(3 * x + 2) })(0)");
14-
}).pipe(Effect.provide(defaultLayer));
1510

1611
const NodeSdkLive = NodeSdk.layer(() => ({
17-
resource: { serviceName: "example" },
12+
resource: { serviceName: "diff" },
1813
spanProcessor: new BatchSpanProcessor(new OTLPTraceExporter()),
1914
}));
2015

21-
Effect.runPromise(
22-
exampleProgram.pipe(
23-
Effect.provide(NodeSdkLive),
24-
Effect.catchAllCause(Effect.logError),
25-
),
26-
);
27-
2816
const program = (input: string) =>
2917
Effect.gen(function* () {
3018
const evaluator = yield* Evaluator;

src/services/diff/obj.ts

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,41 +35,49 @@ const baseBuiltInDiffFunc =
3535
Schema.decodeUnknown(BuiltInDiffFunc)(fn.fn).pipe(
3636
Effect.flatMap((diffFn) =>
3737
Match.value(diffFn).pipe(
38-
Match.when("sin", () =>
39-
Schema.decodeUnknown(PolynomialObj)(args[0]).pipe(
40-
Effect.flatMap((g) =>
41-
Match.value(g).pipe(
42-
Match.tag("IdentObj", () =>
43-
Effect.succeed(
44-
CallObj.make({
45-
fn: BuiltInObj.make({ fn: "cos" }),
46-
args,
47-
}),
48-
),
49-
),
50-
Match.tag("IntegerObj", () =>
51-
Effect.succeed(
52-
CallObj.make({
53-
fn: BuiltInObj.make({ fn: "cos" }),
54-
args,
55-
}),
38+
Match.when(
39+
"sin",
40+
Effect.fn("diff.sin")(() =>
41+
Schema.decodeUnknown(PolynomialObj)(args[0]).pipe(
42+
Effect.flatMap((g) =>
43+
Match.value(g).pipe(
44+
Match.tag("IdentObj", () =>
45+
Effect.succeed(
46+
CallObj.make({
47+
fn: BuiltInObj.make({ fn: "cos" }),
48+
args,
49+
}),
50+
),
5651
),
57-
),
58-
Match.orElse(() =>
59-
Effect.gen(function* () {
60-
yield* Effect.log(
61-
`Differentiating sin with arg type: ${g._tag}`,
62-
);
63-
const chain = yield* chainRule(
52+
Match.tag("IntegerObj", () =>
53+
Effect.succeed(
6454
CallObj.make({
65-
fn: BuiltInObj.make({ fn: "sin" }),
66-
args: [IdentObj.make({ identExp: x })],
55+
fn: BuiltInObj.make({ fn: "cos" }),
56+
args,
6757
}),
68-
g,
69-
x,
70-
);
71-
return chain;
72-
}),
58+
),
59+
),
60+
Match.orElse(() =>
61+
Effect.gen(function* () {
62+
yield* Effect.log(
63+
`Differentiating sin with arg type: ${g._tag}`,
64+
);
65+
const chain = yield* chainRule(
66+
CallObj.make({
67+
fn: BuiltInObj.make({ fn: "sin" }),
68+
args: [IdentObj.make({ identExp: x })],
69+
}),
70+
g,
71+
x,
72+
);
73+
74+
return chain;
75+
}).pipe(
76+
Effect.tap((x) =>
77+
Effect.annotateCurrentSpan("chain", x),
78+
),
79+
),
80+
),
7381
),
7482
),
7583
),

0 commit comments

Comments
 (0)