Shared_ast.Operator
Resolving operators from the surface syntax proceeds in three steps:
TAny
) or, if they have an explicit type suffix (e.g. the $
for "money" in +$
), their operands types are already explicited in the EOp
expression node.Shared_ast.Typing
Typing for the default calculus. Because of the error terms, we perform type inference using the classical W algorithm with union-find unification.will then enforce these constraints in addition to the known built-in type for each operator (e.g. Eq: 'a -> 'a -> 'a
isn't encoded in the first-order AST types).
Finally, during
Scopelang.From_desugared
Translation from Desugared.Ast
to Scopelang.Ast
, these types are leveraged to resolve the overloaded operators to their concrete, monomorphic counterparts
Operands and return types of the operator are fixed
The operator is truly polymorphic: it's the same runtime function that may work on multiple types. We require that resolving the argument types from right to left trivially resolves all type variables declared in the operator type.
The operator is ambiguous and requires the types of its arguments to be known before it can be typed, using a pre-defined table
Explicit monomorphic versions of the overloaded operators
type _ t =
| Not : < monomorphic.. > t |
| GetDay : < monomorphic.. > t |
| GetMonth : < monomorphic.. > t |
| GetYear : < monomorphic.. > t |
| FirstDayOfMonth : < monomorphic.. > t |
| LastDayOfMonth : < monomorphic.. > t |
| Length : < polymorphic.. > t |
| Minus : < overloaded.. > t |
| Minus_int : < resolved.. > t |
| Minus_rat : < resolved.. > t |
| Minus_mon : < resolved.. > t |
| Minus_dur : < resolved.. > t |
| ToRat : < overloaded.. > t |
| ToRat_int : < resolved.. > t |
| ToRat_mon : < resolved.. > t |
| ToMoney : < overloaded.. > t |
| ToMoney_rat : < resolved.. > t |
| Round : < overloaded.. > t |
| Round_rat : < resolved.. > t |
| Round_mon : < resolved.. > t |
| And : < monomorphic.. > t |
| Or : < monomorphic.. > t |
| Xor : < monomorphic.. > t |
| Eq : < polymorphic.. > t |
| Map : < polymorphic.. > t |
| Concat : < polymorphic.. > t |
| Filter : < polymorphic.. > t |
| Reduce : < polymorphic.. > t |
| Add : < overloaded.. > t |
| Add_int_int : < resolved.. > t |
| Add_rat_rat : < resolved.. > t |
| Add_mon_mon : < resolved.. > t |
| Add_dat_dur : Runtime_ocaml.Runtime.date_rounding -> < resolved.. > t |
| Add_dur_dur : < resolved.. > t |
| Sub : < overloaded.. > t |
| Sub_int_int : < resolved.. > t |
| Sub_rat_rat : < resolved.. > t |
| Sub_mon_mon : < resolved.. > t |
| Sub_dat_dat : < resolved.. > t |
| Sub_dat_dur : < resolved.. > t |
| Sub_dur_dur : < resolved.. > t |
| Mult : < overloaded.. > t |
| Mult_int_int : < resolved.. > t |
| Mult_rat_rat : < resolved.. > t |
| Mult_mon_rat : < resolved.. > t |
| Mult_dur_int : < resolved.. > t |
| Div : < overloaded.. > t |
| Div_int_int : < resolved.. > t |
| Div_rat_rat : < resolved.. > t |
| Div_mon_rat : < resolved.. > t |
| Div_mon_mon : < resolved.. > t |
| Div_dur_dur : < resolved.. > t |
| Lt : < overloaded.. > t |
| Lt_int_int : < resolved.. > t |
| Lt_rat_rat : < resolved.. > t |
| Lt_mon_mon : < resolved.. > t |
| Lt_dat_dat : < resolved.. > t |
| Lt_dur_dur : < resolved.. > t |
| Lte : < overloaded.. > t |
| Lte_int_int : < resolved.. > t |
| Lte_rat_rat : < resolved.. > t |
| Lte_mon_mon : < resolved.. > t |
| Lte_dat_dat : < resolved.. > t |
| Lte_dur_dur : < resolved.. > t |
| Gt : < overloaded.. > t |
| Gt_int_int : < resolved.. > t |
| Gt_rat_rat : < resolved.. > t |
| Gt_mon_mon : < resolved.. > t |
| Gt_dat_dat : < resolved.. > t |
| Gt_dur_dur : < resolved.. > t |
| Gte : < overloaded.. > t |
| Gte_int_int : < resolved.. > t |
| Gte_rat_rat : < resolved.. > t |
| Gte_mon_mon : < resolved.. > t |
| Gte_dat_dat : < resolved.. > t |
| Gte_dur_dur : < resolved.. > t |
| Eq_int_int : < resolved.. > t |
| Eq_rat_rat : < resolved.. > t |
| Eq_mon_mon : < resolved.. > t |
| Eq_dur_dur : < resolved.. > t |
| Eq_dat_dat : < resolved.. > t |
| Fold : < polymorphic.. > t |
| HandleDefault : < polymorphic.. > t |
| HandleDefaultOpt : < polymorphic.. > t |
val name : 'a t -> string
Returns the operator name as a valid ident starting with a lowercase character. This is different from Print.operator which returns operator symbols, e.g. +$
.
val kind_dispatch :
polymorphic:( < polymorphic : Shared_ast__Definitions.yes.. > t -> 'b ) ->
monomorphic:( < monomorphic : Shared_ast__Definitions.yes.. > t -> 'b ) ->
?overloaded:( < overloaded : Shared_ast__Definitions.yes.. > t -> 'b ) ->
?resolved:( < resolved : Shared_ast__Definitions.yes.. > t -> 'b ) ->
'a t ->
'b
Calls one of the supplied functions depending on the kind of the operator
val translate : 'a no_overloads t -> 'b no_overloads t
An identity function that allows translating an operator between different passes that don't change operator types
val monomorphic_type :
monomorphic t Catala_utils.Mark.pos ->
Shared_ast__Definitions.naked_typ Catala_utils.Mark.pos
val resolved_type :
resolved t Catala_utils.Mark.pos ->
Shared_ast__Definitions.naked_typ Catala_utils.Mark.pos
val overload_type :
Shared_ast__Definitions.decl_ctx ->
overloaded t Catala_utils.Mark.pos ->
Shared_ast__Definitions.naked_typ Catala_utils.Mark.pos list ->
Shared_ast__Definitions.naked_typ Catala_utils.Mark.pos
The type for typing overloads is different since the types of the operands are required in advance.
Polymorphic operators are typed directly within Typing
, since their types may contain type variables that can't be expressed outside of it
val resolve_overload :
Shared_ast__Definitions.decl_ctx ->
overloaded t Catala_utils.Mark.pos ->
Shared_ast__Definitions.naked_typ Catala_utils.Mark.pos list ->
< resolved : Shared_ast__Definitions.yes.. > t * [ `Straight | `Reversed ]
Some overloads are sugar for an operation with reversed operands, e.g. TRat * TMoney
is using mult_mon_rat
. `Reversed
is returned to signify this case.