src/sml/ME/ctreeNEW.sml
author wneuper
Fri, 04 Mar 2005 19:02:06 +0100
changeset 2146 de62f4a39c04
parent 2144 aa5ec618bba5
permissions -rw-r--r--
sml-050304b-inform: all tests ok except 3 in auto-inform.sml
     1 (* use"../ME/ctreeNEW.sml";
     2    use"ME/ctreeNEW.sml";
     3    use"ctreeNEW.sml";
     4    *)
     5 
     6 
     7 (*.get all positions in a ptree until ([],Res) or ostate=Incomplete
     8 val get_allp = fn : 
     9   pos' list -> : accumulated, start with []
    10   pos ->       : the offset for subtrees wrt the root
    11   ptree ->     : (sub)tree
    12   pos'         : initialization (the last pos' before ...)
    13   -> pos' list : of positions in this (sub) tree (relative to the root)
    14 .*)
    15 (*###################################################################*)
    16 (* val (cuts, P, pt, pos) = ([], [3], get_nd pt [3], ([], Frm):pos');
    17    val (cuts, P, pt, pos) = ([], [2], get_nd pt [2], ([], Frm):pos');
    18    length (children pt);
    19    *)
    20 fun get_allp (cuts:pos' list) (P:pos, pos:pos') pt =
    21     (let val nxt = move_dn [] pt pos (*exn if Incomplete reached*)
    22      in if nxt <> ([],Res) 
    23 	then get_allp (cuts @ [nxt]) (P, nxt) pt
    24 	else (map (apfst (curry op@ P)) (cuts @ [nxt])): pos' list
    25      end) handle PTREE _ => (map (apfst (curry op@ P)) cuts);
    26 (*###################################################################*)
    27 
    28 
    29 
    30 (*----OLD-----*)
    31 fun get_allps (cuts: pos' list) (P:pos) [] = cuts
    32   | get_allps cuts P (pt::pts) =
    33     let val cuts' = 
    34 	    get_allp [] (P, ([], Frm)) pt
    35     in get_allps (cuts @ cuts') (lev_on P) pts end;
    36 
    37 print_depth 11;
    38 (* val (cuts: pos' list, P, pt::pts) = ([], [1], children pt);
    39    val cuts' = get_allp [] P pt ([], Frm);
    40    val (cuts: pos' list, P, pt::pts) = (cuts @ cuts', lev_on P, pts);
    41    val cuts' = get_allp [] P pt ([], Frm);
    42    length (children pt);
    43    *)
    44 (*the pts are assumed to be on the same level*)
    45 fun get_allps (cuts: pos' list) (P:pos) [] = cuts
    46   | get_allps cuts P (pt::pts) =
    47     let val below = get_allp [] (P, ([], Frm)) pt
    48 	val levfrm = 
    49 	    if is_pblnd pt 
    50 	    then (P, Pbl)::below
    51 	    else if last_elem P = 1 
    52 	    then (P, Frm)::below
    53 	    else (*Trans*) below
    54 	val levres = levfrm @ (if null below then [(P, Res)] else [])
    55     in get_allps (cuts @ levres) (lev_on P) pts end;
    56 
    57 
    58 
    59 print_depth 11; move_dn; print_depth 3;
    60 print_depth 11; get_allpos'; print_depth 3;
    61 
    62 
    63 (**.these 2 funs decide on how far cut_tree goes.**)
    64 (*.shall the nodes _after_ the pos to be inserted at be deleted?.*)
    65 fun test_trans (PrfObj{branch = Transitive,...}) = true
    66   | test_trans (PrfObj{branch = NoBranch,...}) = true
    67   | test_trans (PblObj{branch = Transitive,...}) = true 
    68   | test_trans (PblObj{branch = NoBranch,...}) = true 
    69   | test_trans _ = false;
    70 (*.shall cutting be continued on the higher level(s)?
    71    the Nd regarded will NOT be changed.*)
    72 fun cutlevup (PblObj _) = false (*for tests of LK0502*)
    73   | cutlevup _ = true;
    74     
    75 (*cut_bottom new S(603)..(608)
    76 cut the level at the bottom of the pos (used by cappend_...)
    77 and handle the parent in order to avoid extra case for root
    78 fn: ptree ->         : the _whole_ ptree for cut_levup
    79     pos * posel ->   : the pos after split_last
    80     ptree ->         : the parent of the Nd to be cut
    81 return
    82     (ptree *         : the updated ptree
    83      pos' list) *    : the pos's cut
    84      bool            : cutting shall be continued on the higher level(s)
    85 *)
    86 (**############# 2nd get_allp finished############################**)
    87 print_depth 99;
    88 fun cut_bottom _ (pt' as Nd (b, [])) = ((pt', []), cutlevup b)
    89   | cut_bottom (P:pos, p:posel) (Nd (b, bs)) =
    90     let (*divide level into 3 parts...*)
    91 	val keep = take (p - 1, bs)
    92 	val pt' as Nd (_,bs') = nth p bs
    93 	(*^^^^^_here_ will be 'insert'ed by 'append_..'*)
    94 	val (tail, tp) = (takerest (p, bs), 
    95 			  if null (takerest (p, bs)) then 0 else p + 1)
    96 	val (children, cuts) = 
    97 	    if test_trans b
    98 	    then (keep,
    99 		  (if is_pblnd pt' then [(P @ [p], Pbl)] else [])
   100 		  @ (get_allp  [] (P @ [p], (P, Frm)) pt')
   101 		  @ (get_allps [] (P @ [p+1]) tail))
   102 	    else (keep @ [(*'insert'ed by 'append_..'*)] @ tail,
   103 		  get_allp  [] (P @ [p], (P, Frm)) pt')
   104 	val (pt'', cuts) = 
   105 	    if cutlevup b
   106 	    then (Nd (del_res b, children), 
   107 		  cuts @ (if g_ostate b = Incomplete then [] else [(P,Res)]))
   108 	    else (Nd (b, children), cuts)
   109 	(*val _= writeln("####cut_bottom (P, p)="^pos2str (P @ [p])^
   110 		       ", Nd=.............................................")
   111 	val _= show_pt pt''
   112 	val _= writeln("####cut_bottom form='"^
   113 		       term2str (get_obj g_form pt'' []))
   114 	val _= writeln("####cut_bottom cuts#="^string_of_int (length cuts)^
   115 		       ", cuts="^pos's2str cuts)*)
   116     in ((pt'', cuts:pos' list), cutlevup b) end;
   117 print_depth 3;
   118 (**######################################################################**);
   119 (*.go all levels from the bottom of 'pos' up to the root, 
   120  on each level compose the children of a node and accumulate the cut Nds
   121 args
   122    pos' list ->      : for accumulation
   123    bool -> 	     : cutting shall be continued on the higher level(s)
   124    ptree -> 	     : the whole ptree for 'get_nd pt P' on each level
   125    ptree -> 	     : the Nd from the lower level for insertion at path
   126    pos * posel ->    : pos=path split for convenience
   127    ptree -> 	     : Nd the children of are under consideration on this call 
   128 returns		     :
   129    ptree * pos' list : the updated parent-Nd and the pos's of the Nds cut
   130 .*)
   131 print_depth 99;
   132 fun cut_levup (cuts:pos' list) clevup pt pt' (P:pos, p:posel) (Nd (b, bs)) =
   133     let (*divide level into 3 parts...*)
   134 	val keep = take (p - 1, bs)
   135 	(*val pt' comes as argument from below*)
   136 	val (tail, tp) = (takerest (p, bs), 
   137 			  if null (takerest (p, bs)) then 0 else p + 1)
   138 	val (children, cuts') = 
   139 	    if clevup
   140 	    then (keep @ [pt'], get_allps [] (P @ [p+1]) tail)
   141 	    else (keep @ [pt'] @ tail, [])
   142 	val clevup' = if clevup then cutlevup b else false 
   143 	(*the first Nd with false stops cutting on all levels above*)
   144 	val (pt'', cuts') = 
   145 	    if clevup'
   146 	    then (Nd (del_res b, children), 
   147 		  cuts' @ (if g_ostate b = Incomplete then [] else [(P,Res)]))
   148 	    else (Nd (b, children), cuts')
   149 	(*val _= writeln("#####cut_levup clevup= "^bool2str clevup)
   150 	val _= writeln("#####cut_levup cutlevup b= "^bool2str (cutlevup b))
   151 	val _= writeln("#####cut_levup (P, p)="^pos2str (P @ [p])^
   152 		       ", Nd=.............................................")
   153 	val _= show_pt pt''
   154 	val _= writeln("#####cut_levup form='"^
   155 		       term2str (get_obj g_form pt'' []))
   156 	val _= writeln("#####cut_levup cuts#="^string_of_int (length cuts)^
   157 		       ", cuts="^pos's2str cuts)*)
   158     in if null P then (pt'', (cuts @ cuts'):pos' list)
   159        else let val (P, p) = split_last P
   160 	    in cut_levup (cuts @ cuts') clevup' pt pt'' (P, p) (get_nd pt P)
   161 	    end
   162     end;
   163 print_depth 3;
   164 (**######################################################################**);
   165 (* use"../ME/ctreeNEW.sml";
   166    use"ME/ctreeNEW.sml";
   167    use"ctreeNEW.sml";
   168    *)
   169 print_depth 99;
   170 fun cut_tree pt (pos,_) =
   171     if not (existpt pos pt) 
   172     then (pt,[]) (*appending a formula never cuts anything*)
   173     else let val (P, p) = split_last pos
   174 	     val ((pt', cuts), clevup) = cut_bottom (P, p) (get_nd pt P)
   175 	 (*        pt' is the updated parent of the Nd to cappend_..*)
   176 	 in if null P then (pt', cuts)
   177 	    else let val (P, p) = split_last P
   178 		 in cut_levup cuts clevup pt pt' (P, p) (get_nd pt P)
   179 		 end
   180 	 end;
   181 print_depth 3;
   182 
   183 
   184 
   185 (*######## eval from here ##########################################**)
   186 (*######## eval from here ##########################################**)
   187 (*######## eval from here ##########################################**)
   188 
   189 fun append_atomic p l f r f' s pt = 
   190     let (*val _= writeln("##append_atomic: pos ="^pos2str p^
   191 	       ", Nd=.............................................")
   192 	val _= show_pt pt*)
   193 	val (iss, f) = if existpt p pt andalso get_obj g_tac pt p=Empty_Tac
   194 		       then (*after Take*)
   195 			   ((fst (get_obj g_loc pt p), Some l), 
   196 			    get_obj g_form pt p) 
   197 		       (*040223	     else ((Some l, None), f)*)
   198 		       else ((None, Some l), f)
   199     in insert (PrfObj {cell = [(*3.00. unused*)],
   200 		       form  = f,
   201 		       tac  = r,
   202 		       loc   = iss,
   203 		       branch= NoBranch,
   204 		       result= f',
   205 		       ostate= s}) pt p end;
   206     
   207 (*20.8.02: cappend_* FIXXXXME cut branches below cannot be decided here:
   208   detail - generate - cappend: inserted, not appended !!!
   209 
   210   cut decided in applicable_in !?!
   211 *)
   212 fun cappend_atomic pt p loc f r f' s = 
   213     let val _= writeln("#cappend_atomic: pos ="^pos2str p)
   214 	val (pt',c) = cut_tree pt (p,Frm)
   215 	val _= writeln("#cappend_atomic: after cut")
   216 	val pt'' = append_atomic p loc f r f' s pt'
   217     in (pt'',c) end;
   218 fun cappend_atomic pt p loc f r f' s = 
   219 (* val (pt, p, loc, f, r, f', s) = 
   220        (pt,p,l,f,Rewrite_Set_Inst (subst2subs subs',id_rls rls'),
   221 	(f',asm),Complete);
   222    *)
   223 ((*writeln("###cappend_atomic: pos ="^pos2str p);*)
   224   apfst (append_atomic p loc f r f' s) (cut_tree pt (p,Frm))
   225 );
   226 
   227 (* called by Take *)
   228 fun append_form p l f pt = 
   229 ((*writeln("###append_form: pos ="^pos2str p);*)
   230   insert (PrfObj {cell = [(*3.00. unused*)],
   231 		  form  = (*if existpt p pt 
   232 		  andalso get_obj g_tac pt p = Empty_Tac 
   233 			    (*distinction from 'old' (+complete!) pobjs*)
   234 			    then get_obj g_form pt p else*) f,
   235 		  tac  = Empty_Tac,
   236 		  loc   = (Some l, None),
   237 		  branch= NoBranch,
   238 		  result= (e_term,[]),
   239 		  ostate= Incomplete}) pt p
   240 );
   241 (* val (p,loc,f) = ([1], e_istate, str2term "x + 1 = 2");
   242    val (p,loc,f) = (fst p, e_istate, str2term "-1 + x = 0");
   243    *)
   244 fun cappend_form pt p loc f =
   245 ((*writeln("###cappend_form: pos ="^pos2str p);*)
   246   apfst (append_form p loc f) (cut_tree pt (p,Frm))
   247 );
   248 
   249 
   250     
   251 fun append_result pt p l f s =
   252 ((*writeln("###append_result: pos ="^pos2str p);*)
   253     (appl_obj (repl_result (fst (get_obj g_loc pt p),
   254 			    Some l) f s) pt p, [])
   255 );
   256 
   257 
   258 (*WN041022 deprecated, still for kbtest/diffapp.sml, /systest/root-equ.sml*)
   259 fun append_parent p l f r b pt = 
   260   let (*val _= writeln("###append_parent: pos ="^pos2str p);*)
   261     val (ll,f) = if existpt p pt andalso get_obj g_tac pt p=Empty_Tac
   262 		  then ((fst (get_obj g_loc pt p), Some l), 
   263 			get_obj g_form pt p) 
   264 		 else ((Some l, None), f)
   265   in insert (PrfObj 
   266 	  {cell = [(*unused*)],
   267 	   form  = f,
   268 	   tac  = r,
   269 	   loc   = ll,
   270 	   branch= b,
   271 	   result= (e_term,[]),
   272 	   ostate= Incomplete}) pt p end;
   273 fun cappend_parent pt p loc f r b =
   274 ((*writeln("###cappend_parent: pos ="^pos2str p);*)
   275   apfst (append_parent p loc f r b) (cut_tree pt (p,Und))
   276 );
   277 
   278 
   279 fun append_problem [] l fmz (strs,spec,hdf) _ =
   280 ((*writeln("###append_problem: pos = []");*)
   281   (Nd (PblObj 
   282 	       {cell  = [(*3.00. unused*)],
   283 		origin= (strs,spec,hdf),
   284 		fmz   = fmz,
   285 		spec  = empty_spec,
   286 		probl = []:itm list,
   287 		meth  = []:itm list,
   288 		env   = None,
   289 		loc   = (Some l, None),
   290 		branch= TransitiveB,(*FIXXXXXME.27.8.03: for equations only*)
   291 		result= (e_term,[]),
   292 		ostate= Incomplete},[]))
   293 )
   294   | append_problem p l fmz (strs,spec,hdf) pt =
   295 ((*writeln("###append_problem: pos ="^pos2str p);*)
   296   insert (PblObj 
   297 	  {cell  = [(*3.00. unused*)],
   298 	   origin= (strs,spec,hdf),
   299 	   fmz   = fmz,
   300 	   spec  = empty_spec,
   301 	   probl = []:itm list,
   302 	   meth  = []:itm list,
   303 	   env   = None,
   304 	   loc   = (Some l, None),
   305 	   branch= TransitiveB,
   306 	   result= (e_term,[]),
   307 	   ostate= Incomplete}) pt p
   308 );
   309 fun cappend_problem _ [] loc fmz ori =
   310 ((*writeln("###cappend_problem: pos = []");*)
   311   (append_problem [] loc fmz ori EmptyPtree,[])
   312 )
   313   | cappend_problem pt p loc fmz ori = 
   314 ((*writeln("###cappend_problem: pos ="^pos2str p);*)
   315   apfst (append_problem p (loc:istate) fmz ori) (cut_tree pt (p,Frm))
   316 );
   317 
   318 
   319 (* use"ME/ctreeNEW.sml";
   320    use"ctreeNEW.sml";
   321    *)
   322