. navigate
 Navigate
7. Procedures, Functions, and Parameters left arrow
x
right arrow 9. Exceptions
Red Reference Manual
8.

CAPSULES

Declaration & Invocation     Visible List



8.   CAPSULES

Capsules are the basic unit of separate translation (see Section 3.1) and can also be nested within other language constructs. Capsules can be used to create common data pools, libraries, and abstract data types, as well as independent, executable programs.

A capsule, like a procedure, is a deferred unit which is invoked. Capsules differ from procedures in that definitions local to a capsule body may be made known outside it. Selected definitions can be made locally known in each scope where the capsule is invoked. The body of a capsule can contain statements as well as definitions. These statements are elaborated to initialize variables and constants defined in the capsule.

There are two ways in which a capsule can be invoked.

  1. It can be invoked as new. Each such invocation will cause the capsule to be elaborated. Local variable and constant declarations in the capsule body create different variables and constants at each new invocation. Capsules invoked as new may be parameterized. Actual parameters are supplied each time a capsule is invoked and serve to specialize the capsule.

  2. It can be invoked as old. ln this case, all old invocations will reference a single version of the capsule which was elaborated upon entry to the scope where the capsule was defined. Each old invocation will reference the same set of variables and constants from the capsule. Capsules which are invoked as old may not have parameters.



NOTES

     Definitions that are known in a scope come from three sources: local definitions written in the scope, definitions which become locally known by invoking a capsule in that scope, and definitions which are available (either implicitly or through an imports list) from the enclosing scope.

RED RATIONALE

Among the encapsulation mechanisms provided by recent languages, capsules are most like modules in Modula. RED capsules can be used to define a single abstract data type, a group of related abstract data types, a group of related procedures (e.g., an arithmetic package) and a group of related data (a compool). In addition, the capsule serves as the unit of separate translation.

ABSTRACT DATA TYPES
An abstract data type is defined by writing a capsule that exports the type and the operations that determine the abstract behavior.

A capsule can be used to define several abstract types. This is useful when operations (such as conversion functions) need access to the underlying properties of two or more abstract types. Capsules can be nested in other capsules. This provides the ability to define two or more types with underlying properties hidden from one another, but with special access rights with respect to one another.

SEPARATE TRANSLATION
The ALGOL-60 rules require that a name's declaration be visible (according to the scope rules) at any point where the name is referenced. A direct consequence of this rule is that everything must be translated together. In the world of embedded applications, such an all-or-nothing philosophy is unacceptable.

the separate translation facility must support the integration of components produced by diverse sources, including libraries with conflicting name usages and programs written in other languages. RED satisfies all these requirements.





8.1  CAPSULE DECLARATION AND INVOCATION

capsule declaration and invocation diagram
C - identifier   2 - body   9 - imports   52 - formal parameters   53 - actual parameters
56 - visible list   67 - actual trans properties   81 - definable symbol

A capsule declaration is invoked to make selected definitions in its body known in the scopes where invocations of the capsule appear.



RULES

Capsule Declaration

Identifier I in a capsule declaration is defined to be a capsule in the scope in which the capsule declaration appears. Identifier 2 must be the same as identifier 1.

A capsule declaration which is invoked as old or which is a translation unit may not have any formal parameters. A capsule may not have formal parameters of the OUT binding class.



Capsule Invocation Declaration

The capsule with name identifier 1 is invoked.

The lifetime of actual parameters passed to READONLY and VAR formal parameters must be greater than or equal to the lifetime of the capsule invocation declaration containing those actual parameters.

A capsule invocation declaration has two effects: it causes elaboration of a capsule declaration and it makes selected definitions visible. There are two ways in which a capsule may be invoked:

  1. As a new invocation (if NEW is specified).

  2. As an old invocation (if NEW is not specified).
  3. Elaboration of a new capsule invocation declaration consists of

    1. elaborating the actual parameters;
    2. binding the actual parameters to the formal parameters of the invoked capsule (see Section 7.3}; and

    3. elaborating the body of the invoked capsule.

If there are any old invocations of a capsule, the body of the invoked capsule is elaborated once upon entry to the scope in which the capsule declaration appears.

A capsule invocation declaration makes selected definitions that are locally known in the body of the capsule also locally known in the scope where an invocation of the capsule appears (see Section 8.2). For new invocations, these definitions are the ones that have been created during elaboration of the capsule invocation declaration. For old invocations these definitions are the ones that were created upon entry to the scope where the capsule declaration appears.

The lifetime of any definitions made known by a new capsule invocation declaration is equal to the lifetime of the capsule invocation declaration. The lifetime of any definitions made known by an old capsule invocation is equal to the lifetime of the declaration of the invoked capsule.

If an invocation includes a RENAMING list, there must be a definition known as a result of the visible list) for each identifier 2 and definable symbol 2 that appears. For each item in the list of the form

    name 2 TO name 3

all definitions that would be known as name 2 are known instead as name 3 in the scope where the capsule invocation declaration appears. It name 3 is a definable symbol, all definitions of name 2 must satisfy all restrictions upon definitions of that definable symboL

If EXTERNAL is specified, then identifier 1 must be the name of some separate translation unit. lf EXTERNAL is not specified, a definition for identifier 1 must be known in the scope in which the capsule invocation declaration appears.

If there are several capsule invocation declarations local to the same scope, then none of these declarations may contain any uses of the definitions that it, or any later capsule invocation declaration, makes known.



NOTES

    A capsule declaration is the only language construct to include an exports list.

    A capsule declaration is a closed scope. The only definitions which are local to a capsule declaration, as opposed to the body of the capsule, are formal parameter definitions (see Section 3.5).

    Variables declared in the capsule that are not exported act as "own" data of the capsule. Like all data in the capsule, such variables come into existence each time the body of the capsule is elaborated; the statements in the capsule body may be used to initialize them.

    Translation time property lists are used to overload capsules and to create generic capsules.


RED RATIONALE

OWN DATA
Capsules can have "own" data; i.e., local variables which are not exported. The own data can be initialized by the capsule body when the capsule is exposed; the initialization can be based on the values of the capsule parameters.



EXAMPLES

  1. Common group of declarations.

    common group example


  2. Abstract data type example.

    abstract data type example


  3. Exposing a definition makes the definition locally known and, thus, able to be exported.

    exportable definition example



8.2  VISIBLE LIST

visible list diagram
C - identifier   81 - definable symbol

Visible lists control the availability of the definitions that are local to the body of a capsule, to those scopes where the capsule is invoked. Visible lists appear in two places:

  1. After the word EXPORTS in the header of a capsule declaration. This visible list exports selected definitions, which are local to the body of the capsule, to invocations of the capsule.
  2. After the word EXPOSE in a capsule invocation declaration. This visible list makes selected definitions, which were exported from the invoked capsule, known in the scope where the capsule invocation declaration appears.



RULES

Visible List in a Capsule Declaration

If ALL is specified, all definitions which are local to the body of the capsule, except goto label definitions, are exported.

If NONE is specified, no definitions are exported.

If a list is specified, all definitions which are local to the body of the capsule, and whose names appear in the list, are exported. Names of goto labels may not appear. There must be a definition, local to the body of the capsule, of each name that appears in the list. The name of any variable definition may be preceded by READONLY; in this case the variable is treated as a readonly data item ln those scopes where it is exposed.



Visible List in a Capsule Invocation Declaration

If ALL is specified, all definitions exported by the invoked capsule, are made known.

If NONE is specified, no definitions are made known.

If a list is specified, all definitions which were exported from the invoked capsule, and whose names appear in the list, are made known. There must be an exported definition for each name that appears in the list. The name of any variable definition may be preceded by READONLY; in this case the variable is treated as a readonly data item in the scope where the capsule invocation appears.



NOTES

    The capability of specifying ALL in a visible list makes it easy to create common data pools and libraries. Exporting no definitions is useful for main translation units. Exporting some definitions is useful for the creation of abstract data types.

    When a type is made visible, assignment and selection operations are not automatically made visible. However, attribute inquiry for that type is automatically made visible when the type is made visible.

    Capsule formal parameters and definitions which are available from the enclosing scope may not be exported since they are not local to the capsule bdoy.

    The visible list of the capsule invocation declaration provides a convenient method of access control. For example, suppose the capsule math_library has been defined as a library of mathematical functions. In one scope only some of the functions may need to be known. The capsule might be invoked as

    EXPOSE integrate, mean FROM math_library;

    In some other scope, a different set of functions might be needed. The invocation there might be

    EXPOSE sin, cos FROM math_library;






Declaration & Invocation     Visible List

7. Procedures, Functions, and Parameters left arrow
x
right arrow 9. Exceptions


Overview

Requirements
     Strawman
     Woodenman
     Tinman
     Ironman
     Steelman

RED Reference
RED Rationale

Types in RED
Time/Life Computer Languages
Memories

Site Index

Overview             Reference ToC             Rationale ToC             Site Index



Home   Favorites   Map

IME logo Copyright © 2009, Mary S. Van Deusen