next up previous contents
Next: Reference Up: Object-Oriented Modules Previous: Model checking a round-robin   Contents


From object-oriented modules to system modules

The best way to understand classes and class inheritance in Full Maude is by making explicit the full structure of an object-oriented module, which is left somewhat implicit by the syntactic conventions adopted for them. Indeed, although object-oriented modules provide convenient syntax for programming object-oriented systems, their semantics can be reduced to that of system modules. We can regard the special syntax reserved for object-oriented modules as syntactic sugar. In fact, each object-oriented module can be translated into a corresponding system module whose semantics is by definition that of the original object-oriented module.

In the translation process, the most basic structure shared by all object-oriented modules is made explicit by the CONFIGURATION functional module. The translation of a given object-oriented module extends this structure with the classes, messages and rules introduced by the module. For example, the following system module is the translation of the ACCOUNT module introduced earlier. Note that a subsort Account of Cid is introduced. The purpose of this subsort is to range over the class identifiers of the subclasses of Account. For the moment, no such subclasses have been introduced; therefore, at present the only constant of sort Account is the class identifier Account.

  mod ACCOUNT is
    protecting INT .
    protecting QID .
    including CONFIGURATION+ .
    including CONFIGURATION .
    sorts Account .
    subsort Qid < Oid .
    subsort Account < Cid .
    op Account : -> Account .
    op credit : Oid Int -> Msg [msg] .
    op debit : Oid Int -> Msg [msg] .
    op from_to_transfer_ : Oid Oid Int -> Msg [msg] .
    op bal :_ : Int -> Attribute .
    var A : Oid .
    var B : Oid .
    var M : Int .
    var N : Int .
    var N' : Int .
    var V@Account : Account .
    var ATTS@0 : AttributeSet .
    var V@Account1 : Account .
    var ATTS@2 : AttributeSet .
    rl [credit] :
      credit(A, M)
      < A : V@Account | bal : N, ATTS@0 >
      => < A : V@Account | bal : (N + M), ATTS@0 > .
    crl [debit] :
      debit(A, M)
      < A : V@Account | bal : N, ATTS@0 >
      => < A : V@Account | bal : (N - M), ATTS@0 >
      if N >= M = true .
    crl [transfer] :
      (from A to B transfer M)
      < A : V@Account | bal : N, ATTS@0 >
      < B : V@Account1 | bal : N', ATTS@2 >
      => < A : V@Account | bal : (N - M), ATTS@0 >
         < B : V@Account1 | bal : (N' + M), ATTS@2 >
      if N >= M = true .
  endm

We can describe the desired transformation from an object-oriented module to a system module as follows:15.5

The rewrite rules given in the original ACCOUNT module are interpreted here--according to the conventions already explained--in a form that can be inherited by subclasses of Account that could be defined later. Thus, SavingAccount inherits the rewrite rules for crediting and debiting accounts, and for transferring funds between accounts that had been defined for Account.

Let us illustrate the treatment of class inheritance with the system module resulting from the transformation of the module SAVING-ACCOUNT introduced previously.

  mod SAVING-ACCOUNT is
    including CONFIGURATION+ .
    including CONFIGURATION .
    including ACCOUNT .
    sorts SavingAccount .
    subsort SavingAccount < Cid .
    subsort SavingAccount < Account .
    op SavingAccount : -> SavingAccount .
    op rate :_ : Int -> Attribute .
  endm

Note that by translating a rule like credit above

  rl [credit] :
    credit(A, M)
    < A : Account | bal : N >
    => < A : Account | bal : (N + M) > .

into its corresponding transformed form

  rl [credit] :
    credit(A, M)
    < A : V0@:Account | bal : N, V1@:AttributeSet >
    => < A : V0@:Account | bal : (N + M), V1@:AttributeSet > .

it is guaranteed that the rule will be applicable to objects of class Account as well as of any of its subclasses.

Note also that a rule like change-age (discussed in Section 15.1.4)

  rl [change-age] :
    < O : Person | >
    to O : new age A
    => < O : Person | age : A > .

is translated into a rule like

  rl [change-age] :
    < O : V0@:Person | name : V1:String, age : V2:Nat,
                       account : V3:Oid, V4@:AttributeSet >
    to O : new age A
    => < O : V0@:Person | age : A, name : V1:String,
                          account : V3:Oid, V4@:AttributeSet > .

With this translation we allow the rule to be applied to objects in subclasses of Person. Furthermore, we guarantee that it is only applied to well-formed objects, that is, to objects with all the required attributes.

See [27] for a detailed explanation of the transformation of object-oriented modules into system modules and how their semantics is by definition that of the original object-oriented module.


next up previous contents
Next: Reference Up: Object-Oriented Modules Previous: Model checking a round-robin   Contents
The Maude Team