Skip to content

Commit 06763a4

Browse files
author
Kota Mizushima
authored
Merge pull request #165 from kmizu/line-app-abs
Add line to App, Abs, Let, and Letrec
2 parents c55c7e8 + f6a4306 commit 06763a4

File tree

5 files changed

+92
-48
lines changed

5 files changed

+92
-48
lines changed

lib/ast.ml

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ type t =
1414
| Constant of line * Constant.t
1515
| Var of line * string
1616
| Tuple of line * t list
17-
| App of t * t list
18-
| Abs of fun_abst
19-
| Let of string * t * t
20-
| Letrec of (string * fun_abst) list * t
17+
| App of line * t * t list
18+
| Abs of line * fun_abst
19+
| Let of line * string * t * t
20+
| Letrec of line * (string * fun_abst) list * t
2121
| Case of t * (pattern * t) list
2222
| LocalFun of {function_name : string; arity: int}
2323
| MFA of {module_name: t; function_name: t; arity: t}
@@ -37,9 +37,9 @@ let line_number_of_t = function
3737
| Constant (line, _) -> line
3838
| Var (line, _) -> line
3939
| Tuple (line, _) -> line
40-
| App (_, _) -> -1
41-
| Abs (_) -> -1
42-
| Let (_, _, _) -> -1
40+
| App (line, _, _) -> line
41+
| Abs (line, _) -> line
42+
| Let (line, _, _, _) -> line
4343
| Letrec (_) -> -1
4444
| Case (_, _) -> -1
4545
| LocalFun _ -> -1

lib/derivation.ml

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ let rec derive context = function
2222
result_map_m ~f:(derive context) exprs
2323
>>| List.unzip
2424
>>| fun (tys, cs) -> (Type.of_elem (TyTuple tys), Conj cs)
25-
| App (f, args) ->
25+
| App (_line, f, args) ->
2626
derive context f >>= fun (tyf, cf) ->
2727
result_map_m
2828
~f:(fun arg ->
@@ -47,7 +47,7 @@ let rec derive context = function
4747
args_constraints
4848
in
4949
Ok (beta, Conj constraints)
50-
| Abs {args=vs; body=e} ->
50+
| Abs (_line, {args=vs; body=e}) ->
5151
let new_tyvars = List.map ~f:(fun v -> (v, (new_tyvar ()))) vs in
5252
let added_context =
5353
List.fold_left
@@ -57,11 +57,11 @@ let rec derive context = function
5757
in
5858
derive added_context e >>= fun (ty_e, c) ->
5959
Ok (Type.of_elem (TyFun (List.map ~f:snd new_tyvars, ty_e)), c)
60-
| Let (v, e1, e2) ->
60+
| Let (_line, v, e1, e2) ->
6161
derive context e1 >>= fun (ty_e1, c1) ->
6262
derive (Context.add (Context.Key.Var v) ty_e1 context) e2 >>= fun (ty_e2, c2) ->
6363
Ok (ty_e2, Conj [c1; c2])
64-
| Letrec (lets , e) ->
64+
| Letrec (line, lets , e) ->
6565
let new_tyvars = List.map ~f:(fun (v, f) -> (v, f, (new_tyvar ()))) lets in
6666
let added_context =
6767
List.fold_left
@@ -73,7 +73,7 @@ let rec derive context = function
7373
in
7474
let constraints_result =
7575
new_tyvars
76-
|> result_map_m ~f:(fun (_, f, tyvar) -> derive added_context (Abs f) >>| fun(ty, c) -> (ty, c, tyvar))
76+
|> result_map_m ~f:(fun (_, f, tyvar) -> derive added_context (Abs (line, f)) >>| fun(ty, c) -> (ty, c, tyvar))
7777
>>| List.map ~f:(fun (ty, c, tyvar) -> [Eq (tyvar, ty); c])
7878
>>| List.concat
7979
in

lib/from_erlang.ml

+34-10
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,30 @@ let rec pattern_of_erlang_pattern = function
190190
| F.PatCons {head; tail; _} ->
191191
PatCons (pattern_of_erlang_pattern head, pattern_of_erlang_pattern tail)
192192

193+
let rec line_number_of_erlang_expr = function
194+
| F.ExprBody {exprs} -> line_number_of_erlang_expr (List.hd_exn exprs)
195+
| ExprCase {line; _} -> line
196+
| ExprCons {line; _} -> line
197+
| ExprNil {line} -> line
198+
| ExprListComprehension {line; _} -> line
199+
| ExprLocalFunRef {line; _} -> line
200+
| ExprRemoteFunRef {line; _} -> line
201+
| ExprFun {line; _} -> line
202+
| ExprLocalCall {line; _} -> line
203+
| ExprRemoteCall {line; _} -> line
204+
| ExprMapCreation {line; _} -> line
205+
| ExprMapUpdate {line; _} -> line
206+
| ExprMatch {line; _} -> line
207+
| ExprBinOp {line; _} -> line
208+
| ExprTuple {line; _} -> line
209+
| ExprVar {line; _} -> line
210+
| ExprLit {lit} ->
211+
match lit with
212+
| LitAtom {line; _} -> line
213+
| LitChar {line; _} -> line
214+
| LitInteger {line; _} -> line
215+
| LitBigInt {line; _} -> line
216+
| LitString {line; _} -> line
193217

194218
(* converts a secuence of expressions `[e1; e2; ...]` to an expression `let _ = e1 in let _ = e2 in ...` *)
195219
(* assume `extract_toplevel` is applied to the argument *)
@@ -202,7 +226,7 @@ let rec expr_of_erlang_exprs = function
202226
let es' = expr_of_erlang_exprs es in
203227
Case (body', [((pattern_of_erlang_pattern pattern, Constant (line, Atom "true")), es')])
204228
| e :: es ->
205-
Let ("_", expr_of_erlang_expr' e, expr_of_erlang_exprs es)
229+
Let (line_number_of_erlang_expr e, "_", expr_of_erlang_expr' e, expr_of_erlang_exprs es)
206230
and expr_of_erlang_expr' = function
207231
| F.ExprBody {exprs} ->
208232
expr_of_erlang_exprs exprs
@@ -225,20 +249,20 @@ and expr_of_erlang_expr' = function
225249
let fun_abst = function_of_clauses clauses in
226250
(* If name is omitted, don't create Letrec *)
227251
(match name with
228-
| Some name -> Letrec ([(name, fun_abst)], Var (line, name))
229-
| None -> Abs fun_abst
252+
| Some name -> Letrec (line, [(name, fun_abst)], Var (line, name))
253+
| None -> Abs (line, fun_abst)
230254
)
231-
| ExprLocalCall {function_expr=ExprLit {lit=LitAtom {atom=function_name; _}}; args; _} ->
255+
| ExprLocalCall {line; function_expr=ExprLit {lit=LitAtom {atom=function_name; _}}; args} ->
232256
let arity = List.length args in
233-
App (LocalFun{function_name; arity}, List.map ~f:expr_of_erlang_expr' args)
234-
| ExprLocalCall {function_expr; args; _} ->
235-
App (expr_of_erlang_expr' function_expr, List.map ~f:expr_of_erlang_expr' args)
257+
App (line, LocalFun{function_name; arity}, List.map ~f:expr_of_erlang_expr' args)
258+
| ExprLocalCall {line; function_expr; args} ->
259+
App (line, expr_of_erlang_expr' function_expr, List.map ~f:expr_of_erlang_expr' args)
236260
| ExprRemoteCall {line; module_expr; function_expr; args; _} ->
237261
let mfa = MFA {
238262
module_name=expr_of_erlang_expr' module_expr;
239263
function_name=expr_of_erlang_expr' function_expr;
240264
arity=Constant (line, Number (List.length args))} in
241-
App (mfa, List.map ~f:expr_of_erlang_expr' args)
265+
App (line, mfa, List.map ~f:expr_of_erlang_expr' args)
242266
| ExprMatch {line; pattern; body} ->
243267
(* There is no match expression in `e` by `extract_match_expr`.
244268
* Futhermore, this match expression is single or the last of expr sequence because
@@ -253,7 +277,7 @@ and expr_of_erlang_expr' = function
253277
function_name = Constant (line, Atom op);
254278
arity=Constant (line, Number 2)
255279
} in
256-
App(func, List.map ~f:expr_of_erlang_expr' [lhs; rhs])
280+
App(line, func, List.map ~f:expr_of_erlang_expr' [lhs; rhs])
257281
| ExprTuple {line; elements} -> Tuple (line, List.map ~f:expr_of_erlang_expr' elements)
258282
| ExprVar {line; id} -> Var (line, id)
259283
| ExprLit {lit} -> expr_of_literal lit
@@ -376,7 +400,7 @@ let module_to_expr m =
376400
Tuple (-1, [Constant (-1, Atom name); LocalFun {function_name=name; arity=List.length args}]))
377401
|> (fun es -> Tuple (-1, es))
378402
in
379-
Letrec(funs, body)
403+
Letrec(-1, funs, body)
380404
|> Result.return
381405

382406
let code_to_expr code =

test/unit-test/test_derivation.ml

+17-17
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,15 @@ let%expect_test "derivation" =
192192
Empty))))
193193
|}];
194194

195-
print Context.empty (Abs {args=["X"]; body=Var (3, "X")});
195+
print Context.empty (Abs (-1, {args=["X"]; body=Var (3, "X")}));
196196
[%expect {|
197197
(Ok ("fun((a) -> a)" Empty)) |}];
198198

199-
print Context.empty (Abs {args=["x"; "y"; "z"]; body=Var (1,"x")});
199+
print Context.empty (Abs (-1, {args=["x"; "y"; "z"]; body=Var (1,"x")}));
200200
[%expect {|
201201
(Ok ("fun((a, b, c) -> a)" Empty)) |}];
202202

203-
print Context.empty (App (Constant (1, Number 57), [Constant (2, Number 42)]));
203+
print Context.empty (App (3, Constant (1, Number 57), [Constant (2, Number 42)]));
204204
[%expect {|
205205
(Ok (
206206
c (
@@ -211,7 +211,7 @@ let%expect_test "derivation" =
211211
(Subtype 42 a)
212212
Empty)))) |}];
213213

214-
print Context.empty (App (Constant (-1, Atom "I am a function!"), [Constant (-1, Number 42); Constant (-1, Number 57)]));
214+
print Context.empty (App (3, Constant (-1, Atom "I am a function!"), [Constant (-1, Number 42); Constant (-1, Number 57)]));
215215
[%expect {|
216216
(Ok (
217217
d (
@@ -224,7 +224,7 @@ let%expect_test "derivation" =
224224
(Subtype 57 b)
225225
Empty)))) |}];
226226

227-
print Context.empty (App (Abs {args=["X"]; body=Var (-1, "X")}, [Constant (-1, Number 42)]));
227+
print Context.empty (App (3, Abs (-1, {args=["X"]; body=Var (-1, "X")}), [Constant (-1, Number 42)]));
228228
[%expect {|
229229
(Ok (
230230
d (
@@ -236,7 +236,7 @@ let%expect_test "derivation" =
236236
Empty))))
237237
|}];
238238

239-
print Context.empty (App (Abs {args=["X"; "Y"]; body=Var (-1, "X")}, [Constant (-1, Number 42); Constant (-1, Number 57)]));
239+
print Context.empty (App (3, Abs (-1, {args=["X"; "Y"]; body=Var (-1, "X")}), [Constant (-1, Number 42); Constant (-1, Number 57)]));
240240
[%expect {|
241241
(Ok (
242242
f (
@@ -249,20 +249,20 @@ let%expect_test "derivation" =
249249
(Subtype 57 d)
250250
Empty)))) |}];
251251

252-
print Context.empty (Let ("x", Constant (-1, Number 42), Var (-1, "x")));
252+
print Context.empty (Let (-1, "x", Constant (-1, Number 42), Var (-1, "x")));
253253
[%expect {|
254254
(Ok (42 (Conj (Empty Empty)))) |}];
255255

256-
print Context.empty (Letrec ([("x", {args=[]; body=Constant (-1, Number 42)})], LocalFun {function_name="x"; arity=0}));
256+
print Context.empty (Letrec (-1, [("x", {args=[]; body=Constant (-1, Number 42)})], LocalFun {function_name="x"; arity=0}));
257257
[%expect {| (Ok (a (Conj (Empty (Eq a "fun(() -> 42)") Empty)))) |}];
258258

259259
print
260260
Context.empty
261261
(Letrec
262-
([
263-
("f", {args=["X"]; body=App (LocalFun {function_name="g"; arity=1}, [Var (1, "X")])});
264-
("g", {args=["X"]; body=App (LocalFun {function_name="f"; arity=1}, [Var (2, "X")])})
265-
], App (LocalFun {function_name="f"; arity=1}, [Constant (3, (Number 42))])));
262+
(-1, [
263+
("f", {args=["X"]; body=App (4, LocalFun {function_name="g"; arity=1}, [Var (1, "X")])});
264+
("g", {args=["X"]; body=App (5, LocalFun {function_name="f"; arity=1}, [Var (2, "X")])})
265+
], App (6, LocalFun {function_name="f"; arity=1}, [Constant (3, (Number 42))])));
266266
[%expect {|
267267
(Ok (
268268
m (
@@ -292,7 +292,7 @@ let%expect_test "derivation" =
292292
(Context.add (Context.Key.MFA {module_name="m"; function_name="f"; arity=0})
293293
(Type.of_elem (TyFun ([], Type.of_elem (TySingleton (Atom "ok")))))
294294
Context.empty)
295-
(App (MFA {module_name=Constant (-1, Atom "m"); function_name=Constant (-1, Atom "f"); arity=Constant (-1, Number 0)}, []));
295+
(App (-1, MFA {module_name=Constant (-1, Atom "m"); function_name=Constant (-1, Atom "f"); arity=Constant (-1, Number 0)}, []));
296296
[%expect {|
297297
(Ok (
298298
b (
@@ -305,10 +305,10 @@ let%expect_test "derivation" =
305305
(Context.add (Context.Key.MFA {module_name="m"; function_name="f"; arity=0})
306306
(Type.of_elem (TyFun ([], Type.of_elem (TySingleton (Atom "ok")))))
307307
Context.empty)
308-
(Let ("M", Constant (-1, Atom "m"),
309-
Let ("F", Constant (-1, Atom "f"),
310-
Let ("A", Constant (-1, Number 0),
311-
App (MFA {module_name=Var (-1, "M"); function_name=Var (-1, "F"); arity=Var (-1, "A")}, [])))));
308+
(Let (-1, "M", Constant (-1, Atom "m"),
309+
Let (-1, "F", Constant (-1, Atom "f"),
310+
Let (-1, "A", Constant (-1, Number 0),
311+
App (-1, MFA {module_name=Var (-1, "M"); function_name=Var (-1, "F"); arity=Var (-1, "A")}, [])))));
312312
[%expect {|
313313
(Ok (
314314
c (

test/unit-test/test_from_erlang.ml

+29-9
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ let%expect_test "from_erlang" =
9696
ClsFun {line=1; patterns=[PatVar {line=1; id="X"}]; guard_sequence=None; body=ExprVar {line=1; id="X"}}
9797
]});
9898
[%expect {|
99-
(Abs ((args (X)) (body (Var 1 X))))
99+
(Abs 1 ((args (X)) (body (Var 1 X))))
100100
|}];
101101

102102
(*
@@ -109,7 +109,27 @@ let%expect_test "from_erlang" =
109109
body=ExprLocalCall {line=1; function_expr=ExprVar {line=1; id="F"}; args=[ExprVar {line=1; id="X"}]}}
110110
]});
111111
[%expect {|
112-
(Letrec ((F ((args (X)) (body (App (Var 1 F) ((Var 1 X))))))) (Var 1 F))
112+
(Letrec 1 ((F ((args (X)) (body (App 1 (Var 1 F) ((Var 1 X))))))) (Var 1 F))
113+
|}];
114+
115+
(*
116+
* fun (X) -> [100; 200] end
117+
*)
118+
print (ExprFun {line=1; name=None; clauses=[
119+
ClsFun {line=1;
120+
patterns=[PatVar {line=2; id="X"}];
121+
guard_sequence=None;
122+
body=ExprCons{line=3; head=ExprLit {lit=LitInteger {line=4; integer=100}};
123+
tail=ExprCons {line=5; head=ExprLit {lit=LitInteger{line=6; integer=200}};
124+
tail=ExprNil {line=7}}}}
125+
]});
126+
[%expect {|
127+
(Abs 1 (
128+
(args (X))
129+
(body (
130+
ListCons
131+
(Constant 4 (Number 100))
132+
(ListCons (Constant 6 (Number 200)) ListNil)))))
113133
|}];
114134

115135
(*
@@ -128,7 +148,7 @@ let%expect_test "from_erlang" =
128148
body=ExprTuple {line=1; elements=[ExprVar {line=1; id="X"}; ExprVar {line=1; id="Y"}]}}
129149
]});
130150
[%expect {|
131-
(Abs (
151+
(Abs 1 (
132152
(args (__A__ __B__))
133153
(body (
134154
Case
@@ -161,7 +181,7 @@ let%expect_test "from_erlang" =
161181
ClsFun {line=1; patterns=[PatLit {lit=LitAtom {line=1; atom="x"}}]; guard_sequence=None; body=ExprLit {lit=LitAtom {line=1; atom="y"}}}
162182
]});
163183
[%expect {|
164-
(Abs (
184+
(Abs 1 (
165185
(args (__A__))
166186
(body (
167187
Case
@@ -179,7 +199,7 @@ let%expect_test "from_erlang" =
179199
ClsFun {line=1; patterns=[PatLit {lit=LitInteger {line=1; integer=42}}]; guard_sequence=None; body=ExprLit {lit=LitInteger {line=1; integer=43}}}
180200
]});
181201
[%expect {|
182-
(Abs (
202+
(Abs 1 (
183203
(args (__A__))
184204
(body (
185205
Case
@@ -200,7 +220,7 @@ let%expect_test "from_erlang" =
200220
ClsFun {line=2; patterns=[PatCons {line=2; head=PatVar {line=2; id="H"}; tail=PatVar {line=2; id="T"}}]; guard_sequence=None; body=ExprVar {line=2; id="T"}}
201221
]});
202222
[%expect {|
203-
(Abs (
223+
(Abs 1 (
204224
(args (__A__))
205225
(body (
206226
Case
@@ -220,7 +240,7 @@ let%expect_test "from_erlang" =
220240
ClsFun {line=1; patterns=[PatLit {lit=LitString {line=1; str="abc"}}]; guard_sequence=None; body=ExprLit {lit=LitAtom {line=1; atom="ok"}}}
221241
]});
222242
[%expect {|
223-
(Abs (
243+
(Abs 1 (
224244
(args (__A__))
225245
(body (
226246
Case
@@ -285,7 +305,7 @@ let%expect_test "from_erlang" =
285305
ExprMatch {line=1; pattern=PatVar {line=1; id="B"}; body=ExprLit {lit=LitInteger {line=1; integer=2}}};
286306
ExprBinOp {line=1; op="+"; lhs=ExprVar {line=1; id="A"}; rhs=ExprVar {line=1; id="B"}}]}}]});
287307
[%expect {|
288-
(Abs (
308+
(Abs 1 (
289309
(args ())
290310
(body (
291311
Case
@@ -296,7 +316,7 @@ let%expect_test "from_erlang" =
296316
(Constant 1 (Number 2))
297317
((
298318
((PatVar B) (Constant 1 (Atom true)))
299-
(App
319+
(App 1
300320
(MFA
301321
(module_name (Constant 1 (Atom erlang)))
302322
(function_name (Constant 1 (Atom +)))

0 commit comments

Comments
 (0)