src/Tools/isac/Specify/cas-command.sml
author wneuper <Walther.Neuper@jku.at>
Sun, 27 Aug 2023 16:48:03 +0200
changeset 60741 22586d7fedb0
parent 60729 43d11e7742e1
child 60744 8f153b365de2
permissions -rw-r--r--
followup 3: rename Pre_Conds.* and I_Model.* (eliminate *_TEST as much as possible)
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
wenzelm@60223
    12
\<^isac_test>\<open>
walther@59982
    13
  val input_: References.T -> (term * term list) list ->
walther@60154
    14
    Problem.id * I_Model.T * MethodC.id * I_Model.T * Pre_Conds.T * Proof.context
walther@59982
    15
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
wenzelm@60223
    19
\<close>
walther@59894
    20
end
walther@59894
    21
walther@59894
    22
(**)
walther@59894
    23
structure CAS_Cmd(**): COMPUTER_ALGEBRA_SYSTEM_COMMAND(**) =
walther@59894
    24
struct
walther@59894
    25
(**)
walther@59894
    26
Walther@60603
    27
type input_pos = CAS_Def.input_pos;
Walther@60547
    28
type T = CAS_Def.T; (*(term * (References_Def.T * generate_fn))*)
walther@59896
    29
Walther@60586
    30
fun dtss2itm_ model (d, ts) =
walther@59982
    31
  let
walther@59982
    32
    val (f, (d, id)) = the (find_first ((curry op= d) o 
walther@59982
    33
  		(#1: (term * term) -> term) o
Walther@60586
    34
  		(#2: Model_Pattern.single -> (term * term))) model)
walther@59982
    35
  in
walther@59982
    36
    ([1], true, f, I_Model.Cor ((d, ts), (id, ts)))
walther@59982
    37
  end
walther@59982
    38
walther@59982
    39
fun flattup2 (a, (b ,c, d, e)) = (a, b, c, d, e)
walther@59982
    40
walther@59982
    41
fun is_from (hdf : TermC.as_string) ((fmz_, spec) : Formalise.T) =
walther@59982
    42
  hdf <> "" andalso fmz_ = [] andalso spec = References.empty
walther@59982
    43
Walther@60729
    44
(*T_TESTold* )
walther@59982
    45
fun input_ ((dI, pI, mI): References.T) dtss = (*WN110515 reconsider thy..ctxt*)
walther@59982
    46
  let
Walther@60676
    47
    val thy = Know_Store.get_via_last_thy dI
Walther@60556
    48
    val ctxt = Proof_Context.init_global thy
Walther@60585
    49
	  val {model, ...} = Problem.from_store ctxt pI
Walther@60585
    50
	  val its_ = map (dtss2itm_ model) dtss (*([1],true,"#Given",Cor (...))*)
Walther@60469
    51
	  val its = O_Model.add_enumerate its_
walther@59982
    52
	  val pits = map flattup2 its
walther@59982
    53
	  val (pI, mI) =
walther@59982
    54
      if mI <> ["no_met"]
walther@59982
    55
      then (pI, mI)
walther@59982
    56
		  else
walther@59982
    57
        case Refine.problem thy pI pits of
Walther@60585
    58
			    SOME (pI, _) => (pI, (hd o #solve_mets o Problem.from_store ctxt) pI)
Walther@60585
    59
			  | NONE => (pI, (hd o #solve_mets o Problem.from_store ctxt) pI)
Walther@60586
    60
	  val {model, where_, where_rls, ...} = MethodC.from_store ctxt mI
Walther@60586
    61
	  val its_ = map (dtss2itm_ model) dtss (*([1],true,"#Given",Cor (...))*)
Walther@60469
    62
	  val its = O_Model.add_enumerate its_
walther@59982
    63
	  val mits = map flattup2 its
Walther@60590
    64
	  val (_, where_) = Pre_Conds.check ctxt where_rls where_ mits 0
walther@59982
    65
    val ctxt = Proof_Context.init_global thy
Walther@60586
    66
  in (pI, pits, mI, mits, where_, ctxt) end;
Walther@60729
    67
( *T_TEST**)
Walther@60729
    68
fun input_ ((dI, pI, mI): References.T) dtss = (*WN110515 reconsider thy..ctxt*)
Walther@60729
    69
  let
Walther@60729
    70
    val thy = Know_Store.get_via_last_thy dI
Walther@60729
    71
    val ctxt = Proof_Context.init_global thy
Walther@60729
    72
	  val {model, ...} = Problem.from_store ctxt pI
Walther@60729
    73
	  val its_ = map (dtss2itm_ model) dtss (*([1],true,"#Given",Cor (...))*)
Walther@60729
    74
	  val its = O_Model.add_enumerate its_
Walther@60729
    75
	  val pits = map flattup2 its
Walther@60729
    76
	  val (pI, mI) =
Walther@60729
    77
      if mI <> ["no_met"]
Walther@60729
    78
      then (pI, mI)
Walther@60729
    79
		  else
Walther@60729
    80
        case Refine.problem thy pI pits of
Walther@60729
    81
			    SOME (pI, _) => (pI, (hd o #solve_mets o Problem.from_store ctxt) pI)
Walther@60729
    82
			  | NONE => (pI, (hd o #solve_mets o Problem.from_store ctxt) pI)
Walther@60729
    83
	  val {model, where_, where_rls, ...} = MethodC.from_store ctxt mI
Walther@60729
    84
	  val its_ = map (dtss2itm_ model) dtss (*([1],true,"#Given",Cor (...))*)
Walther@60729
    85
	  val its = O_Model.add_enumerate its_
Walther@60729
    86
	  val mits = map flattup2 its
Walther@60741
    87
	  val (_, where_) = Pre_Conds.check ctxt where_rls where_ (model, I_Model.OLD_to_TEST mits)
Walther@60729
    88
    val ctxt = Proof_Context.init_global thy
Walther@60729
    89
  in (pI, pits, mI, mits, where_, ctxt) end;
Walther@60729
    90
(*T_TESTnew*)
walther@59982
    91
walther@59982
    92
(* check if the input term is a CAScmd and return a ctree with a _complete_ calchead *)
walther@59982
    93
fun input hdt =
walther@59982
    94
  let
walther@59982
    95
    val (h, argl) = strip_comb hdt
walther@59982
    96
  in
Walther@60550
    97
    case get_cas_global h of
walther@59982
    98
      NONE => NONE
walther@59982
    99
    | SOME (spec as (dI,_,_), argl2dtss) =>
walther@59982
   100
	      let
walther@59982
   101
          val dtss = argl2dtss argl
Walther@60586
   102
	        val (pI, pits, mI, mits, where_, ctxt) = input_ spec dtss
walther@59982
   103
	        val spec = (dI, pI, mI)
walther@59982
   104
	        val (pt,_) = Ctree.cappend_problem Ctree.e_ctree [] (Istate_Def.empty, ContextC.empty)
walther@59982
   105
		        ([], References.empty) ([], References.empty, hdt, ctxt)
walther@59982
   106
	        val pt = Ctree.update_spec pt [] spec
walther@59982
   107
	        val pt = Ctree.update_pbl pt [] pits
walther@59982
   108
	        val pt = Ctree.update_met pt [] mits
walther@59982
   109
	      in
Walther@60586
   110
	        SOME (pt, (true, Pos.Met, hdt, mits, where_, spec) : SpecificationC.T)
walther@59982
   111
	      end
walther@59982
   112
  end
walther@59894
   113
wenzelm@60314
   114
wenzelm@60314
   115
(** Isar command **)
wenzelm@60314
   116
wenzelm@60314
   117
local
wenzelm@60314
   118
Walther@60449
   119
val parse_theory = Problem.parse_item_name \<^keyword>\<open>Theory_Ref\<close> "Isac_Knowledge";
Walther@60449
   120
val parse_problem = Problem.parse_item \<^keyword>\<open>Problem_Ref\<close> Parse.name;
Walther@60449
   121
val parse_method = Problem.parse_item_name \<^keyword>\<open>Method_Ref\<close> "no_met";
wenzelm@60314
   122
wenzelm@60314
   123
val ml = ML_Lex.read;
wenzelm@60314
   124
wenzelm@60314
   125
val _ =
wenzelm@60314
   126
  Outer_Syntax.command \<^command_keyword>\<open>cas\<close>
wenzelm@60314
   127
    "prepare ISAC Computer-Algebra System and register it to Knowledge Store"
wenzelm@60314
   128
    (Parse.term -- (\<^keyword>\<open>=\<close> |--
wenzelm@60314
   129
      Parse.!!! (Parse.ML_source -- parse_theory -- parse_problem -- parse_method))
wenzelm@60314
   130
      >> (fn (term, (((source, thry), pbl), met)) => Toplevel.theory (fn thy =>
wenzelm@60314
   131
        let
Walther@60661
   132
        (*/------------ replace by ParseC.term_position ------------------\*)
Walther@60661
   133
          val SOME t = ParseC.term_opt (Proof_Context.init_global thy) term;
Walther@60661
   134
        (*\------------ replace by ParseC.term_position ------------------/*)
wenzelm@60314
   135
          val pblID = References_Def.explode_id pbl;
wenzelm@60314
   136
          val metID = References_Def.explode_id met;
wenzelm@60314
   137
          val set_data =
wenzelm@60314
   138
            ML_Context.expression (Input.pos_of source)
wenzelm@60314
   139
              (ml "Theory.setup (CAS_Def.set_data (" @ ML_Lex.read_source source @ ml "))")
wenzelm@60314
   140
            |> Context.theory_map;
wenzelm@60314
   141
          val data = CAS_Def.the_data (set_data thy);
Walther@60588
   142
        in Know_Store.add_cass [(t, ((thry, pblID, metID), data))] thy end)));
wenzelm@60314
   143
wenzelm@60314
   144
in end;
wenzelm@60314
   145
walther@59894
   146
(**)end(**)