-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathunified_ops.ml
209 lines (190 loc) · 5.09 KB
/
unified_ops.ml
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
open Misc
(*
Unified_ops is for specialization of some primitive operators.
For example adding two values. We have `+` for ints, `+.` for floats, and `++` for strings.
That because we don't allow implicit conversion or overloading for operations.
It is a fundamental property of the ReScript language, but it is far from the best DX we can think of,
and it became a problem when new primitives like bigint were introduced.
See discussion: https://github.com/rescript-lang/rescript-compiler/issues/6525
Unified ops mitigate the problem by adding ad-hoc translation rules on applications of the core built-in operators
which have form of binary infix ('a -> 'a -> 'a) or unary ('a -> 'a)
Translation rules should be applied in its application, in both type-level and IR(lambda)-level.
The rules:
1. If the lhs type is a primitive type, unify the rhs and the result type to the lhs type.
2. If the lhs type is not a primitive type but the rhs type is, unify lhs and the result type to the rhs type.
3. If both lhs type and rhs type is not a primitive type, unify the whole types to the int.
Since these are simple ad-hoc translations for primitive applications, we cannot use the result type defined in other contexts.
So falling back to int type is the simplest behavior that ensures backwards compatibility.
Actual implementations of translation are colocated into core modules
You can find it in:
- Type-level : ml/typecore.ml
- IR-level : ml/translcore.ml
With function name "translate_unified_ops"
*)
type form = Unary | Binary
(* Note: unified op must support int type *)
type specialization = {
int: Lambda.primitive;
bool: Lambda.primitive option;
float: Lambda.primitive option;
bigint: Lambda.primitive option;
string: Lambda.primitive option;
}
type entry = {
path: string;
(** TODO: Maybe it can be a Path.t in Predef instead of string *)
name: string;
form: form;
specialization: specialization;
}
let builtin x = Primitive_modules.pervasives ^ "." ^ x
let entries =
[|
{
path = builtin "~+";
name = "%plus";
form = Unary;
specialization =
{
int = Pidentity;
bool = None;
float = Some Pidentity;
bigint = Some Pidentity;
string = None;
};
};
{
path = builtin "~-";
name = "%neg";
form = Unary;
specialization =
{
int = Pnegint;
bool = None;
float = Some Pnegfloat;
bigint = Some Pnegbigint;
string = None;
};
};
{
path = builtin "+";
name = "%add";
form = Binary;
specialization =
{
int = Paddint;
bool = None;
float = Some Paddfloat;
bigint = Some Paddbigint;
string = Some Pstringadd;
};
};
{
path = builtin "-";
name = "%sub";
form = Binary;
specialization =
{
int = Psubint;
bool = None;
float = Some Psubfloat;
bigint = Some Psubbigint;
string = None;
};
};
{
path = builtin "*";
name = "%mul";
form = Binary;
specialization =
{
int = Pmulint;
bool = None;
float = Some Pmulfloat;
bigint = Some Pmulbigint;
string = None;
};
};
{
path = builtin "/";
name = "%div";
form = Binary;
specialization =
{
int = Pdivint;
bool = None;
float = Some Pdivfloat;
bigint = Some Pdivbigint;
string = None;
};
};
{
path = builtin "%";
name = "%mod";
form = Binary;
specialization =
{
int = Pmodint;
bool = None;
float = Some Pmodfloat;
bigint = Some Pmodbigint;
string = None;
};
};
{
path = builtin "mod";
name = "%mod";
form = Binary;
specialization =
{
int = Pmodint;
bool = None;
float = Some Pmodfloat;
bigint = Some Pmodbigint;
string = None;
};
};
{
path = builtin "**";
name = "%pow";
form = Binary;
specialization =
{
int = Ppowint;
bool = None;
float = Some Ppowfloat;
bigint = Some Ppowbigint;
string = None;
};
};
{
path = builtin "^";
name = "%bitxor";
form = Binary;
specialization =
{
int = Pxorint;
bool = None;
float = None;
bigint = Some Pxorbigint;
string = None;
};
};
{
path = builtin "|";
name = "%bitor";
form = Binary;
specialization =
{
int = Porint;
bool = None;
float = None;
bigint = Some Porbigint;
string = None;
};
};
|]
let index_by_path =
entries |> Array.map (fun entry -> (entry.path, entry)) |> create_hashtable
let index_by_name =
entries |> Array.map (fun entry -> (entry.name, entry)) |> create_hashtable