src/Tools/isac/KEStore.thy
author Mathias Lehnfeld <s1210629013@students.fh-hagenberg.at>
Mon, 27 Jan 2014 21:49:27 +0100
changeset 55359 73dc85c025ab
parent 55357 f61fe82cd522
child 55360 639f20e506dc
permissions -rw-r--r--
cleanup, naming: 'KEStore_Elems' in Tests now 'Test_KEStore_Elems', 'store_pbts' now 'add_pbts'
neuper@52123
     1
theory KEStore 
neuper@52123
     2
imports Complex_Main
neuper@52123
     3
begin
neuper@52123
     4
  ML_file "~~/src/Tools/isac/library.sml"
neuper@52123
     5
  ML_file "~~/src/Tools/isac/calcelems.sml"
neuper@52123
     6
neuper@52123
     7
section {* Knowledge elements for problems and methods *}
neuper@52123
     8
ML {*
neuper@52139
     9
(* Knowledge (and Exercises) are held by "KEStore" in Isac's Java front-end.
neuper@52123
    10
  In the front-end Knowledge comprises theories, problems and methods.
neuper@52123
    11
  Elements of problems and methods are defined in theories alongside
neuper@52123
    12
  the development of respective language elements. 
neuper@52123
    13
  However, the structure of methods and problems is independent from theories' 
neuper@52123
    14
  deductive structure. Thus respective structures are built in Build_Thydata.thy.
neuper@52139
    15
neuper@52139
    16
  Most elements of problems and methods are implemented in "Knowledge/", but some
neuper@52139
    17
  of them are implemented in "ProgLang/" already; thus "KEStore.thy" got this
neuper@52139
    18
  location in the directory structure.
neuper@52123
    19
*)
neuper@52123
    20
signature KESTORE_ELEMS =
neuper@52123
    21
sig
neuper@52123
    22
  val get_rlss: theory -> (rls' * (theory' * rls)) list
neuper@52123
    23
  val add_rlss: (rls' * (theory' * rls)) list -> theory -> theory
neuper@52123
    24
  val get_calcs: theory -> (prog_calcID * (calID * eval_fn)) list
neuper@52123
    25
  val add_calcs: (prog_calcID * (calID * eval_fn)) list -> theory -> theory
neuper@52169
    26
  val get_cas: theory -> cas_elem list
neuper@52169
    27
  val add_cas: cas_elem list -> theory -> theory
s1210629013@55339
    28
  val get_ptyps: theory -> ptyps
s1210629013@55359
    29
  val add_pbts: (pbt * pblID) list -> theory -> theory
neuper@52123
    30
  (*etc*)
neuper@52123
    31
end;                               
neuper@52123
    32
neuper@52123
    33
structure KEStore_Elems: KESTORE_ELEMS =
neuper@52123
    34
struct
neuper@52139
    35
  fun union_overwrite eq l1 l2 = fold (insert eq) l2 (*..swapped..*) l1;
neuper@52123
    36
neuper@52123
    37
  structure Data = Theory_Data (
neuper@52123
    38
    type T = (rls' * (theory' * rls)) list;
neuper@52123
    39
    val empty = [];
neuper@52123
    40
    val extend = I;
neuper@52141
    41
    val merge = merge_rlss;
neuper@52123
    42
    );  
neuper@52123
    43
  fun get_rlss thy = Data.get thy
neuper@52139
    44
  fun add_rlss rlss = Data.map (union_overwrite rls_eq rlss)
neuper@52123
    45
neuper@52123
    46
  structure Data = Theory_Data (
neuper@52123
    47
    type T = (prog_calcID * (calID * eval_fn)) list;
neuper@52123
    48
    val empty = [];
neuper@52123
    49
    val extend = I;
neuper@52123
    50
    val merge = merge calc_eq;
neuper@52123
    51
    );                                                              
neuper@52123
    52
  fun get_calcs thy = Data.get thy
neuper@52139
    53
  fun add_calcs calcs = Data.map (union_overwrite calc_eq calcs)
neuper@52123
    54
neuper@52169
    55
  structure Data = Theory_Data (
neuper@52169
    56
    type T = (term * (spec * (term list -> (term * term list) list))) list;
neuper@52169
    57
    val empty = [];
neuper@52169
    58
    val extend = I;
neuper@52169
    59
    val merge = merge cas_eq;
neuper@52169
    60
    );                                                              
neuper@52169
    61
  fun get_cas thy = Data.get thy
neuper@52169
    62
  fun add_cas cas = Data.map (union_overwrite cas_eq cas)
neuper@52169
    63
s1210629013@55339
    64
  structure Data = Theory_Data (
s1210629013@55339
    65
    type T = ptyps;
s1210629013@55339
    66
    val empty = [e_Ptyp];
s1210629013@55339
    67
    val extend = I;
s1210629013@55339
    68
    val merge = merge_ptyps;
s1210629013@55339
    69
    );
s1210629013@55339
    70
  fun get_ptyps thy = Data.get thy;
s1210629013@55359
    71
  fun add_pbts pbts thy = let
s1210629013@55359
    72
          fun add_pbt (pbt as {guh,...}, pblID) =
s1210629013@55359
    73
                (* the pblID has the leaf-element as first; better readability achieved *)
s1210629013@55359
    74
                (if (!check_guhs_unique) then check_pblguh_unique guh (Data.get thy) else ();
s1210629013@55359
    75
                  rev pblID |> insrt pblID pbt);
s1210629013@55359
    76
        in Data.map (fold add_pbt pbts) thy end;
s1210629013@55339
    77
neuper@52123
    78
  (*etc*)
neuper@52123
    79
end;
neuper@52123
    80
*}
neuper@52123
    81
neuper@52123
    82
section {* Re-use existing access functions for knowledge elements *}
neuper@52123
    83
text {*
neuper@52123
    84
  The independence of problems' and methods' structure enforces the accesse
neuper@52123
    85
  functions to use "Isac", the final theory which comprises all knowledge defined.
neuper@52123
    86
*}
neuper@52124
    87
ML {*
neuper@52142
    88
(* in this phase of removing Unsynchornized.ref we do NOT overwrite the existing function! *)
neuper@52125
    89
fun assoc_rls (rls' : rls') = (*SWITCH outcommented*)
neuper@52123
    90
  case AList.lookup (op =) (KEStore_Elems.get_rlss (Thy_Info.get_theory "Isac")) rls' of
neuper@52123
    91
    SOME (_, rls) => rls
neuper@52123
    92
  | NONE => raise ERROR ("rls \""^ rls' ^ "\" missing in KEStore.\n" ^
neuper@52123
    93
    "TODO exception hierarchy needs to be established.")
neuper@52142
    94
neuper@52142
    95
fun assoc_rls' thy (rls' : rls') = (*SWITCH outcommented*)
neuper@52142
    96
  case AList.lookup (op =) (KEStore_Elems.get_rlss thy) rls' of
neuper@52142
    97
    SOME (_, rls) => rls
neuper@52142
    98
  | NONE => raise ERROR ("rls \""^ rls' ^ "\" missing in KEStore.\n" ^
neuper@52142
    99
    "TODO exception hierarchy needs to be established.")
neuper@52142
   100
s1210629013@52153
   101
fun assoc_calc thy calID = let
s1210629013@52153
   102
    fun ass ([], key) =
s1210629013@52153
   103
          error ("assoc_calc: '" ^ key ^ "' not found in theory " ^ (Context.theory_name thy))
s1210629013@52153
   104
      | ass ((calc, (keyi, _)) :: pairs, key) =
s1210629013@52153
   105
          if key = keyi then calc else ass (pairs, key);
s1210629013@52153
   106
  in ass (thy |> KEStore_Elems.get_calcs, calID) end;
s1210629013@52145
   107
s1210629013@52153
   108
fun assoc_calc' thy key = let
s1210629013@52153
   109
    fun ass ([], key') =
s1210629013@52153
   110
          error ("assoc_calc': '" ^ key' ^ "' not found in theory " ^ (Context.theory_name thy))
s1210629013@52153
   111
      | ass ((all as (keyi, _)) :: pairs, key') =
s1210629013@52153
   112
          if key' = keyi then all else ass (pairs, key');
s1210629013@52153
   113
  in ass (KEStore_Elems.get_calcs thy, key) end;
neuper@52169
   114
s1210629013@52173
   115
fun assoc_cas thy key = assoc (KEStore_Elems.get_cas thy, key);
s1210629013@55338
   116
s1210629013@55359
   117
fun get_ptyps () = ! ptyps;
neuper@55357
   118
(*SWITCH:
s1210629013@55359
   119
fun get_ptyps () = Thy_Info.get_theory "Build_Knowledge" |> KEStore_Elems.get_ptyps*)
neuper@55357
   120
neuper@55357
   121
(*SWITCH: outcomment store_pbt, if all occurrences in src+test have in parallel 
neuper@55357
   122
  SETUP ..* KEStore_Elems.add_pbt (.....) *.. 
neuper@55357
   123
*)
neuper@55357
   124
fun store_pbt (pbt as {guh,...}, pblID) = 
neuper@55357
   125
    (if (!check_guhs_unique) then check_pblguh_unique guh (! ptyps) else ();
neuper@55357
   126
     ptyps:= insrt pblID pbt (rev pblID) (! ptyps));
neuper@55357
   127
neuper@52124
   128
*}
neuper@52125
   129
setup {* KEStore_Elems.add_rlss 
neuper@52125
   130
  [("e_rls", (Context.theory_name @{theory}, e_rls)), 
neuper@52125
   131
  ("e_rrls", (Context.theory_name @{theory}, e_rrls))] *}
neuper@52123
   132
neuper@52128
   133
section {* Functions for checking KEStore_Elems *}
neuper@52128
   134
ML {*
neuper@52128
   135
fun short_string_of_rls Erls = "Erls"
neuper@52128
   136
  | short_string_of_rls (Rls {calc, rules, ...}) =
neuper@52128
   137
    "Rls {#calc = " ^ string_of_int (length calc) ^
neuper@52128
   138
    ", #rules = " ^ string_of_int (length rules) ^ ", ..."
neuper@52128
   139
  | short_string_of_rls (Seq {calc, rules, ...}) =
neuper@52128
   140
    "Seq {#calc = " ^ string_of_int (length calc) ^
neuper@52128
   141
    ", #rules = " ^ string_of_int (length rules) ^ ", ..."
neuper@52128
   142
  | short_string_of_rls (Rrls _) = "Rrls {...}";
neuper@52128
   143
neuper@52128
   144
fun check_kestore_rls (rls', (thyID, rls)) =
neuper@52128
   145
  "(" ^ rls' ^ ", (" ^ thyID ^ ", " ^ short_string_of_rls rls ^ "))";
s1210629013@52151
   146
s1210629013@52172
   147
fun check_kestore_calc ((id, (c, _)) : calc)  = "(" ^ id ^ ", (" ^ c ^ ", fn))";
s1210629013@52172
   148
s1210629013@52172
   149
fun check_kestore_cas ((t, (s, _)):cas_elem) =
s1210629013@52172
   150
  "(" ^ (term_to_string''' @{theory} t) ^ ", " ^ (spec2str s) ^ ")";
s1210629013@55346
   151
s1210629013@55346
   152
fun count_kestore_ptyps [] = 0
s1210629013@55346
   153
  | count_kestore_ptyps ((Ptyp (_, _, ps)) :: ps') =
s1210629013@55346
   154
      1 + count_kestore_ptyps ps  + count_kestore_ptyps ps';
s1210629013@55346
   155
fun check_kestore_ptyp ((Ptyp (key, pbts, pts)):pbt ptyp) = "Ptyp (" ^ (quote key) ^ ", " ^
s1210629013@55346
   156
      (pbts2str pbts) ^ ", " ^ (map check_kestore_ptyp pts |> list2str) ^ ")" |> linefeed;
s1210629013@55346
   157
fun ptyp_ord ((Ptyp (s1, _, _)), (Ptyp (s2, _, _))) = string_ord (s1, s2);
s1210629013@55346
   158
fun pbt_ord ({guh = guh'1, ...} : pbt, {guh = guh'2, ...} : pbt) = string_ord (guh'1, guh'2);
s1210629013@55346
   159
fun sort_kestore_ptyp [] = []
s1210629013@55346
   160
  | sort_kestore_ptyp ((Ptyp (key, pbts, ps)) :: ps') =
s1210629013@55346
   161
     ((Ptyp (key, sort pbt_ord pbts, sort_kestore_ptyp ps |> sort ptyp_ord))
s1210629013@55346
   162
       :: sort_kestore_ptyp ps');
s1210629013@55346
   163
neuper@52128
   164
*}
neuper@52123
   165
end