src/Tools/isac/BridgeJEdit/Calculation.thy
author wneuper <Walther.Neuper@jku.at>
Mon, 20 Jun 2022 18:37:54 +0200
changeset 60469 89e1d8a633bb
parent 60467 d0b31d83cfad
child 60479 165ced2bbd60
permissions -rw-r--r--
rename functions in o-model.sml
     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 type example_id = string
    53 val example_store = Unsynchronized.ref ([]: (example_id * example) list)
    54 fun get_example example_id =
    55   case AList.lookup op= (!example_store) example_id of
    56     NONE => raise ERROR ("Examnple " ^ quote example_id ^ " not found")
    57   | SOME expl => expl
    58 \<close> ML \<open>
    59 get_example: example_id -> example;
    60 \<close> ML \<open>
    61 example_store := AList.update op= ("Diff_App/No.123 a", 
    62  ("The efficiency of an electrical coil depends on the mass of the kernel." ^
    63   "Given is a kernel with cross-sectional area determined by two rectangles" ^
    64   "of same shape as shown in the figure." ^
    65   "Given a radius r = 7, determine the length u and width v of the rectangles" ^
    66   "such that the cross-sectional area A (and thus the mass of the kernel) is a" ^
    67   "maximum. + Figure", 
    68    ([
    69    (*Problem model:*)
    70      "Constants [r = (7::real)]", "Maximum A", "AdditionalValues [u, v]",
    71      "Extremum (A = 2 * u * v - u \<up> 2)",
    72      "SideConditions [((u::real) / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2]",
    73      "SideConditions [((u::real) / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2]",
    74      "SideConditions [(u::real) / 2 = r * sin \<alpha>, 2 / v = r * cos \<alpha>]",
    75    (*MethodC model:*)
    76      "FunctionVariable a", "FunctionVariable b", "FunctionVariable \<alpha>",
    77      "Domain {0 <..< r}",
    78      "Domain {0 <..< r}",
    79      "Domain {0 <..< \<pi> / 2}",
    80      "ErrorBound (\<epsilon> = (0::real))"]: TermC.as_string list,
    81     ("Diff_App", ["univariate_calculus", "Optimisation"], 
    82       ["Optimisation", "by_univariate_calculus"]): References.T)): example) (!example_store)
    83 \<close> ML \<open>
    84 case get_example "Diff_App/No.123 a" of
    85   (_, ("Constants [r = (7::real)]" :: "Maximum A" :: "AdditionalValues [u, v]" :: _, 
    86     ("Diff_App", ["univariate_calculus", "Optimisation"] ,["Optimisation", "by_univariate_calculus"]))) 
    87     => ()
    88 | _ => error "intermed. example_store CHANGED";
    89 \<close> ML \<open>
    90 \<close> ML \<open>
    91 fun check_input (*ctxt o-model ??*) (_: Problem.model_input) = []: I_Model.T
    92 \<close> ML \<open>
    93 (*at the first encounter of an Example
    94   transfer Example to ctree:
    95     origin: eventually used by sub-problems, too
    96     O_Model: for check of missing items
    97     I_Model: for efficient check of user input
    98 *)
    99 \<close> ML \<open>
   100 \<close> ML \<open>
   101 \<close> ML \<open>
   102 \<close> ML \<open>
   103 \<close> ML \<open>
   104 fun init_ctree example_id =
   105     let
   106       val (_, (fmz as (model, refs as (_(*thy_id*), probl_id, _)))) = get_example example_id
   107       val o_model =    (* TODO works only within Isac_Test_Base ---vvvvvvvvvvvvvvv *)
   108       (*Problem.from_store probl_id |> #ppc |> O_Model.init model (ThyC.get_theory thy_id)*)
   109         Problem.from_store probl_id |> #ppc |> O_Model.init @{theory Diff_App} model
   110     in
   111       Ctree.Nd (Ctree.PblObj {
   112         fmz = fmz, origin = (o_model, refs, TermC.empty (*TODO handle CAS_Cmd*)),
   113         spec = References.empty, probl = I_Model.empty, meth = I_Model.empty,
   114         ctxt = ContextC.empty, loc = (NONE, NONE), 
   115         branch = Ctree.NoBranch, ostate = Ctree.Incomplete, result = (TermC.empty, [])}, [])
   116     end
   117 \<close> ML \<open>
   118 \<close> ML \<open>
   119 fun update_state (example_id, _) Ctree.EmptyPtree = init_ctree example_id
   120   (*TODO +switch RProblem | RMethod*)
   121   | update_state (_, (model, (thy_id, probl_id, meth_id)))
   122     (Ctree.Nd (Ctree.PblObj {fmz, origin, spec = _, probl = _, meth, ctxt, loc, branch, ostate, result}, 
   123       children)) =
   124     Ctree.Nd (Ctree.PblObj {
   125       fmz = fmz, origin = origin,
   126       spec = (thy_id, probl_id, meth_id), probl = check_input model, meth = meth,
   127       ctxt = ctxt, loc = loc, 
   128       branch = branch, ostate = ostate, result = result}, children)
   129   | update_state _ _ = raise ERROR "update_state: uncovered case in fun.def."
   130 \<close> ML \<open>
   131 update_state: example_id * (Problem.model_input * References.T) -> Ctree.ctree -> Ctree.ctree
   132 \<close> ML \<open>
   133 type references_input = (string * (string * string))
   134 \<close> ML \<open>
   135 val parse_references_input =
   136   Parse.!!! ( \<^keyword>\<open>Theory_Ref\<close> -- \<^keyword>\<open>Theory_Ref\<close> |-- Parse.name --
   137   Parse.!!! ( \<^keyword>\<open>Problem_Ref\<close> -- \<^keyword>\<open>Theory_Ref\<close> |-- Parse.name --
   138   Parse.!!! ( \<^keyword>\<open>Method_Ref\<close> -- \<^keyword>\<open>Theory_Ref\<close> |-- Parse.name)))
   139 \<close> ML \<open>
   140 parse_references_input: references_input parser (*TODO: dpes not yet work,
   141   thus in Outer_Syntax.command \<^command_keyword>\<open>Example\<close> replacced by Problem.parse_cas*)
   142 \<close> ML \<open>
   143 op --  : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e;
   144 op |-- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> 'd * 'e
   145 \<close> ML \<open>
   146 \<close> ML \<open>
   147 \<close> ML \<open>
   148 val _ =
   149   Outer_Syntax.command \<^command_keyword>\<open>Example\<close>
   150     "prepare ISAC problem type and register it to Knowledge Store"
   151     (Parse.name -- Parse.!!! (  \<^keyword>\<open>Problem\<close> |-- Parse.name -- 
   152        Parse.!!! ( \<^keyword>\<open>Specification\<close> -- \<^keyword>\<open>:\<close> --
   153          \<^keyword>\<open>Model\<close> -- \<^keyword>\<open>:\<close>  |-- Problem.parse_model_input --
   154          Parse.!!! ( \<^keyword>\<open>References\<close> -- \<^keyword>\<open>:\<close> |-- (*parse_references_input*)Problem.parse_cas))) >>
   155       (fn (example_id, (_(*probl_id'*), (model_input, _(*references_input*)))) =>
   156         Toplevel.theory (fn thy =>
   157           let
   158             val (thy_id, (probl_id, meth_id)) = (*references_input*)("Diff_App", ("univariate_calculus/Optimisation", "Optimisation/by_univariate_calculus"))
   159             val input = update_state (example_id, (model_input, 
   160               (thy_id, References_Def.explode_id probl_id, References_Def.explode_id meth_id)))
   161                 Ctree.EmptyPtree;
   162           in set_data input thy end)));
   163 \<close> ML \<open>
   164 \<close> ML \<open>
   165 (*/----------------------------------------------------------------------------\*)
   166      Parse.name -- Parse.!!! (  \<^keyword>\<open>Problem\<close> |-- Parse.name -- 
   167        Parse.!!! ( \<^keyword>\<open>Specification\<close> -- \<^keyword>\<open>:\<close> --
   168          \<^keyword>\<open>Model\<close> -- \<^keyword>\<open>:\<close>  |-- Problem.parse_model_input --
   169          Parse.!!! ( \<^keyword>\<open>References\<close> -- \<^keyword>\<open>:\<close> |-- parse_references_input)))
   170 : (string * (string * (Problem.model_input * references_input))) parser
   171 (*\----------------------------------------------------------------------------/*) 
   172 : Token.T list ->
   173 (*/----------------------------------------------------------------------------\*)
   174     (string * (string * (Problem.model_input * references_input)))
   175 (*\----------------------------------------------------------------------------/*)
   176 * Token.T list
   177 (*/----------------------------------------------------------------------------\* )
   178    the type above is the type of the argument of \<open>(fn (_(*name*),..\<close> 
   179 ( *\----------------------------------------------------------------------------/*)
   180 \<close> ML \<open>
   181 \<close> ML \<open>
   182 \<close> 
   183 
   184 
   185 end