Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
weird thing with say ++$
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Fayland  
View profile  
 More options Apr 21 2005, 4:32 am
Newsgroups: perl.perl6.compiler
From: fayl...@gmail.com (Fayland)
Date: Thu, 21 Apr 2005 16:32:41 +0800
Local: Thurs, Apr 21 2005 4:32 am
Subject: [pugs]weird thing with say ++$
It has been published at perl6.language, but have no reply.

In perl v5.8.6 built for MSWin32-x86-multi-thread:

my $i = 1;
print $i++, ++$i; # 1 3
my $i = 1;
print ++$i, $i++; # 3 2

in pugs:

my $i = 1;
say $i++, ++$i; # 1 3

my $i = 1;
say ++$i, $i++; # 2 2

which is right?(I think perl5 is) or it's different between Perl5 and Perl6?

thanks.

/Fayland Lam/
--------------
http://www.fayland.org


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Johnson  
View profile  
 More options Apr 21 2005, 5:45 am
Newsgroups: perl.perl6.compiler
From: p...@pjcj.net (Paul Johnson)
Date: Thu, 21 Apr 2005 11:45:27 +0200
Local: Thurs, Apr 21 2005 5:45 am
Subject: Re: [pugs]weird thing with say ++$

I think I understand the implementation details leading to each
behaviour, but rather than saying which was "right", I think I'd be
quite happy to see Perl6 copy (the ideas behind) C's rules regarding
sequence points and undefined behaviour.  I'm not so sure about
implementation defined and unspecified behaviour.

When I see code such as this, I usually encourage people to program Perl
as if it had sequence points and undefined behaviour.  This often
results in explaining what they are, but maybe that's not such a great
problem.

See http://www.eskimo.com/~scs/C-faq/faq.html, especially sections 3.8
and 11.33 for details.

--
Paul Johnson - p...@pjcj.net
http://www.pjcj.net


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Johan Vromans  
View profile  
 More options Apr 21 2005, 7:46 am
Newsgroups: perl.perl6.compiler
From: jvrom...@squirrel.nl (Johan Vromans)
Date: Thu, 21 Apr 2005 13:46:09 +0200
Local: Thurs, Apr 21 2005 7:46 am
Subject: Re: [pugs]weird thing with say ++$

Paul Johnson <p...@pjcj.net> writes:
> I think I understand the implementation details leading to each
> behaviour, but rather than saying which was "right", I think I'd be
> quite happy to see Perl6 copy (the ideas behind) C's rules regarding
> sequence points and undefined behaviour.  I'm not so sure about
> implementation defined and unspecified behaviour.

Isn't this the old prefix-++ problem:

  @a = (++$i,++$i,++$i);
  print "@a";    # prints 3 3 3

-- Johan


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nathan Gray  
View profile  
 More options Apr 21 2005, 9:02 am
Newsgroups: perl.perl6.compiler
From: kolib...@graystudios.org (Nathan Gray)
Date: Thu, 21 Apr 2005 09:02:42 -0400
Local: Thurs, Apr 21 2005 9:02 am
Subject: Re: [pugs]weird thing with say ++$

It certainly makes more sense to me that the answer would be 2 2.  But
however it ends up, so long as we know what the answer will be, we can
utilize it effectively in our programs.

-kolibrie


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matthew Walton  
View profile  
 More options Apr 21 2005, 9:28 am
Newsgroups: perl.perl6.compiler
From: matt...@alledora.co.uk (Matthew Walton)
Date: Thu, 21 Apr 2005 14:28:52 +0100 (BST)
Local: Thurs, Apr 21 2005 9:28 am
Subject: Re: [pugs]weird thing with say ++$

> On Thu, Apr 21, 2005 at 11:45:27AM +0200, Paul Johnson wrote:
> It certainly makes more sense to me that the answer would be 2 2.  But
> however it ends up, so long as we know what the answer will be, we can
> utilize it effectively in our programs.

The trick with this construct usually in C is that the C standard doesn't
specify the order of evaluation of function arguments, so you can never be
sure if you'll get the same result if you compile it other than on your
development system (different compilers may evaluate them in a different
order). The Pugs example given in the original post seems to me to be
fairly sane, as it shows left-to-right evaluation. The Perl 5 example, as
far as I can tell, shows left-to-right for the first case and
right-to-left for the second case, which is just odd... please correct me
if I'm wrong, but that seems the only way those answers could have been
arrived at.
So really, what needs to be said is how Perl 6 is supposed to evaluate the
arguments to function calls, then we can know if the current behaviour in
Pugs is correct.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Macdonald  
View profile  
 More options Apr 21 2005, 10:12 am
Newsgroups: perl.perl6.compiler
From: j...@perlwolf.com (John Macdonald)
Date: Thu, 21 Apr 2005 10:12:51 -0400
Local: Thurs, Apr 21 2005 10:12 am
Subject: Re: [pugs]weird thing with say ++$

That "trick" in the C language is not an accident, but
deliberate.  Leaving the result undefined allows compiler
writers to choose the definition that fits best for that
compiler's internal structure and/or the target hardware
architechture.  Thus, when people write code that doesn't
happen to care about the abiguity in the spec, it is processed
(run/compiled) in the best way the compiler writer can provide.

In C, at least, you can't always tell whether a particular
statement is ambiguous (e.g. "i = *p1++ + *p2++" - do p1 and p2
always point to different memory locations?) and the cost of
treating it in an ambiguity-safe manner might be significant,
since C tends to get used for low level critical operations.

The approach is: make the programmer handle the rare tough cases
himself and let the compiler do the common job really well.
That is similar to "easy things should be easy, hard things
should be possible" but C puts more emphasis on run time speed
than programmer utility in how it applies its philosophy.

--


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Philip Schubiger  
View profile  
 More options Apr 24 2005, 1:03 am
Newsgroups: perl.perl6.compiler
From: ste...@accognoscere.org (Steven Philip Schubiger)
Date: Sun, 24 Apr 2005 07:03:14 +0200 (CEST)
Local: Sun, Apr 24 2005 1:03 am
Subject: Re: [pugs]weird thing with say ++$
On 21 Apr, fayland wrote:

: It has been published at perl6.language, but have no reply.

That was to be expected, as it's no language-design specific issue,
and therefore, unsuitable for p6l.

: In perl v5.8.6 built for MSWin32-x86-multi-thread:
:
: my $i = 1;
: print $i++, ++$i; # 1 3
: my $i = 1;
: print ++$i, $i++; # 3 2

You're misleaded here, by thinking, scalar context is being enforced,
which is not the case; the results of the increment operator operations (ops)
on the scalar are being passed in (implicit) array context to the print builtin.

Every Perl program, that gets compiled and interpreted, will be
converted to a syntax tree initially. The following is a selected excerpt
from the output of the core module B::Terse, which displays, what syntax
tree we actually got hold of.

    LISTOP (0x3c013760) print
        OP (0x3c013780) pushmark
        UNOP (0x3c013700) postinc [2]
            OP (0x3c0136e0) padsv [1]
        UNOP (0x3c013740) preinc
            OP (0x3c013720) padsv [1]

So, LISTOP indicates, that a list context is being encountered, which is
not, what we actually want. The syntax trees for the two examples,
provided above, don't differ that much, except, as you'd assume, in case
of the ordering of the UNOPs. I don't see any way to explain, what's
going wrong *exactly* by examining the syntax tree; that'd require
digging into the internals, I'd assume.

        my $i = 1;

and either

        print $i++; print ++$i;
or
        print ++$i; print $i++;

does what you assumed, it was supposed to do, but without
having the compiler struggling against implicit assumptions.

: in pugs:
:
: my $i = 1;
: say $i++, ++$i; # 1 3
:
: my $i = 1;
: say ++$i, $i++; # 2 2

This leads me to think, Pugs has implemented the behaviour, that has
been outlayed in Apocalypse 03: Operators, RFC 082 - Arrays: Apply
operators element-wise in a list context,
http://dev.perl.org/perl6/doc/design/apo/A03.html

: which is right?(I think perl5 is) or it's different between Perl5 and
Perl6?

I'm not inclined to think, that Perl 5 is wrong here, as letting it
assume array context, is in this context, like assuming, filling your
fridge with food, it consists of, by randomly throwing some of it in,
will come out sorted again; Perl 6 does, what the specifications urge
upon it to do.

: /Fayland Lam/

Steven


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google