# Tag Info

39

Link to the code on GitHub I have been using this. It's mostly Leonid's code from the stackoverflow question you linked to, but it uses Definition instead of DownValues. Symbol names are printed without any context, but the full symbol name is put into a Tooltip so you can always find out what context a symbol is in. Update FullDefinition[symbol] claims ...

29

The answer of @R.M. already explains the essence of the problem. You can streamline the process of removing the Combinatorica from the $ContextPath by loading it via Block[{$ContextPath}, Needs["Combinatorica"]] (or use Get intead of Needs, although Needs is a preferred way to load a package). In this way, you don't have to do anything afterwards, ...

26

The red colouring indicates shadowing — i.e., when a symbol originally in a particular context, is exposed to the current context path, thereby clashing with another symbol of the same name in a different context, also on the context path. Example of shadowing: Here is a short example that demonstrates this. Try it out in a fresh kernel (call Quit[] ...

26

Symbols are created in the current context during parsing. This should not be a problem in normal circumstances as the symbols are merely "initialized" without values or properties. See these posts for more information: Is it possible to use Begin and End inside a Manipulate? Why doesn't this use of Begin[] work? When does Mathematica create a new ...

19

Shadowing occurs only when there are two functions with the same name that are in $ContextPath. So right after you do <<Combinatorica, do the following:$ContextPath = Rest@$ContextPath; What this does is that it removes Combinatorica (which is the package you just loaded). Now the only Graph function that's on the path is SystemGraph and you can ... 19 Short answer: yes, it is possible. The problem is that parsing is done line-by-line only for the top-level code. For code inside some head(s), it is first parsed as a whole. Therefore, your f is parsed to Globalf, and this is why that symbol is used. Here is what you can do, schematically: DynamicModule[{x = 5}, With[{def = MakeBoxes[f[y_] := y^2 + 1;], ... 13 I am not sure these are the best ways but they should work. You could do what you did with Dimensions for all the symbols in Combinatorica For example, running this replaceAndLoad[context_String -> toContext_String] := Block[{$ContextPath}, Needs[context]; Scan[ToExpression[ toContext <> StringReplace[#, context ~~ sym__ :> sym] ...

13

I can now offer a solution which leverages the full power of the code formatter, in its new, more robust form. Load the formatter: Import["https://raw.github.com/lshifr/CodeFormatter/master/CodeFormatter.m"] Some examples: CodeFormatterSpelunk[RunThrough] CodeFormatterSpelunk[PacletManagerCreatePaclet] In the last example, using MakeBoxes ...

13

Here I offer the safe version of Get that can be used successively to collect all the source files and contexts of packages without polluting the memory (too much). What it does I have practically reverse-engineered all the necessary functions (Get, Needs, BeginPackage, Begin, EndPackage and End) so that I could inject the monitoring code for ...

13

It appears since version 3 shadowed variables are given priority, as demonstrated below. In the temp context x is taken as tempx unless Globalx exists. Remove[tempx, Globalx] tempx = 6; Begin["temp"]; {x, Globalx} End[]; {6, x} Remove[tempx, Globalx] tempx = 6; x = 5; Begin["temp"]; {x, Globalx} End[]; {5, 5}

12

I would just use strings, for all their fragility: ClearAll[print]; print[sym_, {conts_String}] := With[{altptrn = Alternatives @@ Reverse[SortBy[{conts}, StringLength]]}, Print@StringReplace[ToString[InputForm@FullDefinition@sym], (x : (_ | "") ~~ altptrn ~~ y : (_ | "")) /; ! (x === "\"" && y === "\"") :> ...

12

Your specific problem looks like you somehow managed to not load the package properly (did you evaluate Get[...]?). There's also an excess space in your long-form call to f (just before EndPackage[]) that will give you an error. Although your package will work if you fix the typo, this is not in general a good way to define your function. To see why, try: ...

11

One possibility would be to modify the notebook's stylesheet so that Input cells (or a clone) have something like: CellProlog -> ($ContextPath = DeleteDuplicates[Prepend[$ContextPath,"abc"]]) Then your package's context should be available to all the code:

11

Not an answer. Just screen shot. I booted up version 2.2 to verify the book result. And it does verify. Something changed since the book was written !

11

This is because of $ContextPath. If the symbol outside the Begin/End is created in a context that is listed in$ContextPath, Mathematica will recognize it as belonging to that context even when called from a different context (even if Begin/End is put in a package file and called via Get): $ContextPath {"DocumentationSearch", "ResourceLocator", ... 11 Using Begin and End won't help, because .mx files are lower-level and the way they are loaded is different from normal packages. I was about to say that this isn't possible, but here is a hack which seems to work: ClearAll[loadInContext]; loadInContext[context_String,file_String/;FileExtension[file]==="mx"]:= Module[{tag}, ... 10 This question is not too distantly related to my own on StackOverflow: Exposing Symbols to$ContextPath Leonid provides an interesting approach there that could be adapted to your problem. Rojo provides a solution that creates "proxy symbols" but these are not 100% equivalent. Information no longer works correctly for example: ?? ComKSubsets This ...

10

I don't have an access to Mathematica at the moment, so what follows is untested. What you observed can be understood by looking at the mechanics of package loading and symbols creation. I dealt with this problem before, and will re-post here verbatim my answer from the MathGroup thread: The setup and the problem Suppose you have two packages, the main ...

10

Well, here is a suggestion: you can overload Needs using Villegas-Gayley trick. To do this safely, here is a generator for local environments, where Needs will be overloaded: createTraceEnvironment[context_String]:= Module[{inNeeds}, Function[ code, InternalInheritedBlock[ ...

10

This is possible in the interactive session with $PreRead. I will adopt my solution to the same problem posted in this Mathgroup thread. To quote my explanation from there, the essence of the present solution is to delay the parsing of the code (body) that must be executed inside a given context until run-time, that is, replace code ... 10 Since nobody has mentioned it yet... V8 introduced the undocumented flag Debug$ExamineCode. When it is set to true, the information functions will display the definitions of ReadProtected symbols: Debug$ExamineCode = True ??BinLists It is sometimes useful to suppress some of the internal package names to make it easier to scan the definitions. Here ... 9 I think, using contexts here is a sensible suggestion, particularly because you want to use several variables. One possible alternative is to set up a struct-like data structure, where encapsulation mechanism is based on Module-generated persistent variables. There were many discussions related to emulation of structs in Mathematica, but, given the ... 9 This behaviour has changed since that book was published. I am writing this additional answer to make it clear how Mathematica 9 searches contexts for symbols and that even the current version 9 documentation is incorrect in describing this. How symbol lookup actually works When you enter a symbol name such as x, Mathematica will check if a symbol with ... 9 One thing that might help you when investigating in such issues is the LinkSnooper. This is a java program that can be used to set up an additional kernel configuration. When you use this kernel, you can watch all traffic between front-end and kernel. With this, you could look what happens if you evaluate a simple 1+1 FE ---> K: ... 8 The issue here is that you haven't actually defined myContextx, just plain old x. It is possible to access global variables within a context (that's exactly what happens when you use built-in function within a package), and that is what you have done. Clear[fun]; fun[] := Module[{}, Begin["myContext"]; x = 1; End[];]; fun[] This is undefined. ... 8 After running your Package code the first time Test1 is added to the$ContextPath. When you call Test1function1[5] the symbol function1 is created in the context Test1 After calling BeginPackage["Test1"] and Begin["Private"] the $ContextPath is: {"Test1","System"} When you use function1 in function1[x_] := x it is found in the$ContextPath and ...

7

I have not tested this yet but here is one possible approach: contextFreeDefinition[sym_Symbol, contexts_List] := InternalInheritedBlock[{sym}, ClearAttributes[sym, ReadProtected]; If[contexts =!= {}, Message[contextFreeDefinition::contexts, contexts]]; Block[{ipf = ToString @ InputForm @ FullDefinition @ sym}, ipf = ...

7

The iterator variable in the package lives in the context that your package sets (in this case, mypackagePrivate`), but the call to ParallelTable from inside your package does not distribute the definitions in your package, because the parallel functions only distribute contexts that are listed in $DistributedContexts. This is by default set to$Context, ...

Only top voted, non community-wiki answers of a minimum length are eligible