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