src/Tools/isac/BridgeJEdit/Calculation.thy
author wneuper <Walther.Neuper@jku.at>
Wed, 03 Aug 2022 18:17:27 +0200
changeset 60508 ce09935439b3
parent 60505 137227934d2e
child 60513 cecbfe45f053
permissions -rw-r--r--
cleanup
walther@60096
     1
(*  Title:      src/Tools/isac/BridgeJEdit/Calculation.thy
walther@60095
     2
    Author:     Walther Neuper, JKU Linz
walther@60095
     3
Walther@60431
     4
Outer syntax for Isabelle/Isac's Calculation interactive via Isabelle/PIDE.
Walther@60431
     5
Second trial following Makarius' definition of "problem" already existing in this repository.
walther@60095
     6
*)
walther@60095
     7
walther@60095
     8
theory Calculation
Walther@60434
     9
  imports
Walther@60434
    10
  (**)"$ISABELLE_ISAC/Knowledge/Build_Thydata"   (*remove after devel.of BridgeJEdit*)
Walther@60434
    11
  (**)"$ISABELLE_ISAC/MathEngine/MathEngine"
Walther@60434
    12
  (** )"SPARK_FDL"                             ( * remove after devel.of BridgeJEdit*)
Walther@60450
    13
keywords "Example" :: thy_decl
Walther@60508
    14
and "Specification" "Model" "References" "Solution" 
Walther@60431
    15
walther@60095
    16
begin
wenzelm@60200
    17
Walther@60431
    18
ML_file "parseC.sml"
Walther@60431
    19
ML_file "preliminary.sml"
walther@60095
    20
Walther@60434
    21
section \<open>new code for Outer_Syntax Example  ...\<close>
Walther@60441
    22
text \<open> 
Walther@60456
    23
  stepwise development starts at changeset
Walther@60441
    24
  https://hg.risc.uni-linz.ac.at/wneuper/isa/rev/9d98791b4080
Walther@60456
    25
  and can be inspected following the subsequent changesets..
Walther@60434
    26
\<close>
walther@60123
    27
Walther@60434
    28
ML \<open>
Walther@60434
    29
\<close> ML \<open>
Walther@60434
    30
\<close> ML \<open>
Walther@60434
    31
structure Data = Theory_Data
Walther@60434
    32
(
Walther@60442
    33
  type T = Ctree.ctree option;
Walther@60434
    34
  val empty = NONE;
Walther@60434
    35
  val extend = I;
Walther@60434
    36
  fun merge _ = NONE;
Walther@60434
    37
);
walther@60150
    38
Walther@60437
    39
val set_data = Data.put o SOME;
Walther@60457
    40
fun the_data thy = 
Walther@60457
    41
  case Data.get thy of NONE => Ctree.EmptyPtree
Walther@60457
    42
  | SOME ctree => ctree;
Walther@60457
    43
\<close> ML \<open>
Walther@60457
    44
\<close> text \<open>
Walther@60457
    45
A provisional Example collection.
Walther@60434
    46
Walther@60457
    47
For notes from ISAC's discontinued Java front-end see
Walther@60457
    48
https://isac.miraheze.org/wiki/Extend_ISAC_Knowledge#Add_an_example_in_XML_format
Walther@60434
    49
\<close> ML \<open>
Walther@60434
    50
\<close> ML \<open>
Walther@60505
    51
val demo_example = ("The efficiency of an electrical coil depends on the mass of the kernel." ^
Walther@60457
    52
  "Given is a kernel with cross-sectional area determined by two rectangles" ^
Walther@60457
    53
  "of same shape as shown in the figure." ^
Walther@60457
    54
  "Given a radius r = 7, determine the length u and width v of the rectangles" ^
Walther@60457
    55
  "such that the cross-sectional area A (and thus the mass of the kernel) is a" ^
Walther@60457
    56
  "maximum. + Figure", 
Walther@60467
    57
   ([
Walther@60467
    58
   (*Problem model:*)
Walther@60467
    59
     "Constants [r = (7::real)]", "Maximum A", "AdditionalValues [u, v]",
Walther@60467
    60
     "Extremum (A = 2 * u * v - u \<up> 2)",
Walther@60467
    61
     "SideConditions [((u::real) / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2]",
Walther@60467
    62
     "SideConditions [((u::real) / 2) \<up> 2 + (2 / v) \<up> 2 = r \<up> 2]",
Walther@60467
    63
     "SideConditions [(u::real) / 2 = r * sin \<alpha>, 2 / v = r * cos \<alpha>]",
Walther@60467
    64
   (*MethodC model:*)
Walther@60467
    65
     "FunctionVariable a", "FunctionVariable b", "FunctionVariable \<alpha>",
Walther@60467
    66
     "Domain {0 <..< r}",
Walther@60467
    67
     "Domain {0 <..< r}",
Walther@60467
    68
     "Domain {0 <..< \<pi> / 2}",
Walther@60467
    69
     "ErrorBound (\<epsilon> = (0::real))"]: TermC.as_string list,
Walther@60457
    70
    ("Diff_App", ["univariate_calculus", "Optimisation"], 
Walther@60505
    71
      ["Optimisation", "by_univariate_calculus"]): References.T)): Example.T
Walther@60505
    72
\<close> 
Walther@60505
    73
setup \<open>KEStore_Elems.add_expls [(demo_example, ["Diff_App-No.123a"])]\<close>
Walther@60505
    74
ML \<open>
Walther@60505
    75
case Store.get (KEStore_Elems.get_expls @{theory}) ["Diff_App-No.123a"] ["Diff_App-No.123a"] of
Walther@60467
    76
  (_, ("Constants [r = (7::real)]" :: "Maximum A" :: "AdditionalValues [u, v]" :: _, 
Walther@60457
    77
    ("Diff_App", ["univariate_calculus", "Optimisation"] ,["Optimisation", "by_univariate_calculus"]))) 
Walther@60457
    78
    => ()
Walther@60457
    79
| _ => error "intermed. example_store CHANGED";
Walther@60457
    80
\<close> ML \<open>
Walther@60434
    81
\<close> ML \<open>
Walther@60489
    82
(*
Walther@60489
    83
  Implementation reasonable, as soo as
Walther@60489
    84
 * clarified, how "empty" items in Model are represented
Walther@60489
    85
 * parse_references_input works
Walther@60489
    86
 * switching \<Odot>\<Otimes> i_model for probl and meth works.
Walther@60489
    87
*)
Walther@60489
    88
fun check_input (*ctxt o-model ??*) (_: Problem.model_input) = []: I_Model.T (*TODO*)
Walther@60434
    89
\<close> ML \<open>
Walther@60489
    90
\<close> ML \<open>
Walther@60489
    91
(* at the first encounter of an Example create the following data and transfer them to ctree:
Walther@60467
    92
    origin: eventually used by sub-problems, too
Walther@60467
    93
    O_Model: for check of missing items
Walther@60467
    94
    I_Model: for efficient check of user input
Walther@60467
    95
*)
Walther@60467
    96
\<close> ML \<open>
Walther@60467
    97
fun init_ctree example_id =
Walther@60467
    98
    let
Walther@60489
    99
      (* TODO     vvvvvvvvvvvvvvv works only within Isac_Test_Base *)
Walther@60489
   100
      val thy = (*ThyC.get_theory thy_id*)@{theory Diff_App}
Walther@60505
   101
      val example_id' = References_Def.explode_id example_id
Walther@60505
   102
      val (_, (fmz as (model, refs as (_(*thy_id*), probl_id, _)))) =
Walther@60505
   103
        Store.get (KEStore_Elems.get_expls @{theory}) example_id' example_id' 
Walther@60489
   104
      val ctxt = ContextC.initialise' thy model;
Walther@60479
   105
Walther@60489
   106
      val o_model = Problem.from_store probl_id |> #ppc |> O_Model.init thy model
Walther@60467
   107
    in
Walther@60467
   108
      Ctree.Nd (Ctree.PblObj {
Walther@60467
   109
        fmz = fmz, origin = (o_model, refs, TermC.empty (*TODO handle CAS_Cmd*)),
Walther@60489
   110
        spec = References.empty, probl = I_Model.empty, meth = I_Model.empty,
Walther@60489
   111
        ctxt = ctxt, loc = (NONE, NONE), 
Walther@60467
   112
        branch = Ctree.NoBranch, ostate = Ctree.Incomplete, result = (TermC.empty, [])}, [])
Walther@60467
   113
    end
Walther@60467
   114
\<close> ML \<open>
Walther@60489
   115
(* 
Walther@60489
   116
At the first call 
Walther@60489
   117
* initialise ctree with origin from example_id
Walther@60489
   118
* add items already input ..TODO..TODO..TODO..TODO..TODO..TODO..TODO..TODO..TODO
Walther@60489
   119
*)
Walther@60467
   120
\<close> ML \<open>
Walther@60489
   121
fun update_state (example_id, (model, refs)) Ctree.EmptyPtree = 
Walther@60489
   122
    init_ctree example_id |>  update_state (example_id, (model, refs))
Walther@60467
   123
  | update_state (_, (model, (thy_id, probl_id, meth_id)))
Walther@60489
   124
    (Ctree.Nd (Ctree.PblObj {fmz, origin, spec as (othy, opbl, omet), probl = _, meth, ctxt, loc, branch, ostate, result}, 
Walther@60443
   125
      children)) =
Walther@60489
   126
      let 
Walther@60489
   127
        val _ = if thy_id <> othy then "re-parse all i_model" else "do nothing"
Walther@60489
   128
        val _ = if probl_id <> opbl then "switch from meth? check_input of probl" else "do nothing"
Walther@60489
   129
        val _ = if meth_id <> omet then "switch from probl check_input of meth" else "do nothing"
Walther@60489
   130
      in
Walther@60489
   131
        Ctree.Nd (Ctree.PblObj {
Walther@60494
   132
          fmz = fmz, origin = origin, spec = References.select_input spec (thy_id, probl_id, meth_id), 
Walther@60489
   133
          probl = check_input model, meth = meth, (*TODO +switch Problem_Ref | Method_Ref*)
Walther@60489
   134
          ctxt = ctxt, loc = loc, 
Walther@60489
   135
          branch = branch, ostate = ostate, result = result}, children)
Walther@60489
   136
      end
Walther@60489
   137
  | update_state _ _ = raise ERROR "update_state: uncovered case in fun.def"
Walther@60489
   138
\<close> ML \<open>
Walther@60489
   139
\<close> ML \<open>
Walther@60505
   140
update_state: string * (Problem.model_input * References.T) -> Ctree.ctree -> Ctree.ctree
Walther@60443
   141
\<close> ML \<open>
Walther@60455
   142
type references_input = (string * (string * string))
Walther@60455
   143
\<close> ML \<open>
Walther@60455
   144
val parse_references_input =
Walther@60455
   145
  Parse.!!! ( \<^keyword>\<open>Theory_Ref\<close> -- \<^keyword>\<open>Theory_Ref\<close> |-- Parse.name --
Walther@60455
   146
  Parse.!!! ( \<^keyword>\<open>Problem_Ref\<close> -- \<^keyword>\<open>Theory_Ref\<close> |-- Parse.name --
Walther@60455
   147
  Parse.!!! ( \<^keyword>\<open>Method_Ref\<close> -- \<^keyword>\<open>Theory_Ref\<close> |-- Parse.name)))
Walther@60455
   148
\<close> ML \<open>
Walther@60457
   149
parse_references_input: references_input parser (*TODO: dpes not yet work,
Walther@60457
   150
  thus in Outer_Syntax.command \<^command_keyword>\<open>Example\<close> replacced by Problem.parse_cas*)
Walther@60455
   151
\<close> ML \<open>
Walther@60455
   152
op --  : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> ('b * 'd) * 'e;
Walther@60455
   153
op |-- : ('a -> 'b * 'c) * ('c -> 'd * 'e) -> 'a -> 'd * 'e
Walther@60455
   154
\<close> ML \<open>
Walther@60455
   155
\<close> ML \<open>
Walther@60443
   156
\<close> ML \<open>
Walther@60499
   157
fun is_empty str = 
Walther@60499
   158
  let
Walther@60499
   159
    val strl = Symbol.explode str |> rev
Walther@60499
   160
  in
Walther@60499
   161
    take (2, strl) = ["_", " "] orelse take (3, strl) = ["]", "_", "["]
Walther@60499
   162
  end
Walther@60499
   163
\<close> ML \<open>
Walther@60499
   164
val m2 = "0 < r";                val m2' = Symbol.explode m2 |> rev;
Walther@60499
   165
val m3 = "Maximum _";            val m3' = Symbol.explode m3 |> rev;
Walther@60499
   166
val m4 = "AdditionalValues [_]"; val m4' = Symbol.explode m4 |> rev;
Walther@60499
   167
\<close> ML \<open>
Walther@60499
   168
\<close> ML \<open>
Walther@60434
   169
val _ =
Walther@60434
   170
  Outer_Syntax.command \<^command_keyword>\<open>Example\<close>
Walther@60434
   171
    "prepare ISAC problem type and register it to Knowledge Store"
Walther@60490
   172
    (Parse.name -- Parse.!!! ( \<^keyword>\<open>Specification\<close> -- \<^keyword>\<open>:\<close> --
Walther@60455
   173
         \<^keyword>\<open>Model\<close> -- \<^keyword>\<open>:\<close>  |-- Problem.parse_model_input --
Walther@60490
   174
         Parse.!!! ( \<^keyword>\<open>References\<close> -- \<^keyword>\<open>:\<close> |-- (*parse_references_input*) Problem.parse_cas)) >>
Walther@60499
   175
      (fn (example_id, (model_input, _(*references_input*))) =>
Walther@60434
   176
        Toplevel.theory (fn thy =>
Walther@60434
   177
          let
Walther@60499
   178
(** )
Walther@60487
   179
val _ = writeln ("#Example model_input: " ^ @{make_string} model_input)
Walther@60499
   180
(*                #Example model_input:
Walther@60487
   181
    [("#Given", ["<markup>"]), ("#Where", ["<markup>"]), ("#Find", ["<markup>", "<markup>"]), ("#Relate", ["<markup>", "<markup>"])]*)
Walther@60487
   182
val [("#Given", [m1]), ("#Where", [m2]), ("#Find", [m3, m4]), ("#Relate", [m5, m6])] = 
Walther@60487
   183
  model_input : Problem.model_input
Walther@60499
   184
val _ = writeln ("#Example m.: " ^ m1 ^ " " ^ m2 ^ " " ^ m3 ^ " " ^ m4 ^ " " ^ m4 ^ " " ^ m5 ^ " " ^ m6)
Walther@60499
   185
Walther@60499
   186
val m2' = Symbol.explode m2;
Walther@60499
   187
val _ = writeln ("#Example Symbol.explode m2: " ^ @{make_string} m2')
Walther@60499
   188
val _ = if (is_empty m2) then writeln "true" else writeln "false";
Walther@60499
   189
val _ = if (is_empty m3) then writeln "true" else writeln "false";
Walther@60499
   190
val _ = if (is_empty m4) then writeln "true" else writeln "false";
Walther@60499
   191
(*                #Example m.: Constants [r = 7] 0 < r Maximum _ AdditionalValues [_] AdditionalValues [_] Extremum _ SideCondition [_]*)
Walther@60499
   192
( **)
Walther@60455
   193
            val (thy_id, (probl_id, meth_id)) = (*references_input*)("Diff_App", ("univariate_calculus/Optimisation", "Optimisation/by_univariate_calculus"))
Walther@60467
   194
            val input = update_state (example_id, (model_input, 
Walther@60467
   195
              (thy_id, References_Def.explode_id probl_id, References_Def.explode_id meth_id)))
Walther@60455
   196
                Ctree.EmptyPtree;
Walther@60443
   197
          in set_data input thy end)));
Walther@60434
   198
\<close> ML \<open>
Walther@60447
   199
\<close> ML \<open>
Walther@60445
   200
(*/----------------------------------------------------------------------------\*)
Walther@60508
   201
     Parse.name -- Parse.!!! ( \<^keyword>\<open>Specification\<close> -- \<^keyword>\<open>:\<close> --
Walther@60455
   202
         \<^keyword>\<open>Model\<close> -- \<^keyword>\<open>:\<close>  |-- Problem.parse_model_input --
Walther@60508
   203
         Parse.!!! ( \<^keyword>\<open>References\<close> -- \<^keyword>\<open>:\<close> |-- parse_references_input))
Walther@60508
   204
(*: (string * (Problem.model_input * references_input)) parser*)
Walther@60455
   205
(*\----------------------------------------------------------------------------/*) 
Walther@60445
   206
: Token.T list ->
Walther@60445
   207
(*/----------------------------------------------------------------------------\*)
Walther@60508
   208
    (string * (Problem.model_input * references_input))
Walther@60445
   209
(*\----------------------------------------------------------------------------/*)
Walther@60445
   210
* Token.T list
Walther@60445
   211
(*/----------------------------------------------------------------------------\* )
Walther@60445
   212
   the type above is the type of the argument of \<open>(fn (_(*name*),..\<close> 
Walther@60445
   213
( *\----------------------------------------------------------------------------/*)
Walther@60434
   214
\<close> ML \<open>
Walther@60434
   215
\<close> ML \<open>
Walther@60487
   216
\<close> text \<open>
Walther@60487
   217
  INVESTIGATE Problem.Outer_Syntax.command \ <^command_keyword>\<open>problem\<close>
Walther@60487
   218
  on Biegelinie.problem pbl_bieg : "Biegelinien"
Walther@60487
   219
Walther@60487
   220
  ML-source shows error-positions already perfectly: what types are involved?..
Walther@60487
   221
\<close> ML \<open>
Walther@60487
   222
Rule_Set.append_rules "empty" Rule_Set.empty [] (* ORIGINAL source = ARGUMENT IN fn *)
Walther@60487
   223
(* (1a)
Walther@60487
   224
  Ouptut from Biegelinie.problem pbl_bieg : "Biegelinien":
Walther@60487
   225
  #problem source: Source {delimited = true, range = ({offset=38, end_offset=87, id=-1652}, {offset=87, id=-1652}), 
Walther@60487
   226
    text = "\127Rule_Set.append_rules \"empty\" Rule_Set.empty []\127"}
Walther@60487
   227
*)
Walther@60487
   228
\<close> ML \<open>
Walther@60487
   229
val source = Input.empty;
Walther@60487
   230
val ml = ML_Lex.read;
Walther@60487
   231
\<close> ML \<open>
Walther@60487
   232
  (ml "Theory.setup (Problem.set_data (" @ ML_Lex.read_source source @ ml "))")
Walther@60487
   233
(*= 
Walther@60487
   234
   [Text (Token (({}, {}), (Long_Ident, "Theory.setup"))), Text (Token (({}, {}), (Space, " "))), Text (Token (({}, {}), (Keyword, "("))), Text (Token (({}, {}), (Long_Ident, "Problem.set_data"))),
Walther@60487
   235
    Text (Token (({}, {}), (Space, " "))), Text (Token (({}, {}), (Keyword, "("))), Text (Token (({}, {}), (Space, " "))), Text (Token (({}, {}), (Keyword, ")"))), Text (Token (({}, {}), (Keyword, ")"))),
Walther@60487
   236
    Text (Token (({}, {}), (Space, " ")))]
Walther@60487
   237
   : ML_Lex.token Antiquote.antiquote list
Walther@60487
   238
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type of (1a)
Walther@60487
   239
*)
Walther@60487
   240
\<close> ML \<open>
Walther@60487
   241
ML_Context.expression (Input.pos_of source)
Walther@60487
   242
  (ml "Theory.setup (Problem.set_data (" @ ML_Lex.read_source source @ ml "))")
Walther@60487
   243
  : Context.generic -> Context.generic
Walther@60487
   244
\<close> ML \<open>
Walther@60487
   245
ML_Context.expression (Input.pos_of source)
Walther@60487
   246
  (ml "Theory.setup (Problem.set_data (" @ ML_Lex.read_source source @ ml "))")
Walther@60487
   247
  |> Context.theory_map
Walther@60487
   248
  : theory -> theory
Walther@60487
   249
\<close> ML \<open>
Walther@60487
   250
Outer_Syntax.command: Outer_Syntax.command_keyword -> string -> 
Walther@60487
   251
  (Toplevel.transition -> Toplevel.transition) parser -> unit
Walther@60487
   252
\<close> ML \<open>
Walther@60487
   253
\<close> text \<open>
Walther@60487
   254
  ML-source shows error-positions already perfectly: types involved are above.
Walther@60487
   255
Walther@60487
   256
  Input at Given does NOT show error-position, 
Walther@60487
   257
    but ISAC msg \<open>ME_Isa: thy "Isac_Knowledge" not in system\<close>
Walther@60487
   258
Walther@60487
   259
  Thus compare ML-source/input with model_input ...
Walther@60487
   260
\<close> ML \<open>
Walther@60487
   261
Problem.parse_model_input:                 Problem.model_input parser;
Walther@60487
   262
Problem.parse_model_input: Token.src ->    Problem.model_input * Token.src;
Walther@60487
   263
Problem.parse_model_input: Token.T list -> Problem.model_input * (Token.T list)
Walther@60487
   264
\<close> ML \<open>
Walther@60487
   265
[]: Position.range list;
Walther@60487
   266
[]: Position.T list;
Walther@60487
   267
\<close> ML \<open>
Walther@60487
   268
\<close> ML \<open>
Walther@60487
   269
(* Given: "Traegerlaenge l_l" "Streckenlast q_q" *)
Walther@60487
   270
(* (1b)
Walther@60487
   271
  Ouptut from Biegelinie.problem pbl_bieg : "Biegelinien":
Walther@60487
   272
  #problem model_input: [("#Given", ["<markup>", "<markup>"]), ("#Find", ["<markup>"]), ("#Relate", ["<markup>"])] 
Walther@60487
   273
                        : Problem.model_input
Walther@60487
   274
  type of  (1b) --------^^^^^^^^^^^^^^^^^^^^^^> contains ? Markup.T ?..
Walther@60487
   275
Walther@60487
   276
  #problem m1: Traegerlaenge l_l
Walther@60487
   277
  #problem m2: Streckenlast q_q 
Walther@60487
   278
  I this -----^^^______________^^^ Markup.T ?
Walther@60487
   279
*)
Walther@60487
   280
\<close> ML \<open> (** investigate parse_model_input **)
Walther@60487
   281
\<close> ML \<open> (* how get a Token.T list without Outer_Syntax.command ? *)
Walther@60487
   282
\<close> ML \<open>
Walther@60487
   283
Parse.term:                 string parser;
Walther@60487
   284
Parse.term: Token.T list -> string * Token.T list;
Walther@60487
   285
\<close> ML \<open>
Walther@60487
   286
\<close> ML \<open> (* relation Token.T -- Markup.T ??? *)
Walther@60487
   287
\<close> ML \<open>
Walther@60487
   288
val ctxt = Proof_Context.init_global @{theory};
Walther@60487
   289
val given_1 = Syntax.read_term ctxt "Traegerlaenge l_l" (* as in parse_tagged_terms *)
Walther@60487
   290
(*  ^^^^^^^ : term  -- why then evaluates parse_model_input to markup ? *)
Walther@60487
   291
\<close> ML \<open>
Walther@60487
   292
\<close> ML \<open>
Walther@60487
   293
Markup.literal_fact "Traegerlaenge l_l" = ("literal_fact", [("name", "Traegerlaenge l_l")])
Walther@60487
   294
\<close> ML \<open>
Walther@60487
   295
\<close> ML \<open>
Walther@60487
   296
\<close> ML \<open>
Walther@60487
   297
\<close>
Walther@60434
   298
Walther@60434
   299
Walther@60434
   300
end