Advanced Issues: More Versatile Control Structures


 

An old idea in the BETA community, and a good one, is to provide "value" versions of the built-in control structures. This has been partially implemented in gbeta (but not in the Mjolner BETA System compiler). Such versions are used in assignment context or in evaluation context.

An example of a "value" version of an if-imperative is:

(if b then 1 else a+b if) -> putint

This works like:

1 -> putint

if b evaluates to true, and it works like:

a+b -> putint

if b evaluates to false. This is of course a similar semantics as that of the if-statement in functional languages such as SML or Haskell. The assignment variant:

x+y*y -> (if b then putint else x if)

resembles a (non-standard) usage of the ternary "?:" operator in C, meaning:

x+y*y -> putint

if b evaluates to true, and:

x+y*y -> x

if b evaluates to false. Of course there is a double-sided version of this, too:

x+y*y -> (if b then putint; 3 else a; a+b if) -> b

The reason why this is not entirely trivial (perhaps it is ;-) is that it is a surprisingly handy way to express a lot of things.

There should be a similar "value" version of the general if-imperative, i.e. the control structure in BETA which resembles switch in C (not too much, mind you!), but this has not yet been implemented. Similarly, there should be a "value" version of the for imperative which would iterate the transfer of values such that:

1 -> (for 5 repeat i; i+1 for) -> putint

would be the same as:

1->i; 
i+1->i; 
i+1->i; 
i+1->i; 
i+1->i; 
i+1->putint

After this slightly less serious section, we'll leave the language gbeta and look at a few changes in the modularization system.

 


Signed by: eernst@cs.auc.dk. Last Modified: 3-Jul-01