[Date Index]
[Thread Index]
[Author Index]
Re: scope all wrong? in Mathematica 4.1
*To*: mathgroup at smc.vnet.net
*Subject*: [mg31847] Re: scope all wrong? in Mathematica 4.1
*From*: "Allan Hayes" <hay at haystack.demon.co.uk>
*Date*: Fri, 7 Dec 2001 05:56:38 -0500 (EST)
*References*: <9ul2ft$6m3$1@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
Richard,
Richard,
I have found a feature amongst your examples that came as a surprise: the
way in which SetDelayed is evaluated. The outputs from your examples seem
to be consequences of this and the standard evaluation process - I'll come
back to them later {in the notebook below].
My understanding was that h[e]:=r evaluates h then e to given h1[e1]:=r;
then stores the rule for this (without looking for rules that might change
it as a whole); it then returns the value Null.
It seems however that more is going on: the rule is stored is as expected
from the above, but it seems that after making later rules; some kind of
evaluation is triggered, as though trying to evaluate the left sides using
the other rules (the rules themselves are not altered). These evaluations
may be due to working out the order in which to store or replace existing
ones. It seems to me that it would be better if these evaluations were done
in private. However, the same kind of evaluations will occur when the rules
are used, and any unwanted assignments can be avoided in both circumstances
by suitable localisation of symbols inside tests and conditions, though
unwanted printing would still occur.
Please see the notebook below for details.
--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
(************** Content-type: application/mathematica **************
Mathematica-Compatible Notebook
This notebook can be used with any Mathematica-compatible
application, such as Mathematica, MathReader or Publicon. The data
for the notebook starts with the line containing 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 at 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[ 17734, 662]*)
(*NotebookOutlinePosition[ 19618, 717]*)
(* CellTagsIndexPosition[ 19362, 707]*)
(*WindowFrame->Normal*)
Notebook[{
Cell["\<\
Richard,
I have found a feature amongst your examples that came as a surprise: \
the way in which SetDelayed is evaluated. The outputs from your \
examples seem to be consequences of this and the standard evaluation \
process - I'll come back to them later.
My understanding was that h[e]:=r evaluates h then e to given \
h1[e1]:=r; then stores the rule for this (without looking for rules \
that might change it as a whole); it then returns the value Null.
It seems however that more is going on: the rule is stored is as \
expected from the above, but it seems that after making later rules; \
some kind of evaluation is triggered, as though trying to evaluate \
the left sides using the other rules (the rules themselves are not \
altered). These evaluations may be due to working out the order in \
which to store or replace existing ones. It seems to me that it \
would be better if these evaluations were done in private. However, \
the same kind of evaluations will occur when the rules are used, and \
any unwanted assignments can be avoided in both circumstances by \
suitable localisation of symbols inside tests and conditions, though \
unwanted printing would still occur.
Here are some examples\
\>", "Text"],
Cell[CellGroupData[{
Cell["Extra evaluations", "Section"],
Cell[BoxData[
\(a = 88; b = 99;\)], "Input"],
Cell["Define a one-place function for uu:", "Text"],
Cell[BoxData[
StyleBox[\(uu[_?\((\(({a = 3, b, Print[F[a, #]]};
True)\) &)\)]\ := \ 2\),
FormatType->StandardForm]], "Input"],
Cell["The value of a is not changed", "Text"],
Cell[CellGroupData[{
Cell[BoxData[
\(a\)], "Input"],
Cell[BoxData[
\(88\)], "Output"]
}, Open ]],
Cell["The stored rule is as expected", "Text"],
Cell[CellGroupData[{
Cell[BoxData[
\(Definition[uu] // InputForm\)], "Input"],
Cell["uu[_?(({a = 3, b, Print[F[a, #1]]}; True) & )] := 2", "Output"]
}, Open ]],
Cell[TextData[{
"Now, define another one-place function for ",
StyleBox["uu",
FontFamily->"Courier"]
}], "Text"],
Cell[CellGroupData[{
Cell[BoxData[
StyleBox[\(uu[
y_ /; \(({b = 4, a, Print[G[b, y]]}; True)\)]\ := \ 2\),
FormatType->StandardForm]], "Input"],
Cell[BoxData[
\(F[3, y_ /; \(({b = 4, a, Print[G[b, y]]}; True)\)]\)], "Print"],
Cell[BoxData[
\(G[4, _?\((\(({a = 3, b, Print[F[a, #1]]};
True)\) &)\)]\)], "Print"]
}, Open ]],
Cell[TextData[{
"The printouts indicate the extra evaluation that I referred to, as \
do the changes in the value of ",
StyleBox["a",
FontFamily->"Courier"],
" and ",
StyleBox["b",
FontFamily->"Courier"],
" shown below"
}], "Text"],
Cell[CellGroupData[{
Cell[BoxData[
\({a, b}\)], "Input"],
Cell[BoxData[
\({3, 4}\)], "Output"]
}, Open ]],
Cell["\<\
However, the stored rules are still as we expected - unaffected by \
the extra evaluations:\
\>", "Text"],
Cell[CellGroupData[{
Cell[BoxData[
\(Definition[uu] // InputForm\)], "Input"],
Cell["\<\
uu[_?(({a = 3, b, Print[F[a, #1]]}; True) & )] := 2
uu[y_ /; ({b = 4, a, Print[G[b, y]]}; True)] := 2\
\>", "Output"]
}, Open ]],
Cell[TextData[{
"Even defining a simple one-place function for ",
StyleBox["uu",
FontFamily->"Courier"],
" (without condition or test)\ntriggers the extra evaluations"
}], "Text"],
Cell[CellGroupData[{
Cell[BoxData[
StyleBox[\(uu[z_]\ := \ 2\),
FormatType->StandardForm]], "Input"],
Cell[BoxData[
\(F[3, z_]\)], "Print"],
Cell[BoxData[
\(G[4, z_]\)], "Print"]
}, Open ]]
}, Closed]],
Cell[CellGroupData[{
Cell["Outputs in your posting", "Section"],
Cell["\<\
I have modified your examples slightly to make some points - please \
allow for these in interpreting the quotes from your posting.\
\>", "Text"],
Cell[BoxData[
\(Clear["\<`*\>"]\)], "Input"],
Cell["> Consider the Mathematica definition", "Text"],
Cell[BoxData[
\(rr[x_] := Block[{x}, x = 5; Print["\<x is \>", x]]\)], "Input"],
Cell[CellGroupData[{
Cell[BoxData[
\(rr[4]\)], "Input"],
Cell[BoxData[
\(Block::"lvsym" \(\(:\)\(\ \)\)
"Local variable specification \!\({4}\) contains \!\(4\) which \
is not a symbol or an assignment to a symbol."\)], "Message"],
Cell[BoxData[
\(Block[{4}, 4 = 5; Print["x is ", 4]]\)], "Output"]
}, Open ]],
Cell["\<\
This is nothing to do with Block: it occurs because passing (5) \
across rule is never blocked, though it does cause re-naming in \
scoping constructs, for example\
\>", "Text"],
Cell[BoxData[
\(f[x_] := \ Function[x, x + y]\)], "Input"],
Cell[CellGroupData[{
Cell[BoxData[
\(f[4]\)], "Input"],
Cell[BoxData[
\(Function::"flpar" \(\(:\)\(\ \)\)
"Parameter specification \!\(4\) in \!\(Function[\(\(4, \(\(4 + \
y\)\)\)\)]\) should be a symbol or a list of symbols."\)], "Message"],
Cell[BoxData[
\(Function[4, 4 + y]\)], "Output"]
}, Open ]],
Cell[BoxData[
\(f[y_] := \ Function[x, x + y]\)], "Input"],
Cell[CellGroupData[{
Cell[BoxData[
\(f[4]\)], "Input"],
Cell[BoxData[
\(Function[x$, x$ + 4]\)], "Output"]
}, Open ]],
Cell[">If you use Module instead of Block, you get the same.", "Text"],
Cell["Yes, for the same reason.", "Text"],
Cell[">However,", "Text"],
Cell[CellGroupData[{
Cell[BoxData[
\(Block[{x = 4},
Block[{x}, x = 5; Print["\<x is \>", x]]]\)], "Input"],
Cell[BoxData[
InterpretationBox[\("x is "\[InvisibleSpace]5\),
SequenceForm[ "x is ", 5],
Editable->False]], "Print"]
}, Open ]],
Cell["\<\
This is because inside the inner Block x is temporarily set the \
value 5. No passing of values across rules is involved. The mechanism \
is to do with the use of temporary rules, not passing.\
\>", "Text"],
Cell["\<\
>Using pattern matching for
>substituting 4 for x in rr[4] even when x is bound seems
>to be counter to what most programming language designers
>would expect.
This general point is better made with the example above that uses \
Function: Block does not bind - as remarked earlier it effect is via \
temporay rules and ordinary evaluation.
>Now look at\
\>", "Text"],
Cell[BoxData[
\(uu[x_?\((\((x = 5; Print["\<x is \>", A[x]]; True)\) &)\)] :=
x\)], "Input"],
Cell["\<\
>Usually when one defines a program, nothing is printed.
>Here, we get
>x is 5
>5
>x is 5
As you see, I did not get anything.\
\>", "Text"],
Cell[">If we do this:", "Text"],
Cell[CellGroupData[{
Cell[BoxData[{
\(\(x = 400;\)\), "\n",
\(uu[70]\), "\[IndentingNewLine]",
\(x\)}], "Input"],
Cell[BoxData[
InterpretationBox[\("x is "\[InvisibleSpace]A[5]\),
SequenceForm[ "x is ",
A[ 5]],
Editable->False]], "Print"],
Cell[BoxData[
\(70\)], "Output"],
Cell[BoxData[
\(5\)], "Output"]
}, Open ]],
Cell["\<\
>x is 5 printed
>the value returned is 70
>and the global x is 5.
Yes: this is what I would expect from the test being evaluated - \
nothing to do with the \"extra evaluations\" observed in the first \
section.
>Try this\
\>", "Text"],
Cell["Before trying note that there is now a rule stored for uu", \
"Text"],
Cell[CellGroupData[{
Cell[BoxData[
\(?? uu\)], "Input"],
Cell["Global`uu", "Print",
CellTags->"Info3216631205-4665998"],
Cell[BoxData[
InterpretationBox[GridBox[{
{GridBox[{
{\(uu[
x_?\((\((x = 5; Print["x is ", A[x]];
True)\) &)\)] := x\)}
},
GridBaseline->{Baseline, {1, 1}},
ColumnWidths->0.999,
ColumnAlignments->{Left}]}
},
GridBaseline->{Baseline, {1, 1}},
ColumnAlignments->{Left}],
Definition[ "uu"],
Editable->False]], "Print",
CellTags->"Info3216631205-4665998"]
}, Open ]],
Cell["\<\
I also reset x to 100 and change your 5 to 6 , resmake a point about \
assignment to global variables; and add an extra line, x to test \
this..\
\>", "Text"],
Cell[CellGroupData[{
Cell[BoxData[{
\(\(x = 100;\)\), "\[IndentingNewLine]",
\(Module[{x}, \[IndentingNewLine]uu[
x_?\((\((x = 6; Print["\<x is \>", B[x]]; True)\) &)\)] :=
x; \[IndentingNewLine]Print["\<in Module, x is \>",
x]\[IndentingNewLine]]\), "\[IndentingNewLine]",
\(x\)}], "Input"],
Cell[BoxData[
InterpretationBox[\("x is "\[InvisibleSpace]A[5]\),
SequenceForm[ "x is ",
A[ 5]],
Editable->False]], "Print"],
Cell[BoxData[
InterpretationBox[\("x is "\[InvisibleSpace]B[6]\),
SequenceForm[ "x is ",
B[ 6]],
Editable->False]], "Print"],
Cell[BoxData[
InterpretationBox[\("in Module, x is "\[InvisibleSpace]6\),
SequenceForm[ "in Module, x is ", 6],
Editable->False]], "Print"],
Cell[BoxData[
\(5\)], "Output"]
}, Open ]],
Cell[TextData[{
"Module renames x in {x} and the free x in its body to x$nn; the \
rule defined inside Module is made then the \"extra evaluations\" \
kick in: the left side of the new rule attempts to use the \
pre-existing rule, which results in x being set to 5 and the printout \
",
StyleBox["x is A[5]",
FontFamily->"Courier"],
"; then trhe left side ot the pre-existing rule attempts to use \
the new rule, which results in x$nn being set to 6 and the printout \
x=6 (not x$nn since we have the string \"x is \"). A consequence is \
that without the pre-existing rule, x would not be assigned a new \
value.\nWe now have two rules for uu. Notice that in the one made \
inside Module, x in x_ and on the right side are bound and so are \
not renamed."
}], "Text"],
Cell[CellGroupData[{
Cell[BoxData[
\(\(?uu\)\)], "Input"],
Cell["Global`uu", "Print",
CellTags->"Info3216631214-9056274"],
Cell[BoxData[
InterpretationBox[GridBox[{
{GridBox[{
{\(uu[
x_?\((\((x = 5; Print["x is ", A[x]];
True)\) &)\)] := x\)},
{" "},
{\(uu[
x_?\((\((x$19 = 6; Print["x is ", B[x$19]];
True)\) &)\)] := x\)}
},
GridBaseline->{Baseline, {1, 1}},
ColumnWidths->0.999,
ColumnAlignments->{Left}]}
},
GridBaseline->{Baseline, {1, 1}},
ColumnAlignments->{Left}],
Definition[ "uu"],
Editable->False]], "Print",
CellTags->"Info3216631214-9056274"]
}, Open ]],
Cell["\<\
As we expect from the first section, printouts pile up even if we \
just repeat the evaluation of the module\
\>", "Text"],
Cell[CellGroupData[{
Cell[BoxData[
\(Module[{x}, \[IndentingNewLine]uu[
x_?\((\((x = 6; Print["\<x is \>", C[x]]; True)\) &)\)] :=
x; \[IndentingNewLine]Print["\<in Module, x is \>",
x]\[IndentingNewLine]]\)], "Input"],
Cell[BoxData[
InterpretationBox[\("x is "\[InvisibleSpace]A[5]\),
SequenceForm[ "x is ",
A[ 5]],
Editable->False]], "Print"],
Cell[BoxData[
InterpretationBox[\("x is "\[InvisibleSpace]6\),
SequenceForm[ "x is ", 6],
Editable->False]], "Print"],
Cell[BoxData[
InterpretationBox[\("x is "\[InvisibleSpace]B[6]\),
SequenceForm[ "x is ",
B[ 6]],
Editable->False]], "Print"],
Cell[BoxData[
InterpretationBox[\("x is "\[InvisibleSpace]6\),
SequenceForm[ "x is ", 6],
Editable->False]], "Print"],
Cell[BoxData[
InterpretationBox[\("in Module, x is "\[InvisibleSpace]6\),
SequenceForm[ "in Module, x is ", 6],
Editable->False]], "Print"]
}, Open ]],
Cell[CellGroupData[{
Cell[BoxData[
\(\(?uu\)\)], "Input"],
Cell["Global`uu", "Print",
CellTags->"Info3216631221-7238232"],
Cell[BoxData[
InterpretationBox[GridBox[{
{GridBox[{
{\(uu[
x_?\((\((x = 5; Print["x is ", A[x]];
True)\) &)\)] := x\)},
{" "},
{\(uu[
x_?\((\((x$19 = 6; Print["x is ", B[x$19]];
True)\) &)\)] := x\)},
{" "},
{\(uu[
x_?\((\((x$23 = 6; Print["x is ", x$23];
True)\) &)\)] := x\)}
},
GridBaseline->{Baseline, {1, 1}},
ColumnWidths->0.999,
ColumnAlignments->{Left}]}
},
GridBaseline->{Baseline, {1, 1}},
ColumnAlignments->{Left}],
Definition[ "uu"],
Editable->False]], "Print",
CellTags->"Info3216631221-7238232"]
}, Open ]],
Cell["\<\
>The expectation in a system supporting lexical scope
>with Module
>is that no use of x inside the module would escape.
>But it does. The global x is set to 5.
The change in x is because of the pre-existing rule, as noted above.
But with no such rule another feature emerges:
Check:\
\>", "Text"],
Cell[CellGroupData[{
Cell[BoxData[{
\(Clear[uu]\), "\[IndentingNewLine]",
\(\(x = 100;\)\), "\[IndentingNewLine]",
\(Module[{x}, \[IndentingNewLine]uu[
x_?\((\((x = 6; Print["\<x is \>", x]; True)\) &)\)] :=
x; \[IndentingNewLine]Print["\<in Module, x is \>",
x]\[IndentingNewLine]]\), "\[IndentingNewLine]",
\(x\)}], "Input"],
Cell[BoxData[
InterpretationBox[\("in Module, x is "\[InvisibleSpace]x$27\),
SequenceForm[ "in Module, x is ", x$27],
Editable->False]], "Print"],
Cell[BoxData[
\(100\)], "Output"]
}, Open ]],
Cell["\<\
There have been no extra evaluations, because there are no \
pre-existing rules:\
\>", "Text"],
Cell[CellGroupData[{
Cell[BoxData[
\(x$67\)], "Input"],
Cell[BoxData[
\(x$67\)], "Output"]
}, Open ]],
Cell["The rule defined in Module is", "Text"],
Cell[CellGroupData[{
Cell[BoxData[
\(\(?uu\)\)], "Input"],
Cell["Global`uu", "Print",
CellTags->"Info3216631226-7466759"],
Cell[BoxData[
InterpretationBox[GridBox[{
{GridBox[{
{\(uu[
x_?\((\((x$27 = 6; Print["x is ", x$27];
True)\) &)\)] := x\)}
},
GridBaseline->{Baseline, {1, 1}},
ColumnWidths->0.999,
ColumnAlignments->{Left}]}
},
GridBaseline->{Baseline, {1, 1}},
ColumnAlignments->{Left}],
Definition[ "uu"],
Editable->False]], "Print",
CellTags->"Info3216631226-7466759"]
}, Open ]],
Cell["\<\
Now from earlier we would expect a new rule to be stored if we repeat \
the evaluation of the module; but it seems to have been deleted early \
on in the evaluation\
\>", "Text"],
Cell[CellGroupData[{
Cell[BoxData[{
\(Clear[uu]\), "\[IndentingNewLine]",
\(\(x = 100;\)\), "\[IndentingNewLine]",
\(Module[{x}, \[IndentingNewLine]uu[
x_?\((\((x = 6; Print["\<x is \>", x]; True)\) &)\)] :=
x; \[IndentingNewLine]Print["\<in Module, x is \>",
x]\[IndentingNewLine]]\), "\[IndentingNewLine]",
\(x\)}], "Input"],
Cell[BoxData[
InterpretationBox[\("in Module, x is "\[InvisibleSpace]x$31\),
SequenceForm[ "in Module, x is ", x$31],
Editable->False]], "Print"],
Cell[BoxData[
\(100\)], "Output"]
}, Open ]],
Cell[CellGroupData[{
Cell[BoxData[
\(\(?uu\)\)], "Input"],
Cell["Global`uu", "Print",
CellTags->"Info3216631234-9152500"],
Cell[BoxData[
InterpretationBox[GridBox[{
{GridBox[{
{\(uu[
x_?\((\((x$31 = 6; Print["x is ", x$31];
True)\) &)\)] := x\)}
},
GridBaseline->{Baseline, {1, 1}},
ColumnWidths->0.999,
ColumnAlignments->{Left}]}
},
GridBaseline->{Baseline, {1, 1}},
ColumnAlignments->{Left}],
Definition[ "uu"],
Editable->False]], "Print",
CellTags->"Info3216631234-9152500"]
}, Open ]],
Cell[CellGroupData[{
Cell[BoxData[
\(x$67\)], "Input"],
Cell[BoxData[
\(x$67\)], "Output"]
}, Open ]]
}, Closed]]
},
FrontEndVersion->"4.1 for Microsoft Windows",
ScreenRectangle->{{0, 1024}, {0, 695}},
WindowSize->{498, 598},
WindowMargins->{{-52, Automatic}, {Automatic, 11}},
ShowSelection->True
]
(*******************************************************************
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->{
"Info3216631205-4665998"->{
Cell[8877, 346, 64, 1, 25, "Print",
CellTags->"Info3216631205-4665998"],
Cell[8944, 349, 532, 15, 25, "Print",
CellTags->"Info3216631205-4665998"]},
"Info3216631214-9056274"->{
Cell[11361, 426, 64, 1, 25, "Print",
CellTags->"Info3216631214-9056274"],
Cell[11428, 429, 698, 19, 58, "Print",
CellTags->"Info3216631214-9056274"]},
"Info3216631221-7238232"->{
Cell[13341, 497, 64, 1, 25, "Print",
CellTags->"Info3216631221-7238232"],
Cell[13408, 500, 861, 23, 91, "Print",
CellTags->"Info3216631221-7238232"]},
"Info3216631226-7466759"->{
Cell[15523, 579, 64, 1, 25, "Print",
CellTags->"Info3216631226-7466759"],
Cell[15590, 582, 535, 15, 25, "Print",
CellTags->"Info3216631226-7466759"]},
"Info3216631234-9152500"->{
Cell[16989, 631, 64, 1, 25, "Print",
CellTags->"Info3216631234-9152500"],
Cell[17056, 634, 535, 15, 25, "Print",
CellTags->"Info3216631234-9152500"]}
}
*)
(*CellTagsIndex
CellTagsIndex->{
{"Info3216631205-4665998", 18364, 678},
{"Info3216631214-9056274", 18563, 683},
{"Info3216631221-7238232", 18764, 688},
{"Info3216631226-7466759", 18965, 693},
{"Info3216631234-9152500", 19166, 698}
}
*)
(*NotebookFileOutline
Notebook[{
Cell[1705, 50, 1236, 21, 318, "Text"],
Cell[CellGroupData[{
Cell[2966, 75, 36, 0, 59, "Section"],
Cell[3005, 77, 48, 1, 30, "Input"],
Cell[3056, 80, 51, 0, 33, "Text"],
Cell[3110, 82, 155, 3, 30, "Input"],
Cell[3268, 87, 45, 0, 33, "Text"],
Cell[CellGroupData[{
Cell[3338, 91, 34, 1, 30, "Input"],
Cell[3375, 94, 36, 1, 29, "Output"]
}, Open ]],
Cell[3426, 98, 46, 0, 33, "Text"],
Cell[CellGroupData[{
Cell[3497, 102, 60, 1, 30, "Input"],
Cell[3560, 105, 69, 0, 43, "Output"]
}, Open ]],
Cell[3644, 108, 120, 4, 33, "Text"],
Cell[CellGroupData[{
Cell[3789, 116, 143, 3, 30, "Input"],
Cell[3935, 121, 83, 1, 25, "Print"],
Cell[4021, 124, 103, 2, 25, "Print"]
}, Open ]],
Cell[4139, 129, 249, 9, 52, "Text"],
Cell[CellGroupData[{
Cell[4413, 142, 39, 1, 30, "Input"],
Cell[4455, 145, 40, 1, 29, "Output"]
}, Open ]],
Cell[4510, 149, 115, 3, 33, "Text"],
Cell[CellGroupData[{
Cell[4650, 156, 60, 1, 30, "Input"],
Cell[4713, 159, 129, 4, 77, "Output"]
}, Open ]],
Cell[4857, 166, 189, 5, 52, "Text"],
Cell[CellGroupData[{
Cell[5071, 175, 90, 2, 30, "Input"],
Cell[5164, 179, 41, 1, 25, "Print"],
Cell[5208, 182, 41, 1, 25, "Print"]
}, Open ]]
}, Closed]],
Cell[CellGroupData[{
Cell[5298, 189, 42, 0, 39, "Section"],
Cell[5343, 191, 155, 3, 52, "Text"],
Cell[5501, 196, 48, 1, 30, "Input"],
Cell[5552, 199, 53, 0, 33, "Text"],
Cell[5608, 201, 83, 1, 30, "Input"],
Cell[CellGroupData[{
Cell[5716, 206, 38, 1, 30, "Input"],
Cell[5757, 209, 184, 3, 60, "Message"],
Cell[5944, 214, 70, 1, 29, "Output"]
}, Open ]],
Cell[6029, 218, 187, 4, 52, "Text"],
Cell[6219, 224, 62, 1, 30, "Input"],
Cell[CellGroupData[{
Cell[6306, 229, 37, 1, 30, "Input"],
Cell[6346, 232, 196, 3, 60, "Message"],
Cell[6545, 237, 52, 1, 29, "Output"]
}, Open ]],
Cell[6612, 241, 62, 1, 30, "Input"],
Cell[CellGroupData[{
Cell[6699, 246, 37, 1, 30, "Input"],
Cell[6739, 249, 54, 1, 29, "Output"]
}, Open ]],
Cell[6808, 253, 70, 0, 33, "Text"],
Cell[6881, 255, 41, 0, 33, "Text"],
Cell[6925, 257, 25, 0, 33, "Text"],
Cell[CellGroupData[{
Cell[6975, 261, 96, 2, 30, "Input"],
Cell[7074, 265, 133, 3, 25, "Print"]
}, Open ]],
Cell[7222, 271, 217, 4, 71, "Text"],
Cell[7442, 277, 378, 11, 185, "Text"],
Cell[7823, 290, 103, 2, 30, "Input"],
Cell[7929, 294, 150, 8, 147, "Text"],
Cell[8082, 304, 31, 0, 33, "Text"],
Cell[CellGroupData[{
Cell[8138, 308, 105, 3, 70, "Input"],
Cell[8246, 313, 149, 4, 25, "Print"],
Cell[8398, 319, 36, 1, 29, "Output"],
Cell[8437, 322, 35, 1, 29, "Output"]
}, Open ]],
Cell[8487, 326, 246, 10, 166, "Text"],
Cell[8736, 338, 75, 1, 33, "Text"],
Cell[CellGroupData[{
Cell[8836, 343, 38, 1, 30, "Input"],
Cell[8877, 346, 64, 1, 25, "Print",
CellTags->"Info3216631205-4665998"],
Cell[8944, 349, 532, 15, 25, "Print",
CellTags->"Info3216631205-4665998"]
}, Open ]],
Cell[9491, 367, 168, 4, 52, "Text"],
Cell[CellGroupData[{
Cell[9684, 375, 312, 6, 130, "Input"],
Cell[9999, 383, 149, 4, 25, "Print"],
Cell[10151, 389, 149, 4, 25, "Print"],
Cell[10303, 395, 155, 3, 25, "Print"],
Cell[10461, 400, 35, 1, 29, "Output"]
}, Open ]],
Cell[10511, 404, 782, 15, 166, "Text"],
Cell[CellGroupData[{
Cell[11318, 423, 40, 1, 30, "Input"],
Cell[11361, 426, 64, 1, 25, "Print",
CellTags->"Info3216631214-9056274"],
Cell[11428, 429, 698, 19, 58, "Print",
CellTags->"Info3216631214-9056274"]
}, Open ]],
Cell[12141, 451, 132, 3, 52, "Text"],
Cell[CellGroupData[{
Cell[12298, 458, 229, 4, 90, "Input"],
Cell[12530, 464, 149, 4, 25, "Print"],
Cell[12682, 470, 133, 3, 25, "Print"],
Cell[12818, 475, 149, 4, 25, "Print"],
Cell[12970, 481, 133, 3, 25, "Print"],
Cell[13106, 486, 155, 3, 25, "Print"]
}, Open ]],
Cell[CellGroupData[{
Cell[13298, 494, 40, 1, 30, "Input"],
Cell[13341, 497, 64, 1, 25, "Print",
CellTags->"Info3216631221-7238232"],
Cell[13408, 500, 861, 23, 91, "Print",
CellTags->"Info3216631221-7238232"]
}, Open ]],
Cell[14284, 526, 308, 10, 185, "Text"],
Cell[CellGroupData[{
Cell[14617, 540, 352, 7, 150, "Input"],
Cell[14972, 549, 161, 3, 25, "Print"],
Cell[15136, 554, 37, 1, 29, "Output"]
}, Open ]],
Cell[15188, 558, 104, 3, 33, "Text"],
Cell[CellGroupData[{
Cell[15317, 565, 37, 1, 30, "Input"],
Cell[15357, 568, 38, 1, 29, "Output"]
}, Open ]],
Cell[15410, 572, 45, 0, 33, "Text"],
Cell[CellGroupData[{
Cell[15480, 576, 40, 1, 30, "Input"],
Cell[15523, 579, 64, 1, 25, "Print",
CellTags->"Info3216631226-7466759"],
Cell[15590, 582, 535, 15, 25, "Print",
CellTags->"Info3216631226-7466759"]
}, Open ]],
Cell[16140, 600, 188, 4, 52, "Text"],
Cell[CellGroupData[{
Cell[16353, 608, 352, 7, 150, "Input"],
Cell[16708, 617, 161, 3, 25, "Print"],
Cell[16872, 622, 37, 1, 29, "Output"]
}, Open ]],
Cell[CellGroupData[{
Cell[16946, 628, 40, 1, 30, "Input"],
Cell[16989, 631, 64, 1, 25, "Print",
CellTags->"Info3216631234-9152500"],
Cell[17056, 634, 535, 15, 25, "Print",
CellTags->"Info3216631234-9152500"]
}, Open ]],
Cell[CellGroupData[{
Cell[17628, 654, 37, 1, 30, "Input"],
Cell[17668, 657, 38, 1, 29, "Output"]
}, Open ]]
}, Closed]]
}
]
*)
(*******************************************************************
End of Mathematica Notebook file.
*******************************************************************)
"Richard Fateman" <fateman at cs.berkeley.edu> wrote in message
news:9ul2ft$6m3$1 at smc.vnet.net...
> Consider the mathematica definition
>
> rr[x_] := Block[{x}, x = 5; Print["x is ", x]]
>
> what do you expect here?
>
> rr[z] --> x is 5 is printed.
> but you might not expect
>
> rr[4] --> "Block::"lvsym": "Local variable specification{4} contains 4
> which is not a symbol or an assignment to a symbol."
>
> If you use Module instead of Block, you get
> the same.
>
> However,
> Block[{x = 4}, Block[{x}, x = 5; Print["x is ", x]]]
>
> prints x is 5
>
> Using pattern matching for
> substituting 4 for x in rr[4] even when x is bound seems
> to be counter to what most programming language designers
> would expect.
>
>
>
> Now look at
>
> uu[x_?((x = 5; Print["x is ", x]; True) &)] := x
>
> Usually when one defines a program, nothing is printed.
> Here, we get
> x is 5
> 5
> x is 5
>
> If we do this:
> x =400
> uu[70]
> x is 5 printed
> the value returned is 70
> and the global x is 5.
>
> Try this:
> Module[{x}, uu[x_?((x = 5; Print["x is ", x]; True) &)] := x;
> Print["in Module, x is " , x]]
>
> The expectation in a system supporting lexical scope
> with Module
> is that no use of x inside the module would escape.
> But it does. The global x is set to 5.
>
>
> The reason this all came up is in correspondence suggesting
> that programs in one computer algebra system could be
> translated into another. If systems are semantically
> "surprising", it is more difficult. I wonder how much
> of mathematica internally depends on wrong scope, or how
> much of the code is susceptible to bugs because of unexpected
> capture of names. (I suspect that this has caused a proliferation
> of package names e.g. MySecretNameSpace`x in Mathematica
> routines).
>
> RJF
>
>
Prev by Date:
**Re: scope all wrong? in Mathematica 4.1**
Next by Date:
**Solve InterpolatingFunction problem**
Previous by thread:
**Re: scope all wrong? in Mathematica 4.1**
Next by thread:
**Re: scope all wrong? in Mathematica 4.1**
| |