Modularization: A Concrete Example (continued)


 

In the example which started on the previous section there were references to three fragments (by means of INCLUDE properties) that we have not yet shown. They provide a hierarchy of patterns for ordered entities. The basic ordered pattern is defined in the file ordered.gb, and it looks like this:

(* FILE ordered *)
ORIGIN 'betaenv';
BODY 'orderedImpl'

-- lib:attributes --

ordered:
  (# <<SLOT OrderedLib:attributes>>;
     cmpType:< ordered :> this(ordered);
     init:< (# do INNER exit this(ordered)[] #);
     lessEqual:< 
       (# other: ^cmpType; b: @boolean
       enter other[] 
       do INNER 
       exit b
       #);
     greaterEqual: 
       (# b: @boolean enter lessEqual->b exit not b #);
     max:
       (# other,maxi: ^cmpType
       enter other[]
       <<SLOT OrderedMax:dopart>>
       exit maxi[]
       #)
  exit this(ordered)[]
  #)

The slot OrderedMax appears where the implementation of the max method would be; this implementation has been taken out and placed in another file, in order to illustrate how the interface and the implementation of an entity can be separated from each other. The implementation fragment looks like this:

(* FILE orderedImpl *)
ORIGIN 'ordered'

-- OrderedMax:dopart --
do  
   (if other[]->greaterEqual then 
       other[]->maxi[]
    else 
       this(ordered)[]->maxi[]
   if)

The ordered pattern is specialized in two directions, to text and to number which is itself specialized to int and to float:

(* FILE orderedText *)
ORIGIN 'betaenv';
INCLUDE 'ordered'

-- lib:attributes --

text: ordered
  (# <<SLOT TextLib:attributes>>;
     cmpType::text;
     init::(# enter value #);
     lessEqual::(# do (other.value<=value) -> b #);
     value: @string
  #)

(* FILE orderedNumber *)
ORIGIN 'betaenv';
INCLUDE 'ordered'

-- lib:attributes --

number: ordered
  (# <<SLOT NumberLib:attributes>>;
     cmpType::number;
     lessEqual::(# do (other.asReal<=asReal)->b #);
     asReal:< (# r: @real do INNER exit r #);
     asInteger:< (# i: @integer do INNER exit i #);
  #);

int: number
  (# <<SLOT IntLib:attributes>>;
     init::(# enter value #);
     asReal::(# do value->r #);
     asInteger::(# do value->i #);
     value: @integer
  #);

float: number
  (# <<SLOT FloatLib:attributes>>;
     init::(# enter value #);
     asReal::(# do value->r #);
     asInteger::(# do value->i #);
     value: @real
  #)

All these patterns are made available in our program (in the file orderedUser.gb) by means of the INCLUDE properties, and the implementation in the file orderedImpl.gb was brought along because of the BODY property in ordered.gb. Note that no files include orderedImpl.gb, so the static analysis of the other files cannot depend on the contents of orderedImpl.gb; in other words, nobody depends on the implementation of the max method.

With these patterns the system is almost complete. The only missing part is that the main program calls an asString method on an ordered object, and we have no such method available in the declarations so far. The next section explains how we can provide such a method unintrusively.

 


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