2020-11-25 20:00:34 +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. *)
|
|
|
|
|
|
|
|
(** Graph representation of the dependencies between scopes in the Catala
|
|
|
|
program. Vertices are functions, x -> y if x is used in the definition of y. *)
|
|
|
|
|
2021-01-21 23:33:04 +03:00
|
|
|
open Utils
|
2022-08-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2020-11-25 20:00:34 +03:00
|
|
|
|
2020-12-06 14:32:36 +03:00
|
|
|
module SVertex = struct
|
2022-08-12 23:42:39 +03:00
|
|
|
type t = ScopeName.t
|
2020-11-25 20:00:34 +03:00
|
|
|
|
2022-08-12 23:42:39 +03:00
|
|
|
let hash x = ScopeName.hash x
|
|
|
|
let compare = ScopeName.compare
|
|
|
|
let equal x y = ScopeName.compare x y = 0
|
2020-11-25 20:00:34 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
(** On the edges, the label is the expression responsible for the use of the
|
|
|
|
function *)
|
2020-12-06 14:32:36 +03:00
|
|
|
module SEdge = struct
|
2020-11-25 20:00:34 +03:00
|
|
|
type t = Pos.t
|
|
|
|
|
|
|
|
let compare = compare
|
|
|
|
let default = Pos.no_pos
|
|
|
|
end
|
|
|
|
|
2020-12-06 14:32:36 +03:00
|
|
|
module SDependencies =
|
|
|
|
Graph.Persistent.Digraph.ConcreteBidirectionalLabeled (SVertex) (SEdge)
|
2022-03-08 17:03:14 +03:00
|
|
|
|
2020-12-06 14:32:36 +03:00
|
|
|
module STopologicalTraversal = Graph.Topological.Make (SDependencies)
|
2020-11-25 20:00:34 +03:00
|
|
|
|
2020-12-06 14:32:36 +03:00
|
|
|
module SSCC = Graph.Components.Make (SDependencies)
|
2020-11-25 20:00:34 +03:00
|
|
|
(** Tarjan's stongly connected components algorithm, provided by OCamlGraph *)
|
|
|
|
|
2020-12-06 14:32:36 +03:00
|
|
|
let build_program_dep_graph (prgm : Ast.program) : SDependencies.t =
|
|
|
|
let g = SDependencies.empty in
|
|
|
|
let g =
|
|
|
|
Ast.ScopeMap.fold
|
|
|
|
(fun v _ g -> SDependencies.add_vertex g v)
|
|
|
|
prgm.program_scopes g
|
|
|
|
in
|
2020-11-25 20:00:34 +03:00
|
|
|
Ast.ScopeMap.fold
|
|
|
|
(fun scope_name scope g ->
|
|
|
|
let subscopes =
|
|
|
|
List.fold_left
|
|
|
|
(fun acc r ->
|
|
|
|
match r with
|
2020-12-10 20:11:43 +03:00
|
|
|
| Ast.Definition _ | Ast.Assertion _ -> acc
|
2020-11-27 14:15:54 +03:00
|
|
|
| Ast.Call (subscope, subindex) ->
|
2020-11-27 13:37:21 +03:00
|
|
|
if subscope = scope_name then
|
|
|
|
Errors.raise_spanned_error
|
2022-05-30 12:20:48 +03:00
|
|
|
(Marked.get_mark
|
2022-08-12 23:42:39 +03:00
|
|
|
(ScopeName.get_info scope.Ast.scope_decl_name))
|
2022-03-08 15:04:27 +03:00
|
|
|
"The scope %a is calling into itself as a subscope, which is \
|
|
|
|
forbidden since Catala does not provide recursion"
|
2022-08-12 23:42:39 +03:00
|
|
|
ScopeName.format_t scope.Ast.scope_decl_name
|
2020-11-27 14:15:54 +03:00
|
|
|
else
|
|
|
|
Ast.ScopeMap.add subscope
|
2022-08-17 18:14:29 +03:00
|
|
|
(Marked.get_mark (SubScopeName.get_info subindex))
|
2020-11-27 14:15:54 +03:00
|
|
|
acc)
|
|
|
|
Ast.ScopeMap.empty scope.Ast.scope_decl_rules
|
2020-11-25 20:00:34 +03:00
|
|
|
in
|
2020-11-27 14:15:54 +03:00
|
|
|
Ast.ScopeMap.fold
|
|
|
|
(fun subscope pos g ->
|
2020-12-06 14:32:36 +03:00
|
|
|
let edge = SDependencies.E.create subscope pos scope_name in
|
|
|
|
SDependencies.add_edge_e g edge)
|
2020-11-25 20:00:34 +03:00
|
|
|
subscopes g)
|
2020-12-04 18:40:17 +03:00
|
|
|
prgm.program_scopes g
|
2020-11-25 20:00:34 +03:00
|
|
|
|
2020-12-06 14:32:36 +03:00
|
|
|
let check_for_cycle_in_scope (g : SDependencies.t) : unit =
|
2020-11-25 20:00:34 +03:00
|
|
|
(* if there is a cycle, there will be an strongly connected component of
|
|
|
|
cardinality > 1 *)
|
2020-12-06 14:32:36 +03:00
|
|
|
let sccs = SSCC.scc_list g in
|
|
|
|
if List.length sccs < SDependencies.nb_vertex g then
|
2020-11-25 20:00:34 +03:00
|
|
|
let scc = List.find (fun scc -> List.length scc > 1) sccs in
|
2022-03-08 15:04:27 +03:00
|
|
|
let spans =
|
|
|
|
List.flatten
|
|
|
|
(List.map
|
|
|
|
(fun v ->
|
|
|
|
let var_str, var_info =
|
2022-08-16 11:04:01 +03:00
|
|
|
Format.asprintf "%a" ScopeName.format_t v, ScopeName.get_info v
|
2022-03-08 15:04:27 +03:00
|
|
|
in
|
|
|
|
let succs = SDependencies.succ_e g v in
|
|
|
|
let _, edge_pos, succ =
|
|
|
|
List.find (fun (_, _, succ) -> List.mem succ scc) succs
|
|
|
|
in
|
2022-08-12 23:42:39 +03:00
|
|
|
let succ_str = Format.asprintf "%a" ScopeName.format_t succ in
|
2022-03-08 15:04:27 +03:00
|
|
|
[
|
|
|
|
( Some ("Cycle variable " ^ var_str ^ ", declared:"),
|
2022-05-30 12:20:48 +03:00
|
|
|
Marked.get_mark var_info );
|
2022-03-08 15:04:27 +03:00
|
|
|
( Some
|
|
|
|
("Used here in the definition of another cycle variable "
|
|
|
|
^ succ_str
|
|
|
|
^ ":"),
|
|
|
|
edge_pos );
|
|
|
|
])
|
|
|
|
scc)
|
|
|
|
in
|
|
|
|
Errors.raise_multispanned_error spans
|
|
|
|
"Cyclic dependency detected between scopes!"
|
2020-11-25 20:00:34 +03:00
|
|
|
|
2022-08-12 23:42:39 +03:00
|
|
|
let get_scope_ordering (g : SDependencies.t) : ScopeName.t list =
|
2020-12-06 14:32:36 +03:00
|
|
|
List.rev (STopologicalTraversal.fold (fun sd acc -> sd :: acc) g [])
|
|
|
|
|
|
|
|
module TVertex = struct
|
2022-08-12 23:42:39 +03:00
|
|
|
type t = Struct of StructName.t | Enum of EnumName.t
|
2020-12-06 14:32:36 +03:00
|
|
|
|
|
|
|
let hash x =
|
2022-08-16 11:04:01 +03:00
|
|
|
match x with Struct x -> StructName.hash x | Enum x -> EnumName.hash x
|
2020-12-06 14:32:36 +03:00
|
|
|
|
|
|
|
let compare x y =
|
|
|
|
match x, y with
|
2022-08-12 23:42:39 +03:00
|
|
|
| Struct x, Struct y -> StructName.compare x y
|
|
|
|
| Enum x, Enum y -> EnumName.compare x y
|
2020-12-06 14:32:36 +03:00
|
|
|
| Struct _, Enum _ -> 1
|
|
|
|
| Enum _, Struct _ -> -1
|
|
|
|
|
|
|
|
let equal x y =
|
|
|
|
match x, y with
|
2022-08-12 23:42:39 +03:00
|
|
|
| Struct x, Struct y -> StructName.compare x y = 0
|
|
|
|
| Enum x, Enum y -> EnumName.compare x y = 0
|
2020-12-06 14:32:36 +03:00
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
let format_t (fmt : Format.formatter) (x : t) : unit =
|
|
|
|
match x with
|
2022-08-12 23:42:39 +03:00
|
|
|
| Struct x -> StructName.format_t fmt x
|
|
|
|
| Enum x -> EnumName.format_t fmt x
|
2020-12-06 14:32:36 +03:00
|
|
|
|
|
|
|
let get_info (x : t) =
|
|
|
|
match x with
|
2022-08-12 23:42:39 +03:00
|
|
|
| Struct x -> StructName.get_info x
|
|
|
|
| Enum x -> EnumName.get_info x
|
2020-12-06 14:32:36 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
module TVertexSet = Set.Make (TVertex)
|
|
|
|
|
|
|
|
(** On the edges, the label is the expression responsible for the use of the
|
|
|
|
function *)
|
|
|
|
module TEdge = struct
|
|
|
|
type t = Pos.t
|
|
|
|
|
|
|
|
let compare = compare
|
|
|
|
let default = Pos.no_pos
|
|
|
|
end
|
|
|
|
|
|
|
|
module TDependencies =
|
|
|
|
Graph.Persistent.Digraph.ConcreteBidirectionalLabeled (TVertex) (TEdge)
|
2022-03-08 17:03:14 +03:00
|
|
|
|
2020-12-06 14:32:36 +03:00
|
|
|
module TTopologicalTraversal = Graph.Topological.Make (TDependencies)
|
|
|
|
|
|
|
|
module TSCC = Graph.Components.Make (TDependencies)
|
|
|
|
(** Tarjan's stongly connected components algorithm, provided by OCamlGraph *)
|
|
|
|
|
2022-05-30 12:20:48 +03:00
|
|
|
let rec get_structs_or_enums_in_type (t : Ast.typ Marked.pos) : TVertexSet.t =
|
|
|
|
match Marked.unmark t with
|
2020-12-06 14:32:36 +03:00
|
|
|
| Ast.TStruct s -> TVertexSet.singleton (TVertex.Struct s)
|
|
|
|
| Ast.TEnum e -> TVertexSet.singleton (TVertex.Enum e)
|
|
|
|
| Ast.TArrow (t1, t2) ->
|
|
|
|
TVertexSet.union
|
|
|
|
(get_structs_or_enums_in_type t1)
|
|
|
|
(get_structs_or_enums_in_type t2)
|
2020-12-30 00:26:10 +03:00
|
|
|
| Ast.TLit _ | Ast.TAny -> TVertexSet.empty
|
2022-05-30 12:20:48 +03:00
|
|
|
| Ast.TArray t1 -> get_structs_or_enums_in_type (Marked.same_mark_as t1 t)
|
2020-12-06 14:32:36 +03:00
|
|
|
|
|
|
|
let build_type_graph (structs : Ast.struct_ctx) (enums : Ast.enum_ctx) :
|
|
|
|
TDependencies.t =
|
|
|
|
let g = TDependencies.empty in
|
|
|
|
let g =
|
2022-08-12 23:42:39 +03:00
|
|
|
StructMap.fold
|
2020-12-06 14:32:36 +03:00
|
|
|
(fun s fields g ->
|
|
|
|
List.fold_left
|
|
|
|
(fun g (_, typ) ->
|
|
|
|
let def = TVertex.Struct s in
|
|
|
|
let g = TDependencies.add_vertex g def in
|
|
|
|
let used = get_structs_or_enums_in_type typ in
|
|
|
|
TVertexSet.fold
|
|
|
|
(fun used g ->
|
|
|
|
if TVertex.equal used def then
|
2022-05-30 12:20:48 +03:00
|
|
|
Errors.raise_spanned_error (Marked.get_mark typ)
|
2022-03-08 15:04:27 +03:00
|
|
|
"The type %a is defined using itself, which is forbidden \
|
|
|
|
since Catala does not provide recursive types"
|
|
|
|
TVertex.format_t used
|
2020-12-06 14:32:36 +03:00
|
|
|
else
|
|
|
|
let edge =
|
2022-05-30 12:20:48 +03:00
|
|
|
TDependencies.E.create used (Marked.get_mark typ) def
|
2020-12-06 14:32:36 +03:00
|
|
|
in
|
|
|
|
TDependencies.add_edge_e g edge)
|
|
|
|
used g)
|
|
|
|
g fields)
|
|
|
|
structs g
|
|
|
|
in
|
|
|
|
let g =
|
2022-08-12 23:42:39 +03:00
|
|
|
EnumMap.fold
|
2020-12-06 14:32:36 +03:00
|
|
|
(fun e cases g ->
|
|
|
|
List.fold_left
|
|
|
|
(fun g (_, typ) ->
|
|
|
|
let def = TVertex.Enum e in
|
|
|
|
let g = TDependencies.add_vertex g def in
|
|
|
|
let used = get_structs_or_enums_in_type typ in
|
|
|
|
TVertexSet.fold
|
|
|
|
(fun used g ->
|
|
|
|
if TVertex.equal used def then
|
2022-05-30 12:20:48 +03:00
|
|
|
Errors.raise_spanned_error (Marked.get_mark typ)
|
2022-03-08 15:04:27 +03:00
|
|
|
"The type %a is defined using itself, which is forbidden \
|
|
|
|
since Catala does not provide recursive types"
|
|
|
|
TVertex.format_t used
|
2020-12-06 14:32:36 +03:00
|
|
|
else
|
|
|
|
let edge =
|
2022-05-30 12:20:48 +03:00
|
|
|
TDependencies.E.create used (Marked.get_mark typ) def
|
2020-12-06 14:32:36 +03:00
|
|
|
in
|
|
|
|
TDependencies.add_edge_e g edge)
|
|
|
|
used g)
|
|
|
|
g cases)
|
|
|
|
enums g
|
|
|
|
in
|
|
|
|
g
|
|
|
|
|
2021-01-29 01:46:39 +03:00
|
|
|
let check_type_cycles (structs : Ast.struct_ctx) (enums : Ast.enum_ctx) :
|
|
|
|
TVertex.t list =
|
2020-12-06 14:32:36 +03:00
|
|
|
let g = build_type_graph structs enums in
|
|
|
|
(* if there is a cycle, there will be an strongly connected component of
|
|
|
|
cardinality > 1 *)
|
|
|
|
let sccs = TSCC.scc_list g in
|
2021-01-29 01:46:39 +03:00
|
|
|
(if List.length sccs < TDependencies.nb_vertex g then
|
2020-12-06 14:32:36 +03:00
|
|
|
let scc = List.find (fun scc -> List.length scc > 1) sccs in
|
2022-03-08 15:04:27 +03:00
|
|
|
let spans =
|
|
|
|
List.flatten
|
|
|
|
(List.map
|
|
|
|
(fun v ->
|
|
|
|
let var_str, var_info =
|
|
|
|
Format.asprintf "%a" TVertex.format_t v, TVertex.get_info v
|
|
|
|
in
|
|
|
|
let succs = TDependencies.succ_e g v in
|
|
|
|
let _, edge_pos, succ =
|
|
|
|
List.find (fun (_, _, succ) -> List.mem succ scc) succs
|
|
|
|
in
|
|
|
|
let succ_str = Format.asprintf "%a" TVertex.format_t succ in
|
|
|
|
[
|
|
|
|
( Some ("Cycle type " ^ var_str ^ ", declared:"),
|
2022-05-30 12:20:48 +03:00
|
|
|
Marked.get_mark var_info );
|
2022-03-08 15:04:27 +03:00
|
|
|
( Some
|
|
|
|
("Used here in the definition of another cycle type "
|
|
|
|
^ succ_str
|
|
|
|
^ ":"),
|
|
|
|
edge_pos );
|
|
|
|
])
|
|
|
|
scc)
|
|
|
|
in
|
|
|
|
Errors.raise_multispanned_error spans
|
|
|
|
"Cyclic dependency detected between types!");
|
2021-01-29 01:46:39 +03:00
|
|
|
List.rev (TTopologicalTraversal.fold (fun v acc -> v :: acc) g [])
|