@@ -1840,6 +1840,17 @@ module Check = struct
18401840 use_def_mode * Term .t list ->
18411841 context_expr :Expr .t ->
18421842 Term .t list * Term .t
1843+ (* * [instantiate_operator_types_from_inferred_types spec relation_name
1844+ num_actual_args (mode, inferred_types) ~context_expr] instantiates
1845+ the types of the arguments and output of the operator [relation_name]
1846+ given the number of actual arguments [num_actual_args] and the
1847+ [inferred_types] for either its arguments or its output depending on
1848+ [mode]. The [context_expr] is used for error reporting. When mode is
1849+ [Use], the [inferred_types] correspond to the argument types. When
1850+ mode is [Def], the [inferred_types] correspond to the output type. By
1851+ instantiating we mean substituting type parameters in the operator
1852+ definition with concrete types inferred for a given operator
1853+ invocation expression. *)
18431854 end = struct
18441855 (* * [unify_parameter_type spec ~relation_name parameter_name
18451856 parameter_type type_env] attempts to unify [parameter_type] with any
@@ -2076,17 +2087,6 @@ module Check = struct
20762087 in
20772088 (instantiated_arg_types, instantiated_output_type)
20782089
2079- (* * [instantiate_operator_types_from_inferred_types spec relation_name
2080- num_actual_args (mode, inferred_types) ~context_expr] instantiates
2081- the types of the arguments and output of the operator [relation_name]
2082- given the number of actual arguments [num_actual_args] and the
2083- [inferred_types] for either its arguments or its output depending on
2084- [mode]. The [context_expr] is used for error reporting. When mode is
2085- [Use], the [inferred_types] correspond to the argument types. When
2086- mode is [Def], the [inferred_types] correspond to the output type. By
2087- instantiating we mean substituting type parameters in the operator
2088- definition with concrete types inferred for a given operator
2089- invocation expression. *)
20902090 let instantiate_operator_types_from_inferred_types spec relation_name
20912091 num_actual_args (mode , inferred_types ) ~context_expr =
20922092 let formal_arg_types =
@@ -2509,7 +2509,7 @@ module Check = struct
25092509 and return the function's output type. *)
25102510 let lhs_type, type_env = infer_type_in_env spec type_env lhs in
25112511 let from_term, to_term =
2512- match lhs_type with
2512+ match CheckTypeInstantiations. reduce_term spec lhs_type with
25132513 | Function { from_type = _ , from_term ; to_type = _ , to_term } ->
25142514 (from_term, to_term)
25152515 | _ -> Error. invalid_map_lhs_type lhs_type ~context_expr: expr
@@ -3111,25 +3111,34 @@ module ExtendNames = struct
31113111 open Expr
31123112 open Rule
31133113
3114- (* * [opt_extend] Wraps [expr] with a name if [opt_name ] is [Some]. Avoids
3115- naming a variable expression with its own name, as an optimization . *)
3116- let opt_extend expr opt_name =
3117- match (expr, opt_name ) with
3114+ (* * [opt_extend] Wraps [expr] with a name if [opt_param_name ] is [Some].
3115+ Avoids naming a variable expression with its own name. *)
3116+ let opt_extend spec expr opt_param_name =
3117+ match (expr, opt_param_name ) with
31183118 | _ , None -> expr
31193119 | Var v , Some name when String. equal v name ->
31203120 expr (* Avoid naming a variable with its own name. *)
3121+ | ( Expr. Relation
3122+ { name = operator_name; is_operator = true ; args = [ Var v ] },
3123+ Some name ) ->
3124+ let relation = relation_for_id spec operator_name in
3125+ if Relation. is_typecast_operator relation && String. equal v name then
3126+ (* Typecasts render the input variable. If the input variable has the same name
3127+ as the parameter, avoid naming it. *)
3128+ expr
3129+ else NamedExpr (expr, name)
31213130 | _ , Some name -> NamedExpr (expr, name)
31223131
31233132 (* * [extend_with_names type_term expr ] recursively transforms [expr] by
31243133 adding names from [type_term] to sub-expressions of [expr]. Currently,
31253134 only tuples (labelled or unlabelled) are supported, which is sufficient
31263135 for most output configurations. *)
3127- let rec extend_with_names type_term expr =
3136+ let rec extend_with_names spec type_term expr =
31283137 match (type_term, expr) with
31293138 | Term. Tuple { label_opt = None ; args = [ (opt_name, _) ] } , _ ->
31303139 (* An unlabelled tuple with a single component serves as a named reference
31313140 to any type.*)
3132- opt_extend expr opt_name
3141+ opt_extend spec expr opt_name
31333142 | ( Term. Tuple { label_opt = term_label_opt; args = term_components },
31343143 Expr. Tuple { label_opt = expr_label_opt; args = expr_components } )
31353144 when Option. equal String. equal term_label_opt expr_label_opt ->
@@ -3147,7 +3156,7 @@ module ExtendNames = struct
31473156 let extended_args =
31483157 List. map2
31493158 (fun (opt_name , arg_type ) arg ->
3150- opt_extend (extend_with_names arg_type arg) opt_name)
3159+ opt_extend spec (extend_with_names spec arg_type arg) opt_name)
31513160 term_components expr_components
31523161 in
31533162 Expr. Tuple { label_opt = expr_label_opt; args = extended_args }
@@ -3167,7 +3176,7 @@ module ExtendNames = struct
31673176 (* Fallback to the main output type. *)
31683177 List. hd output_types
31693178 in
3170- let extended_rhs = extend_with_names output_type rhs in
3179+ let extended_rhs = extend_with_names spec output_type rhs in
31713180 let extended_expr =
31723181 Transition { lhs; rhs = extended_rhs; short_circuit }
31733182 in
0 commit comments