@@ -973,7 +973,7 @@ defmodule Module.Types.Descr do
973
973
For a function type, the domain is the set of valid input types.
974
974
975
975
Returns:
976
- - `:badfunction ` if the type is not a function type
976
+ - `:badfun ` if the type is not a function type
977
977
- A tuple type representing the domain for valid function types
978
978
979
979
Handles both static and dynamic function types:
@@ -990,7 +990,7 @@ defmodule Module.Types.Descr do
990
990
iex> fun_domain(fun([integer(), float()], boolean()))
991
991
domain_repr([integer(), float()])
992
992
"""
993
- def fun_domain ( :term ) , do: :badfunction
993
+ def fun_domain ( :term ) , do: :badfun
994
994
995
995
def fun_domain ( type ) do
996
996
result =
@@ -1000,7 +1000,7 @@ defmodule Module.Types.Descr do
1000
1000
with true <- fun_only? ( type ) , { :ok , domain } <- fun_domain_static ( type ) do
1001
1001
domain
1002
1002
else
1003
- _ -> :badfunction
1003
+ _ -> :badfun
1004
1004
end
1005
1005
1006
1006
{ dynamic , static } when static == @ none ->
@@ -1012,34 +1012,33 @@ defmodule Module.Types.Descr do
1012
1012
{ :ok , dynamic_domain } <- fun_domain_static ( dynamic ) do
1013
1013
union ( dynamic_domain , dynamic ( static_domain ) )
1014
1014
else
1015
- _ -> :badfunction
1015
+ _ -> :badfun
1016
1016
end
1017
1017
end
1018
1018
1019
1019
case result do
1020
- :badfunction -> :badfunction
1021
- result -> if empty? ( result ) , do: :badfunction , else: result
1020
+ :badfun -> :badfun
1021
+ result -> if empty? ( result ) , do: :badfun , else: result
1022
1022
end
1023
1023
end
1024
1024
1025
1025
# Returns {:ok, domain} if the domain of the static type is well-defined.
1026
1026
# For that, it has to contain a non-empty function type.
1027
- # Otherwise, returns :badfunction .
1027
+ # Otherwise, returns :badfun .
1028
1028
defp fun_domain_static ( % { fun: bdd } ) do
1029
1029
case fun_normalize ( bdd ) do
1030
1030
{ domain , _ , _ } -> { :ok , domain }
1031
1031
_ -> { :ok , none ( ) }
1032
1032
end
1033
1033
end
1034
1034
1035
- defp fun_domain_static ( :term ) , do: :badfunction
1035
+ defp fun_domain_static ( :term ) , do: :badfun
1036
1036
defp fun_domain_static ( % { } ) , do: { :ok , none ( ) }
1037
- defp fun_domain_static ( :empty_function ) , do: { :ok , none ( ) }
1038
1037
1039
1038
@ doc """
1040
1039
Applies a function type to a list of argument types.
1041
1040
1042
- Returns the result type if the application is valid, or `:badarguments ` if not.
1041
+ Returns the result type if the application is valid, or `:badarg ` if not.
1043
1042
1044
1043
Handles both static and dynamic function types:
1045
1044
1. For static functions: checks exact argument types
@@ -1061,14 +1060,14 @@ defmodule Module.Types.Descr do
1061
1060
atom()
1062
1061
1063
1062
iex> fun_apply(fun([integer()], atom()), [float()])
1064
- :badarguments
1063
+ :badarg
1065
1064
1066
1065
iex> fun_apply(fun([dynamic()], atom()), [dynamic()])
1067
1066
atom()
1068
1067
"""
1069
1068
def fun_apply ( fun , arguments ) do
1070
1069
if empty? ( domain_descr ( arguments ) ) do
1071
- :badarguments
1070
+ :badarg
1072
1071
else
1073
1072
case :maps . take ( :dynamic , fun ) do
1074
1073
:error -> fun_apply_with_strategy ( fun , nil , arguments )
@@ -1096,7 +1095,7 @@ defmodule Module.Types.Descr do
1096
1095
{ :ok , res2 } <- fun_apply_static ( dynamic_fun , dynamic_args ) do
1097
1096
union ( res1 , dynamic ( res2 ) )
1098
1097
else
1099
- _ -> :badarguments
1098
+ _ -> :badarg
1100
1099
end
1101
1100
end
1102
1101
end
@@ -1110,40 +1109,42 @@ defmodule Module.Types.Descr do
1110
1109
defp fun_apply_static ( % { fun: fun_bdd } , arguments ) do
1111
1110
type_args = domain_descr ( arguments )
1112
1111
1113
- if empty? ( type_args ) do
1114
- # At this stage we do not check that the function can be applied to the arguments (using domain)
1115
- with { _domain , arrows , arity } <- fun_normalize ( fun_bdd ) ,
1116
- true <- arity == length ( arguments ) do
1117
- # Opti: short-circuits when inner loop is none() or outer loop is term()
1118
- result =
1119
- Enum . reduce_while ( arrows , none ( ) , fn intersection_of_arrows , acc ->
1120
- Enum . reduce_while ( intersection_of_arrows , term ( ) , fn
1121
- { _dom , _ret } , acc when acc == @ none -> { :halt , acc }
1122
- { _dom , ret } , acc -> { :cont , intersection ( acc , ret ) }
1123
- end )
1124
- |> case do
1125
- :term -> { :halt , :term }
1126
- inner -> { :cont , union ( inner , acc ) }
1127
- end
1128
- end )
1112
+ case fun_normalize ( fun_bdd ) do
1113
+ { domain , arrows , arity } when arity == length ( arguments ) ->
1114
+ cond do
1115
+ empty? ( type_args ) ->
1116
+ # Opti: short-circuits when inner loop is none() or outer loop is term()
1117
+ result =
1118
+ Enum . reduce_while ( arrows , none ( ) , fn intersection_of_arrows , acc ->
1119
+ Enum . reduce_while ( intersection_of_arrows , term ( ) , fn
1120
+ { _dom , _ret } , acc when acc == @ none -> { :halt , acc }
1121
+ { _dom , ret } , acc -> { :cont , intersection ( acc , ret ) }
1122
+ end )
1123
+ |> case do
1124
+ :term -> { :halt , :term }
1125
+ inner -> { :cont , union ( inner , acc ) }
1126
+ end
1127
+ end )
1129
1128
1130
- { :ok , result }
1131
- else
1132
- false -> :badarity
1133
- end
1134
- else
1135
- with { domain , arrows , arity } <- fun_normalize ( fun_bdd ) ,
1136
- true <- arity == length ( arguments ) ,
1137
- true <- subtype? ( type_args , domain ) do
1138
- result =
1139
- Enum . reduce ( arrows , none ( ) , fn intersection_of_arrows , acc ->
1140
- aux_apply ( acc , type_args , term ( ) , intersection_of_arrows )
1141
- end )
1129
+ { :ok , result }
1142
1130
1143
- { :ok , result }
1144
- else
1145
- _ -> :badarguments
1146
- end
1131
+ subtype? ( type_args , domain ) ->
1132
+ result =
1133
+ Enum . reduce ( arrows , none ( ) , fn intersection_of_arrows , acc ->
1134
+ aux_apply ( acc , type_args , term ( ) , intersection_of_arrows )
1135
+ end )
1136
+
1137
+ { :ok , result }
1138
+
1139
+ true ->
1140
+ :badarg
1141
+ end
1142
+
1143
+ { _ , _ , arity } ->
1144
+ { :badarity , arity }
1145
+
1146
+ :badfun ->
1147
+ :badfun
1147
1148
end
1148
1149
end
1149
1150
@@ -1218,7 +1219,7 @@ defmodule Module.Types.Descr do
1218
1219
## Return Values
1219
1220
#
1220
1221
# - `{domain, arrows, arity}` for valid function BDDs
1221
- # - `:empty_function ` if the BDD represents an empty function type
1222
+ # - `:badfun ` if the BDD represents an empty function type
1222
1223
#
1223
1224
# ## Internal Use
1224
1225
#
@@ -1245,7 +1246,7 @@ defmodule Module.Types.Descr do
1245
1246
end
1246
1247
end )
1247
1248
1248
- if arrows == [ ] , do: :empty_function , else: { domain , arrows , arity }
1249
+ if arrows == [ ] , do: :badfun , else: { domain , arrows , arity }
1249
1250
end
1250
1251
1251
1252
# Checks if a function type is empty.
0 commit comments