package catala

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

Module Shared_ast.OperatorSource

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
Sourcetype monomorphic = < monomorphic : Shared_ast__.Definitions.yes >

Operands and return types of the operator are fixed

Sourcetype 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.

Sourcetype 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

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

Explicit monomorphic versions of the overloaded operators

Sourcetype _ 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. | Log : Shared_ast__.Definitions.log_entry * Catala_utils.Uid.MarkedString.info list -> < polymorphic.. > t
  9. | ToClosureEnv : < polymorphic.. > t
  10. | FromClosureEnv : < polymorphic.. > t
  11. | Minus : < overloaded.. > t
  12. | Minus_int : < resolved.. > t
  13. | Minus_rat : < resolved.. > t
  14. | Minus_mon : < resolved.. > t
  15. | Minus_dur : < resolved.. > t
  16. | ToRat : < overloaded.. > t
  17. | ToRat_int : < resolved.. > t
  18. | ToRat_mon : < resolved.. > t
  19. | ToMoney : < overloaded.. > t
  20. | ToMoney_rat : < resolved.. > t
  21. | Round : < overloaded.. > t
  22. | Round_rat : < resolved.. > t
  23. | Round_mon : < resolved.. > t
  24. | And : < monomorphic.. > t
  25. | Or : < monomorphic.. > t
  26. | Xor : < monomorphic.. > t
  27. | Eq : < polymorphic.. > t
  28. | Map : < 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
Sourceval equal : 'a1 t -> 'a2 t -> bool
Sourceval compare : 'a1 t -> 'a2 t -> int
Sourceval 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. +$.

Sourceval 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

Sourcetype '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
Sourceval 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

Getting the types of operators

Sourceval monomorphic_type : monomorphic t Catala_utils.Mark.pos -> Shared_ast__.Definitions.naked_typ Catala_utils.Mark.pos
Sourceval resolved_type : resolved t Catala_utils.Mark.pos -> Shared_ast__.Definitions.naked_typ Catala_utils.Mark.pos
Sourceval 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

Sourceval 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.

OCaml

Innovation. Community. Security.