2020-11-22 22:56:27 +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-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2022-09-13 16:20:13 +03:00
|
|
|
module A = Definitions
|
2022-07-28 11:36:36 +03:00
|
|
|
|
|
|
|
module Any =
|
2022-11-21 12:46:17 +03:00
|
|
|
Uid.Make
|
2022-07-28 11:36:36 +03:00
|
|
|
(struct
|
|
|
|
type info = unit
|
|
|
|
|
2022-08-17 17:14:14 +03:00
|
|
|
let to_string _ = "any"
|
2022-11-21 12:46:17 +03:00
|
|
|
let format fmt () = Format.fprintf fmt "any"
|
2022-08-25 13:09:51 +03:00
|
|
|
let equal _ _ = true
|
|
|
|
let compare _ _ = 0
|
2022-07-28 11:36:36 +03:00
|
|
|
end)
|
|
|
|
()
|
|
|
|
|
2023-05-17 16:44:57 +03:00
|
|
|
type unionfind_typ = naked_typ Mark.pos UnionFind.elem
|
2023-06-13 11:48:21 +03:00
|
|
|
(** We do not reuse {!type: A.typ} because we have to include a new [TAny]
|
|
|
|
variant. Indeed, error terms can have any type and this has to be captured
|
|
|
|
by the type sytem. *)
|
2022-07-28 11:36:36 +03:00
|
|
|
|
2022-08-25 18:29:00 +03:00
|
|
|
and naked_typ =
|
2022-07-28 11:36:36 +03:00
|
|
|
| TLit of A.typ_lit
|
2023-01-11 13:23:08 +03:00
|
|
|
| TArrow of unionfind_typ list * unionfind_typ
|
2022-08-23 16:23:52 +03:00
|
|
|
| TTuple of unionfind_typ list
|
|
|
|
| TStruct of A.StructName.t
|
|
|
|
| TEnum of A.EnumName.t
|
|
|
|
| TOption of unionfind_typ
|
2022-07-28 11:36:36 +03:00
|
|
|
| TArray of unionfind_typ
|
|
|
|
| TAny of Any.t
|
2023-06-15 12:11:56 +03:00
|
|
|
| TClosureEnv
|
2022-07-28 11:36:36 +03:00
|
|
|
|
2022-12-13 18:06:36 +03:00
|
|
|
let rec typ_to_ast ~leave_unresolved (ty : unionfind_typ) : A.typ =
|
|
|
|
let typ_to_ast = typ_to_ast ~leave_unresolved in
|
2022-07-28 11:36:36 +03:00
|
|
|
let ty, pos = UnionFind.get (UnionFind.find ty) in
|
|
|
|
match ty with
|
2022-08-22 19:53:30 +03:00
|
|
|
| TLit l -> A.TLit l, pos
|
2022-08-23 16:23:52 +03:00
|
|
|
| TTuple ts -> A.TTuple (List.map typ_to_ast ts), pos
|
|
|
|
| TStruct s -> A.TStruct s, pos
|
|
|
|
| TEnum e -> A.TEnum e, pos
|
|
|
|
| TOption t -> A.TOption (typ_to_ast t), pos
|
2023-01-11 13:23:08 +03:00
|
|
|
| TArrow (t1, t2) -> A.TArrow (List.map typ_to_ast t1, typ_to_ast t2), pos
|
2022-08-22 19:53:30 +03:00
|
|
|
| TArray t1 -> A.TArray (typ_to_ast t1), pos
|
2022-10-12 14:09:22 +03:00
|
|
|
| TAny _ ->
|
2022-12-13 18:06:36 +03:00
|
|
|
if leave_unresolved then A.TAny, pos
|
2022-11-28 18:23:27 +03:00
|
|
|
else
|
|
|
|
(* No polymorphism in Catala: type inference should return full types
|
|
|
|
without wildcards, and this function is used to recover the types after
|
|
|
|
typing. *)
|
2023-05-30 16:41:49 +03:00
|
|
|
Messages.raise_spanned_error pos
|
2022-11-28 18:23:27 +03:00
|
|
|
"Internal error: typing at this point could not be resolved"
|
2023-06-15 12:11:56 +03:00
|
|
|
| TClosureEnv -> TClosureEnv, pos
|
2022-11-28 18:23:27 +03:00
|
|
|
|
2022-08-25 18:29:00 +03:00
|
|
|
let rec ast_to_typ (ty : A.typ) : unionfind_typ =
|
2022-07-28 11:36:36 +03:00
|
|
|
let ty' =
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove ty with
|
2022-08-22 19:53:30 +03:00
|
|
|
| A.TLit l -> TLit l
|
2023-01-11 13:23:08 +03:00
|
|
|
| A.TArrow (t1, t2) -> TArrow (List.map ast_to_typ t1, ast_to_typ t2)
|
2022-08-23 16:23:52 +03:00
|
|
|
| A.TTuple ts -> TTuple (List.map ast_to_typ ts)
|
|
|
|
| A.TStruct s -> TStruct s
|
|
|
|
| A.TEnum e -> TEnum e
|
|
|
|
| A.TOption t -> TOption (ast_to_typ t)
|
2022-08-22 19:53:30 +03:00
|
|
|
| A.TArray t -> TArray (ast_to_typ t)
|
|
|
|
| A.TAny -> TAny (Any.fresh ())
|
2023-06-15 12:11:56 +03:00
|
|
|
| A.TClosureEnv -> TClosureEnv
|
2022-07-28 11:36:36 +03:00
|
|
|
in
|
2023-05-17 16:44:57 +03:00
|
|
|
UnionFind.make (Mark.copy ty ty')
|
2020-11-22 22:56:27 +03:00
|
|
|
|
2020-12-14 20:09:38 +03:00
|
|
|
(** {1 Types and unification} *)
|
|
|
|
|
2022-08-25 18:29:00 +03:00
|
|
|
let typ_needs_parens (t : unionfind_typ) : bool =
|
2020-12-30 00:26:10 +03:00
|
|
|
let t = UnionFind.get (UnionFind.find t) in
|
2023-06-13 10:19:14 +03:00
|
|
|
match Mark.remove t with TArrow _ | TArray _ -> true | _ -> false
|
|
|
|
|
|
|
|
let with_color f color fmt x =
|
|
|
|
(* equivalent to [Format.fprintf fmt "@{<color>%s@}" s] *)
|
|
|
|
Format.pp_open_stag fmt Ocolor_format.(Ocolor_style_tag (Fg (C4 color)));
|
|
|
|
f fmt x;
|
|
|
|
Format.pp_close_stag fmt ()
|
|
|
|
|
|
|
|
let pp_color_string = with_color Format.pp_print_string
|
2020-11-22 22:56:27 +03:00
|
|
|
|
2021-01-14 02:17:24 +03:00
|
|
|
let rec format_typ
|
2022-08-12 23:42:39 +03:00
|
|
|
(ctx : A.decl_ctx)
|
2023-06-13 10:19:14 +03:00
|
|
|
~(colors : Ocolor_types.color4 list)
|
2021-01-14 02:17:24 +03:00
|
|
|
(fmt : Format.formatter)
|
2022-08-25 18:29:00 +03:00
|
|
|
(naked_typ : unionfind_typ) : unit =
|
2021-01-14 02:17:24 +03:00
|
|
|
let format_typ = format_typ ctx in
|
2023-06-13 10:19:14 +03:00
|
|
|
let format_typ_with_parens
|
|
|
|
~colors
|
|
|
|
(fmt : Format.formatter)
|
|
|
|
(t : unionfind_typ) =
|
|
|
|
if typ_needs_parens t then (
|
|
|
|
Format.pp_open_hvbox fmt 1;
|
|
|
|
pp_color_string (List.hd colors) fmt "(";
|
|
|
|
format_typ ~colors:(List.tl colors) fmt t;
|
|
|
|
Format.pp_close_box fmt ();
|
|
|
|
pp_color_string (List.hd colors) fmt ")")
|
|
|
|
else Format.fprintf fmt "%a" (format_typ ~colors) t
|
2020-12-30 00:26:10 +03:00
|
|
|
in
|
2022-08-25 18:29:00 +03:00
|
|
|
let naked_typ = UnionFind.get (UnionFind.find naked_typ) in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove naked_typ with
|
2022-09-13 16:20:13 +03:00
|
|
|
| TLit l -> Format.fprintf fmt "%a" Print.tlit l
|
2022-08-23 16:23:52 +03:00
|
|
|
| TTuple ts ->
|
2023-06-13 10:19:14 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a%a%a@]"
|
|
|
|
(pp_color_string (List.hd colors))
|
|
|
|
"("
|
2021-01-14 02:17:24 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt "@ *@ ")
|
2023-06-13 10:19:14 +03:00
|
|
|
(fun fmt t ->
|
|
|
|
Format.fprintf fmt "%a" (format_typ ~colors:(List.tl colors)) t))
|
2020-12-03 22:11:41 +03:00
|
|
|
ts
|
2023-06-13 10:19:14 +03:00
|
|
|
(pp_color_string (List.hd colors))
|
|
|
|
")"
|
2022-08-23 16:23:52 +03:00
|
|
|
| TStruct s -> Format.fprintf fmt "%a" A.StructName.format_t s
|
|
|
|
| TEnum e -> Format.fprintf fmt "%a" A.EnumName.format_t e
|
|
|
|
| TOption t ->
|
2023-06-13 10:19:14 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>option %a@]"
|
|
|
|
(format_typ_with_parens ~colors:(List.tl colors))
|
|
|
|
t
|
2023-02-22 13:52:22 +03:00
|
|
|
| TArrow ([t1], t2) ->
|
2023-06-13 10:19:14 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a@ →@ %a@]"
|
|
|
|
(format_typ_with_parens ~colors)
|
|
|
|
t1 (format_typ ~colors) t2
|
2020-12-30 00:26:10 +03:00
|
|
|
| TArrow (t1, t2) ->
|
2023-06-13 10:19:14 +03:00
|
|
|
Format.fprintf fmt "@[<hov 2>%a%a%a@ →@ %a@]"
|
|
|
|
(pp_color_string (List.hd colors))
|
|
|
|
"("
|
2023-01-11 13:23:08 +03:00
|
|
|
(Format.pp_print_list
|
2023-02-22 13:52:22 +03:00
|
|
|
~pp_sep:(fun fmt () -> Format.fprintf fmt ",@ ")
|
2023-06-13 10:19:14 +03:00
|
|
|
(format_typ_with_parens ~colors:(List.tl colors)))
|
|
|
|
t1
|
|
|
|
(pp_color_string (List.hd colors))
|
|
|
|
")" (format_typ ~colors) t2
|
2022-09-26 15:29:15 +03:00
|
|
|
| TArray t1 -> (
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove (UnionFind.get (UnionFind.find t1)) with
|
2022-11-28 18:23:27 +03:00
|
|
|
| TAny _ when not !Cli.debug_flag -> Format.pp_print_string fmt "collection"
|
2023-06-13 10:19:14 +03:00
|
|
|
| _ -> Format.fprintf fmt "@[collection@ %a@]" (format_typ ~colors) t1)
|
2022-11-28 18:23:27 +03:00
|
|
|
| TAny v ->
|
|
|
|
if !Cli.debug_flag then Format.fprintf fmt "<a%d>" (Any.hash v)
|
|
|
|
else Format.pp_print_string fmt "<any>"
|
2023-06-15 12:11:56 +03:00
|
|
|
| TClosureEnv -> Format.fprintf fmt "closure_env"
|
2020-11-22 22:56:27 +03:00
|
|
|
|
2023-06-13 10:19:14 +03:00
|
|
|
let rec colors =
|
|
|
|
let open Ocolor_types in
|
|
|
|
blue :: cyan :: green :: yellow :: red :: magenta :: colors
|
|
|
|
|
|
|
|
let format_typ ctx fmt naked_typ = format_typ ctx ~colors fmt naked_typ
|
|
|
|
|
2022-08-26 12:06:00 +03:00
|
|
|
exception Type_error of A.any_expr * unionfind_typ * unionfind_typ
|
2022-07-11 12:32:23 +03:00
|
|
|
|
2022-09-16 18:33:09 +03:00
|
|
|
(** Raises an error if unification cannot be performed. The position annotation
|
|
|
|
of the second [unionfind_typ] argument is propagated (unless it is [TAny]). *)
|
2021-01-14 02:17:24 +03:00
|
|
|
let rec unify
|
2022-08-12 23:42:39 +03:00
|
|
|
(ctx : A.decl_ctx)
|
2023-05-17 17:15:00 +03:00
|
|
|
(e : ('a, 'm) A.gexpr) (* used for error context *)
|
2022-08-25 18:29:00 +03:00
|
|
|
(t1 : unionfind_typ)
|
|
|
|
(t2 : unionfind_typ) : unit =
|
2021-01-14 02:17:24 +03:00
|
|
|
let unify = unify ctx in
|
2023-05-30 17:08:25 +03:00
|
|
|
(* Messages.emit_debug "Unifying %a and %a" (format_typ ctx) t1 (format_typ
|
|
|
|
ctx) t2; *)
|
2020-11-22 22:56:27 +03:00
|
|
|
let t1_repr = UnionFind.get (UnionFind.find t1) in
|
|
|
|
let t2_repr = UnionFind.get (UnionFind.find t2) in
|
2022-07-28 11:36:36 +03:00
|
|
|
let raise_type_error () = raise (Type_error (A.AnyExpr e, t1, t2)) in
|
2022-09-16 18:33:09 +03:00
|
|
|
let () =
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove t1_repr, Mark.remove t2_repr with
|
2022-09-16 18:33:09 +03:00
|
|
|
| TLit tl1, TLit tl2 -> if tl1 <> tl2 then raise_type_error ()
|
2023-02-22 14:11:42 +03:00
|
|
|
| TArrow (t11, t12), TArrow (t21, t22) -> (
|
2022-09-16 19:15:30 +03:00
|
|
|
unify e t12 t22;
|
2023-02-22 14:11:42 +03:00
|
|
|
try List.iter2 (unify e) t11 t21
|
|
|
|
with Invalid_argument _ -> raise_type_error ())
|
|
|
|
| TTuple ts1, TTuple ts2 -> (
|
|
|
|
try List.iter2 (unify e) ts1 ts2
|
|
|
|
with Invalid_argument _ -> raise_type_error ())
|
2022-08-23 16:23:52 +03:00
|
|
|
| TStruct s1, TStruct s2 ->
|
2022-09-16 18:33:09 +03:00
|
|
|
if not (A.StructName.equal s1 s2) then raise_type_error ()
|
2022-08-23 16:23:52 +03:00
|
|
|
| TEnum e1, TEnum e2 ->
|
2022-09-16 18:33:09 +03:00
|
|
|
if not (A.EnumName.equal e1 e2) then raise_type_error ()
|
|
|
|
| TOption t1, TOption t2 -> unify e t1 t2
|
|
|
|
| TArray t1', TArray t2' -> unify e t1' t2'
|
2023-06-15 12:11:56 +03:00
|
|
|
| TClosureEnv, TClosureEnv -> ()
|
2022-09-16 18:33:09 +03:00
|
|
|
| TAny _, _ | _, TAny _ -> ()
|
2022-08-23 16:23:52 +03:00
|
|
|
| ( ( TLit _ | TArrow _ | TTuple _ | TStruct _ | TEnum _ | TOption _
|
2023-06-15 12:11:56 +03:00
|
|
|
| TArray _ | TClosureEnv ),
|
2022-08-23 16:23:52 +03:00
|
|
|
_ ) ->
|
|
|
|
raise_type_error ()
|
2020-12-30 03:02:04 +03:00
|
|
|
in
|
2022-09-16 18:33:09 +03:00
|
|
|
ignore
|
|
|
|
@@ UnionFind.merge
|
2023-05-17 16:44:57 +03:00
|
|
|
(fun t1 t2 -> match Mark.remove t2 with TAny _ -> t1 | _ -> t2)
|
2022-09-16 18:33:09 +03:00
|
|
|
t1 t2
|
2020-11-22 22:56:27 +03:00
|
|
|
|
2023-05-17 17:15:00 +03:00
|
|
|
let handle_type_error ctx (A.AnyExpr e) t1 t2 =
|
2022-07-11 12:32:23 +03:00
|
|
|
(* TODO: if we get weird error messages, then it means that we should use the
|
|
|
|
persistent version of the union-find data structure. *)
|
|
|
|
let t1_repr = UnionFind.get (UnionFind.find t1) in
|
|
|
|
let t2_repr = UnionFind.get (UnionFind.find t2) in
|
2023-05-17 16:44:57 +03:00
|
|
|
let t1_pos = Mark.get t1_repr in
|
|
|
|
let t2_pos = Mark.get t2_repr in
|
2023-06-07 19:10:50 +03:00
|
|
|
Messages.raise_multispanned_error_full
|
2022-07-11 12:32:23 +03:00
|
|
|
[
|
|
|
|
( Some
|
2023-06-07 19:15:14 +03:00
|
|
|
(fun ppf ->
|
|
|
|
Format.pp_print_string ppf
|
2023-06-07 19:10:50 +03:00
|
|
|
"Error coming from typechecking the following expression:"),
|
2023-05-17 17:15:00 +03:00
|
|
|
Expr.pos e );
|
2023-06-07 19:15:14 +03:00
|
|
|
( Some
|
|
|
|
(fun ppf ->
|
|
|
|
Format.fprintf ppf "Type @{<yellow>%a@} coming from expression:"
|
|
|
|
(format_typ ctx) t1),
|
|
|
|
t1_pos );
|
|
|
|
( Some
|
|
|
|
(fun ppf ->
|
|
|
|
Format.fprintf ppf "Type @{<yellow>%a@} coming from expression:"
|
|
|
|
(format_typ ctx) t2),
|
|
|
|
t2_pos );
|
2022-07-11 12:32:23 +03:00
|
|
|
]
|
2023-06-13 10:07:42 +03:00
|
|
|
"@[<v>Error during typechecking, incompatible types:@]@\n\
|
|
|
|
@[<v>@{<bold;blue>┌─⯈@} @[<hov>%a@]@,\
|
|
|
|
@{<bold;blue>└─⯈@} @[<hov>%a@]@]" (format_typ ctx) t1 (format_typ ctx) t2
|
2022-07-11 12:32:23 +03:00
|
|
|
|
2023-03-30 19:53:07 +03:00
|
|
|
let lit_type (lit : A.lit) : naked_typ =
|
2022-09-13 16:20:13 +03:00
|
|
|
match lit with
|
|
|
|
| LBool _ -> TLit TBool
|
|
|
|
| LInt _ -> TLit TInt
|
|
|
|
| LRat _ -> TLit TRat
|
|
|
|
| LMoney _ -> TLit TMoney
|
|
|
|
| LDate _ -> TLit TDate
|
|
|
|
| LDuration _ -> TLit TDuration
|
|
|
|
| LUnit -> TLit TUnit
|
|
|
|
|
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
|
|
|
(** [op_type] and [resolve_overload] are a bit similar, and work on disjoint
|
|
|
|
sets of operators. However, their assumptions are different so we keep the
|
|
|
|
functions separate. In particular [resolve_overloads] requires its argument
|
|
|
|
types to be known in advance. *)
|
|
|
|
|
2023-05-17 16:44:57 +03:00
|
|
|
let polymorphic_op_type (op : Operator.polymorphic A.operator Mark.pos) :
|
2023-03-30 16:30:08 +03:00
|
|
|
unionfind_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
|
|
|
let open Operator in
|
2023-05-17 16:44:57 +03:00
|
|
|
let pos = Mark.get op in
|
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
|
|
|
let any = lazy (UnionFind.make (TAny (Any.fresh ()), pos)) in
|
|
|
|
let any2 = lazy (UnionFind.make (TAny (Any.fresh ()), pos)) in
|
|
|
|
let bt = lazy (UnionFind.make (TLit TBool, pos)) in
|
2023-03-28 11:07:01 +03:00
|
|
|
let ut = lazy (UnionFind.make (TLit TUnit, pos)) in
|
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
|
|
|
let it = lazy (UnionFind.make (TLit TInt, pos)) in
|
|
|
|
let array a = lazy (UnionFind.make (TArray (Lazy.force a), pos)) in
|
2023-03-14 20:30:58 +03:00
|
|
|
let option a = lazy (UnionFind.make (TOption (Lazy.force a), pos)) in
|
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
|
|
|
let ( @-> ) x y =
|
2023-01-11 13:23:08 +03:00
|
|
|
lazy (UnionFind.make (TArrow (List.map Lazy.force x, Lazy.force y), 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
|
|
|
in
|
|
|
|
let ty =
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove op with
|
2023-01-11 13:23:08 +03:00
|
|
|
| Fold -> [[any2; any] @-> any2; any2; array any] @-> any2
|
|
|
|
| Eq -> [any; any] @-> bt
|
|
|
|
| Map -> [[any] @-> any2; array any] @-> array any2
|
|
|
|
| Filter -> [[any] @-> bt; array any] @-> array any
|
|
|
|
| Reduce -> [[any; any] @-> any; any; array any] @-> any
|
|
|
|
| Concat -> [array any; array any] @-> array any
|
|
|
|
| Log (PosRecordIfTrueBool, _) -> [bt] @-> bt
|
|
|
|
| Log _ -> [any] @-> any
|
|
|
|
| Length -> [array any] @-> it
|
2023-03-28 11:07:01 +03:00
|
|
|
| HandleDefault -> [array ([ut] @-> any); [ut] @-> bt; [ut] @-> any] @-> any
|
2023-03-14 20:30:58 +03:00
|
|
|
| HandleDefaultOpt ->
|
2023-04-11 18:46:41 +03:00
|
|
|
[array (option any); [ut] @-> option bt; [ut] @-> option any]
|
|
|
|
@-> option any
|
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
|
|
|
in
|
|
|
|
Lazy.force ty
|
|
|
|
|
|
|
|
let resolve_overload_ret_type
|
2022-12-13 18:06:36 +03:00
|
|
|
~leave_unresolved
|
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
|
|
|
(ctx : A.decl_ctx)
|
|
|
|
e
|
2023-03-30 16:30:08 +03:00
|
|
|
(op : Operator.overloaded A.operator)
|
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
|
|
|
tys : unionfind_typ =
|
|
|
|
let op_ty =
|
|
|
|
Operator.overload_type ctx
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.add (Expr.pos e) op)
|
2022-12-13 18:06:36 +03:00
|
|
|
(List.map (typ_to_ast ~leave_unresolved) tys)
|
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
|
|
|
(* We use [unsafe] because the error is caught below *)
|
|
|
|
in
|
|
|
|
ast_to_typ (Type.arrow_return op_ty)
|
2020-11-24 13:27:23 +03:00
|
|
|
|
2020-12-14 20:09:38 +03:00
|
|
|
(** {1 Double-directed typing} *)
|
2020-11-22 22:56:27 +03:00
|
|
|
|
2022-09-26 17:32:02 +03:00
|
|
|
module Env = struct
|
|
|
|
type 'e t = {
|
2022-12-13 18:49:21 +03:00
|
|
|
structs : unionfind_typ A.StructField.Map.t A.StructName.Map.t;
|
|
|
|
enums : unionfind_typ A.EnumConstructor.Map.t A.EnumName.Map.t;
|
2022-09-26 17:32:02 +03:00
|
|
|
vars : ('e, unionfind_typ) Var.Map.t;
|
2022-11-21 12:12:45 +03:00
|
|
|
scope_vars : A.typ A.ScopeVar.Map.t;
|
|
|
|
scopes : A.typ A.ScopeVar.Map.t A.ScopeName.Map.t;
|
2023-02-13 17:00:23 +03:00
|
|
|
toplevel_vars : A.typ A.TopdefName.Map.t;
|
2022-09-14 16:36:24 +03:00
|
|
|
}
|
2020-11-23 12:44:06 +03:00
|
|
|
|
2022-12-13 18:49:21 +03:00
|
|
|
let empty (decl_ctx : A.decl_ctx) =
|
|
|
|
(* We fill the environment initially with the structs and enums
|
|
|
|
declarations *)
|
2022-09-26 17:32:02 +03:00
|
|
|
{
|
2022-12-13 18:49:21 +03:00
|
|
|
structs =
|
|
|
|
A.StructName.Map.map
|
|
|
|
(A.StructField.Map.map ast_to_typ)
|
|
|
|
decl_ctx.ctx_structs;
|
|
|
|
enums =
|
|
|
|
A.EnumName.Map.map
|
|
|
|
(A.EnumConstructor.Map.map ast_to_typ)
|
|
|
|
decl_ctx.ctx_enums;
|
2022-09-26 17:32:02 +03:00
|
|
|
vars = Var.Map.empty;
|
2022-11-21 12:12:45 +03:00
|
|
|
scope_vars = A.ScopeVar.Map.empty;
|
|
|
|
scopes = A.ScopeName.Map.empty;
|
2023-02-13 17:00:23 +03:00
|
|
|
toplevel_vars = A.TopdefName.Map.empty;
|
2022-09-26 17:32:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let get t v = Var.Map.find_opt v t.vars
|
2022-11-21 12:12:45 +03:00
|
|
|
let get_scope_var t sv = A.ScopeVar.Map.find_opt sv t.scope_vars
|
2023-02-13 17:00:23 +03:00
|
|
|
let get_toplevel_var t v = A.TopdefName.Map.find_opt v t.toplevel_vars
|
2022-09-26 17:32:02 +03:00
|
|
|
|
2022-10-21 16:47:17 +03:00
|
|
|
let get_subscope_out_var t scope var =
|
2022-11-21 12:12:45 +03:00
|
|
|
Option.bind (A.ScopeName.Map.find_opt scope t.scopes) (fun vmap ->
|
|
|
|
A.ScopeVar.Map.find_opt var vmap)
|
2022-09-26 17:32:02 +03:00
|
|
|
|
|
|
|
let add v tau t = { t with vars = Var.Map.add v tau t.vars }
|
|
|
|
let add_var v typ t = add v (ast_to_typ typ) t
|
|
|
|
|
|
|
|
let add_scope_var v typ t =
|
2022-11-21 12:12:45 +03:00
|
|
|
{ t with scope_vars = A.ScopeVar.Map.add v typ t.scope_vars }
|
2022-09-26 17:32:02 +03:00
|
|
|
|
2022-10-25 15:03:35 +03:00
|
|
|
let add_scope scope_name ~vars t =
|
2022-11-21 12:12:45 +03:00
|
|
|
{ t with scopes = A.ScopeName.Map.add scope_name vars t.scopes }
|
2022-11-28 18:23:27 +03:00
|
|
|
|
2023-02-13 17:00:23 +03:00
|
|
|
let add_toplevel_var v typ t =
|
|
|
|
{ t with toplevel_vars = A.TopdefName.Map.add v typ t.toplevel_vars }
|
2023-01-23 14:19:36 +03:00
|
|
|
|
2022-11-28 18:23:27 +03:00
|
|
|
let open_scope scope_name t =
|
|
|
|
let scope_vars =
|
|
|
|
A.ScopeVar.Map.union
|
|
|
|
(fun _ _ -> assert false)
|
|
|
|
t.scope_vars
|
|
|
|
(A.ScopeName.Map.find scope_name t.scopes)
|
|
|
|
in
|
|
|
|
{ t with scope_vars }
|
2022-09-26 17:32:02 +03:00
|
|
|
end
|
|
|
|
|
2023-05-17 16:44:57 +03:00
|
|
|
let add_pos e ty = Mark.add (Expr.pos e) ty
|
2023-05-17 17:15:00 +03:00
|
|
|
|
|
|
|
let ty : (_, unionfind_typ A.custom) A.marked -> unionfind_typ =
|
|
|
|
fun (_, A.Custom { A.custom; _ }) -> custom
|
2020-11-22 22:56:27 +03:00
|
|
|
|
2020-12-14 20:09:38 +03:00
|
|
|
(** Infers the most permissive type from an expression *)
|
2022-09-13 16:20:13 +03:00
|
|
|
let rec typecheck_expr_bottom_up :
|
2022-09-27 19:23:10 +03:00
|
|
|
type a m.
|
2022-12-13 18:06:36 +03:00
|
|
|
leave_unresolved:bool ->
|
2022-09-13 16:20:13 +03:00
|
|
|
A.decl_ctx ->
|
2023-05-17 17:15:00 +03:00
|
|
|
(a, m) A.gexpr Env.t ->
|
|
|
|
(a, m) A.gexpr ->
|
|
|
|
(a, unionfind_typ A.custom) A.boxed_gexpr =
|
2022-12-13 18:06:36 +03:00
|
|
|
fun ~leave_unresolved ctx env e ->
|
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env
|
2022-10-07 16:59:10 +03:00
|
|
|
(UnionFind.make (add_pos e (TAny (Any.fresh ()))))
|
|
|
|
e
|
2022-05-04 18:40:55 +03:00
|
|
|
|
2021-01-13 14:04:14 +03:00
|
|
|
(** Checks whether the expression can be typed with the provided type *)
|
2022-09-13 16:20:13 +03:00
|
|
|
and typecheck_expr_top_down :
|
2022-09-27 19:23:10 +03:00
|
|
|
type a m.
|
2022-12-13 18:06:36 +03:00
|
|
|
leave_unresolved:bool ->
|
2022-09-13 16:20:13 +03:00
|
|
|
A.decl_ctx ->
|
2023-05-17 17:15:00 +03:00
|
|
|
(a, m) A.gexpr Env.t ->
|
2022-09-13 16:20:13 +03:00
|
|
|
unionfind_typ ->
|
2023-05-17 17:15:00 +03:00
|
|
|
(a, m) A.gexpr ->
|
|
|
|
(a, unionfind_typ A.custom) A.boxed_gexpr =
|
2022-12-13 18:06:36 +03:00
|
|
|
fun ~leave_unresolved ctx env tau e ->
|
2023-05-30 17:08:25 +03:00
|
|
|
(* Messages.emit_debug "Propagating type %a for naked_expr %a" (format_typ
|
|
|
|
ctx) tau (Expr.format ctx) e; *)
|
2022-09-13 16:20:13 +03:00
|
|
|
let pos_e = Expr.pos e in
|
2022-09-27 19:23:10 +03:00
|
|
|
let () =
|
|
|
|
(* If there already is a type annotation on the given expr, ensure it
|
|
|
|
matches *)
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.get e with
|
2022-09-27 19:23:10 +03:00
|
|
|
| A.Untyped _ | A.Typed { A.ty = A.TAny, _; _ } -> ()
|
|
|
|
| A.Typed { A.ty; _ } -> unify ctx e tau (ast_to_typ ty)
|
2023-05-17 17:15:00 +03:00
|
|
|
| A.Custom _ -> assert false
|
2022-09-27 19:23:10 +03:00
|
|
|
in
|
2023-05-17 17:15:00 +03:00
|
|
|
let context_mark = A.Custom { A.custom = tau; pos = pos_e } in
|
2023-03-31 12:24:43 +03:00
|
|
|
let mark_with_tau_and_unify uf =
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
(* Unify with the supplied type first, and return the mark *)
|
|
|
|
unify ctx e uf tau;
|
2023-05-17 17:15:00 +03:00
|
|
|
A.Custom { A.custom = uf; pos = pos_e }
|
2022-07-11 12:34:01 +03:00
|
|
|
in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
let unionfind ?(pos = e) t = UnionFind.make (add_pos pos t) in
|
2023-03-31 12:24:43 +03:00
|
|
|
let ty_mark ty = mark_with_tau_and_unify (unionfind ty) in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
| A.ELocation loc ->
|
|
|
|
let ty_opt =
|
2022-09-14 16:36:24 +03:00
|
|
|
match loc with
|
|
|
|
| DesugaredScopeVar (v, _) | ScopelangScopeVar v ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Env.get_scope_var env (Mark.remove v)
|
2022-09-26 17:32:02 +03:00
|
|
|
| SubScopeVar (scope, _, v) ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Env.get_subscope_out_var env scope (Mark.remove v)
|
|
|
|
| ToplevelVar v -> Env.get_toplevel_var env (Mark.remove v)
|
2022-09-14 16:36:24 +03:00
|
|
|
in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
let ty =
|
|
|
|
match ty_opt with
|
|
|
|
| Some ty -> ty
|
|
|
|
| None ->
|
2023-05-30 16:41:49 +03:00
|
|
|
Messages.raise_spanned_error pos_e "Reference to %a not found"
|
2023-05-02 17:33:23 +03:00
|
|
|
(Print.expr ()) e
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
in
|
2023-03-31 12:24:43 +03:00
|
|
|
Expr.elocation loc (mark_with_tau_and_unify (ast_to_typ ty))
|
2022-11-17 19:13:35 +03:00
|
|
|
| A.EStruct { name; fields } ->
|
|
|
|
let mark = ty_mark (TStruct name) in
|
2022-12-13 18:49:21 +03:00
|
|
|
let str_ast = A.StructName.Map.find name ctx.A.ctx_structs in
|
|
|
|
let str = A.StructName.Map.find name env.structs in
|
2022-11-17 19:13:35 +03:00
|
|
|
let _check_fields : unit =
|
|
|
|
let missing_fields, extra_fields =
|
2022-11-21 12:12:45 +03:00
|
|
|
A.StructField.Map.fold
|
2022-11-17 19:13:35 +03:00
|
|
|
(fun fld x (remaining, extra) ->
|
2022-11-21 12:12:45 +03:00
|
|
|
if A.StructField.Map.mem fld remaining then
|
|
|
|
A.StructField.Map.remove fld remaining, extra
|
|
|
|
else remaining, A.StructField.Map.add fld x extra)
|
2022-11-17 19:13:35 +03:00
|
|
|
fields
|
2022-12-13 18:49:21 +03:00
|
|
|
(str_ast, A.StructField.Map.empty)
|
2022-11-17 19:13:35 +03:00
|
|
|
in
|
|
|
|
let errs =
|
|
|
|
List.map
|
|
|
|
(fun (f, ty) ->
|
2022-11-21 12:12:45 +03:00
|
|
|
( Some (Format.asprintf "Missing field %a" A.StructField.format_t f),
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.get ty ))
|
2022-11-21 12:12:45 +03:00
|
|
|
(A.StructField.Map.bindings missing_fields)
|
2022-11-17 19:13:35 +03:00
|
|
|
@ List.map
|
|
|
|
(fun (f, ef) ->
|
2022-11-21 12:12:45 +03:00
|
|
|
let dup = A.StructField.Map.mem f str in
|
2022-11-17 19:13:35 +03:00
|
|
|
( Some
|
|
|
|
(Format.asprintf "%s field %a"
|
|
|
|
(if dup then "Duplicate" else "Unknown")
|
2022-11-21 12:12:45 +03:00
|
|
|
A.StructField.format_t f),
|
2022-11-17 19:13:35 +03:00
|
|
|
Expr.pos ef ))
|
2022-11-21 12:12:45 +03:00
|
|
|
(A.StructField.Map.bindings extra_fields)
|
2022-11-17 19:13:35 +03:00
|
|
|
in
|
|
|
|
if errs <> [] then
|
2023-05-30 16:41:49 +03:00
|
|
|
Messages.raise_multispanned_error errs
|
2022-11-17 19:13:35 +03:00
|
|
|
"Mismatching field definitions for structure %a" A.StructName.format_t
|
|
|
|
name
|
|
|
|
in
|
|
|
|
let fields' =
|
2022-11-21 12:12:45 +03:00
|
|
|
A.StructField.Map.mapi
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
(fun f_name f_e ->
|
2022-11-21 12:12:45 +03:00
|
|
|
let f_ty = A.StructField.Map.find f_name str in
|
2022-12-13 18:49:21 +03:00
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env f_ty f_e)
|
2022-11-17 19:13:35 +03:00
|
|
|
fields
|
2022-09-14 16:36:24 +03:00
|
|
|
in
|
2022-11-17 19:13:35 +03:00
|
|
|
Expr.estruct name fields' mark
|
2022-11-28 18:23:27 +03:00
|
|
|
| A.EDStructAccess { e = e_struct; name_opt; field } ->
|
|
|
|
let t_struct =
|
|
|
|
match name_opt with
|
|
|
|
| Some name -> TStruct name
|
|
|
|
| None -> TAny (Any.fresh ())
|
|
|
|
in
|
|
|
|
let e_struct' =
|
2022-12-13 18:06:36 +03:00
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env (unionfind t_struct)
|
|
|
|
e_struct
|
2022-11-28 18:23:27 +03:00
|
|
|
in
|
|
|
|
let name =
|
|
|
|
match UnionFind.get (ty e_struct') with
|
|
|
|
| TStruct name, _ -> name
|
|
|
|
| TAny _, _ ->
|
|
|
|
Printf.ksprintf failwith
|
|
|
|
"Disambiguation failed before reaching field %s" field
|
|
|
|
| _ ->
|
2023-05-30 16:41:49 +03:00
|
|
|
Messages.raise_spanned_error (Expr.pos e)
|
2022-11-28 18:23:27 +03:00
|
|
|
"This is not a structure, cannot access field %s (%a)" field
|
|
|
|
(format_typ ctx) (ty e_struct')
|
|
|
|
in
|
|
|
|
let fld_ty =
|
|
|
|
let str =
|
2022-12-13 18:49:21 +03:00
|
|
|
try A.StructName.Map.find name env.structs
|
2022-11-28 18:23:27 +03:00
|
|
|
with Not_found ->
|
2023-05-30 16:41:49 +03:00
|
|
|
Messages.raise_spanned_error pos_e "No structure %a found"
|
2022-11-28 18:23:27 +03:00
|
|
|
A.StructName.format_t name
|
|
|
|
in
|
|
|
|
let field =
|
2022-12-07 14:28:24 +03:00
|
|
|
let candidate_structs =
|
|
|
|
try A.IdentName.Map.find field ctx.ctx_struct_fields
|
|
|
|
with Not_found ->
|
2023-05-30 16:41:49 +03:00
|
|
|
Messages.raise_spanned_error
|
2023-05-17 17:15:00 +03:00
|
|
|
(Expr.mark_pos context_mark)
|
2023-06-07 19:15:14 +03:00
|
|
|
"Field @{<yellow>\"%s\"@} does not belong to structure \
|
|
|
|
@{<yellow>\"%a\"@} (no structure defines it)"
|
|
|
|
field A.StructName.format_t name
|
2022-12-07 14:28:24 +03:00
|
|
|
in
|
|
|
|
try A.StructName.Map.find name candidate_structs
|
2022-11-28 18:23:27 +03:00
|
|
|
with Not_found ->
|
2023-05-30 16:41:49 +03:00
|
|
|
Messages.raise_spanned_error
|
2023-05-17 17:15:00 +03:00
|
|
|
(Expr.mark_pos context_mark)
|
2023-06-07 19:15:14 +03:00
|
|
|
"@[<hov>Field @{<yellow>\"%s\"@}@ does not belong to@ structure \
|
|
|
|
@{<yellow>\"%a\"@},@ but to %a@]"
|
|
|
|
field A.StructName.format_t name
|
2022-12-07 14:28:24 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun ppf () -> Format.fprintf ppf "@ or@ ")
|
2023-03-31 12:47:44 +03:00
|
|
|
(fun fmt s_name ->
|
2023-06-07 19:15:14 +03:00
|
|
|
Format.fprintf fmt "@{<yellow>\"%a\"@}" A.StructName.format_t
|
|
|
|
s_name))
|
2022-12-07 14:28:24 +03:00
|
|
|
(List.map fst (A.StructName.Map.bindings candidate_structs))
|
2022-11-28 18:23:27 +03:00
|
|
|
in
|
|
|
|
A.StructField.Map.find field str
|
|
|
|
in
|
2023-03-31 12:24:43 +03:00
|
|
|
let mark = mark_with_tau_and_unify fld_ty in
|
2022-11-28 18:23:27 +03:00
|
|
|
Expr.edstructaccess e_struct' field (Some name) mark
|
2022-11-17 19:13:35 +03:00
|
|
|
| A.EStructAccess { e = e_struct; name; field } ->
|
|
|
|
let fld_ty =
|
|
|
|
let str =
|
2022-12-13 18:49:21 +03:00
|
|
|
try A.StructName.Map.find name env.structs
|
2022-11-17 19:13:35 +03:00
|
|
|
with Not_found ->
|
2023-05-30 16:41:49 +03:00
|
|
|
Messages.raise_spanned_error pos_e "No structure %a found"
|
2022-11-17 19:13:35 +03:00
|
|
|
A.StructName.format_t name
|
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
try A.StructField.Map.find field str
|
2022-11-17 19:13:35 +03:00
|
|
|
with Not_found ->
|
2023-05-30 16:41:49 +03:00
|
|
|
Messages.raise_multispanned_error
|
2022-11-17 19:13:35 +03:00
|
|
|
[
|
|
|
|
None, pos_e;
|
|
|
|
( Some "Structure %a declared here",
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.get (A.StructName.get_info name) );
|
2022-11-17 19:13:35 +03:00
|
|
|
]
|
|
|
|
"Structure %a doesn't define a field %a" A.StructName.format_t name
|
2022-11-21 12:12:45 +03:00
|
|
|
A.StructField.format_t field
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
in
|
2023-03-31 12:24:43 +03:00
|
|
|
let mark = mark_with_tau_and_unify fld_ty in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
let e_struct' =
|
2022-12-13 18:06:36 +03:00
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env
|
|
|
|
(unionfind (TStruct name)) e_struct
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
in
|
2022-11-17 19:13:35 +03:00
|
|
|
Expr.estructaccess e_struct' field name mark
|
2023-03-14 20:31:32 +03:00
|
|
|
| A.EInj { name; cons; e = e_enum }
|
2023-05-17 14:26:47 +03:00
|
|
|
when Definitions.EnumName.equal name Expr.option_enum ->
|
|
|
|
if Definitions.EnumConstructor.equal cons Expr.some_constr then
|
2023-04-21 15:51:15 +03:00
|
|
|
let cell_type = unionfind (TAny (Any.fresh ())) in
|
|
|
|
let mark = mark_with_tau_and_unify (unionfind (TOption cell_type)) in
|
|
|
|
let e_enum' =
|
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env cell_type e_enum
|
|
|
|
in
|
|
|
|
Expr.einj e_enum' cons name mark
|
|
|
|
else
|
|
|
|
(* None constructor *)
|
|
|
|
let cell_type = unionfind (TAny (Any.fresh ())) in
|
|
|
|
let mark = mark_with_tau_and_unify (unionfind (TOption cell_type)) in
|
|
|
|
let e_enum' =
|
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env
|
|
|
|
(unionfind (TLit TUnit)) e_enum
|
|
|
|
in
|
|
|
|
Expr.einj e_enum' cons name mark
|
2022-11-17 19:13:35 +03:00
|
|
|
| A.EInj { name; cons; e = e_enum } ->
|
2023-03-31 12:24:43 +03:00
|
|
|
let mark = mark_with_tau_and_unify (unionfind (TEnum name)) in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
let e_enum' =
|
2022-12-13 18:06:36 +03:00
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env
|
2022-12-13 18:49:21 +03:00
|
|
|
(A.EnumConstructor.Map.find cons (A.EnumName.Map.find name env.enums))
|
2022-09-26 19:19:39 +03:00
|
|
|
e_enum
|
2022-09-14 16:36:24 +03:00
|
|
|
in
|
2022-11-17 19:13:35 +03:00
|
|
|
Expr.einj e_enum' cons name mark
|
2023-03-31 12:24:43 +03:00
|
|
|
| A.EMatch { e = e1; name; cases }
|
2023-05-17 14:26:47 +03:00
|
|
|
when Definitions.EnumName.equal name Expr.option_enum ->
|
2023-03-31 12:24:43 +03:00
|
|
|
let cell_type = unionfind ~pos:e1 (TAny (Any.fresh ())) in
|
|
|
|
let t_arg = unionfind ~pos:e1 (TOption cell_type) in
|
2023-03-14 20:31:32 +03:00
|
|
|
let cases_ty =
|
|
|
|
ListLabels.fold_right2
|
2023-05-17 14:26:47 +03:00
|
|
|
[Expr.none_constr; Expr.some_constr]
|
2023-03-31 12:24:43 +03:00
|
|
|
[unionfind ~pos:e1 (TLit TUnit); cell_type]
|
|
|
|
~f:A.EnumConstructor.Map.add ~init:A.EnumConstructor.Map.empty
|
2023-03-14 20:31:32 +03:00
|
|
|
in
|
2023-03-31 12:24:43 +03:00
|
|
|
let t_ret = unionfind ~pos:e (TAny (Any.fresh ())) in
|
|
|
|
let mark = mark_with_tau_and_unify t_ret in
|
2023-03-30 12:02:35 +03:00
|
|
|
let e1' = typecheck_expr_top_down ~leave_unresolved ctx env t_arg e1 in
|
2023-03-14 20:31:32 +03:00
|
|
|
let cases' =
|
|
|
|
A.EnumConstructor.MapLabels.merge cases cases_ty ~f:(fun _ e e_ty ->
|
|
|
|
match e, e_ty with
|
|
|
|
| Some e, Some e_ty ->
|
|
|
|
Some
|
2023-03-30 12:02:35 +03:00
|
|
|
(typecheck_expr_top_down ~leave_unresolved ctx env
|
2023-03-31 12:24:43 +03:00
|
|
|
(unionfind ~pos:e (TArrow ([e_ty], t_ret)))
|
2023-03-14 20:31:32 +03:00
|
|
|
e)
|
|
|
|
| _ -> assert false)
|
|
|
|
in
|
|
|
|
|
|
|
|
Expr.ematch e1' name cases' mark
|
2022-11-17 19:13:35 +03:00
|
|
|
| A.EMatch { e = e1; name; cases } ->
|
2022-11-21 12:12:45 +03:00
|
|
|
let cases_ty = A.EnumName.Map.find name ctx.A.ctx_enums in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
let t_ret = unionfind ~pos:e1 (TAny (Any.fresh ())) in
|
2023-03-31 12:24:43 +03:00
|
|
|
let mark = mark_with_tau_and_unify t_ret in
|
2022-12-13 18:06:36 +03:00
|
|
|
let e1' =
|
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env (unionfind (TEnum name))
|
|
|
|
e1
|
|
|
|
in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
let cases' =
|
2022-11-21 12:12:45 +03:00
|
|
|
A.EnumConstructor.Map.mapi
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
(fun c_name e ->
|
2022-11-21 12:12:45 +03:00
|
|
|
let c_ty = A.EnumConstructor.Map.find c_name cases_ty in
|
2023-02-22 13:41:21 +03:00
|
|
|
(* For now our constructors are limited to zero or one argument. If
|
|
|
|
there is a change to allow for multiple arguments, it might be
|
|
|
|
easier to use tuples directly. *)
|
2023-01-11 13:23:08 +03:00
|
|
|
let e_ty = unionfind ~pos:e (TArrow ([ast_to_typ c_ty], t_ret)) in
|
2022-12-13 18:06:36 +03:00
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env e_ty e)
|
2022-09-14 16:36:24 +03:00
|
|
|
cases
|
|
|
|
in
|
2022-11-17 19:13:35 +03:00
|
|
|
Expr.ematch e1' name cases' mark
|
|
|
|
| A.EScopeCall { scope; args } ->
|
|
|
|
let scope_out_struct =
|
2022-11-21 12:12:45 +03:00
|
|
|
(A.ScopeName.Map.find scope ctx.ctx_scopes).out_struct_name
|
2022-11-17 19:13:35 +03:00
|
|
|
in
|
2023-03-31 12:24:43 +03:00
|
|
|
let mark = mark_with_tau_and_unify (unionfind (TStruct scope_out_struct)) in
|
2022-11-21 12:12:45 +03:00
|
|
|
let vars = A.ScopeName.Map.find scope env.scopes in
|
2022-11-17 19:13:35 +03:00
|
|
|
let args' =
|
2022-11-21 12:12:45 +03:00
|
|
|
A.ScopeVar.Map.mapi
|
2022-10-21 16:47:17 +03:00
|
|
|
(fun name ->
|
2022-12-13 18:06:36 +03:00
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env
|
2022-11-21 12:12:45 +03:00
|
|
|
(ast_to_typ (A.ScopeVar.Map.find name vars)))
|
2022-11-17 19:13:35 +03:00
|
|
|
args
|
2022-10-21 16:47:17 +03:00
|
|
|
in
|
2022-11-17 19:13:35 +03:00
|
|
|
Expr.escopecall scope args' mark
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
| A.ERaise ex -> Expr.eraise ex context_mark
|
2022-11-17 19:13:35 +03:00
|
|
|
| A.ECatch { body; exn; handler } ->
|
2022-12-13 18:06:36 +03:00
|
|
|
let body' = typecheck_expr_top_down ~leave_unresolved ctx env tau body in
|
|
|
|
let handler' =
|
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env tau handler
|
|
|
|
in
|
2022-11-17 19:13:35 +03:00
|
|
|
Expr.ecatch body' exn handler' context_mark
|
2022-09-14 18:56:27 +03:00
|
|
|
| A.EVar v ->
|
|
|
|
let tau' =
|
2022-09-26 17:32:02 +03:00
|
|
|
match Env.get env v with
|
|
|
|
| Some t -> t
|
|
|
|
| None ->
|
2023-05-30 16:41:49 +03:00
|
|
|
Messages.raise_spanned_error pos_e
|
2022-09-14 18:56:27 +03:00
|
|
|
"Variable %s not found in the current context" (Bindlib.name_of v)
|
|
|
|
in
|
2023-03-31 12:24:43 +03:00
|
|
|
Expr.evar (Var.translate v) (mark_with_tau_and_unify tau')
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
| A.ELit lit -> Expr.elit lit (ty_mark (lit_type lit))
|
2022-11-17 19:13:35 +03:00
|
|
|
| A.ETuple es ->
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
let tys = List.map (fun _ -> unionfind (TAny (Any.fresh ()))) es in
|
2023-03-31 12:24:43 +03:00
|
|
|
let mark = mark_with_tau_and_unify (unionfind (TTuple tys)) in
|
2022-12-13 18:06:36 +03:00
|
|
|
let es' =
|
|
|
|
List.map2 (typecheck_expr_top_down ~leave_unresolved ctx env) tys es
|
|
|
|
in
|
2022-11-17 19:13:35 +03:00
|
|
|
Expr.etuple es' mark
|
|
|
|
| A.ETupleAccess { e = e1; index; size } ->
|
|
|
|
if index >= size then
|
2023-05-30 16:41:49 +03:00
|
|
|
Messages.raise_spanned_error (Expr.pos e)
|
2022-11-17 19:13:35 +03:00
|
|
|
"Tuple access out of bounds (%d/%d)" index size;
|
|
|
|
let tuple_ty =
|
|
|
|
TTuple
|
|
|
|
(List.init size (fun n ->
|
|
|
|
if n = index then tau else unionfind ~pos:e1 (TAny (Any.fresh ()))))
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
in
|
2022-12-13 18:06:36 +03:00
|
|
|
let e1' =
|
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env
|
|
|
|
(unionfind ~pos:e1 tuple_ty)
|
|
|
|
e1
|
|
|
|
in
|
2022-11-17 19:13:35 +03:00
|
|
|
Expr.etupleaccess e1' index size context_mark
|
|
|
|
| A.EAbs { binder; tys = t_args } ->
|
2022-06-23 15:04:51 +03:00
|
|
|
if Bindlib.mbinder_arity binder <> List.length t_args then
|
2023-05-30 16:41:49 +03:00
|
|
|
Messages.raise_spanned_error (Expr.pos e)
|
2022-05-31 19:38:14 +03:00
|
|
|
"function has %d variables but was supplied %d types"
|
2022-06-23 15:04:51 +03:00
|
|
|
(Bindlib.mbinder_arity binder)
|
|
|
|
(List.length t_args)
|
2022-05-31 19:38:14 +03:00
|
|
|
else
|
2022-09-14 18:56:27 +03:00
|
|
|
let tau_args = List.map ast_to_typ t_args in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
let t_ret = unionfind (TAny (Any.fresh ())) in
|
2023-01-11 13:23:08 +03:00
|
|
|
let t_func = unionfind (TArrow (tau_args, t_ret)) in
|
2023-03-31 12:24:43 +03:00
|
|
|
let mark = mark_with_tau_and_unify t_func in
|
2022-06-23 15:04:51 +03:00
|
|
|
let xs, body = Bindlib.unmbind binder in
|
2022-09-13 16:20:13 +03:00
|
|
|
let xs' = Array.map Var.translate xs in
|
2022-06-23 15:04:51 +03:00
|
|
|
let env =
|
2022-09-26 17:32:02 +03:00
|
|
|
List.fold_left2
|
|
|
|
(fun env x tau_arg -> Env.add x tau_arg env)
|
|
|
|
env (Array.to_list xs) tau_args
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2022-12-13 18:06:36 +03:00
|
|
|
let body' =
|
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env t_ret body
|
|
|
|
in
|
2022-10-21 16:33:05 +03:00
|
|
|
let binder' = Bindlib.bind_mvar xs' (Expr.Box.lift body') in
|
2022-12-13 19:21:37 +03:00
|
|
|
Expr.eabs binder' (List.map (typ_to_ast ~leave_unresolved) tau_args) mark
|
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
|
|
|
| A.EApp { f = (EOp { op; tys }, _) as e1; args } ->
|
|
|
|
let t_args = List.map ast_to_typ tys in
|
2023-01-11 13:23:08 +03:00
|
|
|
let t_func = unionfind (TArrow (t_args, tau)) in
|
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
|
|
|
let e1', args' =
|
|
|
|
Operator.kind_dispatch op
|
|
|
|
~polymorphic:(fun _ ->
|
|
|
|
(* Type the operator first, then right-to-left: polymorphic operators
|
|
|
|
are required to allow the resolution of all type variables this
|
|
|
|
way *)
|
2022-12-13 18:06:36 +03:00
|
|
|
let e1' =
|
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env t_func e1
|
|
|
|
in
|
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
|
|
|
let args' =
|
|
|
|
List.rev_map2
|
2022-12-13 18:06:36 +03:00
|
|
|
(typecheck_expr_top_down ~leave_unresolved ctx env)
|
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
|
|
|
(List.rev t_args) (List.rev args)
|
|
|
|
in
|
|
|
|
e1', args')
|
|
|
|
~overloaded:(fun _ ->
|
|
|
|
(* Typing the arguments first is required to resolve the operator *)
|
2022-12-13 18:06:36 +03:00
|
|
|
let args' =
|
|
|
|
List.map2
|
|
|
|
(typecheck_expr_top_down ~leave_unresolved ctx env)
|
|
|
|
t_args args
|
|
|
|
in
|
|
|
|
let e1' =
|
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env t_func e1
|
|
|
|
in
|
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
|
|
|
e1', args')
|
|
|
|
~monomorphic:(fun _ ->
|
|
|
|
(* Here it doesn't matter but may affect the error messages *)
|
2022-12-13 18:06:36 +03:00
|
|
|
let e1' =
|
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env t_func e1
|
|
|
|
in
|
|
|
|
let args' =
|
|
|
|
List.map2
|
|
|
|
(typecheck_expr_top_down ~leave_unresolved ctx env)
|
|
|
|
t_args args
|
|
|
|
in
|
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
|
|
|
e1', args')
|
|
|
|
~resolved:(fun _ ->
|
|
|
|
(* This case should not fail *)
|
2022-12-13 18:06:36 +03:00
|
|
|
let e1' =
|
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env t_func e1
|
|
|
|
in
|
|
|
|
let args' =
|
|
|
|
List.map2
|
|
|
|
(typecheck_expr_top_down ~leave_unresolved ctx env)
|
|
|
|
t_args args
|
|
|
|
in
|
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
|
|
|
e1', args')
|
2022-11-28 18:23:27 +03:00
|
|
|
in
|
|
|
|
Expr.eapp e1' args' context_mark
|
|
|
|
| A.EApp { f = e1; args } ->
|
|
|
|
(* Here we type the arguments first (in order), to ensure we know the types
|
|
|
|
of the arguments if [f] is [EAbs] before disambiguation. This is also the
|
|
|
|
right order for the [let-in] form. *)
|
|
|
|
let t_args = List.map (fun _ -> unionfind (TAny (Any.fresh ()))) args in
|
2023-01-11 13:23:08 +03:00
|
|
|
let t_func = unionfind (TArrow (t_args, tau)) in
|
2022-12-13 18:06:36 +03:00
|
|
|
let args' =
|
|
|
|
List.map2 (typecheck_expr_top_down ~leave_unresolved ctx env) t_args args
|
|
|
|
in
|
|
|
|
let e1' = typecheck_expr_top_down ~leave_unresolved ctx env t_func e1 in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
Expr.eapp e1' args' context_mark
|
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
|
|
|
| A.EOp { op; tys } ->
|
|
|
|
let tys' = List.map ast_to_typ tys in
|
|
|
|
let t_ret = unionfind (TAny (Any.fresh ())) in
|
2023-01-11 13:23:08 +03:00
|
|
|
let t_func = unionfind (TArrow (tys', t_ret)) in
|
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
|
|
|
unify ctx e t_func tau;
|
|
|
|
let tys, mark =
|
|
|
|
Operator.kind_dispatch op
|
|
|
|
~polymorphic:(fun op ->
|
2023-05-17 16:44:57 +03:00
|
|
|
tys, mark_with_tau_and_unify (polymorphic_op_type (Mark.add pos_e op)))
|
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
|
|
|
~monomorphic:(fun op ->
|
|
|
|
let mark =
|
2023-03-31 12:24:43 +03:00
|
|
|
mark_with_tau_and_unify
|
2023-05-17 16:44:57 +03:00
|
|
|
(ast_to_typ (Operator.monomorphic_type (Mark.add pos_e op)))
|
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
|
|
|
in
|
2022-12-13 18:06:36 +03:00
|
|
|
List.map (typ_to_ast ~leave_unresolved) tys', mark)
|
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
|
|
|
~overloaded:(fun op ->
|
2022-12-13 18:06:36 +03:00
|
|
|
unify ctx e t_ret
|
|
|
|
(resolve_overload_ret_type ~leave_unresolved ctx e op tys');
|
|
|
|
( List.map (typ_to_ast ~leave_unresolved) tys',
|
2023-05-17 17:15:00 +03:00
|
|
|
A.Custom { A.custom = t_func; pos = pos_e } ))
|
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
|
|
|
~resolved:(fun op ->
|
|
|
|
let mark =
|
2023-03-31 12:24:43 +03:00
|
|
|
mark_with_tau_and_unify
|
2023-05-17 16:44:57 +03:00
|
|
|
(ast_to_typ (Operator.resolved_type (Mark.add pos_e op)))
|
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
|
|
|
in
|
2022-12-13 18:06:36 +03:00
|
|
|
List.map (typ_to_ast ~leave_unresolved) tys', mark)
|
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
|
|
|
in
|
|
|
|
Expr.eop op tys mark
|
2022-11-17 19:13:35 +03:00
|
|
|
| A.EDefault { excepts; just; cons } ->
|
2022-12-13 18:06:36 +03:00
|
|
|
let cons' = typecheck_expr_top_down ~leave_unresolved ctx env tau cons in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
let just' =
|
2022-12-13 18:06:36 +03:00
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env
|
|
|
|
(unionfind ~pos:just (TLit TBool))
|
|
|
|
just
|
|
|
|
in
|
|
|
|
let excepts' =
|
|
|
|
List.map (typecheck_expr_top_down ~leave_unresolved ctx env tau) excepts
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
in
|
|
|
|
Expr.edefault excepts' just' cons' context_mark
|
2022-11-17 19:13:35 +03:00
|
|
|
| A.EIfThenElse { cond; etrue = et; efalse = ef } ->
|
2022-12-13 18:06:36 +03:00
|
|
|
let et' = typecheck_expr_top_down ~leave_unresolved ctx env tau et in
|
|
|
|
let ef' = typecheck_expr_top_down ~leave_unresolved ctx env tau ef in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
let cond' =
|
2022-12-13 18:06:36 +03:00
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env
|
|
|
|
(unionfind ~pos:cond (TLit TBool))
|
|
|
|
cond
|
2022-09-14 18:56:27 +03:00
|
|
|
in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
Expr.eifthenelse cond' et' ef' context_mark
|
2022-05-31 19:38:14 +03:00
|
|
|
| A.EAssert e1 ->
|
2023-03-31 12:24:43 +03:00
|
|
|
let mark = mark_with_tau_and_unify (unionfind (TLit TUnit)) in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
let e1' =
|
2022-12-13 18:06:36 +03:00
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env
|
|
|
|
(unionfind ~pos:e1 (TLit TBool))
|
|
|
|
e1
|
2022-07-11 12:32:23 +03:00
|
|
|
in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
Expr.eassert e1' mark
|
2023-03-30 19:53:07 +03:00
|
|
|
| A.EEmptyError -> Expr.eemptyerror (ty_mark (TAny (Any.fresh ())))
|
2022-11-17 19:13:35 +03:00
|
|
|
| A.EErrorOnEmpty e1 ->
|
2022-12-13 18:06:36 +03:00
|
|
|
let e1' = typecheck_expr_top_down ~leave_unresolved ctx env tau e1 in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
Expr.eerroronempty e1' context_mark
|
2022-05-31 19:38:14 +03:00
|
|
|
| A.EArray es ->
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
let cell_type = unionfind (TAny (Any.fresh ())) in
|
2023-03-31 12:24:43 +03:00
|
|
|
let mark = mark_with_tau_and_unify (unionfind (TArray cell_type)) in
|
2022-12-13 18:06:36 +03:00
|
|
|
let es' =
|
|
|
|
List.map (typecheck_expr_top_down ~leave_unresolved ctx env cell_type) es
|
|
|
|
in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
Expr.earray es' mark
|
2022-07-11 12:32:23 +03:00
|
|
|
|
|
|
|
let wrap ctx f e =
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
try f e
|
2022-07-11 12:32:23 +03:00
|
|
|
with Type_error (e, ty1, ty2) -> (
|
|
|
|
let bt = Printexc.get_raw_backtrace () in
|
|
|
|
try handle_type_error ctx e ty1 ty2
|
|
|
|
with e -> Printexc.raise_with_backtrace e bt)
|
2020-11-23 12:44:06 +03:00
|
|
|
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
let wrap_expr ctx f e =
|
|
|
|
(* We need to unbox here, because the typing may otherwise be stored in
|
|
|
|
Bindlib closures and not yet applied, and would escape the `try..with` *)
|
|
|
|
wrap ctx (fun e -> Expr.unbox (f e)) e
|
|
|
|
|
2020-12-14 20:09:38 +03:00
|
|
|
(** {1 API} *)
|
|
|
|
|
2023-05-17 17:15:00 +03:00
|
|
|
let get_ty_mark ~leave_unresolved (A.Custom { A.custom = uf; pos }) =
|
2022-12-13 18:06:36 +03:00
|
|
|
A.Typed { ty = typ_to_ast ~leave_unresolved uf; pos }
|
2022-07-19 16:19:06 +03:00
|
|
|
|
2022-11-28 18:23:27 +03:00
|
|
|
let expr_raw
|
2022-09-26 13:12:39 +03:00
|
|
|
(type a)
|
2022-12-13 18:06:36 +03:00
|
|
|
~(leave_unresolved : bool)
|
2022-09-26 13:12:39 +03:00
|
|
|
(ctx : A.decl_ctx)
|
2022-12-13 18:49:21 +03:00
|
|
|
?(env = Env.empty ctx)
|
2022-09-26 13:12:39 +03:00
|
|
|
?(typ : A.typ option)
|
2023-05-17 17:15:00 +03:00
|
|
|
(e : (a, 'm) A.gexpr) : (a, unionfind_typ A.custom) A.gexpr =
|
2022-09-26 13:12:39 +03:00
|
|
|
let fty =
|
|
|
|
match typ with
|
2022-12-13 18:06:36 +03:00
|
|
|
| None -> typecheck_expr_bottom_up ~leave_unresolved ctx env
|
|
|
|
| Some typ ->
|
|
|
|
typecheck_expr_top_down ~leave_unresolved ctx env (ast_to_typ typ)
|
2022-09-26 13:12:39 +03:00
|
|
|
in
|
2022-11-28 18:23:27 +03:00
|
|
|
wrap_expr ctx fty e
|
|
|
|
|
2022-12-13 18:06:36 +03:00
|
|
|
let check_expr ~leave_unresolved ctx ?env ?typ e =
|
2022-11-28 18:23:27 +03:00
|
|
|
Expr.map_marks
|
2023-05-17 17:15:00 +03:00
|
|
|
~f:(fun (Custom { pos; _ }) -> A.Untyped { pos })
|
2022-12-13 18:06:36 +03:00
|
|
|
(expr_raw ctx ~leave_unresolved ?env ?typ e)
|
2022-11-28 18:23:27 +03:00
|
|
|
|
|
|
|
(* Infer the type of an expression *)
|
2022-12-13 18:06:36 +03:00
|
|
|
let expr ~leave_unresolved ctx ?env ?typ e =
|
|
|
|
Expr.map_marks
|
|
|
|
~f:(get_ty_mark ~leave_unresolved)
|
|
|
|
(expr_raw ~leave_unresolved ctx ?env ?typ e)
|
2022-09-26 13:12:39 +03:00
|
|
|
|
2022-12-13 18:06:36 +03:00
|
|
|
let rec scope_body_expr ~leave_unresolved ctx env ty_out body_expr =
|
2022-09-26 13:12:39 +03:00
|
|
|
match body_expr with
|
|
|
|
| A.Result e ->
|
2022-12-13 18:06:36 +03:00
|
|
|
let e' =
|
|
|
|
wrap_expr ctx (typecheck_expr_top_down ~leave_unresolved ctx env ty_out) e
|
|
|
|
in
|
|
|
|
let e' = Expr.map_marks ~f:(get_ty_mark ~leave_unresolved) e' in
|
2022-10-21 16:33:05 +03:00
|
|
|
Bindlib.box_apply (fun e -> A.Result e) (Expr.Box.lift e')
|
2022-09-26 13:12:39 +03:00
|
|
|
| A.ScopeLet
|
|
|
|
{
|
|
|
|
scope_let_kind;
|
|
|
|
scope_let_typ;
|
|
|
|
scope_let_expr = e0;
|
|
|
|
scope_let_next;
|
|
|
|
scope_let_pos;
|
|
|
|
} ->
|
|
|
|
let ty_e = ast_to_typ scope_let_typ in
|
2022-12-13 18:06:36 +03:00
|
|
|
let e =
|
|
|
|
wrap_expr ctx (typecheck_expr_bottom_up ~leave_unresolved ctx env) e0
|
|
|
|
in
|
Swap boxing and annotations in expressions
This was the only reasonable solution I found to the issue raised
[here](https://github.com/CatalaLang/catala/pull/334#discussion_r987175884).
This was a pretty tedious rewrite, but it should now ensure we are doing things
correctly. As a bonus, the "smart" expression constructors are now used
everywhere to build expressions (so another refactoring like this one should be
much easier) and this makes the code overall feel more
straightforward (`Bindlib.box_apply` or `let+` no longer need to be visible!)
---
Basically, we were using values of type `gexpr box = naked_gexpr marked box`
throughout when (re-)building expressions. This was done 99% of the time by
using `Bindlib.box_apply add_mark naked_e` right after building `naked_e`. In
lots of places, we needed to recover the annotation of this expression later on,
typically to build its parent term (to inherit the position, or build the type).
Since it wasn't always possible to wrap these uses within `box_apply` (esp. as
bindlib boxes aren't a monad), here and there we had to call `Bindlib.unbox`,
just to recover the position or type. This had the very unpleasant effect of
forcing the resolution of the whole box (including applying any stored closures)
to reach the top-level annotation which isn't even dependant on specific
variable bindings. Then, generally, throwing away the result.
Therefore, the change proposed here transforms
- `naked_gexpr marked Bindlib.box` into
- `naked_gexpr Bindlib.box marked` (aliased to `boxed_gexpr` or `gexpr boxed` for
convenience)
This means only
1. not fitting the mark into the box right away when building, and
2. accessing the top-level mark directly without unboxing
The functions for building terms from module `Shared_ast.Expr` could be changed
easily. But then they needed to be consistently used throughout, without
manually building terms through `Bindlib.apply_box` -- which covers most of the
changes in this patch.
`Expr.Box.inj` is provided to swap back to a box, before binding for example.
Additionally, this gives a 40% speedup on `make -C examples pass_all_tests`,
which hints at the amount of unnecessary work we were doing --'
2022-10-06 20:13:45 +03:00
|
|
|
wrap ctx (fun t -> unify ctx e0 (ty e) t) ty_e;
|
2022-09-26 13:12:39 +03:00
|
|
|
(* We could use [typecheck_expr_top_down] rather than this manual
|
|
|
|
unification, but we get better messages with this order of the [unify]
|
|
|
|
parameters, which keeps location of the type as defined instead of as
|
|
|
|
inferred. *)
|
|
|
|
let var, next = Bindlib.unbind scope_let_next in
|
2022-09-26 17:32:02 +03:00
|
|
|
let env = Env.add var ty_e env in
|
2022-12-13 18:06:36 +03:00
|
|
|
let next = scope_body_expr ~leave_unresolved ctx env ty_out next in
|
2022-09-26 13:12:39 +03:00
|
|
|
let scope_let_next = Bindlib.bind_var (Var.translate var) next in
|
|
|
|
Bindlib.box_apply2
|
|
|
|
(fun scope_let_expr scope_let_next ->
|
|
|
|
A.ScopeLet
|
|
|
|
{
|
|
|
|
scope_let_kind;
|
2022-12-13 16:12:43 +03:00
|
|
|
scope_let_typ =
|
2023-05-17 16:44:57 +03:00
|
|
|
(match Mark.remove scope_let_typ with
|
2022-12-13 18:06:36 +03:00
|
|
|
| TAny -> typ_to_ast ~leave_unresolved (ty e)
|
2022-12-13 16:12:43 +03:00
|
|
|
| _ -> scope_let_typ);
|
2022-09-26 13:12:39 +03:00
|
|
|
scope_let_expr;
|
|
|
|
scope_let_next;
|
|
|
|
scope_let_pos;
|
|
|
|
})
|
2022-12-13 18:06:36 +03:00
|
|
|
(Expr.Box.lift (Expr.map_marks ~f:(get_ty_mark ~leave_unresolved) e))
|
2022-09-26 13:12:39 +03:00
|
|
|
scope_let_next
|
|
|
|
|
2022-12-13 18:06:36 +03:00
|
|
|
let scope_body ~leave_unresolved ctx env body =
|
2023-05-17 16:44:57 +03:00
|
|
|
let get_pos struct_name = Mark.get (A.StructName.get_info struct_name) in
|
2022-09-26 13:12:39 +03:00
|
|
|
let struct_ty struct_name =
|
2023-05-17 16:44:57 +03:00
|
|
|
UnionFind.make (Mark.add (get_pos struct_name) (TStruct struct_name))
|
2022-09-26 13:12:39 +03:00
|
|
|
in
|
|
|
|
let ty_in = struct_ty body.A.scope_body_input_struct in
|
|
|
|
let ty_out = struct_ty body.A.scope_body_output_struct in
|
|
|
|
let var, e = Bindlib.unbind body.A.scope_body_expr in
|
2022-09-26 17:32:02 +03:00
|
|
|
let env = Env.add var ty_in env in
|
2022-12-13 18:06:36 +03:00
|
|
|
let e' = scope_body_expr ~leave_unresolved ctx env ty_out e in
|
2023-01-23 14:19:36 +03:00
|
|
|
( Bindlib.box_apply
|
|
|
|
(fun scope_body_expr -> { body with scope_body_expr })
|
|
|
|
(Bindlib.bind_var (Var.translate var) e'),
|
2022-09-26 13:12:39 +03:00
|
|
|
UnionFind.make
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.add
|
2022-09-26 13:12:39 +03:00
|
|
|
(get_pos body.A.scope_body_output_struct)
|
2023-01-11 13:23:08 +03:00
|
|
|
(TArrow ([ty_in], ty_out))) )
|
2022-09-26 13:12:39 +03:00
|
|
|
|
2022-12-13 18:06:36 +03:00
|
|
|
let rec scopes ~leave_unresolved ctx env = function
|
2023-06-13 11:48:21 +03:00
|
|
|
| A.Nil -> Bindlib.box A.Nil, env
|
2023-01-23 14:19:36 +03:00
|
|
|
| A.Cons (item, next_bind) ->
|
|
|
|
let var, next = Bindlib.unbind next_bind in
|
|
|
|
let env, def =
|
|
|
|
match item with
|
|
|
|
| A.ScopeDef (name, body) ->
|
2023-03-21 14:14:10 +03:00
|
|
|
let body_e, ty_scope = scope_body ~leave_unresolved ctx env body in
|
2023-01-23 14:19:36 +03:00
|
|
|
( Env.add var ty_scope env,
|
|
|
|
Bindlib.box_apply (fun body -> A.ScopeDef (name, body)) body_e )
|
|
|
|
| A.Topdef (name, typ, e) ->
|
2023-03-21 14:14:10 +03:00
|
|
|
let e' = expr_raw ~leave_unresolved ctx ~env ~typ e in
|
2023-05-17 17:15:00 +03:00
|
|
|
let (A.Custom { custom = uf; _ }) = Mark.get e' in
|
2023-03-21 14:14:10 +03:00
|
|
|
let e' = Expr.map_marks ~f:(get_ty_mark ~leave_unresolved) e' in
|
2023-01-23 14:19:36 +03:00
|
|
|
( Env.add var uf env,
|
|
|
|
Bindlib.box_apply
|
|
|
|
(fun e -> A.Topdef (name, typ, e))
|
|
|
|
(Expr.Box.lift e') )
|
2022-09-26 13:12:39 +03:00
|
|
|
in
|
2023-06-13 11:48:21 +03:00
|
|
|
let next', env = scopes ~leave_unresolved ctx env next in
|
2023-01-23 14:19:36 +03:00
|
|
|
let next_bind' = Bindlib.bind_var (Var.translate var) next' in
|
2023-06-13 11:48:21 +03:00
|
|
|
( Bindlib.box_apply2 (fun item next -> A.Cons (item, next)) def next_bind',
|
|
|
|
env )
|
2022-09-26 13:12:39 +03:00
|
|
|
|
2022-12-13 18:06:36 +03:00
|
|
|
let program ~leave_unresolved prg =
|
2023-06-13 11:48:21 +03:00
|
|
|
let code_items, new_env =
|
|
|
|
scopes ~leave_unresolved prg.A.decl_ctx (Env.empty prg.A.decl_ctx)
|
|
|
|
prg.A.code_items
|
2023-02-13 17:00:23 +03:00
|
|
|
in
|
2023-06-13 11:48:21 +03:00
|
|
|
{
|
|
|
|
A.code_items = Bindlib.unbox code_items;
|
|
|
|
decl_ctx =
|
|
|
|
{
|
|
|
|
prg.decl_ctx with
|
|
|
|
ctx_structs =
|
|
|
|
A.StructName.Map.mapi
|
|
|
|
(fun s_name fields ->
|
|
|
|
A.StructField.Map.mapi
|
|
|
|
(fun f_name (t : A.typ) ->
|
|
|
|
match Mark.remove t with
|
|
|
|
| TAny ->
|
|
|
|
typ_to_ast ~leave_unresolved
|
|
|
|
(A.StructField.Map.find f_name
|
|
|
|
(A.StructName.Map.find s_name new_env.structs))
|
|
|
|
| _ -> t)
|
|
|
|
fields)
|
|
|
|
prg.decl_ctx.ctx_structs;
|
|
|
|
ctx_enums =
|
|
|
|
A.EnumName.Map.mapi
|
|
|
|
(fun e_name cons ->
|
|
|
|
A.EnumConstructor.Map.mapi
|
|
|
|
(fun cons_name (t : A.typ) ->
|
|
|
|
match Mark.remove t with
|
|
|
|
| TAny ->
|
|
|
|
typ_to_ast ~leave_unresolved
|
|
|
|
(A.EnumConstructor.Map.find cons_name
|
|
|
|
(A.EnumName.Map.find e_name new_env.enums))
|
|
|
|
| _ -> t)
|
|
|
|
cons)
|
|
|
|
prg.decl_ctx.ctx_enums;
|
|
|
|
};
|
|
|
|
}
|