blob: 7cbd68ec290715df6079b65d693f3237fa8feb72 (
plain)
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
# class point :
int ->
object val mutable x : int method get_x : int method move : int -> unit end
# val p : point = <obj>
# - : int = 7
# - : unit = ()
# - : int = 10
# val q : point = <obj>
# - : int * int = (10, 17)
# class color_point :
int ->
string ->
object
val c : string
val mutable x : int
method color : string
method get_x : int
method move : int -> unit
end
# val p' : color_point = <obj>
# - : int * string = (5, "red")
# val l : point list = [<obj>; <obj>]
# val get_x : < get_x : 'a; .. > -> 'a = <fun>
# val set_x : < set_x : 'a; .. > -> 'a = <fun>
# - : int list = [10; 5]
# Characters 7-96:
......ref x_init = object
val mutable x = x_init
method get = x
method set y = x <- y
end..
Error: Some type variables are unbound in this type:
class ref :
'a ->
object
val mutable x : 'a
method get : 'a
method set : 'a -> unit
end
The method get has type 'a where 'a is unbound
# class ref :
int ->
object val mutable x : int method get : int method set : int -> unit end
# class ['a] ref :
'a -> object val mutable x : 'a method get : 'a method set : 'a -> unit end
# - : int = 2
# class ['a] circle :
'a ->
object
constraint 'a = < move : int -> unit; .. >
val mutable center : 'a
method center : 'a
method move : int -> unit
method set_center : 'a -> unit
end
# class ['a] circle :
'a ->
object
constraint 'a = #point
val mutable center : 'a
method center : 'a
method move : int -> unit
method set_center : 'a -> unit
end
# val c : point circle = <obj>
val c' : color_point circle = <obj>
# class ['a] color_circle :
'a ->
object
constraint 'a = #color_point
val mutable center : 'a
method center : 'a
method color : string
method move : int -> unit
method set_center : 'a -> unit
end
# Characters 28-29:
let c'' = new color_circle p;;
^
Error: This expression has type point but an expression was expected of type
#color_point
The first object type has no method color
# val c'' : color_point color_circle = <obj>
# - : color_point circle = <obj>
# Characters 0-21:
(c'' :> point circle);; (* Echec *)
^^^^^^^^^^^^^^^^^^^^^
Error: Type
color_point color_circle =
< center : color_point; color : string; move : int -> unit;
set_center : color_point -> unit >
is not a subtype of
point circle =
< center : point; move : int -> unit; set_center : point -> unit >
Type point is not a subtype of color_point
# Characters 9-55:
fun x -> (x : color_point color_circle :> point circle);;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: Type
color_point color_circle =
< center : color_point; color : string; move : int -> unit;
set_center : color_point -> unit >
is not a subtype of
point circle =
< center : point; move : int -> unit; set_center : point -> unit >
Type point is not a subtype of color_point
# class printable_point :
int ->
object
val mutable x : int
method get_x : int
method move : int -> unit
method print : unit
end
# val p : printable_point = <obj>
# 7- : unit = ()
# Characters 85-102:
inherit printable_point y as super
^^^^^^^^^^^^^^^^^
Warning 13: the following instance variables are overridden by the class printable_point :
x
The behaviour changed in ocaml 3.10 (previous behaviour was hiding.)
class printable_color_point :
int ->
string ->
object
val c : string
val mutable x : int
method color : string
method get_x : int
method move : int -> unit
method print : unit
end
# val p' : printable_color_point = <obj>
# (7, red)- : unit = ()
# class functional_point :
int ->
object ('a) val x : int method get_x : int method move : int -> 'a end
# val p : functional_point = <obj>
# - : int = 7
# - : int = 10
# - : int = 7
# - : #functional_point -> functional_point = <fun>
# class virtual ['a] lst :
unit ->
object
method virtual hd : 'a
method iter : ('a -> unit) -> unit
method map : ('a -> 'a) -> 'a lst
method virtual null : bool
method print : ('a -> unit) -> unit
method virtual tl : 'a lst
end
and ['a] nil :
unit ->
object
method hd : 'a
method iter : ('a -> unit) -> unit
method map : ('a -> 'a) -> 'a lst
method null : bool
method print : ('a -> unit) -> unit
method tl : 'a lst
end
and ['a] cons :
'a ->
'a lst ->
object
val h : 'a
val t : 'a lst
method hd : 'a
method iter : ('a -> unit) -> unit
method map : ('a -> 'a) -> 'a lst
method null : bool
method print : ('a -> unit) -> unit
method tl : 'a lst
end
# val l1 : int lst = <obj>
# (3::10::[])- : unit = ()
# val l2 : int lst = <obj>
# (4::11::[])- : unit = ()
# val map_list : ('a -> 'b) -> 'a lst -> 'b lst = <fun>
# val p1 : printable_color_point lst = <obj>
# ((3, red)::(10, red)::[])- : unit = ()
# class virtual comparable :
unit -> object ('a) method virtual cmp : 'a -> int end
# class int_comparable :
int -> object ('a) val x : int method cmp : 'a -> int method x : int end
# class int_comparable2 :
int ->
object ('a)
val x : int
val mutable x' : int
method cmp : 'a -> int
method set_x : int -> unit
method x : int
end
# class ['a] sorted_list :
unit ->
object
constraint 'a = #comparable
val mutable l : 'a list
method add : 'a -> unit
method hd : 'a
end
# val l : _#comparable sorted_list = <obj>
# val c : int_comparable = <obj>
# - : unit = ()
# val c2 : int_comparable2 = <obj>
# Characters 6-28:
l#add (c2 :> int_comparable);; (* Echec : 'a comp2 n'est un sous-type *)
^^^^^^^^^^^^^^^^^^^^^^
Error: Type
int_comparable2 =
< cmp : int_comparable2 -> int; set_x : int -> unit; x : int >
is not a subtype of
int_comparable = < cmp : int_comparable -> int; x : int >
Type int_comparable = < cmp : int_comparable -> int; x : int >
is not a subtype of
int_comparable2 =
< cmp : int_comparable2 -> int; set_x : int -> unit; x : int >
# - : unit = ()
# class int_comparable3 :
int ->
object
val mutable x : int
method cmp : int_comparable -> int
method setx : int -> unit
method x : int
end
# val c3 : int_comparable3 = <obj>
# - : unit = ()
# Characters 25-27:
(new sorted_list ())#add c3;; (* Error; strange message with -principal *)
^^
Error: This expression has type
int_comparable3 =
< cmp : int_comparable -> int; setx : int -> unit; x : int >
but an expression was expected of type
#comparable as 'a = < cmp : 'a -> int; .. >
Type int_comparable = < cmp : int_comparable -> int; x : int >
is not compatible with type
int_comparable3 =
< cmp : int_comparable -> int; setx : int -> unit; x : int >
The first object type has no method setx
# val sort : (#comparable as 'a) list -> 'a list = <fun>
# Characters 13-66:
List.map (fun c -> print_int c#x; print_string " ") l;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Warning 10: this expression should have type unit.
val pr : < x : int; .. > list -> unit = <fun>
# val l : int_comparable list = [<obj>; <obj>; <obj>]
# 5 2 4
- : unit = ()
# 2 4 5
- : unit = ()
# val l : int_comparable2 list = [<obj>; <obj>]
# 2 0
- : unit = ()
# 0 2
- : unit = ()
# val min : (#comparable as 'a) -> 'a -> 'a = <fun>
# - : int = 7
# - : int = 3
# class ['a] link :
'a ->
object ('b)
val mutable next : 'b option
val mutable x : 'a
method append : 'b option -> unit
method next : 'b option
method set_next : 'b option -> unit
method set_x : 'a -> unit
method x : 'a
end
# class ['a] double_link :
'a ->
object ('b)
val mutable next : 'b option
val mutable prev : 'b option
val mutable x : 'a
method append : 'b option -> unit
method next : 'b option
method prev : 'b option
method set_next : 'b option -> unit
method set_prev : 'b option -> unit
method set_x : 'a -> unit
method x : 'a
end
# val fold_right : ('a -> 'b -> 'b) -> 'a #link option -> 'b -> 'b = <fun>
# class calculator :
unit ->
object ('a)
val mutable acc : float
val mutable arg : float
val mutable equals : 'a -> float
method acc : float
method add : 'a
method arg : float
method enter : float -> 'a
method equals : float
method sub : 'a
end
# - : float = 5.
# - : float = 1.5
# - : float = 15.
# class calculator :
unit ->
object ('a)
val mutable acc : float
val mutable arg : float
val mutable equals : 'a -> float
method acc : float
method add : 'a
method arg : float
method enter : float -> 'a
method equals : float
method sub : 'a
end
# - : float = 5.
# - : float = 1.5
# - : float = 15.
# class calculator :
float ->
float ->
object
val acc : float
val arg : float
method add : calculator
method enter : float -> calculator
method equals : float
method sub : calculator
end
and calculator_add :
float ->
float ->
object
val acc : float
val arg : float
method add : calculator
method enter : float -> calculator
method equals : float
method sub : calculator
end
and calculator_sub :
float ->
float ->
object
val acc : float
val arg : float
method add : calculator
method enter : float -> calculator
method equals : float
method sub : calculator
end
# val calculator : calculator = <obj>
# - : float = 5.
# - : float = 1.5
# - : float = 15.
#
|