src/Tools/isac/MathEngBasic/method.sml
author wenzelm
Tue, 15 Jun 2021 22:24:20 +0200
changeset 60303 815b0dc8b589
parent 60265 9c9d61fed195
child 60307 d692abd52098
permissions -rw-r--r--
Isar command 'method' as combination of KEStore_Elems.add_mets + MethodC.prep_import, without change of semantics;
     1 (* Title:  Interpret/lucas-interpreter.sml
     2    Author: Walther Neuper 2019
     3    (c) due to copyright terms
     4 *)
     5 
     6 signature METHOD =
     7 sig
     8   type T = Meth_Def.T
     9   val empty: T
    10 
    11   type id = Meth_Def.id
    12   val id_empty: id
    13   val id_to_string: id -> string
    14 
    15   type input
    16   (* TODO: ------------- remove always empty --- vvvvvvvvvvv -- vvv*)
    17   val prep_input : theory ->  Check_Unique.id -> string list -> id ->
    18      id * Problem.model_input * input * thm -> T * id
    19 
    20   val set_input: input -> theory -> theory
    21   val the_input: theory -> input
    22 
    23   val from_store: id -> T
    24   val from_store': theory -> id -> T
    25 end
    26 
    27 (**)
    28 structure MethodC(**): METHOD(**) =
    29 struct
    30 (**)
    31 
    32 type T = Meth_Def.T;
    33 val empty = Meth_Def.empty;
    34 
    35 type id = Meth_Def.id;
    36 val id_empty = Meth_Def.id_empty;
    37 val id_to_string = Meth_Def.id_to_string;
    38 
    39 
    40 (** prepare MethodC for Store **)
    41 
    42 (* a subset of MethodC.T record's fields *)
    43 type input = 
    44   {calc: Rule_Def.calc list, crls: Rule_Set.T, errpats: Error_Pattern_Def.T list, nrls: Rule_Set.T,
    45     prls: Rule_Set.T, rew_ord': Rewrite_Ord.rew_ord', rls': Rule_Set.T, srls: Rule_Set.T}
    46 
    47 fun prep_input thy guh (mathauthors: string list) (init: References_Def.id)
    48 	    (metID, ppc,
    49         {rew_ord' = ro, rls' = rls, srls = srls, prls = prls, calc = _(*scr_isa_fns*), crls = cr,
    50           errpats = ep, nrls = nr}, scr) =
    51     let
    52       fun eq f (f', _) = f = f';
    53       val gi = filter (eq "#Given") ppc;
    54       val gi = (case gi of
    55 		    [] => (writeln ("prep_input: in " ^ guh ^ " #Given is empty ?!?"); [])
    56 		  | ((_, gi') :: []) => 
    57         (case \<^try>\<open> map (Problem.split_did o Thm.term_of o the o (TermC.parse thy)) gi'\<close> of
    58           SOME x => x
    59         | NONE => raise ERROR ("prep_pbt: syntax raise ERROR in '#Given' of " ^ strs2str metID))
    60 		  | _ => raise ERROR ("prep_pbt: more than one '#Given' in " ^ strs2str metID));
    61 		  val gi = map (pair "#Given") gi;
    62 
    63 		  val fi = filter (eq "#Find") ppc;
    64 		  val fi = (case fi of
    65 		    [] => (writeln ("prep_input: in " ^ guh ^ " #Find is empty ?!?"); [])
    66 		  | ((_, fi') :: []) => 
    67         (case \<^try>\<open> map (Problem.split_did o Thm.term_of o the o (TermC.parse thy)) fi'\<close> of
    68           SOME x => x
    69         | NONE => raise ERROR ("prep_pbt: syntax raise ERROR in '#Find' of " ^ strs2str metID))
    70 		  | _ => raise ERROR ("prep_pbt: more than one '#Find' in " ^ strs2str metID));
    71 		  val fi = map (pair "#Find") fi;
    72 
    73 		  val re = filter (eq "#Relate") ppc;
    74 		  val re = (case re of
    75 		    [] => (writeln ("prep_input: in " ^ guh ^ " #Relate is empty ?!?"); [])
    76 		  | ((_,re') :: []) =>
    77         (case \<^try>\<open> map (Problem.split_did o Thm.term_of o the o (TermC.parse thy)) re'\<close> of
    78           SOME x => x
    79         | NONE => raise ERROR ("prep_pbt: syntax raise ERROR in '#Relate' of " ^ strs2str metID))
    80 		  | _ => raise ERROR ("prep_pbt: more than one '#Relate' in " ^ strs2str metID));
    81 		  val re = map (pair "#Relate") re;
    82 
    83 		  val wh = filter (eq "#Where") ppc;
    84 		  val wh = (case wh of
    85 		    [] => (writeln ("prep_input: in " ^ guh ^ " #Where is empty ?!?"); [])
    86 		  | ((_, wh') :: []) => 
    87         (case \<^try>\<open> map (TermC.parse_patt thy) wh'\<close> of
    88           SOME x => x
    89         | NONE => raise ERROR ("prep_pbt: syntax raise ERROR in '#Where' of " ^ strs2str metID))
    90 		  | _ => raise ERROR ("prep_pbt: more than one '#Where' in " ^ strs2str metID));
    91 		  val sc = Program.prep_program scr
    92 		  val calc = if Thm.eq_thm (scr, @{thm refl}) then [] else Auto_Prog.get_calcs thy sc
    93     in
    94        ({guh = guh, mathauthors = mathauthors, init = init, ppc = gi @ fi @ re, pre = wh,
    95          rew_ord' = ro, erls = rls, srls = srls, prls = prls, calc = calc,
    96          crls = cr, errpats = ep, nrls = nr, scr = Rule.Prog sc}, metID)
    97     end;
    98 
    99 
   100 (** Isar command **)
   101 
   102 structure Data = Theory_Data
   103 (
   104   type T = input option;
   105   val empty = NONE;
   106   val extend = I;
   107   fun merge _ = NONE;
   108 );
   109 
   110 val set_input = Data.put o SOME;
   111 val the_input = the o Data.get;
   112 
   113 local
   114 
   115 fun parse_item keyword parse = (keyword -- Args.colon) |-- Parse.!!! parse;
   116 
   117 val parse_authors =
   118   Scan.optional (parse_item \<^keyword>\<open>Author\<close> (Scan.repeat1 Parse.name)) [];
   119 
   120 val parse_program =
   121   Scan.option (parse_item \<^keyword>\<open>Program\<close> (Parse.position Parse.name));
   122 
   123 fun parse_terms keyword (tag: string) =
   124   Scan.optional (parse_item keyword (Scan.repeat Parse.term) >> (fn ts => [(tag, ts)])) [];
   125 
   126 val parse_ppc =
   127   parse_terms \<^keyword>\<open>Given\<close> "#Given" @@@
   128   parse_terms \<^keyword>\<open>Where\<close> "#Where" @@@
   129   parse_terms \<^keyword>\<open>Find\<close> "#Find" @@@
   130   parse_terms \<^keyword>\<open>Relate\<close> "#Relate";
   131 
   132 val ml = ML_Lex.read;
   133 
   134 val _ =
   135   Outer_Syntax.command \<^command_keyword>\<open>method\<close>
   136     "prepare ISAC method and register it to Knowledge Store"
   137     (Parse.name -- Parse.!!! (\<^keyword>\<open>:\<close> |-- Parse.name --
   138       Parse.!!! (\<^keyword>\<open>=\<close> |-- Parse.ML_source -- parse_authors -- parse_program -- parse_ppc)) >>
   139       (fn (name, (id, (((source, mathauthors), simp), ppc))) => Toplevel.theory (fn thy =>
   140         let
   141           val metID = References_Def.explode_id id;
   142           val eval_input =
   143             ML_Context.expression (Input.pos_of source)
   144               (ml "Theory.setup (MethodC.set_input (" @ ML_Lex.read_source source @ ml "))")
   145             |> Context.theory_map;
   146           val input = the_input (eval_input thy);
   147           val thm =
   148             (case simp of
   149               NONE => @{thm refl}
   150             | SOME a => Global_Theory.get_thm thy (Global_Theory.check_fact thy a));
   151           val arg = prep_input thy name mathauthors id_empty (metID, ppc, input, thm);
   152         in KEStore_Elems.add_mets [arg] thy end)))
   153 
   154 in end;
   155 
   156 (** get MethodC from Store **)
   157 
   158 (* TODO: throws exn 'get_pbt not found: ' ... confusing !! take 'ketype' as an argument *)
   159 fun from_store metID = Store.get (get_mets ()) metID metID;
   160 fun from_store' thy metID = Store.get (KEStore_Elems.get_mets thy) metID metID;
   161 
   162 (**)end(**)