Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 539 lines (474 sloc) 20.175 kB
fccc685 Initial open-source release
MLstate authored
1 /*
b7f011a @BourgerieQuentin [enhance] stdlib, types: Just add stringifier for OpaType.ty
BourgerieQuentin authored
2 Copyright © 2011, 2012 MLstate
fccc685 Initial open-source release
MLstate authored
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20 * Manipulating types of OPA values
21 */
22
23 /* **************************************************************** */
24 /* **************************************************************** */
25 /* **************************************************************** */
26 /**
27 * {1 Module OpaTsc}
62c92c3 [doc] review: Review of stdlib.core
Guillem Rieu authored
28 *
fccc685 Initial open-source release
MLstate authored
29 * This module allow to get a type scheme from a name of a type
62c92c3 [doc] review: Review of stdlib.core
Guillem Rieu authored
30 *
fccc685 Initial open-source release
MLstate authored
31 * @category introspection
32 * @destination ??
33 * @author Quentin Bourgerie
34 * @stability almost stable
35 */
36
37 /**
38 * {1 Types defined for OpaTsc}
39 */
40
41 /**
42 * Type of type scheme
43 */
44 type OpaTsc.quantifier = {types:list(OpaType.typevar); rows:list(OpaType.rowvar); cols:list(OpaType.colvar)}
45 @opacapi
46 type OpaTsc.t = {quantifier : OpaTsc.quantifier; body : OpaType.ty}
47 type OpaTsc.instantiation = {types:list(OpaType.ty); rows:list(OpaType.row); cols:list(OpaType.col)}
48
b51ff66 @OpaOnWindowsNow [feature] stdlib.core.opatype: OpaTsc.get on client with unknown type
OpaOnWindowsNow authored
49
50 /* We do not have access in this package to slicing, so we use an updatable fallback for the OpaTsc.get function (client side only) */
51 OpaTsc_get = {{
52 get : string -> option(OpaTsc.t) = %%BslValue.Tsc.get%%
53 @both_implem fallback = @sliced_expr({client=some(Reference.create(get)) server=none})
54 update_fallback(f) = match fallback
55 {none} -> void
56 {some=ref} -> Reference.set(ref,f)
57 get_fallback(ident) = match fallback
58 {none} -> none
59 {some=ref} -> Reference.get(ref)(ident)
60 get_with_fallback(ident) = match OpaTsc_get.get(ident)
61 {none} -> OpaTsc_get.get_fallback(ident)
62 r -> r
63 final_get(ident) = @sliced_expr({client=get_with_fallback(ident) server=get(ident)})
64 }}
65
fccc685 Initial open-source release
MLstate authored
66 OpaTsc = {{
67 /**
68 * {2 Get type schemes}
69 */
70
d8cacd5 @OpaOnWindowsNow [feature] stdlib, types: Unfication of runtime types
OpaOnWindowsNow authored
71 @private @both_implem // TODO differentiate client and server var set
72 freshvar =
73 i = Reference.create(0)
74 get(str) =
75 v = Reference.get(i)
76 do Reference.update(i,_+1)
77 "`"^String.of_int(v)^"_"^str
78 get
79
80 @private freshtypevar(str)= {TyVar=freshvar(str)}
81 @private freshrowvar(str) = {TyRecord_row=[] TyRecord_rowvar=freshvar(str)}
82 @private freshcolvar(str)= {TySum_col=[] TySum_colvar=freshvar(str)}
83
84
fccc685 Initial open-source release
MLstate authored
85 /**
86 * Get a type scheme from a [name] if it exists, else return [none].
87 */
b51ff66 @OpaOnWindowsNow [feature] stdlib.core.opatype: OpaTsc.get on client with unknown type
OpaOnWindowsNow authored
88 get(ident:string) : option(OpaTsc.t) = OpaTsc_get.final_get(ident)
fccc685 Initial open-source release
MLstate authored
89
90 /**
91 * Get a type scheme from a [name] if exists, else make an error.
92 */
93 get_unsafe(name:string) : OpaTsc.t =
94 Option.lazy_default(-> error("OpaTsc.get_unsafe("^name^")"),
95 get(name))
96
97 instantiation_type_only(lt:list(OpaType.ty)) : OpaTsc.instantiation =
d8cacd5 @OpaOnWindowsNow [feature] stdlib, types: Unfication of runtime types
OpaOnWindowsNow authored
98 {types = lt rows = [] cols = []}
fccc685 Initial open-source release
MLstate authored
99
d8cacd5 @OpaOnWindowsNow [feature] stdlib, types: Unfication of runtime types
OpaOnWindowsNow authored
100 instantiation_quantifier({types=lt rows=lr cols=lc}:OpaTsc.quantifier): OpaTsc.instantiation =
101 {types=List.map(freshtypevar,lt) rows=List.map(freshrowvar,lr) cols=List.map(freshcolvar,lc)}
102
103 /**
fccc685 Initial open-source release
MLstate authored
104 * {2 Manipulation of type schemes}
105 */
106
107 /**
108 * Instantiate a type scheme with list of type.
109 * Assumes that list of type has the right length.
110 */
d8cacd5 @OpaOnWindowsNow [feature] stdlib, types: Unfication of runtime types
OpaOnWindowsNow authored
111 instantiate_body(body, (subst_var,subst_row,subst_col)) =
fccc685 Initial open-source release
MLstate authored
112 rec aux(t) =
113 match (t : OpaType.ty) with
114 | {TyConst = _} -> t
d8cacd5 @OpaOnWindowsNow [feature] stdlib, types: Unfication of runtime types
OpaOnWindowsNow authored
115 | {TyVar = var} -> subst_var(var) ? t
fccc685 Initial open-source release
MLstate authored
116 | {~TyArrow_params ~TyArrow_res} ->
117 {TyArrow_params = aux_list(TyArrow_params);
118 TyArrow_res = aux(TyArrow_res)}
119 | {~TyRecord_row} -> {TyRecord_row = aux_fields(TyRecord_row)}
120 | {~TyRecord_row ~TyRecord_rowvar} ->
121 fields = aux_fields(TyRecord_row)
d8cacd5 @OpaOnWindowsNow [feature] stdlib, types: Unfication of runtime types
OpaOnWindowsNow authored
122 row = subst_row(TyRecord_rowvar) ? {TyRecord_row=[]; ~TyRecord_rowvar}
fccc685 Initial open-source release
MLstate authored
123 OpaType.instantiate_row(fields,row)
124 | {~TySum_col} -> {TySum_col = aux_fields_list(TySum_col)}
125 | {~TySum_col ~TySum_colvar} ->
126 fieldss = aux_fields_list(TySum_col)
d8cacd5 @OpaOnWindowsNow [feature] stdlib, types: Unfication of runtime types
OpaOnWindowsNow authored
127 col = subst_col(TySum_colvar) ? {TySum_col=[]; ~TySum_colvar}
fccc685 Initial open-source release
MLstate authored
128 OpaType.instantiate_col(fieldss,col)
129 | {~TyName_args ~TyName_ident} ->
130 {TyName_args = aux_list(TyName_args);
131 TyName_ident = TyName_ident}
132 | {TyAbstract} -> t
133 | {~TyForall_quant ~TyForall_body} ->
134 //TODO : Remove quantifier if all instantiate in body??
135 {TyForall_quant = TyForall_quant;
136 TyForall_body = aux(TyForall_body)}
137 and aux_list(list) = List.map(aux, list)
138 and aux_fields(fields : list(OpaType.field)) = List.map(field ->
139 {label = field.label; ty = aux(field.ty)}, fields)
140 and aux_fields_list(l) = List.map(fields -> aux_fields(fields), l)
d8cacd5 @OpaOnWindowsNow [feature] stdlib, types: Unfication of runtime types
OpaOnWindowsNow authored
141 aux(body)
142
143 make_substitutions(inst:OpaTsc.instantiation, quantifier:OpaTsc.quantifier) =
144 add(vq,ty,subst) = [(vq,ty)|subst]
145 make_subst(vs,tys) =
146 subst = List.fold2(add,vs,tys,[])
147 get(v) = List.assoc_gen(String.equals,v,subst) // List.assoc cannot work here
148 // because it uses `==`
149 get
150 subst_var = make_subst(quantifier.types,inst.types):OpaType.typevar->option(OpaType.ty)
151 subst_row = make_subst(quantifier.rows,inst.rows):OpaType.rowvar->option(OpaType.row)
152 subst_col = make_subst(quantifier.cols,inst.cols):OpaType.colvar->option(OpaType.col)
153 (subst_var,subst_row,subst_col)
154
155 instantiate(inst:OpaTsc.instantiation, tsc:OpaTsc.t) =
fccc685 Initial open-source release
MLstate authored
156 match inst with /* when there is no instantiation to be done, we save ourself
157 * the traversal and reallocation of the type */
158 | {types=[]; rows=[]; cols=[]} -> tsc.body
d8cacd5 @OpaOnWindowsNow [feature] stdlib, types: Unfication of runtime types
OpaOnWindowsNow authored
159 | _ ->
160 instantiate_body(tsc.body, make_substitutions(inst,tsc.quantifier))
fccc685 Initial open-source release
MLstate authored
161
162 instantiate_type_only(inst:list(OpaType.ty), tsc:OpaTsc.t) =
163 instantiate(instantiation_type_only(inst), tsc)
164
d8cacd5 @OpaOnWindowsNow [feature] stdlib, types: Unfication of runtime types
OpaOnWindowsNow authored
165 instantiate_from_quantifier(quant,body)=
166 iquant = instantiation_quantifier(quant):OpaTsc.instantiation
167 (iquant,instantiate_body(body, make_substitutions(iquant,quant)))
168
fccc685 Initial open-source release
MLstate authored
169 /**
170 * [implementation(lt, tsc)] returns the implementation of type
171 * scheme [tsc] instantiated by the type list [lt].
172 */
173 implementation(lt : OpaTsc.instantiation, (tsc : OpaTsc.t)) =
d8cacd5 @OpaOnWindowsNow [feature] stdlib, types: Unfication of runtime types
OpaOnWindowsNow authored
174 OpaType.implementation(instantiate(lt, tsc))
fccc685 Initial open-source release
MLstate authored
175
176 }}
177
178 /* **************************************************************** */
179 /* **************************************************************** */
180 /* **************************************************************** */
181 /**
182 * {1 Module OpaType}
62c92c3 [doc] review: Review of stdlib.core
Guillem Rieu authored
183 *
fccc685 Initial open-source release
MLstate authored
184 * Module OpaType defines what is an OPA type and it offers some
185 * utils for manipulate this type.
62c92c3 [doc] review: Review of stdlib.core
Guillem Rieu authored
186 *
fccc685 Initial open-source release
MLstate authored
187 * @category introspection
188 * @destination ??
189 * @author Quentin Bourgerie
190 * @stability almost stable
191 */
192
193 /**
194 * {1 Interface for module OpaType}
195 */
fe16af9 @OpaOnWindowsNow [feature] stdlib,opabsl: removing all S3 occurences
OpaOnWindowsNow authored
196 /* disabled until module type specialisation on coercion works:
fccc685 Initial open-source release
MLstate authored
197 type OpaType.interface = {{
198 /* 1 - String representation */
199 to_string : OpaType.ty -> string
200 to_pretty : OpaType.ty -> string
201
202 /* 2 - Utilities functions */
203 type_of_field : OpaType.fields -> OpaType.Field.label -> option(OpaType.ty)
204 type_of_field_unsafe : OpaType.fields -> OpaType.Field.label -> OpaType.ty
205
206 instantiate : list(OpaType.ty) -> OpaTsc.t -> OpaType.ty
207
208 is_void : OpaType.ty -> bool
209
210 /* 3 - Module Field */
211 Field : {{
212 of_string : string -> OpaType.Field.label
213 to_string : OpaType.Field.label -> string
214 }}
215 }}
216 */
217
218 /**
219 * {1 Types defined for OpaType}
220 */
221 /**
222 * This type define the type of OPA value.
223 *
224 * Please note that, for the invariants on this datatype to be valid, they have to
225 * be satisfied by Pass_ExplicitInstantiation (for the initial creation of type) and by
226 * by every function in the stdlib (at type instantiation for instance).
62c92c3 [doc] review: Review of stdlib.core
Guillem Rieu authored
227 *
fccc685 Initial open-source release
MLstate authored
228 * BEWARE: for now, the INVARIANT is probably not satisfied because serialization
62c92c3 [doc] review: Review of stdlib.core
Guillem Rieu authored
229 * and unseralization of a type probably shuffles the order.
fccc685 Initial open-source release
MLstate authored
230 * Still, [unifiable] relies on the invariant for now
231 */
232 @opacapi
233 type OpaType.ty =
234 {TyConst : OpaType.ty_const} /
235 {TyVar : OpaType.typevar} /
236 {TyArrow_params : list(OpaType.ty); TyArrow_res : OpaType.ty} /
237 {TyName_args : list(OpaType.ty); TyName_ident : OpaType.ty_ident} /
238 {TyAbstract} /
239 {TyForall_quant : OpaTsc.quantifier; TyForall_body : OpaType.ty} /
240 {TyRecord_row : OpaType.fields} / /* INVARIANT: the lists are sorted by the field names lexixographically
241 * (and the shorter record is the smaller) */
242 {TyRecord_row : OpaType.fields; TyRecord_rowvar : OpaType.rowvar} / /* INVARIANT: same as above */
243 {TySum_col : list(OpaType.fields)} / /* INVARIANT: the records are sorted lexicographically based on the
244 * order on record defined above */
245 {TySum_col : list(OpaType.fields); TySum_colvar : OpaType.colvar} /* INVARIANT: same as above */
246
247 /* FIXME: use this type to define OpaType.ty instead of copy pasting its definition
248 * right now, when trying to do so, the typer stupidly complains that a part of the sum is not a record */
249 @opacapi
250 type OpaType.row =
d8cacd5 @OpaOnWindowsNow [feature] stdlib, types: Unfication of runtime types
OpaOnWindowsNow authored
251 // FIXME use an option for TyRecord_rowvar, and merge the two followings
fccc685 Initial open-source release
MLstate authored
252 {TyRecord_row : OpaType.fields} / /* INVARIANT: the lists are sorted by the field names lexixographically
253 * (and the shorter record is the smaller) */
254 {TyRecord_row : OpaType.fields; TyRecord_rowvar : OpaType.rowvar} /* INVARIANT: same as above */
255
256 /* FIXME: use this type to define OpaType.ty instead of copy pasting its definition
257 * right now, when trying to do so, the typer stupidly complains that a part of the sum is not a record */
258 @opacapi
259 type OpaType.col =
d8cacd5 @OpaOnWindowsNow [feature] stdlib, types: Unfication of runtime types
OpaOnWindowsNow authored
260 // FIXME use an option for TySum_colvar, and merge the two followings
fccc685 Initial open-source release
MLstate authored
261 {TySum_col : list(OpaType.fields)} / /* INVARIANT: the records are sorted lexicographically based on the
262 * order on record defined above */
263 {TySum_col : list(OpaType.fields); TySum_colvar : OpaType.colvar} /* INVARIANT: same as above */
264
265
266 /**
267 * Type of simple value.
268 */
269 type OpaType.ty_const =
270 {TyInt} /
271 {TyFloat} /
272 {TyString}
273
274 opa_type_const_eq(a : OpaType.ty_const, b : OpaType.ty_const) =
275 match (a, b) with
276 | ({TyInt},{TyInt})
277 | ({TyFloat},{TyFloat})
278 | ({TyString}, {TyString}) -> true
279 | _ -> false
280 /**
62c92c3 [doc] review: Review of stdlib.core
Guillem Rieu authored
281 * Below we have lot of sub types for OpaType.ty.
282 *
283 * Maybe we can simplify this? Are all these types useful?
fccc685 Initial open-source release
MLstate authored
284 * -- perhaps more readable and we may have to change it later from string
62c92c3 [doc] review: Review of stdlib.core
Guillem Rieu authored
285 * to something more complex, similar to QmlAst
fccc685 Initial open-source release
MLstate authored
286 */
287 type OpaType.field = {label : OpaType.Field.label; ty : OpaType.ty}
288 type OpaType.fields = list(OpaType.field)
289 @opacapi
290 type OpaType.typevar = string
291 type OpaType.colvar = string
292 type OpaType.rowvar = string
293 type OpaType.ty_ident = string
294
295 /**
296 * Type of a label of a field of a record.
297 */
298 type OpaType.Field.label = string
299
300
301 OpaType = {{
302
303 /**
304 * {2 String representation}
305 */
306
307 /**
308 * Returns a string representation of [ty].
309 */
310 to_string(ty) =
311 rec aux_ty_const(ty_const : OpaType.ty_const) =
312 match ty_const with
313 | {TyInt} -> "TyInt"
314 | {TyFloat} -> "TyFloat"
315 | {TyString} -> "TyString"
316 and aux_list(list) =
317 List.fold(
318 (ty, acc -> acc ^ "{aux(ty)}; "),
319 list, "[") ^ "]"
320 and aux_fields(fields) =
321 List.fold(
322 (field, acc -> acc ^ "{field.label} : {aux(field.ty)}; "), //sugar
323 fields, "[") ^ "]"
324 and aux_fields_list(lfields) =
325 List.fold((item, acc -> acc ^ aux_fields(item) ^ "; "),
326 lfields, "[") ^ "]"
327 and aux(ty : OpaType.ty) =
328 match ty with
329 | {~TyConst} -> "\{TyConst = {aux_ty_const(TyConst)}}"
330 | {~TyVar} -> "\{TyVar = {TyVar}}"
331 | {~TyArrow_params ~TyArrow_res} -> "\{TyArrow_params = {aux_list(TyArrow_params)}; TyArrow_res = {aux(TyArrow_res)}}"
332 | {~TyRecord_row} -> "\{TyRecord_row = {aux_fields(TyRecord_row)}}"
333 | {~TyRecord_row ~TyRecord_rowvar} -> "\{TyRecord_row = {aux_fields(TyRecord_row)}; TyRecord_rowvar = {TyRecord_rowvar}}"
334 | {~TySum_col} -> "\{TySum_col = {aux_fields_list(TySum_col)}}"
335 | {~TySum_col ~TySum_colvar} -> "\{TySum_col = {aux_fields_list(TySum_col)}; TySum_colvar = {TySum_colvar}}"
336 | {~TyName_args ~TyName_ident} -> "\{TyName_args = {aux_list(TyName_args)}; TyName_ident = {TyName_ident}}"
337 | {TyAbstract} -> "\{TyAbstract}"
338 | {~TyForall_quant ~TyForall_body} ->
339
340 "\{TyForall_quant = {aux_quant(TyForall_quant)}; TyForall_body = {aux(TyForall_body)}}"
341 and aux_quant(~{types rows cols}:OpaTsc.quantifier) =
342 "vars:" ^ List.to_string(types) ^ ",rows:" ^ List.to_string(rows) ^ ",cols:" ^ List.to_string(cols)
343 aux(ty)
344
345 to_pretty_fields(fields : list(OpaType.field)) =
346 List.fold((field, (acc, pre) ->
347 (acc ^ pre ^ Field.to_string(field.label) ^ " : " ^ to_pretty(field.ty), "; ")),
348 fields, ("", "")).f1
349
350 to_pretty_lfields(lfields) =
351 List.fold((field, (acc, pre) ->
352 (acc ^ pre ^ "\{" ^ to_pretty_fields(field) ^ "}", " / ")),
353 lfields, ("", "")).f1
354
355 /**
356 * Returns a pretty string representation of [ty].
357 * do not use magic_to_string and other magic functions, or we get a cycle
358 */
b7f011a @BourgerieQuentin [enhance] stdlib, types: Just add stringifier for OpaType.ty
BourgerieQuentin authored
359 @stringifier(OpaType.ty) to_pretty(ty : OpaType.ty)=
fccc685 Initial open-source release
MLstate authored
360 rec aux_ty_const(ty_const : OpaType.ty_const) =
361 match ty_const with
362 | {TyInt} -> "int"
363 | {TyFloat} -> "float"
364 | {TyString} -> "string"
365 //TODO: review and simplify the 2 functions below
366 and aux_list(list) =
367 elems = List.fold((ty, (acc, pre) ->
368 (acc ^ pre ^ aux(ty), " ,")),
369 list, ("", "")).f1
370 if List.is_empty(list) then "" else "(" ^ elems ^ ")"
371 and aux(ty : OpaType.ty) =
372 match ty with
373 | {TyConst = x} -> aux_ty_const(x)
374 | {TyVar = var } -> var
375 | {~TyArrow_params ~TyArrow_res} ->
376 aux_list(TyArrow_params) ^ " -> " ^ aux(TyArrow_res)
377 | {~TyRecord_row} -> "\{" ^ to_pretty_fields(TyRecord_row) ^ "}"
378 | {~TyRecord_row TyRecord_rowvar = _} ->
379 "\{" ^ to_pretty_fields(TyRecord_row) ^ "; ...}"
380 | {~TySum_col} -> to_pretty_lfields(TySum_col)
381 | {~TySum_col TySum_colvar = _} -> to_pretty_lfields(TySum_col) ^ " / __"
382 | {~TyName_args ~TyName_ident} -> TyName_ident ^ aux_list(TyName_args)
383 | {TyAbstract} -> "abstract"
384 | {~TyForall_quant ~TyForall_body} ->
385 "forall(" ^ aux_quant(TyForall_quant) ^ ")." ^ aux(TyForall_body)
386 and aux_quant(~{types rows cols}:OpaTsc.quantifier) =
387 match (rows, cols) with
388 | ([], []) -> List.to_string(types)
389 | _ -> List.to_string(types) ^ ",rows:" ^ List.to_string(rows) ^ ",cols:" ^ List.to_string(cols)
390 end
391 aux(ty)
392
393 /**
394 * {2 Utilities functions}
395 */
396
397 /**
398 * Get the type of field [name] in fields.
399 * If field [name] doesn't exists returns [none], else [some] with
400 * a type.
401 */
402 type_of_field(fields:OpaType.fields, field_label:OpaType.Field.label) =
403 Option.map(c -> c.ty,
404 List.find((field2 ->
405 String.equals(field_label, field2.label)), fields))
406
407
408 /**
409 * Unsafe type_of_field. Make an error if field [name] doesn't
410 * exists else return type of this field.
411 */
412 type_of_field_unsafe(fields, name) =
413 Option.lazy_default(-> error("[type_of_field_unsafe] type \{" ^ String.of_list(x -> x.label, " ", fields) ^ "} doesn't offer any field named " ^ name),
414 type_of_field(fields, name))
415
416 /**
417 * Returns encapsulated type of a named type
418 */
419 type_of_name(ident, (args : list(OpaType.ty))) =
420 OpaTsc.instantiate_type_only(args, OpaTsc.get_unsafe(ident))
421
422 /**
b746c3d @fpessaux [cleanup] Opa TyPrivate: Removed TyPrivate stuff in stdlib (OPA- 666).
fpessaux authored
423 * Get implementation of the given type. i.e. traverse [TyName].
fccc685 Initial open-source release
MLstate authored
424 *
425 * Note : If that should be returns {TyAbstract} instead returns the
426 * named type that encapsulate {TyAbstract} (if it possible...)
427 */
428 implementation(ty : OpaType.ty) =
429 rec aux(ty : OpaType.ty) =
430 match ty with
431 | {TyName_ident = ident; TyName_args = args} ->
432 match type_of_name(ident, args) with
433 | {TyAbstract} -> ty
434 | ty -> aux(ty)
435 end
436 | _ -> ty
437 aux(ty)
438
439 /**
440 * Get the fields type from a [value] and a list of fields
441 * [lfields]. [lfields] must be ordered else cause a runtime
442 * error. Usefull for retrieve type of a record when [@typeof] return
443 * a type sum.
62c92c3 [doc] review: Review of stdlib.core
Guillem Rieu authored
444 *
fccc685 Initial open-source release
MLstate authored
445 * @return (fields corresponding to the value, position on [lfields])
446 *
447 */
448 fields_of_fields_list(value, (lfields : list(OpaType.fields))) =
449 /* Attribute a number to candidates*/
450 (_, lfields, candidates) = List.fold(
451 (fields, (i, lfields, candidates) ->
452 (i+1, fields +> lfields, (fields, i) +> candidates)
453 ), lfields, (0, [],[]))
454 /* Filter candidates with fields names of record */
455 (u, c) = OpaValue.Record.fold(
456 (field, _, (unconstructed, candidates) ->
457 name = OpaValue.Record.name_of_field_unsafe(field)
458 rec aux((u : list), c, (uacc, cacc)) =
459 match u with
460 | [] -> (uacc, cacc)
461 | [fields | tlfields] ->
462 match fields with
463 | [] -> aux(tlfields, List.tail(c), (uacc, cacc))
464 | [hd | tl] ->
465 if String.equals(hd.label, name) then
466 aux(tlfields, List.tail(c), (tl +> uacc, List.head(c) +> cacc))
467 else aux(tlfields, List.tail(c), (uacc, cacc))
468 aux(unconstructed, candidates, ([], []))
469 ), value, (lfields, candidates))
470 /* Remove candidates that contains too mush fields */
471 rec aux((u : list), (c : list), (acc : list)) =
472 match u with
473 | [] -> acc
474 | [hd | tl] ->
475 match hd with
476 | [] -> aux(tl, List.tail(c), List.head(c) +> acc)
477 | _ -> aux(tl, List.tail(c), acc)
478 /* Check if we have only one candidates */
479 match aux(u, c, []) with
480 | [ res | []] -> res
481 | _ -> error("[OpaType.fields_of_fields_list] Can't select a row for :\n" ^
482 "value (dump): "^Debug.dump(value) ^
483 "list fields : "^to_pretty_lfields(lfields))
484
485 /**
486 * Return [true] if the given type represents a [void] value.
487 */
488 is_void =
489 | {TyName_ident = "void"; TyName_args = _}
490 | {TyRecord_row = []}
491 | {TyRecord_row = []; TyRecord_rowvar = _}
492 | {TySum_col = [[]]}
493 | {TySum_col = [[]]; TySum_colvar = _} -> true
494 | {TyName_args = args; TyName_ident = ident} -> is_void(type_of_name(ident, args))
495 | _ -> false
496
497 /**
498 * {2 Module OpaType.Field}
62c92c3 [doc] review: Review of stdlib.core
Guillem Rieu authored
499 *
fccc685 Initial open-source release
MLstate authored
500 * This module provides some functions for manipulate value of
501 * type [OpaType.Field.label]. This field in [OpaType] is not the
502 * same that the field in [OpaValue].
503 */
504 Field = {{
505
506 /** Get the field from a string. */
507 of_string(str) = str
508
509 /** Get the name of given field. */
510 to_string(field) = field
511
512 }}
513
514 compare_field(h1:OpaType.field, h2:OpaType.field) : Order.comparison = String.compare(h1.label,h2.label)
515 compare_record(l1:OpaType.fields,l2:OpaType.fields) = List.compare(compare_field,l1,l2)
516
517 merge_fields(fields1:OpaType.fields, fields2:OpaType.fields) : OpaType.fields =
518 List.merge_with_comparison(compare_field,fields1,fields2)
519 merge_fieldss(fieldss1:list(OpaType.fields), fieldss2:list(OpaType.fields)) : list(OpaType.fields) =
520 List.merge_with_comparison(compare_record,fieldss1,fieldss2)
521
522 instantiate_row(fields1,tyrow) =
523 match tyrow with
524 | {TyRecord_row = fields2; TyRecord_rowvar = var} ->
525 {TyRecord_row = merge_fields(fields1,fields2); TyRecord_rowvar = var}
526 | {TyRecord_row = fields2} ->
527 {TyRecord_row = merge_fields(fields1,fields2)}
528 instantiate_col(fieldss1,tycol) =
529 match tycol with
530 | {TySum_col = fieldss2; TySum_colvar = var} ->
531 {TySum_col = merge_fieldss(fieldss1,fieldss2); TySum_colvar = var}
532 | {TySum_col = fieldss2} ->
533 {TySum_col = merge_fieldss(fieldss1,fieldss2)}
534
fe16af9 @OpaOnWindowsNow [feature] stdlib,opabsl: removing all S3 occurences
OpaOnWindowsNow authored
535 }} /* disabled : OpaType.interface */
fccc685 Initial open-source release
MLstate authored
536
537 @opacapi OpaType_instantiate_row = OpaType.instantiate_row
538 @opacapi OpaType_instantiate_col = OpaType.instantiate_col
Something went wrong with that request. Please try again.