Debian Codegraph @

expander/ppx_compare_expander.ml

raw symbols SCIP
1 (* Generated code should depend on the environment in scope as little as possible.
2 E.g. rather than [foo = []] do [match foo with [] ->], to eliminate the use of [=]. It
3 is especially important to not use polymorphic comparisons, since we are moving more
4 and more to code that doesn't have them in scope. *)
5
6 (* Note: I am introducing a few unnecessary explicit closures, (not all of them some are
7 unnecessary due to the value restriction).
8 *)
9
10 open Base
11 open Ppxlib
12 open Ast_builder.Default
13 include Ppx_compare_expander_intf
14
15 (* Two-argument function, possibly with [local_] arguments *)
16 let ptyp_arrow2 ~loc ~local_args arg1 arg2 res =
17 if local_args
18 then [%type: [%t arg1] -> [%t arg2] -> [%t res]]
19 else [%type: [%t arg1] -> [%t arg2] -> [%t res]]
20 ;;
21
22 type kind =
23 | Compare
24 | Equal
25
26 module type Params = sig
27 val name : string
28 val kind : kind
29 val chain : expression -> expression -> expression
30 val const : loc:Location.t -> Ordering.t -> expression
31 val result_type : loc:Location.t -> core_type
32 val poly : loc:Location.t -> expression -> expression -> expression
33
34 val abstract
35 : loc:Location.t
36 -> type_name:string
37 -> expression
38 -> expression
39 -> expression
40
41 module Attrs : Attrs
42 end
43
44 module Make_attrs (Name : sig
45 val name : string
46 end) : Attrs = struct
47 let ignore_label_declaration =
48 Attribute.declare
49 (Name.name ^ ".ignore")
50 Attribute.Context.label_declaration
51 Ast_pattern.(pstr nil)
52 ()
53 ;;
54
55 let ignore_core_type =
56 Attribute.declare
57 (Name.name ^ ".ignore")
58 Attribute.Context.core_type
59 Ast_pattern.(pstr nil)
60 ()
61 ;;
62 end
63
64 module Compare_params : Params = struct
65 let name = "compare"
66 let kind = Compare
67
68 let chain a b =
69 let loc = a.pexp_loc in
70 [%expr
71 match [%e a] with
72 | 0 -> [%e b]
73 | n -> n]
74 ;;
75
76 let const ~loc (ord : Ordering.t) =
77 eint
78 ~loc
79 (match ord with
80 | Less -> -1
81 | Equal -> 0
82 | Greater -> 1)
83 ;;
84
85 let result_type ~loc = [%type: int]
86 let poly ~loc a b = [%expr Stdlib.compare [%e a] [%e b]]
87
88 let abstract ~loc ~type_name a b =
89 [%expr
90 Ppx_compare_lib.compare_abstract
91 ~type_name:[%e estring ~loc type_name]
92 [%e a]
93 [%e b]]
94 ;;
95
96 module Attrs = Make_attrs (struct
97 let name = name
98 end)
99 end
100
101 module Equal_params : Params = struct
102 let name = "equal"
103 let kind = Equal
104
105 let chain a b =
106 let loc = a.pexp_loc in
107 [%expr Stdlib.( && ) [%e a] [%e b]]
108 ;;
109
110 let const ~loc (ord : Ordering.t) =
111 match ord with
112 | Equal -> [%expr true]
113 | Less | Greater -> [%expr false]
114 ;;
115
116 let result_type ~loc = [%type: bool]
117 let poly ~loc a b = [%expr Stdlib.( = ) [%e a] [%e b]]
118
119 let abstract ~loc ~type_name a b =
120 [%expr
121 Ppx_compare_lib.equal_abstract ~type_name:[%e estring ~loc type_name] [%e a] [%e b]]
122 ;;
123
124 module Attrs = Make_attrs (struct
125 let name = name
126 end)
127 end
128
129 module Make (Params : Params) = struct
130 open Params
131 module Attrs = Attrs
132
133 let str_attributes =
134 [ Attribute.T Attrs.ignore_label_declaration; Attribute.T Attrs.ignore_core_type ]
135 ;;
136
137 let is_ignored_gen ~loc ~compare_attr ~equal_attr ast =
138 match kind, Attribute.get compare_attr ast, Attribute.get equal_attr ast with
139 | _, Some (), Some () | Compare, Some (), None | Equal, None, Some () -> true
140 | _, None, None -> false
141 | Compare, None, Some () ->
142 Location.raise_errorf
143 ~loc
144 "Cannot use [@@equal.ignore] with [@@@@deriving compare]."
145 | Equal, Some (), None ->
146 Location.raise_errorf ~loc "Cannot use [@@compare.ignore] with [@@@@deriving equal]"
147 ;;
148
149 let core_type_is_ignored ty =
150 is_ignored_gen
151 ~loc:ty.ptyp_loc
152 ~compare_attr:Compare_params.Attrs.ignore_core_type
153 ~equal_attr:Equal_params.Attrs.ignore_core_type
154 ty
155 ;;
156
157 let label_is_ignored ld =
158 is_ignored_gen
159 ~loc:ld.pld_loc
160 ~compare_attr:Compare_params.Attrs.ignore_label_declaration
161 ~equal_attr:Equal_params.Attrs.ignore_label_declaration
162 ld
163 ;;
164
165 let with_tuple loc ~value ~ltys f =
166 (* generate
167 let id_1, id_2, id_3, ... id_n = value in expr
168 where expr is the result of (f [id_1, ty_1 ; id_2, ty_2; ...])
169 *)
170 let names_types =
171 List.map ltys ~f:(fun (lbl, t) -> gen_symbol ~prefix:"t" (), lbl, t)
172 in
173 let pattern =
174 let l = List.map names_types ~f:(fun (n, lbl, _) -> lbl, pvar ~loc n) in
175 Ppxlib_jane.Jane_syntax.Pattern.pat_of ~loc ~attrs:[] (Jpat_tuple (l, Closed))
176 in
177 let e = f (List.map names_types ~f:(fun (n, _, t) -> evar ~loc n, t)) in
178 let binding = value_binding ~loc ~pat:pattern ~expr:value in
179 pexp_let ~loc Nonrecursive [ binding ] e
180 ;;
181
182 let phys_equal_first a b cmp =
183 let loc = cmp.pexp_loc in
184 [%expr if Stdlib.( == ) [%e a] [%e b] then [%e const ~loc Equal] else [%e cmp]]
185 ;;
186
187 let rec chain_if ~loc = function
188 | [] -> const ~loc Equal
189 | [ x ] -> x
190 | x :: xs -> chain x (chain_if ~loc:x.pexp_loc xs)
191 ;;
192
193 let tp_name n = Printf.sprintf "_cmp__%s" n
194
195 let type_ ~with_local ~hide ~loc ty =
196 let loc = { loc with loc_ghost = true } in
197 let ptyp_attributes =
198 if hide
199 then Merlin_helpers.hide_attribute :: ty.ptyp_attributes
200 else ty.ptyp_attributes
201 in
202 let hty = { ty with ptyp_attributes } in
203 ptyp_arrow2 ~loc ~local_args:with_local ty hty (result_type ~loc)
204 ;;
205
206 let function_name ~with_local typename =
207 let name =
208 match typename with
209 | "t" -> name
210 | s -> name ^ "_" ^ s
211 in
212 if with_local then name ^ "__local" else name
213 ;;
214
215 let compare_ignore ~loc value1 value2 =
216 [%expr
217 let (_ : _) = [%e value1]
218 and (_ : _) = [%e value2] in
219 [%e const ~loc Equal]]
220 ;;
221
222 let rec compare_applied ~hide ~with_local ~constructor ~args value1 value2 =
223 let args =
224 List.map args ~f:(compare_of_ty_fun ~hide ~with_local ~type_constraint:false)
225 @ [ value1; value2 ]
226 in
227 type_constr_conv
228 ~loc:(Located.loc constructor)
229 constructor
230 args
231 ~f:(function_name ~with_local)
232
233 and compare_of_tuple ~hide ~with_local loc ltys value1 value2 =
234 with_tuple loc ~value:value1 ~ltys (fun elems1 ->
235 with_tuple loc ~value:value2 ~ltys (fun elems2 ->
236 let exprs =
237 List.map2_exn elems1 elems2 ~f:(fun (v1, t) (v2, _) ->
238 compare_of_ty ~hide ~with_local t v1 v2)
239 in
240 chain_if ~loc exprs))
241
242 and compare_variant ~hide ~with_local loc row_fields value1 value2 =
243 let map row =
244 match row.prf_desc with
245 | Rtag ({ txt = cnstr; _ }, true, _) | Rtag ({ txt = cnstr; _ }, _, []) ->
246 case
247 ~guard:None
248 ~lhs:
249 (ppat_tuple
250 ~loc
251 [ ppat_variant ~loc cnstr None; ppat_variant ~loc cnstr None ])
252 ~rhs:(const ~loc Equal)
253 | Rtag ({ txt = cnstr; _ }, false, tp :: _) ->
254 let v1 = gen_symbol ~prefix:"_left" ()
255 and v2 = gen_symbol ~prefix:"_right" () in
256 let body = compare_of_ty ~hide ~with_local tp (evar ~loc v1) (evar ~loc v2) in
257 case
258 ~guard:None
259 ~lhs:
260 (ppat_tuple
261 ~loc
262 [ ppat_variant ~loc cnstr (Some (pvar ~loc v1))
263 ; ppat_variant ~loc cnstr (Some (pvar ~loc v2))
264 ])
265 ~rhs:body
266 | Rinherit { ptyp_desc = Ptyp_constr (id, args); _ } ->
267 (* quite sadly, this code doesn't handle:
268 type 'a id = 'a with compare
269 type t = [ `a | [ `b ] id ] with compare
270 because it will generate a pattern #id, when id is not even a polymorphic
271 variant in the first place.
272 The culprit is caml though, since it only allows #id but not #([`b] id)
273 *)
274 let v1 = gen_symbol ~prefix:"_left" ()
275 and v2 = gen_symbol ~prefix:"_right" () in
276 case
277 ~guard:None
278 ~lhs:
279 (ppat_tuple
280 ~loc
281 [ ppat_alias ~loc (ppat_type ~loc id) (Located.mk ~loc v1)
282 ; ppat_alias ~loc (ppat_type ~loc id) (Located.mk ~loc v2)
283 ])
284 ~rhs:
285 (compare_applied
286 ~hide
287 ~with_local
288 ~constructor:id
289 ~args
290 (evar ~loc v1)
291 (evar ~loc v2))
292 | Rinherit ty ->
293 Location.raise_errorf ~loc:ty.ptyp_loc "Ppx_compare.compare_variant: unknown type"
294 in
295 let e =
296 let matched = pexp_tuple ~loc [ value1; value2 ] in
297 match List.map ~f:map row_fields with
298 | [ v ] -> pexp_match ~loc matched [ v ]
299 | l ->
300 pexp_match
301 ~loc
302 matched
303 (l
304 @ (* Providing we didn't screw up badly we now know that the tags of the variants
305 are different. We let pervasive do its magic. *)
306 [ case ~guard:None ~lhs:[%pat? x, y] ~rhs:(poly ~loc [%expr x] [%expr y]) ])
307 in
308 phys_equal_first value1 value2 e
309
310 and branches_of_sum ~hide ~with_local cds =
311 let rightmost_index = List.length cds - 1 in
312 List.concat
313 (List.mapi cds ~f:(fun i cd ->
314 let rightmost = i = rightmost_index in
315 let loc = cd.pcd_loc in
316 if Option.is_some cd.pcd_res
317 then
318 (* If we get GADTs support, fix the constant sum type optimization for them *)
319 Location.raise_errorf ~loc "GADTs are not supported by comparelib";
320 match cd.pcd_args with
321 | Pcstr_record lds ->
322 let value1 = gen_symbol ~prefix:"_a" () in
323 let value2 = gen_symbol ~prefix:"_b" () in
324 let res =
325 case
326 ~guard:None
327 ~lhs:
328 (ppat_tuple
329 ~loc
330 [ pconstruct cd (Some (pvar ~loc value1))
331 ; pconstruct cd (Some (pvar ~loc value2))
332 ])
333 ~rhs:
334 (compare_of_record_no_phys_equal
335 ~hide
336 ~with_local
337 loc
338 lds
339 (evar ~loc value1)
340 (evar ~loc value2))
341 in
342 if rightmost
343 then [ res ]
344 else (
345 let pany = ppat_any ~loc in
346 let pcnstr = pconstruct cd (Some pany) in
347 let case l r ord =
348 case ~guard:None ~lhs:(ppat_tuple ~loc [ l; r ]) ~rhs:(const ~loc ord)
349 in
350 [ res; case pcnstr pany Less; case pany pcnstr Greater ])
351 | Pcstr_tuple pcd_args ->
352 (match pcd_args with
353 | [] ->
354 let pcnstr = pconstruct cd None in
355 let pany = ppat_any ~loc in
356 let case l r ord =
357 case ~guard:None ~lhs:(ppat_tuple ~loc [ l; r ]) ~rhs:(const ~loc ord)
358 in
359 if rightmost
360 then [ case pcnstr pcnstr Equal ]
361 else
362 [ case pcnstr pcnstr Equal
363 ; case pcnstr pany Less
364 ; case pany pcnstr Greater
365 ]
366 | tps ->
367 let ids_ty =
368 List.map tps ~f:(fun ty ->
369 let a = gen_symbol ~prefix:"_a" () in
370 let b = gen_symbol ~prefix:"_b" () in
371 a, b, ty)
372 in
373 let lpatt =
374 List.map ids_ty ~f:(fun (l, _r, _ty) -> pvar ~loc l) |> ppat_tuple ~loc
375 and rpatt =
376 List.map ids_ty ~f:(fun (_l, r, _ty) -> pvar ~loc r) |> ppat_tuple ~loc
377 and body =
378 List.map ids_ty ~f:(fun (l, r, ty) ->
379 compare_of_ty ~hide ~with_local ty (evar ~loc l) (evar ~loc r))
380 |> chain_if ~loc
381 in
382 let res =
383 case
384 ~guard:None
385 ~lhs:
386 (ppat_tuple
387 ~loc
388 [ pconstruct cd (Some lpatt); pconstruct cd (Some rpatt) ])
389 ~rhs:body
390 in
391 if rightmost
392 then [ res ]
393 else (
394 let pany = ppat_any ~loc in
395 let pcnstr = pconstruct cd (Some pany) in
396 let case l r ord =
397 case ~guard:None ~lhs:(ppat_tuple ~loc [ l; r ]) ~rhs:(const ~loc ord)
398 in
399 [ res; case pcnstr pany Less; case pany pcnstr Greater ]))))
400
401 and compare_sum ~hide ~with_local loc cds value1 value2 =
402 let is_sum_type_with_all_constant_constructors =
403 List.for_all cds ~f:(fun cd ->
404 Option.is_none cd.pcd_res
405 &&
406 (* we could support GADTs, but the general case
407 doesn't, so let's hold off *)
408 match cd.pcd_args with
409 | Pcstr_tuple l -> List.is_empty l
410 | Pcstr_record l -> List.is_empty l)
411 in
412 if is_sum_type_with_all_constant_constructors
413 then
414 (* the compiler will optimize the polymorphic comparison to an integer one *)
415 poly ~loc value1 value2
416 else (
417 let mcs = branches_of_sum ~hide ~with_local cds in
418 let e = pexp_match ~loc (pexp_tuple ~loc [ value1; value2 ]) mcs in
419 phys_equal_first value1 value2 e)
420
421 and compare_of_ty ~hide ~with_local ty value1 value2 =
422 let loc = ty.ptyp_loc in
423 if core_type_is_ignored ty
424 then compare_ignore ~loc value1 value2
425 else (
426 match Ppxlib_jane.Jane_syntax.Core_type.of_ast ty with
427 | Some (Jtyp_tuple ltps, _attrs) ->
428 compare_of_tuple ~hide ~with_local loc ltps value1 value2
429 | Some (Jtyp_layout _, _) ->
430 Location.raise_errorf
431 ~loc
432 "Layout annotations are not currently supported with [ppx_compare]."
433 | None ->
434 (match ty.ptyp_desc with
435 | Ptyp_constr (constructor, args) ->
436 compare_applied ~hide ~with_local ~constructor ~args value1 value2
437 | Ptyp_tuple tys ->
438 compare_of_tuple
439 ~hide
440 ~with_local
441 loc
442 (List.map ~f:(fun ty -> None, ty) tys)
443 value1
444 value2
445 | Ptyp_var name -> eapply ~loc (evar ~loc (tp_name name)) [ value1; value2 ]
446 | Ptyp_arrow _ ->
447 Location.raise_errorf ~loc "ppx_compare: Functions can not be compared."
448 | Ptyp_variant (row_fields, Closed, None) ->
449 compare_variant ~hide ~with_local loc row_fields value1 value2
450 | Ptyp_any -> compare_ignore ~loc value1 value2
451 | _ -> Location.raise_errorf ~loc "ppx_compare: unknown type"))
452
453 and compare_of_ty_fun ~hide ~with_local ~type_constraint ty =
454 let loc = { ty.ptyp_loc with loc_ghost = true } in
455 let do_hide hide_fun x = if hide then hide_fun x else x in
456 let a = gen_symbol ~prefix:"a" () in
457 let b = gen_symbol ~prefix:"b" () in
458 let e_a = evar ~loc a in
459 let e_b = evar ~loc b in
460 let mk_pat x =
461 if type_constraint then ppat_constraint ~loc (pvar ~loc x) ty else pvar ~loc x
462 in
463 let body =
464 do_hide Merlin_helpers.hide_expression (compare_of_ty ~hide ~with_local ty e_a e_b)
465 in
466 eta_reduce_if_possible
467 [%expr
468 fun [%p mk_pat a] [%p do_hide Merlin_helpers.hide_pattern (mk_pat b)] -> [%e body]]
469
470 and compare_of_record_no_phys_equal ~hide ~with_local loc lds value1 value2 =
471 let is_evar = function
472 | { pexp_desc = Pexp_ident _; _ } -> true
473 | _ -> false
474 in
475 assert (is_evar value1);
476 assert (is_evar value2);
477 List.filter lds ~f:(fun ld -> not (label_is_ignored ld))
478 |> List.map ~f:(fun ld ->
479 let loc = ld.pld_loc in
480 let label = Located.map lident ld.pld_name in
481 compare_of_ty
482 ~hide
483 ~with_local
484 ld.pld_type
485 (pexp_field ~loc value1 label)
486 (pexp_field ~loc value2 label))
487 |> chain_if ~loc
488 ;;
489
490 let compare_of_record ~hide ~with_local loc lds value1 value2 =
491 compare_of_record_no_phys_equal ~hide ~with_local loc lds value1 value2
492 |> phys_equal_first value1 value2
493 ;;
494
495 let compare_abstract loc type_name v_a v_b = abstract ~loc ~type_name v_a v_b
496
497 let scheme_of_td ~hide ~with_local td =
498 let loc = td.ptype_loc in
499 let type_ = combinator_type_of_type_declaration td ~f:(type_ ~hide ~with_local) in
500 match td.ptype_params with
501 | [] -> type_
502 | l ->
503 let vars = List.map l ~f:get_type_param_name in
504 ptyp_poly ~loc vars type_
505 ;;
506
507 let compare_of_td ~hide ~with_local td ~rec_flag =
508 let loc = td.ptype_loc in
509 let a = gen_symbol ~prefix:"a" () in
510 let b = gen_symbol ~prefix:"b" () in
511 let v_a = evar ~loc a in
512 let v_b = evar ~loc b in
513 let function_body =
514 match td.ptype_kind with
515 | Ptype_variant cds -> compare_sum ~hide ~with_local loc cds v_a v_b
516 | Ptype_record lds -> compare_of_record ~hide ~with_local loc lds v_a v_b
517 | Ptype_open ->
518 Location.raise_errorf ~loc "ppx_compare: open types are not yet supported"
519 | Ptype_abstract ->
520 (match td.ptype_manifest with
521 | None -> compare_abstract loc td.ptype_name.txt v_a v_b
522 | Some ty ->
523 (match ty.ptyp_desc with
524 | Ptyp_variant (_, Open, _) | Ptyp_variant (_, Closed, Some (_ :: _)) ->
525 Location.raise_errorf
526 ~loc:ty.ptyp_loc
527 "ppx_compare: cannot compare open polymorphic variant types"
528 | Ptyp_variant (row_fields, _, _) ->
529 compare_variant ~hide ~with_local loc row_fields v_a v_b
530 | _ -> compare_of_ty ~hide ~with_local ty v_a v_b))
531 in
532 let extra_names =
533 List.map td.ptype_params ~f:(fun p -> tp_name (get_type_param_name p).txt)
534 in
535 let patts = List.map (extra_names @ [ a; b ]) ~f:(pvar ~loc)
536 and bnd = pvar ~loc (function_name ~with_local td.ptype_name.txt) in
537 let poly_scheme =
538 match extra_names with
539 | [] -> false
540 | _ :: _ -> true
541 in
542 let body =
543 eta_reduce_if_possible_and_nonrec ~rec_flag (eabstract ~loc patts function_body)
544 in
545 if poly_scheme
546 then
547 value_binding
548 ~loc
549 ~pat:(ppat_constraint ~loc bnd (scheme_of_td ~hide ~with_local td))
550 ~expr:body
551 else
552 value_binding
553 ~loc
554 ~pat:bnd
555 ~expr:(pexp_constraint ~loc body (scheme_of_td ~hide ~with_local td))
556 ;;
557
558 let bindings_of_tds tds ~hide ~with_local ~rec_flag =
559 List.map tds ~f:(fun td -> compare_of_td td ~hide ~with_local ~rec_flag)
560 ;;
561
562 let eta_expand2 ~loc f =
563 eabstract
564 ~loc
565 [ pvar ~loc "a"; pvar ~loc "b" ]
566 (eapply ~loc f [ evar ~loc "a"; evar ~loc "b" ])
567 ;;
568
569 let aliases_of_tds tds ~hide =
570 (* So that ~localize doesn't double the size of the generated code, we define the non
571 local_ function as an alias to the local_ function. This only works for ground
572 types, as [('a -> 'a -> int) -> 'a list -> 'a list -> int] is a type that is
573 neither stronger nor weaker than the same type with local_ on the 'a and 'a
574 list. If the compiler supports polymorphism over locality one day, we may be able
575 to only generate one version of the code, the local version. *)
576 if List.for_all tds ~f:(fun td -> List.is_empty td.ptype_params)
577 then
578 Some
579 (List.map tds ~f:(fun td ->
580 let loc = td.ptype_name.loc in
581 value_binding
582 ~loc
583 ~pat:(pvar ~loc (function_name ~with_local:false td.ptype_name.txt))
584 ~expr:
585 (pexp_constraint
586 ~loc
587 (eta_expand2
588 ~loc
589 (evar ~loc (function_name ~with_local:true td.ptype_name.txt)))
590 (scheme_of_td ~hide ~with_local:false td))))
591 else None
592 ;;
593
594 let str_type_decl ~ctxt (rec_flag, tds) localize =
595 let loc = Expansion_context.Deriver.derived_item_loc ctxt in
596 let hide = not (Expansion_context.Deriver.inline ctxt) in
597 let tds = List.map tds ~f:name_type_params_in_td in
598 let rec_flag =
599 (object
600 inherit type_is_recursive rec_flag tds as super
601
602 method! label_declaration ld =
603 if not (label_is_ignored ld) then super#label_declaration ld
604
605 method! core_type ty = if not (core_type_is_ignored ty) then super#core_type ty
606 end)
607 #go
608 ()
609 in
610 if localize
611 then
612 [ pstr_value ~loc rec_flag (bindings_of_tds tds ~hide ~with_local:true ~rec_flag)
613 ; (match aliases_of_tds tds ~hide with
614 | Some values -> pstr_value ~loc Nonrecursive values
615 | None ->
616 pstr_value
617 ~loc
618 rec_flag
619 (bindings_of_tds tds ~hide ~with_local:false ~rec_flag))
620 ]
621 else
622 [ pstr_value ~loc rec_flag (bindings_of_tds tds ~hide ~with_local:false ~rec_flag) ]
623 ;;
624
625 let mk_sig ~ctxt ~localize (_rec_flag, tds) =
626 let hide = not (Expansion_context.Deriver.inline ctxt) in
627 let tds = List.map tds ~f:name_type_params_in_td in
628 List.concat_map tds ~f:(fun td ->
629 let generate ~with_local =
630 let compare_of =
631 combinator_type_of_type_declaration td ~f:(type_ ~hide ~with_local)
632 in
633 let name = function_name ~with_local td.ptype_name.txt in
634 let loc = td.ptype_loc in
635 psig_value
636 ~loc
637 (value_description
638 ~loc
639 ~name:{ td.ptype_name with txt = name }
640 ~type_:compare_of
641 ~prim:[])
642 in
643 if localize
644 then [ generate ~with_local:false; generate ~with_local:true ]
645 else [ generate ~with_local:false ])
646 ;;
647
648 let sig_type_decl ~ctxt (rec_flag, tds) localize =
649 let loc = Expansion_context.Deriver.derived_item_loc ctxt in
650 let module_name =
651 match kind with
652 | Compare -> "Comparable"
653 | Equal -> "Equal"
654 in
655 let mk_named_sig ~with_local =
656 let module_type_name = if with_local then "S_local" else "S" in
657 let sg_name = Printf.sprintf "Ppx_compare_lib.%s.%s" module_name module_type_name in
658 mk_named_sig ~loc ~sg_name ~handle_polymorphic_variant:false tds
659 in
660 match mk_named_sig ~with_local:false, mk_named_sig ~with_local:true with
661 | Some include_infos, _ when not localize -> [ psig_include ~loc include_infos ]
662 | Some include_infos, Some include_infos_local when localize ->
663 [ psig_include ~loc include_infos; psig_include ~loc include_infos_local ]
664 | _ -> mk_sig ~ctxt ~localize (rec_flag, tds)
665 ;;
666
667 let compare_core_type ~with_local ty =
668 compare_of_ty_fun ~hide:true ~with_local ~type_constraint:true ty
669 ;;
670
671 let core_type ~with_local = compare_core_type ~with_local
672 end
673
674 module Compare = struct
675 include Make (Compare_params)
676
677 let equal_core_type ~with_local ty =
678 let loc = { ty.ptyp_loc with loc_ghost = true } in
679 let arg1 = gen_symbol () in
680 let arg2 = gen_symbol () in
681 let body =
682 Merlin_helpers.hide_expression
683 [%expr
684 match
685 [%e compare_core_type ~with_local ty] [%e evar ~loc arg1] [%e evar ~loc arg2]
686 with
687 | 0 -> true
688 | _ -> false]
689 in
690 [%expr fun ([%p pvar ~loc arg1] : [%t ty]) [%p pvar ~loc arg2] -> [%e body]]
691 ;;
692 end
693
694 module Equal = Make (Equal_params)