MathGroup Archive 1995

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Crossreference, code documentation

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg1623] Re: Crossreference, code documentation
  • From: wagner at bullwinkle.cs.Colorado.EDU (Dave Wagner)
  • Date: Thu, 6 Jul 1995 23:53:02 -0400
  • Organization: University of Colorado, Boulder

This is a followup to my own followup on the topic of creating
function/variable cross-references.  In my last post, I noted the
difficulty of figuring out whether a variable in the Global context
was truly "global" (in the sense of not being local to a function) or
not.  An idea struck me, which goes like this:

As before, suppose we have a few "global" variables lying around.

    In[3]:=
	{a,b,c,x,y,z,f,g};

Define a function h that uses some of these.  Note that h has parameters
that have the same names as some global variables.

    In[4]:=
	h[x_] := a + f[x]
	h[x_,y_] := g[x,b] + y

    In[6]:=
	DownValues[h]
    Out[6]=
	{Literal[h[x_]] :> a + f[x], Literal[h[x_, y_]] :> g[x, b] + y}

Now, rather than just mapping FindSymbols onto the RHS of each delayed
rule, map it onto both sides.  This can be accomplished by mapping
at level 2:

    In[7]:=
	Map[FindSymbols, %, {2}]
    Out[7]=
	{{Blank, h, Literal, Pattern, x} :> FindSymbols[a + f[x]], 
	 {Blank, h, Literal, Pattern, x, y} :> FindSymbols[g[x, b] + y]}

Note that the LHS evaluated immediately, but the RHS doesn't because
of the RuleDelayed.  Change all RuleDelayed's to Lists:

    In[8]:=
	% /. RuleDelayed->List
    Out[8]=
	{{{Blank, h, Literal, Pattern, x}, {a, f, Plus, x}}, 
	  {{Blank, h, Literal, Pattern, x, y}, {b, g, Plus, x, y}}}

We want to complement each pair of sub-lists.  First we have to
reverse the order:

    In[9]:=
	Reverse /@ %
    Out[9]=
	{{{a, f, Plus, x}, {Blank, h, Literal, Pattern, x}}, 
	 {{b, g, Plus, x, y}, {Blank, h, Literal, Pattern, x, y}}}

Now complement:

    In[10]:=
	Apply[Complement, %, {1}]
    Out[10]=
	{{a, f, Plus}, {b, g, Plus}}

The result is a list of all symbols used by h THAT ARE NOT PARAMETERS.
Finally, select global symbols like this:

    In[11]:=
	Select[Union[Flatten[%]], (Context@@#=="Global`")&]
    Out[11]=
	{a, b, f, g}

This leaves the problem of eliminating local variables such as those
declared inside of Module, Block, and With statements.  This could most
easily be done by modifying FindSymbols itself so that if the head of
an expression were one of the above, descend recursively into the
second part of the expression only (e.g., in Module[{vars}, body], "body" is
the second part), and complement the result relative to all variable
names in the first part ("{vars}").  There is probably a modest amount
of work to do here and I leave it to the interested reader to smooth
out the rough edges.

		Dave Wagner
		Principia Consulting
		(303) 786-8371
		dbwagner at princon.com
		http://www.princon.com/princon


  • Prev by Date: Re: projected gradient
  • Next by Date: Mathematica Workshops
  • Previous by thread: Re: Crossreference, code documentation
  • Next by thread: Re: Crossreference, code documentation