src/Tools/isac/Specify/step-specify.sml
author wneuper <Walther.Neuper@jku.at>
Wed, 21 Dec 2022 18:48:23 +0100
changeset 60628 f54e20d9e6ee
parent 60609 5967b6e610b5
child 60638 8942f07ead44
permissions -rw-r--r--
make Minisubplb/710-interSteps-short.sml independent from Thy_Info
walther@59747
     1
(* Title:  Specify/step-specify.sml
walther@59747
     2
   Author: Walther Neuper 2019
walther@59747
     3
   (c) due to copyright terms
walther@59747
     4
*)
walther@59747
     5
walther@59747
     6
signature STEP_SPECIFY =
walther@59747
     7
sig
walther@59791
     8
(*val do_next: Step.specify_do_next requires LI.by_tactic, which is not yet known in Step_Specify*)
walther@60020
     9
  val by_tactic_input: Tactic.input -> Calc.T -> string * Calc.state_post
walther@59981
    10
  val by_tactic: Tactic.T -> Calc.T -> string * Calc.state_post
walther@59975
    11
Walther@60576
    12
  val initialise: Proof.context -> Formalise.T ->
walther@60114
    13
    term * term * References.T * O_Model.T * Proof.context
Walther@60557
    14
  val nxt_specify_init_calc: Proof.context -> Formalise.T -> Calc.T * State_Steps.T
walther@59747
    15
end
walther@59747
    16
walther@59747
    17
(**)
walther@59747
    18
structure Step_Specify(**): STEP_SPECIFY(**) =
walther@59747
    19
struct
walther@59747
    20
(**)
walther@59763
    21
open Pos
walther@59763
    22
open Ctree
walther@59977
    23
open Specification
walther@59747
    24
walther@59806
    25
fun by_tactic_input (tac as Tactic.Model_Problem) (pt, pos as (p, _)) =
walther@59763
    26
    let
walther@59763
    27
      val (oris, ospec, probl, spec, ctxt) = case get_obj I pt p of
walther@59763
    28
        PblObj {origin = (oris, ospec, _), probl, spec, ctxt, ...} => (oris, ospec, probl, spec, ctxt)
walther@59962
    29
      | _ => raise ERROR "by_tactic_input Model_Problem; uncovered case get_obj"
Walther@60494
    30
      val (_, pI, mI) = References.select_input ospec spec
Walther@60586
    31
      val mpc = (#model o MethodC.from_store ctxt) mI (* just for reuse I_Model.complete_method *)
Walther@60585
    32
      val {cas, model, ...} = Problem.from_store (Ctree.get_ctxt pt pos) pI
Walther@60585
    33
      val pbl = I_Model.init model (* fill in descriptions *)
walther@59763
    34
      (*----------------if you think, this should be done by the Dialog 
walther@59763
    35
       in the java front-end, search there for WN060225-modelProblem----*)
walther@59763
    36
      val (pbl, met) = case cas of
walther@59763
    37
        NONE => (pbl, [])
Walther@60585
    38
  		| _ => I_Model.complete_method (oris, mpc, model, probl)
walther@59763
    39
      (*----------------------------------------------------------------*)
walther@59763
    40
      val tac_ = Tactic.Model_Problem' (pI, pbl, met)
walther@59933
    41
      val (pos,c,_,pt) = Specify_Step.add tac_ (Istate_Def.Uistate, ctxt) (pt, pos)
walther@60020
    42
    in
walther@60020
    43
      ("ok",([(tac, tac_, (pos, (Istate_Def.Uistate, ContextC.empty)))], c, (pt,pos)))
walther@60020
    44
    end
walther@60021
    45
  | by_tactic_input (Tactic.Add_Given ct) ptp = Specify.by_Add_ "#Given" ct ptp
walther@60021
    46
  | by_tactic_input (Tactic.Add_Find  ct) ptp = Specify.by_Add_ "#Find"  ct ptp
walther@60021
    47
  | by_tactic_input (Tactic.Add_Relation ct) ptp = Specify.by_Add_"#Relate" ct ptp
walther@60021
    48
walther@60154
    49
    (* called with MethodC.id_empty *)     
walther@59810
    50
  | by_tactic_input (Tactic.Refine_Tacitly pI) (ptp as (pt, pos as (p, _))) =
walther@59763
    51
    let 
walther@59763
    52
      val (oris, dI, ctxt) = case get_obj I pt p of
walther@59763
    53
        (PblObj {origin = (oris, (dI, _, _), _), ctxt, ...}) => (oris, dI, ctxt)
walther@59962
    54
      | _ => raise ERROR "by_tactic_input Refine_Tacitly: uncovered case get_obj"
Walther@60559
    55
      val opt = Refine.refine_ori ctxt oris pI
walther@59763
    56
    in 
walther@59763
    57
      case opt of
walther@59763
    58
	      SOME pI' => 
Walther@60556
    59
	        let
Walther@60585
    60
            val {solve_mets, ...} = Problem.from_store (Ctree.get_ctxt pt pos) pI'
walther@59763
    61
	          (*val pt = update_pbl pt p pbl ..done by Model_Problem*)
Walther@60585
    62
	          val mI = if length solve_mets = 0 then MethodC.id_empty else hd solve_mets
walther@59763
    63
	          val (pos, c, _, pt) = 
walther@59933
    64
		          Specify_Step.add (Tactic.Refine_Tacitly' (pI, pI', dI, mI,(*pbl*)[])) (Istate_Def.Uistate, ctxt) (pt, pos)
walther@59763
    65
	        in
walther@60020
    66
	          ("ok", ([(Tactic.Refine_Tacitly pI, Tactic.Refine_Tacitly' (pI,pI',dI,mI,(*pbl*)[]),
walther@60020
    67
	              (pos, (Istate_Def.Uistate, ContextC.empty)))], c, (pt,pos)))
walther@59763
    68
          end
walther@60021
    69
	    | NONE => ("failure", ([], [], ptp))
walther@59763
    70
    end
walther@59806
    71
  | by_tactic_input (Tactic.Refine_Problem pI) (ptp as (pt, pos as (p,_))) =
walther@59763
    72
    let
walther@59763
    73
      val (dI, dI', probl, ctxt) = case get_obj I pt p of
walther@59763
    74
        PblObj {origin= (_, (dI, _, _), _), spec = (dI', _, _), probl, ctxt, ...} =>
walther@59763
    75
          (dI, dI', probl, ctxt)
walther@59962
    76
      | _ => raise ERROR "by_tactic_input Refine_Problem: uncovered case get_obj"
walther@59879
    77
	    val thy = if dI' = ThyC.id_empty then dI else dI'
walther@59763
    78
    in 
walther@59960
    79
      case Refine.problem (ThyC.get_theory thy) pI probl of
walther@60021
    80
	      NONE => ("failure", ([], [], ptp))
walther@59763
    81
	    | SOME rfd => 
walther@59763
    82
	      let 
walther@59933
    83
          val (pos,c,_,pt) = Specify_Step.add (Tactic.Refine_Problem' rfd) (Istate_Def.Uistate, ctxt) (pt, pos)
walther@59763
    84
	      in
walther@60020
    85
	        ("ok", ([(Tactic.Refine_Problem pI, Tactic.Refine_Problem' rfd,
walther@60020
    86
            (pos, (Istate_Def.Uistate, ContextC.empty)))], c, (pt,pos)))
walther@59763
    87
        end
walther@59763
    88
    end
walther@60020
    89
  | by_tactic_input (Tactic.Specify_Problem pI) (pt, pos as (p, _)) =
walther@59763
    90
    let
Walther@60590
    91
      val (oris, pI', probl, ctxt) = case get_obj I pt p of
Walther@60590
    92
        PblObj {origin = (oris, _, _), spec = (_ ,pI',_), probl, ctxt, ...} =>
Walther@60590
    93
          (oris, pI', probl, ctxt)
walther@59962
    94
      | _ => raise ERROR ""
Walther@60585
    95
      val {model, where_, where_rls,...} = Problem.from_store (Ctree.get_ctxt pt pos) pI
walther@59763
    96
	    val pbl = 
walther@59903
    97
	      if pI' = Problem.id_empty andalso pI = Problem.id_empty
Walther@60585
    98
	      then (false, (I_Model.init model, []))
Walther@60590
    99
	      else M_Match.match_itms_oris ctxt probl (model, where_, where_rls) oris
walther@59763
   100
	      (*FIXXXME~~~~~~~~~~~~~~~: take pbl and compare with new pI WN.8.03*)
walther@59810
   101
	    val (c, pt) =
walther@59933
   102
	      case Specify_Step.add (Tactic.Specify_Problem' (pI, pbl)) (Istate_Def.Uistate, ctxt) (pt, pos) of
walther@59810
   103
  	      ((_, Pbl), c, _, pt) => (c, pt)
walther@59962
   104
  	    | _ => raise ERROR ""
walther@59763
   105
    in
walther@60020
   106
      ("ok", ([(Tactic.Specify_Problem pI, Tactic.Specify_Problem' (pI, pbl),
walther@60020
   107
        (pos, (Istate_Def.Uistate, ContextC.empty)))], c, (pt, pos)))
walther@59763
   108
    end
walther@60014
   109
  | by_tactic_input (Tactic.Specify_Method id) (pt, pos) =
walther@59763
   110
    let
walther@60014
   111
      val (o_model, ctxt, i_model) = Specify_Step.complete_for id (pt, pos)
walther@60014
   112
      val (pos, _, _, pt) = Specify_Step.add (Tactic.Specify_Method' (id, o_model, i_model))
walther@60014
   113
        (Istate_Def.Uistate, ctxt) (pt, pos)
walther@60011
   114
    in
walther@60020
   115
      ("ok", ([(Tactic.Specify_Method id, Tactic.Specify_Method' (id, o_model, i_model),
walther@60020
   116
        (pos, (Istate_Def.Uistate, ContextC.empty)))], [], (pt, pos)))
walther@59763
   117
    end    
walther@59806
   118
  | by_tactic_input (Tactic.Specify_Theory dI) (pt, pos as (_, Pbl)) =
walther@59763
   119
    let
walther@59763
   120
      val ctxt = get_ctxt pt pos
walther@60014
   121
	    val (pos, c, _, pt) =            
walther@59933
   122
	      Specify_Step.add (Tactic.Specify_Theory' dI)  (Istate_Def.Uistate, ctxt) (pt, pos)
walther@59763
   123
    in (*FIXXXME: check if pbl can still be parsed*)
walther@60020
   124
	    ("ok", ([(Tactic.Specify_Theory dI, Tactic.Specify_Theory' dI,
walther@60020
   125
        (pos, (Istate_Def.Uistate, ctxt)))], c, (pt, pos)))
walther@59763
   126
    end
walther@59806
   127
  | by_tactic_input (Tactic.Specify_Theory dI) (pt, pos as (_, Met)) =
walther@59763
   128
    let
walther@59763
   129
      val ctxt = get_ctxt pt pos
walther@59933
   130
	    val (pos, c, _, pt) = Specify_Step.add (Tactic.Specify_Theory' dI) (Istate_Def.Uistate, ctxt) (pt, pos)
walther@59763
   131
    in  (*FIXXXME: check if met can still be parsed*)
walther@60020
   132
	    ("ok", ([(Tactic.Specify_Theory dI, Tactic.Specify_Theory' dI,
walther@60020
   133
        (pos, (Istate_Def.Uistate, ctxt)))], c, (pt, pos)))
walther@59763
   134
    end
Walther@60628
   135
  | by_tactic_input m' (ctree, pos) =
Walther@60628
   136
    let
Walther@60628
   137
      val ctxt = Ctree.get_ctxt ctree pos
Walther@60628
   138
    in
Walther@60628
   139
      raise ERROR ("by_tactic_input: not impl. for " ^ Tactic.input_to_string ctxt m')
Walther@60628
   140
    end
walther@60020
   141
(**)
walther@60020
   142
walther@59806
   143
walther@60015
   144
fun by_tactic (Tactic.Model_Problem' (id, pbl, met)) (pt, pos)  =
walther@59806
   145
    let 
walther@60015
   146
      val ((p, _), _, _, pt) = Specify_Step.add (Tactic.Model_Problem'(id, pbl, met))
walther@60015
   147
        (Istate_Def.Uistate, ContextC.empty) (pt, pos)
walther@60015
   148
(* deprecated^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *)
walther@59806
   149
    in
walther@59806
   150
      ("ok", ([], [], (pt, (p, Pbl))))
walther@59806
   151
    end
walther@59806
   152
    (* called only if no_met is specified *)     
walther@59806
   153
  | by_tactic (Tactic.Refine_Tacitly' (pI, pIre, _, _, _)) (pt, pos) =
walther@59806
   154
      let
Walther@60585
   155
        val {solve_mets, thy,...} = Problem.from_store (Ctree.get_ctxt pt pos) pIre
Walther@60585
   156
        val (domID, metID) = (Context.theory_name thy, 
Walther@60585
   157
          if length solve_mets = 0 then MethodC.id_empty 
Walther@60585
   158
          else hd solve_mets)
walther@59806
   159
        val ((p,_), _, _, pt) = 
walther@59933
   160
	        Specify_Step.add (Tactic.Refine_Tacitly' (pI, pIre, domID, metID, [(*pbl*)]))
walther@59846
   161
            (Istate_Def.Uistate, ContextC.empty) (pt, pos)
walther@59806
   162
(* deprecated^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *)
walther@59806
   163
      in 
walther@59806
   164
        ("ok", ([], [], (pt,(p, Pbl))))
walther@59806
   165
      end
walther@60021
   166
  | by_tactic (Tactic.Refine_Problem' (rfd as (id, _))) (pt, pos)  =
walther@59806
   167
      let
walther@59806
   168
        val ctxt = get_ctxt pt pos
walther@59806
   169
        val (pos, _, _, pt) =
walther@59933
   170
          Specify_Step.add (Tactic.Refine_Problem' rfd) (Istate_Def.Uistate, ctxt) (pt, pos)
walther@59806
   171
      in
walther@60021
   172
        ("ok", ([(Tactic.Refine_Problem id, Tactic.Refine_Problem' rfd,
walther@60021
   173
            (pos, (Istate_Def.Uistate,ctxt)))], [], (pt, pos)))
walther@59806
   174
      end
Walther@60586
   175
  | by_tactic (Tactic.Specify_Problem' (pI, (ok, (itms, where_)))) (pt, pos as (p, _)) =
walther@59806
   176
      let
walther@59995
   177
        val (_, _, _, _, _, _, ctxt, _) = case get_obj I pt p of
walther@59806
   178
          PblObj {origin= (oris, (dI', pI', mI'), _), spec= (dI, _, mI), ctxt, meth = met, ...} =>
walther@59806
   179
            (oris, dI', pI', mI', dI, mI, ctxt, met)
walther@59962
   180
        | _ => raise ERROR "Step_Solve.by_tactic (Specify_Problem': uncovered case get_obj"
walther@59806
   181
        val (p, pt) =
Walther@60586
   182
          case  Specify_Step.add (Tactic.Specify_Problem' (pI, (ok, (itms, where_)))) (Istate_Def.Uistate, ctxt) (pt, pos) of
walther@59806
   183
            ((p, Pbl), _, _, pt) => (p, pt)
walther@59962
   184
          | _ => raise ERROR "Step_Solve.by_tactic (Specify_Problem': uncovered case generate1 (WARNING WHY ?)"
walther@59806
   185
      in
walther@59806
   186
        ("ok", ([], [], (pt, (p, Pbl))))
walther@59806
   187
      end    
walther@60014
   188
  | by_tactic (Tactic.Specify_Method' (id, _, _)) (pt, pos) =
walther@59806
   189
      let
walther@60014
   190
        val (o_model, ctxt, i_model) = Specify_Step.complete_for id (pt, pos)
walther@60014
   191
        val (pos, _, _, pt) = Specify_Step.add (Tactic.Specify_Method' (id, o_model, i_model))
walther@60014
   192
          (Istate_Def.Uistate, ctxt) (pt, pos)
walther@60011
   193
      in
walther@59806
   194
        ("ok", ([], [], (pt, pos)))
walther@60014
   195
      end
walther@60016
   196
  | by_tactic (Tactic.Add_Given' (ct, _)) (pt, p)  = Specify.by_Add_ "#Given" ct (pt, p)
walther@60016
   197
  | by_tactic (Tactic.Add_Find'  (ct, _)) (pt, p) = Specify.by_Add_ "#Find" ct (pt, p)
walther@60016
   198
  | by_tactic (Tactic.Add_Relation' (ct, _)) (pt, p) = Specify.by_Add_"#Relate" ct (pt, p)
walther@60020
   199
    (*strange old code, redes*)
walther@60015
   200
  | by_tactic (Tactic.Specify_Theory' domID) (pt, (p, p_)) =
walther@59806
   201
      let
walther@59806
   202
        val p_ = case p_ of Met => Met | _ => Pbl
walther@60015
   203
        val {spec = (dI, _, _), ctxt, ...} = Calc.specify_data (pt, (p, p_))
walther@59806
   204
      in
walther@60015
   205
        if domID = dI then
walther@60015
   206
          ("ok", ([], [], (pt, (p, p_))))
Walther@60586
   207
        else (*FIXME: check model wrt. (new!) domID ..? still parsable?*)
walther@59806
   208
	        let 
walther@60015
   209
	          val ((p, p_), _, _, pt) = Specify_Step.add (Tactic.Specify_Theory' domID)
walther@60015
   210
	            (Istate_Def.Uistate, ctxt) (pt, (p, p_))
walther@59806
   211
	        in
walther@59806
   212
            ("ok", ([], [], (pt, (p, p_))))
walther@59990
   213
          end                
walther@59806
   214
      end
walther@59806
   215
  | by_tactic _ _ = raise ERROR "Step_Specify.by_tactic uncovered pattern in fun.def"
walther@59806
   216
Walther@60556
   217
(* create a calc-tree with oris via a cas.refined pbl *)
Walther@60559
   218
(*  initialise <-?-> nxt_specify_init_calc *)
Walther@60576
   219
fun initialise ctxt (fmz, (dI, pI, mI)) = 
walther@60150
   220
    let
Walther@60576
   221
	    val thy = ThyC.get_theory_PIDE ctxt dI
Walther@60576
   222
	    val ctxt = Proof_Context.init_global thy (* ctxt restricted to Formalise *)
walther@60150
   223
	    val (pI, (pors, pctxt), mI) = 
walther@60150
   224
	      if mI = ["no_met"] 
walther@60150
   225
	      then 
walther@60150
   226
          let 
Walther@60585
   227
            val pors = Problem.from_store ctxt pI |> #model |> O_Model.init thy fmz; (*..TermC.parseNEW'*)
walther@60150
   228
            val pctxt = ContextC.initialise' thy fmz;                (*..DUPLICATE ermC.parseNEW'*)
Walther@60559
   229
		        val pI' = Refine.refine_ori' pctxt pors pI;
walther@60150
   230
		      in (pI', (pors (*refinement over models with diff.precond only*), pctxt),
Walther@60585
   231
		        (hd o #solve_mets o Problem.from_store ctxt) pI')
walther@60150
   232
		      end
walther@60150
   233
	      else
walther@60150
   234
	        let
Walther@60585
   235
	          val pors = Problem.from_store ctxt pI |> #model |> O_Model.init thy fmz; (*..TermC.parseNEW'*)
walther@60150
   236
            val pctxt = ContextC.initialise' thy fmz;                (*..DUPLICATE ermC.parseNEW'*)
walther@60150
   237
	        in (pI, (pors, pctxt), mI) end;
Walther@60585
   238
	    val {cas, model, thy = thy', ...} = Problem.from_store ctxt pI (*take dI from _refined_ pbl*)
Walther@60609
   239
	    val dI = Context.theory_name (Sub_Problem.common_sub_thy (thy, thy'))
walther@60150
   240
	    val hdl = case cas of
Walther@60559
   241
		    NONE => Auto_Prog.pblterm dI pI
Walther@60585
   242
		  | SOME t => subst_atomic ((Model_Pattern.variables model) ~~~ O_Model.values pors) t
walther@60113
   243
	    val hdlPIDE = case cas of
Walther@60559
   244
		    NONE => Auto_Prog.pblterm dI pI
Walther@60585
   245
		  | SOME t => subst_atomic ((Model_Pattern.variables model) ~~~ O_Model.values pors) t
walther@60111
   246
    in
walther@60114
   247
      (hdlPIDE, hdl, (dI, pI, mI), pors, pctxt)
walther@60111
   248
    end;
Walther@60559
   249
(*TODO: HOW RELATES nxt_specify_init_calc \<longleftrightarrow> initialise *)
Walther@60557
   250
fun nxt_specify_init_calc ctxt ([], (dI, pI, mI)) = (*this will probably be dropped with PIDE*)
Walther@60557
   251
    if pI <> []
walther@59806
   252
    then (* from pbl-browser or from CAS cmd with pI=[e_pblID] *)
Walther@60556
   253
	    let
Walther@60585
   254
        val {cas, solve_mets, model, thy, ...} = Problem.from_store ctxt pI
walther@59880
   255
	      val dI = if dI = "" then Context.theory_name thy else dI
Walther@60585
   256
	      val mI = if mI = [] then hd solve_mets else mI
Walther@60559
   257
	      val hdl = case cas of NONE => Auto_Prog.pblterm dI pI | SOME t => t
walther@59952
   258
	      val (pt, _) = cappend_problem e_ctree [] (Istate_Def.Uistate, ContextC.empty) ([], (dI, pI, mI))
walther@59846
   259
				  ([], (dI,pI,mI), hdl, ContextC.empty)
walther@59806
   260
	      val pt = update_spec pt [] (dI, pI, mI)
Walther@60585
   261
	      val pits = I_Model.init model
walther@59806
   262
	      val pt = update_pbl pt [] pits
walther@59806
   263
	    in ((pt, ([] , Pbl)), []) end
walther@59806
   264
    else 
walther@59806
   265
      if mI <> [] 
walther@59806
   266
      then (* from met-browser *)
Walther@60557
   267
	      let
Walther@60586
   268
          val {model, ...} = MethodC.from_store ctxt mI
walther@59806
   269
	        val dI = if dI = "" then "Isac_Knowledge" else dI
walther@59846
   270
	        val (pt, _) = cappend_problem e_ctree [] (Istate_Def.empty, ContextC.empty)
walther@59861
   271
	          ([], (dI, pI, mI)) ([], (dI, pI, mI), TermC.empty, ContextC.empty)
walther@59806
   272
	        val pt = update_spec pt [] (dI, pI, mI)
Walther@60586
   273
	        val mits = I_Model.init model
walther@59806
   274
	        val pt = update_met pt [] mits
walther@59806
   275
	      in ((pt, ([], Met)), []) end
walther@59806
   276
      else (* new example, pepare for interactive modeling *)
walther@59806
   277
	      let
walther@59846
   278
	        val (pt, _) = cappend_problem e_ctree [] (Istate_Def.empty, ContextC.empty) 
walther@59976
   279
	          ([], References.empty) ([], References.empty, TermC.empty, ContextC.empty)
walther@59806
   280
	      in ((pt, ([], Pbl)), []) end
Walther@60576
   281
  | nxt_specify_init_calc ctxt (model, (dI, pI, mI)) = 
walther@60111
   282
    let            (* both ^^^  ^^^^^^^^^^^^  are either empty or complete *)
Walther@60576
   283
	    val (_, hdl, (dI, pI, mI), pors, pctxt) = initialise ctxt (model, (dI, pI, mI))
walther@59846
   284
      val (pt, _) = cappend_problem e_ctree [] (Istate_Def.empty, pctxt)
Walther@60576
   285
        (model, (dI, pI, mI)) (pors, (dI, pI, mI), hdl, pctxt)
walther@59806
   286
    in
walther@60020
   287
      ((pt, ([], Pbl)), (fst3 o snd) (by_tactic_input Tactic.Model_Problem (pt, ([], Pbl))))
walther@59806
   288
    end
walther@59763
   289
walther@59763
   290
(**)end(**)