Skip to content

Commit e38b117

Browse files
committed
Partial progress towards derivations histories changes
1 parent 673dacb commit e38b117

File tree

1 file changed

+59
-39
lines changed

1 file changed

+59
-39
lines changed

src/herbie/DerivationComponent.tsx

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import * as HerbieContext from './HerbieContext';
33
import './DerivationComponent.css';
44
import { ReactNode, useEffect } from 'react';
55
import { DerivationNode, ProofStep } from './HerbieTypes';
6+
import ReactDOMServer from 'react-dom/server';
7+
import KaTeX from 'katex';
8+
import { expressionToTex } from './ExpressionTable';
9+
import * as fpcorejs from './lib/fpcore'
610

711
// Function to convert FPCore to TeX (simplified version)
812
function fpCoreToTeX(expr: string, options?: { loc: any; color: any; }) {
@@ -86,7 +90,7 @@ function renderProof(proof: ProofStep[], options = {}) {
8690

8791

8892
// Render history recursively
89-
function renderHistory(altn: DerivationNode, options = {}): ReactNode[] {
93+
async function renderHistory(altn: DerivationNode, options = {}, serverUrl: string): Promise<ReactNode[]> {
9094
const items = [];
9195

9296
if (altn.type === "start") {
@@ -98,17 +102,22 @@ function renderHistory(altn: DerivationNode, options = {}): ReactNode[] {
98102
<li key="start">
99103
<p>
100104
Initial program <span className="error" title={`${err2} on training set`}>{err}</span>
101-
</p>
102-
<div className="math" dangerouslySetInnerHTML={{
103-
__html: `\\[${fpCoreToTeX(altn.program)}\\]`
104-
}} />
105+
</p>
106+
<div className="math" dangerouslySetInnerHTML={{
107+
//__html: KaTeX.renderToString(`\\[${fpCoreToTeX(altn.program)}\\]`, { throwOnError: false })
108+
__html: KaTeX.renderToString(
109+
await expressionToTex(altn.program, fpcorejs.getVarnamesMathJS(altn.program).length, serverUrl), { throwOnError: false }
110+
)
111+
}}
112+
/>
113+
await expressionToTex(expression, fpcorejs.getVarnamesMathJS(expression).length, serverUrl)
105114
</li>
106115
);
107116
}
108117
else if (altn.type === "add-preprocessing") {
109118
// Add the previous items if they exist
110119
if (altn.prev) {
111-
items.push(...renderHistory(altn.prev, options));
120+
items.push(...await renderHistory(altn.prev, options, serverUrl));
112121
}
113122

114123
// Add the preprocessing step
@@ -117,26 +126,32 @@ function renderHistory(altn: DerivationNode, options = {}): ReactNode[] {
117126
else if (altn.type === "taylor") {
118127
// Add the previous items
119128
if (altn.prev) {
120-
items.push(...renderHistory(altn.prev, options));
129+
items.push(...await renderHistory(altn.prev, options, serverUrl));
121130
}
122131

123132
// Add Taylor expansion step
124133
items.push(
125134
<li key="taylor">
126135
<p>Taylor expanded in {altn.var} around {altn.pt}</p>
127-
<div className="math" dangerouslySetInnerHTML={{
128-
__html: `\\[\\leadsto ${fpCoreToTeX(altn.program, {
129-
loc: altn.loc,
130-
color: "blue"
131-
})}\\]`
132-
}} />
136+
<Latex>
137+
{
138+
ReactDOMServer.renderToString(
139+
<div className="math" dangerouslySetInnerHTML={{
140+
__html: `\\[\\leadsto ${fpCoreToTeX(altn.program, {
141+
loc: altn.loc,
142+
color: "blue"
143+
})}\\]`
144+
}} />
145+
)
146+
}
147+
</Latex>
133148
</li>
134149
);
135150
}
136151
else if (altn.type === "rr") {
137152
// Add the previous items
138153
if (altn.prev) {
139-
items.push(...renderHistory(altn.prev, options));
154+
items.push(...await renderHistory(altn.prev, options, serverUrl));
140155
}
141156

142157
// Add proof if it exists
@@ -168,18 +183,7 @@ function renderHistory(altn: DerivationNode, options = {}): ReactNode[] {
168183

169184
// Main component for rendering derivations
170185
const DerivationRenderer = (derivation: DerivationNode) => {
171-
// Load MathJax
172-
useEffect(() => {
173-
const script = document.createElement('script');
174-
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-AMS_HTML';
175-
script.async = true;
176-
document.body.appendChild(script);
177-
178-
return () => {
179-
document.body.removeChild(script);
180-
};
181-
}, []);
182-
186+
const [serverUrl,] = HerbieContext.useGlobal(HerbieContext.ServerContext)
183187

184188
// // Example JSON data
185189
// const exampleDerivation: DerivationNode = {
@@ -269,10 +273,14 @@ const DerivationRenderer = (derivation: DerivationNode) => {
269273
<div className="bg-white rounded-lg p-4 shadow">
270274
<div id="history">
271275
<ol>
272-
{renderHistory(derivation, {
273-
fpCoreToTeX,
274-
formatAccuracy
275-
})}
276+
{await renderHistory(
277+
derivation,
278+
{
279+
fpCoreToTeX,
280+
formatAccuracy
281+
},
282+
serverUrl
283+
)}
276284
</ol>
277285
</div>
278286
</div>
@@ -302,8 +310,6 @@ const DerivationRenderer = (derivation: DerivationNode) => {
302310
);
303311
};
304312

305-
306-
307313
const DerivationComponent = ({ expressionId }: { expressionId: number }) => {
308314
const [derivations, setDerivations] = HerbieContext.useGlobal(HerbieContext.DerivationsContext)
309315

@@ -312,6 +318,18 @@ const DerivationComponent = ({ expressionId }: { expressionId: number }) => {
312318
return <div>Could not find expression with id {expressionId}</div>
313319
}
314320

321+
useEffect(() => {
322+
async function() {
323+
selectedDerivation?.derivation ?
324+
await DerivationRenderer(selectedDerivation.derivation) :
325+
<Latex>
326+
{
327+
selectedDerivation.history
328+
}
329+
</Latex>
330+
}
331+
}, []);
332+
315333
let currentExpressionId: number | undefined = selectedDerivation.id
316334
let expressionAncestry = []
317335

@@ -333,13 +351,15 @@ const DerivationComponent = ({ expressionId }: { expressionId: number }) => {
333351
}
334352

335353
return <div>
336-
selectedDerivation.derivation ?
337-
DerivationRenderer(selectedDerivation.derivation) :
338-
<Latex>
339-
{
340-
selectedDerivation.history
341-
}
342-
</Latex>
354+
{
355+
selectedDerivation.derivation ?
356+
await DerivationRenderer(selectedDerivation.derivation) :
357+
<Latex>
358+
{
359+
selectedDerivation.history
360+
}
361+
</Latex>
362+
}
343363
</div>
344364
};
345365

0 commit comments

Comments
 (0)