From de4c8f5412cd9986b66a552b1c0171cae5f66f98 Mon Sep 17 00:00:00 2001 From: Gaith Hallak Date: Sun, 30 Apr 2023 13:48:23 +0300 Subject: [PATCH] Remove the dependency of type_utils on errors --- src/aeso_ast_infer_types.erl | 6 +++--- src/aeso_tc_constraints.erl | 12 ++++++------ src/aeso_tc_type_utils.erl | 16 +++++----------- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/aeso_ast_infer_types.erl b/src/aeso_ast_infer_types.erl index 6c02c2b..19ca056 100644 --- a/src/aeso_ast_infer_types.erl +++ b/src/aeso_ast_infer_types.erl @@ -89,7 +89,7 @@ when_warning(A, B) -> aeso_tc_options:when_warning(A, B). %% ------- -ensure_first_order(A, B) -> aeso_tc_type_utils:ensure_first_order(A, B). +is_first_order(A) -> aeso_tc_type_utils:is_first_order(A). app_t(A, B, C) -> aeso_tc_type_utils:app_t(A, B, C). %% ------- @@ -945,9 +945,9 @@ check_entrypoints(Defs) -> get_option(allow_higher_order_entrypoints, false) =:= false ]. ensure_first_order_entrypoint({letfun, Ann, Id = {id, _, Name}, Args, Ret, _}) -> - [ ensure_first_order(ArgType, {higher_order_entrypoint, AnnArg, Id, {argument, ArgId, ArgType}}) + [ is_first_order(ArgType) orelse type_error({higher_order_entrypoint, AnnArg, Id, {argument, ArgId, ArgType}}) || {typed, AnnArg, ArgId, ArgType} <- Args ], - [ ensure_first_order(Ret, {higher_order_entrypoint, Ann, Id, {result, Ret}}) + [ is_first_order(Ret) orelse type_error({higher_order_entrypoint, Ann, Id, {result, Ret}}) || Name /= "init" ], %% init can return higher-order values, since they're written to the store %% rather than being returned. ok. diff --git a/src/aeso_tc_constraints.erl b/src/aeso_tc_constraints.erl index 01631d7..b98f725 100644 --- a/src/aeso_tc_constraints.erl +++ b/src/aeso_tc_constraints.erl @@ -46,8 +46,8 @@ type_error(A) -> aeso_tc_errors:type_error(A). %% ------- -ensure_monomorphic(A, B) -> aeso_tc_type_utils:ensure_monomorphic(A, B). -ensure_first_order(A, B) -> aeso_tc_type_utils:ensure_first_order(A, B). +is_monomorphic(A) -> aeso_tc_type_utils:is_monomorphic(A). +is_first_order(A) -> aeso_tc_type_utils:is_first_order(A). app_t(A, B, C) -> aeso_tc_type_utils:app_t(A, B, C). %% ------- @@ -432,10 +432,10 @@ check_oracle_type_constraints(_Env, []) -> check_oracle_type_constraints(Env, [{oracle_type, Ann, OType} | Rest]) -> Type = unfold_types_in_type(Env, aeso_tc_type_utils:instantiate(OType)), {app_t, _, {id, _, "oracle"}, [QType, RType]} = Type, - ensure_monomorphic(QType, {invalid_oracle_type, polymorphic, query, Ann, Type}), - ensure_monomorphic(RType, {invalid_oracle_type, polymorphic, response, Ann, Type}), - ensure_first_order(QType, {invalid_oracle_type, higher_order, query, Ann, Type}), - ensure_first_order(RType, {invalid_oracle_type, higher_order, response, Ann, Type}), + is_monomorphic(QType) orelse type_error({invalid_oracle_type, polymorphic, query, Ann, Type}), + is_monomorphic(RType) orelse type_error({invalid_oracle_type, polymorphic, response, Ann, Type}), + is_first_order(QType) orelse type_error({invalid_oracle_type, higher_order, query, Ann, Type}), + is_first_order(RType) orelse type_error({invalid_oracle_type, higher_order, response, Ann, Type}), check_oracle_type_constraints(Env, Rest). %% -- Field constraints -- diff --git a/src/aeso_tc_type_utils.erl b/src/aeso_tc_type_utils.erl index 5165376..d63d9e3 100644 --- a/src/aeso_tc_type_utils.erl +++ b/src/aeso_tc_type_utils.erl @@ -5,15 +5,12 @@ , instantiate/1 , typesig_to_fun_t/1 , fun_arity/1 - , ensure_first_order/2 - , ensure_monomorphic/2 , opposite_variance/1 , app_t/3 + , is_first_order/1 + , is_monomorphic/1 ]). -typesig_to_fun_t({type_sig, Ann, _Constr, Named, Args, Res}) -> - {fun_t, Ann, Named, Args, Res}. - dereference(T = {uvar, _, R}) -> case aeso_tc_ets_manager:ets_lookup(type_vars, R) of [] -> @@ -67,17 +64,11 @@ integer_to_tvar(X) -> fun_arity({fun_t, _, _, Args, _}) -> length(Args); fun_arity(_) -> none. -ensure_monomorphic(Type, Err) -> - is_monomorphic(Type) orelse aeso_tc_errors:type_error(Err). - is_monomorphic({tvar, _, _}) -> false; is_monomorphic(Ts) when is_list(Ts) -> lists:all(fun is_monomorphic/1, Ts); is_monomorphic(Tup) when is_tuple(Tup) -> is_monomorphic(tuple_to_list(Tup)); is_monomorphic(_) -> true. -ensure_first_order(Type, Err) -> - is_first_order(Type) orelse aeso_tc_errors:type_error(Err). - is_first_order({fun_t, _, _, _, _}) -> false; is_first_order(Ts) when is_list(Ts) -> lists:all(fun is_first_order/1, Ts); is_first_order(Tup) when is_tuple(Tup) -> is_first_order(tuple_to_list(Tup)); @@ -90,3 +81,6 @@ opposite_variance(bivariant) -> bivariant. app_t(_Ann, Name, []) -> Name; app_t(Ann, Name, Args) -> {app_t, Ann, Name, Args}. + +typesig_to_fun_t({type_sig, Ann, _Constr, Named, Args, Res}) -> + {fun_t, Ann, Named, Args, Res}.