/**  XIso **/
/*A: K McIsaac*/
/*S: University of Western Australia, Nedlands 6009, Australia*/
/*D: December, 1986 */
/*K: Simplify */
/*B: Has work around for bug in At */
/*U: Feburary 1988 McIsaac.
	Added break function */
/*W: % and @ refer to the lines in the interactive procedure and not the
lines in the top level as described in the manual.*/

/*:  Iso[$exp,($partspec:{{1},{2},...{Len[$exp]}})]
	An interactive version of At. An interactive procedure is created
	for each sub-expression denoted by $partspec. %O[0] is given the
	value of the current expression to be studied. When the interactive 
	procedure is left the returned value is substituted for the 
	specified part. To leave an interactive session use ctl-D or
	Ret[]

	The default for $partspec is to isolate each term of the top 
	projection. If a list of parts is given then each part is visited 
	in turn. If a pattern is given then each expression matching the 
	pattern is visited in turn. 
	
	The keyword break will exit to the previous level.  The symbols
	% and @ have their normal action, contary to the SMP
	documentation for an interactive procedure.*/

<<XStr0
<<XStatus

/* Break[($levels:1)]
	Break $lev levels upwards */
Brea_:Tier;
Break[] :: Break[1]
Break[$lev] :: (Do[i,1,$lev,(PopS[];Dec[%level])];\
		Ret[,4*$lev])

Isolate_:Iso_:Tier;

/*:  Isolate does the dirty work */
Isolate[$x] :: \
	(Lcl[%t,%,Post,#O,#I,Init];\
	 #O[$$1] : %O[$$1]; #I[$$1] : %I[$$1];\
	 PushS[CJoin["Level ",%level,", term ",Inc[%term]," of ", %nterms]];\
	 Post :: (%:$1); % : Init : $x;\
	 Get[]; %retns : %%;\
	 PopS[];%retns)


%level : 0;
Iso[$x,$pos_=Listp[$pos]] :: (Lcl[%tt,%term,%nterms]; \
			      %term : 0;%nterms : Len[$pos];\
	      		      Inc[%level];\
			      %tt : At[Isolate[$1],$x,$pos];\
			      Dec[%level];\
			      %tt)

Iso[$x] :: Iso[$x,Ar[Len[$x],{$1}]]

/* Workaround for at. At[f,a-b,Pos[b,a-b]]  = a+f[b] because Pos returns
{{2,0}} rather than {{2}} */
Iso[$x,$fn] :: Iso[$x,pos[$fn,$x]]
<XList0
pos[$tmpl, $exp] :: \
	Map[If[Len[Proj[$exp,Abl[$1]]]=0,Abl[$1],$1],Pos[$tmpl,$exp]]
Abl[$x] :: Tk[Len[$x]-1,$x]

/*E:
SMP 1.5.0   (May 14 1986)
Thu Feb 12 19:21:51 1987


#I[1]::  <XIso

#I[2]::  Iso[a+b+c,{{2}}]

Entering level 1

%I[1]::  %

%O[1]:   b

%I[2]::  %^(i+j)

	  i + j
%O[2]:   b

%I[3]::  Ret[]

Leaving level 1

		  i + j
#O[2]:   a + c + b

#I[3]::  Iso[f[f[a]+f[b]+f[c]],f[$]]

Entering level 1

%I[1]::  %

%O[1]:   f[a]

%I[2]::  a1

%O[2]:   a1

%I[3]::  Ret[]

Leaving level 1
Entering level 1

%I[1]::  %

%O[1]:   f[b]

%I[2]::  b1

%O[2]:   b1

%I[3]::  Ret[]

Leaving level 1
Entering level 1

%I[1]::  %

%O[1]:   f[c]

%I[2]::  c1

%O[2]:   c1

%I[3]::  Ret[]

Leaving level 1
Entering level 1

%I[1]::  %+x

%O[1]:   x + f[a1 + b1 + c1]

%I[2]::  Ret[]

Leaving level 1

#O[3]:   x + f[a1 + b1 + c1]

#I[4]::  <end>
*/
