source: project/structures/structures.html @ 3542

Last change on this file since 3542 was 3542, checked in by felix winkelmann, 13 years ago

marked obsolete eggs

File size: 8.3 KB
Line 
1<html>
2<head><title>Eggs Unlimited - structures</title></head>
3<body>
4
5<center><img src="egg.jpg"></center>
6<center><a href="index.html">back</a></center>
7
8<h2>structures</h2>
9
10<h3>Description:</h3>
11A simple but flexible module system, based on Standard ML modules and Taylor
12Campbells "lexmod" module system.
13
14This extension is obsolete. Use <a href="http://chicken.wiki.br/lexmod">lexmod</a>
15instead.
16
17<h3>Author:</h3>
18<a href="mailto:felix@call-with-current-continuation.org">felix</a>
19
20<h3>Version:</h3>
21<ul>
22<li>1.5
23Added structures.html to egg
24<li>1.4
25Signature table wasn't available at compile time [Thanks to Kon Lovett]
26<li>1.3
27Adapted to SRFI-69-compatible hash-tables
28<li>1.2
29Adapted to new setup scheme
30<li>1.1
31Added support for signature-names in <code>define-signature</code> forms
32<li>1.0
33</ul>
34
35<h3>Usage:</h3>
36<pre>
37(require-extension structures)
38</pre>
39
40<h3>Download:</h3>
41<a href="structures.egg">structures.egg</a>
42
43<h3>Documentation:</h3>
44
45<ul>
46
47<p><li>Terminology
48
49A <i>structure</i> is an object containing named values. A <i>signature</i> is a
50set of names and (optionally) constraint-predicates which specify an interface
51that a structure must satisfy. A <i>functor</i> is a structure parameterized
52over other structures. A <i>module</i> is a structure or functor.
53
54<p>Note that this facility does not work at together with the
55<a href="syntax-case.html">syntax-case</a> extension.
56
57<p><li>Signatures
58
59<dl>
60
61<dt><pre><b>[syntax] (define-signature SIGNATURE SIG ...)</b></pre>
62<dd>Declares an interface. <code>SIG</code> should be a symbol or a signature
63name, or a list of the form <code>(NAME PREDICATE)</code>
64</dl>
65
66<p><li>Structures
67
68<dl>
69
70<dt><pre><b>[syntax] (structure (EXPORT ...) DEFINITION ...)</b></pre>
71<dd>Creates and returns a structure containing <code>DEFINITION ...</code>.
72<code>DEFINITION</code> should be one of the following forms:
73
74<pre>
75    (define (NAME ...) ...)
76    (define NAME VALUE)
77    (define-values (NAME ...) VALUES)
78</pre>
79
80The definitions are executed sequentially, and the value of each definition is
81only available in subsequent definitions (but may be referred to by earlier
82definitions, with the same restrictions as local defines). Definitions may be
83interspersed by normal expressions.
84
85<br>The defined names will only be available in this structure unless exported.
86
87<p><code>EXPORT ...</code> should be a list of export-specifications conforming
88to the following grammar:
89
90<pre>
91    EXPORT = (export NAME ...)
92           | SIGNATURE
93</pre>
94
95If <code>(EXPORT ...)</code> is empty, the structure exports all defined
96bindings. If <code>(EXPORT ...)</code> is not empty, then the structure does
97only export bindings, for which export-specifications are given. If a signature
98defines a constraint-predicate and an exported value does not satisfy the
99predicate an error is signalled. An error is also signalled, when a structure
100does not define an exported binding.
101
102<dt><pre><b>[procedure] (structure? X [EXPORT ...])</b></pre>
103<dd>Returns <code>#t</code>, if <code>X</code> is a structure or functor, or
104<code>#f</code> otherwise. If <code>EXPORT</code> is supplied, then must be a
105form following the grammer for <code>EXPORT</code> given above. One or more
106instances of an <code>EXPORT</code> form may be supplied.
107
108<dt><pre><b>[procedure] (compound-structure STRUCTURE ...)</b></pre>
109<dd>Returns a new structure that shares all bindings given in <code>STRUCTURE ...</code>.
110If multiple structures export a identically named value, an error is signalled.
111
112</dl>
113
114<p><li>Functors
115
116<dl>
117
118<dt><pre><b>[syntax] (functor ((VAR STRUCTURE IMPORT ...) ...) (EXPORT ...) DEFINITION ...)</b></pre>
119<dd>Similar to <code>structure</code>, but the definitions are evaluated in a
120lexical environment, where the variables <code>VAR ...</code> are bound to
121structures given in <code>STRUCTURE ...</code>. Each argument structure may be
122constrained by an import-specification, of the following grammar:
123
124<pre>
125    IMPORT = (import NAME ...)
126           | SIGNATURE
127</pre>
128
129If no constraint is given for an argument structure, then all exported bindings
130of that structure are imported. <br>All imported bindings of the argument
131structures are lexically visible inside the <code>DEFINITIONS ...</code>.
132
133<dt><pre><b>[syntax] (define-functor (NAME STRUCTURE-ARGUMENT ...) (EXPORT ...) DEFINITION ...)</b></pre>
134<dd>A convenient syntax for procedures that create functors. <code>STRUCTURE-ARGUMENT</code> should be a variable
135or a list of the form <code>(VAR IMPORT ...)</code>.
136<br>For example
137<pre>
138(define-functor (make-f x (y my-signature)) () ...)
139</pre>
140Is equivalent to
141<pre>
142(define (make-f x y)
143  (functor ((x x) (y y my-signature)) ()
144    ...) )
145</pre>
146
147</dl>
148
149<p><li>Using structures
150
151<dl>
152
153<dt><pre><b>[syntax] (in-structure STRUCTURE (IMPORT ...) BODY ...)</b></pre>
154<dd>Evaluates <code>BODY ...</code> with an environment that binds the values
155from <code>STRUCTURES</code> and given in <code>IMPORT ...</code> to lexically
156visible variables. <code>IMPORT</code> should be of the form:
157
158<pre>
159    IMPORT = (import NAME ...)
160           | SIGNATURE
161</pre>
162
163<dt><pre><b>[procedure] (open STRUCTURE ...)</b></pre>
164<dd>Imports all exported bindings from the argument structures as toplevel variables.
165
166<dt><pre><b>[procedure] (structure-ref STRUCTURE NAME)</b></pre>
167<dd>Locates the exported binding of the symbol <code>NAME</code> in
168<code>STRUCTURE</code> and returns its value. If <code>STRUCTURE</code> does not
169export <code>name</code>, an error is signalled.
170
171</dl>
172
173</ul>
174
175
176<p><h3>Examples:</h3>
177
178<pre>
179(define-signature arith-signature zero sum diff prod recip quo)
180
181(define complex
182  (structure (arith-signature)
183    (define zero '(0 . 0))
184    (define (sum x y) (cons (+ (car x) (car y)) (+ (cdr x) (cdr y))))
185    (define (diff x y) (cons (- (car x) (car y)) (- (cdr x) (cdr y))))
186    (define (prod x y)
187      (cons (- (* (car x) (car y)) (* (cdr x) (cdr y)))
188            (+ (* (car x) (cdr y)) (* (car y) (cdr x))) ) )
189    (define (recip x)
190      (let ([t (+ (* (car x) (car x)) (* (cdr x) (cdr x)))])
191        (cons (/ (car x) t) (/ (- (cdr x) t))) ) )
192    (define (quo x) (prod x (recip x))) ) )
193
194(define simple
195  (structure (arith-signature)
196    (define zero 0)
197    (define sum +)
198    (define diff -)
199    (define prod *)
200    (define recip /)
201    (define quo /) ) )
202
203(in-structure simple ((import sum))
204  (print (sum 33 44)))                 ; 77
205
206(in-structure complex ((import sum))
207  (print (sum '(10 . 1) '(33 . 2))) )  ; (43 . 3)
208
209(define-functor (squarer (s arith-signature)) ((export square))
210  (define (square n) (prod n n) ) )
211
212(define simple-squarer (squarer simple))
213(define complex-squarer (squarer complex))
214
215(print ((structure-ref simple-squarer 'square) 9))         ; 81
216(print ((structure-ref complex-squarer 'square) '(3 . 2))) ; (5 . 12)
217
218(open simple-squarer)
219(print (square 10))   ; 100
220</pre>
221
222<h3>License:</h3>
223<pre>
224Copyright (c) 2003, Felix L. Winkelmann
225All rights reserved.
226
227Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
228conditions are met:
229
230  Redistributions of source code must retain the above copyright notice, this list of conditions and the following
231    disclaimer.
232  Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
233    disclaimer in the documentation and/or other materials provided with the distribution.
234  Neither the name of the author nor the names of its contributors may be used to endorse or promote
235    products derived from this software without specific prior written permission.
236
237THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
238OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
239AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
240CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
242SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
244OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
245POSSIBILITY OF SUCH DAMAGE.
246</pre>
247
248
249<hr><a href="index.html">back</a>
250
251</body>
252</html>
Note: See TracBrowser for help on using the repository browser.