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