2022-08-16 18:09:26 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2020-2022 Inria,
|
|
|
|
contributor: Denis Merigoux <denis.merigoux@inria.fr>, Alain Delaët-Tixeuil
|
|
|
|
<alain.delaet--tixeuil@inria.fr>, Louis Gesbert <louis.gesbert@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. *)
|
|
|
|
|
2022-08-22 19:53:30 +03:00
|
|
|
open Definitions
|
|
|
|
|
2023-02-13 17:00:23 +03:00
|
|
|
let map_exprs ~f ~varf { code_items; decl_ctx } =
|
2022-08-22 19:53:30 +03:00
|
|
|
Bindlib.box_apply
|
2023-02-13 17:00:23 +03:00
|
|
|
(fun code_items -> { code_items; decl_ctx })
|
|
|
|
(Scope.map_exprs ~f ~varf code_items)
|
2022-08-22 19:53:30 +03:00
|
|
|
|
2023-03-30 19:52:29 +03:00
|
|
|
let fold_left_exprs ~f ~init { code_items; decl_ctx = _ } =
|
|
|
|
Scope.fold_left ~f:(fun acc e _ -> f acc e) ~init code_items
|
|
|
|
|
|
|
|
let fold_right_exprs ~f ~init { code_items; decl_ctx = _ } =
|
|
|
|
Scope.fold_right ~f:(fun e _ acc -> f e acc) ~init code_items
|
|
|
|
|
2023-02-13 17:00:23 +03:00
|
|
|
let get_scope_body { code_items; _ } scope =
|
2022-11-17 19:13:35 +03:00
|
|
|
match
|
|
|
|
Scope.fold_left ~init:None
|
2023-01-23 14:19:36 +03:00
|
|
|
~f:(fun acc item _ ->
|
|
|
|
match item with
|
|
|
|
| ScopeDef (name, body) when ScopeName.equal scope name -> Some body
|
|
|
|
| _ -> acc)
|
2023-02-13 17:00:23 +03:00
|
|
|
code_items
|
2022-11-17 19:13:35 +03:00
|
|
|
with
|
|
|
|
| None -> raise Not_found
|
|
|
|
| Some body -> body
|
|
|
|
|
2022-08-25 20:46:13 +03:00
|
|
|
let untype : 'm. ('a, 'm mark) gexpr program -> ('a, untyped mark) gexpr program
|
2022-08-22 19:53:30 +03:00
|
|
|
=
|
2022-08-25 20:46:13 +03:00
|
|
|
fun prg -> Bindlib.unbox (map_exprs ~f:Expr.untype ~varf:Var.translate prg)
|
2022-08-17 12:49:16 +03:00
|
|
|
|
|
|
|
let rec find_scope name vars = function
|
|
|
|
| Nil -> raise Not_found
|
2023-01-23 14:19:36 +03:00
|
|
|
| Cons (ScopeDef (n, body), _) when ScopeName.equal name n ->
|
|
|
|
List.rev vars, body
|
|
|
|
| Cons (_, next_bind) ->
|
|
|
|
let var, next = Bindlib.unbind next_bind in
|
2022-08-17 12:49:16 +03:00
|
|
|
find_scope name (var :: vars) next
|
|
|
|
|
2023-03-17 13:34:52 +03:00
|
|
|
let rec all_scopes code_item_list =
|
|
|
|
match code_item_list with
|
|
|
|
| Nil -> []
|
|
|
|
| Cons (ScopeDef (n, _), next_bind) ->
|
|
|
|
let _var, next = Bindlib.unbind next_bind in
|
|
|
|
n :: all_scopes next
|
|
|
|
| Cons (_, next_bind) ->
|
|
|
|
let _var, next = Bindlib.unbind next_bind in
|
|
|
|
all_scopes next
|
|
|
|
|
2022-08-17 12:49:16 +03:00
|
|
|
let to_expr p main_scope =
|
2023-02-13 17:00:23 +03:00
|
|
|
let _, main_scope_body = find_scope main_scope [] p.code_items in
|
2023-03-17 19:20:35 +03:00
|
|
|
|
|
|
|
let res =
|
|
|
|
Scope.unfold p.decl_ctx p.code_items
|
|
|
|
(Scope.get_body_mark main_scope_body)
|
|
|
|
(ScopeName main_scope)
|
|
|
|
in
|
2023-04-03 12:20:19 +03:00
|
|
|
Catala_utils.Bindlib_ext.assert_closed (Expr.Box.lift res);
|
2023-03-17 19:20:35 +03:00
|
|
|
res
|
2023-03-17 13:34:52 +03:00
|
|
|
|
|
|
|
let equal p p' =
|
|
|
|
let ss = all_scopes p.code_items in
|
|
|
|
let ss' = all_scopes p'.code_items in
|
|
|
|
|
2023-04-04 16:18:08 +03:00
|
|
|
List.length ss = List.length ss'
|
|
|
|
&& ListLabels.for_all2 ss ss' ~f:(fun s s' ->
|
|
|
|
ScopeName.equal s s'
|
|
|
|
&&
|
|
|
|
let e1 = Expr.unbox @@ to_expr p s in
|
2023-04-07 13:10:08 +03:00
|
|
|
let e2 = Expr.unbox @@ to_expr p' s in
|
2023-04-04 16:18:08 +03:00
|
|
|
Expr.equal e1 e2)
|