11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

3.6 Interfaces and Implementations • 113client use. But this was only a convention. Moreover, it was necessaryto use global variables to communicate information and state among theroutines that comprised a subsystem (such as solve or assume). Now,using modules, it is possible to design software systems that enforce theircontracts by a mechanism embedded in the Maple language.InterfacesThe contracts discussed previously in this section are represented formallyin Maple by an interface. An interface is a special kind of structured type.It has the form‘module‘( symseq );in which symseq is a sequence of symbols or of typed symbols (expressionsof the form symbol::type). For example, an interface for a ringcan be written as> ‘type/ring‘ := ’‘module‘( ‘+‘, ‘*‘, ‘-‘, zero, one )’:while an (additive) abelian group can take the form> ‘type/abgroup‘ := ’‘module‘( ‘+‘, ‘-‘, zero )’:These symbols are the ones to which clients have access as moduleexports.A module is said to satisfy, or to implement, an interface if it is ofthe type defined by the interface.> z5 := module()> description "the integers modulo 5";> export ‘+‘, ‘*‘, ‘-‘, zero, one;> ‘+‘ := (a,b) -> a+b mod 5;> ‘*‘ := (a,b) -> a*b mod 5;> ‘-‘ := s -> 5-s mod 5;> zero := 0;> one := 1;> end module:> type( z5, ’ring’ );trueA module can satisfy more than one interface.> type( z5, ’abgroup’ );true

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!