src/Tools/isac/MathEngBasic/method.sml
author wneuper <Walther.Neuper@jku.at>
Mon, 01 Jan 2024 11:31:16 +0100
changeset 60789 8fa678b678e8
parent 60766 2e0603ca18a4
permissions -rw-r--r--
Doc/Specify_Phase 4: start use antiquotations from isar-ref
     1 (* Title:  Interpret/lucas-interpreter.sml
     2    Author: Walther Neuper 2019
     3    (c) due to copyright terms
     4 
     5 \<open>ML_structure MethodC\<close> holds the essential information required for automated doing
     6 \<open>ML_structure Specification\<close> and \<open>ML_structure Solution\<close>.
     7 
     8 Why \<open>ML_structure MethodC\<close> is parsed with general types within the current \<open>ML_structure Context\<close>
     9 and refined with appropriate types from the ctxt of an Example.
    10 *)
    11 
    12 signature METHOD =
    13 sig
    14   type T = Meth_Def.T
    15   val empty: T
    16 
    17   type id = Meth_Def.id
    18   val id_empty: id
    19   val id_to_string: id -> string
    20 
    21   type ml_input
    22   val prep_store: theory -> Check_Unique.id -> string list -> References_Def.id ->
    23     id * (Model_Pattern.m_field * (Model_Def.descriptor * term)) list * term list * ml_input * thm ->
    24       T * id
    25 
    26   val set_data: ml_input -> theory -> theory
    27   val the_data: theory -> ml_input
    28 
    29   val from_store': theory -> id -> T
    30   val from_store: Proof.context -> id -> T
    31 end
    32 
    33 (**)
    34 structure MethodC(**): METHOD(**) =
    35 struct
    36 (**)
    37 
    38 type T = Meth_Def.T;
    39 val empty = Meth_Def.empty;
    40 
    41 type id = Meth_Def.id;
    42 val id_empty = Meth_Def.id_empty;
    43 val id_to_string = Meth_Def.id_to_string;
    44 
    45 
    46 (** prepare MethodC for Store **)
    47 
    48 (* a subset of MethodC.T record's fields occuring in \<open>method\<close> *)
    49 type ml_input = 
    50   {calc: Rule_Def.eval_ml_from_prog list, errpats: Error_Pattern_Def.T list, rew_rls: Rule_Set.T,
    51     where_rls: Rule_Set.T, rew_ord: Rewrite_Ord.id, rls': Rule_Set.T, prog_rls: Rule_Set.T}
    52 
    53 fun prep_store thy guh mathauthors start_refine (met_id, model, where_,
    54     {rew_ord = ro, rls' = rls, prog_rls = prog_rls, where_rls = where_rls, calc = _(*scr_isa_fns*),
    55       errpats = ep, rew_rls = nr}, prog) =
    56   let
    57 	  val sc = Program.prep_program prog
    58 	  val calc = if Thm.eq_thm (prog, @{thm refl}) then [] else Auto_Prog.get_calcs thy sc
    59   in
    60      ({guh = guh, mathauthors = mathauthors, start_refine = start_refine, model = model(*'*),
    61       where_ = where_, rew_ord = ro, asm_rls = rls, prog_rls = prog_rls, where_rls = where_rls,
    62       calc = calc, errpats = ep, rew_rls = nr, program = Rule.Prog sc}: T, met_id)
    63   end;
    64 
    65 (** Isar command **)
    66 
    67 structure Data = Theory_Data
    68 (
    69   type T = ml_input option;
    70   val empty = NONE;
    71   fun merge _ = NONE;
    72 );
    73 
    74 val set_data = Data.put o SOME;
    75 val the_data = the o Data.get;
    76 
    77 local
    78 
    79 val drop_pos_model = (fn (a, b, _) => (a, b))
    80 val drop_pos_where_ = (fn (b, _) => b)
    81 
    82 val parse_program =
    83   Scan.option (Problem.parse_item \<^keyword>\<open>Program\<close> (Parse.position Parse.name));
    84 
    85 val ml = ML_Lex.read;
    86 
    87 val _ =
    88   Outer_Syntax.command \<^command_keyword>\<open>method\<close>
    89     "prepare ISAC method and register it to Knowledge Store"
    90     (Parse.name -- Parse.!!! (\<^keyword>\<open>:\<close> |-- Parse.name --
    91       Parse.!!! (\<^keyword>\<open>=\<close> |-- Parse.ML_source -- Problem.parse_authors --
    92         parse_program -- (** )Problem.parse_model_input( **)Problem.parse_pos_model_input(**) )) >>
    93       (fn (name, (id, (((source, maa), program), model_input))) => Toplevel.theory (fn thy =>
    94         let
    95           val ctxt = Proof_Context.init_global thy
    96           val met_id = References_Def.explode_id id;
    97           val (model, where_) = Model_Pattern.parse_pos ctxt model_input
    98           val (model', where_') = (map drop_pos_model model, map drop_pos_where_ where_)
    99           val set_data =
   100             ML_Context.expression (Input.pos_of source)
   101               (ml "Theory.setup (MethodC.set_data (" @ ML_Lex.read_source source @ ml "))")
   102             |> Context.theory_map;
   103           val ml_input = the_data (set_data thy);
   104           val thm =
   105             (case program of
   106               NONE => @{thm refl}
   107             | SOME a => Global_Theory.get_thm thy (Global_Theory.check_fact thy a));
   108           val arg = prep_store thy name maa id_empty (met_id, model', where_', ml_input, thm);
   109         in Know_Store.add_mets ctxt [arg] thy end)))      
   110 
   111 in end;
   112 
   113 (** get MethodC from Store **)
   114 
   115 fun adapt_to_type ctxt {guh, mathauthors, start_refine, rew_ord, asm_rls, prog_rls, (*crls,*) rew_rls, errpats, calc, 
   116     program, where_rls, model, where_} =
   117   let
   118     val where_ = map (ParseC.adapt_term_to_type ctxt) where_
   119     val model = map (Model_Pattern.adapt_to_type ctxt) model
   120     val errpats = map (Error_Pattern_Def.adapt_to_type ctxt) errpats
   121   in
   122     {guh = guh, mathauthors = mathauthors, start_refine = start_refine, rew_ord = rew_ord, asm_rls = asm_rls, 
   123     prog_rls = prog_rls, (*crls = crls,*) rew_rls = rew_rls, errpats = errpats, calc = calc, program = program, where_rls = where_rls, 
   124     model = model, where_ = where_}
   125     end
   126 
   127 (* TODO: throws exn 'get_pbt not found: ' ... confusing !! take 'ketype' as an argument *)
   128 fun from_store' thy id =
   129   (Store.get (Know_Store.get_mets thy) id id)
   130     handle ERROR _ =>
   131       raise error ("*** ERROR MethodC.from_store' thy " ^ strs2str id ^ " not found");
   132 fun from_store ctxt id =
   133   let
   134     val pbl = Store.get (get_mets ()) id id
   135   in adapt_to_type ctxt pbl end
   136     handle ERROR _ =>
   137       raise error ("*** ERROR MethodC.from_store ctxt " ^ strs2str id ^ " not found");
   138 
   139 (**)end(**)