src/Tools/isac/BridgeJEdit/Calculation.thy
author wneuper <Walther.Neuper@jku.at>
Thu, 28 Jul 2022 11:43:27 +0200
changeset 60499 d2407e9cb491
parent 60494 3dee3ec06f54
child 60505 137227934d2e
permissions -rw-r--r--
trial on "empty" 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 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 (*
    92   Implementation reasonable, as soo as
    93  * clarified, how "empty" items in Model are represented
    94  * parse_references_input works
    95  * switching \<Odot>\<Otimes> i_model for probl and meth works.
    96 *)
    97 fun check_input (*ctxt o-model ??*) (_: Problem.model_input) = []: I_Model.T (*TODO*)
    98 \<close> ML \<open>
    99 \<close> ML \<open>
   100 (* at the first encounter of an Example create the following data and transfer them to ctree:
   101     origin: eventually used by sub-problems, too
   102     O_Model: for check of missing items
   103     I_Model: for efficient check of user input
   104 *)
   105 \<close> ML \<open>
   106 fun init_ctree example_id =
   107     let
   108       (* TODO     vvvvvvvvvvvvvvv works only within Isac_Test_Base *)
   109       val thy = (*ThyC.get_theory thy_id*)@{theory Diff_App}
   110       val (_, (fmz as (model, refs as (_(*thy_id*), probl_id, _)))) = get_example example_id
   111       val ctxt = ContextC.initialise' thy model;
   112 
   113       val o_model = Problem.from_store probl_id |> #ppc |> O_Model.init thy model
   114     in
   115       Ctree.Nd (Ctree.PblObj {
   116         fmz = fmz, origin = (o_model, refs, TermC.empty (*TODO handle CAS_Cmd*)),
   117         spec = References.empty, probl = I_Model.empty, meth = I_Model.empty,
   118         ctxt = ctxt, loc = (NONE, NONE), 
   119         branch = Ctree.NoBranch, ostate = Ctree.Incomplete, result = (TermC.empty, [])}, [])
   120     end
   121 \<close> ML \<open>
   122 (* 
   123 At the first call 
   124 * initialise ctree with origin from example_id
   125 * add items already input ..TODO..TODO..TODO..TODO..TODO..TODO..TODO..TODO..TODO
   126 *)
   127 \<close> ML \<open>
   128 fun update_state (example_id, (model, refs)) Ctree.EmptyPtree = 
   129     init_ctree example_id |>  update_state (example_id, (model, refs))
   130   | update_state (_, (model, (thy_id, probl_id, meth_id)))
   131     (Ctree.Nd (Ctree.PblObj {fmz, origin, spec as (othy, opbl, omet), probl = _, meth, ctxt, loc, branch, ostate, result}, 
   132       children)) =
   133       let 
   134         val _ = if thy_id <> othy then "re-parse all i_model" else "do nothing"
   135         val _ = if probl_id <> opbl then "switch from meth? check_input of probl" else "do nothing"
   136         val _ = if meth_id <> omet then "switch from probl check_input of meth" else "do nothing"
   137       in
   138         Ctree.Nd (Ctree.PblObj {
   139           fmz = fmz, origin = origin, spec = References.select_input spec (thy_id, probl_id, meth_id), 
   140           probl = check_input model, meth = meth, (*TODO +switch Problem_Ref | Method_Ref*)
   141           ctxt = ctxt, loc = loc, 
   142           branch = branch, ostate = ostate, result = result}, children)
   143       end
   144   | update_state _ _ = raise ERROR "update_state: uncovered case in fun.def"
   145 \<close> ML \<open>
   146 \<close> ML \<open>
   147 \<close> ML \<open>
   148 update_state: example_id * (Problem.model_input * References.T) -> Ctree.ctree -> Ctree.ctree
   149 \<close> ML \<open>
   150 type references_input = (string * (string * string))
   151 \<close> ML \<open>
   152 val parse_references_input =
   153   Parse.!!! ( \<^keyword>\<open>Theory_Ref\<close> -- \<^keyword>\<open>Theory_Ref\<close> |-- Parse.name --
   154   Parse.!!! ( \<^keyword>\<open>Problem_Ref\<close> -- \<^keyword>\<open>Theory_Ref\<close> |-- Parse.name --
   155   Parse.!!! ( \<^keyword>\<open>Method_Ref\<close> -- \<^keyword>\<open>Theory_Ref\<close> |-- Parse.name)))
   156 \<close> ML \<open>
   157 parse_references_input: references_input parser (*TODO: dpes not yet work,
   158   thus in Outer_Syntax.command \<^command_keyword>\<open>Example\<close> replacced by Problem.parse_cas*)
   159 \<close> ML \<open>
   160 op --  : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e;
   161 op |-- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> 'd * 'e
   162 \<close> ML \<open>
   163 \<close> ML \<open>
   164 \<close> ML \<open>
   165 fun is_empty str = 
   166   let
   167     val strl = Symbol.explode str |> rev
   168   in
   169     take (2, strl) = ["_", " "] orelse take (3, strl) = ["]", "_", "["]
   170   end
   171 \<close> ML \<open>
   172 val m2 = "0 < r";                val m2' = Symbol.explode m2 |> rev;
   173 val m3 = "Maximum _";            val m3' = Symbol.explode m3 |> rev;
   174 val m4 = "AdditionalValues [_]"; val m4' = Symbol.explode m4 |> rev;
   175 \<close> ML \<open>
   176 \<close> ML \<open>
   177 val _ =
   178   Outer_Syntax.command \<^command_keyword>\<open>Example\<close>
   179     "prepare ISAC problem type and register it to Knowledge Store"
   180     (Parse.name -- Parse.!!! ( \<^keyword>\<open>Specification\<close> -- \<^keyword>\<open>:\<close> --
   181          \<^keyword>\<open>Model\<close> -- \<^keyword>\<open>:\<close>  |-- Problem.parse_model_input --
   182          Parse.!!! ( \<^keyword>\<open>References\<close> -- \<^keyword>\<open>:\<close> |-- (*parse_references_input*) Problem.parse_cas)) >>
   183       (fn (example_id, (model_input, _(*references_input*))) =>
   184         Toplevel.theory (fn thy =>
   185           let
   186 (** )
   187 val _ = writeln ("#Example model_input: " ^ @{make_string} model_input)
   188 (*                #Example model_input:
   189     [("#Given", ["<markup>"]), ("#Where", ["<markup>"]), ("#Find", ["<markup>", "<markup>"]), ("#Relate", ["<markup>", "<markup>"])]*)
   190 val [("#Given", [m1]), ("#Where", [m2]), ("#Find", [m3, m4]), ("#Relate", [m5, m6])] = 
   191   model_input : Problem.model_input
   192 val _ = writeln ("#Example m.: " ^ m1 ^ " " ^ m2 ^ " " ^ m3 ^ " " ^ m4 ^ " " ^ m4 ^ " " ^ m5 ^ " " ^ m6)
   193 
   194 val m2' = Symbol.explode m2;
   195 val _ = writeln ("#Example Symbol.explode m2: " ^ @{make_string} m2')
   196 val _ = if (is_empty m2) then writeln "true" else writeln "false";
   197 val _ = if (is_empty m3) then writeln "true" else writeln "false";
   198 val _ = if (is_empty m4) then writeln "true" else writeln "false";
   199 (*                #Example m.: Constants [r = 7] 0 < r Maximum _ AdditionalValues [_] AdditionalValues [_] Extremum _ SideCondition [_]*)
   200 ( **)
   201             val (thy_id, (probl_id, meth_id)) = (*references_input*)("Diff_App", ("univariate_calculus/Optimisation", "Optimisation/by_univariate_calculus"))
   202             val input = update_state (example_id, (model_input, 
   203               (thy_id, References_Def.explode_id probl_id, References_Def.explode_id meth_id)))
   204                 Ctree.EmptyPtree;
   205           in set_data input thy end)));
   206 \<close> ML \<open>
   207 \<close> ML \<open>
   208 (*/----------------------------------------------------------------------------\*)
   209      Parse.name -- Parse.!!! (  \<^keyword>\<open>Problem\<close> |-- Parse.name -- 
   210        Parse.!!! ( \<^keyword>\<open>Specification\<close> -- \<^keyword>\<open>:\<close> --
   211          \<^keyword>\<open>Model\<close> -- \<^keyword>\<open>:\<close>  |-- Problem.parse_model_input --
   212          Parse.!!! ( \<^keyword>\<open>References\<close> -- \<^keyword>\<open>:\<close> |-- parse_references_input)))
   213 : (string * (string * (Problem.model_input * references_input))) parser
   214 (*\----------------------------------------------------------------------------/*) 
   215 : Token.T list ->
   216 (*/----------------------------------------------------------------------------\*)
   217     (string * (string * (Problem.model_input * references_input)))
   218 (*\----------------------------------------------------------------------------/*)
   219 * Token.T list
   220 (*/----------------------------------------------------------------------------\* )
   221    the type above is the type of the argument of \<open>(fn (_(*name*),..\<close> 
   222 ( *\----------------------------------------------------------------------------/*)
   223 \<close> ML \<open>
   224 \<close> ML \<open>
   225 \<close> text \<open>
   226   INVESTIGATE Problem.Outer_Syntax.command \ <^command_keyword>\<open>problem\<close>
   227   on Biegelinie.problem pbl_bieg : "Biegelinien"
   228 
   229   ML-source shows error-positions already perfectly: what types are involved?..
   230 \<close> ML \<open>
   231 Rule_Set.append_rules "empty" Rule_Set.empty [] (* ORIGINAL source = ARGUMENT IN fn *)
   232 (* (1a)
   233   Ouptut from Biegelinie.problem pbl_bieg : "Biegelinien":
   234   #problem source: Source {delimited = true, range = ({offset=38, end_offset=87, id=-1652}, {offset=87, id=-1652}), 
   235     text = "\127Rule_Set.append_rules \"empty\" Rule_Set.empty []\127"}
   236 *)
   237 \<close> ML \<open>
   238 val source = Input.empty;
   239 val ml = ML_Lex.read;
   240 \<close> ML \<open>
   241   (ml "Theory.setup (Problem.set_data (" @ ML_Lex.read_source source @ ml "))")
   242 (*= 
   243    [Text (Token (({}, {}), (Long_Ident, "Theory.setup"))), Text (Token (({}, {}), (Space, " "))), Text (Token (({}, {}), (Keyword, "("))), Text (Token (({}, {}), (Long_Ident, "Problem.set_data"))),
   244     Text (Token (({}, {}), (Space, " "))), Text (Token (({}, {}), (Keyword, "("))), Text (Token (({}, {}), (Space, " "))), Text (Token (({}, {}), (Keyword, ")"))), Text (Token (({}, {}), (Keyword, ")"))),
   245     Text (Token (({}, {}), (Space, " ")))]
   246    : ML_Lex.token Antiquote.antiquote list
   247    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type of (1a)
   248 *)
   249 \<close> ML \<open>
   250 ML_Context.expression (Input.pos_of source)
   251   (ml "Theory.setup (Problem.set_data (" @ ML_Lex.read_source source @ ml "))")
   252   : Context.generic -> Context.generic
   253 \<close> ML \<open>
   254 ML_Context.expression (Input.pos_of source)
   255   (ml "Theory.setup (Problem.set_data (" @ ML_Lex.read_source source @ ml "))")
   256   |> Context.theory_map
   257   : theory -> theory
   258 \<close> ML \<open>
   259 Outer_Syntax.command: Outer_Syntax.command_keyword -> string -> 
   260   (Toplevel.transition -> Toplevel.transition) parser -> unit
   261 \<close> ML \<open>
   262 \<close> text \<open>
   263   ML-source shows error-positions already perfectly: types involved are above.
   264 
   265   Input at Given does NOT show error-position, 
   266     but ISAC msg \<open>ME_Isa: thy "Isac_Knowledge" not in system\<close>
   267 
   268   Thus compare ML-source/input with model_input ...
   269 \<close> ML \<open>
   270 Problem.parse_model_input:                 Problem.model_input parser;
   271 Problem.parse_model_input: Token.src ->    Problem.model_input * Token.src;
   272 Problem.parse_model_input: Token.T list -> Problem.model_input * (Token.T list)
   273 \<close> ML \<open>
   274 []: Position.range list;
   275 []: Position.T list;
   276 \<close> ML \<open>
   277 \<close> ML \<open>
   278 (* Given: "Traegerlaenge l_l" "Streckenlast q_q" *)
   279 (* (1b)
   280   Ouptut from Biegelinie.problem pbl_bieg : "Biegelinien":
   281   #problem model_input: [("#Given", ["<markup>", "<markup>"]), ("#Find", ["<markup>"]), ("#Relate", ["<markup>"])] 
   282                         : Problem.model_input
   283   type of  (1b) --------^^^^^^^^^^^^^^^^^^^^^^> contains ? Markup.T ?..
   284 
   285   #problem m1: Traegerlaenge l_l
   286   #problem m2: Streckenlast q_q 
   287   I this -----^^^______________^^^ Markup.T ?
   288 *)
   289 \<close> ML \<open> (** investigate parse_model_input **)
   290 \<close> ML \<open> (* how get a Token.T list without Outer_Syntax.command ? *)
   291 \<close> ML \<open>
   292 Parse.term:                 string parser;
   293 Parse.term: Token.T list -> string * Token.T list;
   294 \<close> ML \<open>
   295 \<close> ML \<open> (* relation Token.T -- Markup.T ??? *)
   296 \<close> ML \<open>
   297 val ctxt = Proof_Context.init_global @{theory};
   298 val given_1 = Syntax.read_term ctxt "Traegerlaenge l_l" (* as in parse_tagged_terms *)
   299 (*  ^^^^^^^ : term  -- why then evaluates parse_model_input to markup ? *)
   300 \<close> ML \<open>
   301 \<close> ML \<open>
   302 Markup.literal_fact "Traegerlaenge l_l" = ("literal_fact", [("name", "Traegerlaenge l_l")])
   303 \<close> ML \<open>
   304 \<close> ML \<open>
   305 \<close> ML \<open>
   306 \<close>
   307 
   308 
   309 end