Advanced Issues: Using the Code Twice


 

The module system in gbeta is basically the same as in the Mjolner BETA System, i.e. it is based on the fragment language and the notion of a fragment graph whose edges are specified using properties at the top of source code files such as ORIGIN 'betaenv'. A few changes have been introduced, though, because it was so easy in the given setting.

Firstly, since the implementation of the fragment language builds on a search-and-replace semantics with added checks in the fragment graph for correct visibility decisions, the fragment system in gbeta allows all non-terminals in the grammar as slots. However, the parser does not allow slot applications in all positions of the syntax, and the error checking which should detect inconsistencies is incomplete. Hence, you can use a very large subset of the (ideal) fragment language, much more than supported by the Mjolner BETA System, but real separate compilation would require significant changes in the current implementation of gbeta, and there are some glitches. If things start behaving in strange ways, please go back and use the fragment language in more conventional ways..

One of the genuinely new properties of the gbeta fragment language is that it is possible to have more than one ORIGIN property in one fragment group (i.e. source code file), and it is possible to have more than one slot application for a given slot declaration.

This is the reason for the title of this section. It looks like this, e.g.:

-- program:merge --
(#
   p: (# x:< object <<SLOT doSomething:dopart>> #);
   q: (# x: @integer <<SLOT doSomething:dopart>> #);
do 
   ..
#)

-- doSomething:dopart --
do x

As you can see, the name application "x" ends up in two different contexts, and this means that two entirely separate sets of information from static analysis ("semantic attributes") must be maintained. The reason why this might be useful is that it makes it possible to express and exploit the fact that two implementations of something may be different when it comes to the type system, but still so related at some (conceptual) level that the source code is syntactically identical.

Example 10

Here's a runnable example of that:

(* FILE aex10.gb *)
-- betaenv:descriptor --
(#
   p: (# x,y,z: @integer 
      do (2,3)->(x,y); 
         <<SLOT xplusy:expression>> -> z;
         (if z=5 then 'Hello'->stdio if)
      #);
   q: (# x:< string; y: (# exit 'world!\n' #)
      do p; <<SLOT xplusy:expression>> -> stdio
      #);
   r: q(# x::(# do ', '->value #)#)
do
   r
#)

-- xplusy:expression --
x+y

You might prefer to single-step this program on the command line, since the pretty printing shows the resulting program, after the search-and-replace process which eliminates all fragment forms except betaenv.
 


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