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