Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Q: Can colons control backtracking in logical expressions?

8 views
Skip to first unread message

Austin Hastings

unread,
Apr 2, 2004, 7:49:21 AM4/2/04
to Perl6 Language

Consider this code:

if (specific_condition())
{
if (detail())
{
act();
}
}
elsif (general_condition())
{
act();
}

Logically, this turns into:

if ((my $sc = specific_condition()) && detail())
|| (!$sc && general_condition())
{
act();
}

Both look horrible, of course. I'd like to rewrite them as

if (specific_condition() :: && detail()) || general_condition()
{
act();
}

so that if specific_condition() succeeded, it would cause the entire
expression to fail if detail() failed.

The use of :: comes, of course, from rexen.

Is this feasible?

=Austin

Damian Conway

unread,
Apr 2, 2004, 8:09:16 AM4/2/04
to Perl6 Language
> All you really need is:
>
> if ( specific_condition() ? detail() : general_condition() )


Oops! That's the Perl 5 version. In Perl 6, of course, it's:

if ( specific_condition() ?? detail() :: general_condition() )

Which means you *do* get to use the :: after all!
(Just not where you might have expected ;-)

Damian

Damian Conway

unread,
Apr 2, 2004, 8:05:24 AM4/2/04
to Perl6 Language
Austin Hastings wrote:

> Both look horrible, of course. I'd like to rewrite them as
>
> if (specific_condition() :: && detail()) || general_condition()
> {
> act();
> }
>
> so that if specific_condition() succeeded, it would cause the entire
> expression to fail if detail() failed.
>
> The use of :: comes, of course, from rexen.
>
> Is this feasible?

Not in Perl itself. But you could always put 'em in regexen:

if (/<(specific_condition())> :: <(detail())> | <(general_condition())>/)
{
act();
}

However that's just showing off. All you really need is:

if ( specific_condition() ? detail() : general_condition() )

{
act();
}

;-)

Damian

Austin Hastings

unread,
Apr 2, 2004, 8:40:42 AM4/2/04
to Damian Conway, Perl6 Language
> -----Original Message-----
> From: Damian Conway [mailto:dam...@conway.org]
>
> > Since paren's aren't required with keywords, can they still serve as rex
> > delimiters?
>
> No. For other syntactic reasons parens aren't allowed as rule
> delimiters at all. And only /.../ delimit "raw" matches. Every other
delimiter
> requires an explicit 'm'.

I keep forgetting poor C<m>.

> > Sadly, it doesn't generalize well:
> >
> > if (specific() :: && detail1() :: && detail2()) || general() {...}
> >
> > becoming
> >
> > if (specific() ? detail1() ? detail2() : FALSE : general() ) {...}
> >
> > to say nothing of readability.
>
> Your proposed version is hardly a model of readability either. ;-)

Hmm. You'll want to take that up with the folks responsible for the rex
syntax. :-O

> Besides, the correct solution there is just:
>
> if (specific() ?? detail1() && detail2() :: general()) {...}

For some value of "correct" I suppose. Using ??:: within an if/else context
makes my skin crawl, stylistically. :-(

=Austin

Simon Cozens

unread,
Apr 2, 2004, 9:05:30 AM4/2/04
to perl6-l...@perl.org
Austin_...@Yahoo.com (Austin Hastings) writes:
> > if (specific() ?? detail1() && detail2() :: general()) {...}
>
> For some value of "correct" I suppose. Using ??:: within an if/else context
> makes my skin crawl, stylistically. :-(

Ah, then use if!

if (if(specific()) { detail() } else { general() }) {
...
} else {
...
}
--
Complete the following sentence: People *ought* to weigh bricks, cats
and cinnamon in the same units because... - Ian Johnston

Larry Wall

unread,
Apr 2, 2004, 10:01:07 AM4/2/04
to perl6-l...@perl.org
On Fri, Apr 02, 2004 at 03:05:30PM +0100, Simon Cozens wrote:
: Austin_...@Yahoo.com (Austin Hastings) writes:
: > > if (specific() ?? detail1() && detail2() :: general()) {...}
: >
: > For some value of "correct" I suppose. Using ??:: within an if/else context
: > makes my skin crawl, stylistically. :-(
:
: Ah, then use if!
:
: if (if(specific()) { detail() } else { general() }) {
: ...
: } else {
: ...
: }

What's that? PL/Ruby?

In Perl 6 that would more clearly be written:

if do { if specific() { detail() } else { general() } } {
...
} else {
...
}

or, of course,

if specific() ?? detail() :: general() {
...
} else {
...
}

Sorry, Perl 6 is just not going to confuse statements and expressions.
A statement is the artificial equivalent of a sentence, and it's no
coincidence that every natural language has something like a sentence
for the purpose of clocking and resynchronizing the discourse.
I think pinning the large-scale control structures to the discourse
structure is a really good idea, both for the computer trying to
decide what error message to spew, and for the person who is going
to have to try to decipher that error message. To break that in
the name of orthogonality ignores the linguistic evidence built up
over many thousands of years. Even an almost purely RPN language
like Japanese likes to use honorifics and/or a bunch of particles to
mark the ends of its sentences, yo?

Larry

Aaron Sherman

unread,
Apr 2, 2004, 4:24:05 PM4/2/04
to Larry Wall, Perl6 Language List
On Fri, 2004-04-02 at 10:01, Larry Wall wrote:
> Even an almost purely RPN language
> like Japanese likes to use honorifics and/or a bunch of particles to
> mark the ends of its sentences, yo?

Hai, and it's not just there to help in error reporting. Imagine the
case:

if if if if if ... {...} else {...} {...} else {...} {...} else {...}
{...} else {...} {
...
} else {
...
}

so very, very far from readability. Even if you try to indent in some
logical way, it's just painful.

What's more, your,

if specific() ?? detail() :: general() { ... }

is also:

if ((my $s = ?specific()) && detail()) || (!$s && general()) { ... }

If you prefer to avoid trinary logic constructs directly. I'm not sure
if using the "?" there does what I want (which is to strip away any
potential side-effects on $s for when I evaluate it again). It would be
nice to have an un-lazying operator of some sort which could assert a
lack of side-effects as a side-effect.

--
Aaron Sherman <a...@ajs.com>
Senior Systems Engineer and Toolsmith
"It's the sound of a satellite saying, 'get me down!'" -Shriekback


Austin Hastings

unread,
Apr 2, 2004, 6:14:35 PM4/2/04
to Larry Wall, perl6-l...@perl.org


Or, as per the original:

if specific()
{
if detail()
{
act();
}
}
elsif general()
{
act();
}

> Even an almost purely RPN language like Japanese likes to use
> honorifics and/or a bunch of particles to mark the ends of its
> sentences, yo?

I'm witcha, homie. Word.

However, what I was trying for was not so much "destroy the end product of
thousands of years of linguistic evidence", as more of the kind of
abstractions that have happened before. In this case,

"A :: in a rex is just a form of 'cut', and 'cut' is a valid expression
modifier, so we'll put the same operators (or similar ones) to use outside
the context of rexen."

=Austin

0 new messages