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