doc work (start on module guide)
svn: r6730
This commit is contained in:
parent
dc8d09997c
commit
f7bf029234
|
@ -326,7 +326,7 @@
|
||||||
fns)))
|
fns)))
|
||||||
|
|
||||||
(define/override (part-whole-page? d)
|
(define/override (part-whole-page? d)
|
||||||
(= 2 (collecting-sub)))
|
((collecting-sub) . <= . 2))
|
||||||
|
|
||||||
(define/private (toc-part? d)
|
(define/private (toc-part? d)
|
||||||
(and (styled-part? d)
|
(and (styled-part? d)
|
||||||
|
|
|
@ -50,6 +50,7 @@
|
||||||
(define-color "schemeresult" "ResultColor")
|
(define-color "schemeresult" "ResultColor")
|
||||||
(define-color "schemestdout" "OutputColor")
|
(define-color "schemestdout" "OutputColor")
|
||||||
(define-color "schememeta" "IdentifierColor")
|
(define-color "schememeta" "IdentifierColor")
|
||||||
|
(define-color "schememod" "black")
|
||||||
(define-color "schemevariablecol" "IdentifierColor")
|
(define-color "schemevariablecol" "IdentifierColor")
|
||||||
(printf "\\newcommand{\\schemevariable}[1]{{\\schemevariablecol{\\textsl{#1}}}}\n")
|
(printf "\\newcommand{\\schemevariable}[1]{{\\schemevariablecol{\\textsl{#1}}}}\n")
|
||||||
(define-color "schemeerrorcol" "red")
|
(define-color "schemeerrorcol" "red")
|
||||||
|
@ -232,6 +233,7 @@
|
||||||
[(ldquo) "``"]
|
[(ldquo) "``"]
|
||||||
[(rdquo) "''"]
|
[(rdquo) "''"]
|
||||||
[(rsquo) "'"]
|
[(rsquo) "'"]
|
||||||
|
[(prime) "$'$"]
|
||||||
[(rarr) "$\\rightarrow$"]))]
|
[(rarr) "$\\rightarrow$"]))]
|
||||||
[else (display-protected (format "~s" i))])
|
[else (display-protected (format "~s" i))])
|
||||||
null)
|
null)
|
||||||
|
|
|
@ -94,7 +94,7 @@
|
||||||
|
|
||||||
(provide onscreen menuitem defterm
|
(provide onscreen menuitem defterm
|
||||||
schemefont schemevalfont schemeresultfont schemeidfont
|
schemefont schemevalfont schemeresultfont schemeidfont
|
||||||
schemeparenfont schemekeywordfont schememetafont
|
schemeparenfont schemekeywordfont schememetafont schememodfont
|
||||||
file exec
|
file exec
|
||||||
link procedure
|
link procedure
|
||||||
idefterm)
|
idefterm)
|
||||||
|
@ -120,6 +120,8 @@
|
||||||
(make-element "schemeparen" (decode-content str)))
|
(make-element "schemeparen" (decode-content str)))
|
||||||
(define (schememetafont . str)
|
(define (schememetafont . str)
|
||||||
(make-element "schememeta" (decode-content str)))
|
(make-element "schememeta" (decode-content str)))
|
||||||
|
(define (schememodfont . str)
|
||||||
|
(make-element "schememod" (decode-content str)))
|
||||||
(define (schemekeywordfont . str)
|
(define (schemekeywordfont . str)
|
||||||
(make-element "schemekeyword" (decode-content str)))
|
(make-element "schemekeyword" (decode-content str)))
|
||||||
(define (file . str)
|
(define (file . str)
|
||||||
|
@ -808,12 +810,15 @@
|
||||||
(let loop ([i i])
|
(let loop ([i i])
|
||||||
(cond
|
(cond
|
||||||
[(string? i)
|
[(string? i)
|
||||||
(let ([m (regexp-match #rx"^(.*)([()])(.*)$" i)])
|
(cond
|
||||||
(if m
|
[(regexp-match #rx"^(.*)([()])(.*)$" i)
|
||||||
(append (loop (cadr m))
|
=> (lambda (m)
|
||||||
(list (caddr m))
|
(append (loop (cadr m))
|
||||||
(loop (cadddr m)))
|
(list (caddr m))
|
||||||
(list (make-element 'italic (list i)))))]
|
(loop (cadddr m))))]
|
||||||
|
[else
|
||||||
|
(list (make-element 'italic (list i)))])]
|
||||||
|
[(eq? i 'rsquo) (list 'prime)]
|
||||||
[else (list i)])))
|
[else (list i)])))
|
||||||
c)))))
|
c)))))
|
||||||
|
|
||||||
|
|
|
@ -49,6 +49,8 @@
|
||||||
|
|
||||||
(define-struct (sized-element element) (length))
|
(define-struct (sized-element element) (length))
|
||||||
|
|
||||||
|
(define-struct spaces (pre cnt post))
|
||||||
|
|
||||||
(define (typeset c multi-line? prefix1 prefix suffix color?)
|
(define (typeset c multi-line? prefix1 prefix suffix color?)
|
||||||
(let* ([c (syntax-ize c 0)]
|
(let* ([c (syntax-ize c 0)]
|
||||||
[content null]
|
[content null]
|
||||||
|
@ -78,26 +80,35 @@
|
||||||
[(and (element? v)
|
[(and (element? v)
|
||||||
(= 1 (length (element-content v))))
|
(= 1 (length (element-content v))))
|
||||||
(sz-loop (car (element-content v)))]
|
(sz-loop (car (element-content v)))]
|
||||||
|
[(spaces? v)
|
||||||
|
(+ (sz-loop (spaces-pre v))
|
||||||
|
(spaces-cnt v)
|
||||||
|
(sz-loop (spaces-post v)))]
|
||||||
[else 1])))]
|
[else 1])))]
|
||||||
[(v cls len)
|
[(v cls len)
|
||||||
(unless (equal? v "")
|
(unless (equal? v "")
|
||||||
(if (equal? v "\n")
|
(cond
|
||||||
(if multi-line?
|
[(spaces? v)
|
||||||
(begin
|
(out (spaces-pre v) cls 0)
|
||||||
(finish-line!)
|
(out (make-element 'hspace (list (make-string (spaces-cnt v) #\space))) #f 0)
|
||||||
(out prefix cls))
|
(out (spaces-post v) cls len)]
|
||||||
(out " " cls))
|
[(equal? v "\n")
|
||||||
(begin
|
(if multi-line?
|
||||||
(set! content (cons ((if highlight?
|
(begin
|
||||||
(lambda (c)
|
(finish-line!)
|
||||||
(make-element "highlighted" (list c)))
|
(out prefix cls))
|
||||||
values)
|
(out " " cls))]
|
||||||
(if color?
|
[else
|
||||||
(make-element cls (list v))
|
(set! content (cons ((if highlight?
|
||||||
(make-element #f (list v))))
|
(lambda (c)
|
||||||
content))
|
(make-element "highlighted" (list c)))
|
||||||
(set! dest-col (+ dest-col len)))))]))
|
values)
|
||||||
(define advance
|
(if color?
|
||||||
|
(make-element cls (list v))
|
||||||
|
(make-element #f (list v))))
|
||||||
|
content))
|
||||||
|
(set! dest-col (+ dest-col len))]))]))
|
||||||
|
(define advance
|
||||||
(case-lambda
|
(case-lambda
|
||||||
[(c init-line! delta)
|
[(c init-line! delta)
|
||||||
(let ([c (+ delta (syntax-column c))]
|
(let ([c (+ delta (syntax-column c))]
|
||||||
|
@ -168,12 +179,9 @@
|
||||||
(define (literalize-spaces i)
|
(define (literalize-spaces i)
|
||||||
(let ([m (regexp-match-positions #rx" +" i)])
|
(let ([m (regexp-match-positions #rx" +" i)])
|
||||||
(if m
|
(if m
|
||||||
(make-element
|
(make-spaces (literalize-spaces (substring i 0 (caar m)))
|
||||||
#f
|
(- (cdar m) (caar m))
|
||||||
(list (literalize-spaces (substring i 0 (caar m)))
|
(literalize-spaces (substring i (cdar m))))
|
||||||
(make-element 'hspace
|
|
||||||
(list (substring i (caar m) (cdar m))))
|
|
||||||
(literalize-spaces (substring i (cdar m)))))
|
|
||||||
i)))
|
i)))
|
||||||
(define (loop init-line! quote-depth)
|
(define (loop init-line! quote-depth)
|
||||||
(lambda (c)
|
(lambda (c)
|
||||||
|
|
|
@ -41,7 +41,7 @@
|
||||||
width: 10em;
|
width: 10em;
|
||||||
margin-right: 2em;
|
margin-right: 2em;
|
||||||
text-align: left;
|
text-align: left;
|
||||||
background-color: #ddffdd;
|
background-color: #F5F5DC;
|
||||||
}
|
}
|
||||||
|
|
||||||
.tocviewtitle {
|
.tocviewtitle {
|
||||||
|
@ -237,6 +237,11 @@
|
||||||
font-family: Courier; font-size: 80%;
|
font-family: Courier; font-size: 80%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.schememod {
|
||||||
|
color: black;
|
||||||
|
font-family: Courier; font-size: 80%;
|
||||||
|
}
|
||||||
|
|
||||||
.schemeopt {
|
.schemeopt {
|
||||||
color: black;
|
color: black;
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,23 +6,28 @@
|
||||||
|
|
||||||
@title[#:tag "guide:define-struct"]{Programmer-Defined Datatypes}
|
@title[#:tag "guide:define-struct"]{Programmer-Defined Datatypes}
|
||||||
|
|
||||||
This section introduces the @scheme[define-struct] form for creating
|
@refalso["mz:structures"]{structure types}
|
||||||
your own datatypes. The class-based object system offers an alternate
|
|
||||||
mechanism for creating new datatypes; the resulting objects are
|
New datatypes are normally created with the @scheme[define-struct]
|
||||||
nevertheless implemented as structures, and we defer discussion of
|
form, which is the topic of this chapter. The class-based object
|
||||||
objects to @secref["classes"].
|
system, which we defer to @secref["classes"], offers an alternate
|
||||||
|
mechanism for creating new datatypes, but even classes and objects are
|
||||||
|
implemented in terms of structure types.
|
||||||
|
|
||||||
@; ------------------------------------------------------------
|
@; ------------------------------------------------------------
|
||||||
@section{Simple Structure Types: @scheme[define-struct]}
|
@section{Simple Structure Types: @scheme[define-struct]}
|
||||||
|
|
||||||
|
@refalso["mz:define-struct"]{@scheme[define-struct]}
|
||||||
|
|
||||||
To a first approximation, the syntax of @scheme[define-struct] is
|
To a first approximation, the syntax of @scheme[define-struct] is
|
||||||
|
|
||||||
@specform[
|
@specform[
|
||||||
(define-struct struct-id (field-id ...))
|
(define-struct struct-id (field-id ...))
|
||||||
]{}
|
]{}
|
||||||
|
|
||||||
Such a definition binds @scheme[_struct-id], but only to static
|
A @scheme[define-struct] declaration binds @scheme[_struct-id], but
|
||||||
information about the structure type that cannot be used directly:
|
only to static information about the structure type that cannot be
|
||||||
|
used directly:
|
||||||
|
|
||||||
@def+int[
|
@def+int[
|
||||||
(define-struct posn (x y))
|
(define-struct posn (x y))
|
||||||
|
@ -30,11 +35,11 @@ posn
|
||||||
]
|
]
|
||||||
|
|
||||||
We explain one use of the @scheme[_struct-id] binding in the next
|
We explain one use of the @scheme[_struct-id] binding in the next
|
||||||
section.
|
section, @secref["guide:struct-subtypes"].
|
||||||
|
|
||||||
In addition to defining @scheme[_struct-id], however,
|
Meanwhile, in addition to defining @scheme[_struct-id],
|
||||||
@scheme[define-struct] also defines a number of functions whose names
|
@scheme[define-struct] also defines a number of identifiers that are
|
||||||
are built from @scheme[_struct-id] and the @scheme[_field-id]s:
|
built from @scheme[_struct-id] and the @scheme[_field-id]s:
|
||||||
|
|
||||||
@itemize{
|
@itemize{
|
||||||
|
|
||||||
|
@ -71,8 +76,9 @@ are built from @scheme[_struct-id] and the @scheme[_field-id]s:
|
||||||
|
|
||||||
@item{@schemeidfont{struct:}@scheme[_struct-id] : a
|
@item{@schemeidfont{struct:}@scheme[_struct-id] : a
|
||||||
@deftech{structure type descriptor}, which is a value that
|
@deftech{structure type descriptor}, which is a value that
|
||||||
represents the structure type (as opposed to a single instance)
|
represents the structure type as a first-class value (with
|
||||||
for certain reflective operations.}
|
@scheme[#:super], as discussed later in
|
||||||
|
@secref["guide:struct-options"]).}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,9 +92,9 @@ as requiring them to be numbers, is normally the job of a contract, as
|
||||||
discussed later in @secref["guide:contracts"].
|
discussed later in @secref["guide:contracts"].
|
||||||
|
|
||||||
@; ------------------------------------------------------------
|
@; ------------------------------------------------------------
|
||||||
@section{Structure Subtypes}
|
@section[#:tag "guide:struct-subtypes"]{Structure Subtypes}
|
||||||
|
|
||||||
An extended form of @scheme[defin-struct] can be used to define a
|
An extended form of @scheme[define-struct] can be used to define a
|
||||||
@defterm{structure subtype}, which is a structure type that extends an
|
@defterm{structure subtype}, which is a structure type that extends an
|
||||||
existing structure type:
|
existing structure type:
|
||||||
|
|
||||||
|
@ -97,8 +103,8 @@ existing structure type:
|
||||||
]
|
]
|
||||||
|
|
||||||
The @scheme[_super-id] must be a structure type name bound by
|
The @scheme[_super-id] must be a structure type name bound by
|
||||||
@scheme[define-struct] (i.e., the name bound by @scheme[define-struct]
|
@scheme[define-struct] (i.e., the name that cannot be used directly as
|
||||||
that cannot be used directly as an expression).
|
an expression).
|
||||||
|
|
||||||
@as-examples[@schemeblock+eval[
|
@as-examples[@schemeblock+eval[
|
||||||
(define-struct posn (x y))
|
(define-struct posn (x y))
|
||||||
|
@ -146,7 +152,7 @@ field-name sequence:
|
||||||
|
|
||||||
An instance of a transparent structure type prints like a vector, and
|
An instance of a transparent structure type prints like a vector, and
|
||||||
it shows the content of the structure's fields. A transparent
|
it shows the content of the structure's fields. A transparent
|
||||||
structure type also allows reflective operations, like
|
structure type also allows reflective operations, such as
|
||||||
@scheme[struct?] and @scheme[struct-info], to be used on its instances
|
@scheme[struct?] and @scheme[struct-info], to be used on its instances
|
||||||
(see @secref["reflection"]). Different values for @scheme[#:inspector]
|
(see @secref["reflection"]). Different values for @scheme[#:inspector]
|
||||||
support more controlled access to reflective operations.
|
support more controlled access to reflective operations.
|
||||||
|
@ -158,7 +164,7 @@ library cannot manipulate the data in the structure except as allowed
|
||||||
by the library.
|
by the library.
|
||||||
|
|
||||||
@; ------------------------------------------------------------
|
@; ------------------------------------------------------------
|
||||||
@section{More Structure Type Options}
|
@section[#:tag "guide:struct-options"]{More Structure Type Options}
|
||||||
|
|
||||||
The full syntax of @scheme[define-struct] supports many options, both
|
The full syntax of @scheme[define-struct] supports many options, both
|
||||||
at the structure-type level and at the level of individual fields:
|
at the structure-type level and at the level of individual fields:
|
||||||
|
@ -199,8 +205,8 @@ A @scheme[_struct-option] always starts with a keyword:
|
||||||
|
|
||||||
Specifies a value to be used for all automatic fields in the
|
Specifies a value to be used for all automatic fields in the
|
||||||
structure type, where an automatic field is indicated by the
|
structure type, where an automatic field is indicated by the
|
||||||
@scheme[#:auto] @scheme[_field-option]. The structure type's
|
@scheme[#:auto] field option. The constructor procedure does not
|
||||||
constructor omits arguments for automatic fields.
|
accept arguments for automatic fields.
|
||||||
|
|
||||||
@defexamples[
|
@defexamples[
|
||||||
(define-struct posn (x y [z #:auto])
|
(define-struct posn (x y [z #:auto])
|
||||||
|
@ -302,7 +308,7 @@ times.
|
||||||
|
|
||||||
@defexamples[
|
@defexamples[
|
||||||
(define (add-bigger-fish lst)
|
(define (add-bigger-fish lst)
|
||||||
(define-struct fish (size) #:inspector #f)
|
(define-struct fish (size) #:inspector #f) (code:comment #,(t "new every time"))
|
||||||
(cond
|
(cond
|
||||||
[(null? lst) (list (make-fish 1))]
|
[(null? lst) (list (make-fish 1))]
|
||||||
[else (cons (make-fish (* 2 (fish-size (car lst))))
|
[else (cons (make-fish (* 2 (fish-size (car lst))))
|
||||||
|
@ -320,3 +326,5 @@ times.
|
||||||
lst)]))]
|
lst)]))]
|
||||||
(add-bigger-fish (add-bigger-fish null))
|
(add-bigger-fish (add-bigger-fish null))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@refdetails["mz:structures"]{structure types}
|
||||||
|
|
|
@ -31,7 +31,7 @@ precise details to @|MzScheme| and other reference manuals.
|
||||||
|
|
||||||
@include-section["define-struct.scrbl"]
|
@include-section["define-struct.scrbl"]
|
||||||
|
|
||||||
@include-section["module-basics.scrbl"]
|
@include-section["modules.scrbl"]
|
||||||
|
|
||||||
@; ----------------------------------------------------------------------
|
@; ----------------------------------------------------------------------
|
||||||
@section[#:tag "guide:contracts"]{Contracts}
|
@section[#:tag "guide:contracts"]{Contracts}
|
||||||
|
@ -80,7 +80,7 @@ of an expression to the values for the clause:
|
||||||
@section[#:tag "threads"]{Threads}
|
@section[#:tag "threads"]{Threads}
|
||||||
|
|
||||||
@; ----------------------------------------------------------------------
|
@; ----------------------------------------------------------------------
|
||||||
@section[#:tag "guide:macros"]{Syntactic Extension@aux-elem{ (Modules and Macros)}}
|
@section[#:tag "guide:macros"]{Syntactic Extension@aux-elem{ (Macros)}}
|
||||||
|
|
||||||
|
|
||||||
@; ----------------------------------------------------------------------
|
@; ----------------------------------------------------------------------
|
||||||
|
|
|
@ -3,19 +3,12 @@
|
||||||
@require[(lib "eval.ss" "scribble")]
|
@require[(lib "eval.ss" "scribble")]
|
||||||
@require["guide-utils.ss"]
|
@require["guide-utils.ss"]
|
||||||
|
|
||||||
@title{Modules}
|
@title[#:tag "guide:module-basics"]{Module Basics}
|
||||||
|
|
||||||
Scheme definitions and expressions are normally written inside of a
|
The space of module names is distinct from the space of normal Scheme
|
||||||
module. Although a REPL evaluates definitions and expressions outide
|
definitions. Indeed, since modules typically reside in files, the
|
||||||
of a module, and although @scheme[load] can evaluate definitions and
|
space of module names is explicitly tied to the filesystem at run
|
||||||
expressions from a file as if they appeared in a REPL interaction,
|
time. For example, if the file @file{/home/molly/cake.ss} contains
|
||||||
code that is meant to last for more than a few seconds belongs in a
|
|
||||||
module.
|
|
||||||
|
|
||||||
The space of modules is distinct from the space of normal Scheme
|
|
||||||
definitions. Since modules typically reside in files, the space of
|
|
||||||
module names is explicitly tied to the filesystem at run time. For
|
|
||||||
example, if the file @file{/home/molly/cake.ss} contains
|
|
||||||
|
|
||||||
@schememod[
|
@schememod[
|
||||||
big
|
big
|
||||||
|
@ -31,10 +24,14 @@ big
|
||||||
]
|
]
|
||||||
|
|
||||||
then it can be used as the source of a module whose full name is based
|
then it can be used as the source of a module whose full name is based
|
||||||
on the path @file{/home/molly/cake.ss}. Instead of using the full
|
on the path @file{/home/molly/cake.ss}. The @scheme[provide] line
|
||||||
path, however, the module is likely to be referenced by a releative
|
exports the definition @scheme[print-cake] so that it can be used
|
||||||
path. For example, a file @file{/home/molly/random-cake.ss} could use
|
outside the module.
|
||||||
the module like this:
|
|
||||||
|
Instead of using its full path, a module is more likely to be
|
||||||
|
referenced by a relative path. For example, a file
|
||||||
|
@file{/home/molly/random-cake.ss} could use the @file{cake.ss} module
|
||||||
|
like this:
|
||||||
|
|
||||||
@schememod[
|
@schememod[
|
||||||
big
|
big
|
||||||
|
@ -44,10 +41,40 @@ big
|
||||||
(print-cake (random 30))
|
(print-cake (random 30))
|
||||||
]
|
]
|
||||||
|
|
||||||
The relative reference @scheme[(require "cake.ss")] works because the
|
The relative reference @scheme["cake.ss"] in the import
|
||||||
@file{cake.ss} module source is in the same directory as the
|
@scheme[(require "cake.ss")] works because the @file{cake.ss} module
|
||||||
@file{random-cake.ss} file.
|
source is in the same directory as the @file{random-cake.ss}
|
||||||
|
file. (Unix-style relative paths are used for relative module
|
||||||
|
references on all platforms, much like relative URLs.)
|
||||||
|
|
||||||
As you see in the above examples, @scheme[provide] and
|
Library modules that are distributed with PLT Scheme are referenced
|
||||||
@scheme[require] are module-level declarations that export and import
|
through a @scheme[lib] path. A @scheme[lib] path is like a relative
|
||||||
bindings between modules.
|
path, but it is relative (roughly) to the library installation
|
||||||
|
directory.
|
||||||
|
|
||||||
|
@schememod[
|
||||||
|
big
|
||||||
|
|
||||||
|
(require (lib "mzlib/date.ss"))
|
||||||
|
|
||||||
|
(printf "Today is ~s\n"
|
||||||
|
(date->string (seconds->date (current-seconds))))
|
||||||
|
]
|
||||||
|
|
||||||
|
Additional third-party libraries that are distributed through
|
||||||
|
@|PLaneT| can be imported using a @scheme[planet] form:
|
||||||
|
|
||||||
|
@schememod[
|
||||||
|
big
|
||||||
|
|
||||||
|
(require "cake.ss"
|
||||||
|
(planet "random.ss" ("schematics" "random.plt" 1 0)))
|
||||||
|
|
||||||
|
(print-cake (inexact->exact
|
||||||
|
(round (* 30 (random-gaussian)))))
|
||||||
|
]
|
||||||
|
|
||||||
|
A @|PLaneT| reference starts like a @scheme[lib] reference, with a
|
||||||
|
relative path, but the path is followed by information about the
|
||||||
|
producer, archive, and version of the library. The specified archive
|
||||||
|
is downloaded and installed on demand.
|
||||||
|
|
87
collects/scribblings/guide/module-syntax.scrbl
Normal file
87
collects/scribblings/guide/module-syntax.scrbl
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
#reader(lib "docreader.ss" "scribble")
|
||||||
|
@require[(lib "manual.ss" "scribble")]
|
||||||
|
@require[(lib "eval.ss" "scribble")]
|
||||||
|
@require["guide-utils.ss"]
|
||||||
|
|
||||||
|
@title{Module Syntax}
|
||||||
|
|
||||||
|
The @litchar{#module} at the start of a module file begins a shorthand
|
||||||
|
for a @scheme[module] form, much like @litchar{'} is a shorthand for a
|
||||||
|
@scheme[quote] form. Unlike @litchar{'}, the @litchar{#module}
|
||||||
|
shorthand does not work well in a REPL, in part because it must be
|
||||||
|
terminated by an end-of-file, but also because the longhand expansion
|
||||||
|
of @litchar{#module} depends on the name of the enclosing file.
|
||||||
|
|
||||||
|
@;------------------------------------------------------------------------
|
||||||
|
@section{The @scheme[module] Form}
|
||||||
|
|
||||||
|
The longhand form of a module declaration, which works in a REPL as
|
||||||
|
well as a file, is
|
||||||
|
|
||||||
|
@specform[
|
||||||
|
(module name-id initial-module-path
|
||||||
|
decl ...)
|
||||||
|
]
|
||||||
|
|
||||||
|
where the @scheme[_name-id] is a name for the module,
|
||||||
|
@scheme[_initial-module-path] is an initial import, and each
|
||||||
|
@scheme[_decl] is an import, export, definition, or expression. In
|
||||||
|
the case of a file, @scheme[_name-id] must match the name of the
|
||||||
|
containing file, minus its directory path or file extension.
|
||||||
|
|
||||||
|
The @scheme[_initial-module-path] is needed because even the
|
||||||
|
@scheme[require] form must be imported for further use in the module
|
||||||
|
body. In other words, the @scheme[_initial-module-path] import
|
||||||
|
bootstraps the syntax available in the body. The most commonly used
|
||||||
|
@scheme[_initial-module-path] is @scheme[(lib "big/lang.ss")], which
|
||||||
|
supplies most of the bindings described in this guide, including
|
||||||
|
@scheme[require], @scheme[define], and @scheme[provide].
|
||||||
|
|
||||||
|
For example, the @file{cake.ss} example of the
|
||||||
|
@seclink["guide:module-basics"]{previous section} could be written as
|
||||||
|
|
||||||
|
@schemeblock[
|
||||||
|
(module cake (lib "big/lang.ss")
|
||||||
|
(provide print-cake)
|
||||||
|
|
||||||
|
(define (print-cake n)
|
||||||
|
(printf " ~a \n" (make-string n #\.))
|
||||||
|
(printf " .-~a-.\n" (make-string n #\|))
|
||||||
|
(printf " | ~a |\n" (make-string n #\space))
|
||||||
|
(printf "---~a---\n" (make-string n #\-))))
|
||||||
|
]
|
||||||
|
|
||||||
|
Furthermore, this @scheme[module] form can be evaluated in a REPL to
|
||||||
|
declare a @scheme[cake] module that is not associated with any file.
|
||||||
|
|
||||||
|
@;------------------------------------------------------------------------
|
||||||
|
@section{The @schememodfont{#module} Shorthand}
|
||||||
|
|
||||||
|
Unlike @litchar{'}, there is no fixed syntax for the body of a
|
||||||
|
@litchar{#module} shorthand. In general, the syntax is determined by
|
||||||
|
the language name that follows @litchar{#module}.
|
||||||
|
|
||||||
|
In the case of @schememodfont{#module} @schememodname[big], the syntax
|
||||||
|
is
|
||||||
|
|
||||||
|
@schememod[
|
||||||
|
big
|
||||||
|
_decl ...]
|
||||||
|
|
||||||
|
which reads the same as
|
||||||
|
|
||||||
|
@schemeblock[
|
||||||
|
(module _name (lib "big/lang.ss")
|
||||||
|
_decl ...)
|
||||||
|
]
|
||||||
|
|
||||||
|
where @scheme[_name] is derived from the name of the file that
|
||||||
|
contains the @schememodfont{#module} form.
|
||||||
|
|
||||||
|
The @schememodfont{#module} @scheme[little] form has the same syntax
|
||||||
|
as @schememodfont{#module} @schememodname[big], except that the
|
||||||
|
longhand expansion uses @scheme[(lib "little/lang.ss")] instead of
|
||||||
|
@scheme[(lib "big/lang.ss")]. The @schememodfont{#module}
|
||||||
|
@scheme[honu] form, in contrast, has a completely different syntax
|
||||||
|
that doesn't even look like Scheme, and which we do not attempt to
|
||||||
|
describe in this guide.
|
18
collects/scribblings/guide/modules.scrbl
Normal file
18
collects/scribblings/guide/modules.scrbl
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#reader(lib "docreader.ss" "scribble")
|
||||||
|
@require[(lib "manual.ss" "scribble")]
|
||||||
|
@require[(lib "eval.ss" "scribble")]
|
||||||
|
@require["guide-utils.ss"]
|
||||||
|
|
||||||
|
@title[#:tag "guide:modules" #:style 'toc]{Modules}
|
||||||
|
|
||||||
|
Scheme definitions and expressions are normally written inside of a
|
||||||
|
module. Although a REPL evaluates definitions and expressions outside
|
||||||
|
of a module for exploration and debugging purposes, and although
|
||||||
|
@scheme[load] can evaluate definitions and expressions from a file as
|
||||||
|
if they appeared in a REPL interaction, code that is meant to last for
|
||||||
|
more than a few seconds belongs in a module.
|
||||||
|
|
||||||
|
@local-table-of-contents[]
|
||||||
|
|
||||||
|
@include-section["module-basics.scrbl"]
|
||||||
|
@include-section["module-syntax.scrbl"]
|
|
@ -22,38 +22,42 @@
|
||||||
[field-option #:immutable
|
[field-option #:immutable
|
||||||
#:auto])]{
|
#:auto])]{
|
||||||
|
|
||||||
Creates a new structure type, and binds transformers and
|
@moreref["mz:structures"]{structures}
|
||||||
variables related to the new structure type. A
|
|
||||||
|
Creates a new @techlink{structure type}, and binds transformers and
|
||||||
|
variables related to the new @tech{structure type}. A
|
||||||
@scheme[define-struct] form with @math{n} @scheme[field]s defines
|
@scheme[define-struct] form with @math{n} @scheme[field]s defines
|
||||||
up to @math{4+2n} names:
|
up to @math{4+2n} names:
|
||||||
|
|
||||||
@itemize{
|
@itemize{
|
||||||
|
|
||||||
@item{@schemeidfont{struct:}@scheme[id], a @tech{structure type descriptor}
|
@item{@schemeidfont{struct:}@scheme[id], a @deftech{structure type
|
||||||
value that represents the new datatype.}
|
descriptor} value that represents the new @tech{structure
|
||||||
|
type}.}
|
||||||
|
|
||||||
@item{@schemeidfont{make-}@scheme[id], a @deftech{constructor}
|
@item{@schemeidfont{make-}@scheme[id], a @deftech{constructor}
|
||||||
procedure that takes @math{m} arguments and returns a new
|
procedure that takes @math{m} arguments and returns a new
|
||||||
instance of the structure type, where @math{m} is the number of
|
instance of the @tech{structure type}, where @math{m} is the
|
||||||
@scheme[field]s that do not include an @scheme[#:auto] option.}
|
number of @scheme[field]s that do not include an
|
||||||
|
@scheme[#:auto] option.}
|
||||||
|
|
||||||
@item{@scheme[id]@schemeidfont{?}, a @deftech{predicate} procedure
|
@item{@scheme[id]@schemeidfont{?}, a @deftech{predicate} procedure
|
||||||
that returns @scheme[#t] for instances of the structure type
|
that returns @scheme[#t] for instances of the @tech{structure
|
||||||
(constructed by @schemeidfont{make-}@scheme[id] or the
|
type} (constructed by @schemeidfont{make-}@scheme[id] or the
|
||||||
@tech{constructor} for a subtype) and @scheme[#f] for any other
|
@tech{constructor} for a subtype) and @scheme[#f] for any other
|
||||||
value.}
|
value.}
|
||||||
|
|
||||||
@item{@scheme[id]@schemeidfont{-}@scheme[field-id], for each
|
@item{@scheme[id]@schemeidfont{-}@scheme[field-id], for each
|
||||||
@scheme[field]; an @deftech{accessor} procedure that takes an
|
@scheme[field]; an @deftech{accessor} procedure that takes an
|
||||||
instance of the structure type and extracts the value for the
|
instance of the @tech{structure type} and extracts the value
|
||||||
corresponding field.}
|
for the corresponding field.}
|
||||||
|
|
||||||
@item{@schemeidfont{set-}@scheme[id]@schemeidfont{-}@scheme[field-id]@schemeidfont{!},
|
@item{@schemeidfont{set-}@scheme[id]@schemeidfont{-}@scheme[field-id]@schemeidfont{!},
|
||||||
for each @scheme[field] that does not include a
|
for each @scheme[field] that does not include a
|
||||||
@scheme[#:immutable] option, and only when the
|
@scheme[#:immutable] option, and only when the
|
||||||
@scheme[#:immutable] option is not specified as a
|
@scheme[#:immutable] option is not specified as a
|
||||||
@scheme[struct-option]; a @deftech{mutator} procedure that
|
@scheme[struct-option]; a @deftech{mutator} procedure that
|
||||||
takes an instance of the structure type and a new field
|
takes an instance of the @tech{structure type} and a new field
|
||||||
value. The structure is destructively updated with the new
|
value. The structure is destructively updated with the new
|
||||||
value, and @|void-const| is returned.}
|
value, and @|void-const| is returned.}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,19 @@
|
||||||
[(_ s) (scheme s)]))
|
[(_ s) (scheme s)]))
|
||||||
(provide exnraise Exn)
|
(provide exnraise Exn)
|
||||||
|
|
||||||
(provide Guide guideintro)
|
(provide refalso moreref Guide guideintro)
|
||||||
|
|
||||||
|
(define/kw (refalso tag #:body s)
|
||||||
|
(apply margin-note
|
||||||
|
(decode-content (append (list magnify (secref tag) " also provides information on ")
|
||||||
|
s
|
||||||
|
(list ".")))))
|
||||||
|
|
||||||
|
(define/kw (moreref tag #:body s)
|
||||||
|
(apply margin-note
|
||||||
|
(decode-content (append (list magnify (secref tag) " provides more information on ")
|
||||||
|
s
|
||||||
|
(list ".")))))
|
||||||
|
|
||||||
(define Guide
|
(define Guide
|
||||||
(italic (link "../guide/index.html" "A Guide to PLT Scheme")))
|
(italic (link "../guide/index.html" "A Guide to PLT Scheme")))
|
||||||
|
@ -27,5 +39,6 @@
|
||||||
(define/kw (guideintro tag #:body s)
|
(define/kw (guideintro tag #:body s)
|
||||||
(apply margin-note
|
(apply margin-note
|
||||||
(decode-content (append (list finger (secref tag) " in " Guide " introduces ")
|
(decode-content (append (list finger (secref tag) " in " Guide " introduces ")
|
||||||
s)))))
|
s
|
||||||
|
(list "."))))))
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,9 @@
|
||||||
|
|
||||||
@title[#:tag "mz:structures"]{Structures}
|
@title[#:tag "mz:structures"]{Structures}
|
||||||
|
|
||||||
A @pidefterm{structure type} is a record datatype composing a number
|
@guideintro["guide:define-struct"]{structure types via @scheme[define-struct]}
|
||||||
|
|
||||||
|
A @deftech{structure type} is a record datatype composing a number
|
||||||
of @idefterm{fields}. A @pidefterm{structure}, an instance of a
|
of @idefterm{fields}. A @pidefterm{structure}, an instance of a
|
||||||
structure type, is a first-class value that contains a value for each
|
structure type, is a first-class value that contains a value for each
|
||||||
field of the structure type. A structure instance is created with a
|
field of the structure type. A structure instance is created with a
|
||||||
|
@ -13,6 +15,8 @@ accessed and changed with type-specific @tech{accessor} and
|
||||||
@tech{predicate} procedure that answers @scheme[#t] for instances of
|
@tech{predicate} procedure that answers @scheme[#t] for instances of
|
||||||
the structure type and @scheme[#f] for any other value.
|
the structure type and @scheme[#f] for any other value.
|
||||||
|
|
||||||
|
@refalso["mz:define-struct"]{structure types via @scheme[define-struct]}
|
||||||
|
|
||||||
A structure type's fields are essentially unnamed, though names are
|
A structure type's fields are essentially unnamed, though names are
|
||||||
supported for error-reporting purposes. The constructor procedure
|
supported for error-reporting purposes. The constructor procedure
|
||||||
takes one value for each field of the structure type, except that some
|
takes one value for each field of the structure type, except that some
|
||||||
|
@ -83,13 +87,13 @@ Creates a new structure type. The @scheme[name] argument is used as
|
||||||
the type name. If @scheme[super-type] is not @scheme[#f], the new type
|
the type name. If @scheme[super-type] is not @scheme[#f], the new type
|
||||||
is a subtype of the corresponding structure type.
|
is a subtype of the corresponding structure type.
|
||||||
|
|
||||||
The new structure type has @scheme[(+ init-field-cnt auto-field-cnt)]
|
The new structure type has @math{@scheme[init-field-cnt]+@scheme[auto-field-cnt]}
|
||||||
fields (in addition to any fields from @scheme[super-type]), but only
|
fields (in addition to any fields from @scheme[super-type]), but only
|
||||||
@scheme[init-field-cnt] constructor arguments (in addition to any
|
@scheme[init-field-cnt] constructor arguments (in addition to any
|
||||||
constructor arguments from @scheme[super-type]). The remaining
|
constructor arguments from @scheme[super-type]). The remaining
|
||||||
fields are initialized with @scheme[auto-v].
|
fields are initialized with @scheme[auto-v].
|
||||||
|
|
||||||
The @scheme{props} argument is a list of pairs, where the @scheme[car]
|
The @scheme[props] argument is a list of pairs, where the @scheme[car]
|
||||||
of each pair is a structure type property descriptor, and the
|
of each pair is a structure type property descriptor, and the
|
||||||
@scheme[cdr] is an arbitrary value. See @secref["mz:structprops"] for
|
@scheme[cdr] is an arbitrary value. See @secref["mz:structprops"] for
|
||||||
more information about properties.
|
more information about properties.
|
||||||
|
@ -131,7 +135,7 @@ subtype's guard procedure become the first @math{n} arguments to
|
||||||
@scheme[guard].
|
@scheme[guard].
|
||||||
|
|
||||||
The result of @scheme[make-struct-type] is five values:
|
The result of @scheme[make-struct-type] is five values:
|
||||||
%
|
|
||||||
@itemize{
|
@itemize{
|
||||||
|
|
||||||
@item{a @tech{structure type descriptor},}
|
@item{a @tech{structure type descriptor},}
|
||||||
|
@ -158,7 +162,9 @@ The result of @scheme[make-struct-type] is five values:
|
||||||
(a-ref an-a 2)
|
(a-ref an-a 2)
|
||||||
(define a-first (make-struct-field-accessor a-ref 0))
|
(define a-first (make-struct-field-accessor a-ref 0))
|
||||||
(a-first an-a)
|
(a-first an-a)
|
||||||
|
]
|
||||||
|
|
||||||
|
@interaction[
|
||||||
(define-values (struct:b make-b b? b-ref b-set!)
|
(define-values (struct:b make-b b? b-ref b-set!)
|
||||||
(make-struct-type 'b struct:a 1 2 'b-uninitialized))
|
(make-struct-type 'b struct:a 1 2 'b-uninitialized))
|
||||||
(define a-b (make-b 'x 'y 'z))
|
(define a-b (make-b 'x 'y 'z))
|
||||||
|
@ -167,7 +173,9 @@ The result of @scheme[make-struct-type] is five values:
|
||||||
(b-ref a-b 0)
|
(b-ref a-b 0)
|
||||||
(b-ref a-b 1)
|
(b-ref a-b 1)
|
||||||
(b-ref a-b 2)
|
(b-ref a-b 2)
|
||||||
|
]
|
||||||
|
|
||||||
|
@interaction[
|
||||||
(define-values (struct:c make-c c? c-ref c-set!)
|
(define-values (struct:c make-c c? c-ref c-set!)
|
||||||
(make-struct-type
|
(make-struct-type
|
||||||
'c struct:b 0 0 #f null (make-inspector) #f null
|
'c struct:b 0 0 #f null (make-inspector) #f null
|
||||||
|
@ -235,7 +243,7 @@ A @index['("structure type properties")]{@defterm{structure type
|
||||||
|
|
||||||
@itemize{
|
@itemize{
|
||||||
|
|
||||||
@item{a @deftech{structure property type descriptor}, for use with
|
@item{a @deftech{structure type property descriptor}, for use with
|
||||||
@scheme[make-struct-type] and @scheme[define-struct];}
|
@scheme[make-struct-type] and @scheme[define-struct];}
|
||||||
|
|
||||||
@item{a @deftech{property predicate} procedure, which takes an
|
@item{a @deftech{property predicate} procedure, which takes an
|
||||||
|
|
Loading…
Reference in New Issue
Block a user