src/Tools/isac/Specify/step-specify.sml
author wneuper <Walther.Neuper@jku.at>
Wed, 25 Jan 2023 17:51:52 +0100
changeset 60652 75003e8f96ab
parent 60651 b7a2ad3b3d45
child 60653 fff1c0f0a9e7
permissions -rw-r--r--
use exclusively new Step_Specify.initialise / initialise'
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@60652
    12
  val initialise: theory -> Formalise.T ->
walther@60114
    13
    term * term * References.T * O_Model.T * Proof.context
Walther@60652
    14
  val initialise': theory -> 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@60638
    79
      case Refine.problem (ThyC.get_theory_PIDE ctxt 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@60652
   218
(*  initialise <-?-> initialise' *)
Walther@60652
   219
fun initialise thy (fmz, (_, pI, mI)) = 
walther@60150
   220
    let
Walther@60576
   221
	    val ctxt = Proof_Context.init_global thy (* ctxt restricted to Formalise *)
walther@60150
   222
	    val (pI, (pors, pctxt), mI) = 
walther@60150
   223
	      if mI = ["no_met"] 
walther@60150
   224
	      then 
walther@60150
   225
          let 
Walther@60652
   226
            val (pors, pctxt) = Problem.from_store ctxt pI |> #model |> O_Model.init_PIDE thy fmz;
Walther@60651
   227
            val pI' = Refine.refine_ori' pctxt pors pI;
Walther@60651
   228
          in (pI', (pors (*refinement over models with diff.precond only*), pctxt),
Walther@60651
   229
            (hd o #solve_mets o Problem.from_store ctxt) pI')
Walther@60651
   230
          end
Walther@60651
   231
	      else
Walther@60651
   232
	        let
Walther@60652
   233
            val (pors, pctxt) = Problem.from_store ctxt pI |> #model |> O_Model.init_PIDE thy fmz;
Walther@60651
   234
          in (pI, (pors, pctxt), mI) end;
Walther@60651
   235
	    val {cas, model, thy = thy', ...} = Problem.from_store ctxt pI (*take dI from _refined_ pbl*)
Walther@60651
   236
	    val dI = Context.theory_name (Sub_Problem.common_sub_thy (thy, thy'))
Walther@60651
   237
	    val hdl = case cas of
Walther@60651
   238
		    NONE => Auto_Prog.pblterm dI pI
Walther@60651
   239
		  | SOME t => subst_atomic ((Model_Pattern.variables model) ~~~ O_Model.values pors) t
Walther@60651
   240
	    val hdlPIDE = case cas of
Walther@60651
   241
		    NONE => Auto_Prog.pblterm dI pI
Walther@60651
   242
		  | SOME t => subst_atomic ((Model_Pattern.variables model) ~~~ O_Model.values pors) t
Walther@60651
   243
    in
Walther@60651
   244
      (hdlPIDE, hdl, (dI, pI, mI), pors, pctxt)
Walther@60651
   245
    end;
Walther@60652
   246
(*TODO: HOW RELATES initialise' \<longleftrightarrow> initialise *)
Walther@60652
   247
fun initialise' thy ([], (dI, pI, mI)) = (*this will probably be dropped with PIDE*)
Walther@60651
   248
    if pI <> []
Walther@60651
   249
    then (* from pbl-browser or from CAS cmd with pI=[e_pblID] *)
Walther@60651
   250
	    let
Walther@60651
   251
        val {cas, solve_mets, model, thy, ...} = Problem.from_store (Proof_Context.init_global thy) pI
Walther@60651
   252
	      val dI = if dI = "" then Context.theory_name thy else dI
Walther@60651
   253
	      val mI = if mI = [] then hd solve_mets else mI
Walther@60651
   254
	      val hdl = case cas of NONE => Auto_Prog.pblterm dI pI | SOME t => t
Walther@60651
   255
	      val (pt, _) = cappend_problem e_ctree [] (Istate_Def.Uistate, ContextC.empty) ([], (dI, pI, mI))
Walther@60651
   256
				  ([], (dI,pI,mI), hdl, ContextC.empty)
Walther@60651
   257
	      val pt = update_spec pt [] (dI, pI, mI)
Walther@60651
   258
	      val pits = I_Model.init model
Walther@60651
   259
	      val pt = update_pbl pt [] pits
Walther@60651
   260
	    in ((pt, ([] , Pbl)), []) end
Walther@60651
   261
    else 
Walther@60651
   262
      if mI <> [] 
Walther@60651
   263
      then (* from met-browser *)
Walther@60651
   264
	      let
Walther@60651
   265
          val {model, ...} = MethodC.from_store (Proof_Context.init_global thy) mI
Walther@60651
   266
	        val dI = if dI = "" then "Isac_Knowledge" else dI
Walther@60651
   267
	        val (pt, _) = cappend_problem e_ctree [] (Istate_Def.empty, ContextC.empty)
Walther@60651
   268
	          ([], (dI, pI, mI)) ([], (dI, pI, mI), TermC.empty, ContextC.empty)
Walther@60651
   269
	        val pt = update_spec pt [] (dI, pI, mI)
Walther@60651
   270
	        val mits = I_Model.init model
Walther@60651
   271
	        val pt = update_met pt [] mits
Walther@60651
   272
	      in ((pt, ([], Met)), []) end
Walther@60651
   273
      else (* new example, pepare for interactive modeling *)
Walther@60651
   274
	      let
Walther@60651
   275
	        val (pt, _) = cappend_problem e_ctree [] (Istate_Def.empty, ContextC.empty) 
Walther@60651
   276
	          ([], References.empty) ([], References.empty, TermC.empty, ContextC.empty)
Walther@60651
   277
	      in ((pt, ([], Pbl)), []) end
Walther@60652
   278
  | initialise' thy (model, refs) = 
Walther@60651
   279
    let            (* both          ^^^^^  ^^^^^^^^^^^^  are either empty or complete *)
Walther@60652
   280
	    val (_, hdl, refs, pors, pctxt) = initialise thy (model, refs)
Walther@60651
   281
      val (pt, _) = cappend_problem e_ctree [] (Istate_Def.empty, pctxt)
Walther@60651
   282
        (model, refs) (pors, refs, hdl, pctxt)
Walther@60651
   283
    in
Walther@60651
   284
      ((pt, ([], Pbl)), (fst3 o snd) (by_tactic_input Tactic.Model_Problem (pt, ([], Pbl))))
Walther@60651
   285
    end
Walther@60651
   286
 
walther@59763
   287
(**)end(**)