Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #8308 > unrolled thread

Funny Behavior of Module

Started byLouis Talman <talmanl@gmail.com>
First post2012-02-03 07:10 +0000
Last post2012-02-04 11:40 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Funny Behavior of Module Louis Talman <talmanl@gmail.com> - 2012-02-03 07:10 +0000
    Re: Funny Behavior of Module "Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com> - 2012-02-04 11:28 +0000
    Re: Funny Behavior of Module Roger Wilson <rogerhw999@gmail.com> - 2012-02-04 11:39 +0000
    Re: Funny Behavior of Module "Nasser M. Abbasi" <nma@12000.org> - 2012-02-04 11:40 +0000

#8308 — Funny Behavior of Module

FromLouis Talman <talmanl@gmail.com>
Date2012-02-03 07:10 +0000
SubjectFunny Behavior of Module
Message-ID<jgg195$bnb$1@smc.vnet.net>
I know that Module exhibits some funny behavior, but this behavior is 
something I've never seen before. It seems wrong to me.

In a new kernel session enter

f[x_] := Module[{a},a = x^2; x = y; a]

f[z]

z

Mathematica returns y for z, as it ought to. (In fact, the purpose of 
the code was to demonstrate to students what can happen when a Module 
monkeys with its formal parameters.)

But f[z] returns y^2. It seems to me that it should return z^2, but that 
for some reason it's interpreting the first Set instruction in the body 
of the Module as a SetDelayed instruction. (We get exactly the same 
behavior if we replace that Set with SetDelayed in the code.)

Can anyone explain what's going on here? ITABOAF?


--Lou Talman
  Department of Mathematical & Computer Sciences
  Metropolitan State College of Denver

  <http://rowdy.mscd.edu/%7Etalmanl>




[toc] | [next] | [standalone]


#8324

From"Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com>
Date2012-02-04 11:28 +0000
Message-ID<jgj4ol$msm$1@smc.vnet.net>
In reply to#8308
On Fri, 03 Feb 2012 07:10:29 -0000, Louis Talman <talmanl@gmail.com> wrote:

> I know that Module exhibits some funny behavior, but this behavior is
> something I've never seen before. It seems wrong to me.
>
> In a new kernel session enter
>
> f[x_] := Module[{a},a = x^2; x = y; a]
>
> f[z]
>
> z
>
> Mathematica returns y for z, as it ought to. (In fact, the purpose of
> the code was to demonstrate to students what can happen when a Module
> monkeys with its formal parameters.)
>
> But f[z] returns y^2. It seems to me that it should return z^2, but that
> for some reason it's interpreting the first Set instruction in the body
> of the Module as a SetDelayed instruction. (We get exactly the same
> behavior if we replace that Set with SetDelayed in the code.)
>
> Can anyone explain what's going on here? ITABOAF?
>
>
> --Lou Talman
>

f[z] does return z^2, in a sense. However, after setting a = z^2 in the  
Module, z is immediately set to y. So, in the output, z is duly replaced  
with y and the result appears as y^2. I'm not too sure where the source of  
confusion lies but perhaps it will help to recall that in pattern-matched  
definitions such as f[x_] := (some function of x), the parameter is  
directly substituted into the function body where x appears. Here x does  
not exist as a symbol in its own right; it just acts as a placeholder for  
whatever was pattern-matched to x_. If you pass a symbol that can't be  
acted on by Set, x = y does not apply and you will get a different result:

In :=
SetAttributes[q, Protected];
f[q]

During evaluation of In := Set::wrsym: Symbol q is Protected.

Out =
q^2

So, neither bug nor feature, but simply a confusing example.

[toc] | [prev] | [next] | [standalone]


#8345

FromRoger Wilson <rogerhw999@gmail.com>
Date2012-02-04 11:39 +0000
Message-ID<jgj5cp$n4h$1@smc.vnet.net>
In reply to#8308
When you evaluate f[z] every instance of x on the RHS is replaced by z.

So you have in reality executed...
a=z^2;z=y;a

[toc] | [prev] | [next] | [standalone]


#8347

From"Nasser M. Abbasi" <nma@12000.org>
Date2012-02-04 11:40 +0000
Message-ID<jgj5em$n5b$1@smc.vnet.net>
In reply to#8308
On 2/3/2012 1:10 AM, Louis Talman wrote:
> I know that Module exhibits some funny behavior, but this behavior is
> something I've never seen before. It seems wrong to me.
>
> In a new kernel session enter
>
> f[x_] := Module[{a},a = x^2; x = y; a]
>
> f[z]
>
> z
>
> Mathematica returns y for z, as it ought to. (In fact, the purpose of
> the code was to demonstrate to students what can happen when a Module
> monkeys with its formal parameters.)
>
> But f[z] returns y^2. It seems to me that it should return z^2, but that
> for some reason it's interpreting the first Set instruction in the body
> of the Module as a SetDelayed instruction. (We get exactly the same
> behavior if we replace that Set with SetDelayed in the code.)
>
> Can anyone explain what's going on here? ITABOAF?
>
>
> --Lou Talman
>    Department of Mathematical&  Computer Sciences
>    Metropolitan State College of Denver
>
>    <http://rowdy.mscd.edu/%7Etalmanl>
>

The program is not really a valid Mathematica code, inside
the function, the pattern x_ should always show up on the RHS
of any assignment, not on the LHS.
  
In your example this line

    x=y

is not valid, since the pattern x shows on the left side of
an assignment not on the right side.

As what happens when you wrote it the way you did:

a=x^2 ===> this becomes a=z^2 since x is z
next, you say x=y, which became z=y, since x is z.
next, the last thing you have is 'a'. Now Mathematica
will evaluate 'a', which is z^2. But now 'z' is 'y',
so it does one more transformation rule, Hence 'a' which is 'z^2'
evaluates to 'y^2'

And this is what you see.

This is no different than writing

------------------
Remove["Global`*"]
a=z^2;
z=y;
a
-------------------

and now you'll see that 'a' is y^2 and not z^2. Since
'a' evaluates 'a' to z^2, and 'z' evaluates to 'y^2',
and now the evaluator stops, since there is no more
transformation rules to apply.

One way to do what you did is by introducing a local symbol

-----------------------
Remove["Global`*"]
f[$x_] := Module[{a, x = $x}, a = x^2; x = y; a]
f[z]
--------------------------

now it returns  z^2. The reason the above returns z^2 and not
y^2, is because:

a=x^2 ===> this becomes a=z^2 since x is z
next, x=y, but now x is the local x and not z. So, x becomes y.
next, we have'a'. Now Mathematica will evaluate 'a', which is z^2,
and no more. z still is z. So what you get is z^2.

--Nasser

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web