test/Tools/isac/BridgeJEdit/Test_VSCode_Example.thy
author wneuper <Walther.Neuper@jku.at>
Mon, 27 Feb 2023 09:45:29 +0100
changeset 60698 f7795240462a
parent 60697 dd386fd3ec5e
child 60699 dcaf63259e1d
permissions -rw-r--r--
PIDE turn 9: handle empty input, error in Test_VSCode_Example, isabelle@
     1 theory Test_VSCode_Example
     2   imports "Isac.Build_Isac" (*"Isac.Calculation"*)
     3   keywords "Example_TEST" :: thy_decl
     4 begin
     5 
     6 text \<open>
     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.
     9 \<close>
    10 
    11 subsection \<open>Development\<close>
    12 text \<open>
    13   Here we develop the semantics of \<open>Example_TEST\<close> as test
    14   --- build Outer_Syntax Example_TEST n:
    15   and save intermediate states as separate tests in test/../preliminary.sml
    16   as soon as some stable state is achieved.
    17 
    18   This way we make all \<open>Example\<close> to \<open>Example_TEST\<close> step by step 
    19   and finally rename all again to \<open>Example\<close>.
    20 \<close> ML \<open>
    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"])
    27 \<close> ML \<open>
    28 \<close> ML \<open>
    29 \<close> ML \<open>
    30 \<close> ML \<open>
    31 \<close> ML \<open>
    32 \<close> ML \<open>
    33 \<close> ML \<open>
    34 \<close> ML \<open>
    35 \<close> ML \<open> (** preliminary handling of References **)
    36 \<close> ML \<open> (* assumption:  we read the Specfication as a whole *)
    37 (*/-------------------- state of src/../preliminary.sml at that time  -------------------------\*)
    38 fun re_eval_all ((*dummyarg*)) = ("re_eval_all if new_thy"; ((*dummyarg*)))
    39 fun re_eval_pbl ((*dummyarg*)) = ("re_eval_pbl"; ((*dummyarg*)))
    40 fun re_eval_met ((*dummyarg*)) = ("re_eval_met"; ((*dummyarg*)))
    41 ;
    42 re_eval_all: unit -> unit;
    43 (*
    44   switch_pbl_met IS MISSING! Greater changes are to expect with respective introduction.
    45   In the meanwhile we just \<open>re_eval\<close> the \<open>I_Model\<close> of \<open>Problem\<close>;
    46   in case this is complete, we automatically complete the \<open>I_Model\<close> of \<open>MethodC\<close>
    47   without displaying the respective \<open>P_Model\<close> and immediately start solve-phase by \<open>Apply_Method\<close>;
    48 *)
    49 fun re_eval (input_thy, input_pbl, input_met) (spec_thy, spec_pbl, spec_met) =
    50   if spec_thy <> input_thy
    51     then re_eval_all ()
    52     else if spec_pbl <> input_pbl
    53       then re_eval_pbl ()
    54       else if spec_met <> input_met
    55         then re_eval_met ()
    56         else ("nothing to re-evaluate"; ((*dummyarg*)))
    57 ;
    58 re_eval: References.T -> References.T -> unit
    59 \<close> ML \<open>
    60 \<close> ML \<open>
    61 \<close> ML \<open>
    62 \<close> ML \<open>
    63 \<close> ML \<open>
    64 (*\-------------------- state of src/../preliminary.sml at that time  -------------------------/*)
    65 \<close> ML \<open> (*\<longrightarrow> test/../calculation.sml*)
    66 "----------- build Outer_Syntax Example_TEST ---------------------------------------------------";
    67 "----------- build Outer_Syntax Example_TEST ---------------------------------------------------";
    68 "----------- build Outer_Syntax Example_TEST ---------------------------------------------------";
    69 "----------- build Outer_Syntax Example_TEST: stepwise input, References empty -----------------";
    70 "----------- build Outer_Syntax Example_TEST: stepwise input, References empty -----------------";
    71 "----------- build Outer_Syntax Example_TEST: stepwise input, References empty -----------------";
    72 (*/---------------------------------------- mimic input from PIDE -----------------------------\*)
    73 (* Example_TEST "Diff_App-No.123a" (*complete Model, empty References*) *)
    74 val thy = @{theory}
    75 val model_input = (*type Position.T is hidden, thus redefinition*)
    76  [("#Given", [("Constants [r = 7]", Position.none)]),
    77   ("#Where", [("0 < r", Position.none)]),
    78   ("#Find",
    79    [("Maximum A", Position.none), ("AdditionalValues [u, v]", Position.none)]),
    80   ("#Relate",
    81    [("Extremum (A = 2 * u * v - u \<up> 2)", Position.none),
    82     ("SideConditions [(u / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2]", Position.none)])]
    83 : (Model_Pattern.m_field * (string * Position.T) list) list
    84 (**)
    85 val ((thy_id, thy_id_pos), (probl_id, probl_id_pos), (meth_id, meth_id_pos)) =
    86   (("__", Position.none), ( "__/__", Position.none), ("__/__", Position.none))
    87 (**)
    88 val example_id = "Diff_App-No.123a";
    89 (*\---------------------------------------- mimic input from PIDE -----------------------------/*)
    90 \<close> ML \<open>
    91             val state =
    92               case the_data thy of
    93                   Ctree.EmptyPtree => Preliminary.init_ctree_TEST thy example_id
    94                 | _ => the_data thy
    95 \<close> ML \<open>
    96                   (*let*)
    97                     val {origin = (_, o_ref, _), spec = spec_ref, ctxt, ...} =
    98                       Ctree.get_obj I state [(*Pos will become variable*)] |> Ctree.rep_specify_data
    99                     val thy_id' = References.for_parse thy_id (#1 spec_ref) (#1 o_ref)
   100                     val (model, where_) = Model_Pattern.parse_pos_TEST ctxt model_input (*+check syntax*)
   101 \<close> ML \<open>
   102 (model, where_) (*GOON*)
   103 \<close> ML \<open>
   104 (**)
   105                     val _ = re_eval References.empty References.empty;
   106 (**)
   107 \<close> ML \<open>
   108             (*in*) Preliminary.update_state_TEST thy
   109                       (example_id, (model_input, (thy_id, probl_id, meth_id))) (the_data thy)
   110             (*end*)
   111 \<close> ML \<open>
   112 (*POSTPONED Specify.check_input \<longrightarrow> specify.sml*)
   113 fun check_input _ = 123
   114 \<close> ML \<open>
   115 \<close> ML \<open>
   116 \<close> ML \<open>
   117 \<close> ML \<open>
   118 \<close> ML \<open>
   119 \<close> ML \<open>
   120 \<close> ML \<open>
   121 \<close> ML \<open>
   122 \<close> ML \<open>
   123 \<close> ML \<open>
   124 val _ =
   125   Outer_Syntax.command \<^command_keyword>\<open>Example_TEST\<close>
   126     "prepare ISAC problem type and register it to Knowledge Store"
   127     (Parse.name -- Parse.!!! ( \<^keyword>\<open>Specification\<close> -- \<^keyword>\<open>:\<close> --
   128          \<^keyword>\<open>Model\<close> -- \<^keyword>\<open>:\<close>  |-- Problem.parse_pos_model_input --
   129            (\<^keyword>\<open>References\<close> -- \<^keyword>\<open>:\<close> |-- Parse.!!! References.parse_input_pos
   130              (** )-- ( \<^keyword>\<open>Solution\<close> -- keyword>\<open>:\<close>)( ** )WHAT IS WRONG HEREWITH?( **)) ) >>
   131       (fn (example_id, (model_input,
   132           ((thy_id, thy_id_pos), ((probl_id, probl_id_pos), (meth_id, meth_id_pos)) ) )) =>
   133         Toplevel.theory (fn thy =>
   134           let
   135             val state =
   136               case the_data thy of
   137                   Ctree.EmptyPtree => Preliminary.init_ctree_TEST thy example_id
   138                 | _ =>
   139                   let
   140                     val {origin = (_, o_ref, _), spec = spec_ref, ctxt, ...} =
   141                       Ctree.get_obj I state [] |> Ctree.rep_specify_data
   142                     val thy_id' = References.for_parse thy_id (#1 spec_ref) (#1 o_ref)
   143                     val (model, where_) = Model_Pattern.parse_pos_TEST ctxt model_input
   144 (**)
   145                     val _ = re_eval References.empty References.empty
   146 (**)
   147                   in
   148                     Preliminary.update_state_TEST thy
   149                       (example_id, (model_input, (thy_id, probl_id, meth_id))) (the_data thy)
   150                   end
   151           in set_data state thy end)));
   152 \<close> ML \<open>
   153 \<close> ML \<open> (*Example_TEST below*)
   154 \<close>
   155 
   156 subsection \<open>Mirror from src/../VSCode_Example.thy for testing purposes\<close>
   157 
   158 subsubsection \<open>Complete Specification at once\<close>
   159 
   160 text \<open>
   161   Just a test, that this works also in $ISABELLE_ISAC_TEST
   162 \<close>
   163 Example "Diff_App-No.123a" (*complete Specification*)
   164   Specification:
   165     Model:
   166       Given: \<open>Constants [r = 7]\<close>
   167       Where: \<open>0 < r\<close>
   168       Find:  \<open>Maximum A\<close> \<open>AdditionalValues [u, v]\<close>
   169       Relate: \<open>Extremum A = 2 * u * v - u \<up> 2\<close> 
   170         \<open>SideConditions [(u / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2]\<close>
   171     References:
   172       Theory_Ref: "Diff_App"
   173   (*\<Otimes>*) Problem_Ref: "univariate_calculus/Optimisation"
   174   (*\<Odot>*) Method_Ref: "Optimisation/by_univariate_calculus"
   175 (*Solution:*)
   176 
   177 text \<open>
   178   Check complete \<open>Model\<close>, infer empty \<open>References\<close> from data hidden in "Diff_App-No.123a"
   179 \<close>
   180 (*
   181 Example: no syntax check of terms, OK.
   182 Example_TEST: proper syntax check of terms with Model_Pattern.parse_pos,
   183   but with parse_pos_TEST ERROR Malformed YXML: unbalanced element "input"
   184 *)
   185 (**)Example
   186 (** )Example_TEST( **) "Diff_App-No.123a" (*complete Model, empty References*)
   187   Specification:
   188     Model:
   189       Given: \<open>Constants [r = 7]\<close>
   190       Where: \<open>0 < r\<close>
   191       Find:  \<open>Maximum A\<close> \<open>AdditionalValues [u, v]\<close>
   192       Relate: \<open>Extremum (A = 2 * u * v - u \<up> 2)\<close> 
   193         \<open>SideConditions [(u / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2]\<close>
   194     References: 
   195       Theory_Ref: "__"
   196   (*\<Odot>*) Problem_Ref: "__/__"
   197   (*\<Otimes>*) Method_Ref: "__/__"
   198 (*Solution:*)
   199 
   200 subsubsection \<open>Empty Specification ready for input by students\<close>
   201 text \<open>
   202   In order to help the student (similar to a personal instructur helping a beginner)
   203   the \<open>Specification\<close> is given as a template,
   204   where so-called \<open>descriptor\<close>s give hints about the input requested
   205   (in native language!) and delimiters are (partially) given.
   206   
   207   For the same reason \<open>Where\<close> is already filled; most beginners are 
   208   not aware of "where_-conditions", i.e. of preconditions.
   209 \<close>
   210 
   211 Example "Diff_App-No.123a"
   212   Specification:
   213     Model:
   214       Given: \<open>Constants [__=__, __=__]\<close>
   215       Where: \<open>0 < r\<close>
   216       Find:  \<open>Maximum __\<close> \<open>AdditionalValues [__, __]\<close>
   217       Relate: \<open>Extremum __\<close> 
   218         \<open>SideConditions [__=__, __=__]\<close>
   219     References: 
   220       Theory_Ref: "__"
   221   (*\<Odot>*) Problem_Ref: "__/__"
   222   (*\<Otimes>*) Method_Ref: "__/__"
   223 (*Solution:*)
   224 
   225 subsubsection \<open>Start Example with a CAS_Cmd\<close>
   226 text \<open>
   227   The student wants to use Isabelle/Isac like an Algebra System and issues a CAS_Cmd.
   228   The CAS_Cmd triggers implicit Specification (which usually is not requested 
   229   in case of CAS_Cmd).
   230 
   231   This user-requirement is not reflected so far. Three possible solutions are sketched below.
   232 \<close>
   233 (*----------------------------------------*  )
   234 Example ""
   235   Specification (\<open>Simplify (2*a + 3*a)\<close>):
   236   Solution:
   237     \<open>2*a + 3*a\<close>
   238     :
   239 (  *-----------------------------------------*)
   240 
   241 (*----------------------------------------*  )
   242 Example \<open>Simplify (2*a + 3*a)\<close>
   243   Specification:
   244   Solution:
   245     \<open>2*a + 3*a\<close>
   246     :
   247 (  *----------------------------------------*)
   248 
   249 (*----------------------------------------*  )
   250 \<open>Simplify (2*a + 3*a)\<close>
   251   Specification:
   252   Solution:
   253     \<open>2*a + 3*a\<close>
   254     :
   255 (  *----------------------------------------*)
   256 
   257 subsubsection \<open>Specification step by step\<close>
   258 
   259 text \<open>
   260   In an electronic textbook there shall be references to examples given, for instance,
   261   by \<open>Example "Diff_App-No.123a"\<close>. Such a reference shall be easily "activated" 
   262   to start a Specification + Solution at the spot ?and?or? to open a separate window.
   263 
   264   Activating the reference \<open>Example "Diff_App-No.123a"\<close> shall result in a threefold alternative,
   265   where (2)=(3) holds for the parsers:
   266   (1) the Example presents a "normal problem" with a Specification (preferred by engineers),
   267     see above subsubsection \<open>Empty Specification\<close>,
   268   (2) the Example presents a "normal problem" without a Specification (preferred in high-school):
   269     |Example "Diff_App-No.123a"
   270     |  Specification:
   271     |  Solution:
   272     |    ..first formula as given by the specifid Method..
   273   (3) the Example presents a "CAS_Cmd":
   274     |Example \<open>Simplify (2*a + 3*a)\<close>
   275     |  Specification:
   276     |  Solution:
   277     |    solve (x + 1 = 2, x)
   278   \<open>Specification\<close> is always visible in order to remind of the existence.
   279   \<open>Outer_Syntax.command \<^command_keyword>\<open>Example\<close>\<close> shall be able to parse all three cases
   280   (while still skipping considerations about \<open>Specification\<close> and sub-problems).
   281 
   282   Below is the Example with case (1):
   283   * The lines \<open>text --- remove this line and make content Isar input---\\<close>
   284     and      ---------- remove this line and make content Isar input---/
   285     will be deleted after correct implementation
   286   * The \<open>ML \<open>blocks\<close>\<close> in between show, that ISAC is able of "next step guidance"; 
   287     these can be deleted as well.
   288   * This Example also shows the toggle between \<open>\<Otimes>\<close> and \<open>\<Odot>\<close>.
   289 \<close> 
   290 
   291 text \<open>
   292   # 1: ISAC starts the Example by presenting an "empty" Specification.
   293 
   294   "Presenting" should mean: by "activation" of Example the system presents
   295   the empty Specification as a template, where the items give hints on the input format.
   296 
   297   The field \<open>Where\<close> is already filled, but editable and shows missing input by \<open>False\<close>.
   298 \<close>
   299 ML \<open>
   300 \<close> ML \<open> (* # 1: At the first call of \<open>Example\<close> ISAC initialises a calculation *)
   301 val p = ([], Pos.Pbl);
   302 val pt = Preliminary.update_state @{theory} (*here we have Ctree.EmptyPtree and thus call init_ctree*)
   303   ("Diff_App-No.123a", 
   304   ( [("#Given", [("Constants [r = 7]", Position.none)]),
   305      ("#Where", [("0 < r", Position.none)]), 
   306      ("#Find", [("Maximum A", Position.none), ("AdditionalValues [u, v]", Position.none)]), 
   307      ("#Relate", [("Extremum A = 2 * u * v - u \<up> 2", Position.none), 
   308         ("SideConditions [((u::real) / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2]", Position.none)])
   309     ],
   310     ("Diff_App", "univariate_calculus/Optimisation", "Optimisation/by_univariate_calculus")
   311   )
   312 ) Ctree.EmptyPtree;
   313 val ptp = (pt, p);
   314 \<close>
   315 Example "Diff_App-No.123a"
   316   Specification:
   317     Model:
   318       Given: \<open>Constants [__=__, __=__]\<close>
   319       Where: \<open>0 < r\<close>
   320       Find:  \<open>Maximum __\<close> \<open>AdditionalValues [__, __]\<close>
   321       Relate: \<open>Extremum __\<close> 
   322         \<open>SideConditions [__=__, __=__]\<close>
   323     References: 
   324       Theory_Ref: "__"
   325   (*\<Odot>*) Problem_Ref: "__/__"
   326   (*\<Otimes>*) Method_Ref: "__/__"
   327 (*Solution:*)
   328 
   329 text \<open>
   330   # 2: Somewhere in the Model, the user inputs a next item, which is marked "correct" by ISAC.
   331 
   332   By this input Where: \<open>0 < r\<close> is not rendered \<open>False\<close> anymore.
   333 \<close> 
   334 ML \<open> (* # 2: Somewhere in the Model, the user inputs a next item *)
   335 val ("ok", ([(Tactic.Add_Given "Constants [r = 7]", _, _)], _, ptp))
   336   = Step.specify_do_next ptp;
   337 \<close>
   338 Example "Diff_App-No.123a"
   339   Specification:
   340     Model:
   341       Given: \<open>Constants [r = 7]\<close>
   342       Where: \<open>0 < r\<close>
   343       Find:  \<open>Maximum _\<close> \<open>AdditionalValues [__, __]\<close>
   344       Relate: \<open>Extremum _\<close> \<open>SideConditions [__=__, __=__]\<close>
   345     References:
   346       Theory_Ref: "__"
   347   (*\<Odot>*) Problem_Ref: "__/__"
   348   (*\<Otimes>*) Method_Ref: "__/__"
   349 (*Solution:*)
   350 
   351 text \<open>
   352   # 3: The user inputs a next item somewhere in the \<open>Specification\<close>;
   353        Since this item requests for a list, \<open>AdditionalValues\<close> is marked "incomplete":
   354 \<close> 
   355 ML \<open> (* 3: ISAC suggests a next item within a term list *)
   356 val ("ok", ([(Tactic.Add_Find "Maximum A", _, _)], _, ptp))
   357   = Step.specify_do_next ptp;
   358 \<close>
   359 Example "Diff_App-No.123a"
   360   Specification:
   361     Model:
   362       Given: \<open>Constants r = 7\<close>
   363       Where: \<open>0 < r\<close>
   364       Find:  \<open>Maximum _\<close> \<open>AdditionalValues [u]\<close>
   365       Relate: \<open>Extremum _\<close> \<open>SideConditions [__=__, __=__]\<close>
   366     References:
   367       Theory_Ref: "__"
   368   (*\<Odot>*) Problem_Ref: "__/__"
   369   (*\<Otimes>*) Method_Ref: "__/__"
   370 (*Solution:*)
   371 
   372 text \<open>
   373   # 4: The user inputs a next item, and the item \<open>AdditionalValues\<close> is marked "correct":
   374 \<close> 
   375 ML \<open> (* # 4: ISAC suggests a next step on request, even a next element within a list *)
   376 val ("ok", ([(Tactic.Add_Find "AdditionalValues [u]", _, _)], _, ptp))
   377   = Step.specify_do_next ptp;
   378 \<close>
   379 
   380 Example "Diff_App-No.123a"
   381   Specification:
   382     Model:
   383       Given: \<open>Constants r = 7\<close>
   384       Where: \<open>0 < r\<close>
   385       Find:  \<open>Maximum _\<close> \<open>AdditionalValues [u, v]\<close>
   386       Relate: \<open>Extremum _\<close> \<open>SideConditions [__=__, __=__]\<close>
   387     References:
   388       Theory_Ref: "__"
   389   (*\<Odot>*) Problem_Ref: "__/__"
   390   (*\<Otimes>*) Method_Ref: "__/__"
   391 (*Solution:*)
   392 
   393 text \<open>
   394   # 5: Input to \<open>Problem_Ref\<close> is according to the (hidden) formalisation:
   395 \<close> 
   396 ML \<open> (* # 5: Input to \<open>Problem_Ref\<close> might change the \<open>Model\<close> *)
   397 \<close>
   398 Example "Diff_App-No.123a"
   399   Specification:
   400     Model:
   401       Given: \<open>Constants r = 7\<close>
   402       Where: \<open>0 < r\<close>
   403       Find:  \<open>Maximum _\<close> \<open>AdditionalValues [u, v]\<close>
   404       Relate: \<open>Extremum _\<close> \<open>SideConditions [__=__, __=__]\<close>
   405     References:
   406       Theory_Ref: "__"
   407   (*\<Odot>*) Problem_Ref: "__/__"
   408   (*\<Otimes>*) Method_Ref: "__/__"
   409 (*Solution:*)
   410 
   411 text \<open>
   412   # 6: Input to \<open>Method_Ref\<close> changes the \<open>Model\<close> to the respective \<open>Method\<close>;
   413        There is also a feature to change the \<open>Model\<close> according to 
   414        direct manipulation of \<open>\<Otimes>\<close> and \<open>\<Odot>\<close>.
   415 \<close> 
   416 ML \<open> (* # 6: Input to \<open>Method_Ref\<close> changes the \<open>Model\<close> to the respective \<open>Method\<close> *)
   417 \<close>
   418 Example "Diff_App-No.123a"
   419   Specification:
   420     Model:      (* FIXME: lookup model of method *)
   421       Given: \<open>Constants r = 7\<close>
   422       Where: \<open>0 < r\<close>
   423       Find:  \<open>Maximum _\<close> \<open>AdditionalValues [u, v]\<close>
   424       Relate: \<open>Extremum _\<close> \<open>SideConditions [_]\<close>
   425     References:
   426       Theory_Ref: "__"
   427   (*\<Odot>*) Problem_Ref: "univariate_calculus/Optimisation"
   428   (*\<Otimes>*) Method_Ref: "Optimisation/by_univariate_calculus"
   429 (*Solution:*)
   430 
   431 text \<open>
   432   # 7: xxxxx:
   433 \<close> 
   434 ML \<open> (* # 7: ISAC suggests a next step on request *)
   435 \<close>
   436 text \<open> --- remove this line and make content Isar input---\
   437 ---------- remove this line and make content Isar input---/
   438 \<close>
   439 
   440 text \<open>
   441   # 8: xxxxx:
   442 \<close> 
   443 ML \<open> (* # 8: ISAC suggests a next step on request *)
   444 \<close>
   445 text \<open> --- remove this line and make content Isar input---\
   446 ---------- remove this line and make content Isar input---/
   447 \<close>
   448 
   449 text \<open>
   450   # 9: xxxxx:
   451 \<close> 
   452 ML \<open> (* # 9: ISAC suggests a next step on request *)
   453 \<close>
   454 text \<open> --- remove this line and make content Isar input---\
   455 ---------- remove this line and make content Isar input---/
   456 \<close>
   457 
   458 ML \<open>
   459 \<close> ML \<open>
   460 \<close> ML \<open>
   461 \<close>
   462 
   463 end