src/Tools/isac/Interpret/ctree.sml
author Mathias Lehnfeld <s1210629013@students.fh-hagenberg.at>
Thu, 24 Oct 2013 00:02:29 +0100
changeset 52153 26f274076fd2
parent 52144 1536870f7dc5
child 52166 522d2cab05e8
permissions -rw-r--r--
switched from "calclist' = Unsynchronized.ref" to Theory_Data
neuper@37906
     1
(* use"../ME/ctree.sml";
neuper@37906
     2
   use"ME/ctree.sml";
neuper@37906
     3
   use"ctree.sml";
neuper@37906
     4
   W.N.26.10.99
neuper@37906
     5
neuper@38015
     6
tracing (pr_ptree pr_short pt); 
neuper@37906
     7
neuper@37906
     8
val Nd ( _, ns) = pt;
neuper@37906
     9
neuper@37906
    10
*)
neuper@37906
    11
neuper@37906
    12
(*structure Ptree (**): PTREE (**) = ###### outcommented ######*)
neuper@37906
    13
signature PTREE =
neuper@37906
    14
sig
neuper@37906
    15
  type ptree
neuper@37906
    16
  type envp
neuper@37906
    17
  val e_ptree : ptree
neuper@37906
    18
  exception PTREE of string
neuper@37906
    19
  type branch
neuper@37906
    20
  type ostate
neuper@37906
    21
  type cellID
neuper@37906
    22
  type cid
neuper@37906
    23
  type posel
neuper@37906
    24
  type pos
neuper@37906
    25
  type pos'
neuper@37906
    26
  type loc
neuper@37906
    27
  type domID
neuper@37906
    28
  type pblID
neuper@37906
    29
  type metID
neuper@37906
    30
  type spec
neuper@37906
    31
  type 'a ppc
neuper@37906
    32
  type con
neuper@37906
    33
  type subs
neuper@37906
    34
  type subst
neuper@37906
    35
  type env
neuper@37906
    36
  type ets
neuper@37906
    37
  val ets2str : ets -> string
neuper@37906
    38
  type item
neuper@37906
    39
  type tac
neuper@37906
    40
  type tac_
neuper@37906
    41
  val tac_2str : tac_ -> string
neuper@37906
    42
  type safe
neuper@37906
    43
  val safe2str : safe -> string
neuper@37906
    44
neuper@37906
    45
  type meth
neuper@37906
    46
  val cappend_atomic : ptree -> pos -> loc -> cterm' -> tac
neuper@37906
    47
    -> cterm' -> ostate -> cid -> ptree * posel list * cid
neuper@37906
    48
  val cappend_form : ptree
neuper@37906
    49
    -> pos -> loc -> cterm' -> cid -> ptree * pos * cid
neuper@37906
    50
  val cappend_parent : ptree -> pos -> loc -> cterm' -> tac
neuper@37906
    51
    -> branch -> cid -> ptree * int list * cid
neuper@37906
    52
  val cappend_problem : ptree -> posel list(*FIXME*) -> loc
neuper@37906
    53
    -> cterm' list * spec -> cid -> ptree * int list * cellID list
neuper@37906
    54
  val append_result : ptree -> pos -> cterm' -> ostate -> ptree * pos
neuper@37906
    55
neuper@37906
    56
  type ppobj
neuper@37906
    57
  val g_branch : ppobj -> branch
neuper@37906
    58
  val g_cell : ppobj -> cid
neuper@37906
    59
  val g_args : ppobj -> (int * (term list)) list (*args of scr*)
neuper@37906
    60
  val g_form : ppobj -> cterm'
neuper@37906
    61
  val g_loc : ppobj -> loc
neuper@37906
    62
  val g_met : ppobj -> meth
neuper@37906
    63
  val g_domID : ppobj -> domID
neuper@37906
    64
  val g_metID : ppobj -> metID
neuper@37906
    65
  val g_model : ppobj -> cterm' ppc
neuper@37906
    66
  val g_tac : ppobj -> tac
neuper@37906
    67
  val g_origin : ppobj -> cterm' list * spec
neuper@37906
    68
  val g_ostate : ppobj -> ostate
neuper@37906
    69
  val g_pbl : ppobj -> pblID * item ppc
neuper@37906
    70
  val g_result : ppobj -> cterm'
neuper@37906
    71
  val g_spec : ppobj -> spec
neuper@37906
    72
(*  val get_all : (ppobj -> 'a) -> ptree -> 'a list
neuper@37906
    73
  val get_alls : (ppobj -> 'a) -> ptree list -> 'a list *)
neuper@37906
    74
  val get_obj : (ppobj -> 'a) -> ptree -> pos -> 'a     
neuper@37906
    75
  val gpt_cell : ptree -> cid
neuper@37906
    76
  val par_pblobj : ptree -> pos -> pos
neuper@37906
    77
  val pre_pos : pos -> pos
neuper@37906
    78
  val lev_dn : int list -> int list
neuper@37906
    79
  val lev_on : pos -> posel list
neuper@37906
    80
  val lev_pred : pos -> pos
neuper@37906
    81
  val lev_up : pos -> pos
neuper@37906
    82
(*  val pr_cell : pos -> ppobj -> string
neuper@37906
    83
  val pr_pos : int list -> string        *)
neuper@37906
    84
  val pr_ptree : (pos -> ppobj -> string) -> ptree -> string
neuper@37906
    85
  val pr_short : pos -> ppobj -> string
neuper@37906
    86
(*  val repl : 'a list -> int -> 'a -> 'a list
neuper@37906
    87
  val repl_app : 'a list -> int -> 'a -> 'a list
neuper@37906
    88
  val repl_branch : branch -> ppobj -> ppobj
neuper@37906
    89
  val repl_domID : domID -> ppobj -> ppobj
neuper@37906
    90
  val repl_form : cterm' -> ppobj -> ppobj
neuper@37906
    91
  val repl_met : item ppc -> ppobj -> ppobj
neuper@37906
    92
  val repl_metID : metID -> ppobj -> ppobj
neuper@37906
    93
  val repl_model : cterm' list -> ppobj -> ppobj
neuper@37906
    94
  val repl_tac : tac -> ppobj -> ppobj
neuper@37906
    95
  val repl_pbl : item ppc -> ppobj -> ppobj
neuper@37906
    96
  val repl_pblID : pblID -> ppobj -> ppobj
neuper@37906
    97
  val repl_result : cterm' -> ostate -> ppobj -> ppobj
neuper@37906
    98
  val repl_spec : spec -> ppobj -> ppobj
neuper@37906
    99
  val repl_subs : (string * string) list -> ppobj -> ppobj     *)
neuper@37906
   100
  val rootthy : ptree -> domID
neuper@37906
   101
(*  val test_trans : ppobj -> bool
neuper@37906
   102
  val uni__asm : (string * pos) list -> ppobj -> ppobj
neuper@37906
   103
  val uni__cid : cellID list -> ppobj -> ppobj                 *)
neuper@37906
   104
  val union_asm : ptree -> pos -> (string * pos) list -> ptree
neuper@37906
   105
  val union_cid : ptree -> pos -> cellID list -> ptree
neuper@37906
   106
  val update_branch : ptree -> pos -> branch -> ptree
neuper@37906
   107
  val update_domID : ptree -> pos -> domID -> ptree
neuper@37906
   108
  val update_met : ptree -> pos -> meth -> ptree
neuper@37906
   109
  val update_metppc : ptree -> pos -> item ppc -> ptree
neuper@37906
   110
  val update_metID : ptree -> pos -> metID -> ptree
neuper@37906
   111
  val update_tac : ptree -> pos -> tac -> ptree
neuper@37906
   112
  val update_pbl : ptree -> pos -> pblID * item ppc -> ptree
neuper@37906
   113
  val update_pblppc : ptree -> pos -> item ppc -> ptree
neuper@37906
   114
  val update_pblID : ptree -> pos -> pblID -> ptree
neuper@37906
   115
  val update_spec : ptree -> pos -> spec -> ptree
neuper@37906
   116
  val update_subs : ptree -> pos -> (string * string) list -> ptree
neuper@37906
   117
neuper@37906
   118
  val rep_pblobj : ppobj
neuper@37906
   119
    -> {branch:branch, cell:cid, env:envp, loc:loc, meth:meth, model:cterm' ppc,
neuper@37906
   120
        origin:cterm' list * spec, ostate:ostate, probl:pblID * item ppc,
neuper@37906
   121
        result:cterm', spec:spec}
neuper@37906
   122
  val rep_prfobj : ppobj
neuper@37906
   123
    -> {branch:branch, cell:cid, form:cterm', loc:loc, tac:tac,
neuper@37906
   124
        ostate:ostate, result:cterm'}
neuper@37906
   125
end 
neuper@37906
   126
neuper@37906
   127
(* -------------- 
neuper@37906
   128
structure Ptree (**): PTREE (**) =
neuper@37906
   129
struct
neuper@37906
   130
 -------------- *)
neuper@37906
   131
neuper@37906
   132
type env = (term * term) list;
neuper@37906
   133
neuper@37906
   134
   
neuper@37906
   135
datatype branch = 
neuper@37906
   136
	 NoBranch | AndB | OrB 
neuper@37906
   137
       | TransitiveB  (* FIXXXME.8.03: set branch from met in Apply_Method
neuper@37906
   138
                         FIXXXME.0402: -"- in Begin_Trans'*)
neuper@37906
   139
       | SequenceB | IntersectB | CollectB | MapB;
neuper@37906
   140
fun branch2str NoBranch = "NoBranch"
neuper@37906
   141
  | branch2str AndB = "AndB"
neuper@37906
   142
  | branch2str OrB = "OrB"
neuper@37906
   143
  | branch2str TransitiveB = "TransitiveB" 
neuper@37906
   144
  | branch2str SequenceB = "SequenceB"
neuper@37906
   145
  | branch2str IntersectB = "IntersectB"
neuper@37906
   146
  | branch2str CollectB = "CollectB"
neuper@37906
   147
  | branch2str MapB = "MapB";
neuper@37906
   148
neuper@37906
   149
datatype ostate = 
neuper@37906
   150
    Incomplete | Complete | Inconsistent(*WN041020 latter unused*);
neuper@37906
   151
fun ostate2str Incomplete = "Incomplete"
neuper@37906
   152
  | ostate2str Complete = "Complete"
neuper@37906
   153
  | ostate2str Inconsistent = "Inconsistent";
neuper@37906
   154
neuper@37906
   155
type cellID = int;     
neuper@37906
   156
type cid = cellID list;
neuper@37906
   157
neuper@37906
   158
type posel = int;     (* roundabout for (some of) nice signatures *)
neuper@37906
   159
type pos = posel list;
neuper@37906
   160
val pos2str = ints2str';
neuper@37906
   161
datatype pos_ = 
neuper@37906
   162
    Pbl    (*PblObj-position: problem-type*)
neuper@37906
   163
  | Met    (*PblObj-position: method*)
neuper@37906
   164
  | Frm    (*PblObj-position: -> Pbl in ME (not by moveDown !)
neuper@37906
   165
           | PrfObj-position: formula*)
neuper@37906
   166
  | Res    (*PblObj | PrfObj-position: result*)
neuper@37906
   167
  | Und;   (*undefined*)
neuper@37906
   168
fun pos_2str Pbl = "Pbl"
neuper@37906
   169
  | pos_2str Met = "Met"
neuper@37906
   170
  | pos_2str Frm = "Frm"
neuper@37906
   171
  | pos_2str Res = "Res"
neuper@37906
   172
  | pos_2str Und = "Und";
neuper@37906
   173
neuper@37906
   174
type pos' = pos * pos_;
neuper@42003
   175
(*WN0312 remembering interator (pos * pos_) for ptree 
neuper@37906
   176
	   pos : lev_on, lev_dn, lev_up, 
neuper@42003
   177
           lev_onFrm, lev_dnRes (..see solve Apply_Method !) 
neuper@42003
   178
     pos_:
neuper@37906
   179
# generate1 sets pos_ if possible  ...?WN0502?NOT...
neuper@37906
   180
# generate1 does NOT set pos, because certain nodes can be lev_on OR lev_dn
neuper@37906
   181
                     exceptions: Begin/End_Trans
neuper@37906
   182
# thus generate(1) called in
neuper@37906
   183
.# assy, locate_gen 
neuper@37906
   184
.# nxt_solv (tac_ -cases); general case: 
neuper@37906
   185
  val pos' = case pos' of (p,Res) => (lev_on p',Res) | _ => pos'
neuper@37906
   186
# WN050220, S(604):
neuper@37906
   187
  generate1...(Rewrite(f,..,res))..(pos, pos_)
neuper@37906
   188
     cappend_atomic.................pos //////  gets f+res always!!!
neuper@37906
   189
        cut_tree....................pos, pos_ 
neuper@37906
   190
*)
neuper@37906
   191
fun pos'2str (p,p_) = pair2str (ints2str' p, pos_2str p_);
neuper@37906
   192
fun pos's2str ps = (strs2str' o (map pos'2str)) ps;
neuper@37906
   193
val e_pos' = ([],Und):pos';
neuper@37906
   194
neuper@37906
   195
fun res2str (t, ts) = pair2str (term2str t, terms2str ts);
neuper@37906
   196
neuper@37906
   197
neuper@37906
   198
neuper@37906
   199
(*26.4.02: never used after introduction of scripts !!!
neuper@37906
   200
type loc =  loc_ *        (* + interpreter-state          *)
neuper@37906
   201
	    (loc_ * rls') (* -"- for script of the ruleset*)
neuper@37906
   202
		option;
neuper@37926
   203
val e_loc = ([],NONE):loc;
neuper@37906
   204
val ee_loc = (e_loc,e_loc);*)
neuper@37906
   205
neuper@37906
   206
neuper@37906
   207
datatype safe = Sundef | Safe | Unsafe | Helpless;
neuper@37906
   208
fun safe2str Sundef   = "Sundef"
neuper@37906
   209
  | safe2str Safe     = "Safe"
neuper@37906
   210
  | safe2str Unsafe   = "Unsafe" 
neuper@37906
   211
  | safe2str Helpless = "Helpless";
neuper@37906
   212
neuper@37906
   213
type subs = cterm' list; (*16.11.00 for FE-KE*)
neuper@37906
   214
val e_subs = ["(bdv, x)"];
neuper@37906
   215
neuper@42433
   216
(* argument type of tac Rewrite_Inst *)
neuper@37906
   217
type sube = cterm' list;
neuper@37906
   218
val e_sube = []:cterm' list;
neuper@37906
   219
fun sube2str s = strs2str s;
neuper@37906
   220
neuper@37906
   221
(*._sub_stitution as _t_erms of _e_qualities.*)
neuper@37906
   222
type subte = term list;
neuper@37906
   223
val e_subte = []:term list;
neuper@37906
   224
fun subte2str ss = terms2str ss;
neuper@37906
   225
neuper@42360
   226
val subte2sube = map term2str;
neuper@42360
   227
val subst2subs = map (pair2str o (apfst term2str) o (apsnd term2str));
neuper@42360
   228
val subst2subs' = map ((apfst term2str) o (apsnd term2str));
neuper@42385
   229
fun subs2subst thy s = map (isapair2pair o (parse_patt thy)) s;
neuper@42385
   230
fun sube2subst thy s = map (dest_equals' o (parse_patt thy)) s;
neuper@42360
   231
val sube2subte = map str2term;
neuper@42360
   232
val subte2subst = map HOLogic.dest_eq;
neuper@37906
   233
neuper@37906
   234
fun isasub2subst isasub = ((map isapair2pair) o isalist2list) isasub;
neuper@37906
   235
neuper@37906
   236
type scrstate =       (*state for script interpreter*)
neuper@37906
   237
	 env(*stack*) (*used to instantiate tac for checking assod
neuper@37906
   238
		       12.03.noticed: e_ not updated during execution ?!?*)
neuper@37906
   239
	 * loc_       (*location of tac in script*)
neuper@37906
   240
	 * term option(*argument of curried functions*)
neuper@37906
   241
	 * term       (*value obtained by tac executed
neuper@37906
   242
		       updated also after a derivation by 'new_val'*)
neuper@37906
   243
	 * safe       (*estimation of how result will be obtained*)
neuper@37906
   244
	 * bool;      (*true = strongly .., false = weakly associated: 
neuper@37906
   245
					    only used during ass_dn/up*)
neuper@37925
   246
val e_scrstate = ([],[],NONE,e_term,Sundef,false):scrstate;
neuper@42360
   247
fun topt2str NONE = "NONE"
neuper@42360
   248
  | topt2str (SOME t) = "SOME" ^ term2str t;
neuper@42360
   249
fun scrstate2str (env, loc_, topt, t, safe, bool) =
neuper@42360
   250
  "(" ^ env2str env ^ ", " ^ loc_2str loc_ ^ ", " ^ topt2str topt ^ ", \n" ^ 
neuper@42360
   251
  term2str t ^ ", " ^ safe2str safe ^ ", " ^ bool2str bool ^ ")";
neuper@37906
   252
neuper@41996
   253
(* for handling type istate see fun from_pblobj_or_detail', +? *)
neuper@41996
   254
datatype istate =           (*interpreter state*)
neuper@41996
   255
	  Uistate                 (*undefined in modspec, in '_deriv'ation*)
neuper@41996
   256
  | ScrState of scrstate    (*for script interpreter*)
neuper@41996
   257
  | RrlsState of rrlsstate; (*for reverse rewriting*)
neuper@37925
   258
val e_istate = (ScrState ([],[],NONE,e_term,Sundef,false)):istate;
neuper@48761
   259
val e_ctxt = Proof_Context.init_global @{theory "Pure"};
neuper@41992
   260
neuper@41992
   261
(* ATTENTION: does _not_ recognise Variable.declare_constraints, etc...*)
neuper@48761
   262
fun is_e_ctxt ctxt = Theory.eq_thy (Proof_Context.theory_of ctxt, @{theory "Pure"});
neuper@37906
   263
neuper@37906
   264
type iist = istate option * istate option;
neuper@37906
   265
(*val e_iist = (e_istate, e_istate); --- sinnlos f"ur NICHT-equality-type*) 
neuper@37906
   266
neuper@37906
   267
neuper@37906
   268
fun rta2str (r,(t,a)) = "\n("^(rule2str r)^",("^(term2str t)^", "^
neuper@37906
   269
		      (terms2str a)^"))";
neuper@37906
   270
fun istate2str Uistate = "Uistate"
neuper@37906
   271
  | istate2str (ScrState (e,l,to,t,s,b):istate) =
neuper@37906
   272
    "ScrState ("^ subst2str e ^",\n "^ 
neuper@37906
   273
    loc_2str l ^", "^ termopt2str to ^",\n "^
neuper@37906
   274
    term2str t ^", "^ safe2str s ^", "^ bool2str b ^")"
neuper@37906
   275
  | istate2str (RrlsState (t,t1,rss,rtas)) = 
neuper@37906
   276
    "RrlsState ("^(term2str t)^", "^(term2str t1)^", "^
neuper@37906
   277
    ((strs2str o (map (strs2str o (map rule2str)))) rss)^", "^
neuper@37906
   278
    ((strs2str o (map rta2str)) rtas)^")";
neuper@37925
   279
fun istates2str (NONE, NONE) = "(#NONE, #NONE)"
neuper@37925
   280
  | istates2str (NONE, SOME ist) = "(#NONE,\n#SOME "^istate2str ist^")"
neuper@37925
   281
  | istates2str (SOME ist, NONE) = "(#SOME "^istate2str ist^",\n #NONE)"
neuper@37925
   282
  | istates2str (SOME i1, SOME i2) = "(#SOME "^istate2str i1^",\n #SOME "^
neuper@37906
   283
				     istate2str i2^")";
neuper@37906
   284
neuper@37906
   285
fun new_val v (ScrState (env, loc_, topt, _, safe, bool)) =
neuper@37906
   286
    (ScrState (env, loc_, topt, v, safe, bool))
neuper@38031
   287
  | new_val _ _ = error "new_val: only for ScrState";
neuper@37906
   288
neuper@37906
   289
datatype con = land | lor;
neuper@37906
   290
neuper@37906
   291
neuper@37906
   292
type spec = 
neuper@37906
   293
     domID * (*WN.12.03: is replaced by thy from get_met ?FIXME? in:
neuper@37906
   294
	      specify (Init_Proof..), nxt_specify_init_calc,
neuper@37906
   295
	      assod (.SubProblem...), stac2tac (.SubProblem...)*)
neuper@37906
   296
     pblID * 
neuper@37906
   297
     metID;
neuper@37906
   298
fun spec2str ((dom,pbl,met)(*:spec*)) = 
neuper@37906
   299
  "(" ^ (quote dom) ^ ", " ^ (strs2str pbl) ^ 
neuper@37906
   300
  ", " ^ (strs2str met) ^ ")";
neuper@37906
   301
(*> spec2str empty_spec;
neuper@37906
   302
val it = "(\"\", [], (\"\", \"\"))" : string *)
neuper@37906
   303
val empty_spec = (e_domID,e_pblID,e_metID):spec;
neuper@37906
   304
val e_spec = empty_spec;
neuper@37906
   305
neuper@37906
   306
neuper@37906
   307
neuper@37906
   308
(*.tactics propagate the construction of the calc-tree;
neuper@37906
   309
   there are
neuper@37906
   310
   (a) 'specsteps' for the specify-phase, and others for the solve-phase
neuper@37906
   311
   (b) those of the solve-phase are 'initac's and others;
neuper@37906
   312
       initacs start with a formula different from the preceding formula.
neuper@37906
   313
   see 'type tac_' for the internal representation of tactics.*)
neuper@37906
   314
datatype tac = 
neuper@37906
   315
  Init_Proof of ((cterm' list) * spec)
neuper@37906
   316
(*'specsteps'...*)
neuper@37906
   317
| Model_Problem
neuper@37906
   318
| Refine_Problem of pblID              | Refine_Tacitly of pblID
neuper@37906
   319
neuper@37906
   320
| Add_Given of cterm'                  | Del_Given of cterm'
neuper@37906
   321
| Add_Find of cterm'                   | Del_Find of cterm'
neuper@37906
   322
| Add_Relation of cterm'               | Del_Relation of cterm'
neuper@37906
   323
neuper@37906
   324
| Specify_Theory of domID              | Specify_Problem of pblID
neuper@37906
   325
| Specify_Method of metID
neuper@37906
   326
(*...'specsteps'*)
neuper@37906
   327
| Apply_Method of metID 
neuper@37906
   328
(*.creates an 'istate' in PblObj.env; in case of 'init_form' 
neuper@37906
   329
   creates a formula at ((lev_on o lev_dn) p, Frm) and in this ppobj.'loc' 
neuper@37925
   330
   'SOME istate' (at fst of 'loc').
neuper@37906
   331
   As each step (in the solve-phase) has a resulting formula (at the front-end)
neuper@37906
   332
   Apply_Method also does the 1st step in the script (an 'initac') if there
neuper@37906
   333
   is no 'init_form' .*)
neuper@37906
   334
| Check_Postcond of pblID
neuper@37906
   335
| Free_Solve
neuper@37906
   336
neuper@37906
   337
| Rewrite_Inst of ( subs * thm')       | Rewrite of thm'
neuper@37906
   338
                                       | Rewrite_Asm of thm'
neuper@37906
   339
| Rewrite_Set_Inst of ( subs * rls')   | Rewrite_Set of rls'        
neuper@37906
   340
| Detail_Set_Inst of ( subs * rls')    | Detail_Set of rls'
neuper@37906
   341
| End_Detail  (*end of script from next_tac, 
neuper@37906
   342
                in solve: switches back to parent script WN0509 drop!*)
neuper@37906
   343
| Derive of rls' (*an input formula using rls WN0509 drop!*)
neuper@37906
   344
| Calculate of string (* plus | minus | times | cancel | pow | sqrt *)
neuper@37906
   345
| End_Ruleset
neuper@37906
   346
| Substitute of sube                   | Apply_Assumption of cterm' list
neuper@37906
   347
neuper@37906
   348
| Take of cterm'      (*an 'initac'*)
neuper@37906
   349
| Take_Inst of cterm'  
neuper@37906
   350
| Group of (con * int list ) 
neuper@37906
   351
| Subproblem of (domID * pblID) (*an 'initac'*)
neuper@37906
   352
| CAScmd of cterm'  (*6.6.02 URD: Function formula; WN0509 drop!*)
neuper@37906
   353
| End_Subproblem    (*WN0509 drop!*)
neuper@37906
   354
neuper@37906
   355
| Split_And                            | Conclude_And
neuper@37906
   356
| Split_Or                             | Conclude_Or
neuper@37906
   357
| Begin_Trans                          | End_Trans
neuper@37906
   358
| Begin_Sequ                           | End_Sequ(* substitute root.env *)
neuper@37906
   359
| Split_Intersect                      | End_Intersect
neuper@37906
   360
| Check_elementwise of cterm'          | Collect_Trues
neuper@42394
   361
| Or_to_List  (*WN120315 ~ @{thm d2_prescind1},2,3,4 in PolyEq.thy *)
neuper@37906
   362
neuper@38051
   363
| Empty_Tac      (* TODO.11.6.03 ... of string: could carry msg of (Notappl msg)
neuper@38051
   364
	            in 'helpless'*)
neuper@38051
   365
| Tac of string  (* eg.'repeat'*WN0509 drop! (ab)used to report syntaxerror   *)
neuper@38051
   366
| User           (* internal, for ets*WN0509 drop!                            *)
neuper@38051
   367
| End_Proof';    (* inout                                                     *)
neuper@37906
   368
neuper@37906
   369
(* tac2str /--> library.sml: needed in dialog.sml for 'separable *)
neuper@37906
   370
fun tac2str (ma:tac) = case ma of
neuper@37906
   371
    Init_Proof (ppc, spec)  => 
neuper@37906
   372
      "Init_Proof "^(pair2str (strs2str ppc, spec2str spec))
neuper@37906
   373
  | Model_Problem           => "Model_Problem "
neuper@37906
   374
  | Refine_Tacitly pblID    => "Refine_Tacitly "^(strs2str pblID)
neuper@37906
   375
  | Refine_Problem pblID    => "Refine_Problem "^(strs2str pblID)
neuper@37906
   376
  | Add_Given cterm'        => "Add_Given "^cterm'
neuper@37906
   377
  | Del_Given cterm'        => "Del_Given "^cterm'
neuper@37906
   378
  | Add_Find cterm'         => "Add_Find "^cterm'
neuper@37906
   379
  | Del_Find cterm'         => "Del_Find "^cterm'
neuper@37906
   380
  | Add_Relation cterm'     => "Add_Relation "^cterm'
neuper@37906
   381
  | Del_Relation cterm'     => "Del_Relation "^cterm'
neuper@37906
   382
neuper@37906
   383
  | Specify_Theory domID    => "Specify_Theory "^(quote domID    )
neuper@37906
   384
  | Specify_Problem pblID   => "Specify_Problem "^(strs2str pblID )
neuper@37906
   385
  | Specify_Method metID    => "Specify_Method "^(strs2str metID)
neuper@37906
   386
  | Apply_Method metID      => "Apply_Method "^(strs2str metID)
neuper@37906
   387
  | Check_Postcond pblID    => "Check_Postcond "^(strs2str pblID)
neuper@37906
   388
  | Free_Solve              => "Free_Solve"
neuper@37906
   389
neuper@37906
   390
  | Rewrite_Inst (subs,thm')=> 
neuper@37906
   391
      "Rewrite_Inst "^(pair2str (subs2str subs, spair2str thm'))
neuper@37906
   392
  | Rewrite thm'            => "Rewrite "^(spair2str thm')
neuper@37906
   393
  | Rewrite_Asm thm'        => "Rewrite_Asm "^(spair2str thm')
neuper@37906
   394
  | Rewrite_Set_Inst (subs, rls) => 
neuper@37906
   395
      "Rewrite_Set_Inst "^(pair2str (subs2str subs, quote rls))
neuper@37906
   396
  | Rewrite_Set rls         => "Rewrite_Set "^(quote rls    )
neuper@37906
   397
  | Detail_Set rls          => "Detail_Set "^(quote rls    )
neuper@37906
   398
  | Detail_Set_Inst (subs, rls) => 
neuper@37906
   399
      "Detail_Set_Inst "^(pair2str (subs2str subs, quote rls))
neuper@37906
   400
  | End_Detail              => "End_Detail"
neuper@37906
   401
  | Derive rls'             => "Derive "^rls' 
neuper@37906
   402
  | Calculate op_           => "Calculate "^op_ 
neuper@37906
   403
  | Substitute sube         => "Substitute "^sube2str sube	     
neuper@37906
   404
  | Apply_Assumption ct's   => "Apply_Assumption "^(strs2str ct's)
neuper@37906
   405
neuper@37906
   406
  | Take cterm'             => "Take "^(quote cterm'	)
neuper@37906
   407
  | Take_Inst cterm'        => "Take_Inst "^(quote cterm' )
neuper@37906
   408
  | Group (con, ints)       => 
neuper@37906
   409
      "Group "^(pair2str (con2str con, ints2str ints))
neuper@37906
   410
  | Subproblem (domID, pblID) => 
neuper@37906
   411
      "Subproblem "^(pair2str (domID, strs2str pblID))
neuper@37906
   412
(*| Subproblem_Full (spec, cts') => 
neuper@37906
   413
      "Subproblem_Full "^(pair2str (spec2str spec, strs2str cts'))*)
neuper@37906
   414
  | End_Subproblem          => "End_Subproblem"
neuper@37906
   415
  | CAScmd cterm'           => "CAScmd "^(quote cterm')
neuper@37906
   416
neuper@37906
   417
  | Check_elementwise cterm'=> "Check_elementwise "^(quote cterm') 
neuper@37906
   418
  | Or_to_List              => "Or_to_List "
neuper@37906
   419
  | Collect_Trues           => "Collect_Trues"
neuper@37906
   420
neuper@37906
   421
  | Empty_Tac             => "Empty_Tac"
neuper@37906
   422
  | Tac string            => "Tac "^string
neuper@37906
   423
  | User                    => "User"
neuper@37906
   424
  | End_Proof'              => "tac End_Proof'"
neuper@37906
   425
  | _                       => "tac2str not impl. for ?!";
neuper@37906
   426
neuper@37906
   427
fun is_rewset (Rewrite_Set_Inst _) = true
neuper@37906
   428
  | is_rewset (Rewrite_Set _) = true 
neuper@37906
   429
  | is_rewset _ = false;
neuper@37906
   430
fun is_rewtac (Rewrite _) = true
neuper@37906
   431
  | is_rewtac (Rewrite_Inst _) = true
neuper@37906
   432
  | is_rewtac (Rewrite_Asm _) = true
neuper@37906
   433
  | is_rewtac tac = is_rewset tac;
neuper@37906
   434
neuper@37906
   435
fun tac2IDstr (ma:tac) = case ma of
neuper@37906
   436
    Model_Problem           => "Model_Problem"
neuper@37906
   437
  | Refine_Tacitly pblID    => "Refine_Tacitly"
neuper@37906
   438
  | Refine_Problem pblID    => "Refine_Problem"
neuper@37906
   439
  | Add_Given cterm'        => "Add_Given"
neuper@37906
   440
  | Del_Given cterm'        => "Del_Given"
neuper@37906
   441
  | Add_Find cterm'         => "Add_Find"
neuper@37906
   442
  | Del_Find cterm'         => "Del_Find"
neuper@37906
   443
  | Add_Relation cterm'     => "Add_Relation"
neuper@37906
   444
  | Del_Relation cterm'     => "Del_Relation"
neuper@37906
   445
neuper@37906
   446
  | Specify_Theory domID    => "Specify_Theory"
neuper@37906
   447
  | Specify_Problem pblID   => "Specify_Problem"
neuper@37906
   448
  | Specify_Method metID    => "Specify_Method"
neuper@37906
   449
  | Apply_Method metID      => "Apply_Method"
neuper@37906
   450
  | Check_Postcond pblID    => "Check_Postcond"
neuper@37906
   451
  | Free_Solve              => "Free_Solve"
neuper@37906
   452
neuper@37906
   453
  | Rewrite_Inst (subs,thm')=> "Rewrite_Inst"
neuper@37906
   454
  | Rewrite thm'            => "Rewrite"
neuper@37906
   455
  | Rewrite_Asm thm'        => "Rewrite_Asm"
neuper@37906
   456
  | Rewrite_Set_Inst (subs, rls) => "Rewrite_Set_Inst"
neuper@37906
   457
  | Rewrite_Set rls         => "Rewrite_Set"
neuper@37906
   458
  | Detail_Set rls          => "Detail_Set"
neuper@37906
   459
  | Detail_Set_Inst (subs, rls) => "Detail_Set_Inst"
neuper@37906
   460
  | Derive rls'             => "Derive "
neuper@37906
   461
  | Calculate op_           => "Calculate "
neuper@37906
   462
  | Substitute subs         => "Substitute" 
neuper@37906
   463
  | Apply_Assumption ct's   => "Apply_Assumption"
neuper@37906
   464
neuper@37906
   465
  | Take cterm'             => "Take"
neuper@37906
   466
  | Take_Inst cterm'        => "Take_Inst"
neuper@37906
   467
  | Group (con, ints)       => "Group"
neuper@37906
   468
  | Subproblem (domID, pblID) => "Subproblem"
neuper@37906
   469
  | End_Subproblem          => "End_Subproblem"
neuper@37906
   470
  | CAScmd cterm'           => "CAScmd"
neuper@37906
   471
neuper@37906
   472
  | Check_elementwise cterm'=> "Check_elementwise"
neuper@37906
   473
  | Or_to_List              => "Or_to_List "
neuper@37906
   474
  | Collect_Trues           => "Collect_Trues"
neuper@37906
   475
neuper@37906
   476
  | Empty_Tac             => "Empty_Tac"
neuper@37906
   477
  | Tac string            => "Tac "
neuper@37906
   478
  | User                    => "User"
neuper@37906
   479
  | End_Proof'              => "End_Proof'"
neuper@37906
   480
  | _                       => "tac2str not impl. for ?!";
neuper@37906
   481
neuper@37906
   482
fun rls_of (Rewrite_Set_Inst (_, rls)) = rls
neuper@37906
   483
  | rls_of (Rewrite_Set rls) = rls
neuper@38031
   484
  | rls_of tac = error ("rls_of: called with tac '"^tac2IDstr tac^"'");
neuper@37906
   485
neuper@37906
   486
fun thm_of_rew (Rewrite_Inst (subs,(thmID,_))) = 
neuper@38050
   487
    (thmID, SOME ((subs2subst (assoc_thy "Isac") subs):subst))
neuper@37925
   488
  | thm_of_rew (Rewrite  (thmID,_)) = (thmID, NONE)
neuper@37925
   489
  | thm_of_rew (Rewrite_Asm (thmID,_)) = (thmID, NONE);
neuper@37906
   490
neuper@37906
   491
fun rls_of_rewset (Rewrite_Set_Inst (subs,rls)) = 
neuper@38050
   492
    (rls, SOME ((subs2subst (assoc_thy "Isac") subs):subst))
neuper@37925
   493
  | rls_of_rewset (Rewrite_Set rls) = (rls, NONE)
neuper@37925
   494
  | rls_of_rewset (Detail_Set rls) = (rls, NONE)
neuper@37906
   495
  | rls_of_rewset (Detail_Set_Inst (subs, rls)) = 
neuper@38050
   496
    (rls, SOME ((subs2subst (assoc_thy "Isac") subs):subst));
neuper@37906
   497
s1210629013@52153
   498
fun rule2tac thy _ (Calc (opID, thm)) = Calculate (assoc_calc thy opID)
s1210629013@52153
   499
  | rule2tac _ [] (Thm (thmID, thm)) = Rewrite (thmID, string_of_thmI thm)
s1210629013@52153
   500
  | rule2tac _ subst (Thm (thmID, thm)) = 
neuper@37906
   501
    Rewrite_Inst (subst2subs subst, (thmID, string_of_thmI thm))
s1210629013@52153
   502
  | rule2tac _ [] (Rls_ rls) = Rewrite_Set (id_rls rls)
s1210629013@52153
   503
  | rule2tac _ subst (Rls_ rls) = 
neuper@37906
   504
    Rewrite_Set_Inst (subst2subs subst, (id_rls rls))
s1210629013@52153
   505
  | rule2tac _ _ rule = 
neuper@38031
   506
    error ("rule2tac: called with '" ^ rule2str rule ^ "'");
neuper@37906
   507
neuper@37906
   508
type fmz_ = cterm' list;
neuper@37906
   509
neuper@37906
   510
(*.a formalization of an example containing data 
neuper@37906
   511
   sufficient for mechanically finding the solution for the example.*)
neuper@37906
   512
(*FIXME.WN051014: dont store fmz = (_,spec) in the PblObj, 
neuper@37906
   513
  this is done in origin*)
neuper@37906
   514
type fmz = fmz_ * spec;
neuper@37906
   515
val e_fmz = ([],e_spec);
neuper@37906
   516
neuper@42360
   517
(* tac_ contains results from check in 'fun applicable_in'.
neuper@42360
   518
  This is useful for costly results, e.g. from rewriting;
neuper@42360
   519
  however, these results might be changed by Scripts like
neuper@42360
   520
      "      eq = (Rewrite_Set ansatz_rls False) eql;" ^
neuper@42360
   521
      "      eq = drop_questionmarks eq;" ^
neuper@42360
   522
      "      eq = (Rewrite_Set equival_trans False) eq;" ^
neuper@42360
   523
  WN120106 TODO ANALOGOUSLY TO Substitute':
neuper@42360
   524
  So tac_ contains the term t the result was calculated from
neuper@42360
   525
  in order to compare t with t' possibly changed by "Expr "
neuper@42360
   526
  and re-calculate result if t<>t'*)
neuper@37906
   527
datatype tac_ = 
neuper@42023
   528
    Init_Proof' of ((cterm' list) * spec)
neuper@42023
   529
  | Model_Problem' of
neuper@42023
   530
      pblID * 
neuper@42023
   531
		  itm list *  (*the 'untouched' pbl*)
neuper@42023
   532
		  itm list    (*the casually completed met*)
neuper@42023
   533
  | Refine_Tacitly' of
neuper@42023
   534
      pblID *    (*input*)
neuper@42023
   535
		  pblID *    (*the refined from applicable_in*)
neuper@42023
   536
		  domID *    (*from new pbt?! filled in specify*)
neuper@42023
   537
		  metID *    (*from new pbt?! filled in specify*)
neuper@42023
   538
		  itm list   (*drop ! 9.03: remains [] for
neuper@37906
   539
                                  Model_Problem recognizing its activation*)
neuper@42023
   540
  | Refine_Problem' of (pblID * (itm list * (bool * Term.term) list))
neuper@42023
   541
    (*FIXME?040215 drop: done automatically in init_proof + Subproblem'*)
neuper@42023
   542
  | Add_Given' of
neuper@42023
   543
      cterm' *
neuper@42023
   544
		  itm list (*updated with input in fun specify_additem*)
neuper@42023
   545
  | Add_Find' of cterm' * itm list (* see Add_Given' *)
neuper@42023
   546
  | Add_Relation' of cterm' * itm list (* see Add_Given' *)
neuper@42023
   547
  | Del_Given' of cterm'   | Del_Find' of cterm'   | Del_Relation' of cterm'
neuper@42023
   548
    (*4.00.: all..    term: in applicable_in ..? Syn ?only for FormFK?*)
neuper@42023
   549
  | Specify_Theory' of domID              
neuper@42023
   550
  | Specify_Problem' of
neuper@42023
   551
      (pblID *        (*               *)
neuper@42023
   552
		    (bool *        (* matches	     *)
neuper@42023
   553
			    (itm list *   (* ppc	     *)
neuper@42023
   554
			      (bool * term) list))) (* preconditions *)
neuper@42023
   555
  | Specify_Method' of
neuper@42023
   556
      metID *
neuper@42023
   557
		  ori list * (*repl. "#undef"*)
neuper@42023
   558
		  itm list   (*... updated from pbl to met*)
neuper@42023
   559
  | Apply_Method' of
neuper@42023
   560
      metID * 
neuper@42023
   561
		  (term option) * (*init_form*)
neuper@42023
   562
		  istate * Proof.context
neuper@42023
   563
  | Check_Postcond' of 
neuper@42023
   564
      pblID * 
neuper@42023
   565
      (term *      (*returnvalue of script in solve*)
neuper@42023
   566
        term list)  (*collect by get_assumptions_ in applicable_in, except if 
neuper@37906
   567
                 butlast tac is Check_elementwise: take only these asms*)
neuper@42023
   568
  | Free_Solve'
neuper@42023
   569
  | Rewrite_Inst' of theory' * rew_ord' * rls * bool * subst * thm' * term * (term  * term list)
neuper@42023
   570
  | Rewrite' of theory' * rew_ord' * rls * bool * thm' * term * (term * term list)
neuper@42023
   571
  | Rewrite_Asm' of theory' * rew_ord' * rls * bool * thm' * term * (term * term list)
neuper@42023
   572
  | Rewrite_Set_Inst' of theory' * bool * subst * rls * term * (term * term list)
neuper@42023
   573
  | Detail_Set_Inst' of theory' * bool * subst * rls * term * (term * term list)
neuper@42023
   574
  | Rewrite_Set' of theory' * bool * rls * term * (term * term list)
neuper@42023
   575
  | Detail_Set' of theory' * bool * rls * term * (term * term list)
neuper@42023
   576
  | End_Detail' of (term * (term list)) (*see End_Trans'*)
neuper@42023
   577
  | End_Ruleset' of term
neuper@42023
   578
  | Derive' of rls
neuper@42023
   579
  | Calculate' of theory' * string * term * (term * thm') 
neuper@42023
   580
  | Substitute' of
neuper@42360
   581
      rew_ord_ * (*for re-calculation                    *)
neuper@42360
   582
      rls *      (*for re-calculation                    *)
neuper@42360
   583
      subte *    (*the 'substitution': terms of type bool*) 
neuper@42360
   584
		  term * (*to be substituted in                  *)
neuper@42360
   585
		  term   (*resulting from the substitution       *)
neuper@42023
   586
  | Apply_Assumption' of term list * term
neuper@42023
   587
  | Take' of term
neuper@42023
   588
  | Take_Inst' of term  
neuper@42023
   589
  | Group' of (con * int list * term)
neuper@42023
   590
  | Subproblem' of
neuper@42023
   591
      (spec * 
neuper@42023
   592
		  (ori list) *    (* filled in assod Subproblem' *)
neuper@42023
   593
		  term *         (*-"-, headline of calc-head *)
neuper@42023
   594
		  fmz_ * 
neuper@42023
   595
      Proof.context *(* transported from assod to generate1 *)
neuper@42023
   596
		  term)          (* Subproblem(dom,pbl) OR cascmd*)  
neuper@42023
   597
  | CAScmd' of term
neuper@42023
   598
  | End_Subproblem' of term (*???*)
neuper@42023
   599
  | Split_And' of term                    | Conclude_And' of term
neuper@42023
   600
  | Split_Or' of term                     | Conclude_Or' of term
neuper@42023
   601
  | Begin_Trans' of term                  | End_Trans' of (term * (term list))
neuper@42023
   602
  | Begin_Sequ'                           | End_Sequ'(* substitute root.env*)
neuper@42023
   603
  | Split_Intersect' of term              | End_Intersect' of term
neuper@42023
   604
  | Check_elementwise' of (*special case:*)
neuper@42023
   605
      term *   (*(1)the current formula: [x=1,x=...]*)
neuper@42023
   606
      string * (*(2)the pred from Check_elementwise   *)
neuper@42023
   607
      (term *  (*(3)composed from (1) and (2): {x. pred}*)
neuper@42023
   608
        term list) (*20.5.03 assumptions*)
neuper@42023
   609
  | Or_to_List' of term * term            (* (a | b, [a,b]) *)
neuper@42023
   610
  | Collect_Trues' of term
neuper@42023
   611
  | Empty_Tac_
neuper@42023
   612
  | Tac_ of  (*for dummies*)
neuper@42023
   613
      theory *
neuper@42023
   614
      string * (*form*)
neuper@42023
   615
		  string * (*in Tac*)
neuper@42023
   616
		  string   (*result of Tac".."*)
neuper@42023
   617
  | User' (*internal for ets FIXME.WN110521 delete this and check occurrences of others*)
neuper@42023
   618
  | End_Proof'';(*End_Proof:inout*)
neuper@37906
   619
neuper@37906
   620
fun tac_2str ma = case ma of
neuper@37906
   621
    Init_Proof' (ppc, spec)  => 
neuper@37906
   622
      "Init_Proof' "^(pair2str (strs2str ppc, spec2str spec))
neuper@37906
   623
  | Model_Problem' (pblID,_,_)     => "Model_Problem' "^(strs2str pblID )
neuper@37906
   624
  | Refine_Tacitly'(p,prefin,domID,metID,itms)=> 
neuper@37906
   625
    "Refine_Tacitly' ("
neuper@37906
   626
    ^(strs2str p)^", "^(strs2str prefin)^", "
neuper@37906
   627
    ^domID^", "^(strs2str metID)^", pbl-itms)"
neuper@37906
   628
  | Refine_Problem' ms       => "Refine_Problem' ("^(*matchs2str ms*)"..."^")"
neuper@37906
   629
(*| Match_Problem' (pI, (ok, (itms, pre))) => 
neuper@37906
   630
    "Match_Problem' "^(spair2str (strs2str pI,
neuper@37906
   631
				  spair2str (bool2str ok,
neuper@37924
   632
					     spair2str ("itms2str_ itms", 
neuper@37906
   633
							"items2str pre"))))*)
neuper@37906
   634
  | Add_Given' cterm'        => "Add_Given' "(*^cterm'*)
neuper@37906
   635
  | Del_Given' cterm'        => "Del_Given' "(*^cterm'*)
neuper@37906
   636
  | Add_Find' cterm'         => "Add_Find' "(*^cterm'*)
neuper@37906
   637
  | Del_Find' cterm'         => "Del_Find' "(*^cterm'*)
neuper@37906
   638
  | Add_Relation' cterm'     => "Add_Relation' "(*^cterm'*)
neuper@37906
   639
  | Del_Relation' cterm'     => "Del_Relation' "(*^cterm'*)
neuper@37906
   640
neuper@37906
   641
  | Specify_Theory' domID    => "Specify_Theory' "^(quote domID    )
neuper@37906
   642
  | Specify_Problem' (pI, (ok, (itms, pre))) => 
neuper@37906
   643
    "Specify_Problem' "^(spair2str (strs2str pI,
neuper@37906
   644
				  spair2str (bool2str ok,
neuper@37924
   645
					     spair2str ("itms2str_ itms", 
neuper@37906
   646
							"items2str pre"))))
neuper@37906
   647
  | Specify_Method' (pI,oris,itms) => 
neuper@37906
   648
    "Specify_Method' ("^metID2str pI^", "^oris2str oris^", )"
neuper@37906
   649
bonzai@41948
   650
  | Apply_Method' (metID,_,_,_)      => "Apply_Method' "^(strs2str metID)
neuper@37906
   651
  | Check_Postcond' (pblID,(scval,asm)) => 
neuper@42018
   652
      "Check_Postcond' " ^
neuper@42018
   653
      (spair2str (strs2str pblID, spair2str (term2str scval, terms2str asm)))
neuper@37906
   654
neuper@37906
   655
  | Free_Solve'              => "Free_Solve'"
neuper@37906
   656
neuper@37906
   657
  | Rewrite_Inst' (*subs,thm'*) _ => 
neuper@37906
   658
      "Rewrite_Inst' "(*^(pair2str (subs2str subs, spair2str thm'))*)
neuper@37906
   659
  | Rewrite' thm'            => "Rewrite' "(*^(spair2str thm')*)
neuper@37906
   660
  | Rewrite_Asm' thm'        => "Rewrite_Asm' "(*^(spair2str thm')*)
neuper@37906
   661
  | Rewrite_Set_Inst' (*subs,thm'*) _ => 
neuper@37906
   662
      "Rewrite_Set_Inst' "(*^(pair2str (subs2str subs, quote rls))*)
neuper@38053
   663
  | Rewrite_Set' (thy', pasm, rls', f, (f', asm)) =>
neuper@38053
   664
    "Rewrite_Set' (" ^ thy' ^ "," ^ bool2str pasm ^ "," ^ id_rls rls' ^ "," ^
neuper@38053
   665
    term2str f ^ ",(" ^ term2str f' ^ "," ^ terms2str asm ^ "))"
neuper@37906
   666
  | End_Detail' _             => "End_Detail' xxx"
neuper@37906
   667
  | Detail_Set' _             => "Detail_Set' xxx"
neuper@37906
   668
  | Detail_Set_Inst' _        => "Detail_Set_Inst' xxx"
neuper@37906
   669
neuper@37906
   670
  | Derive' rls              => "Derive' "^id_rls rls
neuper@37906
   671
  | Calculate'  _            => "Calculate' "
neuper@42360
   672
  | Substitute' _            => "Substitute' "(*^(subs2str subs)*)    
neuper@37906
   673
  | Apply_Assumption' ct's   => "Apply_Assumption' "(*^(strs2str ct's)*)
neuper@37906
   674
neuper@37906
   675
  | Take' cterm'             => "Take' "(*^(quote cterm'	)*)
neuper@37906
   676
  | Take_Inst' cterm'        => "Take_Inst' "(*^(quote cterm' )*)
neuper@37906
   677
  | Group' (con, ints, _)     => 
neuper@37906
   678
      "Group' "^(pair2str (con2str con, ints2str ints))
neuper@41983
   679
  | Subproblem' (spec, oris, _, _, _, pbl_form) => 
neuper@37906
   680
      "Subproblem' "(*^(pair2str (domID, strs2str ,...))*)
neuper@37906
   681
  | End_Subproblem'  _       => "End_Subproblem'"
neuper@37906
   682
  | CAScmd' cterm'           => "CAScmd' "(*^(quote cterm')*)
neuper@37906
   683
neuper@37906
   684
  | Empty_Tac_             => "Empty_Tac_"
neuper@37906
   685
  | User'                    => "User'"
neuper@37906
   686
  | Tac_ (_,form,id,result) => "Tac_ (thy,"^form^","^id^","^result^")"
neuper@37906
   687
  | _                       => "tac_2str not impl. for arg";
neuper@37906
   688
neuper@37906
   689
(*'executed tactics' (tac_s) with local environment etc.;
neuper@37906
   690
  used for continuing eval script + for generate*)
neuper@37906
   691
type ets =
neuper@37906
   692
    (loc_ *      (* of tactic in scr, tactic (weakly) associated with tac_*)
neuper@37906
   693
     (tac_ * 	 (* (for generate)  *)
neuper@37906
   694
      env *      (* with 'tactic=result' as a rule, tactic ev. _not_ ready:
neuper@37906
   695
		  for handling 'parallel let'*)
neuper@37906
   696
      env *      (* with results of (ready) tacs        *)
neuper@37906
   697
      term *     (* itr_arg of tactic, for upd. env at Repeat, Try*)
neuper@37906
   698
      term * 	 (* result value of the tac         *)
neuper@37906
   699
      safe))
neuper@37906
   700
    list;
neuper@37906
   701
val Ets = []:ets;
neuper@37906
   702
neuper@37906
   703
neuper@37906
   704
fun ets2s (l,(m,eno,env,iar,res,s)) = 
neuper@38053
   705
  "\n(" ^ loc_2str l ^ ",(" ^ tac_2str m ^
neuper@38053
   706
  ",\n  ens= " ^ subst2str eno ^
neuper@38053
   707
  ",\n  env= " ^ subst2str env ^
neuper@38053
   708
  ",\n  iar= " ^ term2str iar ^
neuper@38053
   709
  ",\n  res= " ^ term2str res ^
neuper@38053
   710
  ",\n  " ^ safe2str s ^ "))";
neuper@37906
   711
fun ets2str (ets:ets) = (strs2str o (map ets2s)) ets;
neuper@37906
   712
neuper@37906
   713
neuper@37906
   714
type envp =(*9.5.03: unused, delete with field in ptree.PblObj FIXXXME*)
neuper@37906
   715
   (int * term list) list * (*assoc-list: args of met*)
neuper@37906
   716
   (int * rls) list *       (*assoc-list: tacs already done ///15.9.00*)
neuper@37906
   717
   (int * ets) list *       (*assoc-list: tacs etc. already done*)
neuper@37906
   718
   (string * pos) list;     (*asms * from where*)
neuper@37906
   719
val empty_envp = ([],[],[],[]):envp; 
neuper@37906
   720
neuper@37906
   721
datatype ppobj = 
neuper@41975
   722
    PrfObj of 
neuper@41975
   723
     {cell  : lrd option,       (* where in form tac has been applied *)
neuper@41975
   724
	      (*^^^FIXME.WN0607 rename this field*)
neuper@41975
   725
  	  form  : term,             (* where tac is applied to *)   
neuper@41975
   726
  	  tac   : tac,              (* also in istate *)
neuper@41977
   727
  	  loc   : (istate *         (* script interpreter state *)
neuper@41977
   728
  	           Proof.context)   (* context for provers, type inference *)
neuper@41977
   729
              option *          (* both for interpreter location on Frm, Pbl, Met *)
neuper@41977
   730
              (istate *         (* script interpreter state *)
neuper@41977
   731
               Proof.context)   (* context for provers, type inference *)
neuper@41977
   732
              option,           (* both for interpreter location on Res *)
neuper@41977
   733
                                (*(NONE,NONE) <==> e_istate ! see update_loc, get_loc*)
neuper@41975
   734
  	  branch: branch,           (* only rudimentary *)
neuper@41975
   735
  	  result: term * term list, (* result and assumptions *)
neuper@41975
   736
  	  ostate: ostate}           (* Complete <=> result is OK *)
neuper@41975
   737
  | PblObj of 
neuper@41975
   738
     {cell  : lrd option,       (* unused: meaningful only for some _Prf_Obj *)
neuper@41975
   739
	    fmz   : fmz,              (* from init:FIXME never use this spec;-drop *)
neuper@41975
   740
	    origin: (ori list) *      (* representation from fmz+pbt
neuper@41975
   741
                                   for efficiently adding items in probl, meth *)
neuper@41977
   742
		           spec *           (* updated by Refine_Tacitly *)
neuper@41977
   743
		           term,            (* headline of calc-head, as calculated initially(!)*)
neuper@41975
   744
	    spec  : spec,             (* explicitly input *)
neuper@41975
   745
	    probl : itm list,         (* itms explicitly input *)
neuper@41975
   746
	    meth  : itm list,         (* itms automatically added to copy of probl *)
neuper@41988
   747
      ctxt  : Proof.context,    (* WN110513 introduced to avoid [*] [**]*)
neuper@41975
   748
	    env   : (istate * Proof.context) option,
neuper@41977
   749
                                (* istate only for initac in script
neuper@41984
   750
                                   context for specify phase on this node NO..
neuper@41990
   751
..NO: this conflicts with init_form/initac: see Apply_Method without init_form *)
neuper@41984
   752
	    loc   : (istate * Proof.context) option * (istate * (* like PrfObj *)
neuper@41988
   753
                Proof.context) option, (* for spec-phase [*], NO..
neuper@41988
   754
..NO: raises errors not tracable on WN110513 [**]*)                               
neuper@41977
   755
	    branch: branch,           (* like PrfObj *)
neuper@41977
   756
	    result: term * term list, (* like PrfObj *)
neuper@41977
   757
	    ostate: ostate};          (* like PrfObj *)
neuper@37906
   758
neuper@37906
   759
(*.this tree contains isac's calculations; TODO.WN03 rename to ctree;
neuper@37906
   760
   the structure has been copied from an early version of Theorema(c);
neuper@37906
   761
   it has the disadvantage, that there is no space 
neuper@37906
   762
   for the first tactic in a script generating the first formula at (p,Frm);
neuper@37906
   763
   this trouble has been covered by 'init_form' and 'Take' so far,
neuper@37906
   764
   but it is crucial if the first tactic in a script is eg. 'Subproblem';
neuper@37906
   765
   see 'type tac ', Apply_Method.
neuper@37906
   766
.*)
neuper@37906
   767
datatype ptree = 
neuper@37906
   768
    EmptyPtree
neuper@37906
   769
  | Nd of ppobj * (ptree list);
neuper@37906
   770
val e_ptree = EmptyPtree;
neuper@37906
   771
neuper@37906
   772
fun rep_prfobj (PrfObj {cell,form,tac,loc,branch,result,ostate}) =
neuper@37906
   773
  {cell=cell,form=form,tac=tac,loc=loc,branch=branch,result=result,ostate=ostate};
neuper@41988
   774
fun rep_pblobj (PblObj {cell,origin,fmz,spec,probl,meth,ctxt,
neuper@41988
   775
      env,loc,branch,result,ostate}) =
neuper@41988
   776
        {cell=cell,origin=origin,fmz=fmz,spec=spec,probl=probl,meth=meth,ctxt=ctxt,
neuper@41988
   777
         env=env,loc=loc,branch=branch,result=result,ostate=ostate};
neuper@37906
   778
fun is_prfobj (PrfObj _) = true
neuper@37906
   779
  | is_prfobj _ =false;
neuper@37906
   780
(*val is_prfobj' = get_obj is_prfobj; *)
neuper@37906
   781
fun is_pblobj (PblObj _) = true
neuper@37906
   782
  | is_pblobj _ = false;
neuper@37906
   783
(*val is_pblobj' = get_obj is_pblobj; 'Error: unbound constructor get_obj'*)
neuper@37906
   784
neuper@37906
   785
neuper@37906
   786
exception PTREE of string;
neuper@37906
   787
fun nth _ []      = raise PTREE "nth _ []"
neuper@37906
   788
  | nth 1 (x::xs) = x
neuper@37906
   789
  | nth n (x::xs) = nth (n-1) xs;
neuper@37906
   790
(*> nth 2 [11,22,33]; -->> val it = 22 : int*)
neuper@37906
   791
neuper@37906
   792
fun lev_up ([]:pos) = raise PTREE "lev_up []"
neuper@37906
   793
  | lev_up p = (drop_last p):pos;
neuper@37906
   794
fun lev_on ([]:pos) = raise PTREE "lev_on []"
neuper@37906
   795
  | lev_on pos = 
neuper@37906
   796
    let val len = length pos
neuper@37906
   797
    in (drop_last pos) @ [(nth len pos)+1] end;
neuper@37906
   798
fun lev_onFrm ((p,_):pos') = (lev_on p,Frm):pos'
neuper@37906
   799
  | lev_onFrm p = raise PTREE ("*** lev_onFrm: pos'="^(pos'2str p));
neuper@42427
   800
(*040216: for inform --> embed_deriv: remains on same level TODO.WN120517 compare lev_pred*)
neuper@42427
   801
fun lev_back' (([],_):pos') = raise PTREE "lev_back': called by ([],_)"
neuper@42427
   802
  | lev_back' (p,_) =
neuper@37906
   803
    if last_elem p <= 1 then (p, Frm):pos' 
neuper@37906
   804
    else ((drop_last p) @ [(nth (length p) p) - 1], Res);
neuper@37906
   805
(*.increase pos by n within a level.*)
neuper@37906
   806
fun pos_plus 0 pos = pos
neuper@37906
   807
  | pos_plus n ((p,Frm):pos') = pos_plus (n-1) (p, Res)
neuper@37906
   808
  | pos_plus n ((p,  _):pos') = pos_plus (n-1) (lev_on p, Res);
neuper@37906
   809
neuper@37906
   810
fun lev_pred ([]:pos) = raise PTREE "lev_pred []"
neuper@37906
   811
  | lev_pred (pos:pos) = 
neuper@37906
   812
    let val len = length pos
neuper@37906
   813
    in ((drop_last pos) @ [(nth len pos)-1]):pos end;
neuper@37906
   814
(*lev_pred [1,2,3];
neuper@37906
   815
val it = [1,2,2] : pos
neuper@37906
   816
> lev_pred [1];
neuper@37906
   817
val it = [0] : pos          *)
neuper@37906
   818
neuper@37906
   819
fun lev_dn p = p @ [0];
neuper@37906
   820
(*> (lev_dn o lev_on) [1,2,3];
neuper@37906
   821
val it = [1,2,4,0] : pos    *)
neuper@37906
   822
(*fun lev_dn' ((p,p_):pos') = (lev_dn p, Frm):pos'; WN.3.12.03: never used*)
neuper@37906
   823
fun lev_dnRes ((p,_):pos') = (lev_dn p, Res):pos';
neuper@37906
   824
neuper@37906
   825
(*4.4.00*)
neuper@37906
   826
fun lev_up_ ((p,Res):pos') = (lev_up p,Res):pos'
neuper@38031
   827
  | lev_up_ p' = error ("lev_up_: called for "^(pos'2str p'));
neuper@37906
   828
fun lev_dn_ ((p,_):pos') = (lev_dn p,Res):pos'
neuper@37906
   829
fun ind ((p,_):pos') = length p; (*WN050108 deprecated in favour of lev_of*)
neuper@37906
   830
fun lev_of ((p,_):pos') = length p;
neuper@37906
   831
neuper@37906
   832
neuper@37906
   833
(** convert ptree to a string **)
neuper@37906
   834
neuper@37906
   835
(* convert a pos from list to string *)
neuper@37906
   836
fun pr_pos ps = (space_implode "." (map string_of_int ps))^".   ";
neuper@37906
   837
(* show hd origin or form only *)
neuper@37906
   838
fun pr_short (p:pos) (PblObj {origin = (ori,_,_),...}) = 
neuper@37906
   839
  ((pr_pos p) ^ " ----- pblobj -----\n")
neuper@37929
   840
(*   ((((Syntax.string_of_term (thy2ctxt' "Isac")) o #4 o hd) ori)^" "^
neuper@37929
   841
    (((Syntax.string_of_term (thy2ctxt' "Isac")) o hd(*!?!*) o #5 o hd) ori))^
neuper@37906
   842
   "\n") *)
neuper@37906
   843
  | pr_short p (PrfObj {form = form,...}) =
neuper@37906
   844
  ((pr_pos p) ^ (term2str form) ^ "\n");
neuper@37906
   845
(*
neuper@37906
   846
fun pr_cell (p:pos) (PblObj {cell = c, origin = (ori,_,_),...}) = 
neuper@37906
   847
  ((ints2str c) ^"   "^ 
neuper@37929
   848
   ((((Syntax.string_of_term (thy2ctxt' "Isac")) o #4 o hd) ori)^" "^
neuper@37929
   849
    (((Syntax.string_of_term (thy2ctxt' "Isac")) o hd(*!?!*) o #5 o hd) ori))^
neuper@37906
   850
   "\n")
neuper@37906
   851
  | pr_cell p (PrfObj {cell = c, form = form,...}) =
neuper@37906
   852
  ((ints2str c) ^"   "^ (term2str form) ^ "\n");
neuper@37906
   853
*)
neuper@37906
   854
neuper@37906
   855
(* convert ptree *)
neuper@37906
   856
fun pr_ptree f pt =
neuper@37906
   857
  let
neuper@37906
   858
    fun pr_pt pfn _  EmptyPtree = ""
neuper@37906
   859
      | pr_pt pfn ps (Nd (b, [])) = pfn ps b
neuper@37906
   860
      | pr_pt pfn ps (Nd (b, ts)) = (pfn ps b)^
neuper@37906
   861
      (prts pfn (ps:pos) 1 ts)
neuper@37906
   862
    and prts pfn ps p [] = ""
neuper@37906
   863
      | prts pfn ps p (t::ts) = (pr_pt pfn (ps @ [p]) t)^
neuper@37906
   864
      (prts pfn ps (p+1) ts)
neuper@37906
   865
  in pr_pt f [] pt end;
neuper@37906
   866
(*
neuper@37906
   867
> fun prfn ps b = (pr_pos ps)^"   "^b(*TODO*)^"\n";
neuper@38006
   868
> val pt = Unsynchronized.ref EmptyPtree;
neuper@37986
   869
> pt:=Nd("root'",
neuper@37906
   870
       [Nd("xx1",[]),
neuper@37906
   871
	Nd("xx2",
neuper@37906
   872
	   [Nd("xx2.1.",[]),
neuper@37906
   873
	    Nd("xx2.2.",[])]),
neuper@37906
   874
	Nd("xx3",[])]);
neuper@38015
   875
> tracing (pr_ptree prfn (!pt));
neuper@37906
   876
*)
neuper@37906
   877
neuper@37906
   878
neuper@37906
   879
(** access the branches of ptree **)
neuper@37906
   880
neuper@37906
   881
fun ins_nth 1 e l  = e::l
neuper@37906
   882
  | ins_nth n e [] = raise PTREE "ins_nth n e []"
neuper@37906
   883
  | ins_nth n e (l::ls) = l::(ins_nth (n-1) e ls);
neuper@37906
   884
fun repl []      _ _ = raise PTREE "repl [] _ _"
neuper@37906
   885
  | repl (l::ls) 1 e = e::ls
neuper@37906
   886
  | repl (l::ls) n e = l::(repl ls (n-1) e);
neuper@37906
   887
fun repl_app ls n e = 
neuper@37906
   888
    let val lim = 1 + length ls
neuper@37906
   889
    in if n > lim then raise PTREE "repl_app: n > lim"
neuper@37906
   890
       else if n = lim then ls @ [e]
neuper@37906
   891
	    else repl ls n e end;
neuper@37906
   892
(*  
neuper@37906
   893
> repl [1,2,3] 2 22222;
neuper@37906
   894
val it = [1,22222,3] : int list
neuper@37906
   895
> repl_app [1,2,3,4] 5 5555;
neuper@37906
   896
val it = [1,2,3,4,5555] : int list
neuper@37906
   897
> repl_app [1,2,3] 2 22222;
neuper@37906
   898
val it = [1,22222,3] : int list
neuper@37906
   899
> repl_app [1] 2 22222 ;
neuper@37906
   900
val it = [1,22222] : int list
neuper@37906
   901
*)
neuper@37906
   902
neuper@37906
   903
neuper@37906
   904
(*.get from obj at pos by f : ppobj -> 'a.*)
neuper@37906
   905
fun get_obj f EmptyPtree  (_:pos)  = raise PTREE "get_obj f EmptyPtree"
neuper@37906
   906
  | get_obj f (Nd (b,  _)) []      = f b
neuper@37906
   907
  | get_obj f (Nd (b, bs)) (p::ps) =
neuper@37906
   908
(* val (f, Nd (b, bs), (p::ps)) = (I, pt, p);
neuper@37906
   909
   *)
neuper@37906
   910
  let val _ = (nth p bs) handle _ => raise PTREE ("get_obj: pos = "^
neuper@37906
   911
			   (ints2str' (p::ps))^" does not exist");
neuper@37906
   912
  in (get_obj f (nth p bs) (ps:pos)) 
neuper@37906
   913
      (*before WN050419: 'wrong type..' raised also if pos doesn't exist*)
neuper@37906
   914
    handle _ => raise PTREE (*"get_obj: at pos = "^
neuper@37906
   915
			     (ints2str' (p::ps))^" wrong type of ppobj"*)
neuper@37906
   916
			  ("get_obj: pos = "^
neuper@37906
   917
			   (ints2str' (p::ps))^" does not exist")
neuper@37906
   918
  end;
neuper@37906
   919
fun get_nd EmptyPtree _ = raise PTREE "get_nd EmptyPtree"
neuper@37906
   920
  | get_nd n [] = n
neuper@37906
   921
  | get_nd (Nd (_,nds)) (pos as p::(ps:pos)) = (get_nd (nth p nds) ps)
neuper@37906
   922
    handle _ => raise PTREE ("get_nd: not existent pos = "^(ints2str' pos));
neuper@37906
   923
neuper@37906
   924
(* for use by get_obj *)
neuper@37925
   925
fun g_cell   (PblObj {cell = c,...}) = NONE
neuper@37906
   926
  | g_cell   (PrfObj {cell = c,...}) = c;(*WN0607 hack for quick introduction of lrd + rewrite-at (thms, calcs)*)
neuper@37906
   927
fun g_form   (PrfObj {form = f,...}) = f
neuper@37906
   928
  | g_form   (PblObj {origin=(_,_,f),...}) = f;
neuper@37906
   929
fun g_form' (Nd (PrfObj {form = f,...}, _)) = f
neuper@37906
   930
  | g_form' (Nd (PblObj {origin=(_,_,f),...}, _)) = f;
neuper@37906
   931
(*  | g_form   _ = raise PTREE "g_form not for PblObj";*)
neuper@37906
   932
fun g_origin (PblObj {origin = ori,...}) = ori
neuper@37906
   933
  | g_origin _ = raise PTREE "g_origin not for PrfObj";
neuper@37906
   934
fun g_fmz (PblObj {fmz = f,...}) = f
neuper@37906
   935
  | g_fmz _ = raise PTREE "g_fmz not for PrfObj";
neuper@37906
   936
fun g_spec   (PblObj {spec = s,...}) = s
neuper@37906
   937
  | g_spec _   = raise PTREE "g_spec not for PrfObj";
neuper@37906
   938
fun g_pbl    (PblObj {probl = p,...}) = p
neuper@37906
   939
  | g_pbl  _   = raise PTREE "g_pbl not for PrfObj";
neuper@37906
   940
fun g_met    (PblObj {meth = p,...}) = p
neuper@37906
   941
  | g_met  _   = raise PTREE "g_met not for PrfObj";
neuper@37906
   942
fun g_domID  (PblObj {spec = (d,_,_),...}) = d
neuper@37906
   943
  | g_domID  _ = raise PTREE "g_metID not for PrfObj";
neuper@37906
   944
fun g_metID  (PblObj {spec = (_,_,m),...}) = m
neuper@37906
   945
  | g_metID  _ = raise PTREE "g_metID not for PrfObj";
neuper@41989
   946
fun g_ctxt    (PblObj {ctxt, ...}) = ctxt
neuper@41989
   947
  | g_ctxt    _ = raise PTREE "g_ctxt not for PrfObj"; 
neuper@37906
   948
fun g_env    (PblObj {env,...}) = env
neuper@37906
   949
  | g_env    _ = raise PTREE "g_env not for PrfObj"; 
neuper@37906
   950
fun g_loc    (PblObj {loc = l,...}) = l
neuper@37906
   951
  | g_loc    (PrfObj {loc = l,...}) = l;
neuper@37906
   952
fun g_branch (PblObj {branch = b,...}) = b
neuper@37906
   953
  | g_branch (PrfObj {branch = b,...}) = b;
neuper@37906
   954
fun g_tac  (PblObj {spec = (d,p,m),...}) = Apply_Method m
neuper@37906
   955
  | g_tac  (PrfObj {tac = m,...}) = m;
neuper@37906
   956
fun g_result (PblObj {result = r,...}) = r
neuper@37906
   957
  | g_result (PrfObj {result = r,...}) = r;
neuper@37906
   958
fun g_res (PblObj {result = (r,_),...}) = r
neuper@37906
   959
  | g_res (PrfObj {result = (r,_),...}) = r;
neuper@37906
   960
fun g_res' (Nd (PblObj {result = (r,_),...}, _)) = r
neuper@37906
   961
  | g_res' (Nd (PrfObj {result = (r,_),...}, _)) = r;
neuper@37906
   962
fun g_ostate (PblObj {ostate = r,...}) = r
neuper@37906
   963
  | g_ostate (PrfObj {ostate = r,...}) = r;
neuper@37906
   964
fun g_ostate' (Nd (PblObj {ostate = r,...}, _)) = r
neuper@37906
   965
  | g_ostate' (Nd (PrfObj {ostate = r,...}, _)) = r;
neuper@37906
   966
neuper@37925
   967
fun gpt_cell (Nd (PblObj {cell = c,...},_)) = NONE
neuper@37906
   968
  | gpt_cell (Nd (PrfObj {cell = c,...},_)) = c;
neuper@37906
   969
neuper@42428
   970
(* get the formula preceeding the current position in a calculation *)
neuper@42431
   971
fun get_curr_formula (pt, pos as (p, p_)) = 
neuper@42428
   972
	  case p_ of
neuper@42428
   973
	    Frm => get_obj g_form pt p
neuper@42428
   974
			  | Res => (fst o (get_obj g_result pt)) p
neuper@42428
   975
			  | _ => #3 (get_obj g_origin pt p);
neuper@42428
   976
neuper@37906
   977
(*in CalcTree/Subproblem an 'just_created_' model is created;
neuper@37906
   978
  this is filled to 'untouched' by Model/Refine_Problem*)
neuper@37906
   979
fun just_created_ (PblObj {meth, probl, spec, ...}) = 
neuper@37906
   980
    null meth andalso null probl andalso spec = e_spec;
neuper@37906
   981
val e_origin = ([],e_spec,e_term): (ori list) * spec * term;
neuper@37906
   982
neuper@37906
   983
fun just_created (pt,(p,_):pos') =
neuper@37906
   984
    let val ppobj = get_obj I pt p
neuper@37906
   985
    in is_pblobj ppobj andalso just_created_ ppobj end;
neuper@37906
   986
neuper@37906
   987
(*.does the pos in the ctree exist ?.*)
neuper@37906
   988
fun existpt pos pt = can (get_obj I pt) pos;
neuper@37906
   989
(*.does the pos' in the ctree exist, ie. extra check for result in the node.*)
neuper@37906
   990
fun existpt' ((p,p_):pos') pt = 
neuper@37906
   991
    if can (get_obj I pt) p 
neuper@37906
   992
    then case p_ of 
neuper@37906
   993
	     Res => get_obj g_ostate pt p = Complete
neuper@37906
   994
	   | _ => true
neuper@37906
   995
    else false;
neuper@37906
   996
neuper@37906
   997
(*.is this position appropriate for calculating intermediate steps?.*)
neuper@37906
   998
fun is_interpos ((_, Res):pos') = true
neuper@37906
   999
  | is_interpos _ = false;
neuper@37906
  1000
neuper@37906
  1001
fun last_onlev pt pos = not (existpt (lev_on pos) pt);
neuper@37906
  1002
neuper@37906
  1003
neuper@37906
  1004
(*.find the position of the next parent which is a PblObj in ptree.*)
neuper@37906
  1005
fun par_pblobj pt ([]:pos) = ([]:pos)
neuper@37906
  1006
  | par_pblobj pt p =
neuper@37906
  1007
    let fun par pt [] = []
neuper@37906
  1008
	  | par pt p = if is_pblobj (get_obj I pt p) then p
neuper@37906
  1009
		       else par pt (lev_up p)
neuper@37906
  1010
    in par pt (lev_up p) end; 
neuper@37906
  1011
(* lev_up for hard_gen operating with pos = [...,0] *)
neuper@37906
  1012
neuper@37906
  1013
(*.find the position and the children of the next parent which is a PblObj.*)
neuper@37906
  1014
fun par_children (Nd (PblObj _, children)) ([]:pos) = (children, []:pos)
neuper@37906
  1015
  | par_children (pt as Nd (PblObj _, children)) p =
neuper@37906
  1016
    let fun par [] = (children, [])
neuper@37906
  1017
	  | par p = let val Nd (obj, children) = get_nd pt p
neuper@37906
  1018
		    in if is_pblobj obj then (children, p) else par (lev_up p)
neuper@37906
  1019
		    end;
neuper@37906
  1020
    in par (lev_up p) end; 
neuper@37906
  1021
neuper@37906
  1022
(*.get the children of a node in ptree.*)
neuper@37906
  1023
fun children (Nd (PblObj _, cn)) = cn
neuper@37906
  1024
  | children (Nd (PrfObj _, cn)) = cn;
neuper@37906
  1025
neuper@37906
  1026
neuper@37906
  1027
(*.find the next parent, which is either a PblObj (return true)
neuper@37906
  1028
  or a PrfObj with tac = Detail_Set (return false).*)
neuper@37906
  1029
(*FIXME.3.4.03:re-organize par_pbl_det after rls' --> rls*)
neuper@37906
  1030
fun par_pbl_det pt ([]:pos) = (true, []:pos, Erls)
neuper@37906
  1031
  | par_pbl_det pt p =
neuper@37906
  1032
    let fun par pt [] = (true, [], Erls)
neuper@37906
  1033
	  | par pt p = if is_pblobj (get_obj I pt p) then (true, p, Erls)
neuper@37906
  1034
		       else case get_obj g_tac pt p of
neuper@37906
  1035
				(*Detail_Set rls' => (false, p, assoc_rls rls')
neuper@37906
  1036
			      (*^^^--- before 040206 after ---vvv*)
neuper@37906
  1037
			      |*)Rewrite_Set rls' => (false, p, assoc_rls rls')
neuper@37906
  1038
			      | Rewrite_Set_Inst (_, rls') => 
neuper@37906
  1039
				(false, p, assoc_rls rls')
neuper@37906
  1040
			      | _ => par pt (lev_up p)
neuper@37906
  1041
    in par pt (lev_up p) end; 
neuper@37906
  1042
neuper@37906
  1043
neuper@37906
  1044
neuper@37906
  1045
neuper@37906
  1046
(*.get from the whole ptree by f : ppobj -> 'a.*)
neuper@37906
  1047
fun get_all f EmptyPtree   = []
neuper@37906
  1048
  | get_all f (Nd (b, [])) = [f b]
neuper@37906
  1049
  | get_all f (Nd (b, bs)) = [f b] @ (get_alls f bs)
neuper@37906
  1050
and get_alls f [] = []
neuper@37906
  1051
  | get_alls f pts = flat (map (get_all f) pts);
neuper@37906
  1052
neuper@37906
  1053
neuper@41931
  1054
(*.insert obj b into ptree at pos, ev.overwriting this pos.
neuper@41931
  1055
covers library.ML TODO.WN110315 rename*)
neuper@52111
  1056
fun insert_pt b EmptyPtree   ([]:pos)  = Nd (b, [])
neuper@52111
  1057
  | insert_pt b EmptyPtree    _        = raise PTREE "insert_pt b Empty _"
neuper@52111
  1058
  | insert_pt b (Nd ( _,  _)) []       = raise PTREE "insert_pt b _ []"
neuper@52111
  1059
  | insert_pt b (Nd (b', bs)) (p::[])  = 
neuper@37906
  1060
     Nd (b', repl_app bs p (Nd (b,[]))) 
neuper@52111
  1061
  | insert_pt b (Nd (b', bs)) (p::ps)  =
neuper@52111
  1062
     Nd (b', repl_app bs p (insert_pt b (nth p bs) ps));
neuper@37906
  1063
(*
neuper@37906
  1064
> type ppobj = string;
neuper@38015
  1065
> tracing (pr_ptree prfn (!pt));
neuper@38006
  1066
  val pt = Unsynchronized.ref Empty;
neuper@52111
  1067
  pt:= insert_pt ("root'":ppobj) EmptyPtree [];
neuper@52111
  1068
  pt:= insert_pt ("xx1":ppobj) (!pt) [1];
neuper@52111
  1069
  pt:= insert_pt ("xx2":ppobj) (!pt) [2];
neuper@52111
  1070
  pt:= insert_pt ("xx3":ppobj) (!pt) [3];
neuper@52111
  1071
  pt:= insert_pt ("xx2.1":ppobj) (!pt) [2,1];
neuper@52111
  1072
  pt:= insert_pt ("xx2.2":ppobj) (!pt) [2,2];
neuper@52111
  1073
  pt:= insert_pt ("xx2.1.1":ppobj) (!pt) [2,1,1];
neuper@52111
  1074
  pt:= insert_pt ("xx2.1.2":ppobj) (!pt) [2,1,2];
neuper@52111
  1075
  pt:= insert_pt ("xx2.1.3":ppobj) (!pt) [2,1,3];
neuper@37906
  1076
*)
neuper@37906
  1077
neuper@37906
  1078
(*.insert children to a node without children.*)
neuper@52111
  1079
(*compare: fun insert_pt*)
neuper@37906
  1080
fun ins_chn _  EmptyPtree   (_:pos) = raise PTREE "ins_chn: EmptyPtree"
neuper@37906
  1081
  | ins_chn ns (Nd _)       []      = raise PTREE "ins_chn: pos = []"
neuper@37906
  1082
  | ins_chn ns (Nd (b, bs)) (p::[]) =
neuper@37906
  1083
    if p > length bs then raise PTREE "ins_chn: pos not existent"
neuper@37906
  1084
    else let val Nd (b', bs') = nth p bs
neuper@37906
  1085
	 in if null bs' then Nd (b, repl_app bs p (Nd (b', ns)))
neuper@37906
  1086
	    else raise PTREE "ins_chn: pos mustNOT be overwritten" end
neuper@37906
  1087
  | ins_chn ns (Nd (b, bs)) (p::ps) =
neuper@37906
  1088
     Nd (b, repl_app bs p (ins_chn ns (nth p bs) ps));
neuper@37906
  1089
neuper@52111
  1090
(* print_depth 11;ins_chn;print_depth 3; ###insert_pt#########################*);
neuper@37906
  1091
neuper@37906
  1092
neuper@37906
  1093
(** apply f to obj at pos, f: ppobj -> ppobj **)
neuper@37906
  1094
neuper@37906
  1095
fun appl_to_node f (Nd (b,bs)) = Nd (f b, bs);
neuper@37906
  1096
fun appl_obj f EmptyPtree    []      = EmptyPtree
neuper@37906
  1097
  | appl_obj f EmptyPtree    _       = raise PTREE "appl_obj f Empty _"
neuper@37906
  1098
  | appl_obj f (Nd (b, bs)) []       = Nd (f b, bs)
neuper@37906
  1099
  | appl_obj f (Nd (b, bs)) (p::[])  = 
neuper@37906
  1100
     Nd (b, repl_app bs p (((appl_to_node f) o (nth p)) bs))
neuper@37906
  1101
  | appl_obj f (Nd (b, bs)) (p::ps)  =
neuper@37906
  1102
     Nd (b, repl_app bs p (appl_obj f (nth p bs) (ps:pos)));
neuper@37906
  1103
 
neuper@37906
  1104
(* for use by appl_obj *) 
neuper@37906
  1105
fun repl_form f (PrfObj {cell=c,form= _,tac=tac,loc=loc,
neuper@37906
  1106
			 branch=branch,result=result,ostate=ostate}) =
neuper@37906
  1107
    PrfObj {cell=c,form= f,tac=tac,loc=loc,
neuper@37906
  1108
	    branch=branch,result=result,ostate=ostate}
neuper@37906
  1109
  | repl_form _ _ = raise PTREE "repl_form takes no PblObj";
neuper@37906
  1110
fun repl_pbl x    (PblObj {cell=cell,origin=origin,fmz=fmz,
neuper@41988
  1111
			   spec=spec,probl=_,meth=meth,ctxt=ctxt,env=env,loc=loc,
neuper@37906
  1112
			   branch=branch,result=result,ostate=ostate}) =
neuper@37906
  1113
  PblObj {cell=cell,origin=origin,fmz=fmz,spec=spec,probl= x,
neuper@41988
  1114
	  meth=meth,ctxt=ctxt,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
neuper@37906
  1115
  | repl_pbl _ _ = raise PTREE "repl_pbl takes no PrfObj";
neuper@37906
  1116
fun repl_met x    (PblObj {cell=cell,origin=origin,fmz=fmz,
neuper@41988
  1117
			   spec=spec,probl=probl,meth=_,ctxt=ctxt,env=env,loc=loc,
neuper@37906
  1118
			   branch=branch,result=result,ostate=ostate}) =
neuper@37906
  1119
  PblObj {cell=cell,origin=origin,fmz=fmz,spec=spec,probl=probl,
neuper@41988
  1120
	  meth= x,ctxt=ctxt,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
neuper@37906
  1121
  | repl_met _ _ = raise PTREE "repl_pbl takes no PrfObj";
neuper@37906
  1122
neuper@37906
  1123
fun repl_spec  x    (PblObj {cell=cell,origin=origin,fmz=fmz,
neuper@41988
  1124
			   spec= _,probl=probl,meth=meth,ctxt=ctxt,env=env,loc=loc,
neuper@37906
  1125
			   branch=branch,result=result,ostate=ostate}) =
neuper@37906
  1126
  PblObj {cell=cell,origin=origin,fmz=fmz,spec= x,probl=probl,
neuper@41988
  1127
	  meth=meth,ctxt=ctxt,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
neuper@37906
  1128
  | repl_spec  _ _ = raise PTREE "repl_domID takes no PrfObj";
neuper@37906
  1129
fun repl_domID x    (PblObj {cell=cell,origin=origin,fmz=fmz,
neuper@41988
  1130
			   spec=(_,p,m),probl=probl,meth=meth,ctxt=ctxt,env=env,loc=loc,
neuper@37906
  1131
			   branch=branch,result=result,ostate=ostate}) =
neuper@37906
  1132
  PblObj {cell=cell,origin=origin,fmz=fmz,spec=(x,p,m),probl=probl,
neuper@41988
  1133
	  meth=meth,ctxt=ctxt,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
neuper@37906
  1134
  | repl_domID _ _ = raise PTREE "repl_domID takes no PrfObj";
neuper@37906
  1135
fun repl_pblID x    (PblObj {cell=cell,origin=origin,fmz=fmz,
neuper@41988
  1136
			   spec=(d,_,m),probl=probl,meth=meth,ctxt=ctxt,env=env,loc=loc,
neuper@37906
  1137
			   branch=branch,result=result,ostate=ostate}) =
neuper@37906
  1138
  PblObj {cell=cell,origin=origin,fmz=fmz,spec=(d,x,m),probl=probl,
neuper@41988
  1139
	  meth=meth,ctxt=ctxt,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
neuper@37906
  1140
  | repl_pblID _ _ = raise PTREE "repl_pblID takes no PrfObj";
neuper@37906
  1141
fun repl_metID x (PblObj {cell=cell,origin=origin,fmz=fmz,
neuper@41988
  1142
			   spec=(d,p,_),probl=probl,meth=meth,ctxt=ctxt,env=env,loc=loc,
neuper@37906
  1143
			   branch=branch,result=result,ostate=ostate}) =
neuper@37906
  1144
  PblObj {cell=cell,origin=origin,fmz=fmz,spec=(d,p,x),probl=probl,
neuper@41988
  1145
	  meth=meth,ctxt=ctxt,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
neuper@37906
  1146
  | repl_metID _ _ = raise PTREE "repl_metID takes no PrfObj";
neuper@37906
  1147
neuper@37906
  1148
fun repl_result l f' s (PrfObj {cell=cell,form=form,tac=tac,loc=_,
neuper@37906
  1149
			     branch=branch,result = _ ,ostate = _}) =
neuper@37906
  1150
    PrfObj {cell=cell,form=form,tac=tac,loc= l,
neuper@37906
  1151
	    branch=branch,result = f',ostate = s}
neuper@37906
  1152
  | repl_result l f' s (PblObj {cell=cell,origin=origin,fmz=fmz,
neuper@41988
  1153
			     spec=spec,probl=probl,meth=meth,ctxt=ctxt,env=env,loc=_,
neuper@37906
  1154
			     branch=branch,result= _ ,ostate= _}) =
neuper@37906
  1155
    PblObj {cell=cell,origin=origin,fmz=fmz,
neuper@41988
  1156
	    spec=spec,probl=probl,meth=meth,ctxt=ctxt,env=env,loc= l,
neuper@37906
  1157
	    branch=branch,result= f',ostate= s};
neuper@37906
  1158
neuper@37906
  1159
fun repl_tac x (PrfObj {cell=cell,form=form,tac= _,loc=loc,
neuper@37906
  1160
			  branch=branch,result=result,ostate=ostate}) =
neuper@37906
  1161
    PrfObj {cell=cell,form=form,tac= x,loc=loc,
neuper@37906
  1162
	    branch=branch,result=result,ostate=ostate}
neuper@37906
  1163
  | repl_tac _ _ = raise PTREE "repl_tac takes no PblObj";
neuper@37906
  1164
neuper@37906
  1165
fun repl_branch b (PblObj {cell=cell,origin=origin,fmz=fmz,
neuper@41988
  1166
			   spec=spec,probl=probl,meth=meth,ctxt=ctxt,env=env,loc=loc,
neuper@37906
  1167
			   branch= _,result=result,ostate=ostate}) =
neuper@37906
  1168
  PblObj {cell=cell,origin=origin,fmz=fmz,spec=spec,probl=probl,
neuper@41988
  1169
	  meth=meth,ctxt=ctxt,env=env,loc=loc,branch= b,result=result,ostate=ostate}
neuper@37906
  1170
  | repl_branch b (PrfObj {cell=cell,form=form,tac=tac,loc=loc,
neuper@37906
  1171
			  branch= _,result=result,ostate=ostate}) =
neuper@37906
  1172
    PrfObj {cell=cell,form=form,tac=tac,loc=loc,
neuper@37906
  1173
	    branch= b,result=result,ostate=ostate};
neuper@37906
  1174
neuper@41989
  1175
fun repl_ctxt x
neuper@41989
  1176
      (PblObj {cell, origin, fmz, spec, probl, meth,
neuper@41989
  1177
       ctxt=_, env, loc, branch, result, ostate}) =
neuper@41989
  1178
         PblObj {cell=cell, origin=origin, fmz=fmz, spec=spec, probl=probl, meth=meth,
neuper@41989
  1179
          ctxt=x, env=env, loc=loc, branch=branch, result=result, ostate=ostate}
neuper@41989
  1180
    | repl_ctxt _ _ = raise PTREE "repl_env takes no PrfObj";
neuper@41989
  1181
neuper@37906
  1182
fun repl_env e
neuper@41989
  1183
      (PblObj {cell=cell, origin=origin, fmz=fmz, spec=spec, probl=probl, meth=meth,
neuper@41989
  1184
       ctxt=ctxt, env=_, loc=loc, branch=branch, result=result, ostate=ostate}) =
neuper@41989
  1185
         PblObj {cell=cell, origin=origin, fmz=fmz, spec=spec, probl=probl, meth=meth,
neuper@41989
  1186
          ctxt=ctxt, env=e, loc=loc, branch=branch, result=result, ostate=ostate}
neuper@41989
  1187
    | repl_env _ _ = raise PTREE "repl_env takes no PrfObj";
neuper@37906
  1188
neuper@37906
  1189
fun repl_oris oris
neuper@37906
  1190
  (PblObj {cell=cell,origin=(_,spe,hdf),fmz=fmz,
neuper@41988
  1191
	   spec=spec,probl=probl,meth=meth,ctxt=ctxt,env=env,loc=loc,
neuper@37906
  1192
	   branch=branch,result=result,ostate=ostate}) =
neuper@37906
  1193
  PblObj{cell=cell,origin=(oris,spe,hdf),fmz=fmz,spec=spec,probl=probl,
neuper@41988
  1194
	  meth=meth,ctxt=ctxt,env=env,loc=loc,branch=branch,
neuper@37906
  1195
	  result=result,ostate=ostate}
neuper@37906
  1196
  | repl_oris _ _ = raise PTREE "repl_oris takes no PrfObj";
neuper@37906
  1197
fun repl_orispec spe
neuper@37906
  1198
  (PblObj {cell=cell,origin=(oris,_,hdf),fmz=fmz,
neuper@41988
  1199
	   spec=spec,probl=probl,meth=meth,ctxt=ctxt,env=env,loc=loc,
neuper@37906
  1200
	   branch=branch,result=result,ostate=ostate}) =
neuper@37906
  1201
  PblObj{cell=cell,origin=(oris,spe,hdf),fmz=fmz,spec=spec,probl=probl,
neuper@41988
  1202
	  meth=meth,ctxt=ctxt,env=env,loc=loc,branch=branch,
neuper@37906
  1203
	  result=result,ostate=ostate}
neuper@37906
  1204
  | repl_orispec _ _ = raise PTREE "repl_orispec takes no PrfObj";
neuper@37906
  1205
neuper@41988
  1206
fun repl_loc l (PblObj {cell=cell,origin=origin,fmz=fmz,spec=spec,probl=probl,meth=meth,
neuper@41988
  1207
    ctxt=ctxt,env=env,loc=_,branch=branch,result=result,ostate=ostate}) =
neuper@41988
  1208
       PblObj {cell=cell,origin=origin,fmz=fmz,spec=spec,probl=probl,meth=meth,
neuper@41988
  1209
         ctxt=ctxt,env=env,loc=l,branch=branch,result=result,ostate=ostate}
neuper@41983
  1210
  | repl_loc l (PrfObj {cell=cell,form=form,tac=tac,
neuper@41983
  1211
    loc=_,branch=branch,result=result,ostate=ostate}) =
neuper@41983
  1212
       PrfObj {cell=cell,form=form,tac=tac,
neuper@41983
  1213
       loc= l,branch=branch,result=result,ostate=ostate};
neuper@37906
  1214
neuper@37906
  1215
(*WN050219 put here for interpreting code for cut_tree below...*)
neuper@37906
  1216
type ocalhd =
neuper@37906
  1217
     bool *                (*ALL itms+preconds true*)
neuper@37906
  1218
     pos_ *                (*model belongs to Problem | Method*)
neuper@37906
  1219
     term *                (*header: Problem... or Cas
neuper@37906
  1220
				FIXXXME.12.03: item! for marking syntaxerrors*)
neuper@37906
  1221
     itm list *            (*model: given, find, relate*)
neuper@37906
  1222
     ((bool * term) list) *(*model: preconds*)
neuper@37906
  1223
     spec;                 (*specification*)
neuper@37906
  1224
val e_ocalhd = (false, Und, e_term, [e_itm], [(false, e_term)], e_spec);
neuper@37906
  1225
neuper@37906
  1226
datatype ptform =
neuper@37906
  1227
	 Form of term
neuper@37906
  1228
       | ModSpec of ocalhd;
neuper@37906
  1229
val e_ptform = Form e_term;
neuper@37906
  1230
val e_ptform' = ModSpec e_ocalhd;
neuper@37906
  1231
neuper@37906
  1232
neuper@37906
  1233
neuper@37906
  1234
(*.applies (snd f) to the branches at a pos if ((fst f) b),
neuper@37906
  1235
   f : (ppobj -> bool) * (int -> ptree list -> ptree list).*)
neuper@37906
  1236
neuper@37906
  1237
fun appl_branch f EmptyPtree [] = (EmptyPtree, false)
neuper@37906
  1238
  | appl_branch f EmptyPtree _  = raise PTREE "appl_branch f Empty _"
neuper@37906
  1239
  | appl_branch f (Nd ( _, _)) [] = raise PTREE "appl_branch f _ []"
neuper@37906
  1240
  | appl_branch f (Nd (b, bs)) (p::[]) = 
neuper@37906
  1241
    if (fst f) b then (Nd (b, (snd f) (p:posel) bs), true)
neuper@37906
  1242
    else (Nd (b, bs), false)
neuper@37906
  1243
  | appl_branch f (Nd (b, bs)) (p::ps) =
neuper@37906
  1244
	let val (b',bool) = appl_branch f (nth p bs) ps
neuper@37906
  1245
	in (Nd (b, repl_app bs p b'), bool) end;
neuper@37906
  1246
neuper@37906
  1247
(* for cut_level;  appl_branch(deprecated) *)
neuper@37906
  1248
fun test_trans (PrfObj{branch = Transitive,...}) = true
neuper@37906
  1249
  | test_trans (PblObj{branch = Transitive,...}) = true
neuper@37906
  1250
  | test_trans _ = false;
neuper@37906
  1251
neuper@37906
  1252
fun is_pblobj' pt (p:pos) =
neuper@37906
  1253
    let val ppobj = get_obj I pt p
neuper@37906
  1254
    in is_pblobj ppobj end;
neuper@37906
  1255
neuper@37906
  1256
fun delete_result pt (p:pos) =
neuper@37925
  1257
    (appl_obj (repl_result (fst (get_obj g_loc pt p), NONE) 
neuper@37906
  1258
			   (e_term,[]) Incomplete) pt p);
neuper@37906
  1259
neuper@37906
  1260
fun del_res (PblObj {cell, fmz, origin, spec, probl, meth, 
neuper@41988
  1261
		     ctxt, env, loc=(l1,_), branch, result, ostate}) =
neuper@37906
  1262
    PblObj {cell=cell,fmz=fmz,origin=origin,spec=spec,probl=probl,meth=meth,
neuper@41988
  1263
	    ctxt=ctxt,env=env, loc=(l1,NONE), branch=branch, result=(e_term,[]), 
neuper@37906
  1264
	    ostate=Incomplete}
neuper@37906
  1265
neuper@37906
  1266
  | del_res (PrfObj {cell, form, tac, loc=(l1,_), branch, result, ostate}) =
neuper@37925
  1267
    PrfObj {cell=cell,form=form,tac=tac, loc=(l1,NONE), branch=branch, 
neuper@37906
  1268
	    result=(e_term,[]), ostate=Incomplete};
neuper@37906
  1269
neuper@42009
  1270
(*FIXME.WN0312 update_X X pos pt -> pt could be chained by o (efficiency?)*)
neuper@42009
  1271
(*fun update_fmz  pt pos x = appl_obj (repl_fmz  x) pt pos; WN01xx *)
neuper@41996
  1272
fun update_ctxt   pt pos x = appl_obj (repl_ctxt   x) pt pos; (*for use on PblObj, 
neuper@42394
  1273
  otherwise use fun generate1; compare fun get_ctxt*)
neuper@37906
  1274
fun update_env    pt pos x = appl_obj (repl_env    x) pt pos;
neuper@37906
  1275
fun update_domID  pt pos x = appl_obj (repl_domID  x) pt pos;
neuper@37906
  1276
fun update_pblID  pt pos x = appl_obj (repl_pblID  x) pt pos;
neuper@37906
  1277
fun update_metID  pt pos x = appl_obj (repl_metID  x) pt pos;
neuper@37906
  1278
fun update_spec   pt pos x = appl_obj (repl_spec   x) pt pos;
neuper@37906
  1279
fun update_pbl    pt pos x = appl_obj (repl_pbl    x) pt pos;
neuper@37906
  1280
fun update_pblppc pt pos x = appl_obj (repl_pbl    x) pt pos;
neuper@37906
  1281
fun update_met    pt pos x = appl_obj (repl_met    x) pt pos;
neuper@41990
  1282
fun update_metppc pt pos x = appl_obj (repl_met    x) pt pos;		   
neuper@37906
  1283
fun update_branch pt pos x = appl_obj (repl_branch x) pt pos;
neuper@41990
  1284
fun update_tac    pt pos x = appl_obj (repl_tac    x) pt pos;
neuper@37906
  1285
fun update_oris   pt pos x = appl_obj (repl_oris   x) pt pos;
neuper@42009
  1286
fun update_orispec pt pos x = appl_obj (repl_orispec   x) pt pos;
neuper@37906
  1287
neuper@42009
  1288
(*WN050305 for handling cut_tree in cappend_atomic + for testing*)
neuper@42009
  1289
fun update_loc'   pt pos x = appl_obj (repl_loc    x) pt pos;
neuper@37906
  1290
neuper@37906
  1291
(*13.8.02: options, because istate is no equalitype any more*)
bonzai@41948
  1292
fun get_loc EmptyPtree _ = (e_istate, e_ctxt)
neuper@37906
  1293
  | get_loc pt (p,Res) =
neuper@37906
  1294
    (case get_obj g_loc pt p of
neuper@37925
  1295
	 (SOME i, NONE) => i
bonzai@41948
  1296
       | (NONE  , NONE) => (e_istate, e_ctxt)
neuper@37925
  1297
       | (_     , SOME i) => i)
neuper@37906
  1298
  | get_loc pt (p,_) =
neuper@37906
  1299
    (case get_obj g_loc pt p of
neuper@37925
  1300
	 (NONE  , SOME i) => i (*13.8.02 just copied from ^^^: too liberal ?*)
bonzai@41948
  1301
       | (NONE  , NONE) => (e_istate, e_ctxt)
neuper@37925
  1302
       | (SOME i, _) => i);
bonzai@41953
  1303
fun get_istate pt p = get_loc pt p |> #1;
neuper@41990
  1304
fun get_ctxt pt (pos as (p, p_)) =
neuper@41990
  1305
  if member op = [Frm, Res] p_
neuper@41990
  1306
  then get_loc pt pos |> #2 (*for script interpretation rely on fun get_loc*)
neuper@41990
  1307
  else get_obj g_ctxt pt p (*for specify phase take ctx from PblObj*)
neuper@37906
  1308
e0726734@41957
  1309
fun get_assumptions_ pt p = get_ctxt pt p |> get_assumptions;
neuper@37906
  1310
neuper@37906
  1311
(*---------
neuper@37906
  1312
end
neuper@37906
  1313
neuper@37906
  1314
open Ptree;
neuper@37906
  1315
----------*)
neuper@37906
  1316
neuper@37906
  1317
(*pos of the formula on FE relative to the current pos,
neuper@37906
  1318
  which is the next writepos*)
neuper@37906
  1319
fun pre_pos ([]:pos) = []:pos
neuper@37906
  1320
  | pre_pos pp =
neuper@37906
  1321
  let val (ps,p) = split_last pp
neuper@37906
  1322
  in case p of 1 => ps | n => ps @ [n-1] end;
neuper@37906
  1323
neuper@37906
  1324
(*WN.20.5.03 ... but not used*)
neuper@37906
  1325
fun posless [] (_::_) = true
neuper@37906
  1326
  | posless (_::_) [] = false
neuper@37906
  1327
  | posless (p::ps) (q::qs) = if p = q then posless ps qs else p < q;
neuper@37906
  1328
(* posless [2,3,4] [3,4,5];
neuper@37906
  1329
true
neuper@37906
  1330
>  posless [2,3,4] [1,2,3];
neuper@37906
  1331
false
neuper@37906
  1332
>  posless [2,3] [2,3,4];
neuper@37906
  1333
true
neuper@37906
  1334
>  posless [2,3,4] [2,3];
neuper@37906
  1335
false                    
neuper@37906
  1336
>  posless [6] [6,5,2];
neuper@37906
  1337
true
neuper@37906
  1338
+++ see Isabelle/../library.ML*)
neuper@37906
  1339
neuper@37906
  1340
neuper@37906
  1341
(**.development for extracting an 'interval' from ptree.**)
neuper@37906
  1342
neuper@41990
  1343
(*WN0510 version stopped in favour of get_interval with !!!move_dn, getFormulaeFromTo
neuper@37906
  1344
  actually used (inefficient) version with move_dn: see modspec.sml*)
neuper@37906
  1345
local
neuper@37906
  1346
neuper@37906
  1347
fun hdp [] = 1     | hdp [0] = 1     | hdp x = hd x;(*start with first*)
neuper@37906
  1348
fun hdq	[] = 99999 | hdq [0] = 99999 | hdq x = hd x;(*take until last*)
neuper@37906
  1349
fun tlp [] = [0]     | tlp [_] = [0]     | tlp x = tl x;
neuper@37906
  1350
fun tlq [] = [99999] | tlq [_] = [99999] | tlq x = tl x;
neuper@37906
  1351
neuper@37906
  1352
fun getnd i (b,p) q (Nd (po, nds)) =
neuper@37906
  1353
    (if  i <= 0 then [[b]] else []) @
neuper@37906
  1354
    (getnds (i-1) true (b@[hdp p], tlp p) (tlq q)
neuper@37906
  1355
	   (take_fromto (hdp p) (hdq q) nds))
neuper@37906
  1356
neuper@37906
  1357
and getnds _ _ _ _ [] = []                         (*no children*)
neuper@37906
  1358
  | getnds i _ (b,p) q [nd] = (getnd i (b,p) q nd) (*l+r-margin*)
neuper@37906
  1359
neuper@37906
  1360
  | getnds i true (b,p) q [n1, n2] =               (*l-margin,  r-margin*)
neuper@37906
  1361
    (getnd i      (       b, p ) [99999] n1) @
neuper@37906
  1362
    (getnd ~99999 (lev_on b,[0]) q       n2)
neuper@37906
  1363
neuper@37906
  1364
  | getnds i _    (b,p) q [n1, n2] =               (*intern,  r-margin*)
neuper@37906
  1365
    (getnd i      (       b,[0]) [99999] n1) @
neuper@37906
  1366
    (getnd ~99999 (lev_on b,[0]) q       n2)
neuper@37906
  1367
neuper@37906
  1368
  | getnds i true (b,p) q (nd::(nds as _::_)) =    (*l-margin, intern*)
neuper@37906
  1369
    (getnd i             (       b, p ) [99999] nd) @
neuper@37906
  1370
    (getnds ~99999 false (lev_on b,[0]) q nds)
neuper@37906
  1371
neuper@37906
  1372
  | getnds i _ (b,p) q (nd::(nds as _::_)) =       (*intern, ...*)
neuper@37906
  1373
    (getnd i             (       b,[0]) [99999] nd) @
neuper@37906
  1374
    (getnds ~99999 false (lev_on b,[0]) q nds); 
neuper@37906
  1375
in
neuper@37906
  1376
(*get an 'interval from to' from a ptree as 'intervals f t' of respective nodes
neuper@37906
  1377
  where 'from' are pos, i.e. a key as int list, 'f' an int (to,t analoguous)
neuper@37906
  1378
(1) the 'f' are given 
neuper@37906
  1379
(1a) by 'from' if 'f' = the respective element of 'from' (left margin)
neuper@37906
  1380
(1b) -inifinity, if 'f' > the respective element of 'from' (internal node)
neuper@37906
  1381
(2) the 't' ar given
neuper@37906
  1382
(2a) by 'to' if 't' = the respective element of 'to' (right margin)
neuper@37906
  1383
(2b) inifinity, if 't' < the respective element of 'to (internal node)'
neuper@37906
  1384
the 'f' and 't' are set by hdp,... *)
neuper@37906
  1385
fun get_trace pt p q =
neuper@37906
  1386
    (flat o (getnds ((length p) -1) true ([hdp p], tlp p) (tlq q))) 
neuper@37906
  1387
	(take_fromto (hdp p) (hdq q) (children pt));
neuper@37906
  1388
end;
neuper@37906
  1389
neuper@37906
  1390
fun get_somespec ((dI,pI,mI):spec) ((dI',pI',mI'):spec) =
neuper@37906
  1391
    let val domID = if dI = e_domID
neuper@37906
  1392
		    then if dI' = e_domID 
neuper@38031
  1393
			 then error"pt_extract: no domID in probl,origin"
neuper@37906
  1394
			 else dI'
neuper@37906
  1395
		    else dI
neuper@37906
  1396
	val pblID = if pI = e_pblID
neuper@37906
  1397
		    then if pI' = e_pblID 
neuper@38031
  1398
			 then error"pt_extract: no pblID in probl,origin"
neuper@37906
  1399
			 else pI'
neuper@37906
  1400
		    else pI
neuper@37906
  1401
	val metID = if mI = e_metID
neuper@37906
  1402
		    then if pI' = e_metID 
neuper@38031
  1403
			 then error"pt_extract: no metID in probl,origin"
neuper@37906
  1404
			 else mI'
neuper@37906
  1405
		    else mI
neuper@37906
  1406
    in (domID, pblID, metID):spec end;
neuper@37906
  1407
fun get_somespec' ((dI,pI,mI):spec) ((dI',pI',mI'):spec) =
neuper@37906
  1408
    let val domID = if dI = e_domID then dI' else dI
neuper@37906
  1409
	val pblID = if pI = e_pblID then pI' else pI
neuper@37906
  1410
	val metID = if mI = e_metID then mI' else mI
neuper@37906
  1411
    in (domID, pblID, metID):spec end;
neuper@37906
  1412
neuper@37906
  1413
(*extract a formula or model from ptree for itms2itemppc or model2xml*)
neuper@37906
  1414
fun preconds2str bts = 
neuper@37906
  1415
    (strs2str o (map (linefeed o pair2str o
neuper@37906
  1416
		      (apsnd term2str) o 
neuper@37906
  1417
		      (apfst bool2str)))) bts;
neuper@37906
  1418
fun ocalhd2str ((b, p, hdf, itms, prec, spec):ocalhd) =
neuper@37906
  1419
    "("^bool2str b^", "^pos_2str p^", "^term2str hdf^
neuper@37928
  1420
    ", "^itms2str_ (thy2ctxt' "Isac") itms^
neuper@37906
  1421
    ", "^preconds2str prec^", \n"^spec2str spec^" )";
neuper@37906
  1422
neuper@37906
  1423
neuper@37906
  1424
neuper@37906
  1425
fun is_pblnd (Nd (ppobj, _)) = is_pblobj ppobj;
neuper@37906
  1426
neuper@37906
  1427
neuper@37906
  1428
(**.functions for the 'ptree iterator' as seen from the FE-Kernel interface.**)
neuper@37906
  1429
neuper@37906
  1430
(*move one step down into existing nodes of ptree; regard TransitiveB
neuper@37906
  1431
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##################
neuper@37906
  1432
fun move_dn _ (Nd (c, ns)) ([],p_) = (*root problem*)
neuper@37906
  1433
(* val (Nd (c, ns), ([],p_)) = (pt, get_pos cI uI);
neuper@37906
  1434
   *)
neuper@37906
  1435
    if is_pblobj c 
neuper@37906
  1436
    then case p_ of (*Frm => ([], Pbl) 1.12.03
neuper@37906
  1437
		  |*) Res => raise PTREE "move_dn: end of calculation"
neuper@37906
  1438
		  | _ => if null ns (*go down from Pbl + Met*)
neuper@37906
  1439
			 then raise PTREE "move_dn: solve problem not started"
neuper@37906
  1440
			 else ([1], Frm)
neuper@37906
  1441
    else (case p_ of Res => raise PTREE "move_dn: end of (sub-)tree"
neuper@37906
  1442
		  | _ => if null ns
neuper@37906
  1443
			 then raise PTREE "move_dn: pos not existent 1"
neuper@37906
  1444
			 else ([1], Frm))
neuper@37906
  1445
neuper@37906
  1446
  (*iterate towards end of pos*)
neuper@37906
  1447
(* val (P,(Nd (_, ns)),(p::(ps as (_::_)),p_)) = ([]:pos, pt, get_pos cI uI);
neuper@37906
  1448
   val (P,(Nd (_, ns)),(p::(ps as (_::_)),p_)) = ((P@[p]),(nth p ns),(ps, p_));
neuper@37906
  1449
   *) 
neuper@37906
  1450
 | move_dn P  (Nd (_, ns)) (p::(ps as (_::_)),p_) =
neuper@37906
  1451
    if p > length ns then raise PTREE "move_dn: pos not existent 2"
neuper@37906
  1452
    else move_dn ((P@[p]): pos) (nth p ns) (ps, p_)
neuper@37906
  1453
(* val (P, (Nd (c, ns)), ([p], p_)) = ((P@[p]), (nth p ns), (ps, p_));
neuper@37906
  1454
   val (P, (Nd (c, ns)), ([p], p_)) = ([],pt,get_pos cI uI);
neuper@37906
  1455
   *)
neuper@37906
  1456
  | move_dn P (Nd (c, ns)) ([p], p_) = (*act on last element of pos*)
neuper@37906
  1457
    if p > length ns then raise PTREE "move_dn: pos not existent 3"
neuper@37906
  1458
    else if is_pblnd (nth p ns)  then
neuper@38015
  1459
	((*tracing("### move_dn: is_pblnd (nth p ns), P= "^ints2str' P^", \n"^
neuper@37906
  1460
		 "length ns= "^((string_of_int o length) ns)^
neuper@37906
  1461
		 ", p= "^string_of_int p^", p_= "^pos_2str p_);*)
neuper@37906
  1462
	 case p_ of Res => if p = length ns 
neuper@37906
  1463
			   then if g_ostate c = Complete then (P, Res)
neuper@37906
  1464
				else raise PTREE (ints2str' P^" not complete")
neuper@37906
  1465
			   (*FIXME here handle not-sequent-branches*)
neuper@37906
  1466
			   else if g_branch c = TransitiveB 
neuper@37906
  1467
				   andalso (not o is_pblnd o (nth (p+1))) ns
neuper@37906
  1468
			   then (P@[p+1], Res)
neuper@37906
  1469
			   else (P@[p+1], if is_pblnd (nth (p+1) ns) 
neuper@37906
  1470
					  then Pbl else Frm)
neuper@37906
  1471
		  | _ => if (null o children o (nth p)) ns (*go down from Pbl*)
neuper@37906
  1472
			 then raise PTREE "move_dn: solve subproblem not started"
neuper@37906
  1473
			 else (P @ [p, 1], 
neuper@37906
  1474
			       if (is_pblnd o hd o children o (nth p)) ns
neuper@37906
  1475
			       then Pbl else Frm)
neuper@37906
  1476
			      )
neuper@37906
  1477
    (* val (P, Nd (c, ns), ([p], p_)) = ([], pt, ([1], Frm));
neuper@37906
  1478
        *)
neuper@37906
  1479
    else case p_ of Frm => if (null o children o (nth p)) ns 
neuper@37906
  1480
			 (*then if g_ostate c = Complete then (P@[p],Res)*)
neuper@37906
  1481
			   then if g_ostate' (nth p ns) = Complete 
neuper@37906
  1482
				then (P@[p],Res)
neuper@37906
  1483
				else raise PTREE "move_dn: pos not existent 4"
neuper@37906
  1484
			   else (P @ [p, 1], (*go down*) 
neuper@37906
  1485
				 if (is_pblnd o hd o children o (nth p)) ns
neuper@37906
  1486
				 then Pbl else Frm)
neuper@37906
  1487
		  | Res => if p = length ns 
neuper@37906
  1488
			   then 
neuper@37906
  1489
			      if g_ostate c = Complete then (P, Res)
neuper@37906
  1490
			      else raise PTREE (ints2str' P^" not complete")
neuper@37906
  1491
			   else 
neuper@37906
  1492
			       if g_branch c = TransitiveB 
neuper@37906
  1493
				  andalso (not o is_pblnd o (nth (p+1))) ns
neuper@37906
  1494
			       then if (null o children o (nth (p+1))) ns
neuper@37906
  1495
				    then (P@[p+1], Res)
neuper@37906
  1496
				    else (P@[p+1,1], Frm)(*040221*)
neuper@37906
  1497
			       else (P@[p+1], if is_pblnd (nth (p+1) ns) 
neuper@37906
  1498
					      then Pbl else Frm); 
neuper@37906
  1499
*)
neuper@37906
  1500
(*.move one step down into existing nodes of ptree; skip Res = Frm.nxt;
neuper@37906
  1501
   move_dn at the end of the calc-tree raises PTREE.*)
neuper@37906
  1502
fun move_dn _ (Nd (c, ns)) ([],p_) = (*root problem*)
neuper@37906
  1503
    (case p_ of 
neuper@37906
  1504
	     Res => raise PTREE "move_dn: end of calculation"
neuper@37906
  1505
	   | _ => if null ns (*go down from Pbl + Met*)
neuper@37906
  1506
		  then raise PTREE "move_dn: solve problem not started"
neuper@37906
  1507
		  else ([1], Frm))
neuper@37906
  1508
  | move_dn P  (Nd (_, ns)) (p::(ps as (_::_)),p_) =(*iterate to end of pos*)
neuper@37906
  1509
    if p > length ns then raise PTREE "move_dn: pos not existent 2"
neuper@37906
  1510
    else move_dn ((P@[p]): pos) (nth p ns) (ps, p_)
neuper@37906
  1511
neuper@37906
  1512
  | move_dn P (Nd (c, ns)) ([p], p_) = (*act on last element of pos*)
neuper@37906
  1513
    if p > length ns then raise PTREE "move_dn: pos not existent 3"
neuper@37906
  1514
    else case p_ of 
neuper@37906
  1515
	     Res => 
neuper@37906
  1516
	     if p = length ns (*last Res on this level: go a level up*)
neuper@37906
  1517
	     then if g_ostate c = Complete then (P, Res)
neuper@37906
  1518
		  else raise PTREE (ints2str' P^" not complete 1")
neuper@37906
  1519
	     else (*go to the next Nd on this level, or down into the next Nd*)
neuper@37906
  1520
		 if is_pblnd (nth (p+1) ns) then (P@[p+1], Pbl)
neuper@37906
  1521
		 else 
neuper@37906
  1522
		     if g_res' (nth p ns) = g_form' (nth (p+1) ns)
neuper@37906
  1523
		     then if (null o children o (nth (p+1))) ns
neuper@37906
  1524
			  then (*take the Res if Complete*) 
neuper@37906
  1525
			      if g_ostate' (nth (p+1) ns) = Complete 
neuper@37906
  1526
			      then (P@[p+1], Res)
neuper@37906
  1527
			      else raise PTREE (ints2str' (P@[p+1])^
neuper@37906
  1528
						" not complete 2")
neuper@37906
  1529
			  else (P@[p+1,1], Frm)(*go down into the next PrfObj*)
neuper@37906
  1530
		     else (P@[p+1], Frm)(*take Frm: exists if the Nd exists*)
neuper@37906
  1531
	   | Frm => (*go down or to the Res of this Nd*)
neuper@37906
  1532
	     if (null o children o (nth p)) ns
neuper@37906
  1533
	     then if g_ostate' (nth p ns) = Complete then (P @ [p], Res)
neuper@37906
  1534
		  else raise PTREE (ints2str' (P @ [p])^" not complete 3")
neuper@37906
  1535
	     else (P @ [p, 1], Frm)
neuper@37906
  1536
	   | _ => (*is Pbl or Met*)
neuper@37906
  1537
	     if (null o children o (nth p)) ns
neuper@37906
  1538
	     then raise PTREE "move_dn:solve subproblem not startd"
neuper@37906
  1539
	     else (P @ [p, 1], 
neuper@37906
  1540
		   if (is_pblnd o hd o children o (nth p)) ns
neuper@37906
  1541
		   then Pbl else Frm);
neuper@37906
  1542
neuper@37906
  1543
neuper@37906
  1544
(*.go one level down into ptree.*)
neuper@37906
  1545
fun movelevel_dn [] (Nd (c, ns)) ([],p_) = (*root problem*)
neuper@37906
  1546
    if is_pblobj c 
neuper@37906
  1547
    then if null ns 
neuper@37906
  1548
	 then raise PTREE "solve problem not started"
neuper@37906
  1549
	 else ([1], if (is_pblnd o hd) ns then Pbl else Frm)
neuper@37906
  1550
    else raise PTREE "pos not existent 1"
neuper@37906
  1551
neuper@37906
  1552
  (*iterate towards end of pos*)
neuper@37906
  1553
  | movelevel_dn P (Nd (_, ns)) (p::(ps as (_::_)),p_) =
neuper@37906
  1554
    if p > length ns then raise PTREE "pos not existent 2"
neuper@37906
  1555
    else movelevel_dn (P@[p]) (nth p ns) (ps, p_)
neuper@37906
  1556
neuper@37906
  1557
  | movelevel_dn P (Nd (c, ns)) ([p], p_) = (*act on last element of pos*)
neuper@37906
  1558
    if p > length ns then raise PTREE "pos not existent 3" else
neuper@37906
  1559
    case p_ of Res => 
neuper@37906
  1560
	       if p = length ns 
neuper@37906
  1561
	       then raise PTREE "no children"
neuper@37906
  1562
	       else 
neuper@37906
  1563
		   if g_branch c = TransitiveB
neuper@37906
  1564
		   then if (null o children o (nth (p+1))) ns
neuper@37906
  1565
			then raise PTREE "no children"
neuper@37906
  1566
			else (P @ [p+1, 1], 
neuper@37906
  1567
			      if (is_pblnd o hd o children o (nth (p+1))) ns
neuper@37906
  1568
			      then Pbl else Frm)
neuper@37906
  1569
		   else if (null o children o (nth p)) ns
neuper@37906
  1570
		   then raise PTREE "no children"
neuper@37906
  1571
		   else (P @ [p, 1], if (is_pblnd o hd o children o (nth p)) ns
neuper@37906
  1572
				     then Pbl else Frm)
neuper@37906
  1573
	     | _ => if (null o children o (nth p)) ns 
neuper@37906
  1574
		    then raise PTREE "no children"
neuper@37906
  1575
		    else (P @ [p, 1], (*go down*)
neuper@37906
  1576
			  if (is_pblnd o hd o children o (nth p)) ns
neuper@37906
  1577
			  then Pbl else Frm);
neuper@37906
  1578
neuper@37906
  1579
neuper@37906
  1580
neuper@37906
  1581
(*.go to the previous position in ptree; regard TransitiveB.*)
neuper@37906
  1582
fun move_up _ (Nd (c, ns)) (([],p_):pos') = (*root problem*)
neuper@37906
  1583
    if is_pblobj c 
neuper@37906
  1584
    then case p_ of Res => if null ns then ([], Pbl) (*Res -> Pbl (not Met)!*)
neuper@37906
  1585
			   else ([length ns], Res)
neuper@37906
  1586
		  | _  => raise PTREE "begin of calculation"
neuper@37906
  1587
    else raise PTREE "pos not existent"
neuper@37906
  1588
neuper@37906
  1589
  | move_up P  (Nd (_, ns)) (p::(ps as (_::_)),p_) = (*iterate to end of pos*)
neuper@37906
  1590
    if p > length ns then raise PTREE "pos not existent"
neuper@37906
  1591
    else move_up (P@[p]) (nth p ns) (ps,p_)
neuper@37906
  1592
neuper@37906
  1593
  | move_up P (Nd (c, ns)) ([p], p_) = (*act on last element of pos*)
neuper@37906
  1594
    if p > length ns then raise PTREE "pos not existent"
neuper@37906
  1595
    else if is_pblnd (nth p ns)  then
neuper@37906
  1596
	case p_ of Res => 
neuper@37906
  1597
		   let val nc = (length o children o (nth p)) ns
neuper@37906
  1598
		   in if nc = 0 then (P@[p], Pbl) (*Res -> Pbl (not Met)!*)
neuper@37906
  1599
		      else (P @ [p, nc], Res) end (*go down*)
neuper@37906
  1600
		 | _ => if p = 1 then (P, Pbl) else (P@[p-1], Res) 
neuper@37906
  1601
    else case p_ of Frm => if p <> 1 then (P, Frm) 
neuper@37906
  1602
			  else if is_pblobj c then (P, Pbl) else (P, Frm)
neuper@37906
  1603
		  | Res => 
neuper@37906
  1604
		    let val nc = (length o children o (nth p)) ns
neuper@37906
  1605
		    in if nc = 0 (*cannot go down*)
neuper@37906
  1606
		       then if g_branch c = TransitiveB andalso p <> 1
neuper@37906
  1607
			    then (P@[p-1], Res) else (P@[p], Frm)
neuper@37906
  1608
		       else (P @ [p, nc], Res) end; (*go down*)
neuper@37906
  1609
neuper@37906
  1610
neuper@37906
  1611
neuper@37906
  1612
(*.go one level up in ptree; sets the position on Frm.*)
neuper@37906
  1613
fun movelevel_up _ (Nd (c, ns)) (([],p_):pos') = (*root problem*)
neuper@37906
  1614
    raise PTREE "pos not existent"
neuper@37906
  1615
neuper@37906
  1616
  (*iterate towards end of pos*)
neuper@37906
  1617
  | movelevel_up P  (Nd (_, ns)) (p::(ps as (_::_)),p_) = 
neuper@37906
  1618
    if p > length ns then raise PTREE "pos not existent"
neuper@37906
  1619
    else movelevel_up (P@[p]) (nth p ns) (ps,p_)
neuper@37906
  1620
neuper@37906
  1621
  | movelevel_up P (Nd (c, ns)) ([p], p_) = (*act on last element of pos*)
neuper@37906
  1622
    if p > length ns then raise PTREE "pos not existent"
neuper@37906
  1623
    else if is_pblobj c then (P, Pbl) else (P, Frm);
neuper@37906
  1624
neuper@37906
  1625
neuper@37906
  1626
(*.go to the next calc-head up in the calc-tree.*)
neuper@37906
  1627
fun movecalchd_up pt ((p, Res):pos') =
neuper@37906
  1628
    (par_pblobj pt p, Pbl):pos'
neuper@37906
  1629
  | movecalchd_up pt (p, _) =
neuper@37906
  1630
    if is_pblobj (get_obj I pt p) 
neuper@37906
  1631
    then (p, Pbl) else (par_pblobj pt p, Pbl);
neuper@37906
  1632
neuper@37906
  1633
(*.determine the previous pos' on the same level.*)
neuper@42427
  1634
(*WN0502 made for interSteps; _only_ works for branch TransitiveB WN120517 compare lev_back*)
neuper@37906
  1635
fun lev_pred' pt (pos:pos' as ([],Res)) = ([],Pbl):pos'
neuper@37906
  1636
  | lev_pred' pt (pos:pos' as (p, Res)) =
neuper@37906
  1637
    let val (p', last) = split_last p
neuper@37906
  1638
    in if last = 1 
neuper@37906
  1639
       then if (is_pblobj o (get_obj I pt)) p then (p,Pbl) else (p, Frm)
neuper@37906
  1640
       else if get_obj g_res pt (p' @ [last - 1]) = get_obj g_form pt p
neuper@37906
  1641
       then (p' @ [last - 1], Res) (*TransitiveB*)
neuper@37906
  1642
       else if (is_pblobj o (get_obj I pt)) p then (p,Pbl) else (p, Frm)
neuper@37906
  1643
    end;
neuper@37906
  1644
neuper@42427
  1645
neuper@37906
  1646
(*.determine the next pos' on the same level.*)
neuper@37906
  1647
fun lev_on' pt (([],Pbl):pos') = ([],Res):pos'
neuper@37906
  1648
  | lev_on' pt (p, Res) =
neuper@37906
  1649
    if get_obj g_res pt p = get_obj g_form pt (lev_on p)(*TransitiveB*)
neuper@37906
  1650
    then if existpt' (lev_on p, Res) pt then (lev_on p, Res)
neuper@38031
  1651
	 else error ("lev_on': (p, Res) -> (p, Res) not existent, \
neuper@37906
  1652
		      \p = "^ints2str' (lev_on p))
neuper@37906
  1653
    else (lev_on p, Frm)
neuper@37906
  1654
  | lev_on' pt (p, _) =
neuper@37906
  1655
    if existpt' (p, Res) pt then (p, Res)
neuper@38031
  1656
    else error ("lev_on': (p, Frm) -> (p, Res) not existent, \
neuper@37906
  1657
		      \p = "^ints2str' p);
neuper@37906
  1658
neuper@37906
  1659
fun exist_lev_on' pt p = (lev_on' pt p; true) handle _ => false;
neuper@37906
  1660
neuper@37906
  1661
(*.is the pos' at the last element of a calulation _AND_ can be continued.*)
neuper@37906
  1662
(* val (pt, pos as (p,p_)) = (pt, ([1],Frm));
neuper@37906
  1663
   *)
neuper@37906
  1664
fun is_curr_endof_calc pt (([],Res) : pos') = false
neuper@37906
  1665
  | is_curr_endof_calc pt (pos as (p,_)) =
neuper@37906
  1666
    not (exist_lev_on' pt pos) 
neuper@37906
  1667
    andalso get_obj g_ostate pt (lev_up p) = Incomplete;
neuper@37906
  1668
neuper@37906
  1669
neuper@37906
  1670
(**.insert into ctree and cut branches accordingly.**)
neuper@37906
  1671
  
neuper@37906
  1672
(*.get all positions of certain intervals on the ctree.*)
neuper@37906
  1673
(*OLD VERSION without move_dn; kept for occasional redesign
neuper@37906
  1674
   get all pos's to be cut in a ptree
neuper@37906
  1675
   below a pos or from a ptree list after i-th element (NO level_up).*)
neuper@37906
  1676
fun get_allpos' (_:pos, _:posel) EmptyPtree   = ([]:pos' list)
neuper@37906
  1677
  | get_allpos' (p, 1) (Nd (b, bs)) = (*p is pos of Nd*)
neuper@37906
  1678
    if g_ostate b = Incomplete 
neuper@38015
  1679
    then ((*tracing("get_allpos' (p, 1) Incomplete: p="^ints2str' p);*)
neuper@37906
  1680
	  [(p,Frm)] @ (get_allpos's (p, 1) bs)
neuper@37906
  1681
	  )
neuper@38015
  1682
    else ((*tracing("get_allpos' (p, 1) else: p="^ints2str' p);*)
neuper@37906
  1683
	  [(p,Frm)] @ (get_allpos's (p, 1) bs) @ [(p,Res)]
neuper@37906
  1684
	  )
neuper@37906
  1685
    (*WN041020 here we assume what is presented on the worksheet ?!*)
neuper@37906
  1686
  | get_allpos' (p, i) (Nd (b, bs)) = (*p is pos of Nd*)
neuper@37906
  1687
    if length bs > 0 orelse is_pblobj b
neuper@37906
  1688
    then if g_ostate b = Incomplete 
neuper@37906
  1689
	 then [(p,Frm)] @ (get_allpos's (p, 1) bs)
neuper@37906
  1690
	 else [(p,Frm)] @ (get_allpos's (p, 1) bs) @ [(p,Res)]
neuper@37906
  1691
    else 
neuper@37906
  1692
	if g_ostate b = Incomplete 
neuper@37906
  1693
	then []
neuper@37906
  1694
	else [(p,Res)]
neuper@37906
  1695
(*WN041020 here we assume what is presented on the worksheet ?!*)
neuper@37906
  1696
and get_allpos's _ [] = []
neuper@37906
  1697
  | get_allpos's (p, i) (pt::pts) = (*p is pos of parent-Nd*)
neuper@37906
  1698
    (get_allpos' (p@[i], i) pt) @ (get_allpos's (p, i+1) pts);
neuper@37906
  1699
neuper@37906
  1700
(*.get all positions of certain intervals on the ctree.*)
neuper@37906
  1701
(*NEW version WN050225*)
neuper@37906
  1702
neuper@37906
  1703
neuper@37906
  1704
(*.cut branches.*)
neuper@37906
  1705
(*before WN041019......
neuper@37906
  1706
val cut_branch = (test_trans, curry take):
neuper@37906
  1707
    (ppobj -> bool) * (int -> ptree list -> ptree list);
neuper@37906
  1708
.. formlery used for ...
neuper@37906
  1709
fun cut_tree''' _ [] = EmptyPtree
neuper@37906
  1710
  | cut_tree''' pt pos = 
neuper@37906
  1711
  let val (pt',cut) = appl_branch cut_branch pt pos
neuper@37906
  1712
  in if cut andalso length pos > 1 then cut_tree''' pt' (lev_up pos)
neuper@37906
  1713
     else pt' end;
neuper@37906
  1714
*)
neuper@37906
  1715
(*OLD version before WN050225*)
neuper@37906
  1716
(*WN050106 like cut_level, but deletes exactly 1 node --- for tests ONLY*)
neuper@37906
  1717
fun cut_level_'_ (_:pos' list) (_:pos) EmptyPtree (_:pos') =
neuper@37906
  1718
    raise PTREE "cut_level_'_ Empty _"
neuper@37906
  1719
  | cut_level_'_ _ _ (Nd ( _, _)) ([],_) = raise PTREE "cut_level_'_ _ []"
neuper@37906
  1720
  | cut_level_'_ cuts P (Nd (b, bs)) (p::[],p_) = 
neuper@37906
  1721
    if test_trans b 
neuper@37906
  1722
    then (Nd (b, drop_nth [] (p:posel, bs)),
neuper@37906
  1723
	  (*     ~~~~~~~~~~~*)
neuper@37906
  1724
	  cuts @ 
neuper@37906
  1725
	  (if p_ = Frm then [(P@[p],Res)] else ([]:pos' list)) @
neuper@37906
  1726
	  (*WN041020 here we assume what is presented on the worksheet ?!*)
neuper@37906
  1727
	  (get_allpos's (P, p+1) (drop_nth [] (p, bs))))
neuper@37906
  1728
    (*                            ~~~~~~~~~~~*)
neuper@37906
  1729
    else (Nd (b, bs), cuts)
neuper@37906
  1730
  | cut_level_'_ cuts P (Nd (b, bs)) ((p::ps),p_) =
neuper@37906
  1731
    let val (bs',cuts') = cut_level_'_ cuts P (nth p bs) (ps, p_)
neuper@37906
  1732
    in (Nd (b, repl_app bs p bs'), cuts @ cuts') end;
neuper@37906
  1733
neuper@37906
  1734
(*before WN050219*)
neuper@37906
  1735
fun cut_level (_:pos' list) (_:pos) EmptyPtree (_:pos') =
neuper@37906
  1736
    raise PTREE "cut_level EmptyPtree _"
neuper@37906
  1737
  | cut_level _ _ (Nd ( _, _)) ([],_) = raise PTREE "cut_level _ []"
neuper@37906
  1738
neuper@37906
  1739
  | cut_level cuts P (Nd (b, bs)) (p::[],p_) = 
neuper@37906
  1740
    if test_trans b 
neuper@37906
  1741
    then (Nd (b, take (p:posel, bs)),
neuper@37906
  1742
	  cuts @ 
neuper@37906
  1743
	  (if p_ = Frm andalso (*#*) g_ostate b = Complete
neuper@37906
  1744
	   then [(P@[p],Res)] else ([]:pos' list)) @
neuper@37906
  1745
	  (*WN041020 here we assume what is presented on the worksheet ?!*)
neuper@37906
  1746
	  (get_allpos's (P, p+1) (takerest (p, bs))))
neuper@37906
  1747
    else (Nd (b, bs), cuts)
neuper@37906
  1748
neuper@37906
  1749
  | cut_level cuts P (Nd (b, bs)) ((p::ps),p_) =
neuper@37906
  1750
    let val (bs',cuts') = cut_level cuts P (nth p bs) (ps, p_)
neuper@37906
  1751
    in (Nd (b, repl_app bs p bs'), cuts @ cuts') end;
neuper@37906
  1752
neuper@37906
  1753
(*OLD version before WN050219, overwritten below*)
neuper@37906
  1754
fun cut_tree _ (([],_):pos') = raise PTREE "cut_tree _ ([],_)"
neuper@37906
  1755
  | cut_tree pt (pos as ([p],_)) =
neuper@37906
  1756
    let	val (pt', cuts) = cut_level ([]:pos' list) [] pt pos
neuper@37906
  1757
    in (pt', cuts @ (if get_obj g_ostate pt [] = Incomplete 
neuper@37906
  1758
		     then [] else [([],Res)])) end
neuper@37906
  1759
  | cut_tree pt (p,p_) =
neuper@37906
  1760
    let	
neuper@37906
  1761
	fun cutfn pt cuts (p,p_) = 
neuper@37906
  1762
	    let val (pt', cuts') = cut_level [] (lev_up p) pt (p,p_)
neuper@37906
  1763
		val cuts'' = if get_obj g_ostate pt (lev_up p) = Incomplete 
neuper@37906
  1764
			     then [] else [(lev_up p, Res)]
neuper@37906
  1765
	    in if length cuts' > 0 andalso length p > 1
neuper@37906
  1766
	       then cutfn pt' (cuts @ cuts') (lev_up p, Frm(*-->(p,Res)*))
neuper@37906
  1767
	       else (pt',cuts @ cuts') end
neuper@37906
  1768
	val (pt', cuts) = cutfn pt [] (p,p_)
neuper@37906
  1769
    in (pt', cuts @ (if get_obj g_ostate pt [] = Incomplete 
neuper@37906
  1770
		     then [] else [([], Res)])) end;
neuper@37906
  1771
neuper@37906
  1772
neuper@37906
  1773
(*########/ inserted from ctreeNEW.sml \#################################**)
neuper@37906
  1774
neuper@37906
  1775
(*.get all positions in a ptree until ([],Res) or ostate=Incomplete
neuper@37906
  1776
val get_allp = fn : 
neuper@37906
  1777
  pos' list -> : accumulated, start with []
neuper@37906
  1778
  pos ->       : the offset for subtrees wrt the root
neuper@37906
  1779
  ptree ->     : (sub)tree
neuper@37906
  1780
  pos'         : initialization (the last pos' before ...)
neuper@37906
  1781
  -> pos' list : of positions in this (sub) tree (relative to the root)
neuper@37906
  1782
.*)
neuper@37906
  1783
(* val (cuts, P, pt, pos) = ([], [3], get_nd pt [3], ([], Frm):pos');
neuper@37906
  1784
   val (cuts, P, pt, pos) = ([], [2], get_nd pt [2], ([], Frm):pos');
neuper@37906
  1785
   length (children pt);
neuper@37906
  1786
   *)
neuper@37906
  1787
fun get_allp (cuts:pos' list) (P:pos, pos:pos') pt =
neuper@37906
  1788
    (let val nxt = move_dn [] pt pos (*exn if Incomplete reached*)
neuper@37906
  1789
     in if nxt <> ([],Res) 
neuper@37906
  1790
	then get_allp (cuts @ [nxt]) (P, nxt) pt
neuper@37906
  1791
	else (map (apfst (curry op@ P)) (cuts @ [nxt])): pos' list
neuper@37906
  1792
     end) handle PTREE _ => (map (apfst (curry op@ P)) cuts);
neuper@37906
  1793
neuper@37906
  1794
neuper@37906
  1795
(*the pts are assumed to be on the same level*)
neuper@37906
  1796
fun get_allps (cuts: pos' list) (P:pos) [] = cuts
neuper@37906
  1797
  | get_allps cuts P (pt::pts) =
neuper@37906
  1798
    let val below = get_allp [] (P, ([], Frm)) pt
neuper@37906
  1799
	val levfrm = 
neuper@37906
  1800
	    if is_pblnd pt 
neuper@37906
  1801
	    then (P, Pbl)::below
neuper@37906
  1802
	    else if last_elem P = 1 
neuper@37906
  1803
	    then (P, Frm)::below
neuper@37906
  1804
	    else (*Trans*) below
neuper@37906
  1805
	val levres = levfrm @ (if null below then [(P, Res)] else [])
neuper@37906
  1806
    in get_allps (cuts @ levres) (lev_on P) pts end;
neuper@37906
  1807
neuper@37906
  1808
neuper@37906
  1809
(**.these 2 funs decide on how far cut_tree goes.**)
neuper@37906
  1810
(*.shall the nodes _after_ the pos to be inserted at be deleted?.*)
neuper@37906
  1811
fun test_trans (PrfObj{branch = Transitive,...}) = true
neuper@37906
  1812
  | test_trans (PrfObj{branch = NoBranch,...}) = true
neuper@37906
  1813
  | test_trans (PblObj{branch = Transitive,...}) = true 
neuper@37906
  1814
  | test_trans (PblObj{branch = NoBranch,...}) = true 
neuper@37906
  1815
  | test_trans _ = false;
neuper@37906
  1816
(*.shall cutting be continued on the higher level(s)?
neuper@37906
  1817
   the Nd regarded will NOT be changed.*)
neuper@37906
  1818
fun cutlevup (PblObj _) = false (*for tests of LK0502*)
neuper@37906
  1819
  | cutlevup _ = true;
neuper@37906
  1820
val cutlevup = test_trans;(*WN060727 after summerterm tests.LK0502 withdrawn*)
neuper@37906
  1821
    
neuper@37906
  1822
(*cut_bottom new sml603..608
neuper@37906
  1823
cut the level at the bottom of the pos (used by cappend_...)
neuper@37906
  1824
and handle the parent in order to avoid extra case for root
neuper@37906
  1825
fn: ptree ->         : the _whole_ ptree for cut_levup
neuper@37906
  1826
    pos * posel ->   : the pos after split_last
neuper@37906
  1827
    ptree ->         : the parent of the Nd to be cut
neuper@37906
  1828
return
neuper@37906
  1829
    (ptree *         : the updated ptree
neuper@37906
  1830
     pos' list) *    : the pos's cut
neuper@37906
  1831
     bool            : cutting shall be continued on the higher level(s)
neuper@37906
  1832
*)
neuper@37906
  1833
fun cut_bottom _ (pt' as Nd (b, [])) = ((pt', []), cutlevup b)
neuper@37906
  1834
  | cut_bottom (P:pos, p:posel) (Nd (b, bs)) =
neuper@37906
  1835
    let (*divide level into 3 parts...*)
neuper@37906
  1836
	val keep = take (p - 1, bs)
neuper@37906
  1837
	val pt' as Nd (_,bs') = nth p bs
neuper@52111
  1838
	(*^^^^^_here_ will be 'insert_pt'ed by 'append_..'*)
neuper@37906
  1839
	val (tail, tp) = (takerest (p, bs), 
neuper@37906
  1840
			  if null (takerest (p, bs)) then 0 else p + 1)
neuper@37906
  1841
	val (children, cuts) = 
neuper@37906
  1842
	    if test_trans b
neuper@37906
  1843
	    then (keep,
neuper@37906
  1844
		  (if is_pblnd pt' then [(P @ [p], Pbl)] else [])
neuper@37906
  1845
		  @ (get_allp  [] (P @ [p], (P, Frm)) pt')
neuper@37906
  1846
		  @ (get_allps [] (P @ [p+1]) tail))
neuper@52111
  1847
	    else (keep @ [(*'insert_pt'ed by 'append_..'*)] @ tail,
neuper@37906
  1848
		  get_allp  [] (P @ [p], (P, Frm)) pt')
neuper@37906
  1849
	val (pt'', cuts) = 
neuper@37906
  1850
	    if cutlevup b
neuper@37906
  1851
	    then (Nd (del_res b, children), 
neuper@37906
  1852
		  cuts @ (if g_ostate b = Incomplete then [] else [(P,Res)]))
neuper@37906
  1853
	    else (Nd (b, children), cuts)
neuper@38015
  1854
	(*val _= tracing("####cut_bottom (P, p)="^pos2str (P @ [p])^
neuper@37906
  1855
		       ", Nd=.............................................")
neuper@37906
  1856
	val _= show_pt pt''
neuper@38015
  1857
	val _= tracing("####cut_bottom form='"^
neuper@37906
  1858
		       term2str (get_obj g_form pt'' []))
neuper@38015
  1859
	val _= tracing("####cut_bottom cuts#="^string_of_int (length cuts)^
neuper@37906
  1860
		       ", cuts="^pos's2str cuts)*)
neuper@37906
  1861
    in ((pt'', cuts:pos' list), cutlevup b) end;
neuper@37906
  1862
neuper@37906
  1863
neuper@37906
  1864
(*.go all levels from the bottom of 'pos' up to the root, 
neuper@37906
  1865
 on each level compose the children of a node and accumulate the cut Nds
neuper@37906
  1866
args
neuper@37906
  1867
   pos' list ->      : for accumulation
neuper@37906
  1868
   bool -> 	     : cutting shall be continued on the higher level(s)
neuper@37906
  1869
   ptree -> 	     : the whole ptree for 'get_nd pt P' on each level
neuper@37906
  1870
   ptree -> 	     : the Nd from the lower level for insertion at path
neuper@37906
  1871
   pos * posel ->    : pos=path split for convenience
neuper@37906
  1872
   ptree -> 	     : Nd the children of are under consideration on this call 
neuper@37906
  1873
returns		     :
neuper@37906
  1874
   ptree * pos' list : the updated parent-Nd and the pos's of the Nds cut
neuper@37906
  1875
.*)
neuper@37906
  1876
fun cut_levup (cuts:pos' list) clevup pt pt' (P:pos, p:posel) (Nd (b, bs)) =
neuper@37906
  1877
    let (*divide level into 3 parts...*)
neuper@37906
  1878
	val keep = take (p - 1, bs)
neuper@37906
  1879
	(*val pt' comes as argument from below*)
neuper@37906
  1880
	val (tail, tp) = (takerest (p, bs), 
neuper@37906
  1881
			  if null (takerest (p, bs)) then 0 else p + 1)
neuper@37906
  1882
	val (children, cuts') = 
neuper@37906
  1883
	    if clevup
neuper@37906
  1884
	    then (keep @ [pt'], get_allps [] (P @ [p+1]) tail)
neuper@37906
  1885
	    else (keep @ [pt'] @ tail, [])
neuper@37906
  1886
	val clevup' = if clevup then cutlevup b else false 
neuper@37906
  1887
	(*the first Nd with false stops cutting on all levels above*)
neuper@37906
  1888
	val (pt'', cuts') = 
neuper@37906
  1889
	    if clevup'
neuper@37906
  1890
	    then (Nd (del_res b, children), 
neuper@37906
  1891
		  cuts' @ (if g_ostate b = Incomplete then [] else [(P,Res)]))
neuper@37906
  1892
	    else (Nd (b, children), cuts')
neuper@38015
  1893
	(*val _= tracing("#####cut_levup clevup= "^bool2str clevup)
neuper@38015
  1894
	val _= tracing("#####cut_levup cutlevup b= "^bool2str (cutlevup b))
neuper@38015
  1895
	val _= tracing("#####cut_levup (P, p)="^pos2str (P @ [p])^
neuper@37906
  1896
		       ", Nd=.............................................")
neuper@37906
  1897
	val _= show_pt pt''
neuper@38015
  1898
	val _= tracing("#####cut_levup form='"^
neuper@37906
  1899
		       term2str (get_obj g_form pt'' []))
neuper@38015
  1900
	val _= tracing("#####cut_levup cuts#="^string_of_int (length cuts)^
neuper@37906
  1901
		       ", cuts="^pos's2str cuts)*)
neuper@37906
  1902
    in if null P then (pt'', (cuts @ cuts'):pos' list)
neuper@37906
  1903
       else let val (P, p) = split_last P
neuper@37906
  1904
	    in cut_levup (cuts @ cuts') clevup' pt pt'' (P, p) (get_nd pt P)
neuper@37906
  1905
	    end
neuper@37906
  1906
    end;
neuper@37906
  1907
 
neuper@37906
  1908
(*.cut nodes after and below an inserted node in the ctree;
neuper@37906
  1909
   the cuts range is limited by the predicate 'fun cutlevup'.*)
neuper@37906
  1910
fun cut_tree pt (pos,_) =
neuper@37906
  1911
    if not (existpt pos pt) 
neuper@37906
  1912
    then (pt,[]) (*appending a formula never cuts anything*)
neuper@37906
  1913
    else let val (P, p) = split_last pos
neuper@37906
  1914
	     val ((pt', cuts), clevup) = cut_bottom (P, p) (get_nd pt P)
neuper@37906
  1915
	 (*        pt' is the updated parent of the Nd to cappend_..*)
neuper@37906
  1916
	 in if null P then (pt', cuts)
neuper@37906
  1917
	    else let val (P, p) = split_last P
neuper@37906
  1918
		 in cut_levup cuts clevup pt pt' (P, p) (get_nd pt P)
neuper@37906
  1919
		 end
neuper@37906
  1920
	 end;
neuper@37906
  1921
neuper@37906
  1922
fun append_atomic p l f r f' s pt = 
neuper@38015
  1923
  let (**val _= tracing("#@append_atomic: pos ="^pos2str p)**)
neuper@37906
  1924
	val (iss, f) = if existpt p pt andalso get_obj g_tac pt p=Empty_Tac
neuper@37906
  1925
		     then (*after Take*)
neuper@37925
  1926
			 ((fst (get_obj g_loc pt p), SOME l), 
neuper@37906
  1927
			  get_obj g_form pt p) 
neuper@37925
  1928
		     else ((NONE, SOME l), f)
neuper@52111
  1929
  in insert_pt (PrfObj {cell = NONE,
neuper@37906
  1930
		     form  = f,
neuper@37906
  1931
		     tac  = r,
neuper@37906
  1932
		     loc   = iss,
neuper@37906
  1933
		     branch= NoBranch,
neuper@37906
  1934
		     result= f',
neuper@37906
  1935
		     ostate= s}) pt p end;
neuper@37906
  1936
neuper@37906
  1937
neuper@37906
  1938
(*20.8.02: cappend_* FIXXXXME cut branches below cannot be decided here:
neuper@37906
  1939
  detail - generate - cappend: inserted, not appended !!!
neuper@37906
  1940
neuper@37906
  1941
  cut decided in applicable_in !?!
neuper@37906
  1942
*)
neuper@37906
  1943
fun cappend_atomic pt p loc f r f' s = 
neuper@37906
  1944
(* val (pt, p, loc, f, r, f', s) = 
neuper@37906
  1945
       (pt,p,l,f,Rewrite_Set_Inst (subst2subs subs',id_rls rls'),
neuper@37906
  1946
	(f',asm),Complete);
neuper@37906
  1947
   *)
neuper@38015
  1948
((*tracing("##@cappend_atomic: pos ="^pos2str p);*)
neuper@37906
  1949
  apfst (append_atomic p loc f r f' s) (cut_tree pt (p,Frm))
neuper@37906
  1950
);
neuper@37906
  1951
(*TODO.WN050305 redesign the handling of istates*)
neuper@37906
  1952
fun cappend_atomic pt p ist_res f r f' s = 
neuper@41994
  1953
      if existpt p pt andalso get_obj g_tac pt p=Empty_Tac
neuper@41994
  1954
      then (*after Take: transfer Frm and respective istate*)
neuper@41994
  1955
	      let
neuper@41994
  1956
          val (ist_form, f) =
neuper@41994
  1957
            (get_loc pt (p,Frm), get_obj g_form pt p)
neuper@41994
  1958
	        val (pt, cs) = cut_tree pt (p,Frm)
neuper@41994
  1959
	        val pt = append_atomic p (e_istate, e_ctxt) f r f' s pt
neuper@41994
  1960
	        val pt = update_loc' pt p (SOME ist_form, SOME ist_res)
neuper@41994
  1961
	      in (pt, cs) end
neuper@41994
  1962
      else apfst (append_atomic p ist_res f r f' s) (cut_tree pt (p,Frm));
neuper@37906
  1963
neuper@37906
  1964
(* called by Take *)
neuper@37906
  1965
fun append_form p l f pt = 
neuper@38015
  1966
((*tracing("##@append_form: pos ="^pos2str p);*)
neuper@52111
  1967
  insert_pt (PrfObj {cell = NONE,
neuper@37906
  1968
		  form  = (*if existpt p pt 
neuper@37906
  1969
		  andalso get_obj g_tac pt p = Empty_Tac 
neuper@37906
  1970
			    (*distinction from 'old' (+complete!) pobjs*)
neuper@37906
  1971
			    then get_obj g_form pt p else*) f,
neuper@37906
  1972
		  tac  = Empty_Tac,
neuper@37925
  1973
		  loc   = (SOME l, NONE),
neuper@37906
  1974
		  branch= NoBranch,
neuper@37906
  1975
		  result= (e_term,[]),
neuper@37906
  1976
		  ostate= Incomplete}) pt p
neuper@37906
  1977
);
neuper@37906
  1978
(* val (p,loc,f) = ([1], e_istate, str2term "x + 1 = 2");
neuper@37906
  1979
   val (p,loc,f) = (fst p, e_istate, str2term "-1 + x = 0");
neuper@37906
  1980
   *)
neuper@37906
  1981
fun cappend_form pt p loc f =
neuper@38015
  1982
((*tracing("##@cappend_form: pos ="^pos2str p);*)
neuper@37906
  1983
  apfst (append_form p loc f) (cut_tree pt (p,Frm))
neuper@37906
  1984
);
neuper@37906
  1985
fun cappend_form pt p loc f =
neuper@38015
  1986
let (*val _= tracing("##@cappend_form: pos ="^pos2str p)
neuper@38015
  1987
    val _= tracing("##@cappend_form before cut_tree: loc ="^istate2str loc)*)
neuper@37906
  1988
    val (pt', cs) = cut_tree pt (p,Frm)
neuper@37906
  1989
    val pt'' = append_form p loc f pt'
neuper@38015
  1990
    (*val _= tracing("##@cappend_form after append: loc ="^
neuper@37906
  1991
		   istates2str (get_obj g_loc pt'' p))*)
neuper@37906
  1992
in (pt'', cs) end;
neuper@37906
  1993
neuper@37906
  1994
neuper@37906
  1995
    
neuper@37906
  1996
fun append_result pt p l f s =
neuper@38015
  1997
((*tracing("##@append_result: pos ="^pos2str p);*)
neuper@37906
  1998
    (appl_obj (repl_result (fst (get_obj g_loc pt p),
neuper@37925
  1999
			    SOME l) f s) pt p, [])
neuper@37906
  2000
);
neuper@37906
  2001
neuper@37906
  2002
neuper@37906
  2003
(*WN041022 deprecated, still for kbtest/diffapp.sml, /systest/root-equ.sml*)
neuper@37906
  2004
fun append_parent p l f r b pt = 
neuper@38015
  2005
  let (*val _= tracing("###append_parent: pos ="^pos2str p);*)
neuper@37906
  2006
    val (ll,f) = if existpt p pt andalso get_obj g_tac pt p=Empty_Tac
neuper@37925
  2007
		  then ((fst (get_obj g_loc pt p), SOME l), 
neuper@37906
  2008
			get_obj g_form pt p) 
neuper@37925
  2009
		 else ((SOME l, NONE), f)
neuper@52111
  2010
  in insert_pt (PrfObj 
neuper@37925
  2011
	  {cell = NONE,
neuper@37906
  2012
	   form  = f,
neuper@37906
  2013
	   tac  = r,
neuper@37906
  2014
	   loc   = ll,
neuper@37906
  2015
	   branch= b,
neuper@37906
  2016
	   result= (e_term,[]),
neuper@37906
  2017
	   ostate= Incomplete}) pt p end;
neuper@37906
  2018
fun cappend_parent pt p loc f r b =
neuper@38015
  2019
((*tracing("###cappend_parent: pos ="^pos2str p);*)
neuper@37906
  2020
  apfst (append_parent p loc f r b) (cut_tree pt (p,Und))
neuper@37906
  2021
);
neuper@37906
  2022
neuper@37906
  2023
neuper@37906
  2024
fun append_problem [] l fmz (strs,spec,hdf) _ =
neuper@38015
  2025
((*tracing("###append_problem: pos = []");*)
neuper@37906
  2026
  (Nd (PblObj 
neuper@37925
  2027
	       {cell  = NONE,
neuper@37906
  2028
		origin= (strs,spec,hdf),
neuper@37906
  2029
		fmz   = fmz,
neuper@37906
  2030
		spec  = empty_spec,
neuper@37906
  2031
		probl = []:itm list,
neuper@37906
  2032
		meth  = []:itm list,
neuper@41988
  2033
    ctxt  = e_ctxt,
neuper@37925
  2034
		env   = NONE,
neuper@37925
  2035
		loc   = (SOME l, NONE),
neuper@37906
  2036
		branch= TransitiveB,(*FIXXXXXME.27.8.03: for equations only*)
neuper@37906
  2037
		result= (e_term,[]),
neuper@37906
  2038
		ostate= Incomplete},[]))
neuper@37906
  2039
)
neuper@37906
  2040
  | append_problem p l fmz (strs,spec,hdf) pt =
neuper@38015
  2041
((*tracing("###append_problem: pos ="^pos2str p);*)
neuper@52111
  2042
  insert_pt (PblObj 
neuper@37925
  2043
	  {cell  = NONE,
neuper@37906
  2044
	   origin= (strs,spec,hdf),
neuper@37906
  2045
	   fmz   = fmz,
neuper@37906
  2046
	   spec  = empty_spec,
neuper@37906
  2047
	   probl = []:itm list,
neuper@37906
  2048
	   meth  = []:itm list,
neuper@41988
  2049
     ctxt  = e_ctxt,
neuper@37925
  2050
	   env   = NONE,
neuper@37925
  2051
	   loc   = (SOME l, NONE),
neuper@37906
  2052
	   branch= TransitiveB,
neuper@37906
  2053
	   result= (e_term,[]),
neuper@37906
  2054
	   ostate= Incomplete}) pt p
neuper@37906
  2055
);
neuper@37906
  2056
fun cappend_problem _ [] loc fmz ori =
neuper@38015
  2057
((*tracing("###cappend_problem: pos = []");*)
neuper@37906
  2058
  (append_problem [] loc fmz ori EmptyPtree,[])
neuper@37906
  2059
)
neuper@37906
  2060
  | cappend_problem pt p loc fmz ori = 
neuper@38015
  2061
((*tracing("###cappend_problem: pos ="^pos2str p);*)
bonzai@41948
  2062
  apfst (append_problem p (loc:(istate * Proof.context)) fmz ori) (cut_tree pt (p,Frm))
neuper@37906
  2063
);
neuper@37906
  2064
neuper@37906
  2065
(*.get the theory explicitly specified for the rootpbl;
neuper@37906
  2066
   thus use this function _after_ finishing specification.*)
neuper@41988
  2067
fun rootthy (Nd (PblObj {spec=(thyID, _, _), ...}, _)) = assoc_thy thyID
neuper@38031
  2068
  | rootthy _ = error "rootthy";
neuper@37906
  2069