1996-09-27 06:29:02 -04:00
|
|
|
%
|
1998-04-10 06:59:06 -04:00
|
|
|
% STk Reference manual (Appendix: Modules)
|
1996-09-27 06:29:02 -04:00
|
|
|
%
|
|
|
|
% Author: Erick Gallesio [eg@unice.fr]
|
1998-04-10 06:59:06 -04:00
|
|
|
% Creation date: 6-Apr-1998 11:19
|
|
|
|
% Last file update: 8-Apr-1998 10:10
|
1996-09-27 06:29:02 -04:00
|
|
|
%
|
|
|
|
|
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
This appendix shows some usages of the {\stk} modules. Most of the
|
|
|
|
examples which are exhibited here are derived from the Tung and Dybvig paper
|
|
|
|
\cite{Tung-Dybvig-96}.
|
1996-09-27 06:29:02 -04:00
|
|
|
|
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
\subsection*{Interactive Redefinition}
|
|
|
|
|
|
|
|
Consider first the definitions,
|
|
|
|
\begin{scheme}
|
|
|
|
(define-module A
|
|
|
|
(export square)
|
|
|
|
(define square
|
|
|
|
(lambda (x) (+ x x))))
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
(define-module B
|
|
|
|
(import A)
|
|
|
|
(define distance
|
|
|
|
(lambda (x y)
|
|
|
|
(sqrt (+ (square x) (square y))))))
|
|
|
|
\end{scheme}
|
|
|
|
Obviously, the \texttt{square} function exported from \texttt{A} is
|
|
|
|
incorrect, as we can see in its usage below:
|
|
|
|
\begin{scheme}
|
|
|
|
(with-module B (round (distance 3 4))) \lev 4.0
|
|
|
|
\end{scheme}
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
The function can be redefined (\textit{corrected}) by the following expression:
|
|
|
|
\begin{scheme}
|
|
|
|
(with-module A
|
|
|
|
(set! square
|
|
|
|
(lambda (x) (* x x))))
|
|
|
|
\end{scheme}
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
And now,
|
|
|
|
\begin{scheme}
|
|
|
|
(with-module B (round (distance 3 4))) \lev 5
|
|
|
|
\end{scheme}
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
which is correct.
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
\subsection*{Lexical principle}
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
This example reuses the modules \texttt{A} and \texttt{B} of previous
|
|
|
|
section and adds a \texttt{Compare} module that exports the
|
|
|
|
\texttt{less-than-4?} predicates, which states if the distance from a
|
|
|
|
point to the origin is less than 4.
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
\begin{scheme}
|
|
|
|
(define-module A
|
|
|
|
(export square)
|
|
|
|
(define square (lambda (x) (* x x))))
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
(define-module B
|
|
|
|
(import A)
|
|
|
|
(export distance)
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
(define distance
|
|
|
|
(lambda (x y) (sqrt (+ (square x) (square y))))))
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
(define-module Compare
|
|
|
|
(import B)
|
|
|
|
(define less-than-4? (lambda (x y) (< (distance x y) 4)))
|
|
|
|
(define square (lambda (x) (+ x x))))
|
|
|
|
\end{scheme}
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
Consider now the call,
|
|
|
|
\begin{scheme}
|
|
|
|
(with-module compare (less-than-4? 3 4)) \lev \schfalse
|
|
|
|
\end{scheme}
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
The call to \texttt{distance} done from \texttt{less-than-4?}
|
|
|
|
indirectly calls the \texttt{square} procedure of module \texttt{A} rather than
|
|
|
|
the one defined locally in module \texttt{Compare}.
|
1996-09-27 06:29:02 -04:00
|
|
|
|
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
\subsection*{Mutually Referential Modules}
|
|
|
|
This example uses two mutually referential modules taht import and
|
|
|
|
export to each other to implement mutually recursive even? and odd?
|
|
|
|
procedures
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
\begin{scheme}
|
|
|
|
(define-module Odd) ;; Forward declaration
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
(define-module Even
|
|
|
|
(import Odd)
|
|
|
|
(export even?)
|
|
|
|
(define even? (lambda (x) (if (zero? x) \schtrue (odd? (- x 1))))))
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
(define-module Odd
|
|
|
|
(import Even)
|
|
|
|
(export odd?)
|
|
|
|
(define odd? (lambda (x) (if (zero? x) \schfalse (even? (- x 1))))))
|
|
|
|
\end{scheme}
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
Hereafter are some usages of theses procedures:
|
|
|
|
\begin{scheme}
|
|
|
|
(with-module Odd (odd? 3)) \lev \schtrue
|
|
|
|
(with-module Odd (odd? 10)) \lev \schfalse
|
1996-09-27 06:29:02 -04:00
|
|
|
|
1998-04-10 06:59:06 -04:00
|
|
|
(with-module Even (even? 3)) \lev \schfalse
|
|
|
|
(with-module Even (even? 10)) \lev \schtrue
|
|
|
|
\end{scheme}
|
|
|
|
|
|
|
|
|
|
|
|
%%% Local Variables:
|
|
|
|
%%% mode: latex
|
|
|
|
%%% TeX-master:"manual"
|
|
|
|
%%% End:
|
1996-09-27 06:29:02 -04:00
|
|
|
|