src/Tools/isac/Specify/step-specify.sml
author wneuper <Walther.Neuper@jku.at>
Sun, 23 Oct 2022 16:08:27 +0200
changeset 60576 11dd56e2a6b8
parent 60559 aba19e46dd84
child 60578 baf06b1b2aaa
permissions -rw-r--r--
follow up 6a: eliminate Thy_Inof.get_thoery for Minisubplb/100-init-rootpbl.sml independent -- src only, rollback
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@60559
    31
      val mpc = (#ppc o MethodC.from_store ctxt) mI (* just for reuse I_Model.complete_method *)
Walther@60559
    32
      val {cas, ppc, ...} = Problem.from_store (Ctree.get_ctxt pt pos) pI
walther@59958
    33
      val pbl = I_Model.init ppc (* 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@59988
    38
  		| _ => I_Model.complete_method (oris, mpc, ppc, 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@60559
    60
            val {met, ...} = Problem.from_store (Ctree.get_ctxt pt pos) pI'
walther@59763
    61
	          (*val pt = update_pbl pt p pbl ..done by Model_Problem*)
walther@60154
    62
	          val mI = if length met = 0 then MethodC.id_empty else hd met
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@59763
    91
      val (oris, dI, dI', pI', probl, ctxt) = case get_obj I pt p of
walther@59763
    92
        PblObj {origin = (oris, (dI,_,_),_), spec = (dI',pI',_), probl, ctxt, ...} =>
walther@59763
    93
          (oris, dI, dI', pI', probl, ctxt)
walther@59962
    94
      | _ => raise ERROR ""
walther@59881
    95
	    val thy = ThyC.get_theory (if dI' = ThyC.id_empty then dI else dI');
Walther@60559
    96
      val {ppc,where_,prls,...} = Problem.from_store (Ctree.get_ctxt pt pos) pI
walther@59763
    97
	    val pbl = 
walther@59903
    98
	      if pI' = Problem.id_empty andalso pI = Problem.id_empty
walther@59958
    99
	      then (false, (I_Model.init ppc, []))
walther@59984
   100
	      else M_Match.match_itms_oris thy probl (ppc,where_,prls) oris
walther@59763
   101
	      (*FIXXXME~~~~~~~~~~~~~~~: take pbl and compare with new pI WN.8.03*)
walther@59810
   102
	    val (c, pt) =
walther@59933
   103
	      case Specify_Step.add (Tactic.Specify_Problem' (pI, pbl)) (Istate_Def.Uistate, ctxt) (pt, pos) of
walther@59810
   104
  	      ((_, Pbl), c, _, pt) => (c, pt)
walther@59962
   105
  	    | _ => raise ERROR ""
walther@59763
   106
    in
walther@60020
   107
      ("ok", ([(Tactic.Specify_Problem pI, Tactic.Specify_Problem' (pI, pbl),
walther@60020
   108
        (pos, (Istate_Def.Uistate, ContextC.empty)))], c, (pt, pos)))
walther@59763
   109
    end
walther@60014
   110
  | by_tactic_input (Tactic.Specify_Method id) (pt, pos) =
walther@59763
   111
    let
walther@60014
   112
      val (o_model, ctxt, i_model) = Specify_Step.complete_for id (pt, pos)
walther@60014
   113
      val (pos, _, _, pt) = Specify_Step.add (Tactic.Specify_Method' (id, o_model, i_model))
walther@60014
   114
        (Istate_Def.Uistate, ctxt) (pt, pos)
walther@60011
   115
    in
walther@60020
   116
      ("ok", ([(Tactic.Specify_Method id, Tactic.Specify_Method' (id, o_model, i_model),
walther@60020
   117
        (pos, (Istate_Def.Uistate, ContextC.empty)))], [], (pt, pos)))
walther@59763
   118
    end    
walther@59806
   119
  | by_tactic_input (Tactic.Specify_Theory dI) (pt, pos as (_, Pbl)) =
walther@59763
   120
    let
walther@59763
   121
      val ctxt = get_ctxt pt pos
walther@60014
   122
	    val (pos, c, _, pt) =            
walther@59933
   123
	      Specify_Step.add (Tactic.Specify_Theory' dI)  (Istate_Def.Uistate, ctxt) (pt, pos)
walther@59763
   124
    in (*FIXXXME: check if pbl can still be parsed*)
walther@60020
   125
	    ("ok", ([(Tactic.Specify_Theory dI, Tactic.Specify_Theory' dI,
walther@60020
   126
        (pos, (Istate_Def.Uistate, ctxt)))], c, (pt, pos)))
walther@59763
   127
    end
walther@59806
   128
  | by_tactic_input (Tactic.Specify_Theory dI) (pt, pos as (_, Met)) =
walther@59763
   129
    let
walther@59763
   130
      val ctxt = get_ctxt pt pos
walther@59933
   131
	    val (pos, c, _, pt) = Specify_Step.add (Tactic.Specify_Theory' dI) (Istate_Def.Uistate, ctxt) (pt, pos)
walther@59763
   132
    in  (*FIXXXME: check if met can still be parsed*)
walther@60020
   133
	    ("ok", ([(Tactic.Specify_Theory dI, Tactic.Specify_Theory' dI,
walther@60020
   134
        (pos, (Istate_Def.Uistate, ctxt)))], c, (pt, pos)))
walther@59763
   135
    end
walther@59962
   136
  | by_tactic_input m' _ = raise ERROR ("by_tactic_input: not impl. for " ^ Tactic.input_to_string m')
walther@60020
   137
(**)
walther@60020
   138
walther@59806
   139
walther@60015
   140
fun by_tactic (Tactic.Model_Problem' (id, pbl, met)) (pt, pos)  =
walther@59806
   141
    let 
walther@60015
   142
      val ((p, _), _, _, pt) = Specify_Step.add (Tactic.Model_Problem'(id, pbl, met))
walther@60015
   143
        (Istate_Def.Uistate, ContextC.empty) (pt, pos)
walther@60015
   144
(* deprecated^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *)
walther@59806
   145
    in
walther@59806
   146
      ("ok", ([], [], (pt, (p, Pbl))))
walther@59806
   147
    end
walther@59806
   148
    (* called only if no_met is specified *)     
walther@59806
   149
  | by_tactic (Tactic.Refine_Tacitly' (pI, pIre, _, _, _)) (pt, pos) =
walther@59806
   150
      let
Walther@60559
   151
        val {met, thy,...} = Problem.from_store (Ctree.get_ctxt pt pos) pIre
walther@60154
   152
        val (domID, metID) = (Context.theory_name thy, if length met = 0 then MethodC.id_empty else hd met)
walther@59806
   153
        val ((p,_), _, _, pt) = 
walther@59933
   154
	        Specify_Step.add (Tactic.Refine_Tacitly' (pI, pIre, domID, metID, [(*pbl*)]))
walther@59846
   155
            (Istate_Def.Uistate, ContextC.empty) (pt, pos)
walther@59806
   156
(* deprecated^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *)
walther@59806
   157
      in 
walther@59806
   158
        ("ok", ([], [], (pt,(p, Pbl))))
walther@59806
   159
      end
walther@60021
   160
  | by_tactic (Tactic.Refine_Problem' (rfd as (id, _))) (pt, pos)  =
walther@59806
   161
      let
walther@59806
   162
        val ctxt = get_ctxt pt pos
walther@59806
   163
        val (pos, _, _, pt) =
walther@59933
   164
          Specify_Step.add (Tactic.Refine_Problem' rfd) (Istate_Def.Uistate, ctxt) (pt, pos)
walther@59806
   165
      in
walther@60021
   166
        ("ok", ([(Tactic.Refine_Problem id, Tactic.Refine_Problem' rfd,
walther@60021
   167
            (pos, (Istate_Def.Uistate,ctxt)))], [], (pt, pos)))
walther@59806
   168
      end
walther@59810
   169
  | by_tactic (Tactic.Specify_Problem' (pI, (ok, (itms, pre)))) (pt, pos as (p, _)) =
walther@59806
   170
      let
walther@59995
   171
        val (_, _, _, _, _, _, ctxt, _) = case get_obj I pt p of
walther@59806
   172
          PblObj {origin= (oris, (dI', pI', mI'), _), spec= (dI, _, mI), ctxt, meth = met, ...} =>
walther@59806
   173
            (oris, dI', pI', mI', dI, mI, ctxt, met)
walther@59962
   174
        | _ => raise ERROR "Step_Solve.by_tactic (Specify_Problem': uncovered case get_obj"
walther@59806
   175
        val (p, pt) =
walther@59933
   176
          case  Specify_Step.add (Tactic.Specify_Problem' (pI, (ok, (itms, pre)))) (Istate_Def.Uistate, ctxt) (pt, pos) of
walther@59806
   177
            ((p, Pbl), _, _, pt) => (p, pt)
walther@59962
   178
          | _ => raise ERROR "Step_Solve.by_tactic (Specify_Problem': uncovered case generate1 (WARNING WHY ?)"
walther@59806
   179
      in
walther@59806
   180
        ("ok", ([], [], (pt, (p, Pbl))))
walther@59806
   181
      end    
walther@60014
   182
  | by_tactic (Tactic.Specify_Method' (id, _, _)) (pt, pos) =
walther@59806
   183
      let
walther@60014
   184
        val (o_model, ctxt, i_model) = Specify_Step.complete_for id (pt, pos)
walther@60014
   185
        val (pos, _, _, pt) = Specify_Step.add (Tactic.Specify_Method' (id, o_model, i_model))
walther@60014
   186
          (Istate_Def.Uistate, ctxt) (pt, pos)
walther@60011
   187
      in
walther@59806
   188
        ("ok", ([], [], (pt, pos)))
walther@60014
   189
      end
walther@60016
   190
  | by_tactic (Tactic.Add_Given' (ct, _)) (pt, p)  = Specify.by_Add_ "#Given" ct (pt, p)
walther@60016
   191
  | by_tactic (Tactic.Add_Find'  (ct, _)) (pt, p) = Specify.by_Add_ "#Find" ct (pt, p)
walther@60016
   192
  | by_tactic (Tactic.Add_Relation' (ct, _)) (pt, p) = Specify.by_Add_"#Relate" ct (pt, p)
walther@60020
   193
    (*strange old code, redes*)
walther@60015
   194
  | by_tactic (Tactic.Specify_Theory' domID) (pt, (p, p_)) =
walther@59806
   195
      let
walther@59806
   196
        val p_ = case p_ of Met => Met | _ => Pbl
walther@60015
   197
        val {spec = (dI, _, _), ctxt, ...} = Calc.specify_data (pt, (p, p_))
walther@59806
   198
      in
walther@60015
   199
        if domID = dI then
walther@60015
   200
          ("ok", ([], [], (pt, (p, p_))))
walther@59806
   201
        else (*FIXME: check ppc wrt. (new!) domID ..? still parsable?*)
walther@59806
   202
	        let 
walther@60015
   203
	          val ((p, p_), _, _, pt) = Specify_Step.add (Tactic.Specify_Theory' domID)
walther@60015
   204
	            (Istate_Def.Uistate, ctxt) (pt, (p, p_))
walther@59806
   205
	        in
walther@59806
   206
            ("ok", ([], [], (pt, (p, p_))))
walther@59990
   207
          end                
walther@59806
   208
      end
walther@59806
   209
  | by_tactic _ _ = raise ERROR "Step_Specify.by_tactic uncovered pattern in fun.def"
walther@59806
   210
Walther@60556
   211
(* create a calc-tree with oris via a cas.refined pbl *)
Walther@60559
   212
(*  initialise <-?-> nxt_specify_init_calc *)
Walther@60576
   213
fun initialise ctxt (fmz, (dI, pI, mI)) = 
walther@60150
   214
    let
Walther@60576
   215
	    val thy = ThyC.get_theory_PIDE ctxt dI
Walther@60576
   216
	    val ctxt = Proof_Context.init_global thy (* ctxt restricted to Formalise *)
walther@60150
   217
	    val (pI, (pors, pctxt), mI) = 
walther@60150
   218
	      if mI = ["no_met"] 
walther@60150
   219
	      then 
walther@60150
   220
          let 
Walther@60559
   221
            val pors = Problem.from_store ctxt pI |> #ppc |> O_Model.init thy fmz; (*..TermC.parseNEW'*)
walther@60150
   222
            val pctxt = ContextC.initialise' thy fmz;                (*..DUPLICATE ermC.parseNEW'*)
Walther@60559
   223
		        val pI' = Refine.refine_ori' pctxt pors pI;
walther@60150
   224
		      in (pI', (pors (*refinement over models with diff.precond only*), pctxt),
Walther@60559
   225
		        (hd o #met o Problem.from_store ctxt) pI')
walther@60150
   226
		      end
walther@60150
   227
	      else
walther@60150
   228
	        let
Walther@60559
   229
	          val pors = Problem.from_store ctxt pI |> #ppc |> O_Model.init thy fmz; (*..TermC.parseNEW'*)
walther@60150
   230
            val pctxt = ContextC.initialise' thy fmz;                (*..DUPLICATE ermC.parseNEW'*)
walther@60150
   231
	        in (pI, (pors, pctxt), mI) end;
Walther@60559
   232
	    val {cas, ppc, thy = thy', ...} = Problem.from_store ctxt pI (*take dI from _refined_ pbl*)
walther@60150
   233
	    val dI = Context.theory_name (ThyC.sub_common (thy, thy'))
walther@60150
   234
	    val hdl = case cas of
Walther@60559
   235
		    NONE => Auto_Prog.pblterm dI pI
walther@60150
   236
		  | SOME t => subst_atomic ((Model_Pattern.variables ppc) ~~~ O_Model.values pors) t
walther@60113
   237
	    val hdlPIDE = case cas of
Walther@60559
   238
		    NONE => Auto_Prog.pblterm dI pI
walther@60113
   239
		  | SOME t => subst_atomic ((Model_Pattern.variables ppc) ~~~ O_Model.values pors) t
walther@60111
   240
    in
walther@60114
   241
      (hdlPIDE, hdl, (dI, pI, mI), pors, pctxt)
walther@60111
   242
    end;
Walther@60559
   243
(*TODO: HOW RELATES nxt_specify_init_calc \<longleftrightarrow> initialise *)
Walther@60557
   244
fun nxt_specify_init_calc ctxt ([], (dI, pI, mI)) = (*this will probably be dropped with PIDE*)
Walther@60557
   245
    if pI <> []
walther@59806
   246
    then (* from pbl-browser or from CAS cmd with pI=[e_pblID] *)
Walther@60556
   247
	    let
Walther@60559
   248
        val {cas, met, ppc, thy, ...} = Problem.from_store ctxt pI
walther@59880
   249
	      val dI = if dI = "" then Context.theory_name thy else dI
walther@59806
   250
	      val mI = if mI = [] then hd met else mI
Walther@60559
   251
	      val hdl = case cas of NONE => Auto_Prog.pblterm dI pI | SOME t => t
walther@59952
   252
	      val (pt, _) = cappend_problem e_ctree [] (Istate_Def.Uistate, ContextC.empty) ([], (dI, pI, mI))
walther@59846
   253
				  ([], (dI,pI,mI), hdl, ContextC.empty)
walther@59806
   254
	      val pt = update_spec pt [] (dI, pI, mI)
walther@59958
   255
	      val pits = I_Model.init ppc
walther@59806
   256
	      val pt = update_pbl pt [] pits
walther@59806
   257
	    in ((pt, ([] , Pbl)), []) end
walther@59806
   258
    else 
walther@59806
   259
      if mI <> [] 
walther@59806
   260
      then (* from met-browser *)
Walther@60557
   261
	      let
Walther@60559
   262
          val {ppc, ...} = MethodC.from_store ctxt mI
walther@59806
   263
	        val dI = if dI = "" then "Isac_Knowledge" else dI
walther@59846
   264
	        val (pt, _) = cappend_problem e_ctree [] (Istate_Def.empty, ContextC.empty)
walther@59861
   265
	          ([], (dI, pI, mI)) ([], (dI, pI, mI), TermC.empty, ContextC.empty)
walther@59806
   266
	        val pt = update_spec pt [] (dI, pI, mI)
walther@59958
   267
	        val mits = I_Model.init ppc
walther@59806
   268
	        val pt = update_met pt [] mits
walther@59806
   269
	      in ((pt, ([], Met)), []) end
walther@59806
   270
      else (* new example, pepare for interactive modeling *)
walther@59806
   271
	      let
walther@59846
   272
	        val (pt, _) = cappend_problem e_ctree [] (Istate_Def.empty, ContextC.empty) 
walther@59976
   273
	          ([], References.empty) ([], References.empty, TermC.empty, ContextC.empty)
walther@59806
   274
	      in ((pt, ([], Pbl)), []) end
Walther@60576
   275
  | nxt_specify_init_calc ctxt (model, (dI, pI, mI)) = 
walther@60111
   276
    let            (* both ^^^  ^^^^^^^^^^^^  are either empty or complete *)
Walther@60576
   277
	    val (_, hdl, (dI, pI, mI), pors, pctxt) = initialise ctxt (model, (dI, pI, mI))
walther@59846
   278
      val (pt, _) = cappend_problem e_ctree [] (Istate_Def.empty, pctxt)
Walther@60576
   279
        (model, (dI, pI, mI)) (pors, (dI, pI, mI), hdl, pctxt)
walther@59806
   280
    in
walther@60020
   281
      ((pt, ([], Pbl)), (fst3 o snd) (by_tactic_input Tactic.Model_Problem (pt, ([], Pbl))))
walther@59806
   282
    end
walther@59763
   283
walther@59763
   284
(**)end(**)