package catala

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

Catala operator utilities

Resolving operators from the surface syntax proceeds in three steps:

  • During desugaring, the operators may remain untyped (with 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 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, these types are leveraged to resolve the overloaded operators to their concrete, monomorphic counterparts
type monomorphic = < monomorphic : Shared_ast__.Definitions.yes >

Operands and return types of the operator are fixed

type polymorphic = < polymorphic : Shared_ast__.Definitions.yes >

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.

type overloaded = < overloaded : Shared_ast__.Definitions.yes >

The operator is ambiguous and requires the types of its arguments to be known before it can be typed, using a pre-defined table

type resolved = < resolved : Shared_ast__.Definitions.yes >

Explicit monomorphic versions of the overloaded operators

type _ t =
  1. | Not : < monomorphic.. > t
  2. | GetDay : < monomorphic.. > t
  3. | GetMonth : < monomorphic.. > t
  4. | GetYear : < monomorphic.. > t
  5. | FirstDayOfMonth : < monomorphic.. > t
  6. | LastDayOfMonth : < monomorphic.. > t
  7. | Length : < polymorphic.. > t
  8. | ToClosureEnv : < polymorphic.. > t
  9. | FromClosureEnv : < polymorphic.. > t
  10. | Minus : < overloaded.. > t
  11. | Minus_int : < resolved.. > t
  12. | Minus_rat : < resolved.. > t
  13. | Minus_mon : < resolved.. > t
  14. | Minus_dur : < resolved.. > t
  15. | ToRat : < overloaded.. > t
  16. | ToRat_int : < resolved.. > t
  17. | ToRat_mon : < resolved.. > t
  18. | ToMoney : < overloaded.. > t
  19. | ToMoney_rat : < resolved.. > t
  20. | Round : < overloaded.. > t
  21. | Round_rat : < resolved.. > t
  22. | Round_mon : < resolved.. > t
  23. | And : < monomorphic.. > t
  24. | Or : < monomorphic.. > t
  25. | Xor : < monomorphic.. > t
  26. | Eq : < polymorphic.. > t
  27. | Map : < polymorphic.. > t
  28. | Map2 : < polymorphic.. > t
  29. | Concat : < polymorphic.. > t
  30. | Filter : < polymorphic.. > t
  31. | Add : < overloaded.. > t
  32. | Add_int_int : < resolved.. > t
  33. | Add_rat_rat : < resolved.. > t
  34. | Add_mon_mon : < resolved.. > t
  35. | Add_dat_dur : Runtime_ocaml.Runtime.date_rounding -> < resolved.. > t
  36. | Add_dur_dur : < resolved.. > t
  37. | Sub : < overloaded.. > t
  38. | Sub_int_int : < resolved.. > t
  39. | Sub_rat_rat : < resolved.. > t
  40. | Sub_mon_mon : < resolved.. > t
  41. | Sub_dat_dat : < resolved.. > t
  42. | Sub_dat_dur : < resolved.. > t
  43. | Sub_dur_dur : < resolved.. > t
  44. | Mult : < overloaded.. > t
  45. | Mult_int_int : < resolved.. > t
  46. | Mult_rat_rat : < resolved.. > t
  47. | Mult_mon_rat : < resolved.. > t
  48. | Mult_dur_int : < resolved.. > t
  49. | Div : < overloaded.. > t
  50. | Div_int_int : < resolved.. > t
  51. | Div_rat_rat : < resolved.. > t
  52. | Div_mon_rat : < resolved.. > t
  53. | Div_mon_mon : < resolved.. > t
  54. | Div_dur_dur : < resolved.. > t
  55. | Lt : < overloaded.. > t
  56. | Lt_int_int : < resolved.. > t
  57. | Lt_rat_rat : < resolved.. > t
  58. | Lt_mon_mon : < resolved.. > t
  59. | Lt_dat_dat : < resolved.. > t
  60. | Lt_dur_dur : < resolved.. > t
  61. | Lte : < overloaded.. > t
  62. | Lte_int_int : < resolved.. > t
  63. | Lte_rat_rat : < resolved.. > t
  64. | Lte_mon_mon : < resolved.. > t
  65. | Lte_dat_dat : < resolved.. > t
  66. | Lte_dur_dur : < resolved.. > t
  67. | Gt : < overloaded.. > t
  68. | Gt_int_int : < resolved.. > t
  69. | Gt_rat_rat : < resolved.. > t
  70. | Gt_mon_mon : < resolved.. > t
  71. | Gt_dat_dat : < resolved.. > t
  72. | Gt_dur_dur : < resolved.. > t
  73. | Gte : < overloaded.. > t
  74. | Gte_int_int : < resolved.. > t
  75. | Gte_rat_rat : < resolved.. > t
  76. | Gte_mon_mon : < resolved.. > t
  77. | Gte_dat_dat : < resolved.. > t
  78. | Gte_dur_dur : < resolved.. > t
  79. | Eq_int_int : < resolved.. > t
  80. | Eq_rat_rat : < resolved.. > t
  81. | Eq_mon_mon : < resolved.. > t
  82. | Eq_dur_dur : < resolved.. > t
  83. | Eq_dat_dat : < resolved.. > t
  84. | Reduce : < polymorphic.. > t
  85. | Fold : < polymorphic.. > t
  86. | HandleDefault : < polymorphic.. > t
  87. | HandleDefaultOpt : < polymorphic.. > t
val equal : 'a1 t -> 'a2 t -> bool
val compare : 'a1 t -> 'a2 t -> int
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 Catala_utils.Mark.pos -> 'b) -> monomorphic: (< monomorphic : Shared_ast__.Definitions.yes.. > t Catala_utils.Mark.pos -> 'b) -> ?overloaded: (< overloaded : Shared_ast__.Definitions.yes.. > t Catala_utils.Mark.pos -> 'b) -> ?resolved: (< resolved : Shared_ast__.Definitions.yes.. > t Catala_utils.Mark.pos -> 'b) -> 'a t Catala_utils.Mark.pos -> 'b

Calls one of the supplied functions depending on the kind of the operator

type 'a no_overloads = < overloaded : Shared_ast__.Definitions.no ; monomorphic : Shared_ast__.Definitions.yes ; polymorphic : Shared_ast__.Definitions.yes ; resolved : Shared_ast__.Definitions.yes.. > as 'a

An identity function that allows translating an operator between different passes that don't change operator types

Getting the types of operators

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.

  • raises a

    detailed user error if no matching operator can be found

Polymorphic operators are typed directly within Typing, since their types may contain type variables that can't be expressed outside of it

Overload handling

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 Catala_utils.Mark.pos * [ `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.

OCaml

Innovation. Community. Security.