src/Tools/isac/Specify/cas-command.sml
author wneuper <Walther.Neuper@jku.at>
Mon, 30 Jan 2023 12:11:40 +0100
changeset 60661 91c30b11e5bc
parent 60603 eec3b6fd6c7a
child 60676 8c37f1009457
permissions -rw-r--r--
cleanup parse #4: eliminate TermC.parse
     1 (* Title:  Interpret/lucas-interpreter.sml
     2    Author: Walther Neuper 2019
     3    (c) due to copyright terms
     4 *)
     5 
     6 signature COMPUTER_ALGEBRA_SYSTEM_COMMAND =
     7 sig
     8   type input_pos
     9   type T = CAS_Def.T
    10   val input : term -> (Ctree.ctree * SpecificationC.T) option
    11   val is_from: TermC.as_string -> Formalise.T -> bool
    12 \<^isac_test>\<open>
    13   val input_: References.T -> (term * term list) list ->
    14     Problem.id * I_Model.T * MethodC.id * I_Model.T * Pre_Conds.T * Proof.context
    15 
    16   val dtss2itm_: Model_Pattern.T -> term * term list ->
    17     int list * bool * string * I_Model.feedback (*I_Model.single'*)
    18   val flattup2: 'a * ('b * 'c * 'd * 'e) -> 'a * 'b * 'c * 'd * 'e
    19 \<close>
    20 end
    21 
    22 (**)
    23 structure CAS_Cmd(**): COMPUTER_ALGEBRA_SYSTEM_COMMAND(**) =
    24 struct
    25 (**)
    26 
    27 type input_pos = CAS_Def.input_pos;
    28 type T = CAS_Def.T; (*(term * (References_Def.T * generate_fn))*)
    29 
    30 fun dtss2itm_ model (d, ts) =
    31   let
    32     val (f, (d, id)) = the (find_first ((curry op= d) o 
    33   		(#1: (term * term) -> term) o
    34   		(#2: Model_Pattern.single -> (term * term))) model)
    35   in
    36     ([1], true, f, I_Model.Cor ((d, ts), (id, ts)))
    37   end
    38 
    39 fun flattup2 (a, (b ,c, d, e)) = (a, b, c, d, e)
    40 
    41 fun is_from (hdf : TermC.as_string) ((fmz_, spec) : Formalise.T) =
    42   hdf <> "" andalso fmz_ = [] andalso spec = References.empty
    43 
    44 fun input_ ((dI, pI, mI): References.T) dtss = (*WN110515 reconsider thy..ctxt*)
    45   let
    46     val thy = ThyC.get_theory dI
    47     val ctxt = Proof_Context.init_global thy
    48 	  val {model, ...} = Problem.from_store ctxt pI
    49 	  val its_ = map (dtss2itm_ model) dtss (*([1],true,"#Given",Cor (...))*)
    50 	  val its = O_Model.add_enumerate its_
    51 	  val pits = map flattup2 its
    52 	  val (pI, mI) =
    53       if mI <> ["no_met"]
    54       then (pI, mI)
    55 		  else
    56         case Refine.problem thy pI pits of
    57 			    SOME (pI, _) => (pI, (hd o #solve_mets o Problem.from_store ctxt) pI)
    58 			  | NONE => (pI, (hd o #solve_mets o Problem.from_store ctxt) pI)
    59 	  val {model, where_, where_rls, ...} = MethodC.from_store ctxt mI
    60 	  val its_ = map (dtss2itm_ model) dtss (*([1],true,"#Given",Cor (...))*)
    61 	  val its = O_Model.add_enumerate its_
    62 	  val mits = map flattup2 its
    63 	  val (_, where_) = Pre_Conds.check ctxt where_rls where_ mits 0
    64     val ctxt = Proof_Context.init_global thy
    65   in (pI, pits, mI, mits, where_, ctxt) end;
    66 
    67 (* check if the input term is a CAScmd and return a ctree with a _complete_ calchead *)
    68 fun input hdt =
    69   let
    70     val (h, argl) = strip_comb hdt
    71   in
    72     case get_cas_global h of
    73       NONE => NONE
    74     | SOME (spec as (dI,_,_), argl2dtss) =>
    75 	      let
    76           val dtss = argl2dtss argl
    77 	        val (pI, pits, mI, mits, where_, ctxt) = input_ spec dtss
    78 	        val spec = (dI, pI, mI)
    79 	        val (pt,_) = Ctree.cappend_problem Ctree.e_ctree [] (Istate_Def.empty, ContextC.empty)
    80 		        ([], References.empty) ([], References.empty, hdt, ctxt)
    81 	        val pt = Ctree.update_spec pt [] spec
    82 	        val pt = Ctree.update_pbl pt [] pits
    83 	        val pt = Ctree.update_met pt [] mits
    84 	      in
    85 	        SOME (pt, (true, Pos.Met, hdt, mits, where_, spec) : SpecificationC.T)
    86 	      end
    87   end
    88 
    89 
    90 (** Isar command **)
    91 
    92 local
    93 
    94 val parse_theory = Problem.parse_item_name \<^keyword>\<open>Theory_Ref\<close> "Isac_Knowledge";
    95 val parse_problem = Problem.parse_item \<^keyword>\<open>Problem_Ref\<close> Parse.name;
    96 val parse_method = Problem.parse_item_name \<^keyword>\<open>Method_Ref\<close> "no_met";
    97 
    98 val ml = ML_Lex.read;
    99 
   100 val _ =
   101   Outer_Syntax.command \<^command_keyword>\<open>cas\<close>
   102     "prepare ISAC Computer-Algebra System and register it to Knowledge Store"
   103     (Parse.term -- (\<^keyword>\<open>=\<close> |--
   104       Parse.!!! (Parse.ML_source -- parse_theory -- parse_problem -- parse_method))
   105       >> (fn (term, (((source, thry), pbl), met)) => Toplevel.theory (fn thy =>
   106         let
   107         (*/------------ replace by ParseC.term_position ------------------\*)
   108           val SOME t = ParseC.term_opt (Proof_Context.init_global thy) term;
   109         (*\------------ replace by ParseC.term_position ------------------/*)
   110           val pblID = References_Def.explode_id pbl;
   111           val metID = References_Def.explode_id met;
   112           val set_data =
   113             ML_Context.expression (Input.pos_of source)
   114               (ml "Theory.setup (CAS_Def.set_data (" @ ML_Lex.read_source source @ ml "))")
   115             |> Context.theory_map;
   116           val data = CAS_Def.the_data (set_data thy);
   117         in Know_Store.add_cass [(t, ((thry, pblID, metID), data))] thy end)));
   118 
   119 in end;
   120 
   121 (**)end(**)