@@ -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 =
146148const 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+ ) ;
0 commit comments