src/Tools/isac/BaseDefinitions/Know_Store.thy
author Walther Neuper <walther.neuper@jku.at>
Sun, 19 Apr 2020 15:37:39 +0200
changeset 59888 2c2fdf9dd52d
parent 59887 4616b145b1cd
child 59889 e794e1fbe6da
permissions -rw-r--r--
run Know_Store with Celem1..91 via Celem in calcelements.sml
walther@59887
     1
(*  Title:      src/Tools/isac/Know_Store.thy
wneuper@59586
     2
    Author:     Mathias Lehnfeld
walther@59871
     3
walther@59887
     4
The files (in "xxxxx-def.sml") contain definitions required for Know_Store;
walther@59871
     5
they also include minimal code required for other "xxxxx-def.sml" files.
walther@59871
     6
These files have companion files "xxxxx.sml" with all further code,
walther@59871
     7
located at appropriate positions in the file structure.
walther@59871
     8
walther@59871
     9
The separation of "xxxxx-def.sml" from "xxxxx.sml" should be overcome by
walther@59871
    10
appropriate use of polymorphic high order functions.
wneuper@59586
    11
*)
wneuper@59586
    12
walther@59887
    13
theory Know_Store imports Complex_Main
wneuper@59586
    14
wneuper@59586
    15
begin
wneuper@59586
    16
ML_file libraryC.sml
walther@59880
    17
ML_file theoryC.sml
walther@59868
    18
ML_file unparseC.sml
walther@59874
    19
ML_file "rule-def.sml"
walther@59875
    20
ML_file "thmC-def.sml"
walther@59867
    21
ML_file "exec-def.sml"       (*rename identifiers by use of struct.id*)
walther@59867
    22
ML_file "rewrite-order.sml"  (*rename identifiers by use of struct.id*)
wneuper@59586
    23
ML_file rule.sml
walther@59867
    24
ML_file "error-fill-def.sml" (*rename identifiers by use of struct.id*)
walther@59850
    25
ML_file "rule-set.sml"
walther@59882
    26
walther@59884
    27
ML_file "celem-0.sml"  (*survey Celem.*)
walther@59884
    28
ML_file "celem-1.sml"  (*datatype 'a ptyp*)
walther@59884
    29
ML_file "celem-2.sml"  (*guh*)
walther@59884
    30
ML_file "celem-3.sml"  (*spec*)
walther@59884
    31
ML_file "celem-4.sml"  (*pat*)
walther@59884
    32
ML_file "celem-5.sml"  (*ptyps*)
walther@59884
    33
ML_file "celem-6.sml"  (*mets*)
walther@59884
    34
ML_file "celem-7.sml"  (*cas_elem*)
walther@59884
    35
ML_file "celem-8.sml"  (*thydata*)
walther@59884
    36
ML_file "celem-91.sml" (*check_guhs_unique*)
walther@59882
    37
wneuper@59586
    38
ML_file calcelems.sml
walther@59884
    39
ML_file tracing.sml
wneuper@59586
    40
ML \<open>
wneuper@59586
    41
\<close> ML \<open>
wneuper@59587
    42
\<close> ML \<open>
wneuper@59586
    43
\<close>
wneuper@59586
    44
section \<open>Knowledge elements for problems and methods\<close>
wneuper@59586
    45
ML \<open>
walther@59887
    46
(* Knowledge (and Exercises) are held by "Know_Store" in Isac's Java front-end.
wneuper@59586
    47
  In the front-end Knowledge comprises theories, problems and methods.
wneuper@59586
    48
  Elements of problems and methods are defined in theories alongside
wneuper@59586
    49
  the development of respective language elements. 
wneuper@59586
    50
  However, the structure of methods and problems is independent from theories' 
wneuper@59586
    51
  deductive structure. Thus respective structures are built in Build_Thydata.thy.
wneuper@59586
    52
wneuper@59586
    53
  Most elements of problems and methods are implemented in "Knowledge/", but some
walther@59887
    54
  of them are implemented in "ProgLang/" already; thus "Know_Store.thy" got this
wneuper@59586
    55
  location in the directory structure.
wneuper@59586
    56
wneuper@59586
    57
  get_* retrieves all * of the respective theory PLUS of all ancestor theories.
wneuper@59586
    58
*)
wneuper@59586
    59
signature KESTORE_ELEMS =
wneuper@59586
    60
sig
walther@59879
    61
  val get_rlss: theory -> (Rule_Set.id * (ThyC.id * Rule_Set.T)) list
walther@59879
    62
  val add_rlss: (Rule_Set.id * (ThyC.id * Rule_Set.T)) list -> theory -> theory
walther@59854
    63
  val get_calcs: theory -> (Exec_Def.prog_calcID * (Exec_Def.calID * Exec_Def.eval_fn)) list
walther@59854
    64
  val add_calcs: (Exec_Def.prog_calcID * (Exec_Def.calID * Exec_Def.eval_fn)) list -> theory -> theory
wneuper@59586
    65
  val get_cas: theory -> Celem.cas_elem list
wneuper@59586
    66
  val add_cas: Celem.cas_elem list -> theory -> theory
wneuper@59586
    67
  val get_ptyps: theory -> Celem.ptyps
wneuper@59586
    68
  val add_pbts: (Celem.pbt * Celem.pblID) list -> theory -> theory
wneuper@59586
    69
  val get_mets: theory -> Celem.mets
wneuper@59586
    70
  val add_mets: (Celem.met * Celem.metID) list -> theory -> theory
walther@59887
    71
  val get_thes: theory -> (Celem.thydata Celem1.ptyp) list
wneuper@59586
    72
  val add_thes: (Celem.thydata * Celem.theID) list -> theory -> theory (* thydata dropped at existing elems *)
walther@59858
    73
  val insert_fillpats: (Celem.theID * Error_Fill_Def.fillpat list) list -> theory -> theory 
wneuper@59586
    74
  val get_ref_thy: unit -> theory
wneuper@59586
    75
  val set_ref_thy: theory -> unit
wneuper@59586
    76
end;                               
wneuper@59586
    77
wneuper@59586
    78
structure KEStore_Elems: KESTORE_ELEMS =
wneuper@59586
    79
struct
wneuper@59586
    80
  fun union_overwrite eq l1 l2 = fold (insert eq) l2 (*..swapped..*) l1;
wneuper@59586
    81
wneuper@59586
    82
  structure Data = Theory_Data (
walther@59879
    83
    type T = (Rule_Set.id * (ThyC.id * Rule_Set.T)) list;
wneuper@59586
    84
    val empty = [];
wneuper@59586
    85
    val extend = I;
walther@59852
    86
    val merge = Rule_Set.to_kestore;
wneuper@59586
    87
    );  
wneuper@59586
    88
  fun get_rlss thy = Data.get thy
walther@59852
    89
  fun add_rlss rlss = Data.map (union_overwrite Rule_Set.equal rlss)
wneuper@59586
    90
wneuper@59586
    91
  structure Data = Theory_Data (
walther@59854
    92
    type T = (Exec_Def.prog_calcID * (Exec_Def.calID * Exec_Def.eval_fn)) list;
wneuper@59586
    93
    val empty = [];
wneuper@59586
    94
    val extend = I;
walther@59853
    95
    val merge = merge Exec_Def.calc_eq;
wneuper@59586
    96
    );                                                              
wneuper@59586
    97
  fun get_calcs thy = Data.get thy
walther@59853
    98
  fun add_calcs calcs = Data.map (union_overwrite Exec_Def.calc_eq calcs)
wneuper@59586
    99
wneuper@59586
   100
  structure Data = Theory_Data (
wneuper@59586
   101
    type T = (term * (Celem.spec * (term list -> (term * term list) list))) list;
wneuper@59586
   102
    val empty = [];
wneuper@59586
   103
    val extend = I;
wneuper@59586
   104
    val merge = merge Celem.cas_eq;
wneuper@59586
   105
    );                                                              
wneuper@59586
   106
  fun get_cas thy = Data.get thy
wneuper@59586
   107
  fun add_cas cas = Data.map (union_overwrite Celem.cas_eq cas)
wneuper@59586
   108
wneuper@59586
   109
  structure Data = Theory_Data (
wneuper@59586
   110
    type T = Celem.ptyps;
wneuper@59586
   111
    val empty = [Celem.e_Ptyp];
wneuper@59586
   112
    val extend = I;
walther@59888
   113
    val merge = Celem1.merge_ptyps;
wneuper@59586
   114
    );
wneuper@59586
   115
  fun get_ptyps thy = Data.get thy;
wneuper@59586
   116
  fun add_pbts pbts thy = let
wneuper@59586
   117
          fun add_pbt (pbt as {guh,...}, pblID) =
wneuper@59586
   118
                (* the pblID has the leaf-element as first; better readability achieved *)
wneuper@59586
   119
                (if (!Celem.check_guhs_unique) then Celem.check_pblguh_unique guh (Data.get thy) else ();
walther@59888
   120
                  rev pblID |> Celem1.insrt pblID pbt);
wneuper@59586
   121
        in Data.map (fold add_pbt pbts) thy end;
wneuper@59586
   122
wneuper@59586
   123
  structure Data = Theory_Data (
wneuper@59586
   124
    type T = Celem.mets;
wneuper@59586
   125
    val empty = [Celem.e_Mets];
wneuper@59586
   126
    val extend = I;
walther@59888
   127
    val merge = Celem1.merge_ptyps;
wneuper@59586
   128
    );
wneuper@59586
   129
  val get_mets = Data.get;
wneuper@59586
   130
  fun add_mets mets thy = let
wneuper@59586
   131
          fun add_met (met as {guh,...}, metID) =
wneuper@59586
   132
                (if (!Celem.check_guhs_unique) then Celem.check_metguh_unique guh (Data.get thy) else ();
walther@59888
   133
                  Celem1.insrt metID met metID);
wneuper@59586
   134
        in Data.map (fold add_met mets) thy end;
wneuper@59586
   135
wneuper@59586
   136
  structure Data = Theory_Data (
walther@59887
   137
    type T = (Celem.thydata Celem1.ptyp) list;
wneuper@59586
   138
    val empty = [];
wneuper@59586
   139
    val extend = I;
walther@59888
   140
    val merge = Celem1.merge_ptyps; (* relevant for store_thm, store_rls *)
wneuper@59586
   141
    );                                                              
wneuper@59586
   142
  fun get_thes thy = Data.get thy
wneuper@59586
   143
  fun add_thes thes thy = let
wneuper@59586
   144
    fun add_the (thydata, theID) = Celem.add_thydata ([], theID) thydata
wneuper@59586
   145
  in Data.map (fold add_the thes) thy end;
wneuper@59586
   146
  fun insert_fillpats fis thy =
wneuper@59586
   147
    let
wneuper@59586
   148
      fun update_elem (theID, fillpats) =
wneuper@59586
   149
        let
walther@59888
   150
          val hthm = Celem1.get_py (Data.get thy) theID theID
wneuper@59586
   151
          val hthm' = Celem.update_hthm hthm fillpats
wneuper@59586
   152
            handle ERROR _ =>
wneuper@59586
   153
              error ("insert_fillpats: " ^ strs2str theID ^ "must address a theorem")
walther@59888
   154
        in Celem1.update_ptyps theID theID hthm' end
wneuper@59586
   155
    in Data.map (fold update_elem fis) thy end
wneuper@59586
   156
wneuper@59586
   157
  val cur_thy = Synchronized.var "finally_knowledge_complete" @{theory};
wneuper@59586
   158
  fun set_ref_thy thy = Synchronized.change cur_thy (fn _ => thy); (* never RE-set ! *)
wneuper@59586
   159
  fun get_ref_thy () = Synchronized.value cur_thy;
wneuper@59586
   160
end;
wneuper@59586
   161
\<close>
wneuper@59586
   162
wneuper@59586
   163
section \<open>Re-use existing access functions for knowledge elements\<close>
wneuper@59586
   164
text \<open>
wneuper@59586
   165
  The independence of problems' and methods' structure enforces the accesse
wneuper@59592
   166
  functions to use "Isac_Knowledge", the final theory which comprises all knowledge defined.
wneuper@59586
   167
\<close>
wneuper@59586
   168
ML \<open>
wneuper@59586
   169
val get_ref_thy = KEStore_Elems.get_ref_thy;
wneuper@59586
   170
walther@59867
   171
fun assoc_rls (rls' : Rule_Set.id) =
walther@59879
   172
  case AList.lookup (op =) (KEStore_Elems.get_rlss (ThyC.get_theory "Isac_Knowledge")) rls' of
wneuper@59586
   173
    SOME (_, rls) => rls
walther@59887
   174
  | NONE => raise ERROR ("rls \""^ rls' ^ "\" missing in Know_Store.\n" ^
wneuper@59586
   175
    "TODO exception hierarchy needs to be established.")
wneuper@59586
   176
walther@59867
   177
fun assoc_rls' thy (rls' : Rule_Set.id) =
wneuper@59586
   178
  case AList.lookup (op =) (KEStore_Elems.get_rlss thy) rls' of
wneuper@59586
   179
    SOME (_, rls) => rls
walther@59887
   180
  | NONE => raise ERROR ("rls \""^ rls' ^ "\" missing in Know_Store.\n" ^
wneuper@59586
   181
    "TODO exception hierarchy needs to be established.")
wneuper@59586
   182
wneuper@59586
   183
fun assoc_calc thy calID = let
wneuper@59586
   184
    fun ass ([], key) =
wneuper@59586
   185
          error ("assoc_calc: '" ^ key ^ "' not found in theory " ^ (Context.theory_name thy))
wneuper@59586
   186
      | ass ((calc, (keyi, _)) :: pairs, key) =
wneuper@59586
   187
          if key = keyi then calc else ass (pairs, key);
wneuper@59586
   188
  in ass (thy |> KEStore_Elems.get_calcs, calID) end;
wneuper@59586
   189
wneuper@59586
   190
fun assoc_calc' thy key = let
wneuper@59586
   191
    fun ass ([], key') =
wneuper@59586
   192
          error ("assoc_calc': '" ^ key' ^ "' not found in theory " ^ (Context.theory_name thy))
wneuper@59586
   193
      | ass ((all as (keyi, _)) :: pairs, key') =
wneuper@59586
   194
          if key' = keyi then all else ass (pairs, key');
wneuper@59586
   195
  in ass (KEStore_Elems.get_calcs thy, key) end;
wneuper@59586
   196
wneuper@59586
   197
fun assoc_cas thy key = assoc (KEStore_Elems.get_cas thy, key);
wneuper@59586
   198
wneuper@59586
   199
fun get_ptyps () = get_ref_thy () |> KEStore_Elems.get_ptyps;
wneuper@59586
   200
fun get_mets () = get_ref_thy () |> KEStore_Elems.get_mets;
wneuper@59586
   201
fun get_thes () = get_ref_thy () |> KEStore_Elems.get_thes;
wneuper@59586
   202
\<close>
wneuper@59586
   203
setup \<open>KEStore_Elems.add_rlss 
walther@59852
   204
  [("empty", (Context.theory_name @{theory}, Rule_Set.empty)), 
walther@59850
   205
  ("e_rrls", (Context.theory_name @{theory}, Rule_Set.e_rrls))]\<close>
wneuper@59586
   206
wneuper@59586
   207
section \<open>determine sequence of main parts in thehier\<close>
wneuper@59586
   208
setup \<open>
wneuper@59586
   209
KEStore_Elems.add_thes
wneuper@59586
   210
  [(Celem.Html {guh = Celem.part2guh ["IsacKnowledge"], html = "",
wneuper@59586
   211
    mathauthors = ["Isac team"], coursedesign = []}, ["IsacKnowledge"]),
wneuper@59586
   212
  (Celem.Html {guh = Celem.part2guh ["Isabelle"], html = "",
wneuper@59586
   213
    mathauthors = ["Isabelle team, TU Munich"], coursedesign = []}, ["Isabelle"]),
wneuper@59586
   214
  (Celem.Html {guh = Celem.part2guh ["IsacScripts"], html = "",
wneuper@59586
   215
    mathauthors = ["Isac team"], coursedesign = []}, ["IsacScripts"])]
wneuper@59586
   216
\<close>
wneuper@59586
   217
wneuper@59586
   218
section \<open>Functions for checking KEStore_Elems\<close>
wneuper@59586
   219
ML \<open>
walther@59851
   220
fun short_string_of_rls Rule_Set.Empty = "Erls"
walther@59851
   221
  | short_string_of_rls (Rule_Def.Repeat {calc, rules, ...}) =
wneuper@59586
   222
    "Rls {#calc = " ^ string_of_int (length calc) ^
wneuper@59586
   223
    ", #rules = " ^ string_of_int (length rules) ^ ", ..."
walther@59878
   224
  | short_string_of_rls (Rule_Set.Sequence {calc, rules, ...}) =
wneuper@59586
   225
    "Seq {#calc = " ^ string_of_int (length calc) ^
wneuper@59586
   226
    ", #rules = " ^ string_of_int (length rules) ^ ", ..."
walther@59850
   227
  | short_string_of_rls (Rule_Set.Rrls _) = "Rrls {...}";
wneuper@59586
   228
fun check_kestore_rls (rls', (thyID, rls)) =
wneuper@59586
   229
  "(" ^ rls' ^ ", (" ^ thyID ^ ", " ^ short_string_of_rls rls ^ "))";
wneuper@59586
   230
walther@59852
   231
fun check_kestore_calc ((id, (c, _)) : Rule_Def.calc)  = "(" ^ id ^ ", (" ^ c ^ ", fn))";
wneuper@59586
   232
walther@59846
   233
(* we avoid term_to_string''' defined later *)
wneuper@59586
   234
fun check_kestore_cas ((t, (s, _)) : Celem.cas_elem) =
walther@59846
   235
  "(" ^ (Print_Mode.setmp [] (Syntax.string_of_term (Config.put show_markup false
walther@59846
   236
  (Proof_Context.init_global @{theory})))) t ^ ", " ^ Celem.spec2str s ^ ")";
wneuper@59586
   237
wneuper@59586
   238
fun count_kestore_ptyps [] = 0
walther@59887
   239
  | count_kestore_ptyps ((Celem1.Ptyp (_, _, ps)) :: ps') =
wneuper@59586
   240
      1 + count_kestore_ptyps ps  + count_kestore_ptyps ps';
walther@59887
   241
fun check_kestore_ptyp' strfun (Celem1.Ptyp (key, pbts, pts)) = "Ptyp (" ^ (quote key) ^ ", " ^
walther@59888
   242
      (strfun pbts) ^ ", " ^ (map (check_kestore_ptyp' strfun) pts |> list2str) ^ ")" |> linefeed;
wneuper@59586
   243
val check_kestore_ptyp = check_kestore_ptyp' Celem.pbts2str;
walther@59887
   244
fun ptyp_ord ((Celem1.Ptyp (s1, _, _)), (Celem1.Ptyp (s2, _, _))) = string_ord (s1, s2);
wneuper@59586
   245
fun pbt_ord ({guh = guh'1, ...} : Celem.pbt, {guh = guh'2, ...} : Celem.pbt) = string_ord (guh'1, guh'2);
wneuper@59586
   246
fun sort_kestore_ptyp' _ [] = []
walther@59887
   247
  | sort_kestore_ptyp' ordfun ((Celem1.Ptyp (key, pbts, ps)) :: ps') =
walther@59887
   248
     ((Celem1.Ptyp (key, sort ordfun pbts, sort_kestore_ptyp' ordfun ps |> sort ptyp_ord))
wneuper@59586
   249
       :: sort_kestore_ptyp' ordfun ps');
wneuper@59586
   250
val sort_kestore_ptyp = sort_kestore_ptyp' pbt_ord;
wneuper@59586
   251
wneuper@59586
   252
fun metguh2str ({guh,...} : Celem.met) = guh : string;
walther@59887
   253
fun check_kestore_met (mp: Celem.met Celem1.ptyp) =
wneuper@59586
   254
      check_kestore_ptyp' (fn xs => map metguh2str xs |> strs2str) mp;
wneuper@59586
   255
fun met_ord ({guh = guh'1, ...} : Celem.met, {guh = guh'2, ...} : Celem.met) = string_ord (guh'1, guh'2);
wneuper@59586
   256
val sort_kestore_met = sort_kestore_ptyp' met_ord;
wneuper@59586
   257
wneuper@59586
   258
fun check_kestore_thes thes = ((map writeln) o (map (check_kestore_ptyp' Celem.thes2str))) thes
wneuper@59586
   259
fun write_thes thydata_list =
wneuper@59586
   260
  thydata_list 
walther@59888
   261
    |> map (fn (id, the) => (Celem8.theID2str id, Celem8.the2str the))
wneuper@59586
   262
    |> map pair2str
wneuper@59586
   263
    |> map writeln
wneuper@59586
   264
\<close>
wneuper@59586
   265
ML \<open>
wneuper@59586
   266
\<close> ML \<open>
wneuper@59586
   267
\<close> ML \<open>
wneuper@59586
   268
\<close>
wneuper@59586
   269
end