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