src/Tools/isac/BridgeJEdit/Calculation.thy
author wneuper <Walther.Neuper@jku.at>
Sat, 18 Jun 2022 12:34:29 +0200
changeset 60458 af7735fd252f
parent 60457 c0aa65cf50e4
child 60467 d0b31d83cfad
permissions -rw-r--r--
adapth thy to Demo_Example
     1 (*  Title:      src/Tools/isac/BridgeJEdit/Calculation.thy
     2     Author:     Walther Neuper, JKU Linz
     3 
     4 Outer syntax for Isabelle/Isac's Calculation interactive via Isabelle/PIDE.
     5 Second trial following Makarius' definition of "problem" already existing in this repository.
     6 *)
     7 
     8 theory Calculation
     9   imports
    10   (**)"$ISABELLE_ISAC/Knowledge/Build_Thydata"   (*remove after devel.of BridgeJEdit*)
    11   (**)"$ISABELLE_ISAC/MathEngine/MathEngine"
    12   (** )"SPARK_FDL"                             ( * remove after devel.of BridgeJEdit*)
    13 keywords "Example" :: thy_decl
    14 and "Problem" "Specification" "Model" "References" "Solution" 
    15 
    16 begin
    17 
    18 ML_file "parseC.sml"
    19 ML_file "preliminary.sml"
    20 
    21 section \<open>new code for Outer_Syntax Example  ...\<close>
    22 text \<open> 
    23   stepwise development starts at changeset
    24   https://hg.risc.uni-linz.ac.at/wneuper/isa/rev/9d98791b4080
    25   and can be inspected following the subsequent changesets..
    26 \<close>
    27 
    28 ML \<open>
    29 \<close> ML \<open>
    30 \<close> ML \<open>
    31 structure Data = Theory_Data
    32 (
    33   type T = Ctree.ctree option;
    34   val empty = NONE;
    35   val extend = I;
    36   fun merge _ = NONE;
    37 );
    38 
    39 val set_data = Data.put o SOME;
    40 fun the_data thy = 
    41   case Data.get thy of NONE => Ctree.EmptyPtree
    42   | SOME ctree => ctree;
    43 \<close> ML \<open>
    44 \<close> text \<open>
    45 A provisional Example collection.
    46 
    47 For notes from ISAC's discontinued Java front-end see
    48 https://isac.miraheze.org/wiki/Extend_ISAC_Knowledge#Add_an_example_in_XML_format
    49 \<close> ML \<open>
    50 type description = string
    51 type example = (description * Formalise.T)
    52 val example_store = []: (string * example) list
    53 \<close> ML \<open>
    54 val example_store = AList.update op= ("Diff_App/No.123 a", 
    55  ("The efficiency of an electrical coil depends on the mass of the kernel." ^
    56   "Given is a kernel with cross-sectional area determined by two rectangles" ^
    57   "of same shape as shown in the figure." ^
    58   "Given a radius r = 7, determine the length u and width v of the rectangles" ^
    59   "such that the cross-sectional area A (and thus the mass of the kernel) is a" ^
    60   "maximum. + Figure", 
    61    (["[r = 7]", "[A, [u, v]]", "[A = 2 \<sqdot> u \<sqdot> v - u 2 , ( 2 u ) 2 + ( 2 v ) 2 = r 2 ]", 
    62       "{0 < .. < r}"]: TermC.as_string list,
    63       (*TODO: compare paper + test/../Diff_App: more than one list of Model_Def.form_model? *)
    64     ("Diff_App", ["univariate_calculus", "Optimisation"], 
    65       ["Optimisation", "by_univariate_calculus"]): References.T)): example) example_store
    66 \<close> ML \<open>
    67 case AList.lookup op= example_store "Diff_App/No.123 a" of
    68   SOME (_, (["[r = 7]", "[A, [u, v]]", "[A = 2 \<sqdot> u \<sqdot> v - u 2 , ( 2 u ) 2 + ( 2 v ) 2 = r 2 ]", "{0 < .. < r}"], 
    69     ("Diff_App", ["univariate_calculus", "Optimisation"] ,["Optimisation", "by_univariate_calculus"]))) 
    70     => ()
    71 | _ => error "intermed. example_store CHANGED";
    72 \<close> ML \<open>
    73 \<close> ML \<open>
    74 (*? I_Model.* or Problem.* ?*)
    75 fun check_input (*ctxt o-model ??*) (_: Problem.model_input) = []: I_Model.T
    76 \<close> ML \<open>
    77 fun update_state (model, (thy_id, probl_id, meth_id)) (*at the first encounter of an Example*)
    78     Ctree.EmptyPtree =
    79     Ctree.Nd (Ctree.PblObj {
    80       fmz = Formalise.empty, origin = (O_Model.empty, References.empty, TermC.empty),
    81       spec = (thy_id, probl_id, meth_id), probl = check_input model, meth = []: Model_Def.i_model,
    82       ctxt = ContextC.empty, loc = (NONE, NONE), 
    83       branch = Ctree.NoBranch, ostate = Ctree.Incomplete, result = (TermC.empty, [])}, [])
    84   | update_state (model, (thy_id, probl_id, meth_id)) (*TODO +switch RProblem | RMethod*)
    85     (Ctree.Nd (Ctree.PblObj {fmz, origin, spec = _, probl = _, meth, ctxt, loc, branch, ostate, result}, 
    86       children)) =
    87     Ctree.Nd (Ctree.PblObj {
    88       fmz = fmz, origin = origin,
    89       spec = (thy_id, probl_id, meth_id), probl = check_input model, meth = meth,
    90       ctxt = ctxt, loc = loc, 
    91       branch = branch, ostate = ostate, result = result}, children)
    92   | update_state _ _ = raise ERROR "update_state: uncovered case in fun.def."
    93 \<close> ML \<open>
    94 update_state: Problem.model_input * (ThyC.id * Problem.id * MethodC.id) 
    95   -> Ctree.ctree -> Ctree.ctree
    96 \<close> ML \<open>
    97 type references_input = (string * (string * string))
    98 \<close> ML \<open>
    99 val parse_references_input =
   100   Parse.!!! ( \<^keyword>\<open>Theory_Ref\<close> -- \<^keyword>\<open>Theory_Ref\<close> |-- Parse.name --
   101   Parse.!!! ( \<^keyword>\<open>Problem_Ref\<close> -- \<^keyword>\<open>Theory_Ref\<close> |-- Parse.name --
   102   Parse.!!! ( \<^keyword>\<open>Method_Ref\<close> -- \<^keyword>\<open>Theory_Ref\<close> |-- Parse.name)))
   103 \<close> ML \<open>
   104 parse_references_input: references_input parser (*TODO: dpes not yet work,
   105   thus in Outer_Syntax.command \<^command_keyword>\<open>Example\<close> replacced by Problem.parse_cas*)
   106 \<close> ML \<open>
   107 op --  : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e;
   108 op |-- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> 'd * 'e
   109 \<close> ML \<open>
   110 \<close> ML \<open>
   111 \<close> ML \<open>
   112 val _ =
   113   Outer_Syntax.command \<^command_keyword>\<open>Example\<close>
   114     "prepare ISAC problem type and register it to Knowledge Store"
   115     (Parse.name -- Parse.!!! (  \<^keyword>\<open>Problem\<close> |-- Parse.name -- 
   116        Parse.!!! ( \<^keyword>\<open>Specification\<close> -- \<^keyword>\<open>:\<close> --
   117          \<^keyword>\<open>Model\<close> -- \<^keyword>\<open>:\<close>  |-- Problem.parse_model_input --
   118          Parse.!!! ( \<^keyword>\<open>References\<close> -- \<^keyword>\<open>:\<close> |-- (*parse_references_input*)Problem.parse_cas))) >>
   119       (fn (_(*exp_id*), (_(*probl_id'*), (model_input, _(*references_input*)))) =>
   120         Toplevel.theory (fn thy =>
   121           let
   122             val (thy_id, (probl_id, meth_id)) = (*references_input*)("Diff_App", ("univariate_calculus/Optimisation", "Optimisation/by_univariate_calculus"))
   123             val input = update_state (model_input, 
   124               (thy_id, References_Def.explode_id probl_id, References_Def.explode_id meth_id)) 
   125                 Ctree.EmptyPtree;
   126           in set_data input thy end)));
   127 \<close> ML \<open>
   128 \<close> ML \<open>
   129 (*/----------------------------------------------------------------------------\*)
   130      Parse.name -- Parse.!!! (  \<^keyword>\<open>Problem\<close> |-- Parse.name -- 
   131        Parse.!!! ( \<^keyword>\<open>Specification\<close> -- \<^keyword>\<open>:\<close> --
   132          \<^keyword>\<open>Model\<close> -- \<^keyword>\<open>:\<close>  |-- Problem.parse_model_input --
   133          Parse.!!! ( \<^keyword>\<open>References\<close> -- \<^keyword>\<open>:\<close> |-- parse_references_input)))
   134 : (string * (string * (Problem.model_input * references_input))) parser
   135 (*\----------------------------------------------------------------------------/*) 
   136 : Token.T list ->
   137 (*/----------------------------------------------------------------------------\*)
   138     (string * (string * (Problem.model_input * references_input)))
   139 (*\----------------------------------------------------------------------------/*)
   140 * Token.T list
   141 (*/----------------------------------------------------------------------------\* )
   142    the type above is the type of the argument of \<open>(fn (_(*name*),..\<close> 
   143 ( *\----------------------------------------------------------------------------/*)
   144 \<close> ML \<open>
   145 \<close> ML \<open>
   146 \<close> 
   147 
   148 
   149 end