src/Tools/isac/BaseDefinitions/Know_Store.thy
author wenzelm
Thu, 10 Jun 2021 11:54:20 +0200
changeset 60289 a7b88fc19a92
parent 60286 31efa1b39a20
child 60313 8d89a214aedc
permissions -rw-r--r--
clarified command name: this is to register already defined rule sets in the Knowledge Store;
walther@59887
     1
(*  Title:      src/Tools/isac/Know_Store.thy
wneuper@59586
     2
    Author:     Mathias Lehnfeld
walther@59871
     3
walther@60154
     4
Calc work on Problem employing MethodC; both are collected in a respective Store;
walther@59890
     5
The collections' structure is independent from the dependency graph of Isabelle's theories
walther@59890
     6
in Knowledge.
walther@59917
     7
Store is also used by Thy_Write, required for Isac's Java front end, irrelevant for PIDE.
walther@59890
     8
walther@59887
     9
The files (in "xxxxx-def.sml") contain definitions required for Know_Store;
walther@59871
    10
they also include minimal code required for other "xxxxx-def.sml" files.
walther@59871
    11
These files have companion files "xxxxx.sml" with all further code,
walther@59871
    12
located at appropriate positions in the file structure.
walther@59871
    13
walther@59871
    14
The separation of "xxxxx-def.sml" from "xxxxx.sml" should be overcome by
walther@59871
    15
appropriate use of polymorphic high order functions.
wneuper@59586
    16
*)
wneuper@59586
    17
wenzelm@60286
    18
theory Know_Store
wenzelm@60286
    19
  imports Complex_Main
wenzelm@60289
    20
  keywords "rule_set_knowledge" :: thy_decl
wenzelm@60223
    21
begin
wneuper@59586
    22
wenzelm@60223
    23
setup \<open>
wenzelm@60223
    24
  ML_Antiquotation.conditional \<^binding>\<open>isac_test\<close>
wenzelm@60223
    25
    (fn _ => Options.default_bool \<^system_option>\<open>isac_test\<close>)
wenzelm@60223
    26
\<close>
wenzelm@60223
    27
wneuper@59586
    28
ML_file libraryC.sml
walther@59880
    29
ML_file theoryC.sml
walther@59868
    30
ML_file unparseC.sml
walther@59874
    31
ML_file "rule-def.sml"
walther@59875
    32
ML_file "thmC-def.sml"
walther@59919
    33
ML_file "eval-def.sml"       (*rename identifiers by use of struct.id*)
walther@59867
    34
ML_file "rewrite-order.sml"  (*rename identifiers by use of struct.id*)
wneuper@59586
    35
ML_file rule.sml
walther@59909
    36
ML_file "error-pattern-def.sml"
walther@59850
    37
ML_file "rule-set.sml"
walther@59882
    38
walther@59891
    39
ML_file "store.sml"
walther@59892
    40
ML_file "check-unique.sml"
walther@59985
    41
ML_file "references-def.sml"
walther@59945
    42
ML_file "model-pattern.sml"
walther@59893
    43
ML_file "problem-def.sml"
walther@59893
    44
ML_file "method-def.sml"
walther@59893
    45
ML_file "cas-def.sml"
walther@59917
    46
ML_file "thy-write.sml"
walther@59882
    47
wneuper@59586
    48
ML \<open>
wneuper@59586
    49
\<close> ML \<open>
wneuper@59587
    50
\<close> ML \<open>
wneuper@59586
    51
\<close>
wneuper@59586
    52
section \<open>Knowledge elements for problems and methods\<close>
wneuper@59586
    53
ML \<open>
walther@59887
    54
(* Knowledge (and Exercises) are held by "Know_Store" in Isac's Java front-end.
wneuper@59586
    55
  In the front-end Knowledge comprises theories, problems and methods.
wneuper@59586
    56
  Elements of problems and methods are defined in theories alongside
wneuper@59586
    57
  the development of respective language elements. 
wneuper@59586
    58
  However, the structure of methods and problems is independent from theories' 
wneuper@59586
    59
  deductive structure. Thus respective structures are built in Build_Thydata.thy.
wneuper@59586
    60
wneuper@59586
    61
  Most elements of problems and methods are implemented in "Knowledge/", but some
walther@59887
    62
  of them are implemented in "ProgLang/" already; thus "Know_Store.thy" got this
wneuper@59586
    63
  location in the directory structure.
wneuper@59586
    64
wneuper@59586
    65
  get_* retrieves all * of the respective theory PLUS of all ancestor theories.
wneuper@59586
    66
*)
wneuper@59586
    67
signature KESTORE_ELEMS =
wneuper@59586
    68
sig
walther@59879
    69
  val get_rlss: theory -> (Rule_Set.id * (ThyC.id * Rule_Set.T)) list
walther@59879
    70
  val add_rlss: (Rule_Set.id * (ThyC.id * Rule_Set.T)) list -> theory -> theory
walther@59919
    71
  val get_calcs: theory -> (Eval_Def.prog_calcID * (Eval_Def.calID * Eval_Def.eval_fn)) list
walther@59919
    72
  val add_calcs: (Eval_Def.prog_calcID * (Eval_Def.calID * Eval_Def.eval_fn)) list -> theory -> theory
walther@59896
    73
  val get_cas: theory -> CAS_Def.T list
walther@59896
    74
  val add_cas: CAS_Def.T list -> theory -> theory
walther@59894
    75
  val get_ptyps: theory -> Probl_Def.store
walther@59985
    76
  val add_pbts: (Probl_Def.T * References_Def.id) list -> theory -> theory
walther@59895
    77
  val get_mets: theory -> Meth_Def.store
walther@59985
    78
  val add_mets: (Meth_Def.T * References_Def.id) list -> theory -> theory
walther@59917
    79
  val get_thes: theory -> (Thy_Write.thydata Store.node) list
walther@59917
    80
  val add_thes: (Thy_Write.thydata * Thy_Write.theID) list -> theory -> theory (* thydata dropped at existing elems *)
walther@59917
    81
  val insert_fillpats: (Thy_Write.theID * Error_Pattern_Def.fill_in list) list -> theory -> theory 
wneuper@59586
    82
  val get_ref_thy: unit -> theory
wneuper@59586
    83
  val set_ref_thy: theory -> unit
wneuper@59586
    84
end;                               
wneuper@59586
    85
wneuper@59586
    86
structure KEStore_Elems: KESTORE_ELEMS =
wneuper@59586
    87
struct
wneuper@59586
    88
  fun union_overwrite eq l1 l2 = fold (insert eq) l2 (*..swapped..*) l1;
wneuper@59586
    89
wneuper@59586
    90
  structure Data = Theory_Data (
walther@59879
    91
    type T = (Rule_Set.id * (ThyC.id * Rule_Set.T)) list;
wneuper@59586
    92
    val empty = [];
wneuper@59586
    93
    val extend = I;
walther@59852
    94
    val merge = Rule_Set.to_kestore;
wneuper@59586
    95
    );  
wneuper@59586
    96
  fun get_rlss thy = Data.get thy
walther@59852
    97
  fun add_rlss rlss = Data.map (union_overwrite Rule_Set.equal rlss)
wneuper@59586
    98
wneuper@59586
    99
  structure Data = Theory_Data (
walther@59919
   100
    type T = (Eval_Def.prog_calcID * (Eval_Def.calID * Eval_Def.eval_fn)) list;
wneuper@59586
   101
    val empty = [];
wneuper@59586
   102
    val extend = I;
walther@59919
   103
    val merge = merge Eval_Def.calc_eq;
wneuper@59586
   104
    );                                                              
wneuper@59586
   105
  fun get_calcs thy = Data.get thy
walther@59919
   106
  fun add_calcs calcs = Data.map (union_overwrite Eval_Def.calc_eq calcs)
wneuper@59586
   107
wneuper@59586
   108
  structure Data = Theory_Data (
walther@59985
   109
    type T = (term * (References_Def.T * (term list -> (term * term list) list))) list;
wneuper@59586
   110
    val empty = [];
wneuper@59586
   111
    val extend = I;
walther@59896
   112
    val merge = merge CAS_Def.equal;
wneuper@59586
   113
    );                                                              
wneuper@59586
   114
  fun get_cas thy = Data.get thy
walther@59896
   115
  fun add_cas cas = Data.map (union_overwrite CAS_Def.equal cas)
wneuper@59586
   116
wneuper@59586
   117
  structure Data = Theory_Data (
walther@59894
   118
    type T = Probl_Def.store;
walther@59894
   119
    val empty = [Probl_Def.empty_store];
wneuper@59586
   120
    val extend = I;
walther@59897
   121
    val merge = Store.merge;
wneuper@59586
   122
    );
wneuper@59586
   123
  fun get_ptyps thy = Data.get thy;
wneuper@59586
   124
  fun add_pbts pbts thy = let
wneuper@59586
   125
          fun add_pbt (pbt as {guh,...}, pblID) =
wneuper@59586
   126
                (* the pblID has the leaf-element as first; better readability achieved *)
walther@59904
   127
                (if (!Check_Unique.on) then Probl_Def.check_unique guh (Data.get thy) else ();
walther@59897
   128
                  rev pblID |> Store.insert pblID pbt);
wneuper@59586
   129
        in Data.map (fold add_pbt pbts) thy end;
wneuper@59586
   130
wneuper@59586
   131
  structure Data = Theory_Data (
walther@59895
   132
    type T = Meth_Def.store;
walther@59895
   133
    val empty = [Meth_Def.empty_store];
wneuper@59586
   134
    val extend = I;
walther@59897
   135
    val merge = Store.merge;
wneuper@59586
   136
    );
wneuper@59586
   137
  val get_mets = Data.get;
wneuper@59586
   138
  fun add_mets mets thy = let
wneuper@59586
   139
          fun add_met (met as {guh,...}, metID) =
walther@59904
   140
                (if (!Check_Unique.on) then Meth_Def.check_unique guh (Data.get thy) else ();
walther@59897
   141
                  Store.insert metID met metID);
wneuper@59586
   142
        in Data.map (fold add_met mets) thy end;
wneuper@59586
   143
wneuper@59586
   144
  structure Data = Theory_Data (
walther@59917
   145
    type T = (Thy_Write.thydata Store.node) list;
wneuper@59586
   146
    val empty = [];
wneuper@59586
   147
    val extend = I;
walther@59897
   148
    val merge = Store.merge; (* relevant for store_thm, store_rls *)
wneuper@59586
   149
    );                                                              
wneuper@59586
   150
  fun get_thes thy = Data.get thy
wneuper@59586
   151
  fun add_thes thes thy = let
walther@59917
   152
    fun add_the (thydata, theID) = Thy_Write.add_thydata ([], theID) thydata
wneuper@59586
   153
  in Data.map (fold add_the thes) thy end;
wneuper@59586
   154
  fun insert_fillpats fis thy =
wneuper@59586
   155
    let
wneuper@59586
   156
      fun update_elem (theID, fillpats) =
wneuper@59586
   157
        let
walther@59897
   158
          val hthm = Store.get (Data.get thy) theID theID
walther@59917
   159
          val hthm' = Thy_Write.update_hthm hthm fillpats
wneuper@59586
   160
            handle ERROR _ =>
walther@59962
   161
              raise ERROR ("insert_fillpats: " ^ strs2str theID ^ "must address a theorem")
walther@59897
   162
        in Store.update theID theID hthm' end
wneuper@59586
   163
    in Data.map (fold update_elem fis) thy end
wneuper@59586
   164
wneuper@59586
   165
  val cur_thy = Synchronized.var "finally_knowledge_complete" @{theory};
wneuper@59586
   166
  fun set_ref_thy thy = Synchronized.change cur_thy (fn _ => thy); (* never RE-set ! *)
wneuper@59586
   167
  fun get_ref_thy () = Synchronized.value cur_thy;
wneuper@59586
   168
end;
wneuper@59586
   169
\<close>
wneuper@59586
   170
wenzelm@60286
   171
wenzelm@60286
   172
subsection \<open>Isar command syntax\<close>
wenzelm@60286
   173
wenzelm@60286
   174
ML \<open>
wenzelm@60286
   175
local
wenzelm@60286
   176
wenzelm@60286
   177
val parse_rule = Parse.name -- Parse.!!! (\<^keyword>\<open>=\<close> |-- Parse.ML_source);
wenzelm@60286
   178
wenzelm@60286
   179
val ml = ML_Lex.read;
wenzelm@60286
   180
wenzelm@60286
   181
fun ml_rule thy (name, source) =
wenzelm@60286
   182
  ml "(" @ ml (ML_Syntax.print_string name) @ ml ", " @
wenzelm@60286
   183
  ml "(" @ ml (ML_Syntax.print_string (Context.theory_name thy)) @ ml ", " @
wenzelm@60286
   184
  ML_Lex.read_source source @ ml "))";
wenzelm@60286
   185
wenzelm@60286
   186
fun ml_rules thy args =
wenzelm@60286
   187
  ml "Theory.setup (KEStore_Elems.add_rlss [" @
wenzelm@60286
   188
    flat (separate (ml ",") (map (ml_rule thy) args)) @ ml "])";
wenzelm@60286
   189
wenzelm@60286
   190
val _ =
wenzelm@60289
   191
  Outer_Syntax.command \<^command_keyword>\<open>rule_set_knowledge\<close> "register ISAC rule set to Knowledge Store"
wenzelm@60286
   192
    (Parse.and_list1 parse_rule >> (fn args => Toplevel.theory (fn thy =>
wenzelm@60286
   193
      thy |> Context.theory_map
wenzelm@60286
   194
        (ML_Context.expression (Position.thread_data ()) (ml_rules thy args)))));
wenzelm@60286
   195
wenzelm@60286
   196
in end;
wenzelm@60286
   197
\<close>
wenzelm@60286
   198
wenzelm@60286
   199
wneuper@59586
   200
section \<open>Re-use existing access functions for knowledge elements\<close>
wneuper@59586
   201
text \<open>
wneuper@59586
   202
  The independence of problems' and methods' structure enforces the accesse
wneuper@59592
   203
  functions to use "Isac_Knowledge", the final theory which comprises all knowledge defined.
wneuper@59586
   204
\<close>
wneuper@59586
   205
ML \<open>
wneuper@59586
   206
val get_ref_thy = KEStore_Elems.get_ref_thy;
wneuper@59586
   207
walther@59867
   208
fun assoc_rls (rls' : Rule_Set.id) =
walther@59879
   209
  case AList.lookup (op =) (KEStore_Elems.get_rlss (ThyC.get_theory "Isac_Knowledge")) rls' of
wneuper@59586
   210
    SOME (_, rls) => rls
walther@59887
   211
  | NONE => raise ERROR ("rls \""^ rls' ^ "\" missing in Know_Store.\n" ^
wneuper@59586
   212
    "TODO exception hierarchy needs to be established.")
wneuper@59586
   213
walther@59867
   214
fun assoc_rls' thy (rls' : Rule_Set.id) =
wneuper@59586
   215
  case AList.lookup (op =) (KEStore_Elems.get_rlss thy) rls' of
wneuper@59586
   216
    SOME (_, rls) => rls
walther@59887
   217
  | NONE => raise ERROR ("rls \""^ rls' ^ "\" missing in Know_Store.\n" ^
wneuper@59586
   218
    "TODO exception hierarchy needs to be established.")
wneuper@59586
   219
wneuper@59586
   220
fun assoc_calc thy calID = let
wneuper@59586
   221
    fun ass ([], key) =
walther@59962
   222
          raise ERROR ("assoc_calc: '" ^ key ^ "' not found in theory " ^ (Context.theory_name thy))
wneuper@59586
   223
      | ass ((calc, (keyi, _)) :: pairs, key) =
wneuper@59586
   224
          if key = keyi then calc else ass (pairs, key);
wneuper@59586
   225
  in ass (thy |> KEStore_Elems.get_calcs, calID) end;
wneuper@59586
   226
wneuper@59586
   227
fun assoc_calc' thy key = let
wneuper@59586
   228
    fun ass ([], key') =
walther@59962
   229
          raise ERROR ("assoc_calc': '" ^ key' ^ "' not found in theory " ^ (Context.theory_name thy))
wneuper@59586
   230
      | ass ((all as (keyi, _)) :: pairs, key') =
wneuper@59586
   231
          if key' = keyi then all else ass (pairs, key');
wneuper@59586
   232
  in ass (KEStore_Elems.get_calcs thy, key) end;
wneuper@59586
   233
wneuper@59586
   234
fun assoc_cas thy key = assoc (KEStore_Elems.get_cas thy, key);
wneuper@59586
   235
wneuper@59586
   236
fun get_ptyps () = get_ref_thy () |> KEStore_Elems.get_ptyps;
wneuper@59586
   237
fun get_mets () = get_ref_thy () |> KEStore_Elems.get_mets;
wneuper@59586
   238
fun get_thes () = get_ref_thy () |> KEStore_Elems.get_thes;
wneuper@59586
   239
\<close>
wenzelm@60286
   240
wenzelm@60289
   241
rule_set_knowledge
wenzelm@60286
   242
  empty = \<open>Rule_Set.empty\<close> and
wenzelm@60286
   243
  e_rrls = \<open>Rule_Set.e_rrls\<close>
wneuper@59586
   244
wneuper@59586
   245
section \<open>determine sequence of main parts in thehier\<close>
wneuper@59586
   246
setup \<open>
wneuper@59586
   247
KEStore_Elems.add_thes
walther@59917
   248
  [(Thy_Write.Html {guh = Thy_Write.part2guh ["IsacKnowledge"], html = "",
wneuper@59586
   249
    mathauthors = ["Isac team"], coursedesign = []}, ["IsacKnowledge"]),
walther@59917
   250
  (Thy_Write.Html {guh = Thy_Write.part2guh ["Isabelle"], html = "",
wneuper@59586
   251
    mathauthors = ["Isabelle team, TU Munich"], coursedesign = []}, ["Isabelle"]),
walther@59917
   252
  (Thy_Write.Html {guh = Thy_Write.part2guh ["IsacScripts"], html = "",
wneuper@59586
   253
    mathauthors = ["Isac team"], coursedesign = []}, ["IsacScripts"])]
wneuper@59586
   254
\<close>
wneuper@59586
   255
wneuper@59586
   256
section \<open>Functions for checking KEStore_Elems\<close>
wneuper@59586
   257
ML \<open>
walther@59851
   258
fun short_string_of_rls Rule_Set.Empty = "Erls"
walther@59851
   259
  | short_string_of_rls (Rule_Def.Repeat {calc, rules, ...}) =
wneuper@59586
   260
    "Rls {#calc = " ^ string_of_int (length calc) ^
wneuper@59586
   261
    ", #rules = " ^ string_of_int (length rules) ^ ", ..."
walther@59878
   262
  | short_string_of_rls (Rule_Set.Sequence {calc, rules, ...}) =
wneuper@59586
   263
    "Seq {#calc = " ^ string_of_int (length calc) ^
wneuper@59586
   264
    ", #rules = " ^ string_of_int (length rules) ^ ", ..."
walther@59850
   265
  | short_string_of_rls (Rule_Set.Rrls _) = "Rrls {...}";
wneuper@59586
   266
fun check_kestore_rls (rls', (thyID, rls)) =
wneuper@59586
   267
  "(" ^ rls' ^ ", (" ^ thyID ^ ", " ^ short_string_of_rls rls ^ "))";
wneuper@59586
   268
walther@59852
   269
fun check_kestore_calc ((id, (c, _)) : Rule_Def.calc)  = "(" ^ id ^ ", (" ^ c ^ ", fn))";
wneuper@59586
   270
walther@59846
   271
(* we avoid term_to_string''' defined later *)
walther@59896
   272
fun check_kestore_cas ((t, (s, _)) : CAS_Def.T) =
walther@59846
   273
  "(" ^ (Print_Mode.setmp [] (Syntax.string_of_term (Config.put show_markup false
walther@59985
   274
  (Proof_Context.init_global @{theory})))) t ^ ", " ^ References_Def.to_string s ^ ")";
wneuper@59586
   275
wneuper@59586
   276
fun count_kestore_ptyps [] = 0
walther@59897
   277
  | count_kestore_ptyps ((Store.Node (_, _, ps)) :: ps') =
wneuper@59586
   278
      1 + count_kestore_ptyps ps  + count_kestore_ptyps ps';
walther@59897
   279
fun check_kestore_ptyp' strfun (Store.Node (key, pbts, pts)) = "Ptyp (" ^ (quote key) ^ ", " ^
walther@59888
   280
      (strfun pbts) ^ ", " ^ (map (check_kestore_ptyp' strfun) pts |> list2str) ^ ")" |> linefeed;
walther@59894
   281
val check_kestore_ptyp = check_kestore_ptyp' Probl_Def.s_to_string;
walther@59897
   282
fun ptyp_ord ((Store.Node (s1, _, _)), (Store.Node (s2, _, _))) = string_ord (s1, s2);
walther@59894
   283
fun pbt_ord ({guh = guh'1, ...} : Probl_Def.T, {guh = guh'2, ...} : Probl_Def.T) = string_ord (guh'1, guh'2);
wneuper@59586
   284
fun sort_kestore_ptyp' _ [] = []
walther@59897
   285
  | sort_kestore_ptyp' ordfun ((Store.Node (key, pbts, ps)) :: ps') =
walther@59897
   286
     ((Store.Node (key, sort ordfun pbts, sort_kestore_ptyp' ordfun ps |> sort ptyp_ord))
wneuper@59586
   287
       :: sort_kestore_ptyp' ordfun ps');
wneuper@59586
   288
val sort_kestore_ptyp = sort_kestore_ptyp' pbt_ord;
wneuper@59586
   289
walther@59895
   290
fun metguh2str ({guh,...} : Meth_Def.T) = guh : string;
walther@59897
   291
fun check_kestore_met (mp: Meth_Def.T Store.node) =
wneuper@59586
   292
      check_kestore_ptyp' (fn xs => map metguh2str xs |> strs2str) mp;
walther@59895
   293
fun met_ord ({guh = guh'1, ...} : Meth_Def.T, {guh = guh'2, ...} : Meth_Def.T) = string_ord (guh'1, guh'2);
wneuper@59586
   294
val sort_kestore_met = sort_kestore_ptyp' met_ord;
wneuper@59586
   295
walther@59917
   296
fun check_kestore_thes thes = ((map writeln) o (map (check_kestore_ptyp' Thy_Write.thes2str))) thes
wneuper@59586
   297
fun write_thes thydata_list =
wneuper@59586
   298
  thydata_list 
walther@59917
   299
    |> map (fn (id, the) => (Thy_Write.theID2str id, Thy_Write.the2str the))
wneuper@59586
   300
    |> map pair2str
wneuper@59586
   301
    |> map writeln
wneuper@59586
   302
\<close>
wneuper@59586
   303
ML \<open>
wneuper@59586
   304
\<close> ML \<open>
wneuper@59586
   305
\<close> ML \<open>
wneuper@59586
   306
\<close>
wneuper@59586
   307
end