4 Here are some example uses of <:MLBasis:ML Basis> files.
9 Suppose your complete program consists of the files `file1.sml`, ...,
10 `filen.sml`, which depend upon libraries `lib1.mlb`, ..., `libm.mlb`.
13 (* import libraries *)
24 The bases denoted by `lib1.mlb`, ..., `libm.mlb` are merged (bindings
25 of names in later bases take precedence over bindings of the same name
26 in earlier bases), producing a basis in which `file1.sml`, ...,
27 `filen.sml` are elaborated, adding additional bindings to the basis.
32 Suppose you only want to export certain structures, signatures, and
33 functors from a collection of files.
41 (* export filter here *)
47 While `file1.sml`, ..., `filen.sml` may declare top-level identifiers
48 in addition to `F` and `S`, such names are not accessible to programs
49 and libraries that import this `.mlb`.
52 == Export filter with renaming ==
54 Suppose you want an export filter, but want to rename one of the
63 (* export filter, with renaming, here *)
69 Note that `functor F` is an abbreviation for `functor F = F`, which
70 simply exports an identifier under the same name.
75 Suppose you only want to import a functor `F` from one library and a
76 structure `S` from another library.
82 (* import filter here *)
88 (* import filter here *)
97 == Import filter with renaming ==
99 Suppose you want to import a structure `S` from one library and
100 another structure `S` from another library.
106 (* import filter, with renaming, here *)
112 (* import filter, with renaming, here *)
123 Since the Modules level of SML is the natural means for organizing
124 program and library components, MLB files provide convenient syntax
125 for renaming Modules level identifiers (in fact, renaming of functor
126 identifiers provides a mechanism that is not available in SML).
127 However, please note that `.mlb` files elaborate to full bases
128 including top-level types and values (including infix status), in
129 addition to structures, signatures, and functors. For example,
130 suppose you wished to extend the <:BasisLibrary:Basis Library> with an
131 `('a, 'b) either` datatype corresponding to a disjoint sum; the type
132 and some operations should be available at the top-level;
133 additionally, a signature and structure provide the complete
136 We could use the following files.
141 signature EITHER_GLOBAL =
143 datatype ('a, 'b) either = Left of 'a | Right of 'b
144 val & : ('a -> 'c) * ('b -> 'c) -> ('a, 'b) either -> 'c
145 val && : ('a -> 'c) * ('b -> 'd) -> ('a, 'b) either -> ('c, 'd) either
150 include EITHER_GLOBAL
151 val isLeft : ('a, 'b) either -> bool
152 val isRight : ('a, 'b) either -> bool
160 structure Either : EITHER =
162 datatype ('a, 'b) either = Left of 'a | Right of 'b
164 case x of Left z => f z | Right z => g z
165 fun f && g = (Left o f) & (Right o g)
166 fun isLeft x = ((fn _ => true) & (fn _ => false)) x
167 fun isRight x = (not o isLeft) x
170 structure EitherGlobal : EITHER_GLOBAL = Either
189 (* import Basis Library *)
190 $(SML_LIB)/basis/basis.mlb
200 A client that imports `either.mlb` will have access to neither
201 `EITHER_GLOBAL` nor `EitherGlobal`, but will have access to the type
202 `either` and the values `&` and `&&` (with infix status) in the
203 top-level environment. Note that `either-infixes.sml` is outside the
204 scope of the local, because we want the infixes available in the
205 implementation of the library and to clients of the library.