(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 3.0, MathReader 3.0, or any compatible application. The data for the notebook starts with the line of stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 11293, 355]*) (*NotebookOutlinePosition[ 12088, 385]*) (* CellTagsIndexPosition[ 12015, 379]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["\<\ Never suffer from shadowing problems again! And you don't need to learn \ about contexts, either.\ \>", "Subtitle"], Cell["Concept and code by Matthew Cook", "SmallText"], Cell[CellGroupData[{ Cell["The code that should be in your init.m", "Section"], Cell[BoxData[ \(\(ForwardContext::usage = "\symbol, and then sets the removed symbol (which may still exist \ in definitions, such as In[5]:=...) to point to the symbol \ newcontext<>symbol. It also makes the InputForm of the old symbol (now \ removed) remain as just \"symbol\", so that the definitions don't start \ looking peculiar.\>"; \)\)], "Input", InitializationCell->True], Cell[BoxData[{ \(SetAttributes[ClearQ, {HoldFirst, Listable}]\), \(ClearQ@sym_ := Flatten[\(ToExpression[#<>"\"]@sym&\)/@{"\", "\", "\", "\", "\", "\", "\"}] === {}\), \(\(Protect@ClearQ; \)\)}], "Input", InitializationCell->True], Cell[BoxData[{\(Unprotect@Message; \nUnforwardedQ[_, _, _] := True\), \(ForwardContext[sym_String, old_String, new_String] /; UnforwardedQ[sym, old, new] := Module[{command}, UnforwardedQ[sym, old, new] = False; command := ToExpression[ "\"<>old<>sym<>"\<;\>"<>old<>sym<>"\<:=\>"<>new<>sym<> "\<;\>", InputForm, Hold]; First@command; Unprotect@Removed; Format[Removed@sym, InputForm] := Format[sym, OutputForm]; Protect@Removed]\), RowBox[{\($ShadowEntrySafe = True\), ";", "\n", RowBox[{\(Message[sym_::shdw, name_, oldcons_, newcon_]\), ":=", RowBox[{"If", "[", RowBox[{"$ShadowEntrySafe", ",", RowBox[{"With", "[", RowBox[{ \({sname = First@name, scon = First@newcon, ocon = DeleteCases[First@oldcons, First@newcon]}\), ",", RowBox[{"Block", "[", RowBox[{\({$ShadowEntrySafe = False}\), ",", RowBox[{ RowBox[{ RowBox[{"If", "[", RowBox[{ \(ToExpression["\"<>#<>sname]\), ",", \(ForwardContext[sname, #, scon]; Message[sym::fwd, name, #, newcon]\), ",", RowBox[{"Message", "[", RowBox[{ \(sym::shdwvl\), ",", "name", ",", "#", ",", "newcon", ",", RowBox[{"With", "[", RowBox[{ \({zv = sname, zo = #, zn = scon}\), ",", ButtonBox["\"\\"", ButtonFunction:>(ForwardContext[ zv, zo, zn]&), ButtonEvaluator->Automatic, Active->True]}], "]"}]}], "]"}]}], "]"}], "&"}], "/@", "ocon"}]}], "]"}]}], "]"}]}], "]"}]}]}], \(Protect@Message; \)}], "Input", InitializationCell->True], Cell[BoxData[ \(General::fwd = "\"; \nGeneral::shdwvl = "\"; \)], "Input", InitializationCell->True] }, Closed]], Cell[CellGroupData[{ Cell["A example of how it helps", "Section"], Cell["\<\ This is an example of a typical session where one forgets to load a \ package.\ \>", "Text", FontSize->17], Cell["\<\ We make some shorthand definitions because we are a lazy typer:\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \({ppq, sp, cert} = {ProvablePrimeQ, SmallPrime, Certificate}\)], "Input"], Cell[BoxData[ \({ProvablePrimeQ, SmallPrime, Certificate}\)], "Output"] }, Open ]], Cell["We set SmallPrime to a small prime:", "Text"], Cell[BoxData[ \(\(SmallPrime = 37; \)\)], "Input"], Cell["\<\ We wonder if that's really how SmallPrime is supposed to be used:\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(\(?SmallPrime\)\)], "Input"], Cell[BoxData[ \("Global`SmallPrime"\)], "Print"], Cell[BoxData[ InterpretationBox[ StyleBox[\(SmallPrime\ = \ 37\), ShowStringCharacters->True, NumberMarks->True], InputForm[ Definition[ SmallPrime]], Editable->True, AutoDelete->True]], "Print"] }, Open ]], Cell["\<\ What? Oh, we forgot to load the necessary package! Let's load it:\ \>", "Text"], Cell[CellGroupData[{ Cell["<"S5.70.1"], Cell[BoxData[ \(ProvablePrimeQ::"fwd" \( : \ \) "The existing symbol \!\(\"ProvablePrimeQ\"\) in context \ \!\(\"Global`\"\) is being forwarded to context \ \!\(\"NumberTheory`PrimeQ`\"\)."\)], "Message"], Cell[BoxData[ \(Certificate::"fwd" \( : \ \) "The existing symbol \!\(\"Certificate\"\) in context \!\(\"Global`\"\) \ is being forwarded to context \!\(\"NumberTheory`PrimeQ`\"\)."\)], "Message"], Cell[BoxData[ RowBox[{ \(SmallPrime::"shdwvl"\), \( : \ \), "\<\"Definitions have been made for symbol \\!\\(\\\"SmallPrime\\\"\\) \ in context \\!\\(\\\"Global`\\\"\\) which will shadow new definitions in \ context \\!\\(\\\"NumberTheory`PrimeQ`\\\"\\).\\n** Press \\!\\(\\*ButtonBox[\ \\\"\\\\\\\"fix it\\\\\\\"\\\", RuleDelayed[ButtonFunction, \ Function[Global`ForwardContext[\\\"SmallPrime\\\", \\\"Global`\\\", \ \\\"NumberTheory`PrimeQ`\\\"]]], Rule[ButtonEvaluator, Automatic], \ Rule[Active, True]]\\) to use only the new definitions.\"\>"}]], "Message"] }, Open ]], Cell[TextData[{ "(Don't press the \"", StyleBox["fix it", FontWeight->"Bold"], "\" button quite yet.)" }], "Text", FontSize->17], Cell["\<\ It didn't automatically forward SmallPrime, since that would erase the \ definition you made:\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(?? SmallPrime\)], "Input"], Cell[BoxData[ \("Global`SmallPrime"\)], "Print"], Cell[BoxData[ InterpretationBox[ StyleBox[\(SmallPrime\ = \ 37\), ShowStringCharacters->True, NumberMarks->True], InputForm[ Definition[ SmallPrime]], Editable->True, AutoDelete->True]], "Print"] }, Open ]], Cell["\<\ But you don't want that definition. You want to use the \"real\" SmallPrime.\ \ \>", "Text"], Cell[TextData[{ "Now press the \"", StyleBox["fix it", FontWeight->"Bold"], "\" button in the warning message above." }], "Text", FontSize->17], Cell["\<\ Now our definition is gone, and SmallPrime is what we wanted it to be:\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(?? SmallPrime\)], "Input"], Cell[BoxData[ \("SmallPrime is an option for ProvablePrimeQ, PrimeQCertificate, and \ PrimeQCertificateCheck, that gives a lower bound for using the Atkin-Morain \ primality test."\)], "Print"], Cell[BoxData[ InterpretationBox[ StyleBox[\(Attributes[SmallPrime]\ = \ {Protected}\), ShowStringCharacters->True, NumberMarks->True], InputForm[ Definition[ SmallPrime]], Editable->True, AutoDelete->True]], "Print"] }, Open ]], Cell["\<\ What about sp? We defined it before we loaded the real SmallPrime, but this \ has been fixed behind the scenes for us:\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(\(?sp\)\)], "Input"], Cell[BoxData[ \("Global`sp"\)], "Print"], Cell[BoxData[ InterpretationBox[ StyleBox[\(sp\ = \ SmallPrime\), ShowStringCharacters->True, NumberMarks->True], InputForm[ Definition[ sp]], Editable->True, AutoDelete->True]], "Print"] }, Open ]], Cell["In fact, everything works just fine:", "Text"], Cell[CellGroupData[{ Cell[BoxData[ \(ppq[12345678910987654321, cert -> True, sp -> 10^20]\)], "Input"], Cell[BoxData[ \({True, {12345678910987654321, 7, {2, {3, 2, {2}}, {5, 2, {2}}, {11, 2, {2, {5, 2, {2}}}}, {2623783, 5, {2, {3, 2, {2}}, {7, 3, {2, {3, 2, {2}}}}, {179, 2, {2, {89, 3, {2, {11, 2, {2, {5, 2, {2}}}}}}}}, {349, 2, {2, {3, 2, {2}}, {29, 2, {2, {7, 3, {2, {3, 2, {2}}}}}}}}}}, {594103187, 2, {2, {297051593, 3, {2, {911, 17, {2, {5, 2, {2}}, {7, 3, {2, {3, 2, {2}}}}, {13, 2, {2, {3, 2, {2}}}}}}, {40759, 3, {2, {3, 2, {2}}, {6793, 10, {2, {3, 2, {2}}, {283, 3, {2, {3, 2, {2}}, {47, 5, {2, {23, 5, {2, {11, 2, {2, {5, 2, {2}}}}}}}}}}}}}}}}}}}}} \)], "Output"] }, Open ]], Cell["\<\ As we can see, we are not suffering in the slightest from having loaded the \ package after using some of its symbols.\ \>", "Text", FontSize->17], Cell[TextData[{ StyleBox[" \[HappySmiley] ", FontSize->72], StyleBox["Have fun using packages! -Matthew Cook", FontSlant->"Italic"] }], "Text"] }, Closed]] }, Open ]] }, FrontEndVersion->"Microsoft Windows 3.0", ScreenRectangle->{{0, 1280}, {0, 968}}, AutoGeneratedPackage->None, WindowSize->{492, 528}, WindowMargins->{{Automatic, 132}, {Automatic, 48}} ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{ "S5.70.1"->{ Cell[6678, 193, 62, 1, 30, "Input", CellTags->"S5.70.1"]} } *) (*CellTagsIndex CellTagsIndex->{ {"S5.70.1", 11921, 372} } *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1731, 51, 126, 3, 122, "Subtitle"], Cell[1860, 56, 53, 0, 28, "SmallText"], Cell[CellGroupData[{ Cell[1938, 60, 57, 0, 53, "Section"], Cell[1998, 62, 476, 8, 170, "Input", InitializationCell->True], Cell[2477, 72, 339, 7, 110, "Input", InitializationCell->True], Cell[2819, 81, 2293, 42, 482, "Input", InitializationCell->True], Cell[5115, 125, 351, 7, 130, "Input", InitializationCell->True] }, Closed]], Cell[CellGroupData[{ Cell[5503, 137, 44, 0, 33, "Section"], Cell[5550, 139, 118, 4, 62, "Text"], Cell[5671, 145, 87, 2, 33, "Text"], Cell[CellGroupData[{ Cell[5783, 151, 92, 1, 30, "Input"], Cell[5878, 154, 75, 1, 29, "Output"] }, Open ]], Cell[5968, 158, 51, 0, 33, "Text"], Cell[6022, 160, 54, 1, 30, "Input"], Cell[6079, 163, 89, 2, 52, "Text"], Cell[CellGroupData[{ Cell[6193, 169, 48, 1, 30, "Input"], Cell[6244, 172, 52, 1, 25, "Print"], Cell[6299, 175, 249, 8, 25, "Print"] }, Open ]], Cell[6563, 186, 90, 3, 52, "Text"], Cell[CellGroupData[{ Cell[6678, 193, 62, 1, 30, "Input", CellTags->"S5.70.1"], Cell[6743, 196, 215, 4, 63, "Message"], Cell[6961, 202, 207, 3, 63, "Message"], Cell[7171, 207, 578, 9, 87, "Message"] }, Open ]], Cell[7764, 219, 140, 6, 38, "Text"], Cell[7907, 227, 117, 3, 52, "Text"], Cell[CellGroupData[{ Cell[8049, 234, 46, 1, 30, "Input"], Cell[8098, 237, 52, 1, 25, "Print"], Cell[8153, 240, 249, 8, 25, "Print"] }, Open ]], Cell[8417, 251, 103, 3, 52, "Text"], Cell[8523, 256, 155, 6, 62, "Text"], Cell[8681, 264, 94, 2, 52, "Text"], Cell[CellGroupData[{ Cell[8800, 270, 46, 1, 30, "Input"], Cell[8849, 273, 197, 3, 82, "Print"], Cell[9049, 278, 270, 8, 25, "Print"] }, Open ]], Cell[9334, 289, 143, 3, 52, "Text"], Cell[CellGroupData[{ Cell[9502, 296, 40, 1, 30, "Input"], Cell[9545, 299, 44, 1, 25, "Print"], Cell[9592, 302, 241, 8, 25, "Print"] }, Open ]], Cell[9848, 313, 52, 0, 33, "Text"], Cell[CellGroupData[{ Cell[9925, 317, 85, 1, 30, "Input"], Cell[10013, 320, 920, 17, 238, "Output"] }, Open ]], Cell[10948, 340, 158, 4, 86, "Text"], Cell[11109, 346, 156, 5, 95, "Text"] }, Closed]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)