Skip to content

Commit 2b93130

Browse files
committed
Various fixes and enhancements
Fixed the preview function so that it converts assignments into equations, and passes a well-formed WL string to the evaluate function. Made the various MatchQ functions in evaluate work with strings instead of expressions, and tidied up the flow. Made the order of argument sequences more consistent.
1 parent dd9f0be commit 2b93130

2 files changed

Lines changed: 55 additions & 44 deletions

File tree

evaluate.m

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,10 @@
113113
Depatternize[pattern_] := MapAll[DepatternizePattern, pattern]
114114

115115
(*StandardizeEquation: a function that automatically converts all instances
116-
of the equals sign to the repeated equals sign, so that anything WL would
117-
parse as an assignment gets parsed instead as an equu
118-
ation*)
116+
of the equals sign in a string to the repeated equals sign, so that anything WL
117+
would parse as an assignment gets parsed instead as an equation*)
119118

120-
StandardizeEquation[str_]:=FixedPoint[StringReplace["==="->"=="],StringReplace[str,"="->"=="]]
119+
StandardizeEquation[str_String]:=FixedPoint[StringReplace["==="->"=="],StringReplace[str,"="->"=="]]
121120

122121
(*StructureMatchQ: a function that checks whether a user's response \
123122
has the same structure as a given answer template, given a set of \
@@ -156,17 +155,17 @@
156155

157156
Options[StructureMatchQ] = {Atomic -> False};
158157

159-
StructureMatchQ[response_,answerTemplate_,namedVariables_] :=
158+
StructureMatchQ[answerTemplate_String,response_String,namedVariables_List] :=
160159
Module[{response2,answerTemplate2},response2=MapAll[CanonicComplex,ReplaceAll[response,inertFunctionRules]];
161160
answerTemplate2=ReplaceAll[answerTemplate,inertFunctionRules];
162161
MatchQ[response2,Patternize[answerTemplate2,namedVariables]]]
163162

164-
equalQStructure[answer_, response_, params_] := Module[{namedVariables,correctQ},
163+
equalQStructure[answer_String, response_String, params_Association] := Module[{namedVariables,correctQ},
165164
Print["Evaluating Structure"];
166-
namedVariables = ToExpression[Lookup[params,"named_variables",{}],TraditionalForm];
165+
namedVariables = FullForm[ToExpression[Lookup[params,"named_variables",{}],TraditionalForm]];
167166
correctQ = StructureMatchQ[
168-
ToExpression[StandardizeEquation[ToString[response]],TraditionalForm],
169-
ToExpression[StandardizeEquation[ToString[answer]],TraditionalForm],
167+
ToString[ToExpression[StandardizeEquation[answer],TraditionalForm],InputForm],
168+
response,
170169
namedVariables];
171170

172171
<|
@@ -191,35 +190,41 @@
191190
arccoth -> ArcCoth, acoth -> ArcCoth,
192191
exp -> Exp, log -> Log, ln -> Log};
193192

194-
SemanticMatchQ[response_,answer_] := Simplify[(response-answer)/.activeFunctionRules] == 0
193+
SemanticMatchQ[answer_,response_] := Simplify[(response-answer)/.activeFunctionRules] == 0
194+
195+
SemanticMatchQ[answer_Equal, response_Equal] :=
196+
SemanticMatchQ[answer[[1]]-answer[[2]], response[[1]]-response[[2]]]||
197+
SemanticMatchQ[answer[[1]]-answer[[2]], response[[2]]-response[[1]]]
195198

196-
SemanticMatchQ[response_Equal, answer_Equal] :=
197-
SemanticMatchQ[response[[1]]-response[[2]], answer[[1]]-answer[[2]]]||
198-
SemanticMatchQ[response[[1]]-response[[2]], answer[[2]]-answer[[1]]]
199+
SemanticMatchQ[answer_String,response_String] :=
200+
SemanticMatchQ[
201+
ToExpression[answer],
202+
ToExpression[response]
203+
]
199204

200-
SemanticAndStructureMatchQ[response_,answer_,answerTemplate_,namedVariables_] :=
201-
TrueQ[SemanticMatchQ[response,answer]&&StructureMatchQ[response,answerTemplate,namedVariables]]
205+
SemanticAndStructureMatchQ[answer_String,response_String,answerTemplate_String,namedVariables_List] :=
206+
TrueQ[SemanticMatchQ[answer,response]&&StructureMatchQ[answerTemplate,response,namedVariables]]
202207

203-
equalQSemantic[answer_, response_, params_] := Module[{correctQ},
208+
equalQSemantic[answer_String, response_String, params_Association] := Module[{correctQ},
204209
Print["Evaluating Semantic"];
205210
correctQ = SemanticMatchQ[
206-
ToExpression[StandardizeEquation[ToString[response]],TraditionalForm],
207-
ToExpression[StandardizeEquation[ToString[answer]],TraditionalForm]];
211+
ToString[ToExpression[StandardizeEquation[answer],TraditionalForm],InputForm],
212+
response];
208213

209214
<|
210215
"error" -> Null,
211216
"is_correct" -> correctQ
212217
|>
213218
]
214219

215-
equalQSemanticAndStructure[answer_, response_, params_] := Module[{namedVariables,answerTemplate,correctQ},
220+
equalQSemanticAndStructure[answer_String, response_String, params_Association] := Module[{namedVariables,answerTemplate,correctQ},
216221
Print["Evaluating SemanticAndStructure"];
217222
namedVariables = ToExpression[Lookup[params,"named_variables",{}],TraditionalForm];
218-
answerTemplate = ToExpression[Lookup[params,"answer_template",{}],TraditionalForm];
223+
answerTemplate = Lookup[params,"answer_template",{}];
219224
correctQ = SemanticAndStructureMatchQ[
220-
ToExpression[StandardizeEquation[ToString[response]],TraditionalForm],
221-
ToExpression[StandardizeEquation[ToString[answer]],TraditionalForm],
222-
ToExpression[StandardizeEquation[ToString[answerTemplate]],TraditionalForm],
225+
ToString[ToExpression[StandardizeEquation[answer],TraditionalForm],InputForm],
226+
response,
227+
ToString[ToExpression[StandardizeEquation[answerTemplate],TraditionalForm],InputForm],
223228
namedVariables
224229
];
225230

@@ -242,24 +247,24 @@
242247

243248
(* IN PARTICULAR, THE COMPARISON OF THE LENGTHS OF THE SYMBOL LISTS IS VERY HAMFISTED *)
244249

245-
StrictStructureMatchQ[response_,answerTemplate_,namedVariables_] :=
246-
StructureMatchQ[response,answerTemplate,namedVariables]&&
250+
StrictStructureMatchQ[answerTemplate_String,response_String,namedVariables_List] :=
251+
StructureMatchQ[answerTemplate,response,namedVariables]&&
247252
TrueQ[
248-
(Length[Union[UnnamedSymbols[response,namedVariables]]]==
249-
Length[Union[UnnamedSymbols[answerTemplate,namedVariables]]])]
253+
(Length[Union[UnnamedSymbols[ToExpression[response],namedVariables]]]==
254+
Length[Union[UnnamedSymbols[ToExpression[StandardizeEquation[answerTemplate],TraditionalForm],namedVariables]]])]
250255

251256
(* SemanticAndStrictStructureMatchQ: a function that combines a strict structure comparison with a test of
252257
mathematical equivalence *)
253258

254-
SemanticAndStrictStructureMatchQ[response_,answer_,answerTemplate_,namedVariables_] :=
255-
TrueQ[SemanticMatchQ[response,answer]&&StrictStructureMatchQ[response,answerTemplate,namedVariables]]
259+
SemanticAndStrictStructureMatchQ[answer_String,response_String,answerTemplate_String,namedVariables_List] :=
260+
TrueQ[SemanticMatchQ[answer,response]&&StrictStructureMatchQ[answerTemplate,response,namedVariables]]
256261

257-
equalQStrictStructure[answer_, response_, params_] := Module[{namedVariables,correctQ},
262+
equalQStrictStructure[answer_String, response_String, params_Association] := Module[{namedVariables,correctQ},
258263
Print["Evaluating Structure"];
259264
namedVariables = ToExpression[Lookup[params,"named_variables",{}],TraditionalForm];
260265
correctQ = StrictStructureMatchQ[
261-
ToExpression[StandardizeEquation[ToString[response]],TraditionalForm],
262-
ToExpression[StandardizeEquation[ToString[answer]],TraditionalForm],
266+
ToString[ToExpression[StandardizeEquation[answer],TraditionalForm],InputForm],
267+
response,
263268
namedVariables];
264269

265270
<|
@@ -268,14 +273,14 @@
268273
|>
269274
]
270275

271-
equalQSemanticAndStrictStructure[answer_, response_, params_] := Module[{namedVariables,answerTemplate,correctQ},
276+
equalQSemanticAndStrictStructure[answer_String, response_String, params_Association] := Module[{namedVariables,answerTemplate,correctQ},
272277
Print["Evaluating SemanticAndStructure"];
273278
namedVariables = ToExpression[Lookup[params,"named_variables",{}],TraditionalForm];
274-
answerTemplate = ToExpression[Lookup[params,"answer_template",{}],TraditionalForm];
279+
answerTemplate = Lookup[params,"answer_template",{}];
275280
correctQ = SemanticAndStrictStructureMatchQ[
276-
ToExpression[ToString[response],TraditionalForm],
277-
ToExpression[ToString[answer],TraditionalForm],
278-
ToExpression[ToString[answerTemplate],TraditionalForm],
281+
ToString[ToExpression[StandardizeEquation[answer],TraditionalForm],InputForm],
282+
response,
283+
answerTemplate,
279284
namedVariables
280285
];
281286

@@ -290,15 +295,15 @@
290295
evalQ[type_, answer_, response_, params_] := Module[{},
291296
Which[
292297
type == "structure",
293-
equalQStructure[answer,response,params],
298+
equalQStructure[answer, response, params],
294299
type == "semantic",
295-
equalQSemantic[answer,response,params],
300+
equalQSemantic[answer, response, params],
296301
type == "semantic_and_structure",
297-
equalQSemanticAndStructure[answer,response,params],
302+
equalQSemanticAndStructure[answer, response, params],
298303
type == "strict_structure",
299-
equalQStrictStructure[answer,response,params],
304+
equalQStrictStructure[answer, response, params],
300305
type == "semantic_and_strict_structure",
301-
equalQSemanticAndStrictStructure[answer,response,params],
306+
equalQSemanticAndStrictStructure[answer, response, params],
302307
NumericQ[answer],
303308
equalQNumeric[answer, response, params],
304309
True,

preview.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
];
3636

3737
latexString = ToString[parsedResponse/.activeFunctionRules, TeXForm];
38-
wolframString = ToString[parsedResponse/.activeFunctionRules];
38+
wolframString = ToString[parsedResponse/.activeFunctionRules, InputForm];
3939

4040
<|
4141
"latex" -> latexString,
@@ -67,7 +67,7 @@
6767

6868
(* Try to parse the expression safely *)
6969
result = Quiet @ Check[
70-
ToExpression[str, TraditionalForm, Hold],
70+
ToExpression[StandardizeEquation[str], TraditionalForm, Hold],
7171
Return["Error: Failed to parse expression"]
7272
];
7373

@@ -89,6 +89,12 @@
8989
"Error: Unexpected parsing result"
9090
]
9191
]
92+
93+
(*StandardizeEquation: a function that automatically converts all instances
94+
of the equals sign in a string to the repeated equals sign, so that anything WL
95+
would parse as an assignment gets parsed instead as an equation*)
96+
97+
StandardizeEquation[str_String]:=FixedPoint[StringReplace["==="->"=="],StringReplace[str,"="->"=="]]
9298

9399

94100

0 commit comments

Comments
 (0)