Debian Codegraph @

expander/renaming.ml

raw symbols SCIP
1 open! Base
2 open! Ppxlib
3
4 type t =
5 { universal : (Fresh_name.t, string loc) Result.t Map.M(String).t
6 ; existential : bool
7 }
8
9 module Binding_kind = struct
10 type t =
11 | Universally_bound of Fresh_name.t
12 | Existentially_bound
13 end
14
15 let add_universally_bound t name ~prefix =
16 { t with
17 universal =
18 Map.set
19 t.universal
20 ~key:name.txt
21 ~data:(Ok (Fresh_name.create (prefix ^ name.txt) ~loc:name.loc))
22 }
23 ;;
24
25 let binding_kind t var ~loc =
26 match Map.find t.universal var with
27 | None ->
28 if t.existential
29 then Binding_kind.Existentially_bound
30 else Location.raise_errorf ~loc "ppx_sexp_conv: unbound type variable '%s" var
31 | Some (Ok fresh) -> Binding_kind.Universally_bound fresh
32 | Some (Error { loc; txt }) -> Location.raise_errorf ~loc "%s" txt
33 ;;
34
35 (* Return a map translating type variables appearing in the return type of a GADT
36 constructor to their name in the type parameter list.
37
38 For instance:
39
40 {[
41 type ('a, 'b) t = X : 'x * 'y -> ('x, 'y) t
42 ]}
43
44 will produce:
45
46 {v
47 "x" -> Ok "a"
48 "y" -> Ok "b"
49 v}
50
51 If a variable appears twice in the return type it will map to [Error _]. If a
52 variable cannot be mapped to a parameter of the type declaration, it will map to
53 [Error] (for instance [A : 'a -> 'a list t]).
54
55 It returns [original] on user error, to let the typer give the error message *)
56 let with_constructor_declaration original cd ~type_parameters:tps =
57 (* Add all type variables of a type to a map. *)
58 let add_typevars =
59 object
60 inherit [t] Ast_traverse.fold as super
61
62 method! core_type ty t =
63 match ty.ptyp_desc with
64 | Ptyp_var var ->
65 let error =
66 { loc = ty.ptyp_loc
67 ; txt = "ppx_sexp_conv: variable is not a parameter of the type constructor"
68 }
69 in
70 { t with universal = Map.set t.universal ~key:var ~data:(Error error) }
71 | _ -> super#core_type ty t
72 end
73 in
74 let aux t tp_name tp_in_return_type =
75 match tp_in_return_type.ptyp_desc with
76 | Ptyp_var var ->
77 let data =
78 let loc = tp_in_return_type.ptyp_loc in
79 if Map.mem t.universal var
80 then Error { loc; txt = "ppx_sexp_conv: duplicate variable" }
81 else (
82 match Map.find original.universal tp_name with
83 | Some result -> result
84 | None -> Error { loc; txt = "ppx_sexp_conv: unbound type parameter" })
85 in
86 { t with universal = Map.set t.universal ~key:var ~data }
87 | _ -> add_typevars#core_type tp_in_return_type t
88 in
89 match cd.pcd_res with
90 | None -> original
91 | Some ty ->
92 (match ty.ptyp_desc with
93 | Ptyp_constr (_, params) ->
94 if List.length params <> List.length tps
95 then original
96 else
97 Stdlib.ListLabels.fold_left2
98 tps
99 params
100 ~init:{ existential = true; universal = Map.empty (module String) }
101 ~f:aux
102 | _ -> original)
103 ;;
104
105 let of_type_declaration decl ~prefix =
106 { existential = false
107 ; universal =
108 List.fold
109 decl.ptype_params
110 ~init:(Map.empty (module String))
111 ~f:(fun map param ->
112 let name = get_type_param_name param in
113 Map.update map name.txt ~f:(function
114 | None -> Ok (Fresh_name.create (prefix ^ name.txt) ~loc:name.loc)
115 | Some _ ->
116 Error { loc = name.loc; txt = "ppx_sexp_conv: duplicate variable" }))
117 }
118 ;;
119
120 let without_type () = { existential = false; universal = Map.empty (module String) }