src/sml/ME/ctree.sml
author wneuper
Fri, 24 Oct 2003 18:20:30 +0200
changeset 1050 20009d5841dc
parent 969 734b78b26e1f
child 1104 4aadb68cef6f
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 
   441 
   442 (*tac_ is made from tac in applicable_in,
   443   and carries all data necessary for generate;*)
   444 datatype tac_ = 
   445 (* datatype tac = *)
   446   Init_Proof' of ((cterm' list) * spec)
   447                 (* ori list !: code specify -> applicable*)
   448 | Model_Problem' of pblID * 
   449 		    itm list   (*the 'untouched' pbl*)
   450 | Refine_Tacitly' of pblID *    (*input*)
   451 		     pblID *    (*the refined from applicable_in*)
   452 		     domID *    (*from new pbt?! filled in specify*)
   453 		     metID *    (*from new pbt?! filled in specify*)
   454 		     itm list   (*drop ! 9.03: remains [] for
   455                                   Model_Problem recognizing its activation*)
   456 | Refine_Problem' of (pblID * (itm list * (bool * Term.term) list))
   457 | Add_Given'    of cterm' *
   458 		   itm list (*updated with input in fun specify_additem*)
   459 | Add_Find'     of cterm' *
   460 		   itm list (*updated with input in fun specify_additem*)
   461 | Add_Relation' of cterm' *
   462 		 itm list (*updated with input in fun specify_additem*)
   463 | Del_Given' of cterm'   | Del_Find' of cterm'   | Del_Relation' of cterm'
   464   (*4.00.: all..    term: in applicable_in ..? Syn ?only for FormFK?*)
   465 
   466 | Specify_Theory' of domID              
   467 | Specify_Problem' of (pblID *        (*               *)
   468 		       (bool *        (* matches	     *)
   469 			(itm list *   (* ppc	     *)
   470 			 (bool * term) list))) (* preconditions *)
   471 | Specify_Method' of metID *
   472 		     ori list * (*repl. "#undef"*)
   473 		     itm list   (*... updated from pbl to met*)
   474 | Apply_Method' of metID *
   475 		   (term * istate)
   476 		       option (*only for the init_form*)
   477 | Check_Postcond' of 
   478   pblID * 
   479   (term *      (*returnvalue of script in solve*)
   480    cterm' list)(*collect by get_assumptions_ in applicable_in, except if 
   481                  butlast tac is Check_elementwise: take only these asms*)
   482 | Free_Solve'
   483 
   484 | Rewrite_Inst' of theory' * rew_ord' * rls
   485   * bool * subst * thm' * term * (term  * term list)
   486                     (*... form * (result* asumpts  ), saves time*)
   487 | Rewrite' of theory' * rew_ord' * rls * bool * thm' * 
   488   term * (term * term list)
   489 | Rewrite_Asm' of theory' * rew_ord' * rls * bool * thm' * 
   490   term * (term * term list)
   491 | Rewrite_Set_Inst' of theory' * bool * subst * rls * 
   492   term * (term * term list)
   493 | Rewrite_Set' of theory' * bool * rls * term * (term * term list)
   494 (*Detail' in me detailed into ...*)
   495 | End_Detail' of term (*see End_Trans'*)
   496 | Detail_Set' of theory' * rls * term(*term is needed for next_tac before 2nd
   497 				     applicable_in*)
   498 | Detail_Set_Inst' of theory' * subst * rls * term
   499 | End_Ruleset' of term
   500 | Calculate' of theory' * string * term * (term * thm') 
   501 	      (*WN.29.4.03 asm?: * term list??*)
   502 | Substitute' of subst * term (*10.8.00+..*)* term              
   503 | Apply_Assumption' of term list * term
   504 
   505 | Take' of term                         | Take_Inst' of term  
   506 | Group' of (con * int list * term)
   507 | Subproblem' of (spec * 
   508 		  (ori list) * (*filled in ......*)
   509 		  term)        (*Subproblem(dom,pbl)*)  
   510 | CAScmd' of term
   511 | End_Subproblem' of term (*???*)
   512 | Split_And' of term                    | Conclude_And' of term
   513 | Split_Or' of term                     | Conclude_Or' of term
   514 | Begin_Trans' of term                  | End_Trans' of term
   515 | Begin_Sequ'                           | End_Sequ'(* substitute root.env*)
   516 | Split_Intersect' of term              | End_Intersect' of term
   517 | Check_elementwise' of (*special case:*)
   518   term *   (*(1)the current formula: [x=1,x=...]*)
   519   string * (*(2)the pred from Check_elementwise   *)
   520   (term *  (*(3)composed from (1) and (2): {x. pred}*)
   521    term list) (*20.5.03 assumptions*)
   522 
   523 | Or_to_List' of term * term            (* (a | b, [a,b]) *)
   524 | Collect_Trues' of term
   525 
   526 | Empty_Tac_                          | Tac_ of  (*for dummies*)
   527                                             theory *
   528                                             string * (*form*)
   529 					    string * (*in Tac*)
   530 					    string   (*result of Tac".."*)
   531 | User' (*internal for ets*)            | End_Proof'';(*End_Proof:inout*)
   532 (*TODO?: done partially for tests*)
   533 fun tac_2str ma = case ma of
   534     Init_Proof' (ppc, spec)  => 
   535       "Init_Proof' "^(pair2str (strs2str ppc, spec2str spec))
   536   | Model_Problem' (pblID,_)     => "Model_Problem' "^(strs2str pblID )
   537   | Refine_Tacitly'(p,prefin,domID,metID,itms)=> 
   538     "Refine_Tacitly' ("
   539     ^(strs2str p)^", "^(strs2str prefin)^", "
   540     ^domID^", "^(strs2str metID)^", pbl-itms)"
   541   | Refine_Problem' ms       => "Refine_Problem' ("^(*matchs2str ms*)"..."^")"
   542 (*| Match_Problem' (pI, (ok, (itms, pre))) => 
   543     "Match_Problem' "^(spair2str (strs2str pI,
   544 				  spair2str (bool2str ok,
   545 					     spair2str ("itms2str itms", 
   546 							"items2str pre"))))*)
   547   | Add_Given' cterm'        => "Add_Given' "(*^cterm'*)
   548   | Del_Given' cterm'        => "Del_Given' "(*^cterm'*)
   549   | Add_Find' cterm'         => "Add_Find' "(*^cterm'*)
   550   | Del_Find' cterm'         => "Del_Find' "(*^cterm'*)
   551   | Add_Relation' cterm'     => "Add_Relation' "(*^cterm'*)
   552   | Del_Relation' cterm'     => "Del_Relation' "(*^cterm'*)
   553 
   554   | Specify_Theory' domID    => "Specify_Theory' "^(quote domID    )
   555   | Specify_Problem' (pI, (ok, (itms, pre))) => 
   556     "Specify_Problem' "^(spair2str (strs2str pI,
   557 				  spair2str (bool2str ok,
   558 					     spair2str ("itms2str itms", 
   559 							"items2str pre"))))
   560   | Specify_Method' (pI,oris,itms) => 
   561     "Specify_Method' ("^metID2str pI^", "^oris2str oris^", )"
   562 
   563   | Apply_Method' (metID,_)      => "Apply_Method' "^(strs2str metID)
   564   | Check_Postcond' (pblID,(scval,asm)) => 
   565       "Check_Postcond' "^(spair2str(strs2str pblID, 
   566 				    spair2str (term2str scval, strs2str asm)))
   567 
   568   | Free_Solve'              => "Free_Solve'"
   569 
   570   | Rewrite_Inst' (*subs,thm'*) _ => 
   571       "Rewrite_Inst' "(*^(pair2str (subs2str subs, spair2str thm'))*)
   572   | Rewrite' thm'            => "Rewrite' "(*^(spair2str thm')*)
   573   | Rewrite_Asm' thm'        => "Rewrite_Asm' "(*^(spair2str thm')*)
   574   | Rewrite_Set_Inst' (*subs,thm'*) _ => 
   575       "Rewrite_Set_Inst' "(*^(pair2str (subs2str subs, quote rls))*)
   576   | Rewrite_Set'(thy',pasm,rls',f,(f',asm))          
   577     => "Rewrite_Set' ("^thy'^","^(bool2str pasm)^","^(id_rls rls')^","
   578     ^(Sign.string_of_term (sign_of thy) f)^",("^(Sign.string_of_term (sign_of thy) f')
   579     ^","^((strs2str o (map (Sign.string_of_term (sign_of thy)))) asm)^"))"
   580 
   581   | End_Detail' _             => "End_Detail' xxx"
   582   | Detail_Set' _             => "Detail_Set' xxx"
   583   | Detail_Set_Inst' _        => "Detail_Set_Inst' xxx"
   584 
   585   | Calculate'  _            => "Calculate' "
   586   | Substitute' subs         => "Substitute' "(*^(subs2str subs)*)    
   587   | Apply_Assumption' ct's   => "Apply_Assumption' "(*^(strs2str ct's)*)
   588 
   589   | Take' cterm'             => "Take' "(*^(quote cterm'	)*)
   590   | Take_Inst' cterm'        => "Take_Inst' "(*^(quote cterm' )*)
   591   | Group' (con, ints, _)     => 
   592       "Group' "^(pair2str (con2str con, ints2str ints))
   593   | Subproblem' (spec, oris, pbl_form) => 
   594       "Subproblem' "(*^(pair2str (domID, strs2str ,...))*)
   595   | End_Subproblem'  _       => "End_Subproblem'"
   596   | CAScmd' cterm'           => "CAScmd' "(*^(quote cterm')*)
   597 
   598   | Empty_Tac_             => "Empty_Tac_"
   599   | User'                    => "User'"
   600   | Tac_ (_,form,id,result) => "Tac_ (thy,"^form^","^id^","^result^")"
   601   | _                       => "";
   602 
   603 (*'executed tactics' (tac_s) with local environment etc.;
   604   used for continuing eval script + for generate*)
   605 type ets =
   606     (loc_ *      (* of tactic in scr, tactic (weakly) associated with tac_*)
   607      (tac_ * 	 (* (for generate)  *)
   608       env *      (* with 'tactic=result' as a rule, tactic ev. _not_ ready:
   609 		  for handling 'parallel let'*)
   610       env *      (* with results of (ready) tacs        *)
   611       term *     (* itr_arg of tactic, for upd. env at Repeat, Try*)
   612       term * 	 (* result value of the tac         *)
   613       safe))
   614     list;
   615 val Ets = []:ets;
   616 
   617 
   618 fun ets2s (l,(m,eno,env,iar,res,s)) = 
   619   "\n("^(loc_2str l)^",("^(tac_2str m)^
   620   ",\n  ens= "^(subst2str eno)^
   621   ",\n  env= "^(subst2str env)^
   622   ",\n  iar= "^(Sign.string_of_term (sign_of thy) iar)^
   623   ",\n  res= "^(Sign.string_of_term (sign_of thy) res)^
   624   ",\n  "^(safe2str s)^"))";
   625 fun ets2str (ets:ets) = (strs2str o (map ets2s)) ets;
   626 
   627 
   628 type envp =(*9.5.03: unused, delete with field in ptree.PblObj FIXSXXME*)
   629    (int * term list) list * (*assoc-list: args of met*)
   630    (int * rls) list *       (*assoc-list: tacs already done ///15.9.00*)
   631    (int * ets) list *       (*assoc-list: tacs etc. already done*)
   632    (string * pos) list;     (*asms * from where*)
   633 val empty_envp = ([],[],[],[]):envp; 
   634 
   635 datatype ppobj = 
   636     PrfObj of {cell  : cid,    (* 4.00 superfluous: use cid in DG only!!*)
   637 	       form  : term,    
   638 	       tac : tac,  (*8.01: superfluous -> ets !!!*)
   639 	       loc   : istate option * istate option, (*for form, result 
   640 13.8.02: (None,None) <==> e_istate ! see update_loc, get_loc*)
   641 	       branch: branch,
   642 	       result: term * term list,    
   643 	                          (*FIXX@ME result:term * term list
   644 				   => applicable_in =..rewrite_set_
   645 			           => rls = {erls: rls (NOT rls'),...}*)
   646 	       ostate: ostate}    (*Complete <=> result is OK*)
   647   | PblObj of {cell  : cid, 
   648 	       origin: (ori list) * spec,
   649 	       model : cterm' ppc,   (*1.00. not used anymore*)
   650 	       spec  : spec,
   651 	       probl : itm list,     (*8.01: TODO a (_*_) list for UNDO*)
   652 	       meth  : itm list, 
   653 	       env   : envp,(*FIXXXMEby loc 20.8.01 superfluous-- delete*)
   654 	       loc   : istate option * istate option, (*for pbl+met, result 
   655 20.8.01: loc will hold ets (diss p.94: (tac_,denval(=form|res),loc,env)*)
   656 	       branch: branch,
   657 	       result: term * term list,
   658 	       ostate: ostate};   (*Complete <=> result is OK*)
   659 datatype ptree = 
   660     EmptyPtree
   661   | Nd of ppobj * (ptree list);
   662 val e_ptree = EmptyPtree;
   663 
   664 (*in CalcTree/Subproblem an 'just_created' model is created;
   665   this is filled to 'untouched' by Model/Refine_Problem*)
   666 fun just_created (PblObj {meth, probl, spec, ...}) = 
   667     null meth andalso null probl andalso spec = e_spec;
   668 val e_origin = ([],e_spec): (ori list) * spec;
   669 
   670 
   671 
   672 fun rep_prfobj (PrfObj {cell,form,tac,loc,branch,result,ostate}) =
   673   {cell=cell,form=form,tac=tac,loc=loc,branch=branch,result=result,ostate=ostate};
   674 fun rep_pblobj (PblObj {cell,origin,model,spec,probl,meth,env,
   675 			loc,branch,result,ostate}) =
   676   {cell=cell,origin=origin,model=model,spec=spec,probl=probl,meth=meth,
   677    env=env,loc=loc,branch=branch,result=result,ostate=ostate};
   678 fun is_prfobj (PrfObj _) = true
   679   | is_prfobj _ =false;
   680 (*val is_prfobj' = get_obj is_prfobj; *)
   681 fun is_pblobj (PblObj _) = true
   682   | is_pblobj _ = false;
   683 (*val is_pblobj' = get_obj is_pblobj; 'Error: unbound constructor get_obj'*)
   684 
   685 
   686 exception PTREE of string;
   687 fun nth _ []      = raise PTREE "nth _ []"
   688   | nth 1 (x::xs) = x
   689   | nth n (x::xs) = nth (n-1) xs;
   690 (*> nth 2 [11,22,33]; -->> val it = 22 : int*)
   691 
   692 fun lev_up ([]:pos) = raise PTREE "lev_up []"
   693   | lev_up p = (drop_last p):pos;
   694 fun lev_on ([]:pos) = raise PTREE "lev_on []"
   695   | lev_on pos = 
   696     let val len = length pos
   697     in (drop_last pos) @ [(nth len pos)+1] end;
   698 fun lev_onFrm ((p,_):pos') = (lev_on p,Frm):pos'
   699   | lev_onFrm p = raise PTREE ("*** lev_onFrm: pos'="^(pos'2str p));
   700 
   701 fun lev_pred ([]:pos) = raise PTREE "lev_pred []"
   702   | lev_pred (pos:pos) = 
   703     let val len = length pos
   704     in ((drop_last pos) @ [(nth len pos)-1]):pos end;
   705 (*lev_pred [1,2,3];
   706 val it = [1,2,2] : pos
   707 > lev_pred [1];
   708 val it = [0] : pos          *)
   709 fun lev_dn p = p @ [0];
   710 (*> (lev_dn o lev_on) [1,2,3];
   711 val it = [1,2,4,0] : pos    *)
   712 fun lev_dn' ((p,p_):pos') = (lev_dn p, Frm):pos';
   713 fun lev_dnRes ((p,_):pos') = (lev_dn p, Res):pos';
   714 
   715 (*4.4.00*)
   716 fun lev_up_ ((p,Res):pos') = (lev_up p,Res):pos'
   717   | lev_up_ p' = raise error ("lev_up_: called for "^(pos'2str p'));
   718 fun lev_dn_ ((p,_):pos') = (lev_dn p,Res):pos'
   719 fun ind ((p,_):pos') = length p;
   720 
   721 
   722 (** convert ptree to a string **)
   723 
   724 (* convert a pos from list to string *)
   725 fun pr_pos ps = (space_implode "." (map string_of_int ps))^".   ";
   726 (* show hd origin or form only *)
   727 fun pr_short (p:pos) (PblObj {origin = (ori,_),...}) = 
   728   ((pr_pos p) ^ " ----- pblobj -----\n")
   729 (*   ((((Sign.string_of_term (sign_of thy)) o #4 o hd) ori)^" "^
   730     (((Sign.string_of_term (sign_of thy)) o hd(*!?!*) o #5 o hd) ori))^
   731    "\n") *)
   732   | pr_short p (PrfObj {form = form,...}) =
   733   ((pr_pos p) ^ (term2str form) ^ "\n");
   734 fun pr_cell (p:pos) (PblObj {cell = c, origin = (ori,_),...}) = 
   735   ((ints2str c) ^"   "^ 
   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_cell p (PrfObj {cell = c, form = form,...}) =
   740   ((ints2str c) ^"   "^ (term2str form) ^ "\n");
   741 
   742 
   743 (* convert ptree *)
   744 fun pr_ptree f pt =
   745   let
   746     fun pr_pt pfn _  EmptyPtree = ""
   747       | pr_pt pfn ps (Nd (b, [])) = pfn ps b
   748       | pr_pt pfn ps (Nd (b, ts)) = (pfn ps b)^
   749       (prts pfn (ps:pos) 1 ts)
   750     and prts pfn ps p [] = ""
   751       | prts pfn ps p (t::ts) = (pr_pt pfn (ps @ [p]) t)^
   752       (prts pfn ps (p+1) ts)
   753   in pr_pt f [] pt end;
   754 (*
   755 > fun prfn ps b = (pr_pos ps)^"   "^b(*TODO*)^"\n";
   756 > val pt = ref EmptyPtree;
   757 > pt:=Nd("root",
   758        [Nd("xx1",[]),
   759 	Nd("xx2",
   760 	   [Nd("xx2.1.",[]),
   761 	    Nd("xx2.2.",[])]),
   762 	Nd("xx3",[])]);
   763 > writeln (pr_ptree prfn (!pt));
   764 *)
   765 
   766 
   767 (** access the branches of ptree **)
   768 
   769 fun ins_nth 1 e l  = e::l
   770   | ins_nth n e [] = raise PTREE "ins_nth n e []"
   771   | ins_nth n e (l::ls) = l::(ins_nth (n-1) e ls);
   772 fun repl []      _ _ = raise PTREE "repl [] _ _"
   773   | repl (l::ls) 1 e = e::ls
   774   | repl (l::ls) n e = l::(repl ls (n-1) e);
   775 fun repl_app ls n e = 
   776     let val lim = 1 + length ls
   777     in if n > lim then raise PTREE "repl_app: n > lim"
   778        else if n = lim then ls @ [e]
   779 	    else repl ls n e end;
   780 (*  
   781 > repl [1,2,3] 2 22222;
   782 val it = [1,22222,3] : int list
   783 > repl_app [1,2,3,4] 5 5555;
   784 val it = [1,2,3,4,5555] : int list
   785 > repl_app [1,2,3] 2 22222;
   786 val it = [1,22222,3] : int list
   787 > repl_app [1] 2 22222 ;
   788 val it = [1,22222] : int list
   789 *)
   790 
   791 
   792 (** get from obj at pos by f : ppobj -> 'a **)
   793 
   794 fun get_obj f EmptyPtree    _      = raise PTREE "get_obj f EmptyPtree"
   795   | get_obj f (Nd (b,  _)) []      = f b
   796   | get_obj f (Nd (b, bs)) (p::ps) = 
   797   let val _ = (nth p bs) handle _ => raise PTREE ("get_obj: pos = "^
   798 			   (ints2str' (p::ps))^" does not exist");
   799   in (get_obj f (nth p bs) (ps:pos)) 
   800     handle _ => raise PTREE ("get_obj: at pos = "^
   801 			     (ints2str' (p::ps))^" wrong type of ppobj")
   802       (*FIXME: 'wrong type..' raised also if pos doesn't exist*)
   803   end;
   804 fun get_nd EmptyPtree _ = raise PTREE "get_nd EmptyPtree"
   805   | get_nd n [] = n
   806   | get_nd (Nd (_,nds)) (pos as p::(ps:pos)) = (get_nd (nth p nds) ps)
   807     handle _ => raise PTREE ("get_nd: not existent pos = "^(ints2str' pos));
   808 
   809 
   810 (* for use by get_obj *)
   811 fun g_cell   (PblObj {cell = c,...}) = c
   812   | g_cell   (PrfObj {cell = c,...}) = c;
   813 fun g_form   (PrfObj {form = f,...}) = f
   814   | g_form   _ = raise PTREE "g_form not for PblObj";
   815 fun g_origin (PblObj {origin = ori,...}) = ori
   816   | g_origin _ = raise PTREE "g_origin not for PrfObj";
   817 fun g_model (PblObj {model = f,...}) = f
   818   | g_model _ = raise PTREE "g_model not for PrfObj";
   819 fun g_spec   (PblObj {spec = s,...}) = s
   820   | g_spec _   = raise PTREE "g_spec not for PrfObj";
   821 fun g_pbl    (PblObj {probl = p,...}) = p
   822   | g_pbl  _   = raise PTREE "g_pbl not for PrfObj";
   823 fun g_met    (PblObj {meth = p,...}) = p
   824   | g_met  _   = raise PTREE "g_met not for PrfObj";
   825 fun g_domID  (PblObj {spec = (d,_,_),...}) = d
   826   | g_domID  _ = raise PTREE "g_metID not for PrfObj";
   827 fun g_metID  (PblObj {spec = (_,_,m),...}) = m
   828   | g_metID  _ = raise PTREE "g_metID not for PrfObj";
   829 fun g_args    (PblObj {env = (a,_,_,_),...}) = a
   830   | g_args _    = raise PTREE "g_args not for PrfObj";
   831 fun g_tacs    (PblObj {env = (_,a,_,_),...}) = a
   832   | g_tacs _    = raise PTREE "g_tacs not for PrfObj";
   833 fun g_ets    (PblObj {env = (_,_,a,_),...}) = a
   834   | g_ets _    = raise PTREE "g_tacs not for PrfObj";
   835 fun g_asm    (PblObj {env = (_,_,_,a),...}) = a
   836   | g_asm _    = raise PTREE "g_asm not for PrfObj";
   837 fun g_loc    (PblObj {loc = l,...}) = l
   838   | g_loc    (PrfObj {loc = l,...}) = l;
   839 fun g_branch (PblObj {branch = b,...}) = b
   840   | g_branch (PrfObj {branch = b,...}) = b;
   841 fun g_tac  (PblObj {spec = (d,p,m),...}) = Apply_Method m
   842   | g_tac  (PrfObj {tac = m,...}) = m;
   843 fun g_result (PblObj {result = r,...}) = r
   844   | g_result (PrfObj {result = r,...}) = r;
   845 fun g_ostate (PblObj {ostate = r,...}) = r
   846   | g_ostate (PrfObj {ostate = r,...}) = r;
   847 
   848 fun gpt_cell (Nd (PblObj {cell = c,...},_)) = c
   849   | gpt_cell (Nd (PrfObj {cell = c,...},_)) = c;
   850 
   851 fun existpt pos pt = can (get_obj I pt) pos;
   852 fun existpt' ((p,p_):pos') pt = 
   853     if can (get_obj I pt) p 
   854     then case p_ of 
   855 	     Res => get_obj g_ostate pt p = Complete
   856 	   | _ => true
   857     else false;
   858 
   859 (*.find the position of the next parent which is a PblObj in ptree.*)
   860 fun par_pblobj pt ([]:pos) = ([]:pos)
   861   | par_pblobj pt p =
   862     let fun par pt [] = []
   863 	  | par pt p = if is_pblobj (get_obj I pt p) then p
   864 		       else par pt (lev_up p)
   865     in par pt (lev_up p) end; 
   866 (* lev_up for hard_gen operating with pos = [...,0] *)
   867 
   868 (*.find the position and the children of the next parent which is a PblObj.*)
   869 fun par_children (Nd (PblObj _, children)) ([]:pos) = (children, []:pos)
   870   | par_children (pt as Nd (PblObj _, children)) p =
   871     let fun par [] = (children, [])
   872 	  | par p = let val Nd (obj, children) = get_nd pt p
   873 		    in if is_pblobj obj then (children, p) else par (lev_up p)
   874 		    end;
   875     in par (lev_up p) end; 
   876 
   877 (*.get the children of a node in ptree.*)
   878 fun children (Nd (PblObj _, cn)) = cn
   879   | children (Nd (PrfObj _, cn)) = cn;
   880 
   881 
   882 (*.find the next parent, which is either a PblObj (return true)
   883   or a PrfObj with tac = Detail_Set (return false).*)
   884 (*FIXME.3.4.03:re-organize par_pbl_det after rls' --> rls*)
   885 fun par_pbl_det pt ([]:pos) = (true, []:pos, Erls)
   886   | par_pbl_det pt p =
   887     let fun par pt [] = (true, [], Erls)
   888 	  | par pt p = if is_pblobj (get_obj I pt p) then (true, p, Erls)
   889 		       else case get_obj g_tac pt p of
   890 				Detail_Set rls' => (false, p, assoc_rls rls')
   891 			      | _ => par pt (lev_up p)
   892     in par pt (lev_up p) end; 
   893 
   894 
   895 
   896 
   897 (** get from the whole ptree by f : ppobj -> 'a **)
   898 
   899 fun get_all f EmptyPtree   = []
   900   | get_all f (Nd (b, [])) = [f b]
   901   | get_all f (Nd (b, bs)) = [f b] @ (get_alls f bs)
   902 and get_alls f [] = []
   903   | get_alls f pts = flat (map (get_all f) pts);
   904 
   905 
   906 (** insert obj b into ptree at pos **)
   907 
   908 fun insert b EmptyPtree    []       = Nd (b, [])
   909   | insert b EmptyPtree    _        = raise PTREE "insert b Empty _"
   910   | insert b (Nd ( _,  _)) []       = raise PTREE "insert b _ []"
   911   | insert b (Nd (b', bs)) (p::[])  = 
   912      Nd (b', repl_app bs p (Nd (b,[]))) 
   913   | insert b (Nd (b', bs)) (p::ps)  =
   914      Nd (b', repl_app bs p (insert b (nth p bs) (ps:pos)));
   915 (*
   916 > type ppobj = string;
   917 > writeln (pr_ptree prfn (!pt));
   918   val pt = ref Empty;
   919   pt:= insert ("root":ppobj) EmptyPtree [];
   920   pt:= insert ("xx1":ppobj) (!pt) [1];
   921   pt:= insert ("xx2":ppobj) (!pt) [2];
   922   pt:= insert ("xx3":ppobj) (!pt) [3];
   923   pt:= insert ("xx2.1":ppobj) (!pt) [2,1];
   924   pt:= insert ("xx2.2":ppobj) (!pt) [2,2];
   925   pt:= insert ("xx2.1.1":ppobj) (!pt) [2,1,1];
   926   pt:= insert ("xx2.1.2":ppobj) (!pt) [2,1,2];
   927   pt:= insert ("xx2.1.3":ppobj) (!pt) [2,1,3];
   928 *)
   929 
   930 
   931 (** apply f to obj at pos, f: ppobj -> ppobj **)
   932 
   933 fun appl_to_node f (Nd (b,bs)) = Nd (f b, bs);
   934 fun appl_obj f EmptyPtree    []      = EmptyPtree
   935   | appl_obj f EmptyPtree    _       = raise PTREE "appl_obj f Empty _"
   936   | appl_obj f (Nd (b, bs)) []       = Nd (f b, bs)
   937   | appl_obj f (Nd (b, bs)) (p::[])  = 
   938      Nd (b, repl_app bs p (((appl_to_node f) o (nth p)) bs))
   939   | appl_obj f (Nd (b, bs)) (p::ps)  =
   940      Nd (b, repl_app bs p (appl_obj f (nth p bs) (ps:pos)));
   941  
   942 (* for use by appl_obj *) 
   943 fun repl_form f (PrfObj {cell=c,form= _,tac=tac,loc=loc,
   944 			 branch=branch,result=result,ostate=ostate}) =
   945     PrfObj {cell=c,form= f,tac=tac,loc=loc,
   946 	    branch=branch,result=result,ostate=ostate}
   947   | repl_form _ _ = raise PTREE "repl_form takes no PblObj";
   948 fun repl_pbl x    (PblObj {cell=cell,origin=origin,model=model,
   949 			   spec=spec,probl=_,meth=meth,env=env,loc=loc,
   950 			   branch=branch,result=result,ostate=ostate}) =
   951   PblObj {cell=cell,origin=origin,model=model,spec=spec,probl= x,
   952 	  meth=meth,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
   953   | repl_pbl _ _ = raise PTREE "repl_pbl takes no PrfObj";
   954 fun repl_met x    (PblObj {cell=cell,origin=origin,model=model,
   955 			   spec=spec,probl=probl,meth=_,env=env,loc=loc,
   956 			   branch=branch,result=result,ostate=ostate}) =
   957   PblObj {cell=cell,origin=origin,model=model,spec=spec,probl=probl,
   958 	  meth= x,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
   959   | repl_met _ _ = raise PTREE "repl_pbl takes no PrfObj";
   960 
   961 fun repl_spec  x    (PblObj {cell=cell,origin=origin,model=model,
   962 			   spec= _,probl=probl,meth=meth,env=env,loc=loc,
   963 			   branch=branch,result=result,ostate=ostate}) =
   964   PblObj {cell=cell,origin=origin,model=model,spec= x,probl=probl,
   965 	  meth=meth,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
   966   | repl_spec  _ _ = raise PTREE "repl_domID takes no PrfObj";
   967 fun repl_domID x    (PblObj {cell=cell,origin=origin,model=model,
   968 			   spec=(_,p,m),probl=probl,meth=meth,env=env,loc=loc,
   969 			   branch=branch,result=result,ostate=ostate}) =
   970   PblObj {cell=cell,origin=origin,model=model,spec=(x,p,m),probl=probl,
   971 	  meth=meth,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
   972   | repl_domID _ _ = raise PTREE "repl_domID takes no PrfObj";
   973 fun repl_pblID x    (PblObj {cell=cell,origin=origin,model=model,
   974 			   spec=(d,_,m),probl=probl,meth=meth,env=env,loc=loc,
   975 			   branch=branch,result=result,ostate=ostate}) =
   976   PblObj {cell=cell,origin=origin,model=model,spec=(d,x,m),probl=probl,
   977 	  meth=meth,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
   978   | repl_pblID _ _ = raise PTREE "repl_pblID takes no PrfObj";
   979 fun repl_metID x (PblObj {cell=cell,origin=origin,model=model,
   980 			   spec=(d,p,_),probl=probl,meth=meth,env=env,loc=loc,
   981 			   branch=branch,result=result,ostate=ostate}) =
   982   PblObj {cell=cell,origin=origin,model=model,spec=(d,p,x),probl=probl,
   983 	  meth=meth,env=env,loc=loc,branch=branch,result=result,ostate=ostate}
   984   | repl_metID _ _ = raise PTREE "repl_metID takes no PrfObj";
   985 
   986 fun repl_result l f' s (PrfObj {cell=cell,form=form,tac=tac,loc=_,
   987 			     branch=branch,result = _ ,ostate = _}) =
   988     PrfObj {cell=cell,form=form,tac=tac,loc= l,
   989 	    branch=branch,result = f',ostate = s}
   990   | repl_result l f' s (PblObj {cell=cell,origin=origin,model=model,
   991 			     spec=spec,probl=probl,meth=meth,env=env,loc=_,
   992 			     branch=branch,result= _ ,ostate= _}) =
   993     PblObj {cell=cell,origin=origin,model=model,
   994 	    spec=spec,probl=probl,meth=meth,env=env,loc= l,
   995 	    branch=branch,result= f',ostate= s};
   996 
   997 fun repl_tac x (PrfObj {cell=cell,form=form,tac= _,loc=loc,
   998 			  branch=branch,result=result,ostate=ostate}) =
   999     PrfObj {cell=cell,form=form,tac= x,loc=loc,
  1000 	    branch=branch,result=result,ostate=ostate}
  1001   | repl_tac _ _ = raise PTREE "repl_tac takes no PblObj";
  1002 
  1003 fun repl_branch b (PblObj {cell=cell,origin=origin,model=model,
  1004 			   spec=spec,probl=probl,meth=meth,env=env,loc=loc,
  1005 			   branch= _,result=result,ostate=ostate}) =
  1006   PblObj {cell=cell,origin=origin,model=model,spec=spec,probl=probl,
  1007 	  meth=meth,env=env,loc=loc,branch= b,result=result,ostate=ostate}
  1008   | repl_branch b (PrfObj {cell=cell,form=form,tac=tac,loc=loc,
  1009 			  branch= _,result=result,ostate=ostate}) =
  1010     PrfObj {cell=cell,form=form,tac=tac,loc=loc,
  1011 	    branch= b,result=result,ostate=ostate};
  1012 
  1013 fun repl_args args
  1014   (PblObj {cell=cell,origin=origin,model=model,
  1015 	   spec=spec,probl=probl,meth=meth,env=(_,tac,ets,asm),loc=loc,
  1016 	   branch=branch,result=result,ostate=ostate}) =
  1017   PblObj {cell=cell,origin=origin,model=model,spec=spec,probl=probl,
  1018 	  meth=meth,env=(args,tac,ets,asm),loc=loc,branch=branch,
  1019 	  result=result,ostate=ostate}
  1020   | repl_args _ _ = raise PTREE "repl_args takes no PrfObj";
  1021 fun repl_tacs erul
  1022   (PblObj {cell=cell,origin=origin,model=model,
  1023 	   spec=spec,probl=probl,meth=meth,env=(arg,_,ets,asm),loc=loc,
  1024 	   branch=branch,result=result,ostate=ostate}) =
  1025   PblObj {cell=cell,origin=origin,model=model,spec=spec,probl=probl,
  1026 	  meth=meth,env=(arg,erul,ets,asm),loc=loc,branch=branch,
  1027 	  result=result,ostate=ostate}
  1028   | repl_tacs _ _ = raise PTREE "repl_tacs takes no PrfObj";
  1029 fun repl_ets ets
  1030   (PblObj {cell=cell,origin=origin,model=model,
  1031 	   spec=spec,probl=probl,meth=meth,env=(arg,tac,_,asm),loc=loc,
  1032 	   branch=branch,result=result,ostate=ostate}) =
  1033   PblObj {cell=cell,origin=origin,model=model,spec=spec,probl=probl,
  1034 	  meth=meth,env=(arg,tac,ets,asm),loc=loc,branch=branch,
  1035 	  result=result,ostate=ostate}
  1036   | repl_ets _ _ = raise PTREE "repl_ets takes no PrfObj";
  1037 
  1038 fun repl_oris oris
  1039   (PblObj {cell=cell,origin=(_,spe),model=model,
  1040 	   spec=spec,probl=probl,meth=meth,env=env,loc=loc,
  1041 	   branch=branch,result=result,ostate=ostate}) =
  1042   PblObj{cell=cell,origin=(oris,spe),model=model,spec=spec,probl=probl,
  1043 	  meth=meth,env=env,loc=loc,branch=branch,
  1044 	  result=result,ostate=ostate}
  1045   | repl_oris _ _ = raise PTREE "repl_oris takes no PrfObj";
  1046 fun repl_orispec spe
  1047   (PblObj {cell=cell,origin=(oris,_),model=model,
  1048 	   spec=spec,probl=probl,meth=meth,env=env,loc=loc,
  1049 	   branch=branch,result=result,ostate=ostate}) =
  1050   PblObj{cell=cell,origin=(oris,spe),model=model,spec=spec,probl=probl,
  1051 	  meth=meth,env=env,loc=loc,branch=branch,
  1052 	  result=result,ostate=ostate}
  1053   | repl_orispec _ _ = raise PTREE "repl_orispec takes no PrfObj";
  1054 
  1055 fun repl_loc l (PblObj {cell=cell,origin=origin,model=model,
  1056 			spec=spec,probl=probl,meth=meth,env=env,loc=_,
  1057 			branch=branch,result=result,ostate=ostate}) =
  1058   PblObj {cell=cell,origin=origin,model=model,spec=spec,probl=probl,
  1059 	  meth=meth,env=env,loc=l,branch=branch,result=result,ostate=ostate}
  1060   | repl_loc l (PrfObj {cell=cell,form=form,tac=tac,loc=_,
  1061 			branch=branch,result=result,ostate=ostate}) =
  1062   PrfObj {cell=cell,form=form,tac=tac,loc= l,
  1063 	  branch=branch,result=result,ostate=ostate};
  1064 
  1065 fun uni__asm asm' 
  1066   (PblObj {cell=cell,origin=origin,model=model,
  1067 	   spec=spec,probl=probl,meth=meth,env=(arg,tac,ets,asm),loc=loc,
  1068 	   branch=branch,result=result,ostate=ostate}) =
  1069   PblObj {cell=cell,origin=origin,model=model,spec=spec,probl=probl,
  1070 	  meth=meth,env=(arg,tac,ets,asm union asm'),loc=loc,branch=branch,
  1071 	  result=result,ostate=ostate}
  1072   | uni__asm _ _ = raise PTREE "uni__asm takes no PrfObj";
  1073 fun uni__cid cell' 
  1074   (PblObj {cell=cell,origin=origin,model=model,
  1075 	   spec=spec,probl=probl,meth=meth,env=env,loc=loc,
  1076 	   branch=branch,result=result,ostate=ostate}) =
  1077   PblObj {cell=cell union cell',origin=origin,model=model,spec=spec,probl=probl,
  1078 	  meth=meth,env=env,loc=loc,branch=branch,
  1079 	  result=result,ostate=ostate}
  1080   | uni__cid cell'
  1081   (PrfObj {cell=cell,form=form,tac=tac,loc=loc,
  1082 	   branch=branch,result=result,ostate=ostate}) =
  1083   PrfObj {cell=cell union cell',form=form,tac=tac,loc=loc,
  1084 	  branch=branch,result=result,ostate=ostate};
  1085 
  1086   
  1087 
  1088 
  1089 (** applies (snd f) to 1 level of branches if ((fst f) b),
  1090     f : (ppobj -> bool) * (int -> ptree list -> ptree list) **)
  1091 
  1092 fun appl_branch f EmptyPtree [] = (EmptyPtree, false)
  1093   | appl_branch f EmptyPtree _  = raise PTREE "appl_branch f Empty _"
  1094   | appl_branch f (Nd ( _, _)) [] = raise PTREE "appl_branch f _ []"
  1095   | appl_branch f (Nd (b, bs)) (p::[]) = 
  1096     if (fst f) b then (Nd (b, (snd f) (p:posel) bs), true)
  1097     else (Nd (b, bs), false)
  1098   | appl_branch f (Nd (b, bs)) (p::ps) =
  1099 	let val (b',bool) = appl_branch f (nth p bs) ps
  1100 	in (Nd (b, repl_app bs p b'), bool) end;
  1101 
  1102 (* expl for f as used with appl_branch *)
  1103 fun test_trans (PrfObj{branch = Transitive,...}) = true
  1104   | test_trans (PblObj{branch = Transitive,...}) = true
  1105   | test_trans _ = false;
  1106 (*
  1107 val cut_branch = (test_trans, curry take):
  1108     (ppobj -> bool) * (int -> ptree list -> ptree list);
  1109 .. formlery used for ...
  1110 fun cut_tree _ [] = EmptyPtree
  1111   | cut_tree pt pos = 
  1112   let val (pt',cut) = appl_branch cut_branch pt pos
  1113   in if cut andalso length pos > 1 then cut_tree pt' (lev_up pos)
  1114      else pt' end;
  1115 *)
  1116 
  1117 (* specialized appl_branch for cut + return cuts *)
  1118 fun appl_cut EmptyPtree _  = raise PTREE "appl_cut Empty _"
  1119   | appl_cut (Nd ( _, _)) [] = raise PTREE "appl_cut _ []"
  1120   | appl_cut (Nd (b, bs)) (p::[]) = 
  1121   if test_trans b 
  1122     then (Nd (b, take (p:posel, bs)), true,
  1123 	  (flat o (map (get_all g_cell))) (takerest (p,bs)))
  1124   else (Nd (b, bs), false, [])
  1125   | appl_cut (Nd (b, bs)) (p::ps) =
  1126     let val (b',bool,cuts) = appl_cut (nth p bs) ps
  1127     in (Nd (b, repl_app bs p b'), bool, cuts) end;
  1128 
  1129 
  1130 (* cut objs below and after a position as long as Transitive 
  1131    FIXME?: whole ptree copied for each branch-level cut  
  1132    FIXME!: should return pos list instead cellID list*)
  1133 fun cut_tree _ ([]:pos) = raise PTREE "cut_tree _ []"
  1134   | cut_tree pt pos =
  1135   let
  1136     fun cutfn pt cuts pos = 
  1137       let val (pt', cut, cuts') = appl_cut pt pos;
  1138 (*	val _ = writeln (foldr (op^) (cuts',""))     *)
  1139       in if cut andalso length pos > 1 
  1140 	   then cutfn pt' (cuts @ cuts') (lev_up pos)
  1141 	 else (pt',cuts @ cuts') end
  1142   in (apsnd flat (cutfn pt [] pos)):ptree * cid end;
  1143 (* pt of max_on_surface
  1144 > writeln (pr_ptree pr_cell pt);
  1145 > val (pt',cutl) = cut_tree pt [4,1,1,1,1];
  1146 val cutl = ["[4,1,1,1,2]:","[4,1,1,1,3]:"] : cid list
  1147 > writeln (pr_ptree pr_cell pt');
  1148 
  1149 > val (pt'',cutl) = cut_tree pt [4,1];
  1150 val cutl = [] : cid list
  1151 > writeln (pr_ptree pr_cell pt'');
  1152 
  1153 > val (pt''',cutl) = cut_tree pt [1,1];
  1154 val cutl =
  1155   ["[1,2]:","[2]:","[3]:","[3,1]:","[3,2]:","[4]:","[4,1]:","[4,1,1]:",...]
  1156   : cid list
  1157 > writeln (pr_ptree pr_cell pt''');
  1158 *)
  1159 
  1160 
  1161 
  1162 fun append_atomic p l f r f' s pt = 
  1163   let 
  1164       val (iss, f) = if existpt p pt andalso get_obj g_tac pt p=Empty_Tac
  1165 		     then (*after Take*)
  1166 			 ((fst (get_obj g_loc pt p), Some l), 
  1167 			  get_obj g_form pt p) 
  1168 		     else ((Some l, None), f)
  1169   in insert (PrfObj {cell = [(*3.00. unused*)],
  1170 		     form  = f,
  1171 		     tac  = r,
  1172 		     loc   = iss,
  1173 		     branch= NoBranch,
  1174 		     result= f',
  1175 		     ostate= s}) pt p end;
  1176 
  1177 (*20.8.02: cappend_* FIXXXXME cut branches below cannot be decided here:
  1178   detail - generate - cappend: inserted, not appended !!!
  1179 
  1180   cut decided in applicable_in !!!
  1181 *)
  1182 fun cappend_atomic pt p loc f r f' s = 
  1183   apfst (append_atomic p loc f r f' s) (cut_tree pt p);
  1184 
  1185 (* called by Take *)
  1186 fun append_form p l f pt = 
  1187   insert (PrfObj {cell = [(*3.00. unused*)],
  1188 		  form  = (*if existpt p pt 
  1189 		  andalso get_obj g_tac pt p = Empty_Tac 
  1190 			    (*distinction from 'old' (+complete!) pobjs*)
  1191 			    then get_obj g_form pt p else*) f,
  1192 		  tac  = Empty_Tac,
  1193 		  loc   = (Some l, None),
  1194 		  branch= NoBranch,
  1195 		  result= (e_term,[]),
  1196 		  ostate= Incomplete}) pt p;
  1197 fun cappend_form pt p loc f =
  1198   apfst (append_form p loc f) (cut_tree pt p);
  1199 
  1200 
  1201     
  1202 fun append_result pt p l f s =
  1203     (appl_obj (repl_result (fst (get_obj g_loc pt p),
  1204 			    (Some l)) f s) pt p, []);
  1205 
  1206 
  1207 
  1208 fun append_parent p l f r b pt = 
  1209   let 
  1210     val (ll,f) = if existpt p pt andalso get_obj g_tac pt p=Empty_Tac
  1211 		  then ((fst (get_obj g_loc pt p), Some l), 
  1212 			get_obj g_form pt p) 
  1213 		 else ((Some l, None), f)
  1214   in insert (PrfObj 
  1215 	  {cell = [(*unused*)],
  1216 	   form  = f,
  1217 	   tac  = r,
  1218 	   loc   = ll,
  1219 	   branch= b,
  1220 	   result= (e_term,[]),
  1221 	   ostate= Incomplete}) pt p end;
  1222 fun cappend_parent pt p loc f r b =
  1223   apfst (append_parent p loc f r b) (cut_tree pt p);
  1224 
  1225 (*13.8.02 deleted again --- istate option
  1226 fun init_ptree (strs,spec) =
  1227   (Nd (PblObj 
  1228 	       {cell  = [(*3.00. unused*)],
  1229 		origin= (strs,spec),
  1230 		model = empty_ppc_ct',
  1231 		spec  = empty_spec,
  1232 		probl = []:itm list,
  1233 		meth  = []:itm list,
  1234 		env   = empty_envp,
  1235 		loc   = (None, None),
  1236 		branch= NoBranch,
  1237 		result= empty_cterm',
  1238 		ostate= Incomplete},[]));----------------*)
  1239 
  1240 fun append_problem [] l (strs,spec) _ =
  1241   (Nd (PblObj 
  1242 	       {cell  = [(*3.00. unused*)],
  1243 		origin= (strs,spec),
  1244 		model = empty_ppc_ct',
  1245 		spec  = empty_spec,
  1246 		probl = []:itm list,
  1247 		meth  = []:itm list,
  1248 		env   = empty_envp,
  1249 		loc   = (Some l, None),
  1250 		branch= TransitiveB,(*FIXXXXXME.27.8.03: for equations only*)
  1251 		result= (e_term,[]),
  1252 		ostate= Incomplete},[]))
  1253   | append_problem p l (strs,spec) pt =
  1254   insert (PblObj 
  1255 	  {cell  = [(*3.00. unused*)],
  1256 	   origin= (strs,spec),
  1257 	   model = empty_ppc_ct',
  1258 	   spec  = empty_spec,
  1259 	   probl = []:itm list,
  1260 	   meth  = []:itm list,
  1261 	   env   = empty_envp,
  1262 	   loc   = (Some l, None),
  1263 	   branch= TransitiveB,
  1264 	   result= (e_term,[]),
  1265 	   ostate= Incomplete}) pt p;
  1266 fun cappend_problem _ [] loc (oris, spec) =
  1267   (append_problem [] loc (oris, spec) EmptyPtree,[])
  1268   | cappend_problem pt p loc (oris, spec) = 
  1269   apfst (append_problem p (loc:istate) (oris, spec)) (cut_tree pt p);
  1270 
  1271 
  1272 (* use"ME/sequent.sml";
  1273    use"sequent.sml";
  1274    *)
  1275 
  1276 
  1277 
  1278 (*
  1279 fun update_model  pt pos x = appl_obj (repl_model  x) pt pos;
  1280                                        1.00 not used anymore*)
  1281 fun update_args   pt pos x = appl_obj (repl_args   x) pt pos;
  1282 fun update_tacs   pt pos x = appl_obj (repl_tacs   x) pt pos;
  1283 fun update_ets    pt pos x = appl_obj (repl_ets    x) pt pos;
  1284 fun update_domID  pt pos x = appl_obj (repl_domID  x) pt pos;
  1285 fun update_pblID  pt pos x = appl_obj (repl_pblID  x) pt pos;
  1286 fun update_metID  pt pos x = appl_obj (repl_metID  x) pt pos;
  1287 fun update_spec   pt pos x = appl_obj (repl_spec   x) pt pos;
  1288 
  1289 fun update_pbl    pt pos x = appl_obj (repl_pbl    x) pt pos;
  1290 fun update_pblppc pt pos x = appl_obj (repl_pbl    x) pt pos;
  1291 
  1292 fun update_met    pt pos x = appl_obj (repl_met    x) pt pos;
  1293 (*1.09.01 ----
  1294 fun update_metppc pt pos x = 
  1295   let val {rew_ord'=od,rls'=rs,asm_thm=at,asm_rls=ar,...} =
  1296     get_obj g_met pt pos
  1297   in appl_obj (repl_met 
  1298      {rew_ord'=od,rls'=rs,asm_thm=at,asm_rls=ar,ppc=x}) 
  1299     pt pos end;*)
  1300 fun update_metppc pt pos x = appl_obj (repl_met    x) pt pos;
  1301 			 			   
  1302 fun union_asm     pt pos x = appl_obj (uni__asm    x) pt pos; 
  1303 fun union_cid     pt pos x = appl_obj (uni__cid    x) pt pos;
  1304 
  1305 fun update_branch pt pos x = appl_obj (repl_branch x) pt pos;
  1306 fun update_tac  pt pos x = appl_obj (repl_tac  x) pt pos;
  1307 
  1308 fun update_oris   pt pos x = appl_obj (repl_oris   x) pt pos;
  1309 fun update_orispec   pt pos x = appl_obj (repl_orispec   x) pt pos;
  1310 
  1311  (*done by append_* !! 3.5.02*)
  1312 fun update_loc pt (p,_) (ScrState ([],[],None,
  1313 				   Const ("empty",_),Sundef,false)) = 
  1314     appl_obj (repl_loc (None,None)) pt p
  1315   | update_loc pt (p,Res) x =  
  1316     let val (lform,_) = get_obj g_loc pt p
  1317     in appl_obj (repl_loc (lform,Some x)) pt p end
  1318 
  1319   | update_loc pt (p,_) x = 
  1320     let val (_,lres) = get_obj g_loc pt p
  1321     in appl_obj (repl_loc (Some x,lres)) pt p end;
  1322 
  1323 (*13.8.02---------------------------
  1324 fun get_loc EmptyPtree _ = None
  1325   | get_loc pt (p,Res) =
  1326   let val (lfrm,lres) = get_obj g_loc pt p
  1327   in if lres = e_istate then lfrm else lres end
  1328   | get_loc pt (p,_) =
  1329   let val (lfrm,lres) = get_obj g_loc pt p
  1330   in if lfrm = e_istate then lres else lfrm end;  5.10.00: too liberal ?*)
  1331 (*13.8.02: options, because istate is no equalitype any more*)
  1332 fun get_loc EmptyPtree _ = e_istate
  1333   | get_loc pt (p,Res) =
  1334     (case get_obj g_loc pt p of
  1335 	 (Some i, None) => i
  1336        | (None  , None) => e_istate
  1337        | (_     , Some i) => i)
  1338   | get_loc pt (p,_) =
  1339     (case get_obj g_loc pt p of
  1340 	 (None  , Some i) => i (*13.8.02 just copied from ^^^: too liberal ?*)
  1341        | (None  , None) => e_istate
  1342        | (Some i, _) => i);
  1343 val get_istate = get_loc; (*3.5.02*)
  1344 
  1345 (*.collect the assumptions within a problem up to a certain position.*)
  1346 fun get_asm (b:pos, p:pos) (Nd (PblObj {result=(_,asm),...},_)) = 
  1347     ((*writeln ("### get_asm PblObj:(b,p)= "^
  1348 		(pair2str(ints2str b, ints2str p)));*)
  1349      (map (rpair b) asm))
  1350   | get_asm (b, p) (Nd (PrfObj {result=(_,asm),...}, [])) = 
  1351     ((*writeln ("### get_asm PrfObj []:(b,p)= "^
  1352 	      (pair2str(ints2str b, ints2str p)));*)
  1353      (map (rpair b) asm))
  1354   | get_asm (b, p:pos) (Nd (PrfObj _, nds)) = 
  1355     let (*val _= writeln ("### get_asm PrfObj nds:(b,p)= "^
  1356 	      (pair2str(ints2str b, ints2str p)));*)
  1357 	val levdn = 
  1358 	    if p <> [] then (b @ [hd p]:pos, tl p:pos) 
  1359 	    else (b @ [1], [99999]) (*_deeper_ nesting is always _before_ p*)
  1360     in gets_asm levdn 1 nds end
  1361 and gets_asm _ _ [] = []
  1362   | gets_asm (b, p' as p::ps) i (nd::nds) = 
  1363     if p < i then [] 
  1364     else ((*writeln ("### gets_asm: (b,p')= "^(pair2str(ints2str b,
  1365 						      ints2str p')));*)
  1366 	  (get_asm (b @ [i], ps) nd) @ (gets_asm (b, p') (i + 1) nds));
  1367 
  1368 fun get_assumptions_ (Nd (PblObj {result=(r,asm),...}, cn)) (([], _):pos') = 
  1369     if r = e_term then gets_asm ([], [99999]) 1 cn 
  1370     else map (rpair []) asm
  1371   | get_assumptions_ pt (p,p_) =
  1372     let val (cn, base) = par_children pt p
  1373 	val offset = drop (length base, p)
  1374 	val base' = replicate (length base) 1
  1375 	val offset' = case p_ of 
  1376 			 Frm => let val (qs,q) = split_last offset
  1377 				in qs @ [q - 1] end
  1378 		       | _ => offset
  1379         (*val _= writeln ("... get_assumptions: (b,o)= "^
  1380 			(pair2str(ints2str base',ints2str offset)))*)
  1381     in gets_asm (base', offset) 1 cn end;
  1382 
  1383 
  1384 (*---------
  1385 end
  1386 
  1387 open Ptree;
  1388 ----------*)
  1389 
  1390 (*pos of the formula on FE relative to the current pos,
  1391   which is the next writepos*)
  1392 fun pre_pos ([]:pos) = []:pos
  1393   | pre_pos pp =
  1394   let val (ps,p) = split_last pp
  1395   in case p of 1 => ps | n => ps @ [n-1] end;
  1396 
  1397 (*WN.20.5.03 ... but not used*)
  1398 fun posless [] (_::_) = true
  1399   | posless (_::_) [] = false
  1400   | posless (p::ps) (q::qs) = if p = q then posless ps qs else p < q;
  1401 (* posless [2,3,4] [3,4,5];
  1402 true
  1403 >  posless [2,3,4] [1,2,3];
  1404 false
  1405 >  posless [2,3] [2,3,4];
  1406 true
  1407 >  posless [2,3,4] [2,3];
  1408 false                    
  1409 >  posless [6] [6,5,2];
  1410 true
  1411 +++ see Isabelle/../library.ML*)
  1412 
  1413 
  1414 (*development for extracting an 'interval' from ptree stopped 8.03*)
  1415 local
  1416 fun hdp [] = 1     | hdp [0] = 1     | hdp x = hd x;
  1417 fun hdq	[] = 99999 | hdq [0] = 99999 | hdq x = hd x;
  1418 
  1419 fun tlp [] = [0]     | tlp [_] = [0]     | tlp x = tl x;
  1420 fun tlq [] = [99999] | tlq [_] = [99999] | tlq x = tl x;
  1421 
  1422 fun drop_init ([]::xs) = drop_init xs
  1423   | drop_init xs = xs;
  1424 
  1425 fun get_tr i (b,p) q (Nd (po, nds)) =
  1426 
  1427     [if  i <= 0 then b else []] 
  1428     @ (writeln("get_tr: i="^(string_of_int i)^", b="^(ints2str' b)^", p="^
  1429 	       (ints2str' p)^", q="^(ints2str' q));
  1430 
  1431        gets_tr (i-1) (b@[hdp p], tlp p) (tlq q) (take_be (hdp p) (hdq q) nds))
  1432 
  1433 and gets_tr i (b,p) q [] = ((*writeln"gets_tr: -------------------------[]";*)
  1434 			    [])
  1435 
  1436   | gets_tr i (b,p) q (nd::nds) = 
  1437 
  1438     (writeln("gets_tr: i="^(string_of_int i)^", b="^(ints2str' b)^", p="^
  1439 		   (ints2str' p)^", q="^(ints2str' q));
  1440 
  1441     (get_tr i (b,p) q nd) @ (gets_tr ~99999 (lev_on b, p) q nds)
  1442     );
  1443 
  1444 fun get_trace pt p q =
  1445     (drop_init o
  1446      (gets_tr ((length p) -1) ([hdp p], tlp p) (tlq q))) 
  1447 	    (take_be (hdp p) (hdq q) (children pt));
  1448 
  1449 in
  1450 
  1451 (*.get an 'interval' from ptree down to a certain level.*)
  1452 fun get_tr f l i (b:pos, p:pos) (q:pos) (Nd (po, nds)) =
  1453     if 0 < l then 
  1454 	[if i<=0 then [(b, f po)] else [(*during initial recursive descent*)]]
  1455 	@ (gets_tr f (l-1) (i-1) (b@[hdp p]:pos, tlp p:pos) (tlq q:pos) 
  1456 		   (take_be (hdp p) (hdq q) nds))
  1457     else []
  1458 and gets_tr f l i (b,p) q [] = []
  1459   | gets_tr f l i (b,p) q (nd::nds) = 
  1460     (get_tr f l i (b,p) q nd) @ (gets_tr f l ~99999 (lev_on b, p) q nds);
  1461 (*val get_tr = 
  1462     fn : (ppobj -> 'a)      extracts info
  1463          int ->             print_depth
  1464 	 int ->             level count*~1 for initial recursive descent
  1465 	 pos * 	            current position
  1466 	 pos ->             preview to levels lower than current pos
  1467 	 pos ->             end pos (static)
  1468 	 ptree -> 
  1469   * done by recursive descent over the whole ptree.
  1470   * only the relevant nodes are taken at each level.
  1471   * heads of pos dropped at each step to lower level.
  1472   * pos going deeper than initial p q are handled by special hd tl.
  1473   *)    
  1474 fun get_trace pt (p:pos) (q:pos) f l =
  1475     (flat o drop_init o
  1476      (gets_tr f l ((length p) -1) ([hdp p], tlp p) (tlq q))) 
  1477 	(take_be (hdp p) (hdq q) (children pt));
  1478 end (*local*)
  1479 (* mit rlang.sml 55b ... End_Proof
  1480 > fun f _ = "";
  1481 > get_trace pt [4,3] [5] f 99999;
  1482 [[4,3],[4,4],[4,5],[4,5,1],[4,5,2],[4,5,3],[4,5,4],[4,5,5],[5]]
  1483 > get_trace pt [4,3] [5] f 2;
  1484 [([4,3],""),([4,4],""),([4,5],""),([5],"")]
  1485 
  1486 > get_trace pt [4,3] [4,5,1] f 99999;
  1487 [[4,3],[4,4],[4,5],[4,5,1]]
  1488 
  1489 > get_trace pt [] [4,5,1] f 99999;
  1490 [[1],[2],[3],[4],[4,1],[4,2],[4,3],[4,4],[4,5],[4,5,1]]
  1491 
  1492 > get_trace pt [] [] f 99999;
  1493 [[1],[2],[3],[4],[4,1],[4,2],[4,3],[4,4],[4,5],[4,5,1],[4,5,2],[4,5,3],
  1494  [4,5,4],[4,5,5],[5]]
  1495 > get_trace pt [] [] f 2;
  1496 [[1],[2],[3],[4],[4,1],[4,2],[4,3],[4,4],[4,5],[5]]
  1497 
  1498 > get_trace pt [4,5,5] [] f 99999;
  1499 [([4,5,5],""),([5],"")]
  1500 
  1501 > get_trace pt [4,5,3] [4,5,3] f 99999;
  1502 [([4,5,3],"")]
  1503 *)
  1504 
  1505 fun get_somespec ((dI,pI,mI):spec) ((dI',pI',mI'):spec) =
  1506     let val domID = if dI = e_domID
  1507 		    then if dI' = e_domID 
  1508 			 then raise error"pt_extract: no domID in probl,origin"
  1509 			 else dI'
  1510 		    else dI
  1511 	val pblID = if pI = e_pblID
  1512 		    then if pI' = e_pblID 
  1513 			 then raise error"pt_extract: no pblID in probl,origin"
  1514 			 else pI'
  1515 		    else pI
  1516 	val metID = if mI = e_metID
  1517 		    then if pI' = e_metID 
  1518 			 then raise error"pt_extract: no metID in probl,origin"
  1519 			 else mI'
  1520 		    else mI
  1521     in (domID, pblID, metID):spec end;
  1522 
  1523 (*extract a formula or model from ptree for itms2itemppc or model2xml*)
  1524 datatype ptform =
  1525 	 Form of term          (**)
  1526        | ModSpec of 
  1527 	 pos_ *                (*model belongs to Problem | Method*)
  1528 	 term *                (*header: Problem... or Cas*)       
  1529 	 itm list *            (*model: given, find, relate*)
  1530 	 ((bool * term) list) *(*model: preconds*)
  1531 	 spec;                 (*specification*)
  1532 val e_ptform = Form e_term;
  1533 val e_ptform' = ModSpec (Und, e_term, [e_itm], [(false, e_term)], e_spec);
  1534 
  1535 fun pt_model (PblObj {meth,spec,origin=(_,spec'),...}) Met =
  1536     let val (_,_,metID) = get_somespec spec spec'
  1537 	val {prls,pre=where_,...} = get_met metID
  1538 	val (head, pre) = head_precond spec None prls where_ meth 0
  1539     in ModSpec (Met, head, meth, pre, spec) end
  1540   | pt_model (PblObj {probl,spec,origin=(_,spec'),...}) _(*Frm,Pbl*) =
  1541     let val (_,pblID,_) = get_somespec spec spec'
  1542 	val {prls,where_,cas,...} = get_pbt pblID
  1543 	val (head, pre) = head_precond spec cas prls where_ probl 0
  1544     in ModSpec (Pbl, head, probl, pre, spec) end;
  1545 
  1546 
  1547 fun pt_form (PrfObj {form,...}) = Form form
  1548   | pt_form (PblObj {probl,spec,origin=(_,spec'),...}) =
  1549     let val (dI, pI, _) = get_somespec spec spec'
  1550 	val {cas,...} = get_pbt pI
  1551     in case cas of
  1552 	   None => Form (pblterm dI pI)
  1553 	 | Some t => Form (subst_atomic (mk_env probl) t)
  1554     end;
  1555 
  1556 fun pt_result (PrfObj {result=(t,_),...}) = Form t
  1557   | pt_result (PblObj {result=(t,_),...}) = Form t;
  1558 
  1559 type asms = (term * pos) list;
  1560 
  1561 (*fun pt_extract (pt,(p,Frm):pos') = 
  1562     let val f = get_obj pt_form pt p
  1563     in (f, Tac"", []:asms, Sundef) end
  1564   | pt_extract (pt,(p,Res)) =
  1565     let val f = get_obj pt_result pt p
  1566     in (f, Tac"", []:asms, Sundef) end
  1567   | pt_extract (pt,(p,p_)) =
  1568     let val ppobj = get_obj I pt p
  1569 	val f = if is_pblobj ppobj then pt_model ppobj p_
  1570 		else raise PTREE ("pt_extrac: tries to fetch modspec from "^
  1571 				  pos'2str (p,p_))
  1572     in (f, Tac"", []:asms, Sundef) end;*)
  1573 (* val (pt,(p,p_)) = (pt, ip);
  1574    *)
  1575 fun pt_extract (pt,(p,Res)) =
  1576     let val f = get_obj pt_result pt p
  1577     in (f, Tac"", []:asms, Sundef) end
  1578   | pt_extract (pt,(p,p_)) =
  1579     let val ppobj = get_obj I pt p
  1580 	val f = if is_pblobj ppobj then pt_model ppobj p_
  1581 		else get_obj pt_form pt p
  1582     in (f, Tac"", []:asms, Sundef) end;
  1583 
  1584 
  1585 
  1586 
  1587 fun is_pblnd (Nd (ppobj, _)) = is_pblobj ppobj;
  1588 
  1589 
  1590 (**.functions for the 'ptree iterator' as seen from the FE-KI interface.**)
  1591 
  1592 (*.move one step down into existing nodes of ptree; regard TransitiveB.*)
  1593 fun move_dn _ (Nd (c, ns)) ([],p_) = (*root problem*)
  1594 (* val (Nd (c, ns), ([],p_)) = (pt, get_pos cI uI);
  1595    *)
  1596     if is_pblobj c 
  1597     then case p_ of Frm => ([], Pbl) 
  1598 		  | Res => raise PTREE "end of calculation"
  1599 		  | _ => if null ns (*go down from Pbl + Met*)
  1600 			 then raise PTREE "solve problem not started"
  1601 			 else ([1], Frm)
  1602     else raise PTREE "pos not existent 1"
  1603 
  1604   (*iterate towards end of pos*)
  1605   | move_dn P  (Nd (_, ns)) (p::(ps as (_::_)),p_) =
  1606     if p > length ns then raise PTREE "pos not existent 2"
  1607     else move_dn ((P@[p]): pos) (nth p ns) (ps, p_)
  1608 
  1609   | move_dn P (Nd (c, ns)) ([p], p_) = (*act on last element of pos*)
  1610     if p > length ns then raise PTREE "pos not existent 3"
  1611     else if is_pblnd (nth p ns)  then
  1612 	(writeln("### move_dn: is_pblnd (nth p ns), P= "^ints2str' P^", \n"^
  1613 		 "length ns= "^((string_of_int o length) ns)^
  1614 		 ", p= "^string_of_int p^", p_= "^pos_2str p_);
  1615 	 case p_ of Frm => ((P@[p], Pbl) : pos')
  1616 		 | Res => if p = length ns 
  1617 			  then 
  1618 			      if g_ostate c = Complete then (P, Res)
  1619 			      else raise PTREE "parent not complete"
  1620 			  (*FIXME here handle not-sequential branches*)
  1621 			  else 
  1622 			      if g_branch c = TransitiveB 
  1623 				 andalso (not o is_pblnd o (nth (p+1))) ns
  1624 			      then (P@[p+1], Res) else (P@[p+1], Frm)
  1625 		 | _ => if (null o children o (nth p)) ns (*go down from Pbl*)
  1626 			then raise PTREE "solve subproblem not started"
  1627 			else (P @ [p, 1], Frm)
  1628 			     )
  1629     else case p_ of Frm => if (null o children o (nth p)) ns then (P@[p],Res) 
  1630 			   else (P @ [p, 1], Frm) (*go down*)
  1631 		  | Res => if p = length ns 
  1632 			   then 
  1633 			      if g_ostate c = Complete then (P, Res)
  1634 			      else raise PTREE "parent not complete"
  1635 			   else 
  1636 			       if g_branch c = TransitiveB 
  1637 				  andalso (not o is_pblnd o (nth (p+1))) ns
  1638 			       then (P@[p+1], Res) else (P@[p+1], Frm);
  1639 
  1640 (*.go one level down into ptree; goes to Frm.*)
  1641 fun movelevel_dn [] (Nd (c, ns)) ([],p_) = (*root problem*)
  1642     if is_pblobj c 
  1643     then if null ns 
  1644 	 then raise PTREE "solve problem not started"
  1645 	 else ([1], Frm)
  1646     else raise PTREE "pos not existent 1"
  1647 
  1648   (*iterate towards end of pos*)
  1649   | movelevel_dn P (Nd (_, ns)) (p::(ps as (_::_)),p_) =
  1650     if p > length ns then raise PTREE "pos not existent 2"
  1651     else movelevel_dn (P@[p]) (nth p ns) (ps, p_)
  1652 
  1653   | movelevel_dn P (Nd (c, ns)) ([p], p_) = (*act on last element of pos*)
  1654     if p > length ns then raise PTREE "pos not existent 3"
  1655     else case p_ of Frm => if (null o children o (nth p)) ns 
  1656 			   then raise PTREE "no children"
  1657 			   else (P @ [p, 1], Frm) (*go down*)
  1658 		  | Res => if p = length ns 
  1659 			   then raise PTREE "no children"
  1660 			   else 
  1661 			       if g_branch c = TransitiveB
  1662 			       then if (null o children o (nth (p+1))) ns
  1663 				    then raise PTREE "no children"
  1664 				    else (P@[p+1, 1], Frm)
  1665 			       else if (null o children o (nth p)) ns
  1666 				    then raise PTREE "no children"
  1667 				    else (P@[p, 1], Frm);
  1668 
  1669 (*.go to the previous position in ptree; regard TransitiveB.*)
  1670 fun move_up _ (Nd (c, ns)) (([],p_):pos') = (*root problem*)
  1671     if is_pblobj c 
  1672     then case p_ of Frm => raise PTREE "begin of calculation"
  1673 		  | Pbl => ([], Frm) | Met => ([], Pbl)
  1674 		  | Res => if null ns then ([], Pbl) (*Res -> Pbl (not Met)!*)
  1675 			   else ([length ns], Res)
  1676     else raise PTREE "pos not existent"
  1677 
  1678   | move_up P  (Nd (_, ns)) (p::(ps as (_::_)),p_) = (*iterate to end of pos*)
  1679     if p > length ns then raise PTREE "pos not existent"
  1680     else move_up (P@[p]) (nth p ns) (ps,p_)
  1681 
  1682   | move_up P (Nd (c, ns)) ([p], p_) = (*act on last element of pos*)
  1683     if p > length ns then raise PTREE "pos not existent"
  1684     else if is_pblnd (nth p ns)  then
  1685 	case p_ of Frm => if p = 1 then (P, Pbl) else (P@[p-1], Res)
  1686 		  | Pbl => (P@[p], Frm) | Met => (P@[p], Pbl)
  1687 		  | Res => 
  1688 		    let val nc = (length o children o (nth p)) ns
  1689 		    in if nc = 0 then (P@[p], Pbl) (*Res -> Pbl (not Met)!*)
  1690 		       else (P @ [p, nc], Res) end (*go down*)
  1691     else case p_ of Frm => if p <> 1 then (P, Frm) 
  1692 			  else if is_pblobj c then (P, Pbl) else (P, Frm)
  1693 		  | Res => 
  1694 		    let val nc = (length o children o (nth p)) ns
  1695 		    in if nc = 0 (*cannot go down*)
  1696 		       then if g_branch c = TransitiveB andalso p <> 1
  1697 			    then (P@[p-1], Res) else (P@[p], Frm)
  1698 		       else (P @ [p, nc], Res) end; (*go down*)
  1699 
  1700 (*.go one level up in ptree; sets the position on Frm.*)
  1701 fun movelevel_up _ (Nd (c, ns)) (([],p_):pos') = (*root problem*)
  1702     raise PTREE "pos not existent"
  1703 
  1704   (*iterate towards end of pos*)
  1705   | movelevel_up P  (Nd (_, ns)) (p::(ps as (_::_)),p_) = 
  1706     if p > length ns then raise PTREE "pos not existent"
  1707     else movelevel_up (P@[p]) (nth p ns) (ps,p_)
  1708 
  1709   | movelevel_up P (Nd (c, ns)) ([p], p_) = (*act on last element of pos*)
  1710     if p > length ns then raise PTREE "pos not existent"
  1711     else if is_pblobj c then (P, Pbl) else (P, Frm);
  1712 
  1713 
  1714 
  1715 
  1716 
  1717 
  1718