Skip to content

Commit

Permalink
Add comments to the elaboration code
Browse files Browse the repository at this point in the history
  • Loading branch information
yottalogical committed Nov 22, 2021
1 parent 917ef4a commit 97bbd72
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions 13-partial-application/example-code/PartialApplication.re
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,18 @@ let elaborate: exter_exp => option(inter_exp) = {

| Ap(e1, Tuple(es) as e2) as e =>
if (List.mem(Deferral, es)) {
// PARTIAL APPLICATION CODE BEGINS
let deferral_var_name = "~";

// Elaborate the original function being applied
let* e1 = syn_elaborate(ctx, e1);

// Replace the deferred inputs in the applied expression with the deferral variable
let* es_deferred = {
let deferral_replacement = {
// Generate the expression to replace the Deferrals by index
let deferral_replacement: int => inter_exp = {
// True if multiple inputs are deferred, false otherwise
// Used to determine whether the deferral variable should be a tuple
let multiple_deferrals =
es
|> List.filter((e: exter_exp) => e == Deferral)
Expand All @@ -197,13 +203,22 @@ let elaborate: exter_exp => option(inter_exp) = {
(
(index: int) =>
if (multiple_deferrals) {
Proj(Var(deferral_var_name), index);
// Deferral variable is a tuple, so it needs to be projected
Proj(
Var(deferral_var_name),
index,
);
} else {
Var(deferral_var_name);
// Deferral variable is not a tuple, so it can be used directly
Var(
deferral_var_name,
);
}
);
};

// The function used by List.fold_left
// Iterates through the applied expression, replacing each Deferral
let f =
(acc: option((list(inter_exp), int)), e: exter_exp)
: option((list(inter_exp), int)) => {
Expand All @@ -221,12 +236,16 @@ let elaborate: exter_exp => option(inter_exp) = {

let+ (es_deferred_backwards, _) =
es |> List.fold_left(f, Some(([], 0)));

// List.fold_left reverses the order of the list, so reverse it again
List.rev(es_deferred_backwards);
};

// We need to annotate the end result of this elaboration with its type since function literals can't synthesize
let+ t = syn(ctx, e);

Ann(Fun(deferral_var_name, Ap(e1, Tuple(es_deferred))), t);
// PARTIAL APPLICATION CODE ENDS
} else {
let* e1 = syn_elaborate(ctx, e1);
let+ e2 = syn_elaborate(ctx, e2);
Expand Down

0 comments on commit 97bbd72

Please sign in to comment.