COP 4020 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) (skip) ------------------------------------------ METHODOLOGY FOR DEVELOPING SMALL APPLICATIONS - 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) (skip) 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 client | v interface(op1: Op1, ... opn: OpN) | | | module | | 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 Q: What is this like in C, C++, and Java? ------------------------------------------ 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: What is this like in C, C++, and Java? 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 expression. 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 %% file Combinators.oz % Example of how to write a module in Oz functor $ import % This section isn't needed for this example System(showInfo show) export s: S k: K define % The S combinator fun {S F} fun {$ G} fun {$ X} {{F X} {G X}} end end end % The K (constant) combinator 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 *** linking ------------------------------------------ % LINKING MODULES local [Combinators] = {Module.link ['Combinators.ozf']} S = Combinators.s K = Combinators.k in % In this part we use the abbriviations defined above {StartTesting 'Combinators'} {Test {{Combinators.k 7} 6} '==' 7} local I = {{S K} K} in {Test {I 3} '==' 3} end end ------------------------------------------ Q: What is Module.link returning? a list of records Take a look at Testing.oz and Test.oz, Assert.oz in the lib directory *** libraries Several libraries in Oz Base modules automatically available. Others have to be loaded