src/Tools/isac/Knowledge/Build_Thydata.thy
author Walther Neuper <wneuper@ist.tugraz.at>
Thu, 15 Mar 2018 12:42:04 +0100
changeset 59406 509d70b507e5
parent 59396 d1aae4e79859
child 59472 3e904f8ec16c
permissions -rw-r--r--
separate structure Celem: CALC_ELEMENT, finished on src/
     1 (* theory collecting all knowledge defined for the isac mathengine
     2    WN.11.00
     3  *)
     4 
     5 theory Build_Thydata
     6 imports 
     7   Isac "~~/src/Tools/isac/Interpret/Interpret"
     8   Test_Build_Thydata (*for test/../build_thydata.sml, thy_hierarchy.sml*)
     9 begin
    10 
    11 subsection {* Build <Theory>-Data *}
    12 text {*
    13   <Theory>-Data represent Isac's "deductive knowledge" (whereas <Problems> and <Methods> 
    14   represent application-oriented and algorithmic knowledge respectively.) <Theory>-Data 
    15   comprise theories from ProgLang and from Knowledge.
    16   Note, that for bootstrapping Isac "Build_Thydata" theories are available only
    17   via "@{theory Pure}" and "Theory.ancestors_of" but not via "Thy_Info_get_theory"
    18   (while the latter is available in Test_Some.thy loaded from session Isac).
    19 *}
    20 subsubsection {* Get and group the theories defined in Isac *}
    21 ML {*
    22   (* we don't use "fun isacthys ()" etc HERE, 
    23     because Thy_Info.get_theory only works with session Isac, not with Build_Isac.thy *)
    24   val knowledge_parent = @{theory}
    25   val proglang_parent = @{theory ProgLang}
    26 
    27   val allthys = Theory.ancestors_of @{theory};
    28   val allthys = filter_out (member Context.eq_thy (* thys for isac bootstrap *)
    29     [(*@{theory ProgLang}: required in isacthys' below*) @{theory Interpret}, @{theory xmlsrc},
    30     @{theory Frontend}, knowledge_parent]) allthys         (*["Isac", ..., "Isac"]*)
    31 
    32   val isabthys' =                         (*["Complex_Main", "Taylor", .., "Isac"]*)
    33     drop ((find_index (curry Context.eq_thy @{theory Complex_Main}) allthys), allthys);
    34   val isacthys' =                         (*["Isac", "Inverse_Z_Transform",  .., "KEStore"]*)
    35     take ((find_index (curry Context.eq_thy @{theory Complex_Main}) allthys), allthys);
    36 
    37   val knowthys' =                         (*["Isac", .., "Descript", "Delete"]*)
    38     take ((find_index (curry Context.eq_thy proglang_parent) isacthys'), isacthys');
    39   val progthys' =                         (*["Script", "Tools", "ListC", "KEStore"]*)
    40     drop ((find_index (curry Context.eq_thy proglang_parent) isacthys') + 1(*ProgLang*), isacthys');
    41 *}
    42 
    43 subsubsection {* From rule-sets collect theorems, which have been taken from Isabelle *}
    44 ML {*
    45   val isacrlsthms =                      (*length = 460*)
    46     thms_of_rlss @{theory} (KEStore_Elems.get_rlss knowledge_parent) : (Celem.thmID * thm) list
    47 
    48   val rlsthmsNOTisac = isacrlsthms       (*length =  36*)
    49     |> filter (fn (deriv, _) => 
    50       member op= (map Context.theory_name isabthys') (Celem.thyID_of_derivation_name deriv))
    51     : (Celem.thmID * thm) list
    52 *} 
    53 
    54 subsubsection {* Collect data in a (key, data) list and insert data into the tree *}
    55 text {*
    56   The sequence in the (key, data) list is arbitrary, because insertion using the key
    57   is random. The sequence of the root nodes "IsacKnowledge", "Isabelle"
    58   and "IsacScripts" is determined in KEStore.thy.
    59 *}
    60 ML {*
    61 val thydata_list = 
    62   collect_part       "IsacKnowledge" knowledge_parent knowthys' @
    63   (map (collect_isab "Isabelle") rlsthmsNOTisac) @
    64   collect_part       "IsacScripts" proglang_parent progthys'
    65 : (Celem.theID * Celem.thydata) list
    66 *}
    67 setup {* KEStore_Elems.add_thes (map (fn (a, b) => (b, a)) thydata_list) *}
    68 
    69 section {* interface for dialog-authoring *}
    70 subsection {* Add error patterns *}
    71 subsubsection {* Error patterns for differentiation *}
    72 
    73 setup {* KEStore_Elems.add_mets
    74   [update_metpair @{theory Diff} ["diff","differentiate_on_R"]
    75       [("chain-rule-diff-both",
    76         [TermC.parse_patt @{theory Diff} "d_d ?bdv (sin ?u) = cos (d_d ?bdv ?u)",
    77          TermC.parse_patt @{theory Diff} "d_d ?bdv (cos ?u) = - sin (d_d ?bdv ?u)",
    78          TermC.parse_patt @{theory Diff} "d_d ?bdv (?u ^^^ ?n) = ?n * ?u ^^^ (?n - 1)",
    79          TermC.parse_patt @{theory Diff} "d_d ?bdv (LogExp.ln ?u) = 1 / ?u",
    80          TermC.parse_patt @{theory Diff} "d_d ?bdv (LogExp.ln ?u) = 1 / d_d ?bdv ?u"],
    81         [@{thm diff_sin_chain}, @{thm diff_cos_chain}, @{thm diff_pow_chain}, @{thm diff_ln_chain},
    82           @{thm  diff_exp_chain}])]]
    83 *}
    84 
    85 setup {*
    86 KEStore_Elems.insert_fillpats
    87   [(["IsacKnowledge", "Diff", "Theorems", "diff_sin_chain"],
    88     ([("fill-d_d-arg",
    89       TermC.parse_patt @{theory Diff} "d_d ?bdv (sin ?u) = cos ?u * d_d ?bdv _", "chain-rule-diff-both"),
    90     ("fill-both-args",
    91       TermC.parse_patt @{theory Diff} "d_d ?bdv (sin _) = cos ?u * d_d ?bdv _", "chain-rule-diff-both"),
    92     ("fill-d_d",
    93       TermC.parse_patt @{theory Diff} "d_d ?bdv (sin ?u) = cos ?u * _ ?bdv ?u", "chain-rule-diff-both"),
    94     ("fill-inner-deriv",
    95       TermC.parse_patt @{theory Diff} "d_d ?bdv (sin ?u) = cos ?u * _", "chain-rule-diff-both"),
    96     ("fill-all",
    97       TermC.parse_patt @{theory Diff} "d_d ?bdv (sin ?u) = _", "chain-rule-diff-both")]: Celem.fillpat list))
    98   ]
    99 *}
   100 
   101 subsubsection {* Error patterns for calculation with rationals *}
   102 
   103 setup {* KEStore_Elems.add_mets
   104   [update_metpair @{theory Rational} ["simplification", "of_rationals"]
   105       [("addition-of-fractions",
   106         [TermC.parse_patt @{theory Rational} "(?a / ?b + ?c / ?d) = (?a + ?c)/(?b + ?d)",
   107          TermC.parse_patt @{theory Rational} "(?a / ?b + ?c / ?d) = (?a + ?c)/(?b * ?d)",
   108          TermC.parse_patt @{theory Rational} "(?a / ?b + ?c / ?d) = (?c * ?a)/(?d * ?b)",
   109          TermC.parse_patt @{theory Rational} "?a + (?b /?c)= (?a + ?b)/ ?c",
   110          TermC.parse_patt @{theory Rational} "?a + (?b /?c)= (?a * ?b)/ ?c"],
   111         [@{thm rat_add}, @{thm rat_add_assoc}, @{thm rat_add1}, @{thm rat_add1_assoc},
   112           @{thm rat_add2}, @{thm rat_add2_assoc}, @{thm rat_add3}, @{thm rat_add3_assoc}])]]
   113 *}
   114 
   115 ML {*
   116 (*TODO insert_fillpats ["IsacKnowledge", "Rational", "Theorems", "???"]*)
   117 
   118 (* TODO insert_errpats overwrites instead of appending
   119 insert_errpats ["simplification","of_rationals"]
   120  (("cancel", (*see master thesis gdarocy*)
   121     [(*a*)parse_patt @{theory Rational} "(?a + ?b)/?a = ?b",
   122      (*b*)parse_patt @{theory Rational} "(?a + ?b)/?b = ?a",
   123      (*c*)parse_patt @{theory Rational} "(?a + ?b)/(?b + ?c) = ?a / ?c",
   124      (*d*)parse_patt @{theory Rational} "(?a + ?c)/(?b + ?c) = ?a / ?b",
   125      (*e*)parse_patt @{theory Rational} "(?a + ?c)/(?b + ?c) = ?a / ?c",
   126      (*f*)parse_patt @{theory Rational} "(?a + ?b)/(?c + ?a) = ?b / ?c",
   127      (*g*)parse_patt @{theory Rational} "(?a*?b + ?c)/?a = ?b + ?c",
   128      (*h*)parse_patt @{theory Rational} "(?a*?b + ?c)/?b = ?a + ?c",
   129      (*i*)parse_patt @{theory Rational} "(?a + ?b*?c)/?b = ?a + ?c",
   130      (*j*)parse_patt @{theory Rational} "(?a + ?b*?c)/?b = ?a + ?b",
   131      (*k*)parse_patt @{theory Rational} "(?a + ?b*?e)/(?c + ?d*?e) = (?a + ?b)/(?c + ?d)"],
   132     [@{thm real_times_divide_1_eq}, (*TODO*) @{thm real_times_divide_1_eq}]));
   133 
   134 val fillpats = [
   135   ("fill-cancel-left-num",
   136     parse_patt @{theory Rational} "(?a * ?b) / (?a * ?c) = _ / ?c", "cancel"),
   137   ("fill-cancel-left-den",
   138     parse_patt @{theory Rational} "(?a * ?b) / (?a * ?c) = ?b / _", "cancel"),
   139   ("fill-cancel-none",
   140     parse_patt @{theory Rational} "(?a * ?b) / (?a * ?c) = ?b / ?c", "cancel"),
   141 
   142   ("fill-cancel-left-add-num1",
   143     parse_patt @{theory Rational} "(?a * (?b + ?c)) / (?a * ?d) = (_ + ?c) / ?d", "cancel"),
   144   ("fill-cancel-left-add-num2",
   145     parse_patt @{theory Rational} "(?a * (?b + ?c)) / (?a * ?d) = (?b + _) / ?d", "cancel"),
   146   ("fill-cancel-left-add-num3",
   147     parse_patt @{theory Rational} "(?a * (?b + ?c)) / (?a * ?d) = (_ + _) / ?d", "cancel"),
   148   ("fill-cancel-left-add-num4",
   149     parse_patt @{theory Rational} "(?a * (?b + ?c)) / (?a * ?d) = (?b + ?c) / _", "cancel"),
   150   ("fill-cancel-left-add-num5",
   151     parse_patt @{theory Rational} "(?a * (?b + ?c)) / (?a * ?d) = (_ + ?c) / _", "cancel"),
   152   ("fill-cancel-left-add-num6",
   153     parse_patt @{theory Rational} "(?a * (?b + ?c)) / (?a * ?d) = (?b + _) / _", "cancel"),
   154   ("fill-cancel-left-add-none",
   155     parse_patt @{theory Rational} "(?a * (?b + ?c)) / (?a * ?d) = (?b + ?c) / ?d", "cancel"),
   156 
   157   ("fill-cancel-left-add-den1",
   158     parse_patt @{theory Rational} "(?a * ?b) / (?a * (?c + ?d)) = ?b / (?c + ?d)", "cancel"),
   159   ("fill-cancel-left-add-den2",
   160     parse_patt @{theory Rational} "(?a * ?b) / (?a * (?c + ?d)) = ?b / (?c + ?d)", "cancel"),
   161   ("fill-cancel-left-add-den3",
   162     parse_patt @{theory Rational} "(?a * ?b) / (?a * (?c + ?d)) = ?b / (?c + ?d)", "cancel"),
   163   ("fill-cancel-left-add-den4",
   164     parse_patt @{theory Rational} "(?a * ?b) / (?a * (?c + ?d)) = ?b / (?c + ?d)", "cancel"),
   165   ("fill-cancel-left-add-den5",
   166     parse_patt @{theory Rational} "(?a * ?b) / (?a * (?c + ?d)) = ?b / (?c + ?d)", "cancel"),
   167   ("fill-cancel-left-add-den6",
   168     parse_patt @{theory Rational} "(?a * ?b) / (?a * (?c + ?d)) = ?b / (?c + ?d)", "cancel"),
   169   ("fill-cancel-left-add-none",
   170     parse_patt @{theory Rational} "(?a * ?b) / (?a * (?c + ?d)) = ?b / (?c + ?d)", "cancel")
   171  ]: fillpat list;
   172 *)
   173 
   174 (* still ununsed; planned for stepToErrorpattern: this is just a reminder... 
   175 
   176 insert_errpatIDs @{theory} ["IsacKnowledge", "Diff", "Rulesets", "norm_diff"]
   177   (["chain-rule-diff-both", "cancel"]: errpatID list);
   178 [[[ERROR *** app_py: not found: ["IsacKnowledge","Diff","Rulesets","norm_diff"]
   179   since KEStore_Elems.set_ref_thy has been shifted below;
   180   this ERROR will vanish during re-engineering towards KEStore_Elems.]]]
   181 
   182    ...and all other related rls by hand;
   183    TODO: lift errpattIDs from theorems to respective rls AUTOMATICALLY *)
   184 *}
   185 
   186 subsection {* Generate XML representation from SML (data in KEStore) *}
   187 text {*
   188   See the wiki 
   189   http://www.ist.tugraz.at/isac/Generate_representations_for_ISAC_Knowledge#Step_1:_Generate_XML_representation_from_SML
   190 
   191   val path = "/tmp/xmldata/"; 
   192   thy_hierarchy2file (path ^ "thy/");
   193   thes2file          (path ^ "thy/");
   194   pbl_hierarchy2file (path ^ "pbl/");
   195   pbls2file          (path ^ "pbl/");
   196   met_hierarchy2file (path ^ "met/");
   197   mets2file          (path ^ "met/");
   198 
   199   Adjust ''val path'' (for details see wiki) and copy line for line 
   200   into an ML-block.
   201   Scroll down the Output window and look for potential error messages.
   202 *}
   203 
   204 section {* Link KEStore_Elems to completed IsacKnowledge *}
   205 ML {* KEStore_Elems.set_ref_thy @{theory} *}
   206 
   207 end