Skip to content

Commit

Permalink
TC: retry return type unification only after all arguments
Browse files Browse the repository at this point in the history
This can provide more natural instantiations for (e.g.) bitvector
concatenation.
  • Loading branch information
bacam committed Jan 15, 2025
1 parent f080bf5 commit 28ebb39
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/type_check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3970,7 +3970,6 @@ and infer_funapp' l env f (typq, f_typ) xs uannot expected_ret_typ =
);
List.iter (fun unifier -> quants := instantiate_quants !quants unifier) unifiers;
List.iter (fun (v, arg) -> typ_ret := typ_subst v arg !typ_ret) unifiers;
let remaining_typs = instantiate_return_type remaining_typs in
let remaining_typs =
List.map (fun typ -> List.fold_left (fun typ (v, arg) -> typ_subst v arg typ) typ unifiers) remaining_typs
in
Expand All @@ -3994,6 +3993,7 @@ and infer_funapp' l env f (typq, f_typ) xs uannot expected_ret_typ =
| [] -> raise (Reporting.err_unreachable l __POS__ "Empty arguments during instantiation")
in
let xs, typ_args, env, deferred = List.fold_left fold_instantiate ([], typ_args, env, []) xs in
let typ_args = instantiate_return_type typ_args in
let num_deferred = List.length deferred in
typ_debug (lazy (Printf.sprintf "Have %d deferred arguments" num_deferred));
if num_deferred = previously_deferred then (xs, env)
Expand Down
11 changes: 11 additions & 0 deletions test/typecheck/pass/mixed_order_unification.sail
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
default Order dec
$include <prelude.sail>

/* An example where inference of the type parameters needs
unification on the arguments before unification on the result type can
provide 'y, but there's no need to go back to the arguments afterwards. */

val f : forall 'x 'y 'z, 'x + 'y > 0. (int('x), int('z)) -> bits('x + 'y)

val g : unit -> bits(1)
function g() = f(0,2)

0 comments on commit 28ebb39

Please sign in to comment.