test/Pure/Isar/Keywords_Diag.thy
author Walther Neuper <walther.neuper@jku.at>
Fri, 17 Jul 2020 11:42:20 +0200
changeset 60028 bb97dcbf7360
parent 60027 3e8c8c3dcd41
permissions -rw-r--r--
cleanup Test_Parse*, start parsers for keyword ISAC
walther@60027
     1
(* Preparations for Isabelle/jEdit as Isac front-end.
walther@60027
     2
   Walther Neuper 2018
walther@60027
     3
*)
walther@60027
     4
text \<open>These are first trials on introducing Isabelle/jEdit as Isac front-end with hints by Makarius.
walther@60027
     5
  This file cannot be contained in Test_Isac as soon as "xxx" (within
walther@60027
     6
  "Outer_Syntax.command @ {command_keyword xxx"}) is also known to Build_Isac.thy .
walther@60027
     7
walther@60027
     8
  Stepwise incrementing the parser requires outcommenting for the same reason.
walther@60027
     9
\<close>
walther@60027
    10
subsection \<open>definitions for keywords\<close>
walther@60027
    11
walther@60027
    12
theory Keywords_Diag
walther@60028
    13
imports Isac.Isac_Knowledge (*Pure: not for Problem Biegelinie*)
walther@60027
    14
keywords
walther@60027
    15
  (* this has a type and thus is a "major keyword" *)
walther@60027
    16
  "ISAC" :: diag and
walther@60027
    17
  (* the following are without type: "minor keywords" *)
walther@60028
    18
  "Problem" (* root-problem + recursively in Solution *)
walther@60027
    19
  "Specification" "Model" "References" "Solution" (* structure only *) and
walther@60027
    20
  "Given" "Find" "Relate" "Where" (* await input of term *) and
walther@60028
    21
  "RTheory" (* await input of string; "R" distingues "Problem".."RProblem" *) and
walther@60028
    22
  "RProblem" "RMethod" (* await input of string list *) and
walther@60027
    23
  "Tactic" (* optionally repeated with each step 0.. end of calculation *)
walther@60027
    24
begin
walther@60027
    25
walther@60027
    26
subsection \<open>analyse functions' signatures in the command definition\<close>
walther@60027
    27
subsubsection \<open>get args' types from command definition\<close>
walther@60027
    28
ML \<open>
walther@60027
    29
(*val _ =
walther@60027
    30
  Outer_Syntax.command @{command_keyword ISAC}
walther@60027
    31
    "embedded ISAC language" (* an initial hint by Makarius ...*)*)
walther@60027
    32
    (Parse.input Parse.cartouche) >> (fn input =>
walther@60027
    33
      Toplevel.keep (fn _ => ignore (ML_Lex.read_source input)))
walther@60027
    34
  : Token.T list -> (Toplevel.transition -> Toplevel.transition) * Token.T list;
walther@60027
    35
(*---------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ value of >>    *)
walther@60027
    36
\<close> ML \<open>
walther@60028
    37
op >> : ('a -> 'b * 'c) * ('b -> 'd) -> 'a -> 'd * 'c (* args -------vvv-- 1st -- 2nd*)
walther@60028
    38
(*      ('a parser : T list -> 'a * T list
walther@60028
    39
                        * (Input.source -> (Toplevel.transition -> Toplevel.transition))*)
walther@60028
    40
\<close> ML \<open>
walther@60027
    41
(Parse.input Parse.cartouche) : Input.source parser;
walther@60027
    42
(*-----------------------------^^^^^^^^^^^^^^^^^^^^^----------------------------- 1st arg of >> *)
walther@60027
    43
\<close> ML \<open>
walther@60027
    44
(fn input =>
walther@60027
    45
      Toplevel.keep (fn _ => ignore (ML_Lex.read_source input)))
walther@60027
    46
  : Input.source -> (Toplevel.transition -> Toplevel.transition)
walther@60027
    47
(*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2nd arg of >> *)
walther@60027
    48
\<close> ML \<open> (*decompose and investigate 1st arg..-------------------------------------------------  *)
walther@60027
    49
Parse.input : 'a parser -> Input.source parser;
walther@60027
    50
Parse.cartouche : string parser
walther@60028
    51
\<close> ML \<open>
walther@60027
    52
val input = Input.string "123"
walther@60027
    53
(* = Source {delimited = true, range = ({}, {}), text = "123"}*)
walther@60027
    54
: Input.source;
walther@60027
    55
walther@60027
    56
ML_Lex.read_source  (*true*) input
walther@60027
    57
(* = [Text (Token (({}, {}), (Int, "123"))), Text (Token (({}, {}), (Space, " ")))]*)
walther@60027
    58
  : ML_Lex.token Antiquote.antiquote list
walther@60027
    59
\<close> ML \<open>
walther@60027
    60
val input = Input.string "abc"
walther@60027
    61
(* = Source {delimited = true, range = ({}, {}), text = "abc"}*)
walther@60027
    62
: Input.source;
walther@60027
    63
(*  ------decompose and investigate 2nd arg..-------------------------------------------------  *)
walther@60027
    64
\<close> ML \<open>
walther@60027
    65
ML_Lex.read_source  (*true*) input
walther@60027
    66
(* = [Text (Token (({}, {}), (Ident, "abc"))), Text (Token (({}, {}), (Space, " ")))]*)
walther@60027
    67
  : ML_Lex.token Antiquote.antiquote list
walther@60027
    68
;
walther@60027
    69
ignore (ML_Lex.read_source input) : unit
walther@60027
    70
\<close> ML \<open>
walther@60027
    71
Toplevel.keep (fn _ => ignore (ML_Lex.read_source input))
walther@60027
    72
  : Toplevel.transition -> Toplevel.transition
walther@60027
    73
;
walther@60027
    74
Toplevel.keep
walther@60027
    75
  : (Toplevel.state -> unit) -> Toplevel.transition -> Toplevel.transition
walther@60027
    76
\<close> 
walther@60027
    77
subsubsection \<open>infer args' types of >> in command definition from types above\<close>
walther@60027
    78
ML \<open>
walther@60027
    79
op >> : ('a -> 'b * 'c) * ('b -> 'd) -> 'a -> 'd * 'c
walther@60027
    80
(*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ signature of >>*)
walther@60027
    81
(* analysis of types in "Outer_Syntax.command @{command_keyword ISAC}.." starts from value of >> :
walther@60027
    82
                                     -> 'a -> 'd * 'c
walther@60027
    83
value                                  \-------------/ *)
walther@60027
    84
(*                                      'a : Token.T list
walther@60027
    85
                                           -> 'd : Toplevel.transition -> Toplevel.transition
walther@60027
    86
                                                 * 'c : Token.T list
walther@60027
    87
2nd arg                   \--------/
walther@60027
    88
                           'b : Input.source
walther@60027
    89
                              -> 'd : Toplevel.transition -> Toplevel.transition    ((from value))
walther@60027
    90
1st arg \-------------/
walther@60027
    91
         'a : Token.T list                                     ((from value, initialised with []))
walther@60027
    92
            -> 'b : Input.source                               ((from 2nd arg                   ))
walther@60027
    93
                  * 'c : Token.T list                          ((from value, tail still unparsed))
walther@60027
    94
         ^^^^^^^^^^^^^^^^^^^^^^^^^^ = Input.source parser : type 'a parser = T list -> 'a * T list
walther@60027
    95
*)
walther@60027
    96
\<close> ML \<open>
walther@60027
    97
(*//------------------------------------------------------------------------------------------\\*)
walther@60027
    98
Parse.input Parse.cartouche : Input.source parser;
walther@60027
    99
(*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1st arg of >>  *)
walther@60027
   100
\<close> ML \<open>
walther@60027
   101
Parse.input : 'a parser -> Input.source parser;
walther@60027
   102
Parse.cartouche : string parser
walther@60027
   103
(*\\------------------------------------------------------------------------------------------//*)
walther@60028
   104
\<close> ML \<open>
walther@60028
   105
\<close>
walther@60028
   106
walther@60028
   107
subsubsection \<open>tokenise input"\<close>
walther@60028
   108
ML \<open>
walther@60028
   109
\<close> ML \<open>
walther@60028
   110
  Outer_Syntax.command @{command_keyword TEST_SIGNAT} "investigate signatures involveD"
walther@60028
   111
    ((Parse.input Parse.cartouche) >> (fn input =>
walther@60028
   112
      Toplevel.keep (fn _ => ignore (ML_Lex.read_source input) ) ) ) : unit
walther@60028
   113
\<close> ML \<open>
walther@60028
   114
ML_Lex.read_source: Input.source -> (ML_Lex.token Antiquote.antiquote) list
walther@60028
   115
\<close> ML \<open>
walther@60028
   116
Source.source:
walther@60028
   117
   'a Scan.stopper ->
walther@60028
   118
     ('a list -> 'b list * 'a list) ->
walther@60028
   119
       ('a, 'c) Source.source -> ('b, ('a, 'c) Source.source) Source.source
walther@60028
   120
\<close> ML \<open>
walther@60028
   121
\<close> ML \<open>
walther@60028
   122
\<close> ML \<open>
walther@60028
   123
K
walther@60028
   124
\<close> ML \<open>
walther@60028
   125
\<close> ML \<open>
walther@60027
   126
\<close>
walther@60027
   127
walther@60027
   128
subsection \<open>stepwise incrementing the parser\<close>
walther@60027
   129
text \<open>goal of this subsection is to extend ISAC_parser such, that the "complete ISAC calculation"
walther@60027
   130
  at the bottom of this file is is parsed finally.
walther@60027
   131
walther@60027
   132
  "Outer_Syntax.command @ {command_keyword xxx}" is allowed only once in a theory,
walther@60027
   133
  so outcomment accordingly !
walther@60027
   134
\<close>
walther@60028
   135
subsubsection \<open>step -1: investigate signatures "Problem Specification Model -- STOPPED HERE"\<close>
walther@60027
   136
ML \<open>
walther@60027
   137
\<close> ML \<open>
walther@60027
   138
Parse.input : 'a parser -> Input.source parser;
walther@60027
   139
Parse.cartouche : string parser; (* ... how build such parser <<<<<<<<<<<<<<---------------?(2)*)
walther@60027
   140
Parse.input Parse.cartouche : Input.source parser;
walther@60027
   141
\<close> ML \<open>
walther@60027
   142
val input = Input.string "Problem Specification Model": Input.source
walther@60027
   143
\<close> ML \<open>
walther@60027
   144
(* build a dummy parser according to "?(2)": *)
walther@60027
   145
Parse.$$$ : string -> string parser;
walther@60027
   146
Parse.$$$ "Problem Specification Model" : string parser
walther@60027
   147
\<close> ML \<open>
walther@60027
   148
val xxx = Parse.input (Parse.$$$ "Problem Specification Model") : Input.source parser;
walther@60027
   149
xxx : Input.source parser;
walther@60027
   150
\<close> ML \<open>
walther@60027
   151
val xxx : Input.source parser = fn (args : Token.T list) => (input, args);
walther@60027
   152
(*                    where from get ------^^^^^^^^^^^^^            <<<<<<<<<<<<<<----------?(1)*)
walther@60027
   153
(* ================== STOPPED WITH THIS QUESTION ===============================================*)
walther@60027
   154
\<close> ML \<open>
walther@60027
   155
ML_Lex.read_source : (*bool ->*) Input.source -> ML_Lex.token Antiquote.antiquote list
walther@60027
   156
(* we don't need                                          ^^^^^^^^^^^^^^^^^^^ here *)
walther@60027
   157
\<close> ML \<open>
walther@60027
   158
ML_Lex.read_source  (*true*) input (* =
walther@60027
   159
   [Text (Token (({}, {}), (Ident, "Problem"))), Text (Token (({}, {}), (Space, " "))),
walther@60027
   160
    Text (Token (({}, {}), (Ident, "Specification"))), Text (Token (({}, {}), (Space, " "))),
walther@60027
   161
    Text (Token (({}, {}), (Ident, "Model"))), Text (Token (({}, {}), (Space, " ")))]:
walther@60027
   162
   ML_Lex.token Antiquote.antiquote list
walther@60027
   163
*)
walther@60027
   164
\<close> ML \<open>
walther@60027
   165
\<close> 
walther@60028
   166
subsubsection \<open>step 0: provide a structure for steps\<close>
walther@60028
   167
ML \<open>
walther@60028
   168
val ISAC_parser = Parse.cartouche;
walther@60028
   169
ISAC_parser: string parser
walther@60028
   170
\<close> ML \<open>
walther@60028
   171
val check_ISAC : (Token.T list -> Input.source * Token.T list) = fn input => 
walther@60028
   172
  let
walther@60028
   173
    val toks = Parse.input ISAC_parser input
walther@60028
   174
    val _ (*just check if input is according to ..*) = ISAC_parser
walther@60028
   175
  in toks end
walther@60028
   176
\<close> text \<open> (*ML -> Duplicate outer syntax command "ISAC" *)
walther@60028
   177
val _ =  (*-------------v--------------------*)
walther@60028
   178
  Outer_Syntax.command @ {command_keyword ISAC} "parsing stepwise incremented by check_ISAC"
walther@60028
   179
    (check_ISAC >> (fn input => Toplevel.keep (fn _ => ignore (ML_Lex.read_source input))))
walther@60028
   180
\<close> ML \<open>
walther@60028
   181
\<close>
walther@60027
   182
walther@60028
   183
subsubsection \<open>step 1: Problem ("Biegelinie", ["Biegelinien"]) etc\<close>
walther@60028
   184
text \<open>
walther@60028
   185
  we have {term "Problem (''Biegelinie'', [''Biegelinien''])"} !!!
walther@60028
   186
  !!! as formula + as Tactic
walther@60028
   187
\<close>
walther@60027
   188
ML \<open>
walther@60028
   189
val str =      "Problem (''Biegelinie'', [''Biegelinien''])"
walther@60028
   190
val t = @{term "Problem (''Biegelinie'', [''Biegelinien''])"}
walther@60028
   191
\<close> ML \<open>
walther@60028
   192
val input = Input.string str
walther@60028
   193
\<close> ML \<open>
walther@60028
   194
val toks = ML_Lex.read_source input;
walther@60028
   195
\<close> ML \<open>
walther@60028
   196
toks: ML_Lex.token Antiquote.antiquote list
walther@60028
   197
\<close> ML \<open>
walther@60028
   198
length toks = 11;
walther@60028
   199
nth 1 toks (* = Text (Token (({}, {}), (Ident, "Problem")))   <--------- (Keyword, "Problem") *);
walther@60028
   200
nth 2 toks (* = Text (Token (({}, {}), (Space, " ")))*);
walther@60028
   201
nth 3 toks (* = Text (Token (({}, {}), (Keyword, "(")))*);
walther@60028
   202
nth 4 toks (*Text (Token (({}, {}), (Type_Var, "''Biegelinie''")))   <---------- Type_Var !?!?*);
walther@60028
   203
nth 10 toks (* = Text (Token (({}, {}), (Keyword, ")")))*);
walther@60028
   204
\<close> ML \<open>
walther@60028
   205
ignore (ML_Lex.read_source input) : unit;
walther@60028
   206
\<close> ML \<open>
walther@60028
   207
\<close> ML \<open>
walther@60028
   208
\<close> ML \<open>
walther@60028
   209
val str = "a=b";
walther@60028
   210
val input = Input.string str
walther@60028
   211
val toks = ML_Lex.read_source  (*true*) input;
walther@60028
   212
nth 2 toks (* = Text (Token (({}, {}), (Keyword, "=")))    ----------^^^ (Keyword, "Problem") *);
walther@60028
   213
\<close> ML \<open>
walther@60028
   214
\<close>
walther@60027
   215
walther@60028
   216
subsubsection \<open>step 2: Model\<close>
walther@60028
   217
ML \<open>
walther@60028
   218
\<close> ML \<open>
walther@60028
   219
val ISAC_parser = Parse.cartouche;
walther@60028
   220
ISAC_parser: string parser
walther@60028
   221
\<close> ML \<open>
walther@60028
   222
val parse_Model = Parse.reserved "Model" --
walther@60028
   223
  Parse.reserved "Given" -- Parse.list (Parse.term) --
walther@60028
   224
  Parse.reserved "Where" -- Parse.list (Parse.term) --
walther@60028
   225
  Parse.reserved "Find" -- Parse.term --
walther@60028
   226
  Parse.reserved "Relate" -- Parse.list (Parse.term)
walther@60028
   227
\<close> ML \<open>
walther@60028
   228
parse_Model: Token.T list ->
walther@60028
   229
  ((((((((string * string) * string list) * string) * string list) * string) * string) * string) * string list) * Token.T list
walther@60028
   230
\<close> ML \<open> (*/---------- -> ERROR text cartouche was found ----------\*)
walther@60028
   231
val ISAC_parser = (*a* ) parse_Model -- Parse.cartouche ( *a*)
walther@60028
   232
                  (*b*) Parse.cartouche -- parse_Model (*b*)
walther@60028
   233
\<close> ML \<open>
walther@60028
   234
val check_ISAC : (Token.T list -> Input.source * Token.T list) = fn input => 
walther@60028
   235
  let
walther@60028
   236
    val toks = Parse.input ISAC_parser input
walther@60028
   237
    val _ (*just check if input is according to ..*) = ISAC_parser
walther@60028
   238
  in toks end
walther@60028
   239
\<close> ML \<open> (*ML -> Duplicate outer syntax command "ISAC" *)
walther@60028
   240
val _ =  (*-------------v--------------------*)
walther@60028
   241
  Outer_Syntax.command @{command_keyword ISAC} "parsing stepwise incremented by check_ISAC"
walther@60028
   242
    (check_ISAC >> (fn input => Toplevel.keep (fn _ => ignore (ML_Lex.read_source input))))
walther@60028
   243
\<close> ML \<open>
walther@60028
   244
\<close>
walther@60028
   245
(*/----- with above check_ISAC + ISAC_parser (*a*) | (*b*)*)
walther@60028
   246
ISAC \<open>Model:
walther@60027
   247
      Given: "Traegerlaenge L", "Streckenlast q_0"
walther@60027
   248
      Where: "q_0 ist_integrierbar_auf {| 0, L |}", "0 < L"
walther@60027
   249
      Find: "Biegelinie y"
walther@60027
   250
      Relate: "Randbedingungen [ Q_0 = q_0 * L, M_b L = 0, y_0 = 0, d_d x y_0 = 0 ]"
walther@60028
   251
\<close>
walther@60028
   252
(* ----- ERROR (*a*) | (*b*): * )
walther@60028
   253
(*a*) Outer syntax error: reserved identifier "Model" expected,
walther@60028
   254
but text cartouche was found:
walther@60028
   255
\<open>Model: ...
walther@60028
   256
walther@60028
   257
(*b*) Outer syntax error\<^here>: reserved identifier "Model" expected,
walther@60028
   258
but end-of-input\<^here> was found
walther@60028
   259
( *\---------- -> ERROR text cartouche was found ----------/*)
walther@60028
   260
walther@60028
   261
walther@60028
   262
subsection \<open>Makarius' hint on rendering ISAC calculation\<close>
walther@60028
   263
text \<open>
walther@60028
   264
  ISAC command definition, original by Makarius
walther@60028
   265
/--- outcommented during stepwise incrementing the parser ----------------------------\
walther@60028
   266
walther@60028
   267
keywords "ISAC" :: diag
walther@60028
   268
walther@60028
   269
ML \<open>
walther@60028
   270
val _ =
walther@60028
   271
  Outer_Syntax.command @ {command_keyword ISAC}
walther@60028
   272
    "embedded ISAC language" (* an initial hint by Makarius *)
walther@60028
   273
    (Parse.input Parse.cartouche >> 
walther@60028
   274
      (fn input => Toplevel.keep (fn _ => ignore (ML_Lex.read_source input))))
walther@60028
   275
\<close>
walther@60028
   276
\--- outcommented during stepwise incrementing the parser ----------------------------/
walther@60028
   277
\<close>
walther@60028
   278
subsection \<open>complete ISAC calculation\<close>
walther@60028
   279
(*/--- outcomment during stepwise incrementing the parser ----------------------------\*)
walther@60028
   280
ISAC \<open>
walther@60028
   281
"Problem ("Biegelinie", ["Biegelinien"])"
walther@60028
   282
  Specification:
walther@60028
   283
    Model:
walther@60028
   284
      Given: ["Traegerlaenge L", "Streckenlast q_0"] 
walther@60028
   285
      Where: ["q_0 ist_integrierbar_auf {| 0, L |}", "0 < L"]
walther@60028
   286
      Find: "Biegelinie y"
walther@60028
   287
      Relate: ["Randbedingungen [ Q_0 = q_0 * L, M_b L = 0, y_0 = 0, d_d x y_0 = 0 ]"]
walther@60027
   288
    References:
walther@60028
   289
      RTheory: "Biegelinie"
walther@60028
   290
      RProblem: ["Biegelinien"]
walther@60028
   291
      RMethod: ["Integrieren", "KonstanteBestimmen2"]
walther@60027
   292
  Solution:
walther@60028
   293
    "Problem ("Biegelinie", ["vonBelastungZu", "Biegelinien"])"
walther@60027
   294
    "[Q x = c + -1 * q_0 * x, M_b x = c_2 + c * x + -1 * q_0 / 2 * x ^ 2,  y' x =  c_3 + 1 / (-1 * EI) * (c_2 * x + c / 2 * x ^ 2 + -1 * q_0 / 6 * x ^ 3),  y x =  c_4 + c_3 * x +  1 / (-1 * EI) *  (c_2 / 2 * x ^ 2 + c / 6 * x ^ 3 + -1 * q_0 / 24 * x ^ 4)]"
walther@60028
   295
walther@60028
   296
    "Problem ("Biegelinie", ["setzeRandbedingungen", "Biegelinien"])"
walther@60027
   297
    "[L * q_0 = c, 0 = (2 * c_2 + 2 * L * c + -1 * L ^ 2 * q_0) / 2, 0 = c_4,  0 = c_3]"
walther@60028
   298
walther@60027
   299
    "solveSystem [L * q_0 = c, 0 = (2 * c_2 + 2 * L * c + -1 * L ^ 2 * q_0) / 2, 0 = c_4,  0 = c_3] [c, c_1, c_2, c_4]"
walther@60027
   300
    "[c = -1 * L * q_0 / -1, c_2 = -1 * L ^ 2 * q_0 / 2, c_3 = 0, c_4 = 0]"
walther@60028
   301
walther@60027
   302
    "y x = c_4 + c_3 * x + 1 / (-1 * EI) * (c_2 / 2 * x ^ 2 + c / 6 * x ^ 3 + -1 * q_0 / 24 * x ^ 4)"
walther@60027
   303
      Tactic Substitute "c_4 + c_3 * x +  1 / (-1 * EI) *  (c_2 / 2 * x ^ 2 + c / 6 * x ^ 3 + -1 * q_0 / 24 * x ^ 4)" "[c, c_1, c_2, c_4]"
walther@60027
   304
    "y x = 0 + 0 * x + 1 / (-1 * EI) * (-1 * L ^ 2 * q_0 / 2 / 2 * x ^ 2 + -1 * L * q_0 / -1 / 6 * x ^ 3 +  -1 * q_0 / 24 * x ^ 4)"
walther@60027
   305
      Tactic Rewrite_Set_Inst "[(bdv, x)]" "make_ratpoly_in" "y x"
walther@60027
   306
    "y x = -6 * q_0 * L ^ 2 / (-24 * EI) * x ^ 2 + 4 * L * q_0 / (-24 * EI) * x ^ 3 + -1 * q_0 / (-24 * EI) * x ^ 4"
walther@60027
   307
      Tactic Check_Postcond ["Biegelinien"]
walther@60027
   308
"y x = -6 * q_0 * L ^ 2 / (-24 * EI) * x ^ 2 + 4 * L * q_0 / (-24 * EI) * x ^ 3 + -1 * q_0 / (-24 * EI) * x ^ 4"
walther@60027
   309
\<close>
walther@60028
   310
(*\--- outcomment during stepwise incrementing the parser ----------------------------/*)
walther@60028
   311
walther@60027
   312
end