2021-02-12 19:20:14 +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:
|
|
|
|
Denis Merigoux <denis.merigoux@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. *)
|
|
|
|
|
|
|
|
(** Typing for the default calculus. Because of the error terms, we perform type
|
|
|
|
inference using the classical W algorithm with union-find unification. *)
|
|
|
|
|
2022-09-13 16:20:13 +03:00
|
|
|
open Definitions
|
2022-08-12 23:42:39 +03:00
|
|
|
|
2022-09-26 17:32:02 +03:00
|
|
|
module Env : sig
|
|
|
|
type 'e t
|
|
|
|
|
2024-02-07 19:41:04 +03:00
|
|
|
val empty : ?fail_on_any:bool -> ?assume_op_types:bool -> decl_ctx -> 'e t
|
|
|
|
(** The [~fail_on_any] labeled parameter controls the behavior of the typer in
|
|
|
|
the case where polymorphic expressions are still found after typing: if
|
|
|
|
[false], it allows them (giving them [TAny] and losing typing
|
|
|
|
information); if set to [true] (the default), it aborts.
|
|
|
|
|
|
|
|
The [~assume_op_types] flag (default false) ignores the expected built-in
|
|
|
|
types of polymorphic operators, and will assume correct the type
|
|
|
|
information included in [EAppOp] nodes. This is useful after
|
|
|
|
monomorphisation, which changes the expected types for these operators. *)
|
|
|
|
|
2022-09-26 17:32:02 +03:00
|
|
|
val add_var : 'e Var.t -> typ -> 'e t -> 'e t
|
2023-02-13 17:00:23 +03:00
|
|
|
val add_toplevel_var : TopdefName.t -> typ -> 'e t -> 'e t
|
2022-09-26 17:32:02 +03:00
|
|
|
val add_scope_var : ScopeVar.t -> typ -> 'e t -> 'e t
|
2023-11-07 20:25:57 +03:00
|
|
|
|
2023-11-03 19:15:55 +03:00
|
|
|
val add_scope :
|
2023-11-07 20:25:57 +03:00
|
|
|
ScopeName.t ->
|
|
|
|
vars:typ ScopeVar.Map.t ->
|
|
|
|
in_vars:typ ScopeVar.Map.t ->
|
|
|
|
'e t ->
|
|
|
|
'e t
|
|
|
|
|
2022-11-28 18:23:27 +03:00
|
|
|
val open_scope : ScopeName.t -> 'e t -> 'e t
|
2023-08-31 17:54:45 +03:00
|
|
|
|
|
|
|
val dump : Format.formatter -> 'e t -> unit
|
|
|
|
(** For debug purposes *)
|
2022-09-26 17:32:02 +03:00
|
|
|
end
|
2022-09-26 13:12:39 +03:00
|
|
|
|
|
|
|
val expr :
|
|
|
|
decl_ctx ->
|
2022-09-26 17:32:02 +03:00
|
|
|
?env:'e Env.t ->
|
2022-09-26 13:12:39 +03:00
|
|
|
?typ:typ ->
|
2023-05-17 17:15:00 +03:00
|
|
|
(('a, 'm) gexpr as 'e) ->
|
|
|
|
('a, typed) boxed_gexpr
|
2022-09-26 13:12:39 +03:00
|
|
|
(** Infers and marks the types for the given expression. If [typ] is provided,
|
2022-09-27 19:23:10 +03:00
|
|
|
it is assumed to be the outer type and used for inference top-down.
|
2022-09-26 13:12:39 +03:00
|
|
|
|
2022-09-27 19:23:10 +03:00
|
|
|
If the input expression already has type annotations, the full inference is
|
|
|
|
still done, but with unification with the existing annotations at every
|
|
|
|
step. This can be used for double-checking after AST transformations and
|
|
|
|
filling the gaps ([TAny]) if any. Use [Expr.untype] first if this is not
|
2023-11-20 18:01:06 +03:00
|
|
|
what you want.
|
|
|
|
|
2024-01-08 14:09:12 +03:00
|
|
|
Note that typing also transparently performs the following changes to the
|
|
|
|
AST nodes, outside of typing annotations:
|
2023-12-18 12:25:00 +03:00
|
|
|
- disambiguation of constructors: [EDStructAccess] nodes are translated into
|
|
|
|
[EStructAccess] with the suitable structure and field idents (this only
|
|
|
|
concerns [desugared] expressions).
|
2024-04-12 12:36:43 +03:00
|
|
|
- disambiguation of structure names in [EDStructAmend] nodes ([desugared] as
|
|
|
|
well)
|
|
|
|
- resolution of tuple size (when equal to 0) on [ETupleAccess] nodes
|
2023-12-18 12:25:00 +03:00
|
|
|
- resolution of operator types, which are stored (monomorphised) back in the
|
2024-01-08 14:09:12 +03:00
|
|
|
[EAppOp] nodes
|
|
|
|
- resolution of function application input types on the [EApp] nodes, when
|
|
|
|
that was originally empty ([[]]): this documents the arity of the function
|
|
|
|
application, taking de-tuplification into account.
|
|
|
|
- [TAny] appearing within nodes are refined to more precise types, e.g. on
|
|
|
|
`EAbs` nodes (but be careful with this, it may only work for specific
|
2024-02-07 19:41:04 +03:00
|
|
|
structures of generated code ; having [~fail_on_any:true] set in the
|
|
|
|
environment (this is the default) checks that it didn't cause problems) *)
|
2022-09-27 19:23:10 +03:00
|
|
|
|
2022-11-28 18:23:27 +03:00
|
|
|
val check_expr :
|
|
|
|
decl_ctx ->
|
|
|
|
?env:'e Env.t ->
|
|
|
|
?typ:typ ->
|
2023-05-17 17:15:00 +03:00
|
|
|
(('a, 'm) gexpr as 'e) ->
|
|
|
|
('a, untyped) boxed_gexpr
|
2022-11-28 18:23:27 +03:00
|
|
|
(** Same as [expr], but doesn't annotate the returned expression. Equivalent to
|
|
|
|
[Typing.expr |> Expr.untype], but more efficient. This can be useful for
|
|
|
|
type-checking and disambiguation (some AST nodes are updated with missing
|
|
|
|
information, e.g. any [TAny] appearing in the AST is replaced) *)
|
|
|
|
|
2022-12-13 18:06:36 +03:00
|
|
|
val program :
|
2024-02-07 19:41:04 +03:00
|
|
|
?fail_on_any:bool ->
|
|
|
|
?assume_op_types:bool ->
|
2024-01-17 18:03:20 +03:00
|
|
|
('a, 'm) gexpr program ->
|
|
|
|
('a, typed) gexpr program
|
2022-09-26 17:32:02 +03:00
|
|
|
(** Typing on whole programs (as defined in Shared_ast.program, i.e. for the
|
2022-09-27 19:23:10 +03:00
|
|
|
later dcalc/lcalc stages.
|
|
|
|
|
|
|
|
Any existing type annotations are checked for unification. Use
|
|
|
|
[Program.untype] to remove them beforehand if this is not the desired
|
|
|
|
behaviour. *)
|