views:

263

answers:

7

For my curiosity sake I'm looking for a dynamic object oriented language that allows you to change true to false and vice versa.

Something like this:

true = false, false = true;

This should also affect any conditional statements, therefore 42 == 42 should return False. Basically, with this premise, nothing in the language would be safe from the programmer.

Is there any language like this?

+1  A: 

C/C++ would allow you to define macros so TRUE==FALSE and FALSE==TRUE You could also overload the == operator to be equivalent to != (and vice versa) as a separate step.

Program.X
I was aware that c/c++ allows you to use macros for that, I was looking for something more dynamic than macros. Thanks for the answer though!
Maushu
I don't think C++ lets you redefine operators where all parameters are primitive types.
Mike DeSimone
and this doesn't deal with the case of if(foo())...
Brian Postow
You'd also have to overload `==` for every single class you used, and you can't do it for base types. `1 == 2` is going to be false, no matter what you do in C++.
David Thornley
Fair point, guys. For some reason, I've never had the need to find out!
Program.X
+2  A: 

Python (before version 3, apparently):

True = False
print True, False

>>False False

But:

True = False
print 1==1, 1!=2, 2!=2

>>True True False

For details, see, for example:

  1. Why can’t Python handle true/false values as I expect?

  2. Has TRUE always had a non-zero value?

mlvljr
But then what happens if you type "print False"? B-)
Brian Postow
Works in Python 2.6. In Python 3.1, I get `SyntaxError: assignment to keyword`.
Samir Talwar
damn, they fixed it! :)
mlvljr
A: 

In the unlikely event that I wanted this, I would just re-define if, (or make IF a macro and then use it...)

Brian Postow
+1  A: 

There is a difference between changing the literals/constants that represent true and false and changing the meaning of boolean expressions. The first is easy and can be done trivially with either the program source, for example in C:

#define TRUE 0
#define FALSE 1

or the compiler source.

But changing how boolean expressions are evaluated in all circumstances would be less trivial but not too difficult. It would once again require changes to the compiler or interpreter source, though.

anon
Yes, but not dynamically.
Maushu
+1  A: 

Tcl might do what you want. Maybe. You can't exactly redefine true and false since tcl doesn't really have such a thing as a true or false object or value, but you can redefine all the commands that act on expressions to return the opposite of what it normally does. For example, you can redefine "if" to return the opposite of the core definition of if.

Here's a trivial, incomplete example:

# keep the old if around
rename if ::_if

# make a new if with a negated condition
proc ::if {condition args} {
     set command [list _if !($condition) {*}$args]
     uplevel $command
}

Running this interactively I get:

% if true {puts true} else {puts false}
false
% if 1==1 {puts true} else {puts false}
false

(requires tcl 8.5+)

Bryan Oakley
+2  A: 

There are two possible answers to your question.

One possibility: you might still be using true and false, but you live in a foreign land where you call them by different names. In that case, it's just a matter of what the compiler/debugger environment displays - that's changeable by modifying the programming tools. (Or, you could start with LISP, which lacks a boolean primitive.)

Or, maybe what you're saying is that you want it to have consequences. For example, if you wanted this not to print anything:

if (42==42) {
   print("This is true");
}

The consequences of this are beyond my ability to imagine, but if you were to redefine conditional primitives (if, switch, etc.) you could do it. It would probably be easiest in a bare-bones LISP, by creating new versions of those conditionals. Looks like Bryan discussed that when talking about Tcl.


Aside: Let's say you create a language where false is true, true is false, ifs are if nots, !=s are ==s, and so on. Perhaps if you inverted enough things you'd get back to the original language. :)

Chris
For the aside: The complete circle kind of thing? Mathematically two negatives do make a positive. Hmm.
Maushu
+4  A: 

Smalltalk will let you do this.

Everything in Smalltalk-80 is available for modification from within a running program. This means that, for example, the IDE can be changed in a running system without restarting it. In some implementations, the syntax of the language or the garbage collection implementation can also be changed on the fly. Even the statement true become: false is valid in Smalltalk, although executing it is not recommended. When used judiciously, this level of flexibility allows for one of the shortest required times for new code to enter a production system.

http://en.wikipedia.org/wiki/Smalltalk

Dave
I seem to remember this language and I didn't know it could do that. It seems to be exactly I was looking for. Thanks!
Maushu