3
$\begingroup$

I am designing a programming language and I want to give everything the ability to return a value. For example, if I use a block as a right-hand side value, I can assign it to a lvalue:

sum := {
    if (a < 0) { a := -a; }
    if (b < 0) { b := -b; }
    return a + b;
};

Also, if and while statements could return a value, and there is no need for something like a ternary operator then.

After a bit of research, I have found that Rust is capable of doing this. And this is great for language design, because it significantly reduces the number of language constructs needed. You don't really need a do-while loop, as that's just a while loop with a braced condition:

do {
    foo();
} while (a > 0);

Is equivalent to:

while {
    foo();
    return a > 0;
}{}

Also, with ignored variable bindings, you can create continue and break statements for loops:

// Break
_ := while (a > 0) {
    if (a == 5) {
        return ();  // Empty tuple
    }
    a += 1;
};
// Continue
while (a > 0) { _ := {
    if (a == 5) {
        return ();  // Empty tuple
    }
    a += 1;
};}

I like the idea of having less language constructs, because the language is expressive enough to do this without labeling and goto statements.

But as I was thinking about this, two things popped into my head:

  1. Are these just very abstract lambda expressions?
  2. How could the 'return destination' be labeled so we can return from a very nested expression even right out to the function? I think I would probably need some extra notation for that.
$\endgroup$
6
  • $\begingroup$ "I like the idea of having less language constructs" -- trade-offs abound! It doesn't take much to be Turing complete; most language features are "only" sugar! $\endgroup$
    – Raphael
    Jun 20, 2017 at 13:06
  • $\begingroup$ "Is there a language that has already done something similar?" -- I think that you should remove this since it's offtopic here. That said, IIRC every Ruby statement "returns a value" in the sense that every statement is an expression. $\endgroup$
    – Raphael
    Jun 20, 2017 at 13:07
  • $\begingroup$ @Raphael Sorry, I've removed that. I also know that most of the things are just syntax sugar, but I like to give programmers a different set of building blocks, not the style extremes, like lambda calculus or assembly. $\endgroup$ Jun 20, 2017 at 13:14
  • 2
    $\begingroup$ @Raphael: I don't like that questions related to programming languages are considered off-topic. As someone doing research in programming languages, it is really annoying. "Is there a language that has already done something similar?" is akin to "Is there any prior research on this topic?" $\endgroup$ Jun 20, 2017 at 13:20
  • 1
    $\begingroup$ @DaveClarke I think that questions related to PLs are on-topic here, as long as the are about general concepts of PL design/implementation/analysis/etc. (as the tag itself states). Questions about specific PLs like "how does this work in C++" are off-topic, and SO material. Questions like "was this done before in some PL" sound general enough for me, and I would consider them on-topic (?) -- I think I agree with you on this. $\endgroup$
    – chi
    Jun 20, 2017 at 13:29

1 Answer 1

4
$\begingroup$

1) In a sense, yes. Guy Steele wrote the "Lambda papers", where he reduced many different constructs to lambda expressions, in a compositional way. He used the lambda abstractions of Scheme for this, but the construction is general.

2) You should read about continuation passing style. Scheme, for instance, uses "call with current continuation" call/cc to achieve what you describe: "returning" from a deeply nested location.

call/cc is probably not the most convenient notation, but it works, and can be used with any expression. E.g.

(call/cc (lambda (k)
  (+ 1 (if (some-condition) 10 (k 2)))

Above, if the condition is true, the if evaluates to 10, which is added to 1, so that the whole result is 11. If it is false, (k 2) is evaluated, "returning" to the point call/cc was used with result 2. Note that this skips the (+ 1 ..) part. You can nest call/cc arbitrarily, providing many "return points" as wanted.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.