src/Tools/isac/MathEngBasic/ctree-access.sml
author Walther Neuper <walther.neuper@jku.at>
Mon, 04 May 2020 16:25:14 +0200
changeset 59937 c3f3123e8fbc
parent 59903 5037ca1b112b
child 59940 acfad421e656
permissions -rw-r--r--
shift code specific for specify-phase to Specify/*
wneuper@59292
     1
(* Title: read and write access to the calctree
wneuper@59292
     2
   Author: Walther Neuper 2017
wneuper@59292
     3
   (c) due to copyright terms
wneuper@59292
     4
*)
wneuper@59292
     5
signature CALC_TREE_ACCESS =
wneuper@59292
     6
sig
wneuper@59292
     7
walther@59636
     8
  val get_last_formula: CTbasic.state -> term
walther@59846
     9
  val update_branch : CTbasic.ctree -> Pos.pos -> CTbasic.branch -> CTbasic.ctree
walther@59879
    10
  val update_domID : CTbasic.ctree -> Pos.pos -> ThyC.id -> CTbasic.ctree
walther@59937
    11
  val update_met : CTbasic.ctree -> Pos.pos -> Model_Def.itm list -> CTbasic.ctree    (* =vvv= ? *)
walther@59937
    12
  val update_metppc : CTbasic.ctree -> Pos.pos -> Model_Def.itm list -> CTbasic.ctree (* =^^^= ? *)
walther@59903
    13
  val update_metID : CTbasic.ctree -> Pos.pos -> Method.id -> CTbasic.ctree
walther@59937
    14
  val update_pbl : CTbasic.ctree -> Pos.pos -> Model_Def.itm list -> CTbasic.ctree    (* =vvv= ? *)
walther@59937
    15
  val update_pblppc : CTbasic.ctree -> Pos.pos -> Model_Def.itm list -> CTbasic.ctree (* =^^^= ? *)
walther@59903
    16
  val update_pblID : CTbasic.ctree -> Pos.pos -> Problem.id -> CTbasic.ctree
walther@59937
    17
  val update_oris : CTbasic.ctree -> Pos.pos ->  Model_Def.ori list -> CTbasic.ctree
walther@59903
    18
  val update_orispec : CTbasic.ctree -> Pos.pos -> Spec.T -> CTbasic.ctree
walther@59903
    19
  val update_spec : CTbasic.ctree -> Pos.pos -> Spec.T -> CTbasic.ctree
walther@59846
    20
  val update_tac : CTbasic.ctree -> Pos.pos -> Tactic.input -> CTbasic.ctree
walther@59819
    21
walther@59836
    22
  (* original write access *)
walther@59846
    23
  val cappend_form :  CTbasic.ctree ->  Pos.pos ->  Istate_Def.T * Proof.context -> term ->
walther@59846
    24
    CTbasic.ctree *  Pos.pos' list
walther@59846
    25
  val cappend_problem : CTbasic.ctree -> Pos.pos -> Istate_Def.T * Proof.context ->
walther@59937
    26
    Celem.fmz -> Model_Def.ori list * Spec.T * term * Proof.context
walther@59846
    27
    -> CTbasic.ctree * Pos.pos' list
walther@59937
    28
  val cupdate_problem: CTbasic.ctree -> Pos.pos -> Spec.T * Model_Def.itm list * Model_Def.itm list * Proof.context
walther@59820
    29
    -> CTbasic.ctree * Pos.pos' list
wneuper@59294
    30
  val append_atomic :                                                          (* for solve.sml *)
walther@59846
    31
     Pos.pos -> ((Istate_Def.T * Proof.context) option * (Istate_Def.T * Proof.context)) ->
walther@59937
    32
       term -> Tactic.input -> Celem.result -> CTbasic.ostate -> CTbasic.ctree -> CTbasic.ctree
walther@59846
    33
  val cappend_atomic : CTbasic.ctree -> Pos.pos -> Istate_Def.T * Proof.context -> term ->
walther@59937
    34
    Tactic.input -> Celem.result -> CTbasic.ostate -> CTbasic.ctree * Pos.pos' list
walther@59846
    35
  val append_result : CTbasic.ctree -> Pos.pos -> Istate_Def.T * Proof.context ->
walther@59937
    36
    Celem.result -> CTbasic.ostate -> CTbasic.ctree * 'a list
walther@59813
    37
walther@59846
    38
  val update_loc' : CTbasic.ctree -> Pos.pos ->
walther@59806
    39
    (Istate_Def.T * Proof.context) option * (Istate_Def.T * Proof.context) option -> CTbasic.ctree
wneuper@59310
    40
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59809
    41
  (*NONE*)
walther@59886
    42
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59813
    43
  val append_form: Pos.pos -> Istate_Def.T * Proof.context -> term -> CTbasic.ctree -> CTbasic.ctree
walther@59937
    44
  val append_problem : Pos.pos -> Istate_Def.T * Proof.context -> Celem.fmz ->
walther@59937
    45
    Model_Def.ori list * Spec.T * term * Proof.context -> CTbasic.ctree -> CTbasic.ctree
walther@59937
    46
  val update_problem: Pos.pos -> Spec.T * Model_Def.itm list * Model_Def.itm list * Proof.context
walther@59820
    47
    -> CTbasic.ctree -> CTbasic.ctree
walther@59886
    48
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59292
    49
end
wneuper@59292
    50
(**)
wneuper@59292
    51
structure CTaccess(**): CALC_TREE_ACCESS(**) =
wneuper@59292
    52
struct
wneuper@59292
    53
(**)
wneuper@59292
    54
open CTbasic
walther@59694
    55
open Pos
wneuper@59292
    56
walther@59636
    57
fun get_last_formula (pt, (p, _)) =
walther@59636
    58
  let
walther@59636
    59
    val res = get_obj g_res pt p
walther@59636
    60
  in
walther@59861
    61
    if res = TermC.empty
walther@59636
    62
    then get_obj g_form pt p
walther@59636
    63
    else res
walther@59636
    64
  end
walther@59636
    65
walther@59820
    66
(** update elements of ctree; TODO: TRY TO REPLACE BY cappend_*  **)
walther@59820
    67
wneuper@59294
    68
(* for use by appl_obj *) 
walther@59840
    69
fun repl_pbl x (PblObj {origin, fmz, spec, probl = _, meth, ctxt, loc, 
wneuper@59294
    70
      branch, result, ostate}) =
walther@59840
    71
    PblObj {origin = origin, fmz = fmz, spec = spec, probl= x, meth = meth,
walther@59839
    72
      ctxt = ctxt, loc = loc, branch = branch, result = result, ostate = ostate}
wneuper@59294
    73
  | repl_pbl _ _ = raise PTREE "repl_pbl takes no PrfObj";
walther@59840
    74
fun repl_met x (PblObj {origin, fmz, spec, probl, meth = _, ctxt, loc, 
wneuper@59294
    75
      branch, result, ostate}) =
walther@59840
    76
    PblObj {origin = origin, fmz= fmz, spec = spec, probl = probl,
walther@59839
    77
	     meth = x, ctxt = ctxt, loc = loc, branch = branch, result = result,
wneuper@59294
    78
	     ostate = ostate}
wneuper@59294
    79
  | repl_met _ _ = raise PTREE "repl_pbl takes no PrfObj";
walther@59840
    80
fun repl_spec x (PblObj {origin, fmz, spec = _, probl, meth, ctxt, loc, 
wneuper@59294
    81
      branch, result, ostate}) =
walther@59840
    82
    PblObj {origin = origin, fmz = fmz, spec = x, probl = probl,
walther@59839
    83
	    meth = meth, ctxt = ctxt, loc = loc, branch = branch, result = result,
walther@59839
    84
	     ostate = ostate}                                                            
wneuper@59294
    85
  | repl_spec  _ _ = raise PTREE "repl_domID takes no PrfObj";
walther@59840
    86
fun repl_domID x (PblObj {origin, fmz, spec = (_, p, m), probl, meth, ctxt, loc, 
wneuper@59294
    87
      branch, result, ostate}) =
walther@59840
    88
    PblObj {origin = origin, fmz = fmz, spec= (x, p, m), probl = probl,
walther@59839
    89
	    meth = meth, ctxt = ctxt, loc = loc, branch = branch, result = result,
wneuper@59294
    90
	     ostate = ostate}
wneuper@59294
    91
  | repl_domID _ _ = raise PTREE "repl_domID takes no PrfObj";
walther@59840
    92
fun repl_pblID x (PblObj {origin, fmz, spec= (d, _, m), probl, meth, ctxt, loc, 
wneuper@59294
    93
      branch, result, ostate}) =
walther@59840
    94
    PblObj {origin = origin, fmz = fmz, spec= (d, x, m), probl = probl,
walther@59839
    95
	    meth = meth, ctxt = ctxt, loc = loc, branch = branch, result = result,
wneuper@59294
    96
	     ostate = ostate}
wneuper@59294
    97
  | repl_pblID _ _ = raise PTREE "repl_pblID takes no PrfObj";
walther@59840
    98
fun repl_metID x (PblObj {origin, fmz, spec = (d, p,_), probl, meth, ctxt, loc, 
wneuper@59294
    99
      branch, result, ostate}) =
walther@59840
   100
    PblObj {origin = origin, fmz = fmz, spec = (d, p, x), probl = probl,
walther@59839
   101
	    meth = meth, ctxt = ctxt, loc = loc, branch = branch, result = result,
wneuper@59294
   102
	     ostate = ostate}
wneuper@59294
   103
  | repl_metID _ _ = raise PTREE "repl_metID takes no PrfObj";
walther@59840
   104
fun repl_result l f' s (PrfObj {form, tac, loc = _, branch, result = _ , ostate = _}) =
walther@59840
   105
    PrfObj {form = form, tac = tac, loc = l, branch = branch, result = f', ostate = s}
walther@59840
   106
  | repl_result l f' s (PblObj {origin, fmz, spec, probl, meth, ctxt, loc = _,
wneuper@59294
   107
      branch, result = _ , ostate= _}) =
walther@59840
   108
    PblObj {origin = origin, fmz= fmz, spec = spec, probl = probl,
walther@59839
   109
      meth = meth, ctxt = ctxt, loc = l, branch = branch, result = f', ostate = s};
walther@59840
   110
fun repl_tac x (PrfObj {form, tac = _, loc, branch, result, ostate}) =
walther@59840
   111
    PrfObj {form = form, tac = x, loc = loc, branch = branch,
wneuper@59294
   112
      result = result, ostate = ostate}
wneuper@59294
   113
  | repl_tac _ _ = raise PTREE "repl_tac takes no PblObj";
walther@59840
   114
fun repl_ctxt x (PblObj {origin, fmz, spec, probl, meth,
walther@59839
   115
      ctxt = _, loc, branch, result, ostate}) =
walther@59840
   116
    PblObj {origin = origin, fmz = fmz, spec = spec, probl = probl,
walther@59839
   117
      meth = meth, ctxt = x, loc = loc, branch = branch, result = result,
wneuper@59294
   118
      ostate = ostate}
wneuper@59294
   119
  | repl_ctxt _ _ = raise PTREE "repl_env takes no PrfObj";
walther@59840
   120
fun repl_oris oris (PblObj {origin = (_, spe, hdf),fmz, spec, probl, meth,
walther@59839
   121
      ctxt, loc, branch, result, ostate}) =
walther@59840
   122
    PblObj {origin = (oris, spe, hdf), fmz = fmz, spec = spec, probl = probl,
walther@59839
   123
      meth = meth, ctxt = ctxt, loc = loc, branch = branch, result = result,
wneuper@59294
   124
      ostate = ostate}
wneuper@59294
   125
  | repl_oris _ _ = raise PTREE "repl_oris takes no PrfObj";
walther@59840
   126
fun repl_orispec spe (PblObj {origin = (oris, _, hdf), fmz, spec, probl, meth,
walther@59839
   127
      ctxt, loc, branch, result, ostate}) =
walther@59840
   128
    PblObj {origin = (oris, spe, hdf), fmz = fmz, spec = spec, probl = probl,
walther@59839
   129
      meth = meth, ctxt = ctxt, loc = loc, branch = branch, result = result,
wneuper@59294
   130
      ostate = ostate}
wneuper@59294
   131
  | repl_orispec _ _ = raise PTREE "repl_orispec takes no PrfObj";
walther@59840
   132
fun repl_loc l (PblObj {origin, fmz, spec, probl, meth,
walther@59839
   133
      ctxt, loc = _ , branch, result, ostate}) =
walther@59840
   134
    PblObj {origin = origin, fmz = fmz, spec = spec, probl = probl,
walther@59839
   135
      meth = meth, ctxt = ctxt, loc = l, branch = branch, result = result,
wneuper@59294
   136
      ostate = ostate}
walther@59840
   137
  | repl_loc l (PrfObj {form, tac, loc = _, branch, result, ostate}) =
walther@59840
   138
       PrfObj {form = form, tac = tac, loc= l, branch = branch, result = result,
wneuper@59294
   139
      ostate = ostate}
wneuper@59294
   140
walther@59840
   141
fun repl_branch b (PblObj {origin, fmz, spec, probl, meth, ctxt, loc, branch = _,
wneuper@59294
   142
      result, ostate}) =
walther@59840
   143
    PblObj {origin = origin, fmz = fmz, spec = spec, probl = probl,
walther@59839
   144
      meth = meth, ctxt = ctxt, loc = loc, branch = b, result = result,
wneuper@59294
   145
      ostate = ostate}
walther@59840
   146
  | repl_branch b (PrfObj {form, tac, loc, branch = _, result, ostate}) =
walther@59840
   147
    PrfObj {form = form, tac = tac, loc = loc, branch = b,
wneuper@59294
   148
      result = result, ostate = ostate};
wneuper@59294
   149
walther@59820
   150
walther@59820
   151
(** update elements of ctree; a second!!! interface for writing **)
walther@59820
   152
wneuper@59294
   153
fun update_branch pt pos x = appl_obj (repl_branch x) pt pos;
wneuper@59294
   154
fun update_domID  pt pos x = appl_obj (repl_domID  x) pt pos;
wneuper@59294
   155
fun update_met    pt pos x = appl_obj (repl_met    x) pt pos;
wneuper@59294
   156
fun update_metppc pt pos x = appl_obj (repl_met    x) pt pos;		   
wneuper@59294
   157
fun update_metID  pt pos x = appl_obj (repl_metID  x) pt pos;
wneuper@59294
   158
fun update_pbl    pt pos x = appl_obj (repl_pbl    x) pt pos;
wneuper@59294
   159
fun update_pblppc pt pos x = appl_obj (repl_pbl    x) pt pos;
wneuper@59294
   160
fun update_pblID  pt pos x = appl_obj (repl_pblID  x) pt pos;
wneuper@59294
   161
fun update_oris   pt pos x = appl_obj (repl_oris   x) pt pos;
wneuper@59294
   162
fun update_orispec pt pos x = appl_obj (repl_orispec x) pt pos;
wneuper@59294
   163
fun update_spec   pt pos x = appl_obj (repl_spec   x) pt pos;
wneuper@59294
   164
fun update_tac    pt pos x = appl_obj (repl_tac    x) pt pos;
wneuper@59294
   165
wneuper@59294
   166
fun update_loc'   pt pos x = appl_obj (repl_loc    x) pt pos;
wneuper@59294
   167
walther@59820
   168
walther@59820
   169
(** add steps to the ctree; THIS PROBABLY SHOULD BE THE ONLY WRITE ACCESS **)
walther@59820
   170
walther@59813
   171
(* insert a term into the Ctree, which waits for application of a Tactic to complete a step*)
wneuper@59294
   172
fun append_form p l f pt = 
walther@59840
   173
  insert_pt (PrfObj {form = f, tac = Tactic.Empty_Tac, loc = (SOME l, NONE),
walther@59861
   174
		  branch = NoBranch, result = (TermC.empty, []), ostate = Incomplete}) pt p;
wneuper@59294
   175
fun cappend_form pt p loc f =
wneuper@59294
   176
  let
wneuper@59294
   177
    val (pt', cs) = cut_tree pt (p, Frm)
wneuper@59294
   178
    val pt'' = append_form p loc f pt'
wneuper@59294
   179
  in (pt'', cs) end;
wneuper@59294
   180
walther@59813
   181
(* insert a complete step with term, Tactic and result into the Ctree*)
walther@59815
   182
fun append_atomic p (ic_form, ic_res) f r f' s pt = 
walther@59813
   183
  let
walther@59813
   184
    val (iss, f) =
walther@59844
   185
      if existpt p pt andalso Tactic.is_empty (get_obj g_tac pt p)
walther@59816
   186
      (* ^^^^^^^   ^^ probably is cut, so this     ^^^^^^^^^^^^^^^^^^ might cause
walther@59816
   187
          exception PTREE "get_obj: pos = ... does not exist", (!) WHICH IS NOT RECOGNISED BY if *)
walther@59815
   188
		  then ((fst (get_obj g_loc pt p), SOME ic_res), get_obj g_form pt p) (*after Take*)
walther@59815
   189
		  else ((ic_form, SOME ic_res), f)
walther@59813
   190
  in
walther@59840
   191
    insert_pt (PrfObj {form = f, tac = r, loc = iss, branch = NoBranch,
walther@59813
   192
		   result = f', ostate = s}) pt p
walther@59813
   193
  end;
walther@59813
   194
(* 20.8.02: cappend_* FIXXXXME cut branches below cannot be decided here:
walther@59813
   195
   detail - generate - cappend: inserted, not appended !!!
walther@59816
   196
   cut decided in applicable_in !?! and represented by flag ? *)
walther@59813
   197
fun cappend_atomic pt p ic_res f r f' s = 
walther@59844
   198
  if existpt p pt andalso Tactic.is_empty (get_obj g_tac pt p)
walther@59813
   199
  then (*after Take: transfer Frm and respective istate*)
walther@59813
   200
	  let
walther@59813
   201
      val (ic_form, f) = (get_loc pt (p, Frm), get_obj g_form pt p)
walther@59813
   202
	    val (pt, cs) = cut_tree pt (p, Frm)
walther@59815
   203
	    val pt = append_atomic p (SOME ic_form, ic_res) f r f' s pt
walther@59813
   204
	  in
walther@59813
   205
	    (pt, cs)
walther@59813
   206
	  end
walther@59815
   207
  else apfst (append_atomic p (NONE, ic_res) f r f' s) (cut_tree pt (p, Frm));
walther@59820
   208
                                                                             
walther@59820
   209
(* insert a new SubProblem' and create a new PblObj in the Ctree *)
walther@59819
   210
fun append_problem [] l fmz (strs, spec, hdf, ctxt_specify) _ =
walther@59902
   211
    (Nd (PblObj {origin = (strs, spec, hdf), fmz = fmz, spec = Spec.empty,
walther@59839
   212
	  	probl = [], meth = [], ctxt = ctxt_specify, loc = (SOME l, NONE),
walther@59861
   213
	  	branch = TransitiveB, result = (TermC.empty, []), ostate = Incomplete}, []))
walther@59819
   214
  | append_problem p l fmz (strs, spec, hdf, ctxt_specify) pt =
walther@59902
   215
    insert_pt (PblObj {origin = (strs, spec, hdf), fmz = fmz, spec  = Spec.empty,
walther@59839
   216
	   probl = [], meth = [], ctxt = ctxt_specify, loc = (SOME l, NONE),
walther@59861
   217
	   branch = TransitiveB, result = (TermC.empty, []), ostate= Incomplete}) pt p;
wneuper@59294
   218
fun cappend_problem _ [] loc fmz ori = (append_problem [] loc fmz ori EmptyPtree, [])
wneuper@59294
   219
  | cappend_problem pt p loc fmz ori = 
wneuper@59294
   220
    apfst (append_problem p loc fmz ori) (cut_tree pt (p, Frm));
wneuper@59294
   221
walther@59820
   222
(* update data in an existing PblObj; ?TODO: delete all Ctree.update_ *)
walther@59820
   223
fun update_problem p (spec, probl, meth, ctxt_specify) pt =
walther@59820
   224
  let
walther@59840
   225
    val (fmz, origin, loc, branch, result, ostate) =
walther@59820
   226
      case get_obj I pt p of
walther@59840
   227
        PblObj {fmz, origin, loc, branch, result, ostate, ...} =>
walther@59840
   228
          (fmz, origin, loc, branch, result, ostate)
walther@59820
   229
      | _ => raise ERROR "update_problem: uncovered case"
walther@59820
   230
  in
walther@59840
   231
    insert_pt (PblObj {origin = origin, fmz = fmz, spec  = spec, probl = probl, meth = meth,
walther@59839
   232
      ctxt = ctxt_specify, loc = loc, branch = branch, result = result, ostate = ostate})
walther@59820
   233
      pt p
walther@59820
   234
  end
walther@59820
   235
(* update a PblObj; cut_tree might be a hack; await intro. of PIDE *)
walther@59820
   236
fun cupdate_problem pt p data =
walther@59820
   237
  let
walther@59820
   238
    val (pt', c') = cut_tree pt (lev_on_total p, Und)
walther@59820
   239
    val (pt'', c'') = cut_tree pt' (lev_dn p, Und)
walther@59820
   240
  in
walther@59820
   241
    (update_problem p data pt'', c' @ c'')
walther@59820
   242
  end
walther@59820
   243
walther@59816
   244
(* append a result from application of a Tactic,
walther@59813
   245
   in case of SubProblem' this is Check_Postcond' *)
walther@59813
   246
fun append_result pt p ist_ctxt f s =
walther@59813
   247
  (appl_obj (repl_result (fst (get_obj g_loc pt p), SOME ist_ctxt) f s) pt p, []);
wneuper@59294
   248
walther@59813
   249
(**)end(**)