update docs to use `define-type'
document `define-predicate' svn: r18498
This commit is contained in:
parent
c24daa5dbb
commit
24b9078560
|
@ -70,7 +70,7 @@ represent these using @italic{union types}, written @scheme[(U t1 t2 ...)].
|
||||||
|
|
||||||
@schememod[
|
@schememod[
|
||||||
typed/scheme
|
typed/scheme
|
||||||
(define-type-alias Tree (U leaf node))
|
(define-type Tree (U leaf node))
|
||||||
(define-struct: leaf ([val : Number]))
|
(define-struct: leaf ([val : Number]))
|
||||||
(define-struct: node ([left : Tree] [right : Tree]))
|
(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
|
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
|
@scheme[(U node leaf)], which represents a binary tree of numbers. In
|
||||||
essence, we are saying that the @scheme[tree-height] function accepts
|
essence, we are saying that the @scheme[tree-height] function accepts
|
||||||
a @scheme[Tree], which is either a @scheme[node] or a @scheme[leaf],
|
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}
|
@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
|
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[
|
@schememod[
|
||||||
typed-scheme
|
typed-scheme
|
||||||
(define-type-alias Tree (U leaf node))
|
(define-type Tree (U leaf node))
|
||||||
(define-struct: leaf ([val : Number]))
|
(define-struct: leaf ([val : Number]))
|
||||||
(define-struct: node ([left : Tree] [right : Tree]))
|
(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
|
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
|
@scheme[(U node leaf)], which represents a binary tree of numbers. In
|
||||||
essence, we are saying that the @scheme[tree-height] function accepts
|
essence, we are saying that the @scheme[tree-height] function accepts
|
||||||
a @scheme[Tree], which is either a @scheme[node] or a @scheme[leaf],
|
a @scheme[Tree], which is either a @scheme[node] or a @scheme[leaf],
|
||||||
|
@ -217,7 +217,7 @@ typed-scheme
|
||||||
(define-struct: Nothing ())
|
(define-struct: Nothing ())
|
||||||
(define-struct: (a) Just ([v : a]))
|
(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)))
|
(: find (Number (Listof Number) -> (Maybe Number)))
|
||||||
(define (find v l)
|
(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
|
this case) are written before the type name, and can be referred to in
|
||||||
the types of the fields.
|
the types of the fields.
|
||||||
|
|
||||||
The type alias definiton
|
The type definiton
|
||||||
@schemeblock[
|
@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.
|
container for whatever type is supplied.
|
||||||
|
|
||||||
The @scheme[find] function takes a number @scheme[v] and list, and
|
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.
|
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].}
|
The procdure @scheme[e] is used as the value for @scheme[prop:procedure], and must have type @scheme[proc-t].}
|
||||||
|
|
||||||
@subsection{Type Aliases}
|
@subsection{Names for Types}
|
||||||
@defform*[[(define-type-alias name t)
|
@defform*[[(define-type name t)
|
||||||
(define-type-alias (name v ...) t)]]{
|
(define-type (name v ...) t)]]{
|
||||||
The first form defines @scheme[name] as type, with the same meaning as
|
The first form defines @scheme[name] as type, with the same meaning as
|
||||||
@scheme[t]. The second form is equivalent to
|
@scheme[t]. The second form is equivalent to
|
||||||
@scheme[(define-type-alias name (All (v ...) t))]. Type aliases may
|
@scheme[(define-type name (All (v ...) t))]. Type names may
|
||||||
refer to other type aliases or types defined in the same module, but
|
refer to other types defined in the same module, but
|
||||||
cycles among type aliases are prohibited.}
|
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}
|
@subsection{Type Annotation and Instantiation}
|
||||||
|
|
|
@ -175,7 +175,7 @@ typed/scheme
|
||||||
(define-struct: None ())
|
(define-struct: None ())
|
||||||
(define-struct: (a) Some ([v : a]))
|
(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)))
|
(: find (Number (Listof Number) -> (Opt Number)))
|
||||||
(define (find v l)
|
(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
|
this case) are written before the type name, and can be referred to in
|
||||||
the types of the fields.
|
the types of the fields.
|
||||||
|
|
||||||
The type alias definiton
|
The type definiton
|
||||||
@schemeblock[
|
@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.
|
container for whatever type is supplied.
|
||||||
|
|
||||||
The @scheme[find] function takes a number @scheme[v] and list, and
|
The @scheme[find] function takes a number @scheme[v] and list, and
|
||||||
|
|
Loading…
Reference in New Issue
Block a user