update docs to use `define-type'
document `define-predicate' svn: r18498 original commit: 24b9078560d936d046fe14b3198432a3f64cbd1c
This commit is contained in:
parent
2f1771b849
commit
b207bb9c0f
|
@ -70,7 +70,7 @@ represent these using @italic{union types}, written @scheme[(U t1 t2 ...)].
|
|||
|
||||
@schememod[
|
||||
typed/scheme
|
||||
(define-type-alias Tree (U leaf node))
|
||||
(define-type Tree (U leaf node))
|
||||
(define-struct: leaf ([val : Number]))
|
||||
(define-struct: node ([left : Tree] [right : Tree]))
|
||||
|
||||
|
@ -88,7 +88,7 @@ typed/scheme
|
|||
]
|
||||
|
||||
In this module, we have defined two new datatypes: @scheme[leaf] and
|
||||
@scheme[node]. We've also defined the type alias @scheme[Tree] to be
|
||||
@scheme[node]. We've also defined the type name @scheme[Tree] to be
|
||||
@scheme[(U node leaf)], which represents a binary tree of numbers. In
|
||||
essence, we are saying that the @scheme[tree-height] function accepts
|
||||
a @scheme[Tree], which is either a @scheme[node] or a @scheme[leaf],
|
||||
|
|
|
@ -152,9 +152,9 @@ variable has type @scheme[(Integer (Listof Integer) -> Integer)].
|
|||
|
||||
@section{New Type Names}
|
||||
|
||||
Any type can be given a name with @scheme[define-type-alias].
|
||||
Any type can be given a name with @scheme[define-type].
|
||||
|
||||
@schemeblock[(define-type-alias NN (Number -> Number))]
|
||||
@schemeblock[(define-type NN (Number -> Number))]
|
||||
|
||||
Anywhere the name @scheme[NN] is used, it is expanded to
|
||||
@scheme[(Number -> Number)]. Type aliases may not be recursive.
|
||||
@scheme[(Number -> Number)]. Type names may not be recursive.
|
|
@ -141,7 +141,7 @@ represent these using @italic{union types}, written @scheme[(U t1 t2 ...)].
|
|||
|
||||
@schememod[
|
||||
typed-scheme
|
||||
(define-type-alias Tree (U leaf node))
|
||||
(define-type Tree (U leaf node))
|
||||
(define-struct: leaf ([val : Number]))
|
||||
(define-struct: node ([left : Tree] [right : Tree]))
|
||||
|
||||
|
@ -159,7 +159,7 @@ typed-scheme
|
|||
]
|
||||
|
||||
In this module, we have defined two new datatypes: @scheme[leaf] and
|
||||
@scheme[node]. We've also defined the type alias @scheme[Tree] to be
|
||||
@scheme[node]. We've also defined the type name @scheme[Tree] to be
|
||||
@scheme[(U node leaf)], which represents a binary tree of numbers. In
|
||||
essence, we are saying that the @scheme[tree-height] function accepts
|
||||
a @scheme[Tree], which is either a @scheme[node] or a @scheme[leaf],
|
||||
|
@ -217,7 +217,7 @@ typed-scheme
|
|||
(define-struct: Nothing ())
|
||||
(define-struct: (a) Just ([v : a]))
|
||||
|
||||
(define-type-alias (Maybe a) (U Nothing (Just a)))
|
||||
(define-type (Maybe a) (U Nothing (Just a)))
|
||||
|
||||
(: find (Number (Listof Number) -> (Maybe Number)))
|
||||
(define (find v l)
|
||||
|
@ -241,11 +241,11 @@ one element, whose type is that of the type argument to
|
|||
this case) are written before the type name, and can be referred to in
|
||||
the types of the fields.
|
||||
|
||||
The type alias definiton
|
||||
The type definiton
|
||||
@schemeblock[
|
||||
(define-type-alias (Maybe a) (U Nothing (Just a)))
|
||||
(define-type (Maybe a) (U Nothing (Just a)))
|
||||
]
|
||||
creates a parameterized alias --- @scheme[Maybe] is a potential
|
||||
creates a parameterized type --- @scheme[Maybe] is a potential
|
||||
container for whatever type is supplied.
|
||||
|
||||
The @scheme[find] function takes a number @scheme[v] and list, and
|
||||
|
|
|
@ -206,14 +206,20 @@ structure is a substructure of @scheme[parent]. When
|
|||
Like @scheme[define-struct:], but defines an procedural structure.
|
||||
The procdure @scheme[e] is used as the value for @scheme[prop:procedure], and must have type @scheme[proc-t].}
|
||||
|
||||
@subsection{Type Aliases}
|
||||
@defform*[[(define-type-alias name t)
|
||||
(define-type-alias (name v ...) t)]]{
|
||||
@subsection{Names for Types}
|
||||
@defform*[[(define-type name t)
|
||||
(define-type (name v ...) t)]]{
|
||||
The first form defines @scheme[name] as type, with the same meaning as
|
||||
@scheme[t]. The second form is equivalent to
|
||||
@scheme[(define-type-alias name (All (v ...) t))]. Type aliases may
|
||||
refer to other type aliases or types defined in the same module, but
|
||||
cycles among type aliases are prohibited.}
|
||||
@scheme[(define-type name (All (v ...) t))]. Type names may
|
||||
refer to other types defined in the same module, but
|
||||
cycles among them are prohibited.}
|
||||
|
||||
@subsection{Generating Predicates Automatically}
|
||||
@defform[(define-predicate name t)]{
|
||||
Defines @scheme[name] as a predicate for the type @scheme[t].
|
||||
@scheme[name] has the type @scheme[(Any -> Boolean : t)].
|
||||
@scheme[t] may not contain function types.}
|
||||
|
||||
|
||||
@subsection{Type Annotation and Instantiation}
|
||||
|
|
|
@ -175,7 +175,7 @@ typed/scheme
|
|||
(define-struct: None ())
|
||||
(define-struct: (a) Some ([v : a]))
|
||||
|
||||
(define-type-alias (Opt a) (U None (Some a)))
|
||||
(define-type (Opt a) (U None (Some a)))
|
||||
|
||||
(: find (Number (Listof Number) -> (Opt Number)))
|
||||
(define (find v l)
|
||||
|
@ -199,11 +199,11 @@ one element, whose type is that of the type argument to
|
|||
this case) are written before the type name, and can be referred to in
|
||||
the types of the fields.
|
||||
|
||||
The type alias definiton
|
||||
The type definiton
|
||||
@schemeblock[
|
||||
(define-type-alias (Opt a) (U None (Some a)))
|
||||
(define-type (Opt a) (U None (Some a)))
|
||||
]
|
||||
creates a parameterized alias --- @scheme[Opt] is a potential
|
||||
creates a parameterized type --- @scheme[Opt] is a potential
|
||||
container for whatever type is supplied.
|
||||
|
||||
The @scheme[find] function takes a number @scheme[v] and list, and
|
||||
|
|
Loading…
Reference in New Issue
Block a user