Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(compiler): wrap partial application in anonymous functions #92

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion caramel/compiler/ocaml_to_erlang/fun.ml
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ and mk_expression exp ~var_names ~modules ~functions ~module_name =
in
Erlang.Ast.Expr_tuple [ tag; value ]
| Texp_apply (expr, args) ->
let expr_arg_types, _ = Uncurry.uncurry_tarrow expr.exp_type [] in
let wanted_arity = List.length expr_arg_types in
let name =
match
mk_expression expr ~var_names ~modules ~functions ~module_name
Expand All @@ -261,7 +263,21 @@ and mk_expression exp ~var_names ~modules ~functions ~module_name =
(mk_expression arg ~var_names ~modules ~functions ~module_name))
args
in
Expr.apply name args
if wanted_arity > List.length args then
let rec gen_names acc n =
match n with
| 0 -> acc
| n ->
gen_names
(Name.var ("Caramel@" ^ string_of_int n) :: acc)
(n - 1)
in
let outer_names = gen_names [] (wanted_arity - List.length args) in
let lhs = List.map Pat.bind outer_names in
let idents = List.map Expr.ident outer_names in
let rhs = Expr.apply name (args @ idents) in
Expr.fun_ ~cases:[ FunDecl.case ~lhs ~guard:None ~rhs ]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand this correctly, you're

  1. computed the arity of this particular function type, using Uncurry.uncurry_tarrow/2
  2. checking if we're applying this function with the as many arguments as the arity indicated by the type from 1)
  3. if we are missing arguments, create a function that will take the missing arguments and pass them on to the function from 1)

I think this will break for named and default arguments, so we need to have another look at this.

This can be problematic because fun's in Erlang can have a single arity, but multiple clauses. This means that a function f ?(x = 1) y = x + y would translate to 2 erlang functions:

f(Y) -> f(1, Y).
f(X, Y) -> X + Y.

And a function with named arguments like f ~x ~y = x / y would need to be kept track until is fully applied before we can call it.

For example, what does let div_one = (f ~y: 1) translate to?

Div_one = fun (X) -> f(1, X) end,

% or
Div_one = fun (X) -> f(X, 1) end,

I think we may need to take a step back and rethink this. It's turning out to be a tad more complex than I thought id' be at first 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will break for named and default arguments,

which are not supported right now anyway, but I understand we should think ahead...

Gleam supports labeled arguments, and I think those are probably fine to figure out at Caramel compilation time. Not sure about default arguments, they indeed seem tricky. What values are allowed? Just simple literals? If so then maybe they are just in the typedtree and could be fetched from there and printed when needed...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that a function f ?(x = 1) y = x + y would translate to 2 erlang functions:

I would propose only generate the full arguments version in the resulting Erlang, so only support optional / labeled arguments when called in from Caramel, not other langs.

For example, what does let div_one = (f ~y:1) translate to?

This depends on the way f was declared, because of the order, and the rules of Ocaml erasure would need to be preserved. So assuming it is f x ~y = x + y which in erlang is just f(X, Y), then

Div_one = fun (X) -> f(X, 1) end,

and if f ~y x = x + y then fun (X) -> f(1, X) end,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even generate the erlang functions with smaller arity for optional arguments, but only for erlang/elixir interoperability (don't use them from caramel because it's unnecessarily complicating things), and only if there is just one optional or have some rules like Elixir that they can be ommited inclusively starting from the end only.

In any case I think we can figure it out and if not, caramel is not 1.0 anyway :)

else Expr.apply name args
| Texp_record { fields; extended_expression; _ } -> (
let fields =
fields |> Array.to_list
Expand Down
6 changes: 6 additions & 0 deletions tests/compiler/expressions.t/apply.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ let lambda () =
let _ = f () in
let _ = f' 1 in
f'' 1 2

let abs_diff_times x y z = (Erlang.abs (x - y)) * z

let partial_application () =
let f = abs_diff_times 3 in
f (-1) 2
12 changes: 12 additions & 0 deletions tests/compiler/expressions.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
% Source code generated with Caramel.
-module(apply).

-export([abs_diff_times/3]).
-export([f/1]).
-export([f1/1]).
-export([f2/2]).
-export([f3/3]).
-export([f4/4]).
-export([lambda/0]).
-export([partial_application/0]).
-export([run/0]).

-spec f(_) -> _.
Expand Down Expand Up @@ -79,6 +81,16 @@
F_prime(1),
F_prime_prime(1, 2).

-spec abs_diff_times(integer(), integer(), integer()) -> integer().
abs_diff_times(X, Y, Z) -> erlang:'*'(erlang:abs(erlang:'-'(X, Y)), Z).

-spec partial_application() -> integer().
partial_application() ->
F = fun
(Caramel@1, Caramel@2) -> abs_diff_times(3, Caramel@1, Caramel@2)
end,
F(-1, 2).


$ caramel compile binding_on_match.ml
File "binding_on_match.ml", line 4, characters 57-68:
Expand Down