Re: Overloading the tilde operator?

2007-02-08 Thread Markus Triska
[EMAIL PROTECTED] writes:

> Also some flavours of Prolog, as descrived in the classic book by

op/3 is part of the Prolog ISO standard:

http://pauillac.inria.fr/~deransar/prolog/bips.html#operators

so every compliant implementation has it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overloading the tilde operator?

2007-02-08 Thread gatti
On Feb 8, 7:02 am, Dave Benjamin <[EMAIL PROTECTED]> wrote:
> Neil Cerutti wrote:
> > There's been only one (or two?) languages in history that
> > attempted to provide programmers with the ability to implement
> > new infix operators, including defining precedence level and
> > associativity (I can't think of the name right now).
>
> You're probably thinking of SML or Haskell. OCaml also allows you to
> define new infix operators, but the associativities are fixed (and
> determined by what punctuation you use).

Also some flavours of Prolog, as descrived in the classic book by
Clocksin & Mellish.

Regarding the OP, I hope his need for an infix tilde operator is
overestimated; there are plenty of infix operators that can be abused,
and at least one of them should be unused and available for
redefinition.

Lorenzo Gatti

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overloading the tilde operator?

2007-02-07 Thread greg
Dave Benjamin wrote:
> Neil Cerutti wrote:
> 
>> There's been only one (or two?) languages in history that
>> attempted to provide programmers with the ability to implement
>> new infix operators, including defining precedence level and
>> associativity (I can't think of the name right now).
> 
> You're probably thinking of SML or Haskell. OCaml also allows you to 
> define new infix operators, but the associativities are fixed (and 
> determined by what punctuation you use).

Prolog lets you do this, too.

In Smalltalk, you can use just about any sequence of
non-letters as an infix operator, but it has no notion
of precedence, even for the built-in operators.

I think SNOBOL may have had something for defining new
operators, but I can't remember the details.

--
Greg
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overloading the tilde operator?

2007-02-07 Thread Dave Benjamin
Neil Cerutti wrote:
> There's been only one (or two?) languages in history that
> attempted to provide programmers with the ability to implement
> new infix operators, including defining precedence level and
> associativity (I can't think of the name right now).

You're probably thinking of SML or Haskell. OCaml also allows you to 
define new infix operators, but the associativities are fixed (and 
determined by what punctuation you use).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overloading the tilde operator?

2007-02-02 Thread greg
James Stroud wrote:

> You haven't addressed why the limitation isn't arbitrary.

It's not arbitrary because there is a built-in meaning
for infix minus, but not for infix tilde.

Python doesn't go out of its way to provide operators
which aren't used by at least one built-in type.

--
Greg
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overloading the tilde operator?

2007-02-02 Thread Neil Cerutti
On 2007-02-02, Ben Finney <[EMAIL PROTECTED]> wrote:
> James Stroud <[EMAIL PROTECTED]> writes:
>> Ben Finney wrote:
>> > The Python runtime parser is designed to parse Python, not
>> > some arbitrary language that someone chooses to implement in
>> > Python.
>>
>> You haven't addressed why the limitation isn't arbitrary.
>
> Good thing I wasn't trying to do that, then. I was pointing out
> the source of the limitation.
>
> The Python syntax parser must follow the rules of the Python
> language. If you accept that premise, it follows that the '~'
> operator is unary only. If you *don't* accept that premise, I
> have no help to offer.

There's been only one (or two?) languages in history that
attempted to provide programmers with the ability to implement
new infix operators, including defining precedence level and
associativity (I can't think of the name right now).

C++, for example, works the same way as Python here. You can
override most of the operators, but you cannot change their
arity, associativity, or precedence level.

-- 
Neil Cerutti
Let us join David and Lisa in the celebration of their wedding and bring their
happiness to a conclusion. --Church Bulletin Blooper
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overloading the tilde operator?

2007-02-01 Thread Ben Finney
James Stroud <[EMAIL PROTECTED]> writes:

> Ben Finney wrote:
> > The Python runtime parser is designed to parse Python, not some
> > arbitrary language that someone chooses to implement in Python.
>
> You haven't addressed why the limitation isn't arbitrary.

Good thing I wasn't trying to do that, then. I was pointing out the
source of the limitation.

The Python syntax parser must follow the rules of the Python
language. If you accept that premise, it follows that the '~' operator
is unary only. If you *don't* accept that premise, I have no help to
offer.

-- 
 \ "I cannot conceive that anybody will require multiplications at |
  `\   the rate of 40,000 or even 4,000 per hour ..."  -- F. H. Wales, |
_o__) 1936 |
Ben Finney

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overloading the tilde operator?

2007-02-01 Thread George Sakkis
On Feb 2, 12:49 am, James Stroud <[EMAIL PROTECTED]> wrote:

> Ben Finney wrote:
>
> > The Python runtime parser is designed to parse Python, not some
> > arbitrary language that someone chooses to implement in Python.
>
> You haven't addressed why the limitation isn't arbitrary.

Indeed, and that's because it is arbitrary. Python has the arbitrary
limitation that it's not Perl (or C, or Lisp or what have you).

George

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overloading the tilde operator?

2007-02-01 Thread James Stroud
Ben Finney wrote:
> James Stroud <[EMAIL PROTECTED]> writes:
> 
> 
>>Peter Otten wrote:
>>
>>>Chris wrote:
>>>
I am trying to overload the __invert__ operator (~) such that it
can take a second argument,
>>>
>>x ~ x
>>>
>>>  File "", line 1
>>>x ~ x
>>>  ^
>>>SyntaxError: invalid syntax
>>
>>Seems an arbitrary limitation. Consider
>>   - x
>>and
>>   x - y
> 
> 
> Both of which are meaningful syntax in Python, hence the Python parser
> accepts them and knows what operations to call on the objects.
> 
> 
>>Which is inconsistent with limiting ~ to a unary operation.
> 
> 
> Which is the only Python-meaningful way to use that operator, and
> translates to an appropriate call on the object.
> 
> The Python runtime parser is designed to parse Python, not some
> arbitrary language that someone chooses to implement in Python.
> 

You haven't addressed why the limitation isn't arbitrary. You have only 
told us what we already know. So your argument, therefore, is not one.

James
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overloading the tilde operator?

2007-02-01 Thread Ben Finney
James Stroud <[EMAIL PROTECTED]> writes:

> Peter Otten wrote:
> > Chris wrote:
> >>I am trying to overload the __invert__ operator (~) such that it
> >>can take a second argument,
> > 
> x ~ x
> >   File "", line 1
> > x ~ x
> >   ^
> > SyntaxError: invalid syntax
>
> Seems an arbitrary limitation. Consider
>- x
> and
>x - y

Both of which are meaningful syntax in Python, hence the Python parser
accepts them and knows what operations to call on the objects.

> Which is inconsistent with limiting ~ to a unary operation.

Which is the only Python-meaningful way to use that operator, and
translates to an appropriate call on the object.

The Python runtime parser is designed to parse Python, not some
arbitrary language that someone chooses to implement in Python.

-- 
 \ "You know what would make a good story? Something about a clown |
  `\who makes people happy, but inside he's real sad. Also, he has |
_o__)severe diarrhea."  -- Jack Handey |
Ben Finney

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overloading the tilde operator?

2007-02-01 Thread James Stroud
Peter Otten wrote:
> Chris wrote:
> 
> 
>>I am trying to overload the __invert__ operator (~) such that
>>it can take a second argument, other than
>>self, so that I can express:
>>
>>x ~ y
>>
>>by using:
>>
>>def __invert__(self, other): 
>>
>>for example. Is this possible?
> 
> 
> No, you will get a syntax error before python even look up the names:
> 
> 
x
> 
> Traceback (most recent call last):
>   File "", line 1, in ?
> NameError: name 'x' is not defined
> 
x ~ x
> 
>   File "", line 1
> x ~ x
>   ^
> SyntaxError: invalid syntax
> 
> Peter

Seems an arbitrary limitation. Consider

   - x

and

   x - y

Which is inconsistent with limiting ~ to a unary operation.

James
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overloading the tilde operator?

2007-02-01 Thread bearophileHUGS
Peter Otten:
> No, you will get a syntax error before python even look up the names:

There are some tricks that allow the use of "undefined" symbols in
Python too, but they are probably just toys. I have recently posted a
recipe in the cookbook for that.

Bye,
bearophile

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overloading the tilde operator?

2007-02-01 Thread Peter Otten
Chris wrote:

> I am trying to overload the __invert__ operator (~) such that
> it can take a second argument, other than
> self, so that I can express:
> 
> x ~ y
> 
> by using:
> 
> def __invert__(self, other): 
> 
> for example. Is this possible?

No, you will get a syntax error before python even look up the names:

>>> x
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'x' is not defined
>>> x ~ x
  File "", line 1
x ~ x
  ^
SyntaxError: invalid syntax

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


Overloading the tilde operator?

2007-02-01 Thread Chris
I am trying to overload the __invert__ operator (~) such that 
it can take a second argument, other than 
self, so that I can express:

x ~ y

by using:

def __invert__(self, other): 

for example. Is this possible?

Thanks in advance,


-- 
http://mail.python.org/mailman/listinfo/python-list