Debian Codegraph @

doc/guide/src/MLBasisExamples.adoc

raw
1 = MLBasisExamples
2
3 Here are some example uses of <<MLBasis#,ML Basis>> files.
4
5
6 == Complete program
7
8 Suppose your complete program consists of the files `file1.sml`, ...,
9 `filen.sml`, which depend upon libraries `lib1.mlb`, ..., `libm.mlb`.
10
11 ----
12 (* import libraries *)
13 lib1.mlb
14 ...
15 libm.mlb
16
17 (* program files *)
18 file1.sml
19 ...
20 filen.sml
21 ----
22
23 The bases denoted by `lib1.mlb`, ..., `libm.mlb` are merged (bindings
24 of names in later bases take precedence over bindings of the same name
25 in earlier bases), producing a basis in which `file1.sml`, ...,
26 `filen.sml` are elaborated, adding additional bindings to the basis.
27
28
29 == Export filter
30
31 Suppose you only want to export certain structures, signatures, and
32 functors from a collection of files.
33
34 ----
35 local
36 file1.sml
37 ...
38 filen.sml
39 in
40 (* export filter here *)
41 functor F
42 structure S
43 end
44 ----
45
46 While `file1.sml`, ..., `filen.sml` may declare top-level identifiers
47 in addition to `F` and `S`, such names are not accessible to programs
48 and libraries that import this `.mlb`.
49
50
51 == Export filter with renaming
52
53 Suppose you want an export filter, but want to rename one of the
54 modules.
55
56 ----
57 local
58 file1.sml
59 ...
60 filen.sml
61 in
62 (* export filter, with renaming, here *)
63 functor F
64 structure S' = S
65 end
66 ----
67
68 Note that `functor F` is an abbreviation for `functor F = F`, which
69 simply exports an identifier under the same name.
70
71
72 == Import filter
73
74 Suppose you only want to import a functor `F` from one library and a
75 structure `S` from another library.
76
77 ----
78 local
79 lib1.mlb
80 in
81 (* import filter here *)
82 functor F
83 end
84 local
85 lib2.mlb
86 in
87 (* import filter here *)
88 structure S
89 end
90 file1.sml
91 ...
92 filen.sml
93 ----
94
95
96 == Import filter with renaming
97
98 Suppose you want to import a structure `S` from one library and
99 another structure `S` from another library.
100
101 ----
102 local
103 lib1.mlb
104 in
105 (* import filter, with renaming, here *)
106 structure S1 = S
107 end
108 local
109 lib2.mlb
110 in
111 (* import filter, with renaming, here *)
112 structure S2 = S
113 end
114 file1.sml
115 ...
116 filen.sml
117 ----
118
119
120 == Full Basis
121
122 Since the Modules level of SML is the natural means for organizing
123 program and library components, MLB files provide convenient syntax
124 for renaming Modules level identifiers (in fact, renaming of functor
125 identifiers provides a mechanism that is not available in SML).
126 However, please note that `.mlb` files elaborate to full bases
127 including top-level types and values (including infix status), in
128 addition to structures, signatures, and functors. For example,
129 suppose you wished to extend the <<BasisLibrary#,Basis Library>> with an
130 `('a, 'b) either` datatype corresponding to a disjoint sum; the type
131 and some operations should be available at the top-level;
132 additionally, a signature and structure provide the complete
133 interface.
134
135 We could use the following files.
136
137 `either-sigs.sml`
138 [source,sml]
139 ----
140 signature EITHER_GLOBAL =
141 sig
142 datatype ('a, 'b) either = Left of 'a | Right of 'b
143 val & : ('a -> 'c) * ('b -> 'c) -> ('a, 'b) either -> 'c
144 val && : ('a -> 'c) * ('b -> 'd) -> ('a, 'b) either -> ('c, 'd) either
145 end
146
147 signature EITHER =
148 sig
149 include EITHER_GLOBAL
150 val isLeft : ('a, 'b) either -> bool
151 val isRight : ('a, 'b) either -> bool
152 ...
153 end
154 ----
155
156 `either-strs.sml`
157 [source,sml]
158 ----
159 structure Either : EITHER =
160 struct
161 datatype ('a, 'b) either = Left of 'a | Right of 'b
162 fun f & g = fn x =>
163 case x of Left z => f z | Right z => g z
164 fun f && g = (Left o f) & (Right o g)
165 fun isLeft x = ((fn _ => true) & (fn _ => false)) x
166 fun isRight x = (not o isLeft) x
167 ...
168 end
169 structure EitherGlobal : EITHER_GLOBAL = Either
170 ----
171
172 `either-infixes.sml`
173 [source,sml]
174 ----
175 infixr 3 & &&
176 ----
177
178 `either-open.sml`
179 [source,sml]
180 ----
181 open EitherGlobal
182 ----
183
184 `either.mlb`
185 ----
186 either-infixes.sml
187 local
188 (* import Basis Library *)
189 $(SML_LIB)/basis/basis.mlb
190 either-sigs.sml
191 either-strs.sml
192 in
193 signature EITHER
194 structure Either
195 either-open.sml
196 end
197 ----
198
199 A client that imports `either.mlb` will have access to neither
200 `EITHER_GLOBAL` nor `EitherGlobal`, but will have access to the type
201 `either` and the values `&` and `&&` (with infix status) in the
202 top-level environment. Note that `either-infixes.sml` is outside the
203 scope of the local, because we want the infixes available in the
204 implementation of the library and to clients of the library.