Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria, contributor:
|
|
|
|
Louis Gesbert <louis.gesbert@inria.fr>
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
License for the specific language governing permissions and limitations under
|
|
|
|
the License. *)
|
|
|
|
|
|
|
|
(** {1 Catala operator utilities} *)
|
|
|
|
|
2022-12-13 13:51:22 +03:00
|
|
|
(** 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.
|
|
|
|
- {!modules: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 {!modules:Scopelang.From_desugared}, these types are
|
|
|
|
leveraged to resolve the overloaded operators to their concrete,
|
|
|
|
monomorphic counterparts
|
|
|
|
*)
|
|
|
|
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
open Catala_utils
|
|
|
|
open Definitions
|
|
|
|
include module type of Definitions.Op
|
|
|
|
|
2023-03-30 16:30:08 +03:00
|
|
|
val equal : 'a1 t -> 'a2 t -> bool
|
|
|
|
val compare : 'a1 t -> 'a2 t -> int
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
|
2023-03-30 16:30:08 +03:00
|
|
|
val name : 'a t -> string
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
(** 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 :
|
2023-04-04 12:10:59 +03:00
|
|
|
polymorphic:(< polymorphic : yes ; .. > t -> 'b) ->
|
|
|
|
monomorphic:(< monomorphic : yes ; .. > t -> 'b) ->
|
|
|
|
?overloaded:(< overloaded : yes ; .. > t -> 'b) ->
|
|
|
|
?resolved:(< resolved : yes ; .. > t -> 'b) ->
|
2023-03-30 16:30:08 +03:00
|
|
|
'a t ->
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
'b
|
|
|
|
(** Calls one of the supplied functions depending on the kind of the operator *)
|
|
|
|
|
2023-03-27 17:55:52 +03:00
|
|
|
type 'a no_overloads =
|
2023-04-04 12:10:59 +03:00
|
|
|
< overloaded : no
|
|
|
|
; monomorphic : yes
|
|
|
|
; polymorphic : yes
|
|
|
|
; resolved : yes
|
|
|
|
; .. >
|
|
|
|
as
|
|
|
|
'a
|
|
|
|
|
|
|
|
val translate : 'a no_overloads t -> 'b no_overloads t
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
(** An identity function that allows translating an operator between different
|
|
|
|
passes that don't change operator types *)
|
|
|
|
|
|
|
|
(** {2 Getting the types of operators} *)
|
|
|
|
|
2023-03-30 16:30:08 +03:00
|
|
|
val monomorphic_type : monomorphic t Marked.pos -> typ
|
|
|
|
val resolved_type : resolved t Marked.pos -> typ
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
|
2023-03-30 16:30:08 +03:00
|
|
|
val overload_type : decl_ctx -> overloaded t Marked.pos -> typ list -> typ
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
(** The type for typing overloads is different since the types of the operands
|
|
|
|
are required in advance.
|
|
|
|
|
|
|
|
@raise 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*)
|
|
|
|
|
|
|
|
(** {2 Overload handling} *)
|
|
|
|
|
|
|
|
val resolve_overload :
|
|
|
|
decl_ctx ->
|
2023-03-30 16:30:08 +03:00
|
|
|
overloaded t Marked.pos ->
|
Add overloaded operators for the common operations
This uses the same disambiguation mechanism put in place for
structures, calling the typer on individual rules on the desugared AST
to propagate types, in order to resolve ambiguous operators like `+`
to their strongly typed counterparts (`+!`, `+.`, `+$`, `+@`, `+$`) in
the translation to scopelang.
The patch includes some normalisation of the definition of all the
operators, and classifies them based on their typing policy instead of
their arity. It also adds a little more flexibility:
- a couple new operators, like `-` on date and duration
- optional type annotation on some aggregation constructions
The `Shared_ast` lib is also lightly restructured, with the `Expr`
module split into `Type`, `Operator` and `Expr`.
2022-11-29 11:47:53 +03:00
|
|
|
typ list ->
|
2023-04-04 12:10:59 +03:00
|
|
|
< resolved : yes ; .. > t * [ `Straight | `Reversed ]
|
2022-12-13 13:51:22 +03:00
|
|
|
(** 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. *)
|