-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathscript.sml.out
331 lines (233 loc) · 6.67 KB
/
script.sml.out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
(*
* Licensed to Julian Hyde under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Julian Hyde licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*)
(*** Types ***)
val n : int = 66;
val n = 66 : int
val x : real = ~23.0;
val x = ~23.0 : real
(* Inference *)
val s : string = "example";
val s = "example" : string
val b : bool = true;
val b = true : bool
val s = "example";
val s = "example" : string
val b = true;
val b = true : bool
(* Tuples *)
val t0 = ();
val t0 = () : unit
val t3 = (3, "yes", "yes");
val t3 = (3,"yes","yes") : int * string * string
val t3b = (3, "yes", true);
val t3b = (3,"yes",true) : int * string * bool
val t2 = ((1, 2), 3);
val t2 = ((1,2),3) : int * int * int
val pair = ("a", "b");
val pair = ("a","b") : string * string
val (a, b) = pair;
val a = "a" : string
val b = "b" : string
val also_a = #1 pair;
val also_a = "a" : string
(* Records *)
val r = { a = 5, b = "five" };
val r = {a=5,b="five"} : {a:int, b:string}
val r2 = { b = "five", a = 5 };
val r2 = {a=5,b="five"} : {a:int, b:string}
r = r2;
val it = true : bool
val pair2 = { 1 = "a", 2 = "b" };
val pair2 = ("a","b") : string * string
pair = pair2;
val it = true : bool
(*** Functions ***)
fun factorial n =
if n < 1
then 1
else n * factorial (n - 1);
val factorial = fn : int -> int
factorial 3;
val it = 6 : int
(* Explicitly typed *)
(* TODO
fun factorial (n : int) : int = if n < 1 then 1 else n * factorial (n - 1);
factorial 3;
*)
(* Tuples as arguments *)
fun sum (a, b) = a + b;
val sum = fn : int * int -> int
fun average pair = sum pair div 2;
val average = fn : int * int -> int
val four = average (3, 5);
val four = 4 : int
(* TODO
infix averaged_with;
fun a averaged_with b = average (a, b);
val five = 3 averaged_with 7;
*)
(* Function returns tuple *)
fun pair (n : int) = (n, n);
val pair = fn : int -> int * int
(* Polymorphic *)
fun pair x = (x, x);
val pair = fn : 'a -> 'a * 'a
fun swap (x, y) = (y, x);
val swap = fn : 'a * 'b -> 'b * 'a
swap (1, "foo");
val it = ("foo",1) : string * int
(* Higher-order functions *)
fun pair_map (f, (x, y)) = (f x, f y);
val pair_map = fn : ('a -> 'b) * ('a * 'a) -> 'b * 'b
fun sum i j = i + j;
val sum = fn : int -> int -> int
val add_three = sum 3;
val add_three = fn : int -> int
val five = add_three 2;
val five = 5 : int
val ten = sum 5 5;
val ten = 10 : int
(* A more complex function *)
fun gray_code n =
if n < 1
then "" :: nil
else let val prev = gray_code (n-1)
in (map (fn s => "0" ^ s) prev) @ (rev (map (fn s => "1" ^ s) prev))
end;
stdIn:5.14-5.17 Error: unbound variable or constructor: nil
raised at: stdIn:5.14-5.17
val gray_code_3 = gray_code 3;
stdIn:1.19-1.28 Error: unbound variable or constructor: gray_code
raised at: stdIn:1.19-1.28
(*** Type declarations ***)
(* TODO
type int_pair = int * int;
fun swap_int_pair ((i,j) : int_pair) = (j,i);
fun swap_int_pair (i : int, j : int) = (j,i);
*)
(* Datatype declarations *)
datatype int_or_string = INT of int | STRING of string | NEITHER;
datatype int_or_string = INT of int | NEITHER | STRING of string
val i = INT 3;
val i = INT 3 : int_or_string
val s = STRING "qq";
val s = STRING "qq" : int_or_string
val n = NEITHER;
val n = NEITHER : int_or_string
val INT j = i;
val j = 3 : int
datatype int_list = EMPTY | INT_LIST of int * int_list;
datatype int_list = EMPTY | INT_LIST of int * int_list
(* Polymorphic datatype *)
datatype 'a pair = PAIR of 'a * 'a;
datatype 'a pair = PAIR of 'a * 'a
(* Lists *)
(* TODO
datatype 'a list = nil | :: of 'a * 'a list;
fun length nil = 0
| length (_::t) = 1 + length t;
length (1 :: 2 :: nil);
length nil;
*)
(* TODO
local
fun rev_helper (nil, ret) = ret
| rev_helper (h::t, ret) = rev_helper (t, h::ret)
in
fun rev L = rev_helper (L, nil)
end;
rev (false :: false :: true :: nil);
*)
(* Exceptions *)
(* TODO
exception StringException of string;
val e = StringException "example";
val StringException s = e;
*)
(* References *)
(*
val reference : int ref = ref 12;
val ref (twelve : int) = reference;
twelve;
!reference;
reference := !reference + 1;
twelve;
!reference;
*)
(* Equality types *)
datatype suit = HEARTS | CLUBS | DIAMONDS | SPADES;
datatype suit = CLUBS | DIAMONDS | HEARTS | SPADES
datatype int_pair = INT_PAIR of int * int;
datatype int_pair = INT_PAIR of int * int
datatype real_pair = REAL_PAIR of real * real;
datatype real_pair = REAL_PAIR of real * real
datatype 'a option = NONE | SOME of 'a;
datatype 'a option = NONE | SOME of 'a
val suit0 = HEARTS;
val suit0 = HEARTS : suit
val ip = INT_PAIR (1, 2);
val ip = INT_PAIR (1,2) : int_pair
val rp = REAL_PAIR (1.0, 2.0);
val rp = REAL_PAIR (1.0,2.0) : real_pair
val op0 = NONE;
val op0 = NONE : 'a option
val op1 = SOME true;
val op1 = SOME true : bool option
val op2 = SOME 3.0;
val op2 = SOME 3.0 : real option
val r = 5.0;
val r = 5.0 : real
suit0 = suit0;
val it = true : bool
ip = ip;
val it = true : bool
op0 = op0;
val it = true : bool
op1 = op1;
val it = true : bool
(* The following give error:
stdIn:1.2-1.11 Error: operator and operand don't agree [equality type required]
op2 = op2;
rp = rp;
r = r;
stdIn:35.1-35.10 Error: operator and operand don't agree [tycon mismatch]
op1 = op2;
*)
(* Miscellaneous *)
(* Formatting of long values *)
val ilist = [999,999,999,9999,999999,999999,999999,999,999,999,999,999,999,999];
val ilist = [999,999,999,9999,999999,999999,999999,999,999,999,999,999,...]
: int list
val ilist = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26];
val ilist = [1,2,3,4,5,6,7,8,9,10,11,12,...] : int list
(* Errors *)
(*) Should give 'Error: unbound variable or constructor: foo'
foo;
stdIn:5.1-5.4 Error: unbound variable or constructor: foo
raised at: stdIn:5.1-5.4
(*) Should give 'Error: unbound variable or constructor: foo'
5 + foo;
stdIn:3.5-3.8 Error: unbound variable or constructor: foo
raised at: stdIn:3.5-3.8
(*) Should give 'Error: unbound variable or constructor: foo'
(*) or 'Error: unbound variable or constructor: bar'
5 + bar + 6 + foo;
stdIn:4.5-4.8 Error: unbound variable or constructor: bar
raised at: stdIn:4.5-4.8
(* end script.sml *)