Skip to content

Commit c9906a6

Browse files
committed
Optimization: automatically resolve branch if it is the sole branch
1 parent 682a295 commit c9906a6

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/termOperations/eval.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ and eval_rec (term : term) (env : environment) : value =
2424
| Closure (left_branches, left_env) ->
2525
let right_value = eval_rec right_term env in
2626
let resolved_branch =
27-
resolve_branch left_branches (value_to_term right_value)
27+
resolve_branch left_branches right_value
2828
in
2929
eval_rec resolved_branch (right_value :: left_env)
3030
| VConst _ -> raise (Invalid_argument "Cannot apply a constant")
@@ -49,9 +49,11 @@ and eval_rec (term : term) (env : environment) : value =
4949

5050
(* Determines the branch of the abstraction to execute, based on the type of the argument *)
5151
and resolve_branch branches argument =
52+
(* If there's only one branch, use it, since we assume type-checking confirmed type safety *)
53+
if List.length branches = 1 then snd (List.hd branches) else
5254
(* TODO: can I always associate a base type with arguments to guarantee I can determine
5355
the appropriate branch, without needing to recompute each time? *)
54-
let argument_type = Option.get (get_type argument) in
56+
let argument_type = Option.get (get_type (value_to_term argument)) in
5557
let _, resolved_branch =
5658
List.find
5759
(fun (branch_type, _) -> is_subtype argument_type branch_type)

0 commit comments

Comments
 (0)