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