src/Tools/isac/Knowledge/Build_Thydata.thy
author wneuper <Walther.Neuper@jku.at>
Thu, 15 Dec 2022 13:03:51 +0100
changeset 60624 0e0ac7706f0d
parent 60608 5dabcc1c9235
child 60631 d5a69b98afc3
permissions -rw-r--r--
proper indication of error-msg "Wrong descriptor" in problem/method
walther@60149
     1
(*  Title:      Tools/isac/Knowledge/Build_Thydata.thy
walther@60149
     2
    Author:     Walther Neuper, JKU Linz
walther@60149
     3
    (c) due to copyright terms
walther@60149
     4
walther@60149
     5
Collect all knowledge defined for the isac mathengine
walther@60149
     6
*)
neuper@42400
     7
neuper@42400
     8
theory Build_Thydata
walther@60115
     9
imports
walther@60115
    10
  Isac_Knowledge
neuper@55408
    11
  Test_Build_Thydata (*for test/../build_thydata.sml, thy_hierarchy.sml*)
neuper@42400
    12
begin
neuper@42400
    13
wneuper@59472
    14
subsection \<open>Build <Theory>-Data\<close>
wneuper@59472
    15
text \<open>
neuper@55405
    16
  <Theory>-Data represent Isac's "deductive knowledge" (whereas <Problems> and <Methods> 
neuper@55405
    17
  represent application-oriented and algorithmic knowledge respectively.) <Theory>-Data 
neuper@55405
    18
  comprise theories from ProgLang and from Knowledge.
wenzelm@60282
    19
wenzelm@60282
    20
  Note that under normal circumstances, theories are only available from the given formal
wenzelm@60282
    21
  context, not the global theory database (\<^ML_structure>\<open>Thy_Info\<close>). The following
wenzelm@60282
    22
  operations always work:
wenzelm@60282
    23
wenzelm@60282
    24
    \<^item> \<^ML>\<open>@{theory Pure}\<close>, \<^ML>\<open>@{theory Main}\<close>, \<^ML>\<open>@{theory Complex_Main}\<close>
wenzelm@60282
    25
    \<^item> \<^ML>\<open>Theory.ancestors_of\<close>, or better \<^ML>\<open>Theory.nodes_of\<close>
wenzelm@60282
    26
    \<^item> \<^ML>\<open>Context.get_theory\<close> for situations of explicit name-lookup (short or long)
wenzelm@60282
    27
wenzelm@60282
    28
  In contrast, old-style \<^ML>\<open>Thy_Info.get_theory\<close> requires a finished logic image, for
wenzelm@60282
    29
  example in \<^file>\<open>$ISABELLE_ISAC_TEST/Tools/isac/Test_Some.thy\<close> loaded from session Isac.
wenzelm@60282
    30
Walther@60554
    31
  All theories below ML_structure\<open>MathEngine\<close> in the theories' dependency graph
walther@60359
    32
  can be considered as Isac's version of Pure: the respective code handles items
walther@60359
    33
  declared later between \<^ML>\<open>@{theory Base_Tools}\<close> and \<^ML>\<open>@{theory Isac_Knowledge}\<close>.
walther@60359
    34
  These items are contained in the  ML_structure\<open>Context\<close> in current Isabelle,
Walther@60588
    35
  thus all ML_structure\<open>Know_Store\<close> can be replaced by more native Isabelle mechanisms --
walther@60359
    36
  with two exceptions
Walther@60588
    37
    \<^item> \<^ML>\<open>Know_Store.add_pbls\<close> and \<^ML>\<open>Know_Store.get_pbls\<close>, 
walther@60359
    38
      handling a tree of \<^ML_type>\<open>Problem.T\<close>
Walther@60588
    39
    \<^item> \<^ML>\<open>Know_Store.add_mets\<close> and \<^ML>\<open>Know_Store.get_mets\<close>, 
walther@60359
    40
      handling a tree of \<^ML_type>\<open>MethodC.T\<close>
walther@60359
    41
  where both trees have a structure independent from the dependency graph 
walther@60359
    42
  between \<^ML>\<open>@{theory Base_Tools}\<close> and \<^ML>\<open>@{theory Isac_Knowledge}\<close>.
walther@60359
    43
wneuper@59472
    44
\<close>
wneuper@59472
    45
subsubsection \<open>Get and group the theories defined in Isac\<close>
wneuper@59472
    46
ML \<open>
neuper@55456
    47
  (* we don't use "fun isacthys ()" etc HERE, 
neuper@55456
    48
    because Thy_Info.get_theory only works with session Isac, not with Build_Isac.thy *)
neuper@55405
    49
  val knowledge_parent = @{theory}
neuper@55405
    50
  val proglang_parent = @{theory ProgLang}
neuper@42400
    51
neuper@55405
    52
  val allthys = Theory.ancestors_of @{theory};
wneuper@59335
    53
  val allthys = filter_out (member Context.eq_thy (* thys for isac bootstrap *)
wneuper@59600
    54
    [(*@{theory ProgLang}: required in isacthys' below*) @{theory Interpret}, @{theory MathEngine},
wneuper@59600
    55
    @{theory BridgeLibisabelle}, knowledge_parent]) allthys         (*["Isac_Knowledge", ..., "Isac_Knowledge"]*)
neuper@42400
    56
wneuper@59592
    57
  val isabthys' =                         (*["Complex_Main", "Taylor", .., "Isac_Knowledge"]*)
walther@60082
    58
    drop ((find_index (curry Context.eq_thy ThyC.last_isabelle_thy) allthys), allthys);
walther@59887
    59
  val isacthys' =                         (*["Isac_Knowledge", "Inverse_Z_Transform",  .., "Know_Store"]*)
walther@60082
    60
    take ((find_index (curry Context.eq_thy ThyC.last_isabelle_thy) allthys), allthys)
walther@59603
    61
    |> remove Context.eq_thy @{theory Test_Build_Thydata};
neuper@42400
    62
walther@60125
    63
  val knowthys' =                         (*["Isac_Knowledge", .., "Input_Descript"]*)
wneuper@59335
    64
    take ((find_index (curry Context.eq_thy proglang_parent) isacthys'), isacthys');
walther@59887
    65
  val progthys' =                         (*["Auto_Prog", .., "Know_Store"]*)
walther@59603
    66
    drop ((find_index (curry Context.eq_thy proglang_parent) isacthys') + 1(*ProgLang*), isacthys')
walther@59866
    67
    |> remove Context.eq_thy @{theory BaseDefinitions};
walther@59603
    68
\<close> 
walther@59603
    69
declare[[ML_print_depth = 999]]
walther@59603
    70
ML \<open>
walther@59603
    71
\<close> ML \<open>
walther@60359
    72
\<close> ML \<open>
wneuper@59472
    73
\<close>
neuper@42400
    74
wneuper@59472
    75
subsubsection \<open>From rule-sets collect theorems, which have been taken from Isabelle\<close>
wneuper@59472
    76
ML \<open>
neuper@55484
    77
  val isacrlsthms =                      (*length = 460*)
Walther@60588
    78
    Thy_Hierarchy.thms_of_rlss @{theory} (Know_Store.get_rlss knowledge_parent) : ThmC.T list
neuper@42400
    79
neuper@55484
    80
  val rlsthmsNOTisac = isacrlsthms       (*length =  36*)
neuper@55405
    81
    |> filter (fn (deriv, _) => 
walther@59879
    82
      member op= (map Context.theory_name isabthys') (ThyC.cut_id deriv))
walther@59874
    83
    : ThmC.T list
wneuper@59472
    84
\<close> 
neuper@55405
    85
wneuper@59472
    86
subsubsection \<open>Collect data in a (key, data) list and insert data into the tree\<close>
wneuper@59472
    87
text \<open>
neuper@55405
    88
  The sequence in the (key, data) list is arbitrary, because insertion using the key
neuper@55405
    89
  is random. The sequence of the root nodes "IsacKnowledge", "Isabelle"
walther@59887
    90
  and "IsacScripts" is determined in Know_Store.thy.
wneuper@59472
    91
\<close>
walther@59621
    92
text \<open>
walther@59621
    93
  ATTENTION: ===================================================================================
walther@59621
    94
  the code below does NOT terminate in x86_64_32 mode of Poly/ML 5.8.
walther@59621
    95
  Nevertheless, Build_Isac.thy should (and can!) be used in this mode,
walther@59621
    96
  while preferences ML_system_64 = "true" never reaches Build_Isac.thy.
walther@59621
    97
  ==============================================================================================
walther@59621
    98
  See "etc/preferences".
walther@59621
    99
\<close>
wneuper@59472
   100
ML \<open>
walther@59621
   101
val thydata_list = (* SEE NOTE ABOVE *)
walther@60253
   102
  Thy_Hierarchy.collect_part       "IsacKnowledge" knowledge_parent knowthys' @
walther@60253
   103
  (map (Thy_Hierarchy.collect_isab "Isabelle") rlsthmsNOTisac) @
walther@60253
   104
  Thy_Hierarchy.collect_part       "IsacScripts" proglang_parent progthys'
walther@59917
   105
: (Thy_Write.theID * Thy_Write.thydata) list
wneuper@59472
   106
\<close>
walther@59614
   107
ML \<open>
walther@59880
   108
map Context.theory_name knowthys'
walther@59614
   109
\<close> ML \<open>
walther@59880
   110
Context.theory_name knowledge_parent
walther@59614
   111
\<close> text \<open>
walther@59614
   112
  collect_part       "IsacKnowledge" knowledge_parent knowthys'
walther@59614
   113
\<close> ML \<open>
walther@59614
   114
"~~~~~ fun collect_part , args:"; val (part, parent, thys) = ("IsacKnowledge", knowledge_parent, knowthys');
walther@59614
   115
\<close> ML \<open>
walther@60253
   116
    val xxx = (flat (map (Thy_Hierarchy.collect_thms part) thys))
Walther@60624
   117
\<close> ML \<open>
Walther@60624
   118
val 34 = length thys;
Walther@60624
   119
val 479 = length xxx;
walther@59614
   120
\<close> text \<open>
Walther@60588
   121
     (collect_rlss part (Know_Store.get_rlss parent) thys)
walther@59614
   122
\<close> text \<open>
Walther@60588
   123
"~~~~~ fun collect_rlss , args:"; val (part, rlss, thys) = (part, (Know_Store.get_rlss parent), thys);
walther@59614
   124
\<close> text \<open>
walther@59614
   125
(* Build_Thydata.thy in jedit leads to "Exception- Size raised" <<<=============================== 
walther@59614
   126
   =============================== but batch process =============================================
walther@59614
   127
     ~~$ ./bin/isabelle build -o browser_info -v -b Isac
walther@59614
   128
   =============================== works =========================================================
walther@59614
   129
*)
Walther@60588
   130
Know_Store.get_rlss parent
walther@59614
   131
\<close>
walther@59614
   132
Walther@60588
   133
setup \<open>Know_Store.add_thes (map (fn (a, b) => (b, a)) thydata_list)\<close>
neuper@42429
   134
wneuper@59472
   135
section \<open>interface for dialog-authoring\<close>
wneuper@59472
   136
subsection \<open>Add error patterns\<close>
wneuper@59472
   137
subsubsection \<open>Error patterns for differentiation\<close>
neuper@55411
   138
Walther@60588
   139
setup \<open>Know_Store.add_mets @{context}
Walther@60575
   140
  [Pbl_Met_Hierarchy.update_metpair @{theory Diff} ["diff", "differentiate_on_R"]
s1210629013@55378
   141
      [("chain-rule-diff-both",
Walther@60574
   142
        [TermC.parse_patt @{theory Diff} "d_d ?bdv (sin ?u) = cos (d_d ?bdv ?u)",
Walther@60574
   143
         TermC.parse_patt @{theory Diff} "d_d ?bdv (cos ?u) = - sin (d_d ?bdv ?u)",
Walther@60574
   144
         TermC.parse_patt @{theory Diff} "d_d ?bdv (?u \<up> ?n) = ?n * ?u \<up> (?n - 1)",
Walther@60574
   145
         TermC.parse_patt @{theory Diff} "d_d ?bdv (LogExp.ln ?u) = 1 / ?u",
Walther@60574
   146
         TermC.parse_patt @{theory Diff} "d_d ?bdv (LogExp.ln ?u) = 1 / d_d ?bdv ?u"],
s1210629013@55378
   147
        [@{thm diff_sin_chain}, @{thm diff_cos_chain}, @{thm diff_pow_chain}, @{thm diff_ln_chain},
s1210629013@55378
   148
          @{thm  diff_exp_chain}])]]
wneuper@59472
   149
\<close>
neuper@42453
   150
wneuper@59472
   151
setup \<open>
Walther@60588
   152
Know_Store.insert_fillpats
neuper@55448
   153
  [(["IsacKnowledge", "Diff", "Theorems", "diff_sin_chain"],
neuper@55425
   154
    ([("fill-d_d-arg",
wneuper@59390
   155
      TermC.parse_patt @{theory Diff} "d_d ?bdv (sin ?u) = cos ?u * d_d ?bdv _", "chain-rule-diff-both"),
neuper@55425
   156
    ("fill-both-args",
wneuper@59390
   157
      TermC.parse_patt @{theory Diff} "d_d ?bdv (sin _) = cos ?u * d_d ?bdv _", "chain-rule-diff-both"),
neuper@55425
   158
    ("fill-d_d",
wneuper@59390
   159
      TermC.parse_patt @{theory Diff} "d_d ?bdv (sin ?u) = cos ?u * _ ?bdv ?u", "chain-rule-diff-both"),
neuper@55425
   160
    ("fill-inner-deriv",
wneuper@59390
   161
      TermC.parse_patt @{theory Diff} "d_d ?bdv (sin ?u) = cos ?u * _", "chain-rule-diff-both"),
neuper@55425
   162
    ("fill-all",
walther@59909
   163
      TermC.parse_patt @{theory Diff} "d_d ?bdv (sin ?u) = _", "chain-rule-diff-both")]: Error_Pattern_Def.fill_in list))
neuper@55425
   164
  ]
wneuper@59472
   165
\<close>
neuper@55411
   166
wneuper@59472
   167
subsubsection \<open>Error patterns for calculation with rationals\<close>
neuper@55411
   168
Walther@60588
   169
setup \<open>Know_Store.add_mets @{context}
Walther@60575
   170
  [Pbl_Met_Hierarchy.update_metpair @{theory Rational} ["simplification", "of_rationals"]
s1210629013@55378
   171
      [("addition-of-fractions",
Walther@60574
   172
        [TermC.parse_patt @{theory Rational} "(?a / ?b + ?c / ?d) = (?a + ?c)/(?b + ?d)",
Walther@60574
   173
         TermC.parse_patt @{theory Rational} "(?a / ?b + ?c / ?d) = (?a + ?c)/(?b * ?d)",
Walther@60574
   174
         TermC.parse_patt @{theory Rational} "(?a / ?b + ?c / ?d) = (?c * ?a)/(?d * ?b)",
Walther@60574
   175
         TermC.parse_patt @{theory Rational} "?a + (?b /?c)= (?a + ?b)/ ?c",
Walther@60574
   176
         TermC.parse_patt @{theory Rational} "?a + (?b /?c)= (?a * ?b)/ ?c"],
s1210629013@55378
   177
        [@{thm rat_add}, @{thm rat_add_assoc}, @{thm rat_add1}, @{thm rat_add1_assoc},
s1210629013@55378
   178
          @{thm rat_add2}, @{thm rat_add2_assoc}, @{thm rat_add3}, @{thm rat_add3_assoc}])]]
wneuper@59472
   179
\<close>
neuper@42516
   180
wneuper@59472
   181
ML \<open>
neuper@42516
   182
(*TODO insert_fillpats ["IsacKnowledge", "Rational", "Theorems", "???"]*)
neuper@42516
   183
neuper@42516
   184
(* TODO insert_errpats overwrites instead of appending
walther@59997
   185
insert_errpats ["simplification", "of_rationals"]
neuper@42516
   186
 (("cancel", (*see master thesis gdarocy*)
neuper@48895
   187
    [(*a*)parse_patt @{theory Rational} "(?a + ?b)/?a = ?b",
neuper@48895
   188
     (*b*)parse_patt @{theory Rational} "(?a + ?b)/?b = ?a",
neuper@48895
   189
     (*c*)parse_patt @{theory Rational} "(?a + ?b)/(?b + ?c) = ?a / ?c",
neuper@48895
   190
     (*d*)parse_patt @{theory Rational} "(?a + ?c)/(?b + ?c) = ?a / ?b",
neuper@48895
   191
     (*e*)parse_patt @{theory Rational} "(?a + ?c)/(?b + ?c) = ?a / ?c",
neuper@48895
   192
     (*f*)parse_patt @{theory Rational} "(?a + ?b)/(?c + ?a) = ?b / ?c",
neuper@48895
   193
     (*g*)parse_patt @{theory Rational} "(?a*?b + ?c)/?a = ?b + ?c",
neuper@48895
   194
     (*h*)parse_patt @{theory Rational} "(?a*?b + ?c)/?b = ?a + ?c",
neuper@48895
   195
     (*i*)parse_patt @{theory Rational} "(?a + ?b*?c)/?b = ?a + ?c",
neuper@48895
   196
     (*j*)parse_patt @{theory Rational} "(?a + ?b*?c)/?b = ?a + ?b",
neuper@48895
   197
     (*k*)parse_patt @{theory Rational} "(?a + ?b*?e)/(?c + ?d*?e) = (?a + ?b)/(?c + ?d)"],
neuper@42516
   198
    [@{thm real_times_divide_1_eq}, (*TODO*) @{thm real_times_divide_1_eq}]));
neuper@42516
   199
neuper@42445
   200
val fillpats = [
neuper@42445
   201
  ("fill-cancel-left-num",
neuper@42445
   202
    parse_patt @{theory Rational} "(?a * ?b) / (?a * ?c) = _ / ?c", "cancel"),
neuper@42445
   203
  ("fill-cancel-left-den",
neuper@42445
   204
    parse_patt @{theory Rational} "(?a * ?b) / (?a * ?c) = ?b / _", "cancel"),
neuper@42445
   205
  ("fill-cancel-none",
neuper@42445
   206
    parse_patt @{theory Rational} "(?a * ?b) / (?a * ?c) = ?b / ?c", "cancel"),
neuper@42445
   207
neuper@42445
   208
  ("fill-cancel-left-add-num1",
neuper@42445
   209
    parse_patt @{theory Rational} "(?a * (?b + ?c)) / (?a * ?d) = (_ + ?c) / ?d", "cancel"),
neuper@42445
   210
  ("fill-cancel-left-add-num2",
neuper@42445
   211
    parse_patt @{theory Rational} "(?a * (?b + ?c)) / (?a * ?d) = (?b + _) / ?d", "cancel"),
neuper@42445
   212
  ("fill-cancel-left-add-num3",
neuper@42445
   213
    parse_patt @{theory Rational} "(?a * (?b + ?c)) / (?a * ?d) = (_ + _) / ?d", "cancel"),
neuper@42445
   214
  ("fill-cancel-left-add-num4",
neuper@42445
   215
    parse_patt @{theory Rational} "(?a * (?b + ?c)) / (?a * ?d) = (?b + ?c) / _", "cancel"),
neuper@42445
   216
  ("fill-cancel-left-add-num5",
neuper@42445
   217
    parse_patt @{theory Rational} "(?a * (?b + ?c)) / (?a * ?d) = (_ + ?c) / _", "cancel"),
neuper@42445
   218
  ("fill-cancel-left-add-num6",
neuper@42445
   219
    parse_patt @{theory Rational} "(?a * (?b + ?c)) / (?a * ?d) = (?b + _) / _", "cancel"),
neuper@42445
   220
  ("fill-cancel-left-add-none",
neuper@42445
   221
    parse_patt @{theory Rational} "(?a * (?b + ?c)) / (?a * ?d) = (?b + ?c) / ?d", "cancel"),
neuper@42445
   222
neuper@42445
   223
  ("fill-cancel-left-add-den1",
neuper@42445
   224
    parse_patt @{theory Rational} "(?a * ?b) / (?a * (?c + ?d)) = ?b / (?c + ?d)", "cancel"),
neuper@42445
   225
  ("fill-cancel-left-add-den2",
neuper@42445
   226
    parse_patt @{theory Rational} "(?a * ?b) / (?a * (?c + ?d)) = ?b / (?c + ?d)", "cancel"),
neuper@42445
   227
  ("fill-cancel-left-add-den3",
neuper@42445
   228
    parse_patt @{theory Rational} "(?a * ?b) / (?a * (?c + ?d)) = ?b / (?c + ?d)", "cancel"),
neuper@42445
   229
  ("fill-cancel-left-add-den4",
neuper@42445
   230
    parse_patt @{theory Rational} "(?a * ?b) / (?a * (?c + ?d)) = ?b / (?c + ?d)", "cancel"),
neuper@42445
   231
  ("fill-cancel-left-add-den5",
neuper@42445
   232
    parse_patt @{theory Rational} "(?a * ?b) / (?a * (?c + ?d)) = ?b / (?c + ?d)", "cancel"),
neuper@42445
   233
  ("fill-cancel-left-add-den6",
neuper@42445
   234
    parse_patt @{theory Rational} "(?a * ?b) / (?a * (?c + ?d)) = ?b / (?c + ?d)", "cancel"),
neuper@42445
   235
  ("fill-cancel-left-add-none",
neuper@42445
   236
    parse_patt @{theory Rational} "(?a * ?b) / (?a * (?c + ?d)) = ?b / (?c + ?d)", "cancel")
neuper@42445
   237
 ]: fillpat list;
neuper@42445
   238
*)
neuper@42445
   239
neuper@55462
   240
(* still ununsed; planned for stepToErrorpattern: this is just a reminder... 
neuper@55435
   241
s1210629013@55442
   242
insert_errpatIDs @{theory} ["IsacKnowledge", "Diff", "Rulesets", "norm_diff"]
neuper@42454
   243
  (["chain-rule-diff-both", "cancel"]: errpatID list);
walther@59997
   244
[[[ERROR *** app_py: not found: ["IsacKnowledge", "Diff", "Rulesets", "norm_diff"]
Walther@60608
   245
  since Know_Store.set_ref_last_thy has been shifted below;
Walther@60588
   246
  this ERROR will vanish during re-engineering towards Know_Store.]]]
neuper@42453
   247
neuper@55462
   248
   ...and all other related rls by hand;
neuper@42453
   249
   TODO: lift errpattIDs from theorems to respective rls AUTOMATICALLY *)
wneuper@59472
   250
\<close>
neuper@48886
   251
Walther@60588
   252
section \<open>Link Know_Store to completed IsacKnowledge\<close>
Walther@60608
   253
ML \<open>Know_Store.set_ref_last_thy @{theory}\<close>
neuper@55435
   254
neuper@42400
   255
end