2

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?

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Diogo Gomes
  • 2,385
  • 1
  • 19
  • 29
  • 3
    As a hypothetical user of this hypothetical language, I'd just invert all my boolean conditions and that's it. Nothing "unsafe" about that... – Mauricio Scheffer Mar 17 '10 at 17:40
  • Also, nothing would be safe FOR the programmer. Why would you ever want to do this? I know that there were old old versions of FORTRAN that allowed you to re-define 1, but that's about it... – Brian Postow Mar 17 '10 at 17:40
  • 5
    The existence of such a language would scare me. Such a feature is only good for manufacturing bugs out of thin air. – Mike DeSimone Mar 17 '10 at 17:41
  • 3
    This is for a nerd scavenger hunt, isn't it? – Greg Mar 17 '10 at 17:46
  • 1
    If such theorical language allowed this, it means it might allow other interesting (and much more useful) stuff. Nerd scavenger hunt? Hmmm... – Diogo Gomes Mar 17 '10 at 17:47
  • 1
    eh, your example is flawed? true = false; wouldn't this declaration make the next statement false = false? – Makach Mar 17 '10 at 17:51
  • 1
    @Makach, some languages allow you to change content between variables in a single line by using `a = b, b = a`. – Diogo Gomes Mar 17 '10 at 18:05

7 Answers7

5

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

David Laughlin
  • 600
  • 1
  • 6
  • 11
  • 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! – Diogo Gomes Mar 17 '10 at 18:12
2

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?

Community
  • 1
  • 1
mlvljr
  • 4,066
  • 8
  • 44
  • 61
2

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
  • 5,876
  • 3
  • 43
  • 69
1

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
  • 7,250
  • 12
  • 49
  • 83
  • 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! – Diogo Gomes Mar 17 '10 at 17:41
  • I don't think C++ lets you redefine operators where all parameters are primitive types. – Mike DeSimone Mar 17 '10 at 17:42
  • 1
    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 Mar 17 '10 at 17:58
  • Fair point, guys. For some reason, I've never had the need to find out! – Program.X Mar 17 '10 at 18:58
1

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.

1

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
  • 370,779
  • 53
  • 539
  • 685
0

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
  • 11,709
  • 17
  • 81
  • 125