Does this language feature already exist?

Posted by Pindatjuh on Stack Overflow See other posts from Stack Overflow or by Pindatjuh
Published on 2010-03-24T23:10:25Z Indexed on 2010/03/25 21:13 UTC
Read the original article Hit count: 157

I'm currently developing a new language for programming in a continuous environment (compare it to electrical engineering), and I've got some ideas on a certain language construction.

Let me explain the feature by explanation and then by definition:

x = a U b;

Where x is a variable and a and b are other variables (or static values). This works like a union between a and b; no duplicates and no specific order.

with(x) {
    // regular 'with' usage; using the global interpretation of "x"
    x = 5; // will replace the original definition of "x = a U b;"
}
with(x = a) {
    // this code block is executed when the "x" variable
    // has the "a" variable assigned. All references in
    // this code-block to "x" are references to "a". So saying:
    x = 5;
    // would only change the variable "a". If the variable "a"
    // later on changes, x still equals to 5, in this fashion:
    // 'x = a U b U 5;'
    // '[currentscope] = 5;'
    // thus, 'a = 5;'
}
with(x = b) {
    // same but with "b"
}
with(x != a) {
    // here the "x" variable refers to any variable
    // but "a"; thus saying
    x = 5;
    // is equal to the rewriting of
    // 'x = a U b U 5;'
    // 'b = 5;' (since it was the scope of this block)
}
with(x = (a U b)) {
    // guaranteed that "x" is 'a U b'; interacting with "x"
    // will interact with both "a" and "b".
    x = 5;
    // makes both "a" and "b" equal to 5; also the "x" variable
    // is updated to contain:
    // 'x = a U b U 5;'
    // '[currentscope] = 5;'
    // 'a U b = 5;'
    // and thus: 'a = 5; b = 5;'.
}
// etc.

In the above, all code-blocks are executed, but the "scope" changes in each block how x is interpreted. In the first block, x is guaranteed to be a: thus interacting with x inside that block will interact on a. The second and the third code-block are only equal in this situation (because not a: then there only remains b). The last block guarantees that x is at least a or b.

Further more; U is not the "bitwise or operator", but I've called it the "and/or"-operator. Its definition is:

"U" = "and" U "or"

(On my blog, http://cplang.wordpress.com/2009/12/19/binop-and-or/, there is more (mathematical) background information on this operator. It's loosely based on sets. Using different syntax, changed it in this question.)

Update: more examples.

print = "Hello world!" U "How are you?"; // this will print
                                         // both values, but the
                                         // order doesn't matter.
// 'userkey' is a variable containing a key.
with(userkey = "a") {
    print = userkey; // will only print "a".
}
with(userkey = ("shift" U "a")) {
    // pressed both "shift" and the "a" key.
    print = userkey; // will "print" shift and "a", even
                     // if the user also pressed "ctrl":
                     // the interpretation of "userkey" is changed,
                     // such that it only contains the matched cases.
}
with((userkey = "shift") U (userkey = "a")) {
    // same as if-statement above this one, showing the distributivity.
}

x = 5 U 6 U 7;
y = x + x; // will be:
// y = (5 U 6 U 7) + (5 U 6 U 7)
//   = 10 U 11 U 12 U 13 U 14

somewantedkey = "ctrl" U "alt" U "space"
with(userkey = somewantedkey) {
    // must match all elements of "somewantedkey"
    // (distributed the Boolean equals operated)
    // thus only executed when all the defined keys are pressed
}
with(somewantedkey = userkey) {
    // matches only one of the provided "somewantedkey"
    // thus when only "space" is pressed, this block is executed.
}

Update2: more examples and some more context.

with(x = (a U b)) {
    // this
}
// can be written as
with((x = a) U (x = b)) {
    // this: changing the variable like
    x = 5;
    // will be rewritten as:
    // a = 5 and b = 5
}

Some background information: I'm building a language which is "time-independent", like Java is "platform-independant". Everything stated in the language is "as is", and is continuously actively executed. This means; the programmer does not know in which order (unless explicitly stated using constructions) elements are, nor when statements are executed. The language is completely separated from the "time"-concept, i.e. it's continuously executed:

with(a < 5) {
    a++;
} // this is a loop-structure;
  // how and when it's executed isn't known however.

with(a) {
    // everytime the "a" variable changes, this code-block is executed.
    b = 4;
    with(b < 3) {
        // runs only three times.
    }
    with(b > 0) {
        b = b - 1; // runs four times
    }
}

Update 3:

After pondering on the type of this language feature; it closely resemblances Netbeans Platform's Lookup, where each "with"-statement a synchronized agent is, working on it's specific "filter" of objects. Instead of type-based, this is variable-based (fundamentally quite the same; just a different way of identifiying objects).

I greatly thank all of you for providing me with very insightful information and links/hints to great topics I can research. Thanks.

I do not know if this construction already exists, so that's my question: does this language feature already exist?

© Stack Overflow or respective owner

Related posts about language-features

Related posts about dataflow