src/sml/ME/ctree.sml
author wneuper
Mon, 17 Nov 2003 20:03:49 +0100
changeset 1176 c9d48adf50fe
parent 1171 92873ef65cf2
child 1250 6a0e0af15956
permissions -rw-r--r--
*** empty log message ***
     1 (* use"ME/ctree.sml";
     2    use"ctree.sml";
     3    W.N.26.10.99
     4 
     5 writeln (pr_ptree pr_short pt);
     6 *)
     7 
     8 
     9 signature PTREE =
    10 sig
    11   type ptree
    12   type envp
    13   val e_ptree : ptree
    14   exception PTREE of string
    15   type branch
    16   type ostate
    17   type cellID
    18   type cid
    19   type posel
    20   type pos
    21   type pos'
    22   type loc
    23   type domID
    24   type pblID
    25   type metID
    26   type spec
    27   type 'a ppc
    28   type con
    29   type subs
    30   type subst
    31   type env
    32   type ets
    33   val ets2str : ets -> string
    34   type item
    35   type tac
    36   type tac_
    37   val tac_2str : tac_ -> string
    38   type safe
    39   val safe2str : safe -> string
    40 
    41   type meth
    42   val cappend_atomic : ptree -> pos -> loc -> cterm' -> tac
    43     -> cterm' -> ostate -> cid -> ptree * posel list * cid
    44   val cappend_form : ptree
    45     -> pos -> loc -> cterm' -> cid -> ptree * pos * cid
    46   val cappend_parent : ptree -> pos -> loc -> cterm' -> tac
    47     -> branch -> cid -> ptree * int list * cid
    48   val cappend_problem : ptree -> posel list(*FIXME*) -> loc
    49     -> cterm' list * spec -> cid -> ptree * int list * cellID list
    50   val append_result : ptree -> pos -> cterm' -> ostate -> ptree * pos
    51 
    52   type ppobj
    53   val g_branch : ppobj -> branch
    54   val g_cell : ppobj -> cid
    55   val g_args : ppobj -> (int * (term list)) list (*args of scr*)
    56   val g_form : ppobj -> cterm'
    57   val g_loc : ppobj -> loc
    58   val g_met : ppobj -> meth
    59   val g_domID : ppobj -> domID
    60   val g_metID : ppobj -> metID
    61   val g_model : ppobj -> cterm' ppc
    62   val g_tac : ppobj -> tac
    63   val g_origin : ppobj -> cterm' list * spec
    64   val g_ostate : ppobj -> ostate
    65   val g_pbl : ppobj -> pblID * item ppc
    66   val g_result : ppobj -> cterm'
    67   val g_spec : ppobj -> spec
    68 (*  val get_all : (ppobj -> 'a) -> ptree -> 'a list
    69   val get_alls : (ppobj -> 'a) -> ptree list -> 'a list *)
    70   val get_obj : (ppobj -> 'a) -> ptree -> pos -> 'a     
    71   val gpt_cell : ptree -> cid
    72   val par_pblobj : ptree -> pos -> pos
    73   val pre_pos : pos -> pos
    74   val lev_dn : int list -> int list
    75   val lev_on : pos -> posel list
    76   val lev_pred : pos -> pos
    77   val lev_up : pos -> pos
    78 (*  val pr_cell : pos -> ppobj -> string
    79   val pr_pos : int list -> string        *)
    80   val pr_ptree : (pos -> ppobj -> string) -> ptree -> string
    81   val pr_short : pos -> ppobj -> string
    82 (*  val repl : 'a list -> int -> 'a -> 'a list
    83   val repl_app : 'a list -> int -> 'a -> 'a list
    84   val repl_branch : branch -> ppobj -> ppobj
    85   val repl_domID : domID -> ppobj -> ppobj
    86   val repl_form : cterm' -> ppobj -> ppobj
    87   val repl_met : item ppc -> ppobj -> ppobj
    88   val repl_metID : metID -> ppobj -> ppobj
    89   val repl_model : cterm' list -> ppobj -> ppobj
    90   val repl_tac : tac -> ppobj -> ppobj
    91   val repl_pbl : item ppc -> ppobj -> ppobj
    92   val repl_pblID : pblID -> ppobj -> ppobj
    93   val repl_result : cterm' -> ostate -> ppobj -> ppobj
    94   val repl_spec : spec -> ppobj -> ppobj
    95   val repl_subs : (string * string) list -> ppobj -> ppobj
    96   val test_trans : ppobj -> bool
    97   val uni__asm : (string * pos) list -> ppobj -> ppobj
    98   val uni__cid : cellID list -> ppobj -> ppobj                 *)
    99   val union_asm : ptree -> pos -> (string * pos) list -> ptree
   100   val union_cid : ptree -> pos -> cellID list -> ptree
   101   val update_branch : ptree -> pos -> branch -> ptree
   102   val update_domID : ptree -> pos -> domID -> ptree
   103   val update_met : ptree -> pos -> meth -> ptree
   104   val update_metppc : ptree -> pos -> item ppc -> ptree
   105   val update_metID : ptree -> pos -> metID -> ptree
   106   val update_tac : ptree -> pos -> tac -> ptree
   107   val update_pbl : ptree -> pos -> pblID * item ppc -> ptree
   108   val update_pblppc : ptree -> pos -> item ppc -> ptree
   109   val update_pblID : ptree -> pos -> pblID -> ptree
   110   val update_spec : ptree -> pos -> spec -> ptree
   111   val update_subs : ptree -> pos -> (string * string) list -> ptree
   112 
   113   val rep_pblobj : ppobj
   114     -> {branch:branch, cell:cid, env:envp, loc:loc, meth:meth, model:cterm' ppc,
   115         origin:cterm' list * spec, ostate:ostate, probl:pblID * item ppc,
   116         result:cterm', spec:spec}
   117   val rep_prfobj : ppobj
   118     -> {branch:branch, cell:cid, form:cterm', loc:loc, tac:tac,
   119         ostate:ostate, result:cterm'}
   120 end 
   121 
   122 (* -------------- 
   123 structure Ptree (**): PTREE (**) =
   124 struct
   125  -------------- *)
   126 
   127 type env = (term * term) list;
   128 
   129    
   130 datatype branch = 
   131 	 NoBranch | AndB | OrB 
   132        | TransitiveB  (* FIXXXME.8.03: set branch from met in Apply_Method*)
   133        | SequenceB | IntersectB | CollectB | MapB;
   134 fun branch2str NoBranch = "NoBranch"
   135   | branch2str AndB = "AndB"
   136   | branch2str OrB = "OrB"
   137   | branch2str TransitiveB = "TransitiveB" 
   138   | branch2str SequenceB = "SequenceB"
   139   | branch2str IntersectB = "IntersectB"
   140   | branch2str CollectB = "CollectB"
   141   | branch2str MapB = "MapB";
   142 
   143 datatype ostate = 
   144     Incomplete | Complete | Inconsistent;
   145 
   146 type cellID = int;     
   147 type cid = cellID list;
   148 
   149 type posel = int;     (* roundabout for (some of) nice signatures *)
   150 type pos = posel list;
   151 datatype pos_ = 
   152     Pbl    (*PblObj-position: problem-type*)
   153   | Met    (*PblObj-position: method*)
   154   | Frm    (* PrfObj-position: formula*)
   155   | Res    (*PblObj | PrfObj-position: result*)
   156   | Und;   (*undefined*)
   157 fun pos_2str Pbl = "Pbl"
   158   | pos_2str Met = "Met"
   159   | pos_2str Frm = "Frm"
   160   | pos_2str Res = "Res"
   161   | pos_2str Und = "Und";
   162 
   163 type pos' = pos * pos_;
   164 fun pos'2str (p,p_) = pair2str (ints2str p, pos_2str p_);
   165 val e_pos' = ([],Und):pos';
   166 
   167 
   168 fun res2str (t, ts) = pair2str (term2str t, terms2str ts);
   169 fun asm2str (t, p:pos) = pair2str (term2str t, ints2str' p);
   170 fun asms2str asms = (strs2str' o (map asm2str)) asms;
   171 
   172 
   173 
   174 datatype lrd = (*elements of a path (=loc_) into an Isabelle term*)
   175 	 L     (*go left at $*) 
   176        | R     (*go right at $*)
   177        | D;     (*go down at Abs*)
   178 type loc_ = lrd list;
   179 fun ldr2str L = "L"
   180   | ldr2str R = "R"
   181   | ldr2str D = "D";
   182 fun loc_2str (k:loc_) = (strs2str' o (map ldr2str)) k;
   183 
   184 (*26.4.02: never used after introduction of scripts !!!*)
   185 type loc =  loc_ *        (* + interpreter-state          *)
   186 	    (loc_ * rls') (* -"- for script of the ruleset*)
   187 		option;
   188 val e_loc = ([],None):loc;
   189 val ee_loc = (e_loc,e_loc);
   190 
   191 
   192 datatype safe = Sundef | Safe | Unsafe | Helpless;
   193 fun safe2str Sundef   = "Sundef"
   194   | safe2str Safe     = "Safe"
   195   | safe2str Unsafe   = "Unsafe" 
   196   | safe2str Helpless = "Helpless";
   197 
   198 type subs = cterm' list; (*16.11.00 for FE-KE*)
   199 fun subst2str' thy' (s:subst) =
   200   (strs2str o 
   201    (map (pair2str o
   202 	 (apsnd (Sign.string_of_term (sign_of (assoc_thy thy')))) o 
   203 	 (apfst (Sign.string_of_term (sign_of (assoc_thy thy'))))))) s;
   204 
   205 type scrstate =       (*state for script interpreter*)
   206 	 env(*stack*) (*used to instantiate tac for checking assod*)
   207 	 * loc_       (*location of tac in script*)
   208 	 * term option(*argument of curried functions*)
   209 	 * term       (*value obtained by tac executed*)
   210 	 * safe       (*estimation of how result will be obtained*)
   211 	 * bool;      (*true = strongly .., false = weakly associated: 
   212 					    only used during ass_dn/up*)
   213 val e_scrstate = ([],[],None,e_term,Sundef,false):scrstate;
   214 
   215 (*21.8.02 ---> definitions.sml for datatype scr 
   216 type rrlsstate =      (*state for reverse rewriting*)
   217      (term *          (*the current formula*)
   218       rule list      (*of reverse rewrite set (#1#)*)
   219 	    list *    (*may be serveral, eg. in norm_rational*)
   220       (rule *         (*Thm (+ Thm generated from Calc) resulting in ...*)
   221        (term *        (*... rewrite with ...*)
   222 	term list))   (*... assumptions*)
   223 	  list);      (*derivation from given term to normalform
   224 		       in reverse order with sym_thm; 
   225                        (#1#) could be extracted from here #1*) --------*)
   226      
   227 datatype istate =     (*interpreter state*)
   228 	 Uistate                 (*undefined in modspec*)
   229        | ScrState of scrstate    (*for script interpreter*)
   230        | RrlsState of rrlsstate; (*for reverse rewriting*)
   231 type iist = istate option * istate option;
   232 val e_istate = (ScrState ([],[],None,e_term,Sundef,false)):istate;
   233 (*val e_iist = (e_istate, e_istate); --- sinnlos f"ur NICHT-equality-type*) 
   234 fun rta2str (r,(t,a)) = "\n("^(rule2str r)^",("^(term2str t)^", "^
   235 		      (terms2str a)^"))";
   236 fun istate2str (ScrState (e,l,to,t,s,b):istate) =
   237     "ScrState ("^ subst2str e ^",\n "^ 
   238     loc_2str l ^", "^ termopt2str to ^",\n "^
   239     term2str t ^", "^ safe2str s ^", "^ bool2str b ^")"
   240   | istate2str (RrlsState (t,t1,rss,rtas)) = 
   241     "RrlsState ("^(term2str t)^", "^(term2str t1)^", "^
   242     ((strs2str o (map (strs2str o (map rule2str)))) rss)^", "^
   243     ((strs2str o (map rta2str)) rtas)^")";
   244 
   245 
   246 type spec = domID * pblID * metID;
   247 fun spec2str ((dom,pbl,met)(*:spec*)) = 
   248   "(" ^ (quote dom) ^ ", " ^ (strs2str pbl) ^ 
   249   ", " ^ (strs2str met) ^ ")";
   250 (*> spec2str empty_spec;
   251 val it = "(\"\", [], (\"\", \"\"))" : string *)
   252 val empty_spec = (e_domID,e_pblID,e_metID):spec;
   253 val e_spec = empty_spec;
   254 
   255 datatype con = land | lor;
   256 
   257 
   258 fun subst2subs s = map (pair2str o 
   259 			(apfst (Sign.string_of_term (sign_of thy))) o
   260 			(apsnd (Sign.string_of_term (sign_of thy)))) s;
   261 fun subst2subs' s = map ((apfst (Sign.string_of_term (sign_of thy))) o
   262 			 (apsnd (Sign.string_of_term (sign_of thy)))) s;
   263 
   264 
   265 
   266 fun isapair2pair (Const ("Pair",_) $ a $ b) = (a,b);
   267 fun subs2subst thy s = map (isapair2pair o term_of o the o (parse thy)) s;
   268 (*> subs2subst thy ["(bdv,x)","(err,#0)"];
   269 val it =
   270   [(Free ("bdv","RealDef.real"),Free ("x","RealDef.real")),
   271    (Free ("err","RealDef.real"),Free ("#0","RealDef.real"))] 
   272    : (term * term) list*)
   273 
   274 
   275 
   276 
   277 
   278 
   279 (*FE <-> KE: strings*)
   280 datatype tac = 
   281   Init_Proof of ((cterm' list) * spec) 
   282 (*Init_Proo_Hid of (dgmode * (cterm' list) * spec) 
   283 | Init_Proof                                       7.6.02java-sml*)
   284 | Model_Problem of pblID
   285 | Refine_Problem of pblID              | Refine_Tacitly of pblID
   286 (*| Match_Problem of pblID*)
   287 | Add_Given of cterm'                  | Del_Given of cterm'
   288 | Add_Find of cterm'                   | Del_Find of cterm'
   289 | Add_Relation of cterm'               | Del_Relation of cterm'
   290 
   291 | Specify_Theory of domID              | Specify_Problem of pblID
   292 | Specify_Method of metID
   293 | Apply_Method of metID                | Check_Postcond of pblID
   294 | Free_Solve
   295 
   296 | Rewrite_Inst of ( subs * thm')       | Rewrite of thm'
   297                                        | Rewrite_Asm of thm'
   298 | Rewrite_Set_Inst of ( subs * rls')   | Rewrite_Set of rls'        
   299 | Detail      (*user_cmd for detailing some _EXISTING_ data*)
   300 | End_Detail  (*switches back to parent script*)                   
   301 | Detail_Set_Inst of ( subs * rls')    | Detail_Set of rls'
   302 	      (*EITHER: an input of me, details rewrites in a Set _after_
   303                 Rewrite_Set* has created PblObj (+ istate);
   304                 OR:: set by "solve Detail_Set" as tac in Transitive --
   305 		for recognition of "insert pt" detail (versus "cappend pt"*)
   306 | Calculate of string                  | End_Ruleset
   307             (* plus | minus | times | cancel | pow | sqrt *)
   308 | Substitute of subs                   | Apply_Assumption of cterm' list
   309 
   310 | Take of cterm'                       | Take_Inst of cterm'  
   311 | Group of (con * int list ) 
   312 (*| Subproblem_Full of (spec * (cterm' list))   *)
   313 | Subproblem of (domID * pblID)
   314 | CAScmd of cterm'  (*6.6.02 URD: Function formula *)                   
   315 | End_Subproblem
   316 
   317 | Split_And                            | Conclude_And
   318 | Split_Or                             | Conclude_Or
   319 | Begin_Trans                          | End_Trans
   320 | Begin_Sequ                           | End_Sequ(* substitute root.env *)
   321 | Split_Intersect                      | End_Intersect
   322 | Check_elementwise of cterm'          | Collect_Trues
   323 | Or_to_List
   324 
   325 | Empty_Tac (*TODO.11.6.03 ... of string: could carry msg of (Notappl msg)
   326 	       in 'helpless'*)
   327 | Tac of string(* eg.'repeat'*)
   328 | User (*internal, for ets*)           | End_Proof';(* inout*)
   329 
   330 
   331 (* tac2str /--> library.sml: needed in dialog.sml for 'separable *)
   332 fun tac2str (ma:tac) = case ma of
   333     Init_Proof (ppc, spec)  => 
   334       "Init_Proof "^(pair2str (strs2str ppc, spec2str spec))
   335   | Model_Problem pblID     => "Model_Problem "^(strs2str pblID)
   336   | Refine_Tacitly pblID    => "Refine_Tacitly "^(strs2str pblID)
   337   | Refine_Problem pblID    => "Refine_Problem "^(strs2str pblID)
   338 (*| Match_Problem pblID     => "Match_Problem "^(strs2str pblID)*)
   339   | Add_Given cterm'        => "Add_Given "^cterm'
   340   | Del_Given cterm'        => "Del_Given "^cterm'
   341   | Add_Find cterm'         => "Add_Find "^cterm'
   342   | Del_Find cterm'         => "Del_Find "^cterm'
   343   | Add_Relation cterm'     => "Add_Relation "^cterm'
   344   | Del_Relation cterm'     => "Del_Relation "^cterm'
   345 
   346   | Specify_Theory domID    => "Specify_Theory "^(quote domID    )
   347   | Specify_Problem pblID   => "Specify_Problem "^(strs2str pblID )
   348   | Specify_Method metID    => "Specify_Method "^(strs2str metID)
   349   | Apply_Method metID      => "Apply_Method "^(strs2str metID)
   350   | Check_Postcond pblID    => "Check_Postcond "^(strs2str pblID)
   351   | Free_Solve              => "Free_Solve"
   352 
   353   | Rewrite_Inst (subs,thm')=> 
   354       "Rewrite_Inst "^(pair2str (subs2str subs, spair2str thm'))
   355   | Rewrite thm'            => "Rewrite "^(spair2str thm')
   356   | Rewrite_Asm thm'        => "Rewrite_Asm "^(spair2str thm')
   357   | Rewrite_Set_Inst (subs, rls) => 
   358       "Rewrite_Set_Inst "^(pair2str (subs2str subs, quote rls))
   359   | Rewrite_Set rls         => "Rewrite_Set "^(quote rls    )
   360   | Detail                  => "Detail"
   361   | End_Detail              => "End_Detail"
   362   | Detail_Set rls          => "Detail_Set "^(quote rls    )
   363   | Detail_Set_Inst (subs, rls) => 
   364       "Detail_Set_Inst "^(pair2str (subs2str subs, quote rls))
   365   | Calculate op_           => "Calculate "^op_ 
   366   | Substitute subs         => "Substitute "^(subs2str subs)	     
   367   | Apply_Assumption ct's   => "Apply_Assumption "^(strs2str ct's)
   368 
   369   | Take cterm'             => "Take "^(quote cterm'	)
   370   | Take_Inst cterm'        => "Take_Inst "^(quote cterm' )
   371   | Group (con, ints)       => 
   372       "Group "^(pair2str (con2str con, ints2str ints))
   373   | Subproblem (domID, pblID) => 
   374       "Subproblem "^(pair2str (domID, strs2str pblID))
   375 (*| Subproblem_Full (spec, cts') => 
   376       "Subproblem_Full "^(pair2str (spec2str spec, strs2str cts'))*)
   377   | End_Subproblem          => "End_Subproblem"
   378   | CAScmd cterm'           => "CAScmd "^(quote cterm')
   379 
   380   | Check_elementwise cterm'=> "Check_elementwise "^(quote cterm') 
   381   | Or_to_List              => "Or_to_List "
   382   | Collect_Trues           => "Collect_Trues"
   383 
   384   | Empty_Tac             => "Empty_Tac"
   385   | Tac string            => "Tac "^string
   386   | User                    => "User"
   387   | End_Proof'              => "tac End_Proof'"
   388   | _                       => "tac2str not impl. for ?!";
   389 
   390 
   391 fun tac2IDstr (ma:tac) = case ma of
   392     Model_Problem _     => "Model_Problem"
   393   | Refine_Tacitly pblID    => "Refine_Tacitly"
   394   | Refine_Problem pblID    => "Refine_Problem"
   395   | Add_Given cterm'        => "Add_Given"
   396   | Del_Given cterm'        => "Del_Given"
   397   | Add_Find cterm'         => "Add_Find"
   398   | Del_Find cterm'         => "Del_Find"
   399   | Add_Relation cterm'     => "Add_Relation"
   400   | Del_Relation cterm'     => "Del_Relation"
   401 
   402   | Specify_Theory domID    => "Specify_Theory"
   403   | Specify_Problem pblID   => "Specify_Problem"
   404   | Specify_Method metID    => "Specify_Method"
   405   | Apply_Method metID      => "Apply_Method"
   406   | Check_Postcond pblID    => "Check_Postcond"
   407   | Free_Solve              => "Free_Solve"
   408 
   409   | Rewrite_Inst (subs,thm')=> "Rewrite_Inst"
   410   | Rewrite thm'            => "Rewrite"
   411   | Rewrite_Asm thm'        => "Rewrite_Asm"
   412   | Rewrite_Set_Inst (subs, rls) => "Rewrite_Set_Inst"
   413   | Rewrite_Set rls         => "Rewrite_Set"
   414   | Detail                  => "Detail"
   415   | End_Detail              => "End_Detail"
   416   | Detail_Set rls          => "Detail_Set"
   417   | Detail_Set_Inst (subs, rls) => "Detail_Set_Inst"
   418   | Calculate op_           => "Calculate "
   419   | Substitute subs         => "Substitute" 
   420   | Apply_Assumption ct's   => "Apply_Assumption"
   421 
   422   | Take cterm'             => "Take"
   423   | Take_Inst cterm'        => "Take_Inst"
   424   | Group (con, ints)       => "Group"
   425   | Subproblem (domID, pblID) => "Subproblem"
   426   | End_Subproblem          => "End_Subproblem"
   427   | CAScmd cterm'           => "CAScmd"
   428 
   429   | Check_elementwise cterm'=> "Check_elementwise"
   430   | Or_to_List              => "Or_to_List "
   431   | Collect_Trues           => "Collect_Trues"
   432 
   433   | Empty_Tac             => "Empty_Tac"
   434   | Tac string            => "Tac "
   435   | User                    => "User"
   436   | End_Proof'              => "End_Proof'"
   437   | _                       => "tac2str not impl. for ?!";
   438 
   439 
   440 type fmz_ = cterm' list;
   441 type fmz = fmz_ * spec;
   442 val e_fmz = ([],e_spec);
   443 
   444 (*tac_ is made from tac in applicable_in,
   445   and carries all data necessary for generate;*)
   446 datatype tac_ = 
   447 (* datatype tac = *)
   448   Init_Proof' of ((cterm' list) * spec)
   449                 (* ori list !: code specify -> applicable*)
   450 | Model_Problem' of pblID * 
   451 		    itm list   (*the 'untouched' pbl*)
   452 | Refine_Tacitly' of pblID *    (*input*)
   453 		     pblID *    (*the refined from applicable_in*)
   454 		     domID *    (*from new pbt?! filled in specify*)
   455 		     metID *    (*from new pbt?! filled in specify*)
   456 		     itm list   (*drop ! 9.03: remains [] for
   457                                   Model_Problem recognizing its activation*)
   458 | Refine_Problem' of (pblID * (itm list * (bool * Term.term) list))
   459 | Add_Given'    of cterm' *
   460 		   itm list (*updated with input in fun specify_additem*)
   461 | Add_Find'     of cterm' *
   462 		   itm list (*updated with input in fun specify_additem*)
   463 | Add_Relation' of cterm' *
   464 		 itm list (*updated with input in fun specify_additem*)
   465 | Del_Given' of cterm'   | Del_Find' of cterm'   | Del_Relation' of cterm'
   466   (*4.00.: all..    term: in applicable_in ..? Syn ?only for FormFK?*)
   467 
   468 | Specify_Theory' of domID              
   469 | Specify_Problem' of (pblID *        (*               *)
   470 		       (bool *        (* matches	     *)
   471 			(itm list *   (* ppc	     *)
   472 			 (bool * term) list))) (* preconditions *)
   473 | Specify_Method' of metID *
   474 		     ori list * (*repl. "#undef"*)
   475 		     itm list   (*... updated from pbl to met*)
   476 | Apply_Method' of metID *
   477 		   (term * istate)
   478 		       option (*only for the init_form*)
   479 | Check_Postcond' of 
   480   pblID * 
   481   (term *      (*returnvalue of script in solve*)
   482    cterm' list)(*collect by get_assumptions_ in applicable_in, except if 
   483                  butlast tac is Check_elementwise: take only these asms*)
   484 | Free_Solve'
   485 
   486 | Rewrite_Inst' of theory' * rew_ord' * rls
   487   * bool * subst * thm' * term * (term  * term list)
   488                     (*... form * (result* asumpts  ), saves time*)
   489 | Rewrite' of theory' * rew_ord' * rls * bool * thm' * 
   490   term * (term * term list)
   491 | Rewrite_Asm' of theory' * rew_ord' * rls * bool * thm' * 
   492   term * (term * term list)
   493 | Rewrite_Set_Inst' of theory' * bool * subst * rls * 
   494   term * (term * term list)
   495 | Rewrite_Set' of theory' * bool * rls * term * (term * term list)
   496 (*Detail' in me detailed into ...*)
   497 | End_Detail' of term (*see End_Trans'*)
   498 | Detail_Set' of theory' * rls * term(*term is needed for next_tac before 2nd
   499 				     applicable_in*)
   500 | Detail_Set_Inst' of theory' * subst * rls * term
   501 | End_Ruleset' of term
   502 | Calculate' of theory' * string * term * (term * thm') 
   503 	      (*WN.29.4.03 asm?: * term list??*)
   504 | Substitute' of subst * term (*10.8.00+..*)* term              
   505 | Apply_Assumption' of term list * term
   506 
   507 | Take' of term                         | Take_Inst' of term  
   508 | Group' of (con * int list * term)
   509 | Subproblem' of (spec * 
   510 		  (ori list) * (*filled in assod Subproblem'*)
   511 		  term *       (*-"-, headline of calc-head *)
   512 		  fmz_ * 
   513 		  term)        (*Subproblem(dom,pbl)*)  
   514 | CAScmd' of term
   515 | End_Subproblem' of term (*???*)
   516 | Split_And' of term                    | Conclude_And' of term
   517 | Split_Or' of term                     | Conclude_Or' of term
   518 | Begin_Trans' of term                  | End_Trans' of term
   519 | Begin_Sequ'                           | End_Sequ'(* substitute root.env*)
   520 | Split_Intersect' of term              | End_Intersect' of term
   521 | Check_elementwise' of (*special case:*)
   522   term *   (*(1)the current formula: [x=1,x=...]*)
   523   string * (*(2)the pred from Check_elementwise   *)
   524   (term *  (*(3)composed from (1) and (2): {x. pred}*)
   525    term list) (*20.5.03 assumptions*)
   526 
   527 | Or_to_List' of term * term            (* (a | b, [a,b]) *)
   528 | Collect_Trues' of term
   529 
   530 | Empty_Tac_                          | Tac_ of  (*for dummies*)
   531                                             theory *
   532                                             string * (*form*)
   533 					    string * (*in Tac*)
   534 					    string   (*result of Tac".."*)
   535 | User' (*internal for ets*)            | End_Proof'';(*End_Proof:inout*)
   536 (*TODO?: done partially for tests*)
   537 fun tac_2str ma = case ma of
   538     Init_Proof' (ppc, spec)  => 
   539       "Init_Proof' "^(pair2str (strs2str ppc, spec2str spec))
   540   | Model_Problem' (pblID,_)     => "Model_Problem' "^(strs2str pblID )
   541   | Refine_Tacitly'(p,prefin,domID,metID,itms)=> 
   542     "Refine_Tacitly' ("
   543     ^(strs2str p)^", "^(strs2str prefin)^", "
   544     ^domID^", "^(strs2str metID)^", pbl-itms)"
   545   | Refine_Problem' ms       => "Refine_Problem' ("^(*matchs2str ms*)"..."^")"
   546 (*| Match_Problem' (pI, (ok, (itms, pre))) => 
   547     "Match_Problem' "^(spair2str (strs2str pI,
   548 				  spair2str (bool2str ok,
   549 					     spair2str ("itms2str itms", 
   550 							"items2str pre"))))*)
   551   | Add_Given' cterm'        => "Add_Given' "(*^cterm'*)
   552   | Del_Given' cterm'        => "Del_Given' "(*^cterm'*)
   553   | Add_Find' cterm'         => "Add_Find' "(*^cterm'*)
   554   | Del_Find' cterm'         => "Del_Find' "(*^cterm'*)
   555   | Add_Relation' cterm'     => "Add_Relation' "(*^cterm'*)
   556   | Del_Relation' cterm'     => "Del_Relation' "(*^cterm'*)
   557 
   558   | Specify_Theory' domID    => "Specify_Theory' "^(quote domID    )
   559   | Specify_Problem' (pI, (ok, (itms, pre))) => 
   560     "Specify_Problem' "^(spair2str (strs2str pI,
   561 				  spair2str (bool2str ok,
   562 					     spair2str ("itms2str itms", 
   563 							"items2str pre"))))
   564   | Specify_Method' (pI,oris,itms) => 
   565     "Specify_Method' ("^metID2str pI^", "^oris2str oris^", )"
   566 
   567   | Apply_Method' (metID,_)      => "Apply_Method' "^(strs2str metID)
   568   | Check_Postcond' (pblID,(scval,asm)) => 
   569       "Check_Postcond' "^(spair2str(strs2str pblID, 
   570 				    spair2str (term2str scval, strs2str asm)))
   571 
   572   | Free_Solve'              => "Free_Solve'"
   573 
   574   | Rewrite_Inst' (*subs,thm'*) _ => 
   575       "Rewrite_Inst' "(*^(pair2str (subs2str subs, spair2str thm'))*)
   576   | Rewrite' thm'            => "Rewrite' "(*^(spair2str thm')*)
   577   | Rewrite_Asm' thm'        => "Rewrite_Asm' "(*^(spair2str thm')*)
   578   | Rewrite_Set_Inst' (*subs,thm'*) _ => 
   579       "Rewrite_Set_Inst' "(*^(pair2str (subs2str subs, quote rls))*)
   580   | Rewrite_Set'(thy',pasm,rls',f,(f',asm))          
   581     => "Rewrite_Set' ("^thy'^","^(bool2str pasm)^","^(id_rls rls')^","
   582     ^(Sign.string_of_term (sign_of thy) f)^",("^(Sign.string_of_term (sign_of thy) f')
   583     ^","^((strs2str o (map (Sign.string_of_term (sign_of thy)))) asm)^"))"
   584 
   585   | End_Detail' _             => "End_Detail' xxx"
   586   | Detail_Set' _             => "Detail_Set' xxx"
   587   | Detail_Set_Inst' _        => "Detail_Set_Inst' xxx"
   588 
   589   | Calculate'  _            => "Calculate' "
   590   | Substitute' subs         => "Substitute' "(*^(subs2str subs)*)    
   591   | Apply_Assumption' ct's   => "Apply_Assumption' "(*^(strs2str ct's)*)
   592 
   593   | Take' cterm'             => "Take' "(*^(quote cterm'	)*)
   594   | Take_Inst' cterm'        => "Take_Inst' "(*^(quote cterm' )*)
   595   | Group' (con, ints, _)     => 
   596       "Group' "^(pair2str (con2str con, ints2str ints))
   597   | Subproblem' (spec, oris, _,_,pbl_form) => 
   598       "Subproblem' "(*^(pair2str (domID, strs2str ,...))*)
   599   | End_Subproblem'  _       => "End_Subproblem'"
   600   | CAScmd' cterm'           => "CAScmd' "(*^(quote cterm')*)
   601 
   602   | Empty_Tac_             => "Empty_Tac_"
   603   | User'                    => "User'"
   604   | Tac_ (_,form,id,result) => "Tac_ (thy,"^form^","^id^","^result^")"
   605   | _                       => "";
   606 
   607 (*'executed tactics' (tac_s) with local environment etc.;
   608   used for continuing eval script + for generate*)
   609 type ets =
   610     (loc_ *      (* of tactic in scr, tactic (weakly) associated with tac_*)
   611      (tac_ * 	 (* (for generate)  *)
   612       env *      (* with 'tactic=result' as a rule, tactic ev. _not_ ready:
   613 		  for handling 'parallel let'*)
   614       env *      (* with results of (ready) tacs        *)
   615       term *     (* itr_arg of tactic, for upd. env at Repeat, Try*)
   616       term * 	 (* result value of the tac         *)
   617       safe))
   618     list;
   619 val Ets = []:ets;
   620 
   621 
   622 fun ets2s (l,(m,eno,env,iar,res,s)) = 
   623   "\n("^(loc_2str l)^",("^(tac_2str m)^
   624   ",\n  ens= "^(subst2str eno)^
   625   ",\n  env= "^(subst2str env)^
   626   ",\n  iar= "^(Sign.string_of_term (sign_of thy) iar)^
   627   ",\n  res= "^(Sign.string_of_term (sign_of thy) res)^
   628   ",\n  "^(safe2str s)^"))";
   629 fun ets2str (ets:ets) = (strs2str o (map ets2s)) ets;
   630 
   631 
   632 type envp =(*9.5.03: unused, delete with field in ptree.PblObj FIXSXXME*)
   633    (int * term list) list * (*assoc-list: args of met*)
   634    (int * rls) list *       (*assoc-list: tacs already done ///15.9.00*)
   635    (int * ets) list *       (*assoc-list: tacs etc. already done*)
   636    (string * pos) list;     (*asms * from where*)
   637 val empty_envp = ([],[],[],[]):envp; 
   638 
   639 datatype ppobj = 
   640     PrfObj of {cell  : cid,    (* 4.00 superfluous: use cid in DG only!!*)
   641 	       form  : term,    
   642 	       tac : tac,  (*8.01: superfluous -> ets !!!*)
   643 	       loc   : istate option * istate option, (*for form, result 
   644 13.8.02: (None,None) <==> e_istate ! see update_loc, get_loc*)
   645 	       branch: branch,
   646 	       result: term * term list,    
   647 	                          (*FIXX@ME result:term * term list
   648 				   => applicable_in =..rewrite_set_
   649 			           => rls = {erls: rls (NOT rls'),...}*)
   650 	       ostate: ostate}    (*Complete <=> result is OK*)
   651   | PblObj of {cell  : cid,       (*unused*)
   652 	       fmz   : fmz,       (*from init:FIXME never use this spec;-drop*)
   653 	       origin: (ori list) * (*representation from fmz+pbt
   654                                   for efficiently adding items in probl, meth*)
   655 		       spec *     (*updated by Refine_Tacitly*)
   656 		       term,      (*headform of calc-head*)
   657 	       spec  : spec,      (*explicitly input + displayed*)
   658 	       probl : itm list,  (*itms explicitly input*)
   659 	       meth  : itm list,  (*itms automatically added to copy of probl*)
   660 	       env   : envp,      (*FIXXXMEby loc 20.8.01 superfluous--delete*)
   661 	       loc   : istate option * istate option, (*for pbl+met, result 
   662 20.8.01: loc will hold ets (diss p.94: (tac_,denval(=form|res),loc,env)*)
   663 	       branch: branch,
   664 	       result: term * term list,
   665 	       ostate: ostate};   (*Complete <=> result is OK*)
   666 datatype ptree = 
   667     EmptyPtree
   668   | Nd of ppobj * (ptree list);
   669 val e_ptree = EmptyPtree;
   670 
   671 (*in CalcTree/Subproblem an 'just_created' model is created;
   672   this is filled to 'untouched' by Model/Refine_Problem*)
   673 fun just_created (PblObj {meth, probl, spec, ...}) = 
   674     null meth andalso null probl andalso spec = e_spec;
   675 val e_origin = ([],e_spec,e_term): (ori list) * spec * term;
   676 
   677 
   678 
   679 fun rep_prfobj (PrfObj {cell,form,tac,loc,branch,result,ostate}) =
   680   {cell=cell,form=form,tac=tac,loc=loc,branch=branch,result=result,ostate=ostate};
   681 fun rep_pblobj (PblObj {cell,origin,fmz,spec,probl,meth,env,
   682 			loc,branch,result,ostate}) =
   683   {cell=cell,origin=origin,fmz=fmz,spec=spec,probl=probl,meth=meth,
   684    env=env,loc=loc,branch=branch,result=result,ostate=ostate};
   685 fun is_prfobj (PrfObj _) = true
   686   | is_prfobj _ =false;
   687 (*val is_prfobj' = get_obj is_prfobj; *)
   688 fun is_pblobj (PblObj _) = true
   689   | is_pblobj _ = false;
   690 (*val is_pblobj' = get_obj is_pblobj; 'Error: unbound constructor get_obj'*)
   691 
   692 
   693 exception PTREE of string;
   694 fun nth _ []      = raise PTREE "nth _ []"
   695   | nth 1 (x::xs) = x
   696   | nth n (x::xs) = nth (n-1) xs;
   697 (*> nth 2 [11,22,33]; -->> val it = 22 : int*)
   698 
   699 fun lev_up ([]:pos) = raise PTREE "lev_up []"
   700   | lev_up p = (drop_last p):pos;
   701 fun lev_on ([]:pos) = raise PTREE "lev_on []"
   702   | lev_on pos = 
   703     let val len = length pos
   704     in (drop_last pos) @ [(nth len pos)+1] end;
   705 fun lev_onFrm ((p,_):pos') = (lev_on p,Frm):pos'
   706   | lev_onFrm p = raise PTREE ("*** lev_onFrm: pos'="^(pos'2str p));
   707 
   708 fun lev_pred ([]:pos) = raise PTREE "lev_pred []"
   709   | lev_pred (pos:pos) = 
   710     let val len = length pos
   711     in ((drop_last pos) @ [(nth len pos)-1]):pos end;
   712 (*lev_pred [1,2,3];
   713 val it = [1,2,2] : pos
   714 > lev_pred [1];
   715 val it = [0] : pos          *)
   716 fun lev_dn p = p @ [0];
   717 (*> (lev_dn o lev_on) [1,2,3];
   718 val it = [1,2,4,0] : pos    *)
   719 fun lev_dn' ((p,p_):pos') = (lev_dn p, Frm):pos';
   720 fun lev_dnRes ((p,_):pos') = (lev_dn p, Res):pos';
   721 
   722 (*4.4.00*)
   723 fun lev_up_ ((p,Res):pos') = (lev_up p,Res):pos'
   724   | lev_up_ p' = raise error ("lev_up_: called for "^(pos'2str p'));
   725 fun lev_dn_ ((p,_):pos') = (lev_dn p,Res):pos'
   726 fun ind ((p,_):pos') = length p;
   727 
   728 
   729 (** convert ptree to a string **)
   730 
   731 (* convert a pos from list to string *)
   732 fun pr_pos ps = (space_implode "." (map string_of_int ps))^".   ";
   733 (* show hd origin or form only *)
   734 fun pr_short (p:pos) (PblObj {origin = (ori,_,_),...}) = 
   735   ((pr_pos p) ^ " ----- pblobj -----\n")
   736 (*   ((((Sign.string_of_term (sign_of thy)) o #4 o hd) ori)^" "^
   737     (((Sign.string_of_term (sign_of thy)) o hd(*!?!*) o #5 o hd) ori))^
   738    "\n") *)
   739   | pr_short p (PrfObj {form = form,...}) =
   740   ((pr_pos p) ^ (term2str form) ^ "\n");
   741 fun pr_cell (p:pos) (PblObj {cell = c, origin = (ori,_,_),...}) = 
   742   ((ints2str c) ^"   "^ 
   743    ((((Sign.string_of_term (sign_of thy)) o #4 o hd) ori)^" "^
   744     (((Sign.string_of_term (sign_of thy)) o hd(*!?!*) o #5 o hd) ori))^
   745    "\n")
   746   | pr_cell p (PrfObj {cell = c, form = form,...}) =
   747   ((ints2str c) ^"   "^ (term2str form) ^ "\n");
   748 
   749 
   750 (* convert ptree *)
   751 fun pr_ptree f pt =
   752   let
   753     fun pr_pt pfn _  EmptyPtree = ""
   754       | pr_pt pfn ps (Nd (b, [])) = pfn ps b
   755       | pr_pt pfn ps (Nd (b, ts)) = (pfn ps b)^
   756       (prts pfn (ps:pos) 1 ts)
   757     and prts pfn ps p [] = ""
   758       | prts pfn ps p (t::ts) = (pr_pt pfn (ps @ [p]) t)^
   759       (prts pfn ps (p+1) ts)
   760   in pr_pt f [] pt end;
   761 (*
   762 > fun prfn ps b = (pr_pos ps)^"   "^b(*TODO*)^"\n";
   763 > val pt = ref EmptyPtree;
   764 > pt:=Nd("root",
   765        [Nd("xx1",[]),
   766 	Nd("xx2",
   767 	   [Nd("xx2.1.",[]),
   768 	    Nd("xx2.2.",[])]),
   769 	Nd("xx3",[])]);
   770 > writeln (pr_ptree prfn (!pt));
   771 *)
   772 
   773 
   774 (** access the branches of ptree **)
   775 
   776 fun ins_nth 1 e l  = e::l
   777   | ins_nth n e [] = raise PTREE "ins_nth n e []"
   778   | ins_nth n e (l::ls) = l::(ins_nth (n-1) e ls);
   779 fun repl []      _ _ = raise PTREE "repl [] _ _"
   780   | repl (l::ls) 1 e = e::ls
   781   | repl (l::ls) n e = l::(repl ls (n-1) e);
   782 fun repl_app ls n e = 
   783     let val lim = 1 + length ls
   784     in if n > lim then raise PTREE "repl_app: n > lim"
   785        else if n = lim then ls @ [e]
   786 	    else repl ls n e end;
   787 (*  
   788 > repl [1,2,3] 2 22222;
   789 val it = [1,22222,3] : int list
   790 > repl_app [1,2,3,4] 5 5555;
   791 val it = [1,2,3,4,5555] : int list
   792 > repl_app [1,2,3] 2 22222;
   793 val it = [1,22222,3] : int list
   794 > repl_app [1] 2 22222 ;
   795 val it = [1,22222] : int list
   796 *)
   797 
   798 
   799 (** get from obj at pos by f : ppobj -> 'a **)
   800 
   801 fun get_obj f EmptyPtree    _      = raise PTREE "get_obj f EmptyPtree"
   802   | get_obj f (Nd (b,  _)) []      = f b
   803   | get_obj f (Nd (b, bs)) (p::ps) = 
   804   let val _ = (nth p bs) handle _ => raise PTREE ("get_obj: pos = "^
   805 			   (ints2str' (p::ps))^" does not exist");
   806   in (get_obj f (nth p bs) (ps:pos)) 
   807     handle _ => raise PTREE ("get_obj: at pos = "^
   808 			     (ints2str' (p::ps))^" wrong type of ppobj")
   809       (*FIXME: 'wrong type..' raised also if pos doesn't exist*)
   810   end;
   811 fun get_nd EmptyPtree _ = raise PTREE "get_nd EmptyPtree"
   812   | get_nd n [] = n
   813   | get_nd (Nd (_,nds)) (pos as p::(ps:pos)) = (get_nd (nth p nds) ps)
   814     handle _ => raise PTREE ("get_nd: not existent pos = "^(ints2str' pos));
   815 
   816 
   817 (* for use by get_obj *)
   818 fun g_cell   (PblObj {cell = c,...}) = c
   819   | g_cell   (PrfObj {cell = c,...}) = c;
   820 fun g_form   (PrfObj {form = f,...}) = f
   821   | g_form   _ = raise PTREE "g_form not for PblObj";
   822 fun g_origin (PblObj {origin = ori,...}) = ori
   823   | g_origin _ = raise PTREE "g_origin not for PrfObj";
   824 fun g_fmz (PblObj {fmz = f,...}) = f
   825   | g_fmz _ = raise PTREE "g_fmz not for PrfObj";
   826 fun g_spec   (PblObj {spec = s,...}) = s
   827   | g_spec _   = raise PTREE "g_spec not for PrfObj";
   828 fun g_pbl    (PblObj {probl = p,...}) = p
   829   | g_pbl  _   = raise PTREE "g_pbl not for PrfObj";
   830 fun g_met    (PblObj {meth = p,...}) = p
   831   | g_met  _   = raise PTREE "g_met not for PrfObj";
   832 fun g_domID  (PblObj {spec = (d,_,_),...}) = d
   833   | g_domID  _ = raise PTREE "g_metID not for PrfObj";
   834 fun g_metID  (PblObj {spec = (_,_,m),...}) = m
   835   | g_metID  _ = raise PTREE "g_metID not for PrfObj";
   836 fun g_args    (PblObj {env = (a,_,_,_),...}) = a
   837   | g_args _    = raise PTREE "g_args not for PrfObj";
   838 fun g_tacs    (PblObj {env = (_,a,_,_),...}) = a
   839   | g_tacs _    = raise PTREE "g_tacs not for PrfObj";
   840 fun g_ets    (PblObj {env = (_,_,a,_),...}) = a
   841   | g_ets _    = raise PTREE "g_tacs not for PrfObj";
   842 fun g_asm    (PblObj {env = (_,_,_,a),...}) = a
   843   | g_asm _    = raise PTREE "g_asm not for PrfObj";
   844 fun g_loc    (PblObj {loc = l,...}) = l
   845   | g_loc    (PrfObj {loc = l,...}) = l;
   846 fun g_branch (PblObj {branch = b,...}) = b
   847   | g_branch (PrfObj {branch = b,...}) = b;
   848 fun g_tac  (PblObj {spec = (d,p,m),...}) = Apply_Method m
   849   | g_tac  (PrfObj {tac = m,...}) = m;
   850 fun g_result (PblObj {result = r,...}) = r
   851   | g_result (PrfObj {result = r,...}) = r;
   852 fun g_ostate (PblObj {ostate = r,...}) = r
   853   | g_ostate (PrfObj {ostate = r,...}) = r;
   854 
   855 fun gpt_cell (Nd (PblObj {cell = c,...},_)) = c
   856   | gpt_cell (Nd (PrfObj {cell = c,...},_)) = c;
   857 
   858 fun existpt pos pt = can (get_obj I pt) pos;
   859 fun existpt' ((p,p_):pos') pt = 
   860     if can (get_obj I pt) p 
   861     then case p_ of 
   862 	     Res => get_obj g_ostate pt p = Complete
   863 	   | _ => true
   864     else false;
   865 
   866 (*.find the position of the next parent which is a PblObj in ptree.*)
   867 fun par_pblobj pt ([]:pos) = ([]:pos)
   868   | par_pblobj pt p =
   869     let fun par pt [] = []
   870 	  | par pt p = if is_pblobj (get_obj I pt p) then p
   871 		       else par pt (lev_up p)
   872     in par pt (lev_up p) end; 
   873 (* lev_up for hard_gen operating with pos = [...,0] *)
   874 
   875 (*.find the position and the children of the next parent which is a PblObj.*)
   876 fun par_children (Nd (PblObj _, children)) ([]:pos) = (children, []:pos)
   877   | par_children (pt as Nd (PblObj _, children)) p =
   878     let fun par [] = (children, [])
   879 	  | par p = let val Nd (obj, children) = get_nd pt p
   880 		    in if is_pblobj obj then (children, p) else par (lev_up p)
   881 		    end;
   882     in par (lev_up p) end; 
   883 
   884 (*.get the children of a node in ptree.*)
   885 fun children (Nd (PblObj _, cn)) = cn
   886   | children (Nd (PrfObj _, cn)) = cn;
   887 
   888 
   889 (*.find the next parent, which is either a PblObj (return true)
   890   or a PrfObj with tac = Detail_Set (return false).*)
   891 (*FIXME.3.4.03:re-organize par_pbl_det after rls' --> rls*)
   892 fun par_pbl_det pt ([]:pos) = (true, []:pos, Erls)
   893   | par_pbl_det pt p =
   894     let fun par pt [] = (true, [], Erls)
   895 	  | par pt p = if is_pblobj (get_obj I pt p) then (true, p, Erls)
   896 		       else case get_obj g_tac pt p of
   897 				Detail_Set rls' => (false, p, assoc_rls rls')
   898 			      | _ => par pt (lev_up p)
   899     in par pt (lev_up p) end; 
   900 
   901 
   902 
   903 
   904 (** get from the whole ptree by f : ppobj -> 'a **)
   905 
   906 fun get_all f EmptyPtree   = []
   907   | get_all f (Nd (b, [])) = [f b]
   908   | get_all f (Nd (b, bs)) = [f b] @ (get_alls f bs)
   909 and get_alls f [] = []
   910   | get_alls f pts = flat (map (get_all f) pts);
   911 
   912 
   913 (** insert obj b into ptree at pos **)
   914 
   915 fun insert b EmptyPtree    []       = Nd (b, [])
   916   | insert b EmptyPtree    _        = raise PTREE "insert b Empty _"
   917   | insert b (Nd ( _,  _)) []       = raise PTREE "insert b _ []"
   918   | insert b (Nd (b', bs)) (p::[])  = 
   919      Nd (b', repl_app bs p (Nd (b,[]))) 
   920   | insert b (Nd (b', bs)) (p::ps)  =
   921      Nd (b', repl_app bs p (insert b (nth p bs) (ps:pos)));
   922 (*
   923 > type ppobj = string;
   924 > writeln (pr_ptree prfn (!pt));
   925   val pt = ref Empty;
   926   pt:= insert ("root":ppobj) EmptyPtree [];
   927   pt:= insert ("xx1":ppobj) (!pt) [1];
   928   pt:= insert ("xx2":ppobj) (!pt) [2];
   929   pt:= insert ("xx3":ppobj) (!pt) [3];
   930   pt:= insert ("xx2.1":ppobj) (!pt) [2,1];
   931   pt:= insert ("xx2.2":ppobj) (!pt) [2,2];
   932   pt:= insert ("xx2.1.1":ppobj) (!pt) [2,1,1];
   933   pt:= insert ("xx2.1.2":ppobj) (!pt) [2,1,2];
   934   pt:= insert ("xx2.1.3":ppobj) (!pt) [2,1,3];
   935 *)
   936 
   937 
   938 (** apply f to obj at pos, f: ppobj -> ppobj **)
   939 
   940 fun appl_to_node f (Nd (b,bs)) = Nd (f b, bs);
   941 fun appl_obj f EmptyPtree    []      = EmptyPtree
   942   | appl_obj f EmptyPtree    _       = raise PTREE "appl_obj f Empty _"
   943   | appl_obj f (Nd (b, bs)) []       = Nd (f b, bs)
   944   | appl_obj f (Nd (b, bs)) (p::[])  = 
   945      Nd (b, repl_app bs p (((appl_to_node f) o (nth p)) bs))
   946   | appl_obj f (Nd (b, bs)) (p::ps)  =
   947      Nd (b, repl_app bs p (appl_obj f (nth p bs) (ps:pos)));
   948  
   949 (* for use by appl_obj *) 
   950 fun repl_form f (PrfObj {cell=c,form= _,tac=tac,loc=loc,
   951 			 branch=branch,result=result,ostate=ostate}) =
   952     PrfObj {cell=c,form= f,tac=tac,loc=loc,
   953 	    branch=branch,result=result,ostate=ostate}
   954   | repl_form _ _ = raise PTREE "repl_form takes no PblObj";
   955 fun repl_pbl x    (PblObj {cell=cell,origin=origin,fmz=fmz,
   956 			   spec=spec,probl=_,meth=meth,env=env,loc=loc,
   957 			   branch=branch,result=result,ostate=ostate}) =
   958   PblObj {cell=cell,origin=origin,fmz=fmz,spec=spec,probl= x,
   959 	  meth=meth,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
   960   | repl_pbl _ _ = raise PTREE "repl_pbl takes no PrfObj";
   961 fun repl_met x    (PblObj {cell=cell,origin=origin,fmz=fmz,
   962 			   spec=spec,probl=probl,meth=_,env=env,loc=loc,
   963 			   branch=branch,result=result,ostate=ostate}) =
   964   PblObj {cell=cell,origin=origin,fmz=fmz,spec=spec,probl=probl,
   965 	  meth= x,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
   966   | repl_met _ _ = raise PTREE "repl_pbl takes no PrfObj";
   967 
   968 fun repl_spec  x    (PblObj {cell=cell,origin=origin,fmz=fmz,
   969 			   spec= _,probl=probl,meth=meth,env=env,loc=loc,
   970 			   branch=branch,result=result,ostate=ostate}) =
   971   PblObj {cell=cell,origin=origin,fmz=fmz,spec= x,probl=probl,
   972 	  meth=meth,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
   973   | repl_spec  _ _ = raise PTREE "repl_domID takes no PrfObj";
   974 fun repl_domID x    (PblObj {cell=cell,origin=origin,fmz=fmz,
   975 			   spec=(_,p,m),probl=probl,meth=meth,env=env,loc=loc,
   976 			   branch=branch,result=result,ostate=ostate}) =
   977   PblObj {cell=cell,origin=origin,fmz=fmz,spec=(x,p,m),probl=probl,
   978 	  meth=meth,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
   979   | repl_domID _ _ = raise PTREE "repl_domID takes no PrfObj";
   980 fun repl_pblID x    (PblObj {cell=cell,origin=origin,fmz=fmz,
   981 			   spec=(d,_,m),probl=probl,meth=meth,env=env,loc=loc,
   982 			   branch=branch,result=result,ostate=ostate}) =
   983   PblObj {cell=cell,origin=origin,fmz=fmz,spec=(d,x,m),probl=probl,
   984 	  meth=meth,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
   985   | repl_pblID _ _ = raise PTREE "repl_pblID takes no PrfObj";
   986 fun repl_metID x (PblObj {cell=cell,origin=origin,fmz=fmz,
   987 			   spec=(d,p,_),probl=probl,meth=meth,env=env,loc=loc,
   988 			   branch=branch,result=result,ostate=ostate}) =
   989   PblObj {cell=cell,origin=origin,fmz=fmz,spec=(d,p,x),probl=probl,
   990 	  meth=meth,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
   991   | repl_metID _ _ = raise PTREE "repl_metID takes no PrfObj";
   992 
   993 fun repl_result l f' s (PrfObj {cell=cell,form=form,tac=tac,loc=_,
   994 			     branch=branch,result = _ ,ostate = _}) =
   995     PrfObj {cell=cell,form=form,tac=tac,loc= l,
   996 	    branch=branch,result = f',ostate = s}
   997   | repl_result l f' s (PblObj {cell=cell,origin=origin,fmz=fmz,
   998 			     spec=spec,probl=probl,meth=meth,env=env,loc=_,
   999 			     branch=branch,result= _ ,ostate= _}) =
  1000     PblObj {cell=cell,origin=origin,fmz=fmz,
  1001 	    spec=spec,probl=probl,meth=meth,env=env,loc= l,
  1002 	    branch=branch,result= f',ostate= s};
  1003 
  1004 fun repl_tac x (PrfObj {cell=cell,form=form,tac= _,loc=loc,
  1005 			  branch=branch,result=result,ostate=ostate}) =
  1006     PrfObj {cell=cell,form=form,tac= x,loc=loc,
  1007 	    branch=branch,result=result,ostate=ostate}
  1008   | repl_tac _ _ = raise PTREE "repl_tac takes no PblObj";
  1009 
  1010 fun repl_branch b (PblObj {cell=cell,origin=origin,fmz=fmz,
  1011 			   spec=spec,probl=probl,meth=meth,env=env,loc=loc,
  1012 			   branch= _,result=result,ostate=ostate}) =
  1013   PblObj {cell=cell,origin=origin,fmz=fmz,spec=spec,probl=probl,
  1014 	  meth=meth,env=env,loc=loc,branch= b,result=result,ostate=ostate}
  1015   | repl_branch b (PrfObj {cell=cell,form=form,tac=tac,loc=loc,
  1016 			  branch= _,result=result,ostate=ostate}) =
  1017     PrfObj {cell=cell,form=form,tac=tac,loc=loc,
  1018 	    branch= b,result=result,ostate=ostate};
  1019 
  1020 fun repl_args args
  1021   (PblObj {cell=cell,origin=origin,fmz=fmz,
  1022 	   spec=spec,probl=probl,meth=meth,env=(_,tac,ets,asm),loc=loc,
  1023 	   branch=branch,result=result,ostate=ostate}) =
  1024   PblObj {cell=cell,origin=origin,fmz=fmz,spec=spec,probl=probl,
  1025 	  meth=meth,env=(args,tac,ets,asm),loc=loc,branch=branch,
  1026 	  result=result,ostate=ostate}
  1027   | repl_args _ _ = raise PTREE "repl_args takes no PrfObj";
  1028 fun repl_tacs erul
  1029   (PblObj {cell=cell,origin=origin,fmz=fmz,
  1030 	   spec=spec,probl=probl,meth=meth,env=(arg,_,ets,asm),loc=loc,
  1031 	   branch=branch,result=result,ostate=ostate}) =
  1032   PblObj {cell=cell,origin=origin,fmz=fmz,spec=spec,probl=probl,
  1033 	  meth=meth,env=(arg,erul,ets,asm),loc=loc,branch=branch,
  1034 	  result=result,ostate=ostate}
  1035   | repl_tacs _ _ = raise PTREE "repl_tacs takes no PrfObj";
  1036 fun repl_ets ets
  1037   (PblObj {cell=cell,origin=origin,fmz=fmz,
  1038 	   spec=spec,probl=probl,meth=meth,env=(arg,tac,_,asm),loc=loc,
  1039 	   branch=branch,result=result,ostate=ostate}) =
  1040   PblObj {cell=cell,origin=origin,fmz=fmz,spec=spec,probl=probl,
  1041 	  meth=meth,env=(arg,tac,ets,asm),loc=loc,branch=branch,
  1042 	  result=result,ostate=ostate}
  1043   | repl_ets _ _ = raise PTREE "repl_ets takes no PrfObj";
  1044 
  1045 fun repl_oris oris
  1046   (PblObj {cell=cell,origin=(_,spe,hdf),fmz=fmz,
  1047 	   spec=spec,probl=probl,meth=meth,env=env,loc=loc,
  1048 	   branch=branch,result=result,ostate=ostate}) =
  1049   PblObj{cell=cell,origin=(oris,spe,hdf),fmz=fmz,spec=spec,probl=probl,
  1050 	  meth=meth,env=env,loc=loc,branch=branch,
  1051 	  result=result,ostate=ostate}
  1052   | repl_oris _ _ = raise PTREE "repl_oris takes no PrfObj";
  1053 fun repl_orispec spe
  1054   (PblObj {cell=cell,origin=(oris,_,hdf),fmz=fmz,
  1055 	   spec=spec,probl=probl,meth=meth,env=env,loc=loc,
  1056 	   branch=branch,result=result,ostate=ostate}) =
  1057   PblObj{cell=cell,origin=(oris,spe,hdf),fmz=fmz,spec=spec,probl=probl,
  1058 	  meth=meth,env=env,loc=loc,branch=branch,
  1059 	  result=result,ostate=ostate}
  1060   | repl_orispec _ _ = raise PTREE "repl_orispec takes no PrfObj";
  1061 
  1062 fun repl_loc l (PblObj {cell=cell,origin=origin,fmz=fmz,
  1063 			spec=spec,probl=probl,meth=meth,env=env,loc=_,
  1064 			branch=branch,result=result,ostate=ostate}) =
  1065   PblObj {cell=cell,origin=origin,fmz=fmz,spec=spec,probl=probl,
  1066 	  meth=meth,env=env,loc=l,branch=branch,result=result,ostate=ostate}
  1067   | repl_loc l (PrfObj {cell=cell,form=form,tac=tac,loc=_,
  1068 			branch=branch,result=result,ostate=ostate}) =
  1069   PrfObj {cell=cell,form=form,tac=tac,loc= l,
  1070 	  branch=branch,result=result,ostate=ostate};
  1071 
  1072 fun uni__asm asm' 
  1073   (PblObj {cell=cell,origin=origin,fmz=fmz,
  1074 	   spec=spec,probl=probl,meth=meth,env=(arg,tac,ets,asm),loc=loc,
  1075 	   branch=branch,result=result,ostate=ostate}) =
  1076   PblObj {cell=cell,origin=origin,fmz=fmz,spec=spec,probl=probl,
  1077 	  meth=meth,env=(arg,tac,ets,asm union asm'),loc=loc,branch=branch,
  1078 	  result=result,ostate=ostate}
  1079   | uni__asm _ _ = raise PTREE "uni__asm takes no PrfObj";
  1080 fun uni__cid cell' 
  1081   (PblObj {cell=cell,origin=origin,fmz=fmz,
  1082 	   spec=spec,probl=probl,meth=meth,env=env,loc=loc,
  1083 	   branch=branch,result=result,ostate=ostate}) =
  1084   PblObj {cell=cell union cell',origin=origin,fmz=fmz,spec=spec,probl=probl,
  1085 	  meth=meth,env=env,loc=loc,branch=branch,
  1086 	  result=result,ostate=ostate}
  1087   | uni__cid cell'
  1088   (PrfObj {cell=cell,form=form,tac=tac,loc=loc,
  1089 	   branch=branch,result=result,ostate=ostate}) =
  1090   PrfObj {cell=cell union cell',form=form,tac=tac,loc=loc,
  1091 	  branch=branch,result=result,ostate=ostate};
  1092 
  1093   
  1094 
  1095 
  1096 (** applies (snd f) to 1 level of branches if ((fst f) b),
  1097     f : (ppobj -> bool) * (int -> ptree list -> ptree list) **)
  1098 
  1099 fun appl_branch f EmptyPtree [] = (EmptyPtree, false)
  1100   | appl_branch f EmptyPtree _  = raise PTREE "appl_branch f Empty _"
  1101   | appl_branch f (Nd ( _, _)) [] = raise PTREE "appl_branch f _ []"
  1102   | appl_branch f (Nd (b, bs)) (p::[]) = 
  1103     if (fst f) b then (Nd (b, (snd f) (p:posel) bs), true)
  1104     else (Nd (b, bs), false)
  1105   | appl_branch f (Nd (b, bs)) (p::ps) =
  1106 	let val (b',bool) = appl_branch f (nth p bs) ps
  1107 	in (Nd (b, repl_app bs p b'), bool) end;
  1108 
  1109 (* expl for f as used with appl_branch *)
  1110 fun test_trans (PrfObj{branch = Transitive,...}) = true
  1111   | test_trans (PblObj{branch = Transitive,...}) = true
  1112   | test_trans _ = false;
  1113 (*
  1114 val cut_branch = (test_trans, curry take):
  1115     (ppobj -> bool) * (int -> ptree list -> ptree list);
  1116 .. formlery used for ...
  1117 fun cut_tree _ [] = EmptyPtree
  1118   | cut_tree pt pos = 
  1119   let val (pt',cut) = appl_branch cut_branch pt pos
  1120   in if cut andalso length pos > 1 then cut_tree pt' (lev_up pos)
  1121      else pt' end;
  1122 *)
  1123 
  1124 (* specialized appl_branch for cut + return cuts *)
  1125 fun appl_cut EmptyPtree _  = raise PTREE "appl_cut Empty _"
  1126   | appl_cut (Nd ( _, _)) [] = raise PTREE "appl_cut _ []"
  1127   | appl_cut (Nd (b, bs)) (p::[]) = 
  1128   if test_trans b 
  1129     then (Nd (b, take (p:posel, bs)), true,
  1130 	  (flat o (map (get_all g_cell))) (takerest (p,bs)))
  1131   else (Nd (b, bs), false, [])
  1132   | appl_cut (Nd (b, bs)) (p::ps) =
  1133     let val (b',bool,cuts) = appl_cut (nth p bs) ps
  1134     in (Nd (b, repl_app bs p b'), bool, cuts) end;
  1135 
  1136 
  1137 (* cut objs below and after a position as long as Transitive 
  1138    FIXME?: whole ptree copied for each branch-level cut  
  1139    FIXME!: should return pos list instead cellID list*)
  1140 fun cut_tree _ ([]:pos) = raise PTREE "cut_tree _ []"
  1141   | cut_tree pt pos =
  1142   let
  1143     fun cutfn pt cuts pos = 
  1144       let val (pt', cut, cuts') = appl_cut pt pos;
  1145 (*	val _ = writeln (foldr (op^) (cuts',""))     *)
  1146       in if cut andalso length pos > 1 
  1147 	   then cutfn pt' (cuts @ cuts') (lev_up pos)
  1148 	 else (pt',cuts @ cuts') end
  1149   in (apsnd flat (cutfn pt [] pos)):ptree * cid end;
  1150 (* pt of max_on_surface
  1151 > writeln (pr_ptree pr_cell pt);
  1152 > val (pt',cutl) = cut_tree pt [4,1,1,1,1];
  1153 val cutl = ["[4,1,1,1,2]:","[4,1,1,1,3]:"] : cid list
  1154 > writeln (pr_ptree pr_cell pt');
  1155 
  1156 > val (pt'',cutl) = cut_tree pt [4,1];
  1157 val cutl = [] : cid list
  1158 > writeln (pr_ptree pr_cell pt'');
  1159 
  1160 > val (pt''',cutl) = cut_tree pt [1,1];
  1161 val cutl =
  1162   ["[1,2]:","[2]:","[3]:","[3,1]:","[3,2]:","[4]:","[4,1]:","[4,1,1]:",...]
  1163   : cid list
  1164 > writeln (pr_ptree pr_cell pt''');
  1165 *)
  1166 
  1167 
  1168 
  1169 fun append_atomic p l f r f' s pt = 
  1170   let 
  1171       val (iss, f) = if existpt p pt andalso get_obj g_tac pt p=Empty_Tac
  1172 		     then (*after Take*)
  1173 			 ((fst (get_obj g_loc pt p), Some l), 
  1174 			  get_obj g_form pt p) 
  1175 		     else ((Some l, None), f)
  1176   in insert (PrfObj {cell = [(*3.00. unused*)],
  1177 		     form  = f,
  1178 		     tac  = r,
  1179 		     loc   = iss,
  1180 		     branch= NoBranch,
  1181 		     result= f',
  1182 		     ostate= s}) pt p end;
  1183 
  1184 (*20.8.02: cappend_* FIXXXXME cut branches below cannot be decided here:
  1185   detail - generate - cappend: inserted, not appended !!!
  1186 
  1187   cut decided in applicable_in !!!
  1188 *)
  1189 fun cappend_atomic pt p loc f r f' s = 
  1190   apfst (append_atomic p loc f r f' s) (cut_tree pt p);
  1191 
  1192 (* called by Take *)
  1193 fun append_form p l f pt = 
  1194   insert (PrfObj {cell = [(*3.00. unused*)],
  1195 		  form  = (*if existpt p pt 
  1196 		  andalso get_obj g_tac pt p = Empty_Tac 
  1197 			    (*distinction from 'old' (+complete!) pobjs*)
  1198 			    then get_obj g_form pt p else*) f,
  1199 		  tac  = Empty_Tac,
  1200 		  loc   = (Some l, None),
  1201 		  branch= NoBranch,
  1202 		  result= (e_term,[]),
  1203 		  ostate= Incomplete}) pt p;
  1204 fun cappend_form pt p loc f =
  1205   apfst (append_form p loc f) (cut_tree pt p);
  1206 
  1207 
  1208     
  1209 fun append_result pt p l f s =
  1210     (appl_obj (repl_result (fst (get_obj g_loc pt p),
  1211 			    (Some l)) f s) pt p, []);
  1212 
  1213 
  1214 
  1215 fun append_parent p l f r b pt = 
  1216   let 
  1217     val (ll,f) = if existpt p pt andalso get_obj g_tac pt p=Empty_Tac
  1218 		  then ((fst (get_obj g_loc pt p), Some l), 
  1219 			get_obj g_form pt p) 
  1220 		 else ((Some l, None), f)
  1221   in insert (PrfObj 
  1222 	  {cell = [(*unused*)],
  1223 	   form  = f,
  1224 	   tac  = r,
  1225 	   loc   = ll,
  1226 	   branch= b,
  1227 	   result= (e_term,[]),
  1228 	   ostate= Incomplete}) pt p end;
  1229 fun cappend_parent pt p loc f r b =
  1230   apfst (append_parent p loc f r b) (cut_tree pt p);
  1231 
  1232 (*13.8.02 deleted again --- istate option
  1233 fun init_ptree (strs,spec) =
  1234   (Nd (PblObj 
  1235 	       {cell  = [(*3.00. unused*)],
  1236 		origin= (strs,spec),
  1237 		fmz = empty_ppc_ct',
  1238 		spec  = empty_spec,
  1239 		probl = []:itm list,
  1240 		meth  = []:itm list,
  1241 		env   = empty_envp,
  1242 		loc   = (None, None),
  1243 		branch= NoBranch,
  1244 		result= empty_cterm',
  1245 		ostate= Incomplete},[]));----------------*)
  1246 
  1247 fun append_problem [] l fmz (strs,spec,hdf) _ =
  1248   (Nd (PblObj 
  1249 	       {cell  = [(*3.00. unused*)],
  1250 		origin= (strs,spec,hdf),
  1251 		fmz   = fmz,
  1252 		spec  = empty_spec,
  1253 		probl = []:itm list,
  1254 		meth  = []:itm list,
  1255 		env   = empty_envp,
  1256 		loc   = (Some l, None),
  1257 		branch= TransitiveB,(*FIXXXXXME.27.8.03: for equations only*)
  1258 		result= (e_term,[]),
  1259 		ostate= Incomplete},[]))
  1260   | append_problem p l fmz (strs,spec,hdf) pt =
  1261   insert (PblObj 
  1262 	  {cell  = [(*3.00. unused*)],
  1263 	   origin= (strs,spec,hdf),
  1264 	   fmz   = fmz,
  1265 	   spec  = empty_spec,
  1266 	   probl = []:itm list,
  1267 	   meth  = []:itm list,
  1268 	   env   = empty_envp,
  1269 	   loc   = (Some l, None),
  1270 	   branch= TransitiveB,
  1271 	   result= (e_term,[]),
  1272 	   ostate= Incomplete}) pt p;
  1273 fun cappend_problem _ [] loc fmz ori =
  1274   (append_problem [] loc fmz ori EmptyPtree,[])
  1275   | cappend_problem pt p loc fmz ori = 
  1276   apfst (append_problem p (loc:istate) fmz ori) (cut_tree pt p);
  1277 
  1278 
  1279 (* use"ME/sequent.sml";
  1280    use"sequent.sml";
  1281    *)
  1282 
  1283 
  1284 
  1285 (*
  1286 fun update_fmz  pt pos x = appl_obj (repl_fmz  x) pt pos;
  1287                                        1.00 not used anymore*)
  1288 fun update_args   pt pos x = appl_obj (repl_args   x) pt pos;
  1289 fun update_tacs   pt pos x = appl_obj (repl_tacs   x) pt pos;
  1290 fun update_ets    pt pos x = appl_obj (repl_ets    x) pt pos;
  1291 fun update_domID  pt pos x = appl_obj (repl_domID  x) pt pos;
  1292 fun update_pblID  pt pos x = appl_obj (repl_pblID  x) pt pos;
  1293 fun update_metID  pt pos x = appl_obj (repl_metID  x) pt pos;
  1294 fun update_spec   pt pos x = appl_obj (repl_spec   x) pt pos;
  1295 
  1296 fun update_pbl    pt pos x = appl_obj (repl_pbl    x) pt pos;
  1297 fun update_pblppc pt pos x = appl_obj (repl_pbl    x) pt pos;
  1298 
  1299 fun update_met    pt pos x = appl_obj (repl_met    x) pt pos;
  1300 (*1.09.01 ----
  1301 fun update_metppc pt pos x = 
  1302   let val {rew_ord'=od,rls'=rs,asm_thm=at,asm_rls=ar,...} =
  1303     get_obj g_met pt pos
  1304   in appl_obj (repl_met 
  1305      {rew_ord'=od,rls'=rs,asm_thm=at,asm_rls=ar,ppc=x}) 
  1306     pt pos end;*)
  1307 fun update_metppc pt pos x = appl_obj (repl_met    x) pt pos;
  1308 			 			   
  1309 fun union_asm     pt pos x = appl_obj (uni__asm    x) pt pos; 
  1310 fun union_cid     pt pos x = appl_obj (uni__cid    x) pt pos;
  1311 
  1312 fun update_branch pt pos x = appl_obj (repl_branch x) pt pos;
  1313 fun update_tac  pt pos x = appl_obj (repl_tac  x) pt pos;
  1314 
  1315 fun update_oris   pt pos x = appl_obj (repl_oris   x) pt pos;
  1316 fun update_orispec   pt pos x = appl_obj (repl_orispec   x) pt pos;
  1317 
  1318  (*done by append_* !! 3.5.02*)
  1319 fun update_loc pt (p,_) (ScrState ([],[],None,
  1320 				   Const ("empty",_),Sundef,false)) = 
  1321     appl_obj (repl_loc (None,None)) pt p
  1322   | update_loc pt (p,Res) x =  
  1323     let val (lform,_) = get_obj g_loc pt p
  1324     in appl_obj (repl_loc (lform,Some x)) pt p end
  1325 
  1326   | update_loc pt (p,_) x = 
  1327     let val (_,lres) = get_obj g_loc pt p
  1328     in appl_obj (repl_loc (Some x,lres)) pt p end;
  1329 
  1330 (*13.8.02---------------------------
  1331 fun get_loc EmptyPtree _ = None
  1332   | get_loc pt (p,Res) =
  1333   let val (lfrm,lres) = get_obj g_loc pt p
  1334   in if lres = e_istate then lfrm else lres end
  1335   | get_loc pt (p,_) =
  1336   let val (lfrm,lres) = get_obj g_loc pt p
  1337   in if lfrm = e_istate then lres else lfrm end;  5.10.00: too liberal ?*)
  1338 (*13.8.02: options, because istate is no equalitype any more*)
  1339 fun get_loc EmptyPtree _ = e_istate
  1340   | get_loc pt (p,Res) =
  1341     (case get_obj g_loc pt p of
  1342 	 (Some i, None) => i
  1343        | (None  , None) => e_istate
  1344        | (_     , Some i) => i)
  1345   | get_loc pt (p,_) =
  1346     (case get_obj g_loc pt p of
  1347 	 (None  , Some i) => i (*13.8.02 just copied from ^^^: too liberal ?*)
  1348        | (None  , None) => e_istate
  1349        | (Some i, _) => i);
  1350 val get_istate = get_loc; (*3.5.02*)
  1351 
  1352 (*.collect the assumptions within a problem up to a certain position.*)
  1353 fun get_asm (b:pos, p:pos) (Nd (PblObj {result=(_,asm),...},_)) = 
  1354     ((*writeln ("### get_asm PblObj:(b,p)= "^
  1355 		(pair2str(ints2str b, ints2str p)));*)
  1356      (map (rpair b) asm))
  1357   | get_asm (b, p) (Nd (PrfObj {result=(_,asm),...}, [])) = 
  1358     ((*writeln ("### get_asm PrfObj []:(b,p)= "^
  1359 	      (pair2str(ints2str b, ints2str p)));*)
  1360      (map (rpair b) asm))
  1361   | get_asm (b, p:pos) (Nd (PrfObj _, nds)) = 
  1362     let (*val _= writeln ("### get_asm PrfObj nds:(b,p)= "^
  1363 	      (pair2str(ints2str b, ints2str p)));*)
  1364 	val levdn = 
  1365 	    if p <> [] then (b @ [hd p]:pos, tl p:pos) 
  1366 	    else (b @ [1], [99999]) (*_deeper_ nesting is always _before_ p*)
  1367     in gets_asm levdn 1 nds end
  1368 and gets_asm _ _ [] = []
  1369   | gets_asm (b, p' as p::ps) i (nd::nds) = 
  1370     if p < i then [] 
  1371     else ((*writeln ("### gets_asm: (b,p')= "^(pair2str(ints2str b,
  1372 						      ints2str p')));*)
  1373 	  (get_asm (b @ [i], ps) nd) @ (gets_asm (b, p') (i + 1) nds));
  1374 
  1375 fun get_assumptions_ (Nd (PblObj {result=(r,asm),...}, cn)) (([], _):pos') = 
  1376     if r = e_term then gets_asm ([], [99999]) 1 cn 
  1377     else map (rpair []) asm
  1378   | get_assumptions_ pt (p,p_) =
  1379     let val (cn, base) = par_children pt p
  1380 	val offset = drop (length base, p)
  1381 	val base' = replicate (length base) 1
  1382 	val offset' = case p_ of 
  1383 			 Frm => let val (qs,q) = split_last offset
  1384 				in qs @ [q - 1] end
  1385 		       | _ => offset
  1386         (*val _= writeln ("... get_assumptions: (b,o)= "^
  1387 			(pair2str(ints2str base',ints2str offset)))*)
  1388     in gets_asm (base', offset) 1 cn end;
  1389 
  1390 
  1391 (*---------
  1392 end
  1393 
  1394 open Ptree;
  1395 ----------*)
  1396 
  1397 (*pos of the formula on FE relative to the current pos,
  1398   which is the next writepos*)
  1399 fun pre_pos ([]:pos) = []:pos
  1400   | pre_pos pp =
  1401   let val (ps,p) = split_last pp
  1402   in case p of 1 => ps | n => ps @ [n-1] end;
  1403 
  1404 (*WN.20.5.03 ... but not used*)
  1405 fun posless [] (_::_) = true
  1406   | posless (_::_) [] = false
  1407   | posless (p::ps) (q::qs) = if p = q then posless ps qs else p < q;
  1408 (* posless [2,3,4] [3,4,5];
  1409 true
  1410 >  posless [2,3,4] [1,2,3];
  1411 false
  1412 >  posless [2,3] [2,3,4];
  1413 true
  1414 >  posless [2,3,4] [2,3];
  1415 false                    
  1416 >  posless [6] [6,5,2];
  1417 true
  1418 +++ see Isabelle/../library.ML*)
  1419 
  1420 
  1421 (*development for extracting an 'interval' from ptree stopped 8.03*)
  1422 local
  1423 fun hdp [] = 1     | hdp [0] = 1     | hdp x = hd x;
  1424 fun hdq	[] = 99999 | hdq [0] = 99999 | hdq x = hd x;
  1425 
  1426 fun tlp [] = [0]     | tlp [_] = [0]     | tlp x = tl x;
  1427 fun tlq [] = [99999] | tlq [_] = [99999] | tlq x = tl x;
  1428 
  1429 fun drop_init ([]::xs) = drop_init xs
  1430   | drop_init xs = xs;
  1431 
  1432 fun get_tr i (b,p) q (Nd (po, nds)) =
  1433 
  1434     [if  i <= 0 then b else []] 
  1435     @ (writeln("get_tr: i="^(string_of_int i)^", b="^(ints2str' b)^", p="^
  1436 	       (ints2str' p)^", q="^(ints2str' q));
  1437 
  1438        gets_tr (i-1) (b@[hdp p], tlp p) (tlq q) (take_be (hdp p) (hdq q) nds))
  1439 
  1440 and gets_tr i (b,p) q [] = ((*writeln"gets_tr: -------------------------[]";*)
  1441 			    [])
  1442 
  1443   | gets_tr i (b,p) q (nd::nds) = 
  1444 
  1445     (writeln("gets_tr: i="^(string_of_int i)^", b="^(ints2str' b)^", p="^
  1446 		   (ints2str' p)^", q="^(ints2str' q));
  1447 
  1448     (get_tr i (b,p) q nd) @ (gets_tr ~99999 (lev_on b, p) q nds)
  1449     );
  1450 
  1451 fun get_trace pt p q =
  1452     (drop_init o
  1453      (gets_tr ((length p) -1) ([hdp p], tlp p) (tlq q))) 
  1454 	    (take_be (hdp p) (hdq q) (children pt));
  1455 
  1456 in
  1457 
  1458 (*.get an 'interval' from ptree down to a certain level.*)
  1459 fun get_tr f l i (b:pos, p:pos) (q:pos) (Nd (po, nds)) =
  1460     if 0 < l then 
  1461 	[if i<=0 then [(b, f po)] else [(*during initial recursive descent*)]]
  1462 	@ (gets_tr f (l-1) (i-1) (b@[hdp p]:pos, tlp p:pos) (tlq q:pos) 
  1463 		   (take_be (hdp p) (hdq q) nds))
  1464     else []
  1465 and gets_tr f l i (b,p) q [] = []
  1466   | gets_tr f l i (b,p) q (nd::nds) = 
  1467     (get_tr f l i (b,p) q nd) @ (gets_tr f l ~99999 (lev_on b, p) q nds);
  1468 (*val get_tr = 
  1469     fn : (ppobj -> 'a)      extracts info
  1470          int ->             print_depth
  1471 	 int ->             level count*~1 for initial recursive descent
  1472 	 pos * 	            current position
  1473 	 pos ->             preview to levels lower than current pos
  1474 	 pos ->             end pos (static)
  1475 	 ptree -> 
  1476   * done by recursive descent over the whole ptree.
  1477   * only the relevant nodes are taken at each level.
  1478   * heads of pos dropped at each step to lower level.
  1479   * pos going deeper than initial p q are handled by special hd tl.
  1480   *)    
  1481 fun get_trace pt (p:pos) (q:pos) f l =
  1482     (flat o drop_init o
  1483      (gets_tr f l ((length p) -1) ([hdp p], tlp p) (tlq q))) 
  1484 	(take_be (hdp p) (hdq q) (children pt));
  1485 end (*local*)
  1486 (* mit rlang.sml 55b ... End_Proof
  1487 > fun f _ = "";
  1488 > get_trace pt [4,3] [5] f 99999;
  1489 [[4,3],[4,4],[4,5],[4,5,1],[4,5,2],[4,5,3],[4,5,4],[4,5,5],[5]]
  1490 > get_trace pt [4,3] [5] f 2;
  1491 [([4,3],""),([4,4],""),([4,5],""),([5],"")]
  1492 
  1493 > get_trace pt [4,3] [4,5,1] f 99999;
  1494 [[4,3],[4,4],[4,5],[4,5,1]]
  1495 
  1496 > get_trace pt [] [4,5,1] f 99999;
  1497 [[1],[2],[3],[4],[4,1],[4,2],[4,3],[4,4],[4,5],[4,5,1]]
  1498 
  1499 > get_trace pt [] [] f 99999;
  1500 [[1],[2],[3],[4],[4,1],[4,2],[4,3],[4,4],[4,5],[4,5,1],[4,5,2],[4,5,3],
  1501  [4,5,4],[4,5,5],[5]]
  1502 > get_trace pt [] [] f 2;
  1503 [[1],[2],[3],[4],[4,1],[4,2],[4,3],[4,4],[4,5],[5]]
  1504 
  1505 > get_trace pt [4,5,5] [] f 99999;
  1506 [([4,5,5],""),([5],"")]
  1507 
  1508 > get_trace pt [4,5,3] [4,5,3] f 99999;
  1509 [([4,5,3],"")]
  1510 *)
  1511 
  1512 fun get_somespec ((dI,pI,mI):spec) ((dI',pI',mI'):spec) =
  1513     let val domID = if dI = e_domID
  1514 		    then if dI' = e_domID 
  1515 			 then raise error"pt_extract: no domID in probl,origin"
  1516 			 else dI'
  1517 		    else dI
  1518 	val pblID = if pI = e_pblID
  1519 		    then if pI' = e_pblID 
  1520 			 then raise error"pt_extract: no pblID in probl,origin"
  1521 			 else pI'
  1522 		    else pI
  1523 	val metID = if mI = e_metID
  1524 		    then if pI' = e_metID 
  1525 			 then raise error"pt_extract: no metID in probl,origin"
  1526 			 else mI'
  1527 		    else mI
  1528     in (domID, pblID, metID):spec end;
  1529 
  1530 
  1531 
  1532 
  1533 (*extract a formula or model from ptree for itms2itemppc or model2xml*)
  1534 type ocalhd = 
  1535 	 pos_ *                (*model belongs to Problem | Method*)
  1536 	 term *                (*header: Problem... or Cas*)       
  1537 	 itm list *            (*model: given, find, relate*)
  1538 	 ((bool * term) list) *(*model: preconds*)
  1539 	 spec;                 (*specification*)
  1540 val e_ocalhd = (Und, e_term, [e_itm], [(false, e_term)], e_spec);
  1541 fun preconds2str bts = 
  1542     (strs2str o (map (linefeed o pair2str o
  1543 		      (apsnd term2str) o 
  1544 		      (apfst bool2str)))) bts;
  1545 fun ocalhd2str ((p, hdf, itms, prec, spec):ocalhd) =
  1546     "("^pos_2str p^", "^term2str hdf^", "^itms2str (assoc_thy "Isac.thy") itms^
  1547     ", "^preconds2str prec^", \n"^spec2str spec^" )";
  1548 
  1549 datatype ptform =
  1550 	 Form of term
  1551        | ModSpec of ocalhd;
  1552 val e_ptform = Form e_term;
  1553 val e_ptform' = ModSpec e_ocalhd;
  1554 
  1555 fun pt_model (PblObj {meth,spec,origin=(_,spec',_),...}) Met =
  1556     let val (_,_,metID) = get_somespec spec spec'
  1557 	val {prls,pre=where_,...} = get_met metID
  1558 	val (head, pre) = head_precond spec' None prls where_ meth 0
  1559     in ModSpec (Met, head, meth, pre, spec) end
  1560   | pt_model (PblObj {probl,spec,origin=(_,spec',_),...}) _(*Frm,Pbl*) =
  1561     let val (_,pblID,_) = get_somespec spec spec'
  1562 	val {prls,where_,cas,...} = get_pbt pblID
  1563 	val (head, pre) = head_precond spec' cas prls where_ probl 0
  1564     in ModSpec (Pbl, head, probl, pre, spec) end;
  1565 
  1566 
  1567 fun pt_form (PrfObj {form,...}) = Form form
  1568   | pt_form (PblObj {probl,spec,origin=(_,spec',_),...}) =
  1569     let val (dI, pI, _) = get_somespec spec spec'
  1570 	val {cas,...} = get_pbt pI
  1571     in case cas of
  1572 	   None => Form (pblterm dI pI)
  1573 	 | Some t => Form (subst_atomic (mk_env probl) t)
  1574     end;
  1575 
  1576 fun pt_result (PrfObj {result=(t,_),...}) = Form t
  1577   | pt_result (PblObj {result=(t,_),...}) = Form t;
  1578 
  1579 type asms = (term * pos) list;
  1580 
  1581 (*fun pt_extract (pt,(p,Frm):pos') = 
  1582     let val f = get_obj pt_form pt p
  1583     in (f, Tac"", []:asms, Sundef) end
  1584   | pt_extract (pt,(p,Res)) =
  1585     let val f = get_obj pt_result pt p
  1586     in (f, Tac"", []:asms, Sundef) end
  1587   | pt_extract (pt,(p,p_)) =
  1588     let val ppobj = get_obj I pt p
  1589 	val f = if is_pblobj ppobj then pt_model ppobj p_
  1590 		else raise PTREE ("pt_extrac: tries to fetch modspec from "^
  1591 				  pos'2str (p,p_))
  1592     in (f, Tac"", []:asms, Sundef) end;*)
  1593 (* val (pt,(p,p_)) = (pt, ip);
  1594    *)
  1595 fun pt_extract (pt,(p,Res)) =
  1596     let val f = get_obj pt_result pt p
  1597     in (f, Tac"", []:asms, Sundef) end
  1598   | pt_extract (pt,(p,p_)) =
  1599     let val ppobj = get_obj I pt p
  1600 	val f = if is_pblobj ppobj then pt_model ppobj p_
  1601 		else get_obj pt_form pt p
  1602     in (f, Tac"", []:asms, Sundef) end;
  1603 
  1604 
  1605 
  1606 
  1607 fun is_pblnd (Nd (ppobj, _)) = is_pblobj ppobj;
  1608 
  1609 
  1610 (**.functions for the 'ptree iterator' as seen from the FE-Kernel interface.**)
  1611 
  1612 (*.move one step down into existing nodes of ptree; regard TransitiveB.*)
  1613 fun move_dn _ (Nd (c, ns)) ([],p_) = (*root problem*)
  1614 (* val (Nd (c, ns), ([],p_)) = (pt, get_pos cI uI);
  1615    *)
  1616     if is_pblobj c 
  1617     then case p_ of Frm => ([], Pbl) 
  1618 		  | Res => raise PTREE "end of calculation"
  1619 		  | _ => if null ns (*go down from Pbl + Met*)
  1620 			 then raise PTREE "solve problem not started"
  1621 			 else ([1], Frm)
  1622     else raise PTREE "pos not existent 1"
  1623 
  1624   (*iterate towards end of pos*)
  1625   | move_dn P  (Nd (_, ns)) (p::(ps as (_::_)),p_) =
  1626     if p > length ns then raise PTREE "pos not existent 2"
  1627     else move_dn ((P@[p]): pos) (nth p ns) (ps, p_)
  1628 
  1629   | move_dn P (Nd (c, ns)) ([p], p_) = (*act on last element of pos*)
  1630     if p > length ns then raise PTREE "pos not existent 3"
  1631     else if is_pblnd (nth p ns)  then
  1632 	(writeln("### move_dn: is_pblnd (nth p ns), P= "^ints2str' P^", \n"^
  1633 		 "length ns= "^((string_of_int o length) ns)^
  1634 		 ", p= "^string_of_int p^", p_= "^pos_2str p_);
  1635 	 case p_ of Frm => ((P@[p], Pbl) : pos')
  1636 		 | Res => if p = length ns 
  1637 			  then 
  1638 			      if g_ostate c = Complete then (P, Res)
  1639 			      else raise PTREE "parent not complete"
  1640 			  (*FIXME here handle not-sequential branches*)
  1641 			  else 
  1642 			      if g_branch c = TransitiveB 
  1643 				 andalso (not o is_pblnd o (nth (p+1))) ns
  1644 			      then (P@[p+1], Res) else (P@[p+1], Frm)
  1645 		 | _ => if (null o children o (nth p)) ns (*go down from Pbl*)
  1646 			then raise PTREE "solve subproblem not started"
  1647 			else (P @ [p, 1], Frm)
  1648 			     )
  1649     else case p_ of Frm => if (null o children o (nth p)) ns then (P@[p],Res) 
  1650 			   else (P @ [p, 1], Frm) (*go down*)
  1651 		  | Res => if p = length ns 
  1652 			   then 
  1653 			      if g_ostate c = Complete then (P, Res)
  1654 			      else raise PTREE "parent not complete"
  1655 			   else 
  1656 			       if g_branch c = TransitiveB 
  1657 				  andalso (not o is_pblnd o (nth (p+1))) ns
  1658 			       then (P@[p+1], Res) else (P@[p+1], Frm);
  1659 
  1660 (*.go one level down into ptree; goes to Frm.*)
  1661 fun movelevel_dn [] (Nd (c, ns)) ([],p_) = (*root problem*)
  1662     if is_pblobj c 
  1663     then if null ns 
  1664 	 then raise PTREE "solve problem not started"
  1665 	 else ([1], Frm)
  1666     else raise PTREE "pos not existent 1"
  1667 
  1668   (*iterate towards end of pos*)
  1669   | movelevel_dn P (Nd (_, ns)) (p::(ps as (_::_)),p_) =
  1670     if p > length ns then raise PTREE "pos not existent 2"
  1671     else movelevel_dn (P@[p]) (nth p ns) (ps, p_)
  1672 
  1673   | movelevel_dn P (Nd (c, ns)) ([p], p_) = (*act on last element of pos*)
  1674     if p > length ns then raise PTREE "pos not existent 3"
  1675     else case p_ of Frm => if (null o children o (nth p)) ns 
  1676 			   then raise PTREE "no children"
  1677 			   else (P @ [p, 1], Frm) (*go down*)
  1678 		  | Res => if p = length ns 
  1679 			   then raise PTREE "no children"
  1680 			   else 
  1681 			       if g_branch c = TransitiveB
  1682 			       then if (null o children o (nth (p+1))) ns
  1683 				    then raise PTREE "no children"
  1684 				    else (P@[p+1, 1], Frm)
  1685 			       else if (null o children o (nth p)) ns
  1686 				    then raise PTREE "no children"
  1687 				    else (P@[p, 1], Frm);
  1688 
  1689 (*.go to the previous position in ptree; regard TransitiveB.*)
  1690 fun move_up _ (Nd (c, ns)) (([],p_):pos') = (*root problem*)
  1691     if is_pblobj c 
  1692     then case p_ of Frm => raise PTREE "begin of calculation"
  1693 		  | Pbl => ([], Frm) | Met => ([], Pbl)
  1694 		  | Res => if null ns then ([], Pbl) (*Res -> Pbl (not Met)!*)
  1695 			   else ([length ns], Res)
  1696     else raise PTREE "pos not existent"
  1697 
  1698   | move_up P  (Nd (_, ns)) (p::(ps as (_::_)),p_) = (*iterate to end of pos*)
  1699     if p > length ns then raise PTREE "pos not existent"
  1700     else move_up (P@[p]) (nth p ns) (ps,p_)
  1701 
  1702   | move_up P (Nd (c, ns)) ([p], p_) = (*act on last element of pos*)
  1703     if p > length ns then raise PTREE "pos not existent"
  1704     else if is_pblnd (nth p ns)  then
  1705 	case p_ of Frm => if p = 1 then (P, Pbl) else (P@[p-1], Res)
  1706 		  | Pbl => (P@[p], Frm) | Met => (P@[p], Pbl)
  1707 		  | Res => 
  1708 		    let val nc = (length o children o (nth p)) ns
  1709 		    in if nc = 0 then (P@[p], Pbl) (*Res -> Pbl (not Met)!*)
  1710 		       else (P @ [p, nc], Res) end (*go down*)
  1711     else case p_ of Frm => if p <> 1 then (P, Frm) 
  1712 			  else if is_pblobj c then (P, Pbl) else (P, Frm)
  1713 		  | Res => 
  1714 		    let val nc = (length o children o (nth p)) ns
  1715 		    in if nc = 0 (*cannot go down*)
  1716 		       then if g_branch c = TransitiveB andalso p <> 1
  1717 			    then (P@[p-1], Res) else (P@[p], Frm)
  1718 		       else (P @ [p, nc], Res) end; (*go down*)
  1719 
  1720 (*.go one level up in ptree; sets the position on Frm.*)
  1721 fun movelevel_up _ (Nd (c, ns)) (([],p_):pos') = (*root problem*)
  1722     raise PTREE "pos not existent"
  1723 
  1724   (*iterate towards end of pos*)
  1725   | movelevel_up P  (Nd (_, ns)) (p::(ps as (_::_)),p_) = 
  1726     if p > length ns then raise PTREE "pos not existent"
  1727     else movelevel_up (P@[p]) (nth p ns) (ps,p_)
  1728 
  1729   | movelevel_up P (Nd (c, ns)) ([p], p_) = (*act on last element of pos*)
  1730     if p > length ns then raise PTREE "pos not existent"
  1731     else if is_pblobj c then (P, Pbl) else (P, Frm);
  1732 
  1733 
  1734 (*.go to the next calc-head up in the calc-tree.*)
  1735 fun movecalchd_up pt ((p, Res):pos') =
  1736     (par_pblobj pt p, Pbl):pos'
  1737   | movecalchd_up pt (p, _) =
  1738     if is_pblobj (get_obj I pt p) 
  1739     then (p, Pbl) else (par_pblobj pt p, Pbl);
  1740  
  1741 
  1742 
  1743