Com S 541 Lecture -*- Outline -*- * Program Design and Modules (3.9) Q: What do the book's authors consider the distinction between small and large programs? The number of people working on the project; a small project has one person working on it ** design methodology (3.9.1) Q: How would you develop a small application? the book has several steps for designing small programs - informal specification (requirements) - examples, including boundary conditions - exploration of programming fragments and basic operations (bottom up exploration) - structure and coding (some structure from modules) - testing and reasoning to check correctness and efficiency - judging quality (correctness, efficiency, maintainability extensibility, simplicity) They consider testing to be essential, because it gives feedback ** example program requirements (3.9.2) This is the word frequency application: "Given a file name, the application opens a window and displays a list of pairs, where each pair consists of a word and an integer giving the number of times the word occurs in the file." ** Software components (3.9.3) Structure program into logical units, modules each module has an interface and an implementation *** modules and functors ------------------------------------------ MODULES Modules have: - an interface, represented by a record - an implementation ------------------------------------------ Q: what is the implementation? a set of language entities, usually procedures Q: can the interface record see the implementation? Q: how is the implementation hidden from clients? By lexical scoping ------------------------------------------ FUNCTORS A functor is a function that: - takes module interfaces as arguments - creates a new module - returns the new module's interface Syntax (Table 3.8): ::= ... | functor [ import { }+ ] [ export { }+ ] define { }+ [ in ] end ::= [ at ] | ( { } + ) ::= [ : ] ::= | ::= [ : ] ::= ... | functor [ $ ] [ import { }+ ] [ export { }+ ] define { }+ [ in ] end ------------------------------------------ Q: can we also make a functor into an expression? Yes, by using a nesting marker for the first , or by omitting it. When you do that, it creates and applies the functor, otherwise we have in effect a procedure declaration that can be called to create the functor, but which hasn't yet been called. Q: what's the unit of compilation in Mozart? A functor. Q: How are modules linked into a program? Dynamically. Find them, Execute their initialization code, then load on first use - First, a read-only variable is created. - Second, when the value of the read-only variable is needed then the functor is loaded and called in a new thread with the modules it imports as arguments. - Third, the functor call returns the new module. Q: What's the module environment? The set of currently installed modules ------------------------------------------ EXAMPLE declare functor Combinators export s: S k: K define fun {S F} fun {$ G} fun {$ X} {{F X} {G X}} end end end fun {K C} fun {$ X} C end end end ------------------------------------------ *** compilation To compile this module, use ozc -c Combinators.oz To make an application, use ozc -x MyApp.oz *** libraries Several libraries in Oz Base modules automatically available. Others have to be loaded