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