2020-11-25 12:49:53 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria, contributor:
|
|
|
|
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. *)
|
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
(** Translation from {!module: Desugared.Ast} to {!module: Scopelang.Ast} *)
|
|
|
|
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2022-08-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2023-02-27 11:50:42 +03:00
|
|
|
module D = Desugared.Ast
|
2020-11-25 16:35:26 +03:00
|
|
|
|
2022-03-01 12:15:44 +03:00
|
|
|
(** {1 Expression translation}*)
|
|
|
|
|
|
|
|
type target_scope_vars =
|
2022-08-17 18:14:29 +03:00
|
|
|
| WholeVar of ScopeVar.t
|
2022-08-25 13:09:51 +03:00
|
|
|
| States of (StateName.t * ScopeVar.t) list
|
2022-03-01 12:15:44 +03:00
|
|
|
|
|
|
|
type ctx = {
|
2022-11-28 18:23:27 +03:00
|
|
|
decl_ctx : decl_ctx;
|
2022-11-21 12:12:45 +03:00
|
|
|
scope_var_mapping : target_scope_vars ScopeVar.Map.t;
|
2023-11-03 19:15:55 +03:00
|
|
|
reentrant_vars : typ ScopeVar.Map.t;
|
2023-08-10 17:52:39 +03:00
|
|
|
var_mapping : (D.expr, untyped Ast.expr Var.t) Var.Map.t;
|
2022-03-01 12:15:44 +03:00
|
|
|
}
|
|
|
|
|
2022-03-16 13:44:34 +03:00
|
|
|
let tag_with_log_entry
|
2022-11-07 15:50:28 +03:00
|
|
|
(e : untyped Ast.expr boxed)
|
2022-08-12 23:42:39 +03:00
|
|
|
(l : log_entry)
|
2022-11-21 12:46:17 +03:00
|
|
|
(markings : Uid.MarkedString.info list) : untyped Ast.expr boxed =
|
2024-03-15 16:23:30 +03:00
|
|
|
if Global.options.trace then
|
2023-12-18 12:25:00 +03:00
|
|
|
Expr.eappop
|
|
|
|
~op:(Log (l, markings))
|
|
|
|
~tys:[TAny, Expr.pos e]
|
|
|
|
~args:[e] (Mark.get e)
|
2023-04-14 11:50:21 +03:00
|
|
|
else e
|
2022-03-16 13:44:34 +03:00
|
|
|
|
2023-08-10 17:52:39 +03:00
|
|
|
let rec translate_expr (ctx : ctx) (e : D.expr) : untyped Ast.expr boxed =
|
2023-05-17 16:44:57 +03:00
|
|
|
let m = Mark.get e in
|
|
|
|
match Mark.remove e with
|
2023-04-19 19:26:50 +03:00
|
|
|
| EVar v -> Expr.evar (Var.Map.find v ctx.var_mapping) m
|
|
|
|
| EAbs { binder; tys } ->
|
|
|
|
let vars, body = Bindlib.unmbind binder in
|
|
|
|
let new_vars = Array.map (fun var -> Var.make (Bindlib.name_of var)) vars in
|
|
|
|
let ctx =
|
|
|
|
List.fold_left2
|
|
|
|
(fun ctx var new_var ->
|
|
|
|
{ ctx with var_mapping = Var.Map.add var new_var ctx.var_mapping })
|
|
|
|
ctx (Array.to_list vars) (Array.to_list new_vars)
|
|
|
|
in
|
|
|
|
Expr.eabs (Expr.bind new_vars (translate_expr ctx body)) tys m
|
2023-08-10 17:52:39 +03:00
|
|
|
| ELocation (DesugaredScopeVar { name; state = None }) ->
|
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.elocation
|
|
|
|
(ScopelangScopeVar
|
2023-08-10 17:52:39 +03:00
|
|
|
{
|
|
|
|
name =
|
|
|
|
(match
|
|
|
|
ScopeVar.Map.find (Mark.remove name) ctx.scope_var_mapping
|
|
|
|
with
|
|
|
|
| WholeVar new_s_var -> Mark.copy name new_s_var
|
|
|
|
| States _ -> failwith "should not happen");
|
|
|
|
})
|
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
|
|
|
m
|
2023-08-10 17:52:39 +03:00
|
|
|
| ELocation (DesugaredScopeVar { name; state = Some state }) ->
|
|
|
|
Expr.elocation
|
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
|
|
|
(ScopelangScopeVar
|
2023-08-10 17:52:39 +03:00
|
|
|
{
|
|
|
|
name =
|
|
|
|
(match
|
|
|
|
ScopeVar.Map.find (Mark.remove name) ctx.scope_var_mapping
|
|
|
|
with
|
|
|
|
| WholeVar _ -> failwith "should not happen"
|
|
|
|
| States states -> Mark.copy name (List.assoc state states));
|
|
|
|
})
|
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
|
|
|
m
|
2023-02-13 17:00:23 +03:00
|
|
|
| ELocation (ToplevelVar v) -> Expr.elocation (ToplevelVar v) m
|
2023-12-01 01:53:38 +03:00
|
|
|
| EDStructAccess _ ->
|
|
|
|
assert false (* This shouldn't appear in desugared after disambiguation *)
|
2023-08-30 18:49:29 +03:00
|
|
|
| EScopeCall { scope; args } ->
|
|
|
|
Expr.escopecall ~scope
|
2023-08-10 17:52:39 +03:00
|
|
|
~args:
|
|
|
|
(ScopeVar.Map.fold
|
2022-11-17 19:13:35 +03:00
|
|
|
(fun v e args' ->
|
2022-10-21 16:47:17 +03:00
|
|
|
let v' =
|
2022-11-21 12:12:45 +03:00
|
|
|
match ScopeVar.Map.find v ctx.scope_var_mapping with
|
2022-10-21 16:47:17 +03:00
|
|
|
| WholeVar v' -> v'
|
2022-11-03 17:18:51 +03:00
|
|
|
| States ((_, v') :: _) ->
|
|
|
|
(* When there are multiple states, the input is always the
|
|
|
|
first one *)
|
|
|
|
v'
|
2022-10-26 12:08:50 +03:00
|
|
|
| States [] -> assert false
|
2022-10-21 16:47:17 +03:00
|
|
|
in
|
2023-11-03 12:29:32 +03:00
|
|
|
let e' = translate_expr ctx e in
|
2023-11-03 19:15:55 +03:00
|
|
|
let m = Mark.get e in
|
2023-11-03 12:29:32 +03:00
|
|
|
let e' =
|
2023-11-03 19:15:55 +03:00
|
|
|
match ScopeVar.Map.find_opt v ctx.reentrant_vars with
|
|
|
|
| Some (TArrow (targs, _), _) ->
|
2023-11-07 20:25:57 +03:00
|
|
|
(* Functions are treated specially: the default only applies to
|
|
|
|
their return type *)
|
2023-11-03 19:15:55 +03:00
|
|
|
let arg = Var.make "arg" in
|
|
|
|
let pos = Expr.mark_pos m in
|
2023-11-07 20:25:57 +03:00
|
|
|
Expr.make_abs [| arg |]
|
2023-11-08 20:25:50 +03:00
|
|
|
(Expr.edefault ~excepts:[] ~just:(Expr.elit (LBool true) m)
|
|
|
|
~cons:
|
|
|
|
(Expr.epuredefault
|
2023-12-18 12:25:00 +03:00
|
|
|
(Expr.make_app e' [Expr.evar arg m] targs pos)
|
2023-11-08 20:25:50 +03:00
|
|
|
m)
|
2023-11-03 19:15:55 +03:00
|
|
|
m)
|
2023-11-07 20:25:57 +03:00
|
|
|
targs pos
|
2023-11-08 20:25:50 +03:00
|
|
|
| Some _ -> Expr.epuredefault e' m
|
2023-11-07 20:25:57 +03:00
|
|
|
| None -> e'
|
2023-11-03 12:29:32 +03:00
|
|
|
in
|
|
|
|
ScopeVar.Map.add v' e' args')
|
2022-11-21 12:12:45 +03:00
|
|
|
args ScopeVar.Map.empty)
|
2022-10-21 16:47:17 +03:00
|
|
|
m
|
2023-12-18 12:25:00 +03:00
|
|
|
| EApp { f; tys; args } -> (
|
|
|
|
(* Detuplification of function arguments *)
|
|
|
|
let pos = Expr.pos f in
|
|
|
|
let f = translate_expr ctx f in
|
|
|
|
match args, tys with
|
|
|
|
| [arg], [_] -> Expr.eapp ~f ~tys m ~args:[translate_expr ctx arg]
|
|
|
|
| [(ETuple args, _)], _ ->
|
|
|
|
assert (List.length args = List.length tys);
|
|
|
|
Expr.eapp ~f ~tys m ~args:(List.map (translate_expr ctx) args)
|
|
|
|
| [((EVar _, _) as arg)], ts ->
|
|
|
|
let size = List.length ts in
|
|
|
|
let args =
|
|
|
|
let e = translate_expr ctx arg in
|
|
|
|
List.init size (fun index -> Expr.etupleaccess ~e ~size ~index m)
|
|
|
|
in
|
|
|
|
Expr.eapp ~f ~tys m ~args
|
|
|
|
| [arg], ts ->
|
|
|
|
let size = List.length ts in
|
|
|
|
let v = Var.make "args" in
|
|
|
|
let e = Expr.evar v (Mark.get arg) in
|
|
|
|
let args =
|
|
|
|
List.init size (fun index -> Expr.etupleaccess ~e ~size ~index m)
|
|
|
|
in
|
|
|
|
Expr.make_let_in v (TTuple ts, pos) (translate_expr ctx arg)
|
|
|
|
(Expr.eapp ~f ~tys m ~args)
|
|
|
|
pos
|
|
|
|
| args, tys ->
|
|
|
|
assert (List.length args = List.length tys);
|
|
|
|
Expr.eapp ~f ~tys m ~args:(List.map (translate_expr ctx) args))
|
|
|
|
| EAppOp { op; tys; args } ->
|
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.map (translate_expr ctx) args in
|
|
|
|
Operator.kind_dispatch op
|
2023-12-18 12:25:00 +03:00
|
|
|
~monomorphic:(fun op -> Expr.eappop ~op ~tys ~args m)
|
|
|
|
~polymorphic:(fun op -> Expr.eappop ~op ~tys ~args m)
|
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 ->
|
|
|
|
match
|
2023-05-17 16:44:57 +03:00
|
|
|
Operator.resolve_overload ctx.decl_ctx (Mark.add (Expr.pos e) op) 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
|
|
|
with
|
2023-12-18 12:25:00 +03:00
|
|
|
| op, `Straight -> Expr.eappop ~op ~tys ~args m
|
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, `Reversed ->
|
2023-12-18 12:25:00 +03:00
|
|
|
Expr.eappop ~op ~tys:(List.rev tys) ~args:(List.rev args) m)
|
2023-12-01 01:53:38 +03:00
|
|
|
| ( EStruct _ | EStructAccess _ | ETuple _ | ETupleAccess _ | EInj _
|
2023-12-18 12:25:00 +03:00
|
|
|
| EMatch _ | ELit _ | EDefault _ | EPureDefault _ | EIfThenElse _ | EArray _
|
|
|
|
| EEmptyError | EErrorOnEmpty _ ) as e ->
|
2023-04-19 19:26:50 +03:00
|
|
|
Expr.map ~f:(translate_expr ctx) (e, m)
|
2022-03-01 12:15:44 +03:00
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
(** {1 Rule tree construction} *)
|
|
|
|
|
2022-01-05 12:42:46 +03:00
|
|
|
(** Intermediate representation for the exception tree of rules for a particular
|
|
|
|
scope definition. *)
|
|
|
|
type rule_tree =
|
2023-08-10 17:52:39 +03:00
|
|
|
| Leaf of D.rule list
|
2022-01-05 12:42:46 +03:00
|
|
|
(** Rules defining a base case piecewise. List is non-empty. *)
|
2023-08-10 17:52:39 +03:00
|
|
|
| Node of rule_tree list * D.rule list
|
2022-07-13 16:00:57 +03:00
|
|
|
(** [Node (exceptions, base_case)] is a list of exceptions to a non-empty
|
|
|
|
list of rules defining a base case piecewise. *)
|
2020-11-25 18:51:19 +03:00
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
(** Transforms a flat list of rules into a tree, taking into account the
|
|
|
|
priorities declared between rules *)
|
2023-04-18 12:06:58 +03:00
|
|
|
let def_to_exception_graph
|
2023-08-10 17:52:39 +03:00
|
|
|
(def_info : D.ScopeDef.t)
|
|
|
|
(def : D.rule RuleName.Map.t) :
|
2023-04-18 12:06:58 +03:00
|
|
|
Desugared.Dependency.ExceptionsDependencies.t =
|
2022-11-07 15:50:28 +03:00
|
|
|
let exc_graph = Desugared.Dependency.build_exceptions_graph def def_info in
|
2023-02-28 16:40:05 +03:00
|
|
|
Desugared.Dependency.check_for_exception_cycle def exc_graph;
|
2023-04-18 12:06:58 +03:00
|
|
|
exc_graph
|
|
|
|
|
2023-08-10 17:52:39 +03:00
|
|
|
let rule_to_exception_graph (scope : D.scope) = function
|
2024-03-29 20:39:20 +03:00
|
|
|
| Desugared.Dependency.Vertex.Var (var, None)
|
|
|
|
when ScopeVar.Map.mem var scope.scope_sub_scopes ->
|
2023-04-18 12:06:58 +03:00
|
|
|
(* Before calling the sub_scope, we need to include all the re-definitions
|
|
|
|
of subscope parameters*)
|
2024-03-28 19:05:12 +03:00
|
|
|
D.ScopeDef.Map.fold
|
2024-04-04 11:56:56 +03:00
|
|
|
(fun ((sscope, kind) as def_key) scope_def exc_graphs ->
|
|
|
|
match kind with
|
|
|
|
| D.ScopeDef.Var _ -> exc_graphs
|
|
|
|
| D.ScopeDef.SubScope _
|
|
|
|
when (not (ScopeVar.equal var (Mark.remove sscope)))
|
|
|
|
|| Mark.remove scope_def.D.scope_def_io.io_input = NoInput
|
|
|
|
&& RuleName.Map.is_empty scope_def.scope_def_rules ->
|
|
|
|
(* We exclude subscope variables that have 0 re-definitions and are
|
|
|
|
not visible in the input of the subscope *)
|
|
|
|
exc_graphs
|
|
|
|
| D.ScopeDef.SubScope { var_within_origin_scope; _ } ->
|
|
|
|
(* This definition redefines a variable of the correct subscope. But
|
|
|
|
we have to check that this redefinition is allowed with respect to
|
|
|
|
the io parameters of that subscope variable. *)
|
|
|
|
let def = scope_def.D.scope_def_rules in
|
|
|
|
let is_cond = scope_def.scope_def_is_condition in
|
|
|
|
let () =
|
|
|
|
match Mark.remove scope_def.D.scope_def_io.io_input with
|
|
|
|
| NoInput ->
|
|
|
|
Message.raise_multispanned_error
|
|
|
|
(( Some "Incriminated subscope:",
|
|
|
|
Mark.get (ScopeVar.get_info (Mark.remove sscope)) )
|
|
|
|
:: ( Some "Incriminated variable:",
|
|
|
|
Mark.get (ScopeVar.get_info var_within_origin_scope) )
|
|
|
|
:: List.map
|
|
|
|
(fun rule ->
|
2023-04-18 12:06:58 +03:00
|
|
|
( Some "Incriminated subscope variable definition:",
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.get (RuleName.get_info rule) ))
|
2024-04-04 11:56:56 +03:00
|
|
|
(RuleName.Map.keys def))
|
|
|
|
"Invalid assignment to a subscope variable that is not tagged \
|
|
|
|
as input or context."
|
|
|
|
| OnlyInput when RuleName.Map.is_empty def && not is_cond ->
|
|
|
|
(* If the subscope variable is tagged as input, then it shall be
|
|
|
|
defined. *)
|
|
|
|
Message.raise_multispanned_error
|
|
|
|
[
|
|
|
|
( Some "Incriminated subscope:",
|
|
|
|
Mark.get (ScopeVar.get_info (Mark.remove sscope)) );
|
|
|
|
Some "Incriminated variable:", Mark.get sscope;
|
|
|
|
]
|
|
|
|
"This subscope variable is a mandatory input but no definition \
|
|
|
|
was provided."
|
|
|
|
| _ -> ()
|
|
|
|
in
|
|
|
|
let new_exc_graph = def_to_exception_graph def_key def in
|
|
|
|
D.ScopeDef.Map.add def_key new_exc_graph exc_graphs)
|
|
|
|
scope.scope_defs D.ScopeDef.Map.empty
|
|
|
|
| Desugared.Dependency.Vertex.Var (var, state) -> (
|
|
|
|
let pos = Mark.get (ScopeVar.get_info var) in
|
|
|
|
let scope_def =
|
|
|
|
D.ScopeDef.Map.find ((var, pos), D.ScopeDef.Var state) scope.scope_defs
|
|
|
|
in
|
|
|
|
let var_def = scope_def.D.scope_def_rules in
|
|
|
|
match Mark.remove scope_def.D.scope_def_io.io_input with
|
|
|
|
| OnlyInput when not (RuleName.Map.is_empty var_def) ->
|
|
|
|
(* If the variable is tagged as input, then it shall not be redefined. *)
|
|
|
|
Message.raise_multispanned_error
|
|
|
|
((Some "Incriminated variable:", Mark.get (ScopeVar.get_info var))
|
|
|
|
:: List.map
|
|
|
|
(fun rule ->
|
|
|
|
( Some "Incriminated variable definition:",
|
|
|
|
Mark.get (RuleName.get_info rule) ))
|
|
|
|
(RuleName.Map.keys var_def))
|
|
|
|
"It is impossible to give a definition to a scope variable tagged as \
|
|
|
|
input."
|
|
|
|
| OnlyInput -> D.ScopeDef.Map.empty
|
|
|
|
(* we do not provide any definition for an input-only variable *)
|
|
|
|
| _ ->
|
|
|
|
D.ScopeDef.Map.singleton
|
|
|
|
((var, pos), D.ScopeDef.Var state)
|
|
|
|
(def_to_exception_graph ((var, pos), D.ScopeDef.Var state) var_def))
|
2023-08-10 17:52:39 +03:00
|
|
|
| Assertion _ -> D.ScopeDef.Map.empty (* no exceptions for assertions *)
|
2023-04-18 12:06:58 +03:00
|
|
|
|
2023-08-10 17:52:39 +03:00
|
|
|
let scope_to_exception_graphs (scope : D.scope) :
|
|
|
|
Desugared.Dependency.ExceptionsDependencies.t D.ScopeDef.Map.t =
|
2023-04-18 12:06:58 +03:00
|
|
|
let scope_dependencies =
|
|
|
|
Desugared.Dependency.build_scope_dependencies scope
|
|
|
|
in
|
|
|
|
Desugared.Dependency.check_for_cycle scope scope_dependencies;
|
|
|
|
let scope_ordering =
|
|
|
|
Desugared.Dependency.correct_computation_ordering scope_dependencies
|
|
|
|
in
|
|
|
|
List.fold_left
|
|
|
|
(fun exceptions_graphs scope_def_key ->
|
|
|
|
let new_exceptions_graphs = rule_to_exception_graph scope scope_def_key in
|
2023-12-01 01:53:38 +03:00
|
|
|
D.ScopeDef.Map.disjoint_union new_exceptions_graphs exceptions_graphs)
|
2023-08-10 17:52:39 +03:00
|
|
|
D.ScopeDef.Map.empty scope_ordering
|
2023-04-18 12:06:58 +03:00
|
|
|
|
2023-08-10 17:52:39 +03:00
|
|
|
let build_exceptions_graph (pgrm : D.program) :
|
|
|
|
Desugared.Dependency.ExceptionsDependencies.t D.ScopeDef.Map.t =
|
2023-04-18 12:06:58 +03:00
|
|
|
ScopeName.Map.fold
|
|
|
|
(fun _ scope exceptions_graph ->
|
|
|
|
let new_exceptions_graphs = scope_to_exception_graphs scope in
|
2023-12-01 01:53:38 +03:00
|
|
|
D.ScopeDef.Map.disjoint_union new_exceptions_graphs exceptions_graph)
|
2023-11-20 18:01:06 +03:00
|
|
|
pgrm.program_root.module_scopes D.ScopeDef.Map.empty
|
2023-04-18 12:06:58 +03:00
|
|
|
|
|
|
|
(** Transforms a flat list of rules into a tree, taking into account the
|
|
|
|
priorities declared between rules *)
|
|
|
|
let def_map_to_tree
|
2023-08-10 17:52:39 +03:00
|
|
|
(def : D.rule RuleName.Map.t)
|
2023-04-18 12:06:58 +03:00
|
|
|
(exc_graph : Desugared.Dependency.ExceptionsDependencies.t) : rule_tree list
|
|
|
|
=
|
2021-01-13 21:07:35 +03:00
|
|
|
(* we start by the base cases: they are the vertices which have no
|
|
|
|
successors *)
|
|
|
|
let base_cases =
|
2022-11-07 15:50:28 +03:00
|
|
|
Desugared.Dependency.ExceptionsDependencies.fold_vertex
|
2021-01-13 21:07:35 +03:00
|
|
|
(fun v base_cases ->
|
2022-11-07 15:50:28 +03:00
|
|
|
if
|
|
|
|
Desugared.Dependency.ExceptionsDependencies.out_degree exc_graph v = 0
|
|
|
|
then v :: base_cases
|
2021-01-13 21:07:35 +03:00
|
|
|
else base_cases)
|
|
|
|
exc_graph []
|
2020-12-18 17:59:15 +03:00
|
|
|
in
|
2023-04-07 17:35:09 +03:00
|
|
|
let rec build_tree (base_cases : Desugared.Dependency.ExceptionVertex.t) :
|
|
|
|
rule_tree =
|
2022-01-05 12:42:46 +03:00
|
|
|
let exceptions =
|
2022-11-07 15:50:28 +03:00
|
|
|
Desugared.Dependency.ExceptionsDependencies.pred exc_graph base_cases
|
2022-01-05 12:42:46 +03:00
|
|
|
in
|
|
|
|
let base_case_as_rule_list =
|
2022-11-21 12:12:45 +03:00
|
|
|
List.map
|
2023-07-12 12:48:46 +03:00
|
|
|
(fun r -> RuleName.Map.find r def)
|
|
|
|
(RuleName.Map.keys base_cases.rules)
|
2022-01-05 12:42:46 +03:00
|
|
|
in
|
2021-01-13 21:07:35 +03:00
|
|
|
match exceptions with
|
2022-01-05 12:42:46 +03:00
|
|
|
| [] -> Leaf base_case_as_rule_list
|
|
|
|
| _ -> Node (List.map build_tree exceptions, base_case_as_rule_list)
|
2020-11-25 18:51:19 +03:00
|
|
|
in
|
2021-01-13 21:07:35 +03:00
|
|
|
List.map build_tree base_cases
|
2020-11-25 18:51:19 +03:00
|
|
|
|
2022-08-25 17:08:08 +03:00
|
|
|
(** From the {!type: rule_tree}, builds an {!constructor: Dcalc.EDefault}
|
2020-12-14 19:00:42 +03:00
|
|
|
expression in the scope language. The [~toplevel] parameter is used to know
|
|
|
|
when to place the toplevel binding in the case of functions. *)
|
2022-03-06 16:15:09 +03:00
|
|
|
let rec rule_tree_to_expr
|
|
|
|
~(toplevel : bool)
|
2022-12-02 18:42:29 +03:00
|
|
|
~(is_reentrant_var : bool)
|
2023-11-03 12:29:32 +03:00
|
|
|
~(subscope : bool)
|
2022-03-06 16:15:09 +03:00
|
|
|
(ctx : ctx)
|
|
|
|
(def_pos : Pos.t)
|
2023-08-10 17:52:39 +03:00
|
|
|
(params : D.expr Var.t list option)
|
2022-11-07 15:50:28 +03:00
|
|
|
(tree : rule_tree) : untyped Ast.expr boxed =
|
2022-08-26 16:21:47 +03:00
|
|
|
let emark = Untyped { pos = def_pos } in
|
2022-01-05 12:42:46 +03:00
|
|
|
let exceptions, base_rules =
|
2020-12-18 17:59:15 +03:00
|
|
|
match tree with Leaf r -> [], r | Node (exceptions, r) -> exceptions, r
|
|
|
|
in
|
2023-02-20 19:21:44 +03:00
|
|
|
(* because each rule has its own variables parameters and we want to convert
|
|
|
|
the whole rule tree into a function, we need to perform some alpha-renaming
|
|
|
|
of all the expressions *)
|
2023-08-10 17:52:39 +03:00
|
|
|
let substitute_parameter (e : D.expr boxed) (rule : D.rule) : D.expr boxed =
|
|
|
|
match params, rule.D.rule_parameter with
|
2023-02-28 16:40:05 +03:00
|
|
|
| Some new_params, Some (old_params_with_types, _) ->
|
2023-02-20 19:21:44 +03:00
|
|
|
let old_params, _ = List.split old_params_with_types in
|
2023-05-17 16:44:57 +03:00
|
|
|
let old_params = Array.of_list (List.map Mark.remove old_params) in
|
2023-02-20 19:21:44 +03:00
|
|
|
let new_params = Array.of_list new_params in
|
2023-05-17 16:44:57 +03:00
|
|
|
let binder = Bindlib.bind_mvar old_params (Mark.remove e) in
|
|
|
|
Mark.add (Mark.get 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
|
|
|
@@ Bindlib.box_apply2
|
2023-02-20 19:21:44 +03:00
|
|
|
(fun binder new_param -> Bindlib.msubst binder new_param)
|
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
|
|
|
binder
|
2023-02-20 19:21:44 +03:00
|
|
|
(new_params |> Array.map Bindlib.box_var |> Bindlib.box_array)
|
2020-11-25 18:51:19 +03:00
|
|
|
| None, None -> e
|
|
|
|
| _ -> assert false
|
|
|
|
(* should not happen *)
|
|
|
|
in
|
2022-03-06 16:15:09 +03:00
|
|
|
let ctx =
|
2023-02-27 11:50:42 +03:00
|
|
|
match params with
|
2022-03-06 16:15:09 +03:00
|
|
|
| None -> ctx
|
2023-02-20 19:21:44 +03:00
|
|
|
| Some new_params ->
|
|
|
|
ListLabels.fold_left new_params ~init:ctx ~f:(fun ctx new_param ->
|
|
|
|
match Var.Map.find_opt new_param ctx.var_mapping with
|
|
|
|
| None ->
|
|
|
|
let new_param_scope = Var.make (Bindlib.name_of new_param) in
|
|
|
|
{
|
|
|
|
ctx with
|
|
|
|
var_mapping =
|
|
|
|
Var.Map.add new_param new_param_scope ctx.var_mapping;
|
|
|
|
}
|
|
|
|
| Some _ ->
|
|
|
|
(* We only create a mapping if none exists because
|
|
|
|
[rule_tree_to_expr] is called recursively on the exceptions of
|
|
|
|
the tree and we don't want to create a new Scopelang variable for
|
|
|
|
the parameter at each tree level. *)
|
|
|
|
ctx)
|
2022-03-06 16:15:09 +03:00
|
|
|
in
|
2022-01-05 12:42:46 +03:00
|
|
|
let base_just_list =
|
2023-08-10 17:52:39 +03:00
|
|
|
List.map (fun rule -> substitute_parameter rule.D.rule_just rule) base_rules
|
2022-01-05 12:42:46 +03:00
|
|
|
in
|
|
|
|
let base_cons_list =
|
2023-08-10 17:52:39 +03:00
|
|
|
List.map (fun rule -> substitute_parameter rule.D.rule_cons rule) base_rules
|
2022-01-05 12:42:46 +03:00
|
|
|
in
|
2023-08-10 17:52:39 +03:00
|
|
|
let translate_and_unbox_list (list : D.expr boxed list) :
|
2022-11-07 15:50:28 +03:00
|
|
|
untyped Ast.expr boxed list =
|
2022-03-06 16:15:09 +03:00
|
|
|
List.map
|
|
|
|
(fun e ->
|
2022-07-13 16:25:11 +03:00
|
|
|
(* There are two levels of boxing here, the outermost is introduced by
|
2022-03-06 16:15:09 +03:00
|
|
|
the [translate_expr] function for which all of the bindings should
|
|
|
|
have been closed by now, so we can safely unbox. *)
|
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
|
|
|
translate_expr ctx (Expr.unbox e))
|
2022-03-06 16:15:09 +03:00
|
|
|
list
|
|
|
|
in
|
2022-01-05 12:42:46 +03:00
|
|
|
let default_containing_base_cases =
|
2023-11-08 20:25:50 +03:00
|
|
|
Expr.edefault
|
|
|
|
~excepts:
|
2023-11-10 20:14:48 +03:00
|
|
|
(List.fold_right2
|
|
|
|
(fun base_just base_cons acc ->
|
|
|
|
match Expr.unbox base_just with
|
|
|
|
| ELit (LBool false), _ -> acc
|
|
|
|
| _ ->
|
|
|
|
Expr.edefault
|
|
|
|
~excepts:[]
|
|
|
|
(* Here we insert the logging command that records when a
|
|
|
|
decision is taken for the value of a variable. *)
|
|
|
|
~just:(tag_with_log_entry base_just PosRecordIfTrueBool [])
|
|
|
|
~cons:(Expr.epuredefault base_cons emark)
|
|
|
|
emark
|
|
|
|
:: acc)
|
2023-11-08 20:25:50 +03:00
|
|
|
(translate_and_unbox_list base_just_list)
|
2023-11-10 20:14:48 +03:00
|
|
|
(translate_and_unbox_list base_cons_list)
|
|
|
|
[])
|
2023-11-08 20:25:50 +03:00
|
|
|
~just:(Expr.elit (LBool false) emark)
|
|
|
|
~cons:(Expr.eemptyerror emark) emark
|
2022-01-05 12:42:46 +03:00
|
|
|
in
|
2020-12-18 17:59:15 +03:00
|
|
|
let exceptions =
|
2022-12-02 14:07:26 +03:00
|
|
|
List.map
|
2023-11-07 20:25:57 +03:00
|
|
|
(rule_tree_to_expr ~toplevel:false ~is_reentrant_var ~subscope ctx def_pos
|
|
|
|
params)
|
2022-12-02 14:07:26 +03:00
|
|
|
exceptions
|
2020-12-18 17:59:15 +03:00
|
|
|
in
|
2020-11-27 18:27:10 +03:00
|
|
|
let default =
|
2023-11-03 12:29:32 +03:00
|
|
|
if exceptions = [] then default_containing_base_cases
|
|
|
|
else
|
2023-11-08 20:25:50 +03:00
|
|
|
Expr.edefault ~excepts:exceptions
|
|
|
|
~just:(Expr.elit (LBool true) emark)
|
|
|
|
~cons:
|
|
|
|
(* if toplevel then Expr.eerroronempty default_containing_base_cases emark
|
|
|
|
* else *)
|
|
|
|
default_containing_base_cases emark
|
2023-11-03 12:29:32 +03:00
|
|
|
in
|
|
|
|
let default =
|
2023-11-07 20:25:57 +03:00
|
|
|
if toplevel && not (subscope && is_reentrant_var) then
|
|
|
|
Expr.eerroronempty default emark
|
2023-11-03 19:15:55 +03:00
|
|
|
else default
|
2020-11-27 18:27:10 +03:00
|
|
|
in
|
2023-08-10 17:52:39 +03:00
|
|
|
match params, (List.hd base_rules).D.rule_parameter with
|
2020-11-25 18:51:19 +03:00
|
|
|
| None, None -> default
|
2023-02-28 16:40:05 +03:00
|
|
|
| Some new_params, Some (ls, _) ->
|
2023-02-20 19:21:44 +03:00
|
|
|
let _, tys = List.split ls in
|
2020-11-27 18:27:10 +03:00
|
|
|
if toplevel then
|
2020-12-31 02:28:26 +03:00
|
|
|
(* When we're creating a function from multiple defaults, we must check
|
2022-12-02 14:07:26 +03:00
|
|
|
that the result returned by the function is not empty, unless we're
|
2022-12-02 18:42:29 +03:00
|
|
|
dealing with a context variable which is reentrant (either in the
|
|
|
|
caller or callee). In this case the ErrorOnEmpty will be added later in
|
|
|
|
the scopelang->dcalc translation. *)
|
2022-08-25 13:09:51 +03:00
|
|
|
Expr.make_abs
|
2023-02-20 19:21:44 +03:00
|
|
|
(new_params
|
|
|
|
|> List.map (fun x -> Var.Map.find x ctx.var_mapping)
|
|
|
|
|> Array.of_list)
|
|
|
|
default tys def_pos
|
2020-11-27 18:27:10 +03:00
|
|
|
else default
|
2020-12-14 19:00:42 +03:00
|
|
|
| _ -> (* should not happen *) assert false
|
2020-11-25 18:51:19 +03:00
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
(** {1 AST translation} *)
|
2020-11-25 18:51:19 +03:00
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
(** Translates a definition inside a scope, the resulting expression should be
|
2022-08-25 17:08:08 +03:00
|
|
|
an {!constructor: Dcalc.EDefault} *)
|
2022-03-06 16:15:09 +03:00
|
|
|
let translate_def
|
2023-04-18 12:06:58 +03:00
|
|
|
~(is_cond : bool)
|
|
|
|
~(is_subscope_var : bool)
|
2022-03-06 16:15:09 +03:00
|
|
|
(ctx : ctx)
|
2023-08-10 17:52:39 +03:00
|
|
|
(def_info : D.ScopeDef.t)
|
|
|
|
(def : D.rule RuleName.Map.t)
|
2023-05-17 16:44:57 +03:00
|
|
|
(params : (Uid.MarkedString.info * typ) list Mark.pos option)
|
2022-08-25 18:29:00 +03:00
|
|
|
(typ : typ)
|
2023-08-10 17:52:39 +03:00
|
|
|
(io : D.io)
|
2023-04-18 12:06:58 +03:00
|
|
|
(exc_graph : Desugared.Dependency.ExceptionsDependencies.t) :
|
|
|
|
untyped Ast.expr boxed =
|
2020-11-25 16:35:26 +03:00
|
|
|
(* Here, we have to transform this list of rules into a default tree. *)
|
2023-04-18 12:06:58 +03:00
|
|
|
let top_list = def_map_to_tree def exc_graph in
|
2023-02-28 16:40:05 +03:00
|
|
|
let is_input =
|
2023-08-10 17:52:39 +03:00
|
|
|
match Mark.remove io.D.io_input with OnlyInput -> true | _ -> false
|
2022-12-02 18:42:29 +03:00
|
|
|
in
|
2023-02-28 16:40:05 +03:00
|
|
|
let is_reentrant =
|
2023-08-10 17:52:39 +03:00
|
|
|
match Mark.remove io.D.io_input with Reentrant -> true | _ -> false
|
2020-12-31 02:28:26 +03:00
|
|
|
in
|
2023-08-10 17:52:39 +03:00
|
|
|
let top_value : D.rule option =
|
2023-02-28 16:40:05 +03:00
|
|
|
if is_cond && ((not is_subscope_var) || (is_subscope_var && is_input)) then
|
|
|
|
(* We add the bottom [false] value for conditions, only for the scope
|
|
|
|
where the condition is declared. Except when the variable is an input,
|
|
|
|
where we want the [false] to be added at each caller parent scope. *)
|
2023-08-10 17:52:39 +03:00
|
|
|
Some (D.always_false_rule (D.ScopeDef.get_position def_info) params)
|
2023-02-28 16:40:05 +03:00
|
|
|
else None
|
|
|
|
in
|
|
|
|
if
|
|
|
|
RuleName.Map.cardinal def = 0
|
|
|
|
&& is_subscope_var
|
|
|
|
(* Here we have a special case for the empty definitions. Indeed, we could
|
|
|
|
use the code for the regular case below that would create a convoluted
|
|
|
|
default always returning empty error, and this would be correct. But it
|
|
|
|
gets more complicated with functions. Indeed, if we create an empty
|
|
|
|
definition for a subscope argument whose type is a function, we get
|
|
|
|
something like [fun () -> (fun real_param -> < ... >)] that is passed as
|
|
|
|
an argument to the subscope. The sub-scope de-thunks but the de-thunking
|
|
|
|
does not return empty error, signalling there is not reentrant variable,
|
|
|
|
because functions are values! So the subscope does not see that there is
|
|
|
|
not reentrant variable and does not pick its internal definition instead.
|
|
|
|
See [test/test_scope/subscope_function_arg_not_defined.catala_en] for a
|
|
|
|
test case exercising that subtlety.
|
|
|
|
|
|
|
|
To avoid this complication we special case here and put an empty error
|
|
|
|
for all subscope variables that are not defined. It covers the subtlety
|
|
|
|
with functions described above but also conditions with the false default
|
|
|
|
value. *)
|
|
|
|
&& not (is_cond && is_input)
|
|
|
|
(* However, this special case suffers from an exception: when a condition is
|
|
|
|
defined as an OnlyInput to a subscope, since the [false] default value
|
|
|
|
will not be provided by the calee scope, it has to be placed in the
|
|
|
|
caller. *)
|
|
|
|
then
|
2023-08-10 17:52:39 +03:00
|
|
|
let m = Untyped { pos = D.ScopeDef.get_position def_info } in
|
2023-03-30 19:53:07 +03:00
|
|
|
let empty_error = Expr.eemptyerror m in
|
2023-02-28 16:40:05 +03:00
|
|
|
match params with
|
|
|
|
| Some (ps, _) ->
|
|
|
|
let labels, tys = List.split ps in
|
|
|
|
Expr.make_abs
|
|
|
|
(Array.of_list
|
2023-05-17 16:44:57 +03:00
|
|
|
(List.map (fun lbl -> Var.make (Mark.remove lbl)) labels))
|
2023-02-28 16:40:05 +03:00
|
|
|
empty_error tys (Expr.mark_pos m)
|
|
|
|
| _ -> empty_error
|
|
|
|
else
|
2023-11-03 12:29:32 +03:00
|
|
|
rule_tree_to_expr ~toplevel:true ~is_reentrant_var:is_reentrant
|
|
|
|
~subscope:is_subscope_var ctx
|
2023-08-10 17:52:39 +03:00
|
|
|
(D.ScopeDef.get_position def_info)
|
2023-02-28 16:40:05 +03:00
|
|
|
(Option.map
|
|
|
|
(fun (ps, _) ->
|
2023-05-17 16:44:57 +03:00
|
|
|
(List.map (fun (lbl, _) -> Var.make (Mark.remove lbl))) ps)
|
2023-02-28 16:40:05 +03:00
|
|
|
params)
|
|
|
|
(match top_list, top_value with
|
|
|
|
| [], None ->
|
|
|
|
(* In this case, there are no rules to define the expression and no
|
|
|
|
default value so we put an empty rule. *)
|
2023-08-10 17:52:39 +03:00
|
|
|
Leaf [D.empty_rule (Mark.get typ) params]
|
2023-02-28 16:40:05 +03:00
|
|
|
| [], Some top_value ->
|
|
|
|
(* In this case, there are no rules to define the expression but a
|
|
|
|
default value so we put it. *)
|
|
|
|
Leaf [top_value]
|
|
|
|
| _, Some top_value ->
|
|
|
|
(* When there are rules + a default value, we put the rules as
|
|
|
|
exceptions to the default value *)
|
|
|
|
Node (top_list, [top_value])
|
|
|
|
| [top_tree], None -> top_tree
|
2023-08-10 17:52:39 +03:00
|
|
|
| _, None -> Node (top_list, [D.empty_rule (Mark.get typ) params]))
|
2020-11-25 16:35:26 +03:00
|
|
|
|
2023-04-18 12:06:58 +03:00
|
|
|
let translate_rule
|
|
|
|
ctx
|
2023-08-10 17:52:39 +03:00
|
|
|
(scope : D.scope)
|
2023-04-18 12:06:58 +03:00
|
|
|
(exc_graphs :
|
2024-04-04 11:56:56 +03:00
|
|
|
Desugared.Dependency.ExceptionsDependencies.t D.ScopeDef.Map.t) = function
|
|
|
|
| Desugared.Dependency.Vertex.Var (var, state) -> (
|
2024-03-28 19:05:12 +03:00
|
|
|
let pos = Mark.get (ScopeVar.get_info var) in
|
2024-04-04 11:56:56 +03:00
|
|
|
(* TODO: this may point to the place where the variable was declared instead
|
|
|
|
of the binding in the definition being explored. Needs double-checking
|
|
|
|
and maybe adding more position information *)
|
2022-11-28 17:37:32 +03:00
|
|
|
let scope_def =
|
2024-03-28 19:05:12 +03:00
|
|
|
D.ScopeDef.Map.find ((var, pos), D.ScopeDef.Var state) scope.scope_defs
|
2022-11-28 17:37:32 +03:00
|
|
|
in
|
2024-04-04 11:56:56 +03:00
|
|
|
match ScopeVar.Map.find_opt var scope.scope_sub_scopes with
|
|
|
|
| None -> (
|
|
|
|
let var_def = scope_def.D.scope_def_rules in
|
2024-03-29 20:39:20 +03:00
|
|
|
let var_params = scope_def.D.scope_def_parameters in
|
|
|
|
let var_typ = scope_def.D.scope_def_typ in
|
|
|
|
let is_cond = scope_def.D.scope_def_is_condition in
|
|
|
|
match Mark.remove scope_def.D.scope_def_io.io_input with
|
|
|
|
| OnlyInput when not (RuleName.Map.is_empty var_def) ->
|
|
|
|
assert false (* error already raised *)
|
|
|
|
| OnlyInput -> []
|
|
|
|
(* we do not provide any definition for an input-only variable *)
|
|
|
|
| _ ->
|
|
|
|
let scope_def_key = (var, pos), D.ScopeDef.Var state in
|
|
|
|
let expr_def =
|
|
|
|
translate_def ctx scope_def_key var_def var_params var_typ
|
|
|
|
scope_def.D.scope_def_io
|
|
|
|
(D.ScopeDef.Map.find scope_def_key exc_graphs)
|
|
|
|
~is_cond ~is_subscope_var:false
|
|
|
|
in
|
|
|
|
let scope_var =
|
|
|
|
match ScopeVar.Map.find var ctx.scope_var_mapping, state with
|
|
|
|
| WholeVar v, None -> v
|
|
|
|
| States states, Some state -> List.assoc state states
|
|
|
|
| _ -> assert false
|
|
|
|
in
|
|
|
|
[
|
2024-04-04 11:56:56 +03:00
|
|
|
Ast.ScopeVarDefinition
|
|
|
|
{
|
|
|
|
var = scope_var, pos;
|
|
|
|
typ = var_typ;
|
|
|
|
io = scope_def.D.scope_def_io;
|
|
|
|
e = Expr.unbox expr_def;
|
|
|
|
};
|
|
|
|
])
|
|
|
|
| Some subscope ->
|
|
|
|
(* Before calling the subscope, we need to include all the re-definitions
|
|
|
|
of subscope parameters *)
|
|
|
|
let subscope_params =
|
|
|
|
D.ScopeDef.Map.fold
|
|
|
|
(fun def_key scope_def acc ->
|
|
|
|
match def_key with
|
|
|
|
| _, D.ScopeDef.Var _ -> acc
|
|
|
|
| (v, _), D.ScopeDef.SubScope _
|
|
|
|
when (not (ScopeVar.equal var v))
|
|
|
|
|| Mark.remove scope_def.D.scope_def_io.io_input = NoInput
|
|
|
|
&& RuleName.Map.is_empty scope_def.scope_def_rules ->
|
|
|
|
acc
|
|
|
|
| v, D.ScopeDef.SubScope { var_within_origin_scope; _ } ->
|
|
|
|
let pos = Mark.get v in
|
|
|
|
let def = scope_def.D.scope_def_rules in
|
|
|
|
let def_typ = scope_def.scope_def_typ in
|
|
|
|
let is_cond = scope_def.scope_def_is_condition in
|
|
|
|
assert (
|
|
|
|
(* an error should have been already raised *)
|
|
|
|
match scope_def.D.scope_def_io.io_input with
|
|
|
|
| NoInput, _ -> false
|
|
|
|
| OnlyInput, _ -> is_cond || not (RuleName.Map.is_empty def)
|
|
|
|
| _ -> true);
|
|
|
|
let var_within_origin_scope =
|
|
|
|
match
|
|
|
|
ScopeVar.Map.find var_within_origin_scope
|
|
|
|
ctx.scope_var_mapping
|
|
|
|
with
|
|
|
|
| WholeVar v -> v
|
|
|
|
| States ((_, v) :: _) -> v
|
|
|
|
| States [] -> assert false
|
|
|
|
in
|
|
|
|
let def_var =
|
|
|
|
Var.make
|
|
|
|
(String.concat "."
|
|
|
|
[
|
|
|
|
Mark.remove (ScopeVar.get_info (Mark.remove v));
|
|
|
|
Mark.remove (ScopeVar.get_info var_within_origin_scope);
|
|
|
|
])
|
|
|
|
in
|
|
|
|
let typ =
|
|
|
|
Scope.input_type def_typ scope_def.D.scope_def_io.D.io_input
|
|
|
|
in
|
|
|
|
let expr_def =
|
|
|
|
translate_def ctx def_key def scope_def.D.scope_def_parameters
|
|
|
|
def_typ scope_def.D.scope_def_io
|
|
|
|
(D.ScopeDef.Map.find def_key exc_graphs)
|
|
|
|
~is_cond ~is_subscope_var:true
|
|
|
|
in
|
|
|
|
ScopeVar.Map.add var_within_origin_scope
|
|
|
|
(def_var, pos, typ, expr_def)
|
|
|
|
acc)
|
|
|
|
scope.scope_defs ScopeVar.Map.empty
|
|
|
|
in
|
|
|
|
let subscope_param_map =
|
|
|
|
ScopeVar.Map.map (fun (_, _, _, expr) -> expr) subscope_params
|
|
|
|
in
|
|
|
|
let subscope_call_expr =
|
|
|
|
Expr.escopecall ~scope:subscope ~args:subscope_param_map
|
|
|
|
(Untyped { pos })
|
|
|
|
in
|
|
|
|
let subscope_expr = subscope_call_expr in
|
|
|
|
assert (RuleName.Map.is_empty scope_def.D.scope_def_rules);
|
|
|
|
(* The subscope will be defined by its inputs, it's not supposed to have
|
|
|
|
direct rules yet *)
|
|
|
|
let scope_info = ScopeName.Map.find subscope ctx.decl_ctx.ctx_scopes in
|
|
|
|
let subscope_var_dcalc =
|
|
|
|
match ScopeVar.Map.find var ctx.scope_var_mapping with
|
|
|
|
| WholeVar v -> v
|
|
|
|
| _ -> assert false
|
|
|
|
in
|
|
|
|
let subscope_def =
|
|
|
|
Ast.ScopeVarDefinition
|
|
|
|
{
|
|
|
|
var = subscope_var_dcalc, pos;
|
|
|
|
typ =
|
|
|
|
( TStruct scope_info.out_struct_name,
|
|
|
|
Mark.get (ScopeVar.get_info var) );
|
2024-03-29 20:39:20 +03:00
|
|
|
io = scope_def.D.scope_def_io;
|
2024-04-04 11:56:56 +03:00
|
|
|
e = Expr.unbox_closed subscope_expr;
|
2024-03-29 20:39:20 +03:00
|
|
|
}
|
2024-04-04 11:56:56 +03:00
|
|
|
in
|
|
|
|
[subscope_def])
|
2023-04-28 15:15:43 +03:00
|
|
|
| Assertion a_name ->
|
|
|
|
let assertion_expr =
|
2023-08-10 17:52:39 +03:00
|
|
|
D.AssertionName.Map.find a_name scope.scope_assertions
|
2023-04-28 15:15:43 +03:00
|
|
|
in
|
|
|
|
(* we unbox here because assertions do not have free variables (at this
|
2024-03-21 18:59:12 +03:00
|
|
|
point Bindlib variables are only for function parameters)*)
|
2023-04-28 15:15:43 +03:00
|
|
|
let assertion_expr = translate_expr ctx (Expr.unbox assertion_expr) in
|
|
|
|
[Ast.Assertion (Expr.unbox assertion_expr)]
|
2022-11-28 17:37:32 +03:00
|
|
|
|
2023-08-10 17:52:39 +03:00
|
|
|
let translate_scope_interface ctx scope =
|
2024-03-29 17:50:23 +03:00
|
|
|
let get_svar scope_def =
|
|
|
|
let svar_in_ty =
|
|
|
|
Scope.input_type scope_def.D.scope_def_typ
|
|
|
|
scope_def.D.scope_def_io.io_input
|
|
|
|
in
|
|
|
|
{
|
|
|
|
Ast.svar_in_ty;
|
|
|
|
svar_out_ty = scope_def.D.scope_def_typ;
|
|
|
|
svar_io = scope_def.scope_def_io;
|
|
|
|
}
|
|
|
|
in
|
2024-04-04 11:56:56 +03:00
|
|
|
let scope_sig =
|
|
|
|
(* Add the definitions of standard scope vars *)
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.fold
|
2023-08-10 17:52:39 +03:00
|
|
|
(fun var (states : D.var_or_states) acc ->
|
2022-03-06 16:15:09 +03:00
|
|
|
match states with
|
|
|
|
| WholeVar ->
|
|
|
|
let scope_def =
|
2024-04-04 11:56:56 +03:00
|
|
|
D.ScopeDef.Map.find
|
|
|
|
((var, Pos.no_pos), D.ScopeDef.Var None)
|
|
|
|
scope.D.scope_defs
|
2022-03-06 16:15:09 +03:00
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.add
|
|
|
|
(match ScopeVar.Map.find var ctx.scope_var_mapping with
|
2022-03-06 16:15:09 +03:00
|
|
|
| WholeVar v -> v
|
2024-03-29 17:50:23 +03:00
|
|
|
| States _ -> assert false)
|
2023-11-07 20:25:57 +03:00
|
|
|
(get_svar scope_def) acc
|
2022-03-06 16:15:09 +03:00
|
|
|
| States states ->
|
|
|
|
(* What happens in the case of variables with multiple states is
|
2022-11-07 15:50:28 +03:00
|
|
|
interesting. We need to create as many Var entries in the scope
|
|
|
|
signature as there are states. *)
|
2022-03-06 16:15:09 +03:00
|
|
|
List.fold_left
|
2022-08-25 13:09:51 +03:00
|
|
|
(fun acc (state : StateName.t) ->
|
2022-03-06 16:15:09 +03:00
|
|
|
let scope_def =
|
2023-08-10 17:52:39 +03:00
|
|
|
D.ScopeDef.Map.find
|
2024-03-28 19:05:12 +03:00
|
|
|
((var, Pos.no_pos), D.ScopeDef.Var (Some state))
|
2023-08-10 17:52:39 +03:00
|
|
|
scope.D.scope_defs
|
2022-03-06 16:15:09 +03:00
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.add
|
|
|
|
(match ScopeVar.Map.find var ctx.scope_var_mapping with
|
2024-03-29 17:50:23 +03:00
|
|
|
| WholeVar _ -> assert false
|
2022-03-06 16:15:09 +03:00
|
|
|
| States states' -> List.assoc state states')
|
2023-11-07 20:25:57 +03:00
|
|
|
(get_svar scope_def) acc)
|
2022-03-06 16:15:09 +03:00
|
|
|
acc states)
|
2022-11-21 12:12:45 +03:00
|
|
|
scope.scope_vars ScopeVar.Map.empty
|
2020-11-27 13:37:21 +03:00
|
|
|
in
|
2024-04-04 11:56:56 +03:00
|
|
|
let scope_sig =
|
|
|
|
(* Add the definition of vars corresponding to subscope calls, and their
|
|
|
|
parameters (subscope vars) *)
|
|
|
|
ScopeVar.Map.fold
|
|
|
|
(fun var _scope_name acc ->
|
2024-03-29 17:50:23 +03:00
|
|
|
let scope_def =
|
2024-04-04 11:56:56 +03:00
|
|
|
D.ScopeDef.Map.find
|
|
|
|
((var, Pos.no_pos), D.ScopeDef.Var None)
|
|
|
|
scope.D.scope_defs
|
2024-03-29 17:50:23 +03:00
|
|
|
in
|
|
|
|
ScopeVar.Map.add
|
|
|
|
(match ScopeVar.Map.find var ctx.scope_var_mapping with
|
2024-04-04 11:56:56 +03:00
|
|
|
| WholeVar v -> v
|
|
|
|
| States _ -> assert false)
|
2024-03-29 17:50:23 +03:00
|
|
|
(get_svar scope_def) acc)
|
|
|
|
scope.D.scope_sub_scopes scope_sig
|
|
|
|
in
|
2023-05-17 16:44:57 +03:00
|
|
|
let pos = Mark.get (ScopeName.get_info scope.scope_uid) in
|
2023-08-10 17:52:39 +03:00
|
|
|
Mark.add pos
|
2020-11-27 13:37:21 +03:00
|
|
|
{
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.scope_decl_name = scope.scope_uid;
|
2023-08-10 17:52:39 +03:00
|
|
|
Ast.scope_decl_rules = [];
|
2022-11-07 15:50:28 +03:00
|
|
|
Ast.scope_sig;
|
2023-01-20 20:18:53 +03:00
|
|
|
Ast.scope_options = scope.scope_options;
|
2020-11-27 13:37:21 +03:00
|
|
|
}
|
2020-11-25 16:35:26 +03:00
|
|
|
|
2023-08-10 17:52:39 +03:00
|
|
|
let translate_scope
|
|
|
|
(ctx : ctx)
|
|
|
|
(exc_graphs :
|
|
|
|
Desugared.Dependency.ExceptionsDependencies.t D.ScopeDef.Map.t)
|
|
|
|
(scope : D.scope) : untyped Ast.scope_decl Mark.pos =
|
|
|
|
let scope_dependencies =
|
|
|
|
Desugared.Dependency.build_scope_dependencies scope
|
|
|
|
in
|
|
|
|
Desugared.Dependency.check_for_cycle scope scope_dependencies;
|
|
|
|
let scope_ordering =
|
|
|
|
Desugared.Dependency.correct_computation_ordering scope_dependencies
|
|
|
|
in
|
|
|
|
let scope_decl_rules =
|
|
|
|
List.fold_left
|
|
|
|
(fun scope_decl_rules scope_def_key ->
|
|
|
|
let new_rules = translate_rule ctx scope exc_graphs scope_def_key in
|
|
|
|
scope_decl_rules @ new_rules)
|
|
|
|
[] scope_ordering
|
|
|
|
in
|
|
|
|
Mark.map
|
|
|
|
(fun s -> { s with Ast.scope_decl_rules })
|
|
|
|
(translate_scope_interface ctx scope)
|
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
(** {1 API} *)
|
|
|
|
|
2023-04-18 12:06:58 +03:00
|
|
|
let translate_program
|
2023-08-10 17:52:39 +03:00
|
|
|
(desugared : D.program)
|
2023-04-18 12:06:58 +03:00
|
|
|
(exc_graphs :
|
2023-08-10 17:52:39 +03:00
|
|
|
Desugared.Dependency.ExceptionsDependencies.t D.ScopeDef.Map.t) :
|
2023-04-18 12:06:58 +03:00
|
|
|
untyped Ast.program =
|
2022-11-07 15:50:28 +03:00
|
|
|
(* First we give mappings to all the locations between Desugared and This
|
|
|
|
involves creating a new Scopelang scope variable for every state of a
|
|
|
|
Desugared variable. *)
|
2023-11-20 18:01:06 +03:00
|
|
|
let ctx =
|
|
|
|
let ctx =
|
|
|
|
{
|
|
|
|
scope_var_mapping = ScopeVar.Map.empty;
|
|
|
|
var_mapping = Var.Map.empty;
|
|
|
|
reentrant_vars = ScopeVar.Map.empty;
|
|
|
|
decl_ctx = desugared.program_ctx;
|
|
|
|
}
|
|
|
|
in
|
|
|
|
let add_scope_mappings modul ctx =
|
2023-12-01 01:53:38 +03:00
|
|
|
ScopeName.Map.fold
|
|
|
|
(fun _ scdef ctx ->
|
2024-04-04 11:56:56 +03:00
|
|
|
let ctx =
|
|
|
|
(* Add normal scope vars to the env *)
|
|
|
|
ScopeVar.Map.fold
|
|
|
|
(fun scope_var (states : D.var_or_states) ctx ->
|
|
|
|
let var_name, var_pos = ScopeVar.get_info scope_var in
|
|
|
|
let new_var =
|
|
|
|
match states with
|
|
|
|
| D.WholeVar -> WholeVar (ScopeVar.fresh (var_name, var_pos))
|
|
|
|
| States states ->
|
|
|
|
let var_prefix = var_name ^ "_" in
|
|
|
|
let state_var state =
|
|
|
|
ScopeVar.fresh
|
|
|
|
(Mark.map (( ^ ) var_prefix) (StateName.get_info state))
|
2024-03-28 19:05:12 +03:00
|
|
|
in
|
2024-04-04 11:56:56 +03:00
|
|
|
States
|
|
|
|
(List.map (fun state -> state, state_var state) states)
|
|
|
|
in
|
|
|
|
let reentrant =
|
|
|
|
let state =
|
|
|
|
match states with
|
|
|
|
| D.WholeVar -> None
|
|
|
|
| States (s :: _) -> Some s
|
|
|
|
| States [] -> assert false
|
2024-03-28 19:05:12 +03:00
|
|
|
in
|
2024-04-04 11:56:56 +03:00
|
|
|
match
|
|
|
|
D.ScopeDef.Map.find_opt
|
|
|
|
((scope_var, Pos.no_pos), Var state)
|
|
|
|
scdef.D.scope_defs
|
|
|
|
with
|
|
|
|
| Some
|
|
|
|
{
|
|
|
|
scope_def_io = { io_input = Runtime.Reentrant, _; _ };
|
|
|
|
scope_def_typ;
|
|
|
|
_;
|
|
|
|
} ->
|
|
|
|
Some scope_def_typ
|
|
|
|
| _ -> None
|
|
|
|
in
|
|
|
|
{
|
|
|
|
ctx with
|
|
|
|
scope_var_mapping =
|
|
|
|
ScopeVar.Map.add scope_var new_var ctx.scope_var_mapping;
|
|
|
|
reentrant_vars =
|
|
|
|
Option.fold reentrant
|
|
|
|
~some:(fun ty ->
|
|
|
|
ScopeVar.Map.add scope_var ty ctx.reentrant_vars)
|
|
|
|
~none:ctx.reentrant_vars;
|
|
|
|
})
|
|
|
|
scdef.D.scope_vars ctx
|
|
|
|
in
|
|
|
|
let ctx =
|
|
|
|
(* Add scope vars pointing to subscope executions to the env (their
|
|
|
|
definitions are introduced during the processing of the rules
|
|
|
|
above) *)
|
|
|
|
ScopeVar.Map.fold
|
|
|
|
(fun var _ ctx ->
|
|
|
|
let var_name, var_pos = ScopeVar.get_info var in
|
|
|
|
let scope_var_mapping =
|
|
|
|
let new_var = WholeVar (ScopeVar.fresh (var_name, var_pos)) in
|
|
|
|
ScopeVar.Map.add var new_var ctx.scope_var_mapping
|
|
|
|
in
|
|
|
|
{ ctx with scope_var_mapping })
|
|
|
|
scdef.D.scope_sub_scopes ctx
|
|
|
|
in
|
|
|
|
ctx)
|
2023-11-20 18:01:06 +03:00
|
|
|
modul.D.module_scopes ctx
|
2023-11-07 17:46:19 +03:00
|
|
|
in
|
2023-11-20 18:01:06 +03:00
|
|
|
(* Todo: since we rename all scope vars at this point, it would be better to
|
|
|
|
have different types for Desugared.ScopeVar.t and Scopelang.ScopeVar.t *)
|
2023-12-01 01:53:38 +03:00
|
|
|
ModuleName.Map.fold
|
|
|
|
(fun _ m ctx -> add_scope_mappings m ctx)
|
2023-11-20 18:01:06 +03:00
|
|
|
desugared.D.program_modules
|
2023-12-01 01:53:38 +03:00
|
|
|
(add_scope_mappings desugared.D.program_root ctx)
|
2023-08-16 00:52:03 +03:00
|
|
|
in
|
2023-11-20 18:01:06 +03:00
|
|
|
let decl_ctx =
|
2023-08-10 17:52:39 +03:00
|
|
|
let ctx_scopes =
|
|
|
|
ScopeName.Map.map
|
|
|
|
(fun out_str ->
|
2023-12-01 01:53:38 +03:00
|
|
|
let out_struct_fields =
|
|
|
|
ScopeVar.Map.fold
|
|
|
|
(fun var fld out_map ->
|
|
|
|
let var' =
|
|
|
|
match ScopeVar.Map.find var ctx.scope_var_mapping with
|
|
|
|
| WholeVar v -> v
|
|
|
|
| States l -> snd (List.hd (List.rev l))
|
|
|
|
in
|
|
|
|
ScopeVar.Map.add var' fld out_map)
|
|
|
|
out_str.out_struct_fields ScopeVar.Map.empty
|
|
|
|
in
|
|
|
|
{ out_str with out_struct_fields })
|
2023-11-20 18:01:06 +03:00
|
|
|
desugared.program_ctx.ctx_scopes
|
2023-08-10 17:52:39 +03:00
|
|
|
in
|
2023-11-20 18:01:06 +03:00
|
|
|
{ desugared.program_ctx with ctx_scopes }
|
2022-11-17 19:13:35 +03:00
|
|
|
in
|
2023-12-01 01:53:38 +03:00
|
|
|
let ctx = { ctx with decl_ctx } in
|
2023-11-20 18:01:06 +03:00
|
|
|
let program_modules =
|
2023-12-01 01:53:38 +03:00
|
|
|
ModuleName.Map.map
|
|
|
|
(fun m ->
|
|
|
|
ScopeName.Map.map (translate_scope_interface ctx) m.D.module_scopes)
|
2023-08-10 17:52:39 +03:00
|
|
|
desugared.D.program_modules
|
2023-04-07 13:39:26 +03:00
|
|
|
in
|
2023-05-11 18:39:38 +03:00
|
|
|
let program_topdefs =
|
|
|
|
TopdefName.Map.mapi
|
|
|
|
(fun id -> function
|
2023-08-10 17:52:39 +03:00
|
|
|
| Some e, ty -> Expr.unbox (translate_expr ctx e), ty
|
|
|
|
| None, (_, pos) ->
|
|
|
|
Message.raise_spanned_error pos "No definition found for %a"
|
|
|
|
TopdefName.format id)
|
2023-11-20 18:01:06 +03:00
|
|
|
desugared.program_root.module_topdefs
|
2023-08-10 17:52:39 +03:00
|
|
|
in
|
|
|
|
let program_scopes =
|
|
|
|
ScopeName.Map.map
|
|
|
|
(translate_scope ctx exc_graphs)
|
2023-11-20 18:01:06 +03:00
|
|
|
desugared.D.program_root.module_scopes
|
2023-05-11 18:39:38 +03:00
|
|
|
in
|
|
|
|
{
|
2023-12-01 01:53:38 +03:00
|
|
|
Ast.program_module_name =
|
|
|
|
Option.map ModuleName.fresh desugared.D.program_module_name;
|
2023-05-11 18:39:38 +03:00
|
|
|
Ast.program_topdefs;
|
2023-08-10 17:52:39 +03:00
|
|
|
Ast.program_scopes;
|
2023-11-20 18:01:06 +03:00
|
|
|
Ast.program_ctx = ctx.decl_ctx;
|
2023-08-10 17:52:39 +03:00
|
|
|
Ast.program_modules;
|
2023-09-22 18:50:19 +03:00
|
|
|
Ast.program_lang = desugared.program_lang;
|
2020-12-04 18:40:17 +03:00
|
|
|
}
|