2022-03-08 17:03:14 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2020 Inria, contributor:
|
|
|
|
Nicolas Chataing <nicolas.chataing@ens.fr>
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
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
|
2020-11-23 11:22:47 +03:00
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
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
|
2020-11-23 11:22:47 +03:00
|
|
|
the License. *)
|
|
|
|
|
2023-09-01 17:24:27 +03:00
|
|
|
(** Scope dependencies computations using
|
|
|
|
{{:http://ocamlgraph.lri.fr/} OCamlgraph} *)
|
2020-12-14 19:00:42 +03:00
|
|
|
|
2022-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2022-08-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2021-01-13 21:07:35 +03:00
|
|
|
(** {1 Scope variables dependency graph} *)
|
|
|
|
|
|
|
|
(** {2 Graph declaration} *)
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
(** Vertices: scope variables or subscopes.
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
The vertices of the scope dependency graph are either :
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
- the variables of the scope ;
|
|
|
|
- the subscopes of the scope.
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
Indeed, during interpretation, subscopes are executed atomically. *)
|
2020-11-23 11:22:47 +03:00
|
|
|
module Vertex = struct
|
2023-04-28 15:15:43 +03:00
|
|
|
type t =
|
|
|
|
| Var of ScopeVar.t * StateName.t option
|
|
|
|
| SubScope of SubScopeName.t
|
|
|
|
| Assertion of Ast.AssertionName.t
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2020-11-23 14:20:38 +03:00
|
|
|
let hash x =
|
2020-11-23 18:12:45 +03:00
|
|
|
match x with
|
2022-08-25 13:09:51 +03:00
|
|
|
| Var (x, None) -> ScopeVar.hash x
|
|
|
|
| Var (x, Some sx) -> Int.logxor (ScopeVar.hash x) (StateName.hash sx)
|
2022-08-17 18:14:29 +03:00
|
|
|
| SubScope x -> SubScopeName.hash x
|
2023-04-28 15:15:43 +03:00
|
|
|
| Assertion a -> Ast.AssertionName.hash a
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2023-01-23 14:19:36 +03:00
|
|
|
let compare x y =
|
|
|
|
match x, y with
|
|
|
|
| Var (x, xst), Var (y, yst) -> (
|
|
|
|
match ScopeVar.compare x y with
|
|
|
|
| 0 -> Option.compare StateName.compare xst yst
|
|
|
|
| n -> n)
|
|
|
|
| SubScope x, SubScope y -> SubScopeName.compare x y
|
2023-04-28 15:15:43 +03:00
|
|
|
| Assertion a, Assertion b -> Ast.AssertionName.compare a b
|
2023-01-23 14:19:36 +03:00
|
|
|
| Var _, _ -> -1
|
|
|
|
| _, Var _ -> 1
|
2023-04-28 15:15:43 +03:00
|
|
|
| SubScope _, Assertion _ -> -1
|
|
|
|
| Assertion _, SubScope _ -> 1
|
2023-01-23 14:19:36 +03:00
|
|
|
| SubScope _, _ -> .
|
|
|
|
| _, SubScope _ -> .
|
2020-11-23 11:22:47 +03:00
|
|
|
|
|
|
|
let equal x y =
|
2022-05-04 16:48:03 +03:00
|
|
|
match x, y with
|
2023-01-23 14:19:36 +03:00
|
|
|
| Var (x, sx), Var (y, sy) ->
|
|
|
|
ScopeVar.equal x y && Option.equal StateName.equal sx sy
|
|
|
|
| SubScope x, SubScope y -> SubScopeName.equal x y
|
2023-04-28 15:15:43 +03:00
|
|
|
| Assertion a, Assertion b -> Ast.AssertionName.equal a b
|
2023-05-04 19:28:24 +03:00
|
|
|
| (Var _ | SubScope _ | Assertion _), _ -> false
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2023-07-12 12:48:46 +03:00
|
|
|
let format (fmt : Format.formatter) (x : t) : unit =
|
2020-11-23 18:12:45 +03:00
|
|
|
match x with
|
2023-07-12 12:48:46 +03:00
|
|
|
| Var (v, None) -> ScopeVar.format fmt v
|
2022-02-28 19:19:06 +03:00
|
|
|
| Var (v, Some sv) ->
|
2023-07-12 12:48:46 +03:00
|
|
|
Format.fprintf fmt "%a@%a" ScopeVar.format v StateName.format sv
|
|
|
|
| SubScope v -> SubScopeName.format fmt v
|
|
|
|
| Assertion a -> Ast.AssertionName.format fmt a
|
2023-01-23 14:19:36 +03:00
|
|
|
|
|
|
|
let info = function
|
|
|
|
| Var (v, None) -> ScopeVar.get_info v
|
|
|
|
| Var (_, Some sv) -> StateName.get_info sv
|
|
|
|
| SubScope v -> SubScopeName.get_info v
|
2023-04-28 15:15:43 +03:00
|
|
|
| Assertion a -> Ast.AssertionName.get_info a
|
2020-11-23 11:22:47 +03:00
|
|
|
end
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** On the edges, the label is the position of the expression responsible for
|
|
|
|
the use of the variable. In the graph, [x -> y] if [x] is used in the
|
|
|
|
definition of [y].*)
|
2020-11-23 11:22:47 +03:00
|
|
|
module Edge = struct
|
|
|
|
type t = Pos.t
|
|
|
|
|
|
|
|
let compare = compare
|
|
|
|
let default = Pos.no_pos
|
|
|
|
end
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
module ScopeDependencies =
|
|
|
|
Graph.Persistent.Digraph.ConcreteBidirectionalLabeled (Vertex) (Edge)
|
2020-12-14 19:00:42 +03:00
|
|
|
(** Module of the graph, provided by OCamlGraph *)
|
|
|
|
|
2020-11-23 11:22:47 +03:00
|
|
|
module TopologicalTraversal = Graph.Topological.Make (ScopeDependencies)
|
2020-12-14 19:00:42 +03:00
|
|
|
(** Module of the topological traversal of the graph, provided by OCamlGraph *)
|
2020-11-23 11:22:47 +03:00
|
|
|
|
|
|
|
module SCC = Graph.Components.Make (ScopeDependencies)
|
|
|
|
(** Tarjan's stongly connected components algorithm, provided by OCamlGraph *)
|
|
|
|
|
2021-01-13 21:07:35 +03:00
|
|
|
(** {2 Graph computations} *)
|
2020-12-14 19:00:42 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
(** Returns an ordering of the scope variables and subscope compatible with the
|
|
|
|
dependencies of the computation *)
|
2020-11-25 16:35:26 +03:00
|
|
|
let correct_computation_ordering (g : ScopeDependencies.t) : Vertex.t list =
|
|
|
|
List.rev (TopologicalTraversal.fold (fun sd acc -> sd :: acc) g [])
|
|
|
|
|
2020-11-23 11:22:47 +03:00
|
|
|
(** Outputs an error in case of cycles. *)
|
2020-11-25 13:53:56 +03:00
|
|
|
let check_for_cycle (scope : Ast.scope) (g : ScopeDependencies.t) : unit =
|
2023-03-06 16:42:24 +03:00
|
|
|
(* if there is a cycle, there will be a strongly connected component of
|
2022-03-08 17:03:14 +03:00
|
|
|
cardinality > 1 *)
|
2020-11-23 11:22:47 +03:00
|
|
|
let sccs = SCC.scc_list g in
|
2023-03-06 16:42:24 +03:00
|
|
|
match List.find_opt (function [] | [_] -> false | _ -> true) sccs with
|
|
|
|
| None -> ()
|
|
|
|
| Some [] -> assert false
|
|
|
|
| Some (v0 :: _ as scc) ->
|
|
|
|
let module VSet = Set.Make (Vertex) in
|
|
|
|
let scc = VSet.of_list scc in
|
|
|
|
let rec get_cycle cycle cycle_set v =
|
|
|
|
let cycle = v :: cycle in
|
|
|
|
let cycle_set = VSet.add v cycle_set in
|
|
|
|
let succ = ScopeDependencies.succ g v in
|
|
|
|
if List.exists (fun v -> VSet.mem v cycle_set) succ then
|
|
|
|
(* a cycle may be smaller than the scc, in that case we just return the
|
|
|
|
first one found *)
|
|
|
|
let rec cut_after acc = function
|
|
|
|
| [] -> acc
|
|
|
|
| v :: vs ->
|
|
|
|
if List.mem v succ then v :: acc else cut_after (v :: acc) vs
|
|
|
|
in
|
|
|
|
cut_after [] cycle
|
|
|
|
else
|
|
|
|
get_cycle cycle cycle_set
|
|
|
|
(List.find (fun succ -> VSet.mem succ scc) succ)
|
|
|
|
in
|
|
|
|
let cycle = get_cycle [] VSet.empty v0 in
|
2022-03-08 15:04:27 +03:00
|
|
|
let spans =
|
2023-03-06 16:42:24 +03:00
|
|
|
List.map2
|
|
|
|
(fun v1 v2 ->
|
|
|
|
let msg =
|
|
|
|
Format.asprintf "%a is used here in the definition of %a:"
|
2023-07-12 12:48:46 +03:00
|
|
|
Vertex.format v1 Vertex.format v2
|
2023-03-06 16:42:24 +03:00
|
|
|
in
|
|
|
|
let _, edge_pos, _ = ScopeDependencies.find_edge g v1 v2 in
|
|
|
|
Some msg, edge_pos)
|
|
|
|
cycle
|
|
|
|
(List.tl cycle @ [List.hd cycle])
|
2022-03-08 15:04:27 +03:00
|
|
|
in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error spans
|
2023-03-06 16:42:24 +03:00
|
|
|
"@[<hov 2>Cyclic dependency detected between the following variables of \
|
|
|
|
scope %a:@ @[<hv>%a@]@]"
|
2023-07-12 12:48:46 +03:00
|
|
|
ScopeName.format scope.scope_uid
|
2023-03-06 16:42:24 +03:00
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:(fun ppf () -> Format.fprintf ppf " →@ ")
|
2023-07-12 12:48:46 +03:00
|
|
|
Vertex.format)
|
2023-03-06 16:42:24 +03:00
|
|
|
(cycle @ [List.hd cycle])
|
2020-11-23 11:22:47 +03:00
|
|
|
|
2020-12-14 19:00:42 +03:00
|
|
|
(** Builds the dependency graph of a particular scope *)
|
2020-11-25 13:53:56 +03:00
|
|
|
let build_scope_dependencies (scope : Ast.scope) : ScopeDependencies.t =
|
|
|
|
let g = ScopeDependencies.empty in
|
|
|
|
(* Add all the vertices to the graph *)
|
|
|
|
let g =
|
2022-11-21 12:12:45 +03:00
|
|
|
ScopeVar.Map.fold
|
2022-08-25 13:09:51 +03:00
|
|
|
(fun (v : ScopeVar.t) var_or_state g ->
|
2022-02-28 19:19:06 +03:00
|
|
|
match var_or_state with
|
|
|
|
| Ast.WholeVar -> ScopeDependencies.add_vertex g (Vertex.Var (v, None))
|
|
|
|
| Ast.States states ->
|
2022-05-04 18:40:55 +03:00
|
|
|
List.fold_left
|
|
|
|
(fun g state ->
|
|
|
|
ScopeDependencies.add_vertex g (Vertex.Var (v, Some state)))
|
|
|
|
g states)
|
2020-11-25 13:53:56 +03:00
|
|
|
scope.scope_vars g
|
|
|
|
in
|
|
|
|
let g =
|
2022-11-21 12:12:45 +03:00
|
|
|
SubScopeName.Map.fold
|
2022-08-17 18:14:29 +03:00
|
|
|
(fun (v : SubScopeName.t) _ g ->
|
2020-11-25 13:53:56 +03:00
|
|
|
ScopeDependencies.add_vertex g (Vertex.SubScope v))
|
|
|
|
scope.scope_sub_scopes g
|
|
|
|
in
|
2023-04-28 15:15:43 +03:00
|
|
|
let g =
|
|
|
|
Ast.AssertionName.Map.fold
|
|
|
|
(fun a _ g -> ScopeDependencies.add_vertex g (Vertex.Assertion a))
|
|
|
|
scope.scope_assertions g
|
|
|
|
in
|
|
|
|
(* then add the edges *)
|
2020-11-25 13:53:56 +03:00
|
|
|
let g =
|
2023-04-18 11:31:44 +03:00
|
|
|
Ast.ScopeDef.Map.fold
|
2022-01-05 11:14:43 +03:00
|
|
|
(fun def_key scope_def g ->
|
|
|
|
let def = scope_def.Ast.scope_def_rules in
|
2020-11-25 13:53:56 +03:00
|
|
|
let fv = Ast.free_variables def in
|
2023-04-18 11:31:44 +03:00
|
|
|
Ast.ScopeDef.Map.fold
|
2020-11-27 13:37:21 +03:00
|
|
|
(fun fv_def fv_def_pos g ->
|
2022-05-04 16:48:03 +03:00
|
|
|
match def_key, fv_def with
|
2022-03-08 17:03:14 +03:00
|
|
|
| ( Ast.ScopeDef.Var (v_defined, s_defined),
|
|
|
|
Ast.ScopeDef.Var (v_used, s_used) ) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
(* simple case *)
|
2023-01-23 14:19:36 +03:00
|
|
|
if
|
|
|
|
ScopeVar.equal v_used v_defined
|
|
|
|
&& Option.equal StateName.equal s_used s_defined
|
|
|
|
then
|
2022-05-04 18:40:55 +03:00
|
|
|
(* variable definitions cannot be recursive *)
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error fv_def_pos
|
2022-05-04 18:40:55 +03:00
|
|
|
"The variable %a is used in one of its definitions, but \
|
|
|
|
recursion is forbidden in Catala"
|
2023-07-12 12:48:46 +03:00
|
|
|
Ast.ScopeDef.format def_key
|
2022-05-04 18:40:55 +03:00
|
|
|
else
|
2020-11-27 14:15:54 +03:00
|
|
|
let edge =
|
2022-02-28 19:19:06 +03:00
|
|
|
ScopeDependencies.E.create
|
|
|
|
(Vertex.Var (v_used, s_used))
|
2022-05-04 18:40:55 +03:00
|
|
|
fv_def_pos
|
|
|
|
(Vertex.Var (v_defined, s_defined))
|
2020-11-27 14:15:54 +03:00
|
|
|
in
|
|
|
|
ScopeDependencies.add_edge_e g edge
|
2022-09-20 15:04:07 +03:00
|
|
|
| ( Ast.ScopeDef.SubScopeVar (defined, _, _),
|
2022-05-04 18:40:55 +03:00
|
|
|
Ast.ScopeDef.Var (v_used, s_used) ) ->
|
|
|
|
(* here we are defining the input of a subscope using a var of the
|
|
|
|
scope *)
|
|
|
|
let edge =
|
|
|
|
ScopeDependencies.E.create
|
|
|
|
(Vertex.Var (v_used, s_used))
|
|
|
|
fv_def_pos (Vertex.SubScope defined)
|
|
|
|
in
|
|
|
|
ScopeDependencies.add_edge_e g edge
|
2022-09-20 15:04:07 +03:00
|
|
|
| ( Ast.ScopeDef.SubScopeVar (defined, _, _),
|
|
|
|
Ast.ScopeDef.SubScopeVar (used, _, _) ) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
(* here we are defining the input of a scope with the output of
|
|
|
|
another subscope *)
|
2023-01-23 14:19:36 +03:00
|
|
|
if SubScopeName.equal used defined then
|
2022-05-04 18:40:55 +03:00
|
|
|
(* subscopes are not recursive functions *)
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error fv_def_pos
|
2022-05-04 18:40:55 +03:00
|
|
|
"The subscope %a is used when defining one of its inputs, \
|
|
|
|
but recursion is forbidden in Catala"
|
2023-07-12 12:48:46 +03:00
|
|
|
SubScopeName.format defined
|
2022-05-04 18:40:55 +03:00
|
|
|
else
|
2020-11-27 14:15:54 +03:00
|
|
|
let edge =
|
2022-02-28 19:19:06 +03:00
|
|
|
ScopeDependencies.E.create (Vertex.SubScope used) fv_def_pos
|
2022-05-04 18:40:55 +03:00
|
|
|
(Vertex.SubScope defined)
|
2020-11-27 14:15:54 +03:00
|
|
|
in
|
2022-05-04 18:40:55 +03:00
|
|
|
ScopeDependencies.add_edge_e g edge
|
|
|
|
| ( Ast.ScopeDef.Var (v_defined, s_defined),
|
2022-09-20 15:04:07 +03:00
|
|
|
Ast.ScopeDef.SubScopeVar (used, _, _) ) ->
|
2022-05-04 18:40:55 +03:00
|
|
|
(* finally we define a scope var with the output of a subscope *)
|
|
|
|
let edge =
|
|
|
|
ScopeDependencies.E.create (Vertex.SubScope used) fv_def_pos
|
|
|
|
(Vertex.Var (v_defined, s_defined))
|
|
|
|
in
|
|
|
|
ScopeDependencies.add_edge_e g edge)
|
2020-11-25 13:53:56 +03:00
|
|
|
fv g)
|
|
|
|
scope.scope_defs g
|
2023-04-28 15:15:43 +03:00
|
|
|
in
|
|
|
|
let g =
|
|
|
|
Ast.AssertionName.Map.fold
|
|
|
|
(fun a_name a g ->
|
|
|
|
let used_vars = Ast.locations_used (Expr.unbox a) in
|
|
|
|
Ast.LocationSet.fold
|
|
|
|
(fun used_var g ->
|
|
|
|
let edge_from =
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove used_var with
|
2023-08-16 01:04:45 +03:00
|
|
|
| DesugaredScopeVar { name; state } ->
|
|
|
|
Some (Vertex.Var (Mark.remove name, state))
|
2023-08-10 17:52:39 +03:00
|
|
|
| SubScopeVar { alias; _ } ->
|
|
|
|
Some (Vertex.SubScope (Mark.remove alias))
|
2023-04-28 15:15:43 +03:00
|
|
|
| ToplevelVar _ -> None
|
|
|
|
(* we don't add this dependency because toplevel definitions are
|
|
|
|
outside the scope *)
|
|
|
|
in
|
|
|
|
match edge_from with
|
|
|
|
| None -> g
|
|
|
|
| Some edge_from ->
|
|
|
|
let edge =
|
|
|
|
ScopeDependencies.E.create edge_from (Expr.pos a)
|
|
|
|
(Vertex.Assertion a_name)
|
|
|
|
in
|
|
|
|
ScopeDependencies.add_edge_e g edge)
|
|
|
|
used_vars g)
|
|
|
|
scope.scope_assertions g
|
2020-11-25 13:53:56 +03:00
|
|
|
in
|
|
|
|
g
|
2021-01-13 21:07:35 +03:00
|
|
|
|
|
|
|
(** {1 Exceptions dependency graph} *)
|
|
|
|
|
|
|
|
(** {2 Graph declaration} *)
|
|
|
|
|
|
|
|
module ExceptionVertex = struct
|
2023-04-07 17:35:09 +03:00
|
|
|
type t = { rules : Pos.t RuleName.Map.t; label : LabelName.t }
|
|
|
|
|
2023-04-18 11:31:44 +03:00
|
|
|
let compare x y =
|
|
|
|
RuleName.Map.compare
|
|
|
|
(fun _ _ -> 0 (* we don't care about positions here*))
|
|
|
|
x.rules y.rules
|
2022-01-05 11:14:43 +03:00
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let hash (x : t) : int =
|
2023-04-07 17:35:09 +03:00
|
|
|
RuleName.Map.fold
|
|
|
|
(fun r _ acc -> Int.logxor (RuleName.hash r) acc)
|
|
|
|
x.rules 0
|
2021-01-13 21:07:35 +03:00
|
|
|
|
|
|
|
let equal x y = compare x y = 0
|
|
|
|
end
|
|
|
|
|
2022-07-13 16:00:57 +03:00
|
|
|
module EdgeExceptions = struct
|
|
|
|
type t = Pos.t list
|
|
|
|
|
|
|
|
let compare = compare
|
|
|
|
let default = [Pos.no_pos]
|
|
|
|
end
|
|
|
|
|
2021-01-13 21:07:35 +03:00
|
|
|
module ExceptionsDependencies =
|
2022-07-13 16:00:57 +03:00
|
|
|
Graph.Persistent.Digraph.ConcreteBidirectionalLabeled
|
|
|
|
(ExceptionVertex)
|
|
|
|
(EdgeExceptions)
|
2022-03-08 17:03:14 +03:00
|
|
|
(** Module of the graph, provided by OCamlGraph. [x -> y] if [y] is an exception
|
|
|
|
to [x] *)
|
2021-01-13 21:07:35 +03:00
|
|
|
|
|
|
|
module ExceptionsSCC = Graph.Components.Make (ExceptionsDependencies)
|
|
|
|
(** Tarjan's stongly connected components algorithm, provided by OCamlGraph *)
|
|
|
|
|
|
|
|
(** {2 Graph computations} *)
|
|
|
|
|
2022-07-13 16:00:57 +03:00
|
|
|
type exception_edge = {
|
2022-11-07 15:50:28 +03:00
|
|
|
label_from : LabelName.t;
|
|
|
|
label_to : LabelName.t;
|
2022-07-13 16:00:57 +03:00
|
|
|
edge_positions : Pos.t list;
|
|
|
|
}
|
|
|
|
|
2022-03-08 17:03:14 +03:00
|
|
|
let build_exceptions_graph
|
2022-11-21 12:12:45 +03:00
|
|
|
(def : Ast.rule RuleName.Map.t)
|
2022-05-04 19:40:28 +03:00
|
|
|
(def_info : Ast.ScopeDef.t) : ExceptionsDependencies.t =
|
2022-07-13 16:00:57 +03:00
|
|
|
(* First we partition the definitions into groups bearing the same label. To
|
|
|
|
handle the rules that were not labeled by the user, we create implicit
|
|
|
|
labels. *)
|
|
|
|
|
|
|
|
(* All the rules of the form [definition x ...] are base case with no explicit
|
|
|
|
label, so they should share this implicit label. *)
|
2022-11-07 15:50:28 +03:00
|
|
|
let base_case_implicit_label = LabelName.fresh ("base_case", Pos.no_pos) in
|
2022-07-13 16:00:57 +03:00
|
|
|
(* When declaring [exception definition x ...], it means there is a unique
|
|
|
|
rule [R] to which this can be an exception to. So we give a unique label to
|
|
|
|
all the rules that are implicitly exceptions to rule [R]. *)
|
2022-11-21 12:12:45 +03:00
|
|
|
let exception_to_rule_implicit_labels : LabelName.t RuleName.Map.t =
|
|
|
|
RuleName.Map.fold
|
2022-07-13 16:00:57 +03:00
|
|
|
(fun _ rule_from exception_to_rule_implicit_labels ->
|
|
|
|
match rule_from.Ast.rule_exception with
|
|
|
|
| Ast.ExceptionToRule (rule_to, _) -> (
|
2022-11-21 12:12:45 +03:00
|
|
|
match
|
|
|
|
RuleName.Map.find_opt rule_to exception_to_rule_implicit_labels
|
|
|
|
with
|
2022-07-13 16:00:57 +03:00
|
|
|
| Some _ ->
|
|
|
|
(* we already created the label *) exception_to_rule_implicit_labels
|
|
|
|
| None ->
|
2022-11-21 12:12:45 +03:00
|
|
|
RuleName.Map.add rule_to
|
2022-11-07 15:50:28 +03:00
|
|
|
(LabelName.fresh
|
2023-05-17 16:44:57 +03:00
|
|
|
( "exception_to_" ^ Mark.remove (RuleName.get_info rule_to),
|
2022-07-13 16:00:57 +03:00
|
|
|
Pos.no_pos ))
|
|
|
|
exception_to_rule_implicit_labels)
|
|
|
|
| _ -> exception_to_rule_implicit_labels)
|
2022-11-21 12:12:45 +03:00
|
|
|
def RuleName.Map.empty
|
2022-07-13 16:00:57 +03:00
|
|
|
in
|
|
|
|
(* When declaring [exception foo_l definition x ...], the rule is exception to
|
|
|
|
all the rules sharing label [foo_l]. So we give a unique label to all the
|
|
|
|
rules that are implicitly exceptions to rule [foo_l]. *)
|
2022-11-21 12:12:45 +03:00
|
|
|
let exception_to_label_implicit_labels : LabelName.t LabelName.Map.t =
|
|
|
|
RuleName.Map.fold
|
2022-07-13 16:00:57 +03:00
|
|
|
(fun _ rule_from
|
2022-11-21 12:12:45 +03:00
|
|
|
(exception_to_label_implicit_labels : LabelName.t LabelName.Map.t) ->
|
2022-07-13 16:00:57 +03:00
|
|
|
match rule_from.Ast.rule_exception with
|
|
|
|
| Ast.ExceptionToLabel (label_to, _) -> (
|
|
|
|
match
|
2022-11-21 12:12:45 +03:00
|
|
|
LabelName.Map.find_opt label_to exception_to_label_implicit_labels
|
2022-07-13 16:00:57 +03:00
|
|
|
with
|
|
|
|
| Some _ ->
|
|
|
|
(* we already created the label *)
|
|
|
|
exception_to_label_implicit_labels
|
|
|
|
| None ->
|
2022-11-21 12:12:45 +03:00
|
|
|
LabelName.Map.add label_to
|
2022-11-07 15:50:28 +03:00
|
|
|
(LabelName.fresh
|
2023-05-17 16:44:57 +03:00
|
|
|
( "exception_to_" ^ Mark.remove (LabelName.get_info label_to),
|
2022-07-13 16:00:57 +03:00
|
|
|
Pos.no_pos ))
|
|
|
|
exception_to_label_implicit_labels)
|
|
|
|
| _ -> exception_to_label_implicit_labels)
|
2022-11-21 12:12:45 +03:00
|
|
|
def LabelName.Map.empty
|
2022-07-12 16:34:40 +03:00
|
|
|
in
|
|
|
|
|
2022-07-13 16:00:57 +03:00
|
|
|
(* Now we have all the labels necessary to partition our rules into sets, each
|
|
|
|
one corresponding to a label relating to the structure of the exception
|
|
|
|
DAG. *)
|
|
|
|
let label_to_rule_sets =
|
2022-11-21 12:12:45 +03:00
|
|
|
RuleName.Map.fold
|
2022-07-13 16:00:57 +03:00
|
|
|
(fun rule_name rule rule_sets ->
|
|
|
|
let label_of_rule =
|
|
|
|
match rule.Ast.rule_label with
|
|
|
|
| Ast.ExplicitlyLabeled (l, _) -> l
|
|
|
|
| Ast.Unlabeled -> (
|
|
|
|
match rule.Ast.rule_exception with
|
|
|
|
| BaseCase -> base_case_implicit_label
|
|
|
|
| ExceptionToRule (r, _) ->
|
2022-11-21 12:12:45 +03:00
|
|
|
RuleName.Map.find r exception_to_rule_implicit_labels
|
2022-07-13 16:00:57 +03:00
|
|
|
| ExceptionToLabel (l', _) ->
|
2022-11-21 12:12:45 +03:00
|
|
|
LabelName.Map.find l' exception_to_label_implicit_labels)
|
2022-07-13 16:00:57 +03:00
|
|
|
in
|
2022-11-21 12:12:45 +03:00
|
|
|
LabelName.Map.update label_of_rule
|
2022-07-13 16:00:57 +03:00
|
|
|
(fun rule_set ->
|
2023-04-07 17:35:09 +03:00
|
|
|
let pos =
|
|
|
|
(* We have to overwrite the law info on tis position because the
|
|
|
|
pass at the surface AST level that fills the law info on
|
|
|
|
positions only does it for positions inside expressions, the
|
|
|
|
visitor in [surface/fill_positions.ml] does not go into the
|
2023-04-18 12:06:58 +03:00
|
|
|
info of [RuleName.t], etc. Related issue:
|
|
|
|
https://github.com/CatalaLang/catala/issues/194 *)
|
2023-04-07 17:35:09 +03:00
|
|
|
Pos.overwrite_law_info
|
|
|
|
(snd (RuleName.get_info rule.rule_id))
|
|
|
|
(Pos.get_law_info (Expr.pos rule.rule_just))
|
|
|
|
in
|
2022-07-13 16:00:57 +03:00
|
|
|
match rule_set with
|
2023-04-07 17:35:09 +03:00
|
|
|
| None -> Some (RuleName.Map.singleton rule_name pos)
|
|
|
|
| Some rule_set -> Some (RuleName.Map.add rule_name pos rule_set))
|
2022-07-13 16:00:57 +03:00
|
|
|
rule_sets)
|
2022-11-21 12:12:45 +03:00
|
|
|
def LabelName.Map.empty
|
2022-01-05 11:14:43 +03:00
|
|
|
in
|
2022-11-07 15:50:28 +03:00
|
|
|
let find_label_of_rule (r : RuleName.t) : LabelName.t =
|
2022-07-13 16:00:57 +03:00
|
|
|
fst
|
2022-11-21 12:12:45 +03:00
|
|
|
(LabelName.Map.choose
|
|
|
|
(LabelName.Map.filter
|
2023-04-07 17:35:09 +03:00
|
|
|
(fun _ rule_set -> RuleName.Map.mem r rule_set)
|
2022-07-13 16:00:57 +03:00
|
|
|
label_to_rule_sets))
|
2021-01-13 21:07:35 +03:00
|
|
|
in
|
2022-07-13 16:00:57 +03:00
|
|
|
(* Next, we collect the exception edges between those groups of rules referred
|
|
|
|
by their labels. This is also at this step that we check consistency of the
|
|
|
|
edges as they are declared at each rule but should be the same for all the
|
|
|
|
rules of the same group. *)
|
|
|
|
let exception_edges : exception_edge list =
|
2022-11-21 12:12:45 +03:00
|
|
|
RuleName.Map.fold
|
2022-07-13 16:00:57 +03:00
|
|
|
(fun rule_name rule exception_edges ->
|
|
|
|
let label_from = find_label_of_rule rule_name in
|
|
|
|
let label_to_and_pos =
|
|
|
|
match rule.Ast.rule_exception with
|
|
|
|
| Ast.BaseCase -> None
|
|
|
|
| Ast.ExceptionToRule (r', pos) -> Some (find_label_of_rule r', pos)
|
|
|
|
| Ast.ExceptionToLabel (l', pos) -> Some (l', pos)
|
|
|
|
in
|
|
|
|
match label_to_and_pos with
|
|
|
|
| None -> exception_edges
|
|
|
|
| Some (label_to, edge_pos) -> (
|
|
|
|
let other_edges_originating_from_same_label =
|
|
|
|
List.filter
|
2022-11-07 15:50:28 +03:00
|
|
|
(fun edge -> LabelName.compare edge.label_from label_from = 0)
|
2022-07-13 16:00:57 +03:00
|
|
|
exception_edges
|
|
|
|
in
|
|
|
|
(* We check the consistency*)
|
2022-11-07 15:50:28 +03:00
|
|
|
if LabelName.compare label_from label_to = 0 then
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_spanned_error edge_pos
|
2022-07-13 16:00:57 +03:00
|
|
|
"Cannot define rule as an exception to itself";
|
|
|
|
List.iter
|
|
|
|
(fun edge ->
|
2022-11-07 15:50:28 +03:00
|
|
|
if LabelName.compare edge.label_to label_to <> 0 then
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error
|
2022-07-13 16:00:57 +03:00
|
|
|
(( Some
|
2023-02-28 16:40:05 +03:00
|
|
|
"This definition contradicts other exception \
|
|
|
|
definitions:",
|
2022-07-13 16:00:57 +03:00
|
|
|
edge_pos )
|
|
|
|
:: List.map
|
2023-02-28 16:40:05 +03:00
|
|
|
(fun pos -> Some "Other exception definition:", pos)
|
2022-07-13 16:00:57 +03:00
|
|
|
edge.edge_positions)
|
2023-02-28 16:40:05 +03:00
|
|
|
"The definition of exceptions are inconsistent for variable \
|
2022-07-13 16:00:57 +03:00
|
|
|
%a."
|
2023-07-12 12:48:46 +03:00
|
|
|
Ast.ScopeDef.format def_info)
|
2022-07-13 16:00:57 +03:00
|
|
|
other_edges_originating_from_same_label;
|
|
|
|
(* Now we add the edge to the list*)
|
|
|
|
let existing_edge =
|
|
|
|
List.find_opt
|
|
|
|
(fun edge ->
|
2022-11-07 15:50:28 +03:00
|
|
|
LabelName.compare edge.label_from label_from = 0
|
|
|
|
&& LabelName.compare edge.label_to label_to = 0)
|
2022-07-13 16:00:57 +03:00
|
|
|
exception_edges
|
|
|
|
in
|
|
|
|
match existing_edge with
|
|
|
|
| None ->
|
|
|
|
{ label_from; label_to; edge_positions = [edge_pos] }
|
|
|
|
:: exception_edges
|
|
|
|
| Some existing_edge ->
|
|
|
|
{
|
|
|
|
label_from;
|
|
|
|
label_to;
|
|
|
|
edge_positions = edge_pos :: existing_edge.edge_positions;
|
|
|
|
}
|
|
|
|
:: List.filter (fun edge -> edge <> existing_edge) exception_edges))
|
|
|
|
def []
|
|
|
|
in
|
|
|
|
(* We've got the vertices and the edges, let's build the graph! *)
|
|
|
|
let g =
|
2022-11-21 12:12:45 +03:00
|
|
|
LabelName.Map.fold
|
2023-04-07 17:35:09 +03:00
|
|
|
(fun label rule_set g ->
|
|
|
|
ExceptionsDependencies.add_vertex g { rules = rule_set; label })
|
2022-07-13 16:00:57 +03:00
|
|
|
label_to_rule_sets ExceptionsDependencies.empty
|
2022-01-05 12:42:46 +03:00
|
|
|
in
|
2021-01-13 21:07:35 +03:00
|
|
|
(* then we add the edges *)
|
|
|
|
let g =
|
2022-07-13 16:00:57 +03:00
|
|
|
List.fold_left
|
|
|
|
(fun g edge ->
|
|
|
|
let rule_group_from =
|
2023-04-07 17:35:09 +03:00
|
|
|
{
|
|
|
|
ExceptionVertex.rules =
|
|
|
|
LabelName.Map.find edge.label_from label_to_rule_sets;
|
|
|
|
label = edge.label_from;
|
|
|
|
}
|
2022-11-21 12:12:45 +03:00
|
|
|
in
|
|
|
|
let rule_group_to =
|
2023-04-07 17:35:09 +03:00
|
|
|
{
|
|
|
|
ExceptionVertex.rules =
|
|
|
|
LabelName.Map.find edge.label_to label_to_rule_sets;
|
|
|
|
label = edge.label_to;
|
|
|
|
}
|
2022-07-13 16:00:57 +03:00
|
|
|
in
|
|
|
|
let edge =
|
|
|
|
ExceptionsDependencies.E.create rule_group_from edge.edge_positions
|
|
|
|
rule_group_to
|
|
|
|
in
|
|
|
|
ExceptionsDependencies.add_edge_e g edge)
|
|
|
|
g exception_edges
|
2021-01-13 21:07:35 +03:00
|
|
|
in
|
|
|
|
g
|
|
|
|
|
|
|
|
(** Outputs an error in case of cycles. *)
|
2023-02-28 16:40:05 +03:00
|
|
|
let check_for_exception_cycle
|
|
|
|
(def : Ast.rule RuleName.Map.t)
|
|
|
|
(g : ExceptionsDependencies.t) : unit =
|
2022-03-08 17:03:14 +03:00
|
|
|
(* if there is a cycle, there will be an strongly connected component of
|
|
|
|
cardinality > 1 *)
|
2021-01-13 21:07:35 +03:00
|
|
|
let sccs = ExceptionsSCC.scc_list g in
|
|
|
|
if List.length sccs < ExceptionsDependencies.nb_vertex g then
|
|
|
|
let scc = List.find (fun scc -> List.length scc > 1) sccs in
|
2022-03-08 15:04:27 +03:00
|
|
|
let spans =
|
2023-02-28 16:40:05 +03:00
|
|
|
List.rev_map
|
2023-04-07 17:35:09 +03:00
|
|
|
(fun (vs : ExceptionVertex.t) ->
|
|
|
|
let v, _ = RuleName.Map.choose vs.rules in
|
2023-02-28 16:40:05 +03:00
|
|
|
let rule = RuleName.Map.find v def in
|
2023-05-17 16:44:57 +03:00
|
|
|
let pos = Mark.get (RuleName.get_info rule.Ast.rule_id) in
|
2023-02-28 16:40:05 +03:00
|
|
|
None, pos)
|
|
|
|
scc
|
2022-03-08 15:04:27 +03:00
|
|
|
in
|
2023-04-07 17:35:09 +03:00
|
|
|
let v, _ = RuleName.Map.choose (List.hd scc).rules in
|
2023-06-13 12:27:45 +03:00
|
|
|
Message.raise_multispanned_error spans
|
2023-02-28 16:40:05 +03:00
|
|
|
"Exception cycle detected when defining %a: each of these %d exceptions \
|
|
|
|
applies over the previous one, and the first applies over the last"
|
2023-07-12 12:48:46 +03:00
|
|
|
RuleName.format v (List.length scc)
|