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