Last Updated: 2018-11-19 Mon 11:03

CSCI 2041 Lab09: Functors and Memoization

CODE DISTRIBUTION: lab09-code.zip

  • Download the code distribution every lab
  • See further setup instructions below

CHANGELOG:

Mon Nov 5 16:50:55 CST 2018
Typo fixed in comments for fib.ml: "repeatedly call normal fib" appeared in some spots where "memoized fib" should have appeared.

1 Rationale

Functors allow a module template to be created with pieces that are filled in through providing a parameter module. This is why functors are referred to at times as parameterized modules. They are also described as "functions of modules": a module goes in and a different module is returned.

This lab analyzes an interesting application of functors and will further cover possible actions inside functor bodies such as creating new modules and calling other functors.

Memoization is an programming optimization trick to speed up function calls that take a long time to compute. The trick is simply to "save" arguments and their associated return values. While this doesn't help with the first time a function is called with a given argument, subsequent calls can complete nearly instantaneously by looking up the return value in the "memoized" table. Such a table is often a map from arguments to return values.

Associated Reading / Preparation

Grading Policy

  • Check-off 30%: Demonstrate to a TA that a student understands answers to questions. This must be done in person in groups of one or two. Check-offs can happen during the lab period of during a TA office hour.
  • Submit 70%: Submit required files according to the lab instruction. This can be done at any time and from anywhere with a network connection. Submitting does not require attending lab. All students must submit files even if they were checked off in a group during lab.

See the full policy in the syllabus.

2 Codepack

The codepack for the lab contains the following files:

File State Description
QUESTIONS.txt Edit Answer questions in the file to complete the lab
memfunc1.ml Edit Problem 1 file to analyze
fib.ml Edit Problem 2 file to analyze and edit

3 Questions

Analyze the files in the provided codepack and answer the questions given in QUESTIONS.txt.

                           __________________

                            LAB 09 QUESTIONS
                           __________________


- Name: (FILL THIS in)
- NetID: (THE kauf0095 IN kauf0095@umn.edu)

Answer the questions below according to the lab specification. Write
your answers directly in this text file and submit it to complete the
lab.


PROBLEM 1: Memfunc1 Module
==========================

(A)
~~~

  Examine `memfunc1.ml' and answer the following questions.
  1. What is the name of the functor defined in the module?
  2. Like all functors, this one takes a parameter module. What bindings
     must the parameter module have according to its signature?
  3. The module that is created by the functor has several internal
     bindings but only one of them is publicly visible. What is it and
     how is this visibility control enforced?


(B)
~~~

  As in any other place a module is being defined, functors can
  establish nested modules.  The functor in `Memfunc1' creates two
  nested modules within its body.  Describe these two modules providing
  their names and what their purpose appears to be. Describe how
  bindings of the parameter module are used in these nested modules.


(C)
~~~

  Analyze the bindings for `arg_ret_map' and the function `call'.
  Describe how these are used in conjunction the a function carried by
  the parameter module.


PROBLEM 2: fib Program
======================

(A)
~~~

  Examine the first section of the provided `fib.ml' file. Describe how
  the functor in `memfunc1.ml' is used at the top of `fib.ml'. Include a
  description of how the parameter module to the functor is created.


(B)
~~~

  The main intent of the program in `fib.ml' is to compare the amount of
  time it takes to make repeated calls to the `fib' function versus a
  memoized version of it in `MemoFib.call'.  Analyze the main code for
  the program.
  1. Identify where calls to `fib' and `MemoFib.func' are made.
  2. Identify where the results of these calls are stored.
  3. Describe what function is used to gather timing information on how
     long the repeated function calls take.


(C)
~~~

  Use `ocamlc' to Compile and run the `fib.ml' program together with its
  dependency `memfunc1.ml'. Run the resulting program with and paste in
  the output in the space below.

  Note that the program requires command line argument, an integer. Use
  30 as the parameter.


(D)
~~~

  Describe why the time to run `fib' and `MemoFib.call' are very similar
  in the first timing loop but drastically different in the second
  timing loop. Relate your answer to the internal workings of the
  `Memofunc1.Memoize' functor.


Optional Enrichment Problems
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  1. We have seen that `MemoFib.call 30' will take some time on the
     first call but a subsequent call runs in almost no time.  Does this
     mean that the speed of `MemoFib.call 31' will be shorter as well?
     Why or why not?
  2. The functor established in `Memofunc1' works for functions of 1
     parameter.  How would one go about handling functions of two,
     three, or four arguments? If one is writing functions to be
     compatible with memoization, how could one write the functions of
     multiple arguments so as to avoid the need write more functors?

4 What to Understand

Ensure that you understand

  • Basic syntax associated with module functors.
  • Use of signatures to restrict the publicly visible bindings in a module.
  • How to call a functor with a parameter module to produce a new module.
  • How to create modules and call functors within a functor body.
  • The premise of memoizing a function to speed up later calls by "remembering" the return value associated with a parameter.

5 Getting Credit: Submit and Check-off

  • Check-off your answers with a TA in person during lab or office hours.
  • Submit your completed QUESTIONS.txt file to Canvas under the appropriate lab heading. Make sure to paste in any new code and answers you were to write at appropriate locations in the file.

Author: Chris Kauffman (kauffman@umn.edu)
Date: 2018-11-19 Mon 11:03