1 theory Test_VSCode_Example
2 imports "Isac.Build_Isac" (*"Isac.Calculation"*)
3 (*keywords "Example" :: thy_decl -- the active definition is in Calculation.thy*)
7 Tests here in BridgePIDE are already based on Build_Isac.thy bypassing $ISABELLE_ISAC_TEST.
8 TODO: eliminate "Isac." above on eliminating session in $ISABELLE_ISAC_TEST.
11 subsection \<open>Development\<close>
13 Here we develop the semantics of \<open>Example\<close> as test
14 --- build Outer_Syntax Example n:
15 and save intermediate states as separate tests in test/../preliminary.sml
16 as soon as some stable state is achieved.
18 This way we make all \<open>Example\<close> to \<open>Example\<close> step by step
19 and finally rename all again to \<open>Example\<close>.
21 val ((in_thy, thy_id_pos), (in_pbl, probl_id_pos), (in_met, meth_id_pos)) =
22 (("__", Position.none), ( "__/__", Position.none), ("__/__", Position.none))
23 val in_ = (in_thy, in_pbl, in_met)
24 val spec_ as (spec_thy, spec_pbl, spec_met) = References.empty
25 val o_ as (o_thy, o_pbl, o_met) =
26 ("Diff_App", ["univariate_calculus", "Optimisation"], ["Optimisation", "by_univariate_calculus"])
28 \<close> ML \<open> (** preliminary handling of References **)
29 \<close> ML \<open> (* assumption: we read the Specfication as a whole *)
30 (*/-------------------- state of src/../preliminary.sml at that time -------------------------\*)
31 fun re_eval_all ((*dummyarg*)) = ("re_eval_all if new_thy"; ((*dummyarg*)))
32 fun re_eval_pbl ((*dummyarg*)) = ("re_eval_pbl"; ((*dummyarg*)))
33 fun re_eval_met ((*dummyarg*)) = ("re_eval_met"; ((*dummyarg*)))
35 re_eval_all: unit -> unit;
37 switch_pbl_met IS MISSING! Greater changes are to expect with respective introduction.
38 In the meanwhile we just \<open>re_eval\<close> the \<open>I_Model\<close> of \<open>Problem\<close>;
39 in case this is complete, we automatically complete the \<open>I_Model\<close> of \<open>MethodC\<close>
40 without displaying the respective \<open>P_Model\<close> and immediately start solve-phase by \<open>Apply_Method\<close>;
42 fun re_eval (input_thy, input_pbl, input_met) (spec_thy, spec_pbl, spec_met) =
43 if spec_thy <> input_thy
45 else if spec_pbl <> input_pbl
47 else if spec_met <> input_met
49 else ("nothing to re-evaluate"; ((*dummyarg*)))
51 re_eval: References.T -> References.T -> unit;
52 (*\-------------------- state of src/../preliminary.sml at that time -------------------------/*)
53 \<close> ML \<open> (*for building code copy here
54 calculation.sml --- build Outer_Syntax Example 3 ff*)
57 (*/--------------------- the active definition is in Calculation.thy -------------------------\* )
59 Outer_Syntax.command \<^command_keyword>\<open>Example_test\<close>
60 "prepare ISAC problem type and register it to Knowledge Store"
61 (Parse.name -- Parse.!!! ( \<^keyword>\<open>Specification\<close> -- \<^keyword>\<open>:\<close> --
62 \<^keyword>\<open>Model\<close> -- \<^keyword>\<open>:\<close> |-- Problem.parse_pos_model_input --
63 (\<^keyword>\<open>References\<close> -- \<^keyword>\<open>:\<close> |-- Parse.!!! References.parse_input_pos
64 (** )-- ( \<^keyword>\<open>Solution\<close> -- keyword>\<open>:\<close>)( ** )WHAT IS WRONG HEREWITH?( **)) ) >>
65 (fn (example_id, (model_input,
66 ((thy_id, thy_id_pos), ((probl_id, probl_id_pos), (meth_id, meth_id_pos)) ) )) =>
67 Toplevel.theory (fn thy =>
71 CTbasic.EmptyPtree => Preliminary.init_ctree thy example_id
74 val {origin = (o_model, o_ref, _), spec = spec_ref, probl, meth, ctxt, ...} =
75 CTbasic.get_obj I state [(*Pos todo*)] |> CTbasic.rep_specify_data
76 val thy_id' = References.for_parse thy_id (#1 spec_ref) (#1 o_ref)
77 val (model, where_) = Model_Pattern.parse_pos_empty ctxt model_input (*+check syntax*)
78 (*reminder for Template.show ----------------------------------------------------------------\*)
79 val ((_, _, _), (_, pbl_id as ["univariate_calculus", "Optimisation"], _)) =
80 References.for_eval_model (thy_id, probl_id, meth_id) spec_ref o_ref
81 val {model = model_patt, where_ = where_ctree, where_rls, ...} =
82 Problem.from_store ctxt pbl_id;
84 (*/----- ERROR: mk_env not reasonable for "Inc Constants [] [__=__, __=__]"-------------\
85 ------------------------------------------------vvvvvvvvvvvvvvvvvvvvv-----------------------* )
86 val (_, env_eval) = Pre_Conds.sep_variants_envs_OLD model_patt probl
87 (*------------------- must be completed first -----------------^^^^^*)
90 case I_Model.is_complete_OLD ctxt model_patt where_rls where_ctree probl of
91 NONE => writeln "I_Model.is_complete_OLD --> NONE"
93 writeln ("I_Model.is_complete_OLD --> SOME " ^ LibraryC.ints2str' variants)
94 ( *\----- ERROR: mk_env not reasonable for "Inc Constants [] [__=__, __=__]"-------------/*)
95 (*reminder for Template.show ----------------------------------------------------------------/*)
97 (*reminder for Pre_Conds.check ---------------------------------------------------------\* )
98 val (true, [(true, (Const ("Orderings.ord_class.less", _) $ Const ("Groups.zero_class.zero", _) $
99 Free ("r", _), _))]) = Pre_Conds.check ctxt where_rls where_ env_eval;
100 ( *reminder for Pre_Conds.check ---------------------------------------------------------/*)
102 val ((_, _, _), (_, pbl_id as ["univariate_calculus", "Optimisation"], _)) =
103 References.for_eval_model (thy_id, probl_id, meth_id) spec_ref o_ref
104 val _ = re_eval References.empty References.empty
107 Preliminary.update_state thy
108 (example_id, (model_input, (thy_id, probl_id, meth_id))) (the_data thy)
110 in set_data state thy end)));
111 ( *\--------------------- the active definition is in Calculation.thy -------------------------/*)
113 \<close> ML \<open> (*Example below*)
116 subsection \<open>Mirror from src/../VSCode_Example.thy for testing purposes\<close>
118 subsubsection \<open>Complete Specification at once\<close>
121 Just a test, that this works also in $ISABELLE_ISAC
123 Example "Diff_App-No.123a" (*complete Specification*)
126 Given: \<open>Constants [r = 7]\<close>
127 Where: \<open>0 < r\<close>
128 Find: \<open>Maximum A\<close> \<open>AdditionalValues [u, v]\<close>
129 Relate: \<open>Extremum (A = 2 * u * v - u \<up> 2)\<close>
130 \<open>SideConditions [(u / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2]\<close>
132 Theory_Ref: "Diff_App"
133 (*\<Otimes>*) Problem_Ref: "univariate_calculus/Optimisation"
134 (*\<Odot>*) Method_Ref: "Optimisation/by_univariate_calculus"
138 Check complete \<open>Model\<close>, infer empty \<open>References\<close> from data hidden in "Diff_App-No.123a"
141 Example: no syntax check of terms, thus OK.
142 Example: proper syntax check of terms with Model_Pattern.parse_pos,
145 ( **)Example(**) "Diff_App-No.123a" (*complete Model, empty References*)
148 Given: \<open>Constants [r = 7]\<close>
149 Where: \<open>0 < r\<close>
150 Find: \<open>Maximum A\<close> \<open>AdditionalValues [u, v]\<close>
151 Relate: \<open>Extremum (A = 2 * u * v - u \<up> 2)\<close>
152 \<open>SideConditions [(u / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2]\<close>
155 (*\<Odot>*) Problem_Ref: "__/__"
156 (*\<Otimes>*) Method_Ref: "__/__"
159 subsubsection \<open>Empty Specification ready for input by students\<close>
161 In order to help the student (similar to a personal instructur helping a beginner)
162 the \<open>Specification\<close> is given as a template,
163 where so-called \<open>descriptor\<close>s give hints about the input requested
164 (in native language!) and delimiters are (partially) given.
166 For the same reason \<open>Where\<close> is already filled; most beginners are
167 not aware of "where_-conditions", i.e. of preconditions.
171 Example "Diff_App-No.123a"
174 Given: \<open>Constants [__=__, __=__]\<close>
175 Where: \<open>0 < r\<close>
176 Find: \<open>Maximum __\<close> \<open>AdditionalValues [__, __]\<close>
177 Relate: \<open>Extremum (__=__)\<close>
178 \<open>SideConditions [__=__, __=__]\<close>
181 (*\<Odot>*) Problem_Ref: "__/__"
182 (*\<Otimes>*) Method_Ref: "__/__"
185 subsubsection \<open>Start Example with a CAS_Cmd\<close>
187 The student wants to use Isabelle/Isac like an Algebra System and issues a CAS_Cmd.
188 The CAS_Cmd triggers implicit Specification (which usually is not requested
191 This user-requirement is not reflected so far. Three possible solutions are sketched below.
193 (*----------------------------------------* )
195 Specification (\<open>Simplify (2*a + 3*a)\<close>):
197 \<open>2*a + 3*a\<close>
199 ( *-----------------------------------------*)
201 (*----------------------------------------* )
202 Example \<open>Simplify (2*a + 3*a)\<close>
205 \<open>2*a + 3*a\<close>
207 ( *----------------------------------------*)
209 (*----------------------------------------* )
210 \<open>Simplify (2*a + 3*a)\<close>
213 \<open>2*a + 3*a\<close>
215 ( *----------------------------------------*)
217 subsubsection \<open>Specification step by step\<close>
220 In an electronic textbook there shall be references to examples given, for instance,
221 by \<open>Example "Diff_App-No.123a"\<close>. Such a reference shall be easily "activated"
222 to start a Specification + Solution at the spot ?and?or? to open a separate window.
224 Activating the reference \<open>Example "Diff_App-No.123a"\<close> shall result in a threefold alternative,
225 where (2)=(3) holds for the parsers:
226 (1) the Example presents a "normal problem" with a Specification (preferred by engineers),
227 see above subsubsection \<open>Empty Specification\<close>,
228 (2) the Example presents a "normal problem" without a Specification (preferred in high-school):
229 |Example "Diff_App-No.123a"
232 | ..first formula as given by the specifid Method..
233 (3) the Example presents a "CAS_Cmd":
234 |Example \<open>Simplify (2*a + 3*a)\<close>
237 | solve (x + 1 = 2, x)
238 \<open>Specification\<close> is always visible in order to remind of the existence.
239 \<open>Outer_Syntax.command \<^command_keyword>\<open>Example\<close>\<close> shall be able to parse all three cases
240 (while still skipping considerations about \<open>Specification\<close> and sub-problems).
242 Below is the Example with case (1):
243 * The lines \<open>text --- remove this line and make content Isar input---\\<close>
244 and ---------- remove this line and make content Isar input---/
245 will be deleted after correct implementation
246 * The \<open>ML \<open>blocks\<close>\<close> in between show, that ISAC is able of "next step guidance";
247 these can be deleted as well.
248 * This Example also shows the toggle between \<open>\<Otimes>\<close> and \<open>\<Odot>\<close>.
252 # 1: ISAC starts the Example by presenting an "empty" Specification.
254 "Presenting" should mean: by "activation" of Example the system presents
255 the empty Specification as a template, where the items give hints on the input format.
257 The field \<open>Where\<close> is already filled, but editable and shows missing input by \<open>False\<close>.
260 \<close> ML \<open> (* # 1: At the first call of \<open>Example\<close> ISAC initialises a calculation *)
261 val p = ([], Pos.Pbl);
262 val pt = Preliminary.update_state @{theory} (*here we have Ctree.EmptyPtree and thus call init_ctree*)
264 ( [("#Given", [("Constants [r = 7]", Position.none)]),
265 ("#Where", [("0 < r", Position.none)]),
266 ("#Find", [("Maximum A", Position.none), ("AdditionalValues [u, v]", Position.none)]),
267 ("#Relate", [("Extremum A = 2 * u * v - u \<up> 2", Position.none),
268 ("SideConditions [((u::real) / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2]", Position.none)])
270 ("Diff_App", "univariate_calculus/Optimisation", "Optimisation/by_univariate_calculus")
272 ) CTbasic.EmptyPtree;
275 Example "Diff_App-No.123a" (*all empty step 0*)
278 Given: \<open>Constants [__=__, __=__]\<close>
279 Where: \<open>0 < r\<close>
280 Find: \<open>Maximum __\<close> \<open>AdditionalValues [__, __]\<close>
281 Relate: \<open>Extremum (__=__)\<close>
282 \<open>SideConditions [__=__, __=__]\<close>
285 (*\<Odot>*) Problem_Ref: "__/__"
286 (*\<Otimes>*) Method_Ref: "__/__"
290 # 2: Somewhere in the Model, the user inputs a next item, which is marked "correct" by ISAC.
292 By this input Where: \<open>0 < r\<close> is not rendered \<open>False\<close> anymore.
294 ML \<open> (* # 2: Somewhere in the Model, the user inputs a next item *)
295 (*/---Step.specify_do_next still works with Ctree, not Ctree-------------------* )
296 val ("ok", ([(Tactic.Add_Given "Constants [r = 7]", _, _)], _, ptp))
297 = Step.specify_do_next ptp;
298 ( *\---Step.specify_do_next still works with Ctree, not Ctree-------------------*)
300 Example "Diff_App-No.123a" (*step 1: <Constants [r = 7]>*)
303 Given: \<open>Constants [r = 7]\<close>
304 Where: \<open>0 < r\<close>
305 Find: \<open>Maximum __\<close> \<open>AdditionalValues [__, __]\<close>
306 Relate: \<open>Extremum (__=__)\<close> \<open>SideConditions [__=__, __=__]\<close>
309 (*\<Odot>*) Problem_Ref: "__/__"
310 (*\<Otimes>*) Method_Ref: "__/__"
314 # 3: The user inputs a next item somewhere in the \<open>Specification\<close>;
315 Since this item requests for a list, \<open>AdditionalValues\<close> is marked "incomplete":
317 ML \<open> (* 3: ISAC suggests a next item within a term list *)
318 (*/---Step.specify_do_next still works with Ctree, not Ctree-------------------* )
319 val ("ok", ([(Tactic.Add_Find "Maximum A", _, _)], _, ptp))
320 = Step.specify_do_next ptp;
321 ( *\---Step.specify_do_next still works with Ctree, not Ctree-------------------*)
323 Example "Diff_App-No.123a" (*step 2: AdditionalValues [u]*)
326 Given: \<open>Constants [r = 7]\<close>
327 Where: \<open>0 < r\<close>
328 Find: \<open>Maximum __\<close> \<open>AdditionalValues [u]\<close>
329 Relate: \<open>Extremum (__=__)\<close> \<open>SideConditions [__=__, __=__]\<close>
332 (*\<Odot>*) Problem_Ref: "__/__"
333 (*\<Otimes>*) Method_Ref: "__/__"
337 # 4: The user inputs a next item, and the item \<open>AdditionalValues\<close> is marked "correct":
339 ML \<open> (* # 4: ISAC suggests a next step on request, even a next element within a list *)
340 (*/---Step.specify_do_next still works with Ctree, not Ctree-------------------* )
341 val ("ok", ([(Tactic.Add_Find "AdditionalValues [u]", _, _)], _, ptp))
342 = Step.specify_do_next ptp;
343 ( *\---Step.specify_do_next still works with Ctree, not Ctree-------------------*)
346 Example "Diff_App-No.123a" (*step 3: A.Values [u, v]*)
349 Given: \<open>Constants [r = 7]\<close>
350 Where: \<open>0 < r\<close>
351 Find: \<open>Maximum __\<close> \<open>AdditionalValues [u, v]\<close>
352 Relate: \<open>Extremum (__=__)\<close> \<open>SideConditions [__=__, __=__]\<close>
355 (*\<Odot>*) Problem_Ref: "__/__"
356 (*\<Otimes>*) Method_Ref: "__/__"
360 # 5: Input to \<open>Problem_Ref\<close> is according to the (hidden) formalisation:
362 ML \<open> (* # 5: Input to \<open>Problem_Ref\<close> might change the \<open>Model\<close> *)
364 Example "Diff_App-No.123a" (*step 4: (wrong) Problem_Ref input*)
367 Given: \<open>Constants [r = 7]\<close>
368 Where: \<open>0 < r\<close>
369 Find: \<open>Maximum __\<close> \<open>AdditionalValues [u, v]\<close>
370 Relate: \<open>Extremum (__=__)\<close> \<open>SideConditions [__=__, __=__]\<close>
373 (*\<Odot>*) Problem_Ref: "univariate_calculus/Optimisation" (*"Biegelinien/einfache" not in teststore*)
374 (*\<Otimes>*) Method_Ref: "__/__"
378 # 6: Input to \<open>Method_Ref\<close> changes the \<open>Model\<close> to the respective \<open>Method\<close>;
379 There is also a feature to change the \<open>Model\<close> according to
380 direct manipulation of \<open>\<Otimes>\<close> and \<open>\<Odot>\<close>.
382 ML \<open> (* # 6: Input to \<open>Method_Ref\<close> changes the \<open>Model\<close> to the respective \<open>Method\<close> *)
384 Example "Diff_App-No.123a" (*step 5: Method_Ref updated*)
386 Model: (* FIXME: lookup model of method *)
387 Given: \<open>Constants [r = 7]\<close>
388 Where: \<open>0 < r\<close>
389 Find: \<open>Maximum __\<close> \<open>AdditionalValues [u, v]\<close>
390 Relate: \<open>Extremum (__=__)\<close> \<open>SideConditions [__]\<close>
393 (*\<Odot>*) Problem_Ref: "univariate_calculus/Optimisation"
394 (*\<Otimes>*) Method_Ref: "Optimisation/by_univariate_calculus"
400 ML \<open> (* # 7: user inputs value of Method_Ref, which shows the respective Model *)
403 subsubsection \<open>Specification with Model for the Method_Ref\<close>
406 Observe the (still outcommented) toggles \<Odot>\<Otimes> changed according to the Reference
408 Example "Diff_App-No.123a" (*Specification of the Method_Ref*)
411 Given: \<open>Constants [r = 7]\<close> \<open>Maximum A\<close> \<open>AdditionalValues [u, v]\<close>
412 \<open>Extremum (A = 2 * u * v - u \<up> 2)\<close>
413 \<open>SideConditions [(u / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2]\<close>
414 \<open>FunctionVariable u\<close> \<open>Domain {0 <..< r}\<close> \<open>ErrorBound (\<epsilon> = 0)\<close>
415 Where: \<open>0 < r\<close>
416 Find: \<open>Results (A, u, v)\<close>
419 Theory_Ref: "Diff_App"
420 (*\<Odot>*) Problem_Ref: "univariate_calculus/Optimisation"
421 (*\<Otimes>*) Method_Ref: "Optimisation/by_univariate_calculus"
424 text \<open> --- remove this line and make content Isar input---\
425 ---------- remove this line and make content Isar input---/
431 ML \<open> (* # 8: ISAC suggests a next step on request *)
433 text \<open> --- remove this line and make content Isar input---\
434 ---------- remove this line and make content Isar input---/
440 ML \<open> (* # 9: ISAC suggests a next step on request *)
442 text \<open> --- remove this line and make content Isar input---\
443 ---------- remove this line and make content Isar input---/