2022-01-07 14:04:31 +03:00
|
|
|
(* This file is part of the Catala compiler, a specification language for tax
|
|
|
|
and social benefits computation rules. Copyright (C) 2022 Inria, contributor:
|
2022-01-18 17:13:16 +03:00
|
|
|
Denis Merigoux <denis.merigoux@inria.fr>, Alain Delaët
|
2022-11-08 22:48:43 +03:00
|
|
|
<alain.delaet--tixeuil@inria.fr>, Aymeric Fromherz
|
|
|
|
<aymeric.fromherz@inria.fr>
|
2022-01-07 14:04:31 +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
|
|
|
|
|
|
|
|
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-11-21 12:46:17 +03:00
|
|
|
open Catala_utils
|
2022-08-12 23:42:39 +03:00
|
|
|
open Shared_ast
|
2022-01-08 20:37:04 +03:00
|
|
|
open Dcalc
|
2022-01-07 14:04:31 +03:00
|
|
|
open Ast
|
|
|
|
|
2022-01-18 17:13:16 +03:00
|
|
|
(** {1 Helpers and type definitions}*)
|
|
|
|
|
2022-11-17 00:08:07 +03:00
|
|
|
type vc_return = typed expr
|
|
|
|
(** The return type of VC generators is the VC expression *)
|
2022-01-12 17:52:15 +03:00
|
|
|
|
2022-04-04 09:56:48 +03:00
|
|
|
type ctx = {
|
|
|
|
current_scope_name : ScopeName.t;
|
|
|
|
decl : decl_ctx;
|
2022-08-25 20:46:13 +03:00
|
|
|
input_vars : typed expr Var.t list;
|
|
|
|
scope_variables_typs : (typed expr, typ) Var.Map.t;
|
2022-04-04 09:56:48 +03:00
|
|
|
}
|
2022-01-18 19:59:15 +03:00
|
|
|
|
2022-11-08 23:51:56 +03:00
|
|
|
let rec conjunction_exprs (exprs : typed expr list) (mark : typed mark) :
|
|
|
|
typed expr =
|
|
|
|
match exprs with
|
|
|
|
| [] -> ELit (LBool true), mark
|
|
|
|
| hd :: tl ->
|
2023-12-18 12:25:00 +03:00
|
|
|
( EAppOp
|
2023-01-20 22:10:18 +03:00
|
|
|
{
|
2024-04-30 17:35:08 +03:00
|
|
|
op = And, Expr.mark_pos mark;
|
2023-12-18 12:25:00 +03:00
|
|
|
tys = [TLit TBool, Expr.pos hd; TLit TBool, Expr.pos hd];
|
2023-01-20 22:10:18 +03:00
|
|
|
args = [hd; conjunction_exprs tl mark];
|
|
|
|
},
|
|
|
|
mark )
|
2022-11-08 23:51:56 +03:00
|
|
|
|
2022-07-12 16:57:50 +03:00
|
|
|
let conjunction (args : vc_return list) (mark : typed mark) : vc_return =
|
2022-01-12 17:52:15 +03:00
|
|
|
let acc, list =
|
2022-11-17 00:08:07 +03:00
|
|
|
match args with hd :: tl -> hd, tl | [] -> (ELit (LBool true), mark), []
|
2022-01-12 17:52:15 +03:00
|
|
|
in
|
2022-01-07 14:04:31 +03:00
|
|
|
List.fold_left
|
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
|
|
|
(fun acc arg ->
|
2023-12-18 12:25:00 +03:00
|
|
|
( EAppOp
|
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
|
|
|
{
|
2024-04-30 17:35:08 +03:00
|
|
|
op = And, Expr.mark_pos mark;
|
2023-12-18 12:25:00 +03:00
|
|
|
tys = [TLit TBool, Expr.pos acc; TLit TBool, Expr.pos arg];
|
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
|
|
|
args = [arg; acc];
|
|
|
|
},
|
|
|
|
mark ))
|
2022-01-07 14:04:31 +03:00
|
|
|
acc list
|
|
|
|
|
2022-11-17 00:08:07 +03:00
|
|
|
let negation (arg : vc_return) (mark : typed mark) : vc_return =
|
2024-04-30 17:35:08 +03:00
|
|
|
( EAppOp
|
|
|
|
{
|
|
|
|
op = Not, Expr.mark_pos mark;
|
|
|
|
tys = [TLit TBool, Expr.pos arg];
|
|
|
|
args = [arg];
|
|
|
|
},
|
|
|
|
mark )
|
2022-01-10 18:25:20 +03:00
|
|
|
|
2022-07-12 16:57:50 +03:00
|
|
|
let disjunction (args : vc_return list) (mark : typed mark) : vc_return =
|
2022-01-12 17:52:15 +03:00
|
|
|
let acc, list =
|
2022-11-17 00:08:07 +03:00
|
|
|
match args with hd :: tl -> hd, tl | [] -> (ELit (LBool false), mark), []
|
2022-01-12 17:52:15 +03:00
|
|
|
in
|
2022-01-07 14:04:31 +03:00
|
|
|
List.fold_left
|
2022-11-17 00:08:07 +03:00
|
|
|
(fun (acc : vc_return) arg ->
|
2023-12-18 12:25:00 +03:00
|
|
|
( EAppOp
|
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
|
|
|
{
|
2024-04-30 17:35:08 +03:00
|
|
|
op = Or, Expr.mark_pos mark;
|
2023-12-18 12:25:00 +03:00
|
|
|
tys = [TLit TBool, Expr.pos acc; TLit TBool, Expr.pos arg];
|
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
|
|
|
args = [arg; acc];
|
|
|
|
},
|
|
|
|
mark ))
|
2022-01-07 14:04:31 +03:00
|
|
|
acc list
|
|
|
|
|
2022-01-18 17:13:16 +03:00
|
|
|
(** [half_product [a1,...,an] [b1,...,bm] returns [(a1,b1),...(a1,bn),...(an,b1),...(an,bm)]] *)
|
|
|
|
let half_product (l1 : 'a list) (l2 : 'b list) : ('a * 'b) list =
|
|
|
|
l1
|
|
|
|
|> List.mapi (fun i ei ->
|
|
|
|
List.filteri (fun j _ -> i < j) l2 |> List.map (fun ej -> ei, ej))
|
|
|
|
|> List.concat
|
|
|
|
|
|
|
|
(** This code skims through the topmost layers of the terms like this:
|
|
|
|
[log (error_on_empty < reentrant_variable () | true :- e1 >)] for scope
|
|
|
|
variables, or [fun () -> e1] for subscope variables. But what we really want
|
|
|
|
to analyze is only [e1], so we match this outermost structure explicitely
|
|
|
|
and have a clean verification condition generator that only runs on [e1] *)
|
2022-08-25 20:46:13 +03:00
|
|
|
let match_and_ignore_outer_reentrant_default (ctx : ctx) (e : typed expr) :
|
|
|
|
typed expr =
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with
|
2022-11-17 19:13:35 +03:00
|
|
|
| EErrorOnEmpty
|
2022-04-12 12:14:39 +03:00
|
|
|
( EDefault
|
2022-11-17 19:13:35 +03:00
|
|
|
{
|
2023-12-18 12:25:00 +03:00
|
|
|
excepts = [(EApp { f = EVar x, _; args = [(ELit LUnit, _)]; _ }, _)];
|
2022-11-17 19:13:35 +03:00
|
|
|
just = ELit (LBool true), _;
|
|
|
|
cons;
|
|
|
|
},
|
2022-04-12 12:14:39 +03:00
|
|
|
_ )
|
2023-04-15 18:07:24 +03:00
|
|
|
when List.exists (fun x' -> Var.equal x x') ctx.input_vars ->
|
2022-01-18 17:13:16 +03:00
|
|
|
(* scope variables*)
|
|
|
|
cons
|
2022-11-17 19:13:35 +03:00
|
|
|
| EAbs { binder; tys = [(TLit TUnit, _)] } ->
|
2022-02-10 12:05:14 +03:00
|
|
|
(* context sub-scope variables *)
|
2022-01-18 17:13:16 +03:00
|
|
|
let _, body = Bindlib.unmbind binder in
|
2022-04-12 12:14:39 +03:00
|
|
|
body
|
2022-12-02 18:42:29 +03:00
|
|
|
| EAbs { binder; _ } -> (
|
|
|
|
(* context scope variables *)
|
|
|
|
let _, body = Bindlib.unmbind binder in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove body with
|
2022-12-02 18:42:29 +03:00
|
|
|
| EErrorOnEmpty e -> e
|
|
|
|
| _ ->
|
2024-04-10 19:39:30 +03:00
|
|
|
Message.error ~pos:(Expr.pos e)
|
2022-12-02 18:42:29 +03:00
|
|
|
"Internal error: this expression does not have the structure expected \
|
|
|
|
by the VC generator:\n\
|
|
|
|
%a"
|
2023-05-02 17:33:23 +03:00
|
|
|
(Print.expr ()) e)
|
2022-11-17 19:13:35 +03:00
|
|
|
| EErrorOnEmpty d ->
|
2022-02-10 12:05:14 +03:00
|
|
|
d (* input subscope variables and non-input scope variable *)
|
2023-11-07 16:25:52 +03:00
|
|
|
| _ -> e
|
2022-01-18 17:13:16 +03:00
|
|
|
|
|
|
|
(** {1 Verification conditions generator}*)
|
|
|
|
|
|
|
|
(** [generate_vc_must_not_return_empty e] returns the dcalc boolean expression
|
|
|
|
[b] such that if [b] is true, then [e] will never return an empty error. It
|
|
|
|
also returns a map of all the types of locally free variables inside the
|
2022-01-13 14:15:37 +03:00
|
|
|
expression. *)
|
2022-08-25 17:35:08 +03:00
|
|
|
let rec generate_vc_must_not_return_empty (ctx : ctx) (e : typed expr) :
|
2022-07-12 16:57:50 +03:00
|
|
|
vc_return =
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with
|
2022-11-17 19:13:35 +03:00
|
|
|
| EAbs { binder; _ } ->
|
|
|
|
(* Hot take: for a function never to return an empty error when called, it
|
|
|
|
has to do so whatever its input. So we universally quantify over the
|
|
|
|
variable of the function when inspecting the body, resulting in simply
|
|
|
|
traversing through in the code here. *)
|
|
|
|
let _vars, body = Bindlib.unmbind binder in
|
|
|
|
(generate_vc_must_not_return_empty ctx) body
|
|
|
|
| EDefault { excepts; just; cons } ->
|
|
|
|
(* <e1 ... en | ejust :- econs > never returns empty if and only if: - first
|
|
|
|
we look if e1 .. en ejust can return empty; - if no, we check that if
|
|
|
|
ejust is true, whether econs can return empty. *)
|
|
|
|
disjunction
|
|
|
|
(List.map (generate_vc_must_not_return_empty ctx) excepts
|
|
|
|
@ [
|
|
|
|
conjunction
|
|
|
|
[
|
|
|
|
generate_vc_must_not_return_empty ctx just;
|
|
|
|
(let vc_just_expr = generate_vc_must_not_return_empty ctx cons in
|
|
|
|
( EIfThenElse
|
|
|
|
{
|
|
|
|
cond = just;
|
|
|
|
(* Comment from Alain: the justification is not checked for
|
|
|
|
holding an default term. In such cases, we need to
|
|
|
|
encode the logic of the default terms within the
|
|
|
|
generation of the verification condition
|
|
|
|
(Z3encoding.translate_expr). Answer from Denis:
|
|
|
|
Normally, there is a structural invariant from the
|
|
|
|
surface language to intermediate representation
|
|
|
|
translation preventing any default terms to appear in
|
|
|
|
justifications.*)
|
|
|
|
etrue = vc_just_expr;
|
2023-05-17 16:44:57 +03:00
|
|
|
efalse = ELit (LBool false), Mark.get e;
|
2022-11-17 19:13:35 +03:00
|
|
|
},
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.get e ));
|
2022-11-17 19:13:35 +03:00
|
|
|
]
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.get e);
|
2022-11-17 19:13:35 +03:00
|
|
|
])
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.get e)
|
2024-04-26 19:31:26 +03:00
|
|
|
| EEmpty -> Mark.copy e (ELit (LBool false))
|
2022-11-17 19:13:35 +03:00
|
|
|
| EVar _
|
|
|
|
(* Per default calculus semantics, you cannot call a function with an argument
|
|
|
|
that evaluates to the empty error. Thus, all variable evaluate to
|
|
|
|
non-empty-error terms. *)
|
2023-12-18 12:25:00 +03:00
|
|
|
| ELit _ ->
|
2023-05-17 16:44:57 +03:00
|
|
|
Mark.copy e (ELit (LBool true))
|
2023-12-18 12:25:00 +03:00
|
|
|
| EApp { f; args; _ } ->
|
2023-01-20 23:55:28 +03:00
|
|
|
(* Invariant: For the [EApp] case, we assume here that function calls never
|
|
|
|
return empty error, which implies all functions have been checked never
|
|
|
|
to return empty errors. *)
|
|
|
|
conjunction
|
|
|
|
(generate_vc_must_not_return_empty ctx f
|
|
|
|
:: List.flatten
|
|
|
|
(List.map
|
|
|
|
(fun arg ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove arg with
|
2023-01-20 23:55:28 +03:00
|
|
|
| EStruct { fields; _ } ->
|
|
|
|
List.map
|
2023-07-12 12:48:46 +03:00
|
|
|
(fun field ->
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove field with
|
2023-01-20 23:55:28 +03:00
|
|
|
| EAbs { binder; tys = [(TLit TUnit, _)] } -> (
|
|
|
|
(* Invariant: when calling a function with a thunked
|
|
|
|
emptyerror, this means we're in a direct scope call
|
|
|
|
with a context argument. In that case, we don't apply
|
|
|
|
the standard [EAbs] rule and suppose, in coherence
|
|
|
|
with the [EApp] invariant, that the subscope will
|
|
|
|
never return empty error so the thunked emptyerror
|
|
|
|
can be ignored *)
|
|
|
|
let _vars, body = Bindlib.unmbind binder in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove body with
|
2024-04-26 19:31:26 +03:00
|
|
|
| EEmpty -> Mark.copy field (ELit (LBool true))
|
2023-01-20 23:55:28 +03:00
|
|
|
| _ ->
|
|
|
|
(* same as basic [EAbs case]*)
|
|
|
|
generate_vc_must_not_return_empty ctx field)
|
|
|
|
| _ -> generate_vc_must_not_return_empty ctx field)
|
2023-07-12 12:48:46 +03:00
|
|
|
(StructField.Map.values fields)
|
2023-01-20 23:55:28 +03:00
|
|
|
| _ -> [generate_vc_must_not_return_empty ctx arg])
|
|
|
|
args))
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.get e)
|
2022-11-17 19:13:35 +03:00
|
|
|
| _ ->
|
|
|
|
conjunction
|
|
|
|
(Expr.shallow_fold
|
|
|
|
(fun e acc -> generate_vc_must_not_return_empty ctx e :: acc)
|
|
|
|
e [])
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.get e)
|
2022-01-07 14:04:31 +03:00
|
|
|
|
2022-11-17 00:08:07 +03:00
|
|
|
(** [generate_vc_must_not_return_conflict e] returns the dcalc boolean
|
|
|
|
expression [b] such that if [b] is true, then [e] will never return a
|
|
|
|
conflict error. It also returns a map of all the types of locally free
|
|
|
|
variables inside the expression. *)
|
|
|
|
let rec generate_vc_must_not_return_conflict (ctx : ctx) (e : typed expr) :
|
2022-08-25 20:46:13 +03:00
|
|
|
vc_return =
|
2022-11-17 19:13:35 +03:00
|
|
|
(* See the code of [generate_vc_must_not_return_empty] for a list of
|
|
|
|
invariants on which this function relies on. *)
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with
|
2022-11-17 19:13:35 +03:00
|
|
|
| EAbs { binder; _ } ->
|
|
|
|
let _vars, body = Bindlib.unmbind binder in
|
|
|
|
(generate_vc_must_not_return_conflict ctx) body
|
2023-12-18 12:25:00 +03:00
|
|
|
| EVar _ | ELit _ -> Mark.copy e (ELit (LBool true))
|
2022-11-17 19:13:35 +03:00
|
|
|
| EDefault { excepts; just; cons } ->
|
|
|
|
(* <e1 ... en | ejust :- econs > never returns conflict if and only if: -
|
|
|
|
neither e1 nor ... nor en nor ejust nor econs return conflict - there is
|
|
|
|
no two differents ei ej that are not empty. *)
|
|
|
|
let quadratic =
|
|
|
|
negation
|
|
|
|
(disjunction
|
|
|
|
(List.map
|
|
|
|
(fun (e1, e2) ->
|
|
|
|
conjunction
|
|
|
|
[
|
|
|
|
generate_vc_must_not_return_empty ctx e1;
|
|
|
|
generate_vc_must_not_return_empty ctx e2;
|
|
|
|
]
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.get e))
|
2022-11-17 19:13:35 +03:00
|
|
|
(half_product excepts excepts))
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.get e))
|
|
|
|
(Mark.get e)
|
2022-11-17 19:13:35 +03:00
|
|
|
in
|
|
|
|
let others =
|
|
|
|
List.map
|
|
|
|
(generate_vc_must_not_return_conflict ctx)
|
|
|
|
(just :: cons :: excepts)
|
|
|
|
in
|
2023-05-17 16:44:57 +03:00
|
|
|
let out = conjunction (quadratic :: others) (Mark.get e) in
|
2022-11-17 19:13:35 +03:00
|
|
|
out
|
|
|
|
| _ ->
|
|
|
|
conjunction
|
|
|
|
(Expr.shallow_fold
|
|
|
|
(fun e acc -> generate_vc_must_not_return_conflict ctx e :: acc)
|
|
|
|
e [])
|
2023-05-17 16:44:57 +03:00
|
|
|
(Mark.get e)
|
2022-01-10 18:25:20 +03:00
|
|
|
|
2022-01-18 17:13:16 +03:00
|
|
|
(** {1 Interface}*)
|
2022-01-13 14:15:37 +03:00
|
|
|
|
2022-01-10 12:28:14 +03:00
|
|
|
type verification_condition_kind = NoEmptyError | NoOverlappingExceptions
|
|
|
|
|
2022-07-12 16:57:50 +03:00
|
|
|
type verification_condition = {
|
2022-08-25 17:35:08 +03:00
|
|
|
vc_guard : typed expr;
|
2022-01-10 12:28:14 +03:00
|
|
|
(* should have type bool *)
|
|
|
|
vc_kind : verification_condition_kind;
|
2022-11-08 22:48:43 +03:00
|
|
|
(* All assertions defined at the top-level of the scope corresponding to this
|
|
|
|
assertion *)
|
|
|
|
vc_asserts : typed expr;
|
2022-01-12 18:49:44 +03:00
|
|
|
vc_scope : ScopeName.t;
|
2023-05-17 16:44:57 +03:00
|
|
|
vc_variable : typed expr Var.t Mark.pos;
|
2022-01-10 12:28:14 +03:00
|
|
|
}
|
|
|
|
|
2023-05-17 16:44:57 +03:00
|
|
|
let trivial_assert e = Mark.copy e (ELit (LBool true))
|
2022-11-08 22:48:43 +03:00
|
|
|
|
2022-04-04 09:56:48 +03:00
|
|
|
let rec generate_verification_conditions_scope_body_expr
|
2022-04-12 11:53:07 +03:00
|
|
|
(ctx : ctx)
|
2022-08-25 20:46:13 +03:00
|
|
|
(scope_body_expr : 'm expr scope_body_expr) :
|
2022-11-08 22:54:32 +03:00
|
|
|
ctx * verification_condition list * typed expr list =
|
2022-04-04 09:56:48 +03:00
|
|
|
match scope_body_expr with
|
2024-02-09 18:48:02 +03:00
|
|
|
| Last _ -> ctx, [], []
|
|
|
|
| Cons (scope_let, scope_let_next) ->
|
|
|
|
let scope_let_var, scope_let_next = Bindlib.unbind scope_let_next in
|
2022-11-08 22:54:32 +03:00
|
|
|
let new_ctx, vc_list, assert_list =
|
2022-04-04 09:56:48 +03:00
|
|
|
match scope_let.scope_let_kind with
|
2022-11-09 00:09:21 +03:00
|
|
|
| Assertion -> (
|
|
|
|
let e =
|
|
|
|
Expr.unbox (Expr.remove_logging_calls scope_let.scope_let_expr)
|
|
|
|
in
|
2023-05-17 16:44:57 +03:00
|
|
|
match Mark.remove e with
|
2022-11-09 00:09:21 +03:00
|
|
|
| EAssert e ->
|
|
|
|
let e = match_and_ignore_outer_reentrant_default ctx e in
|
|
|
|
ctx, [], [e]
|
|
|
|
| _ ->
|
2024-04-10 19:39:30 +03:00
|
|
|
Message.error ~pos:(Expr.pos e)
|
2022-11-09 00:09:21 +03:00
|
|
|
"Internal error: this assertion does not have the structure \
|
|
|
|
expected by the VC generator:\n\
|
|
|
|
%a"
|
2023-05-02 17:33:23 +03:00
|
|
|
(Print.expr ()) e)
|
2022-04-04 09:56:48 +03:00
|
|
|
| DestructuringInputStruct ->
|
2022-11-08 22:54:32 +03:00
|
|
|
{ ctx with input_vars = scope_let_var :: ctx.input_vars }, [], []
|
2022-04-04 09:56:48 +03:00
|
|
|
| ScopeVarDefinition | SubScopeVarDefinition ->
|
|
|
|
(* For scope variables, we should check both that they never evaluate to
|
|
|
|
emptyError nor conflictError. But for subscope variable definitions,
|
|
|
|
what we're really doing is adding exceptions to something defined in
|
|
|
|
the subscope so we just ought to verify only that the exceptions
|
|
|
|
overlap. *)
|
2022-08-17 12:49:16 +03:00
|
|
|
let 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
|
|
|
Expr.unbox (Expr.remove_logging_calls scope_let.scope_let_expr)
|
2022-08-17 12:49:16 +03:00
|
|
|
in
|
2022-04-12 12:14:39 +03:00
|
|
|
let e = match_and_ignore_outer_reentrant_default ctx e in
|
2022-11-17 00:08:07 +03:00
|
|
|
let vc_confl = generate_vc_must_not_return_conflict ctx e in
|
2022-04-04 09:56:48 +03:00
|
|
|
let vc_confl =
|
2023-06-28 16:57:52 +03:00
|
|
|
if Globals.optimize () then
|
2023-04-21 12:56:07 +03:00
|
|
|
Expr.unbox
|
|
|
|
(Shared_ast.Optimizations.optimize_expr ctx.decl vc_confl)
|
2022-04-04 09:56:48 +03:00
|
|
|
else vc_confl
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2022-04-04 09:56:48 +03:00
|
|
|
let vc_list =
|
2022-05-04 18:40:55 +03:00
|
|
|
[
|
|
|
|
{
|
2023-05-17 16:44:57 +03:00
|
|
|
vc_guard = Mark.copy e (Mark.remove vc_confl);
|
2022-04-04 09:56:48 +03:00
|
|
|
vc_kind = NoOverlappingExceptions;
|
2022-11-08 22:48:43 +03:00
|
|
|
(* Placeholder until we add all assertions in scope once
|
|
|
|
* we finished traversing it *)
|
|
|
|
vc_asserts = trivial_assert e;
|
2022-04-04 09:56:48 +03:00
|
|
|
vc_scope = ctx.current_scope_name;
|
2022-07-28 11:36:36 +03:00
|
|
|
vc_variable = scope_let_var, scope_let.scope_let_pos;
|
2022-05-04 18:40:55 +03:00
|
|
|
};
|
|
|
|
]
|
|
|
|
in
|
2022-04-04 09:56:48 +03:00
|
|
|
let vc_list =
|
|
|
|
match scope_let.scope_let_kind with
|
|
|
|
| ScopeVarDefinition ->
|
2022-11-17 00:08:07 +03:00
|
|
|
let vc_empty = generate_vc_must_not_return_empty ctx e in
|
2022-04-04 09:56:48 +03:00
|
|
|
let vc_empty =
|
2023-06-28 16:57:52 +03:00
|
|
|
if Globals.optimize () then
|
2023-04-21 12:56:07 +03:00
|
|
|
Expr.unbox
|
|
|
|
(Shared_ast.Optimizations.optimize_expr ctx.decl vc_empty)
|
2022-04-04 09:56:48 +03:00
|
|
|
else vc_empty
|
|
|
|
in
|
2022-05-04 18:40:55 +03:00
|
|
|
{
|
2023-05-17 16:44:57 +03:00
|
|
|
vc_guard = Mark.copy e (Mark.remove vc_empty);
|
2022-04-04 09:56:48 +03:00
|
|
|
vc_kind = NoEmptyError;
|
2022-11-08 22:48:43 +03:00
|
|
|
vc_asserts = trivial_assert e;
|
2022-04-04 09:56:48 +03:00
|
|
|
vc_scope = ctx.current_scope_name;
|
2022-07-28 11:36:36 +03:00
|
|
|
vc_variable = scope_let_var, scope_let.scope_let_pos;
|
2022-05-04 18:40:55 +03:00
|
|
|
}
|
2022-04-04 09:56:48 +03:00
|
|
|
:: vc_list
|
|
|
|
| _ -> vc_list
|
2022-05-04 18:40:55 +03:00
|
|
|
in
|
2022-11-08 22:54:32 +03:00
|
|
|
ctx, vc_list, []
|
|
|
|
| _ -> ctx, [], []
|
2022-04-04 09:56:48 +03:00
|
|
|
in
|
2022-11-08 22:54:32 +03:00
|
|
|
let new_ctx, new_vcs, new_asserts =
|
2022-04-04 09:56:48 +03:00
|
|
|
generate_verification_conditions_scope_body_expr
|
|
|
|
{
|
|
|
|
new_ctx with
|
|
|
|
scope_variables_typs =
|
2022-07-28 11:36:36 +03:00
|
|
|
Var.Map.add scope_let_var scope_let.scope_let_typ
|
2022-04-04 09:56:48 +03:00
|
|
|
new_ctx.scope_variables_typs;
|
|
|
|
}
|
|
|
|
scope_let_next
|
|
|
|
in
|
2022-11-08 22:54:32 +03:00
|
|
|
new_ctx, vc_list @ new_vcs, assert_list @ new_asserts
|
2022-04-04 09:56:48 +03:00
|
|
|
|
2023-02-13 17:00:23 +03:00
|
|
|
let generate_verification_conditions_code_items
|
2022-04-12 11:53:07 +03:00
|
|
|
(decl_ctx : decl_ctx)
|
2023-02-13 17:00:23 +03:00
|
|
|
(code_items : 'm expr code_item_list)
|
2022-07-12 16:57:50 +03:00
|
|
|
(s : ScopeName.t option) : verification_condition list =
|
Implement safe renaming of idents for backend printing
Previously we had some heuristics in the backends trying to achieve this with a
lot of holes ; this should be much more solid, relying on `Bindlib` to do the
correct renamings.
**Note1**: it's not plugged into the backends other than OCaml at the moment.
**Note2**: the related, obsolete heuristics haven't been cleaned out yet
**Note3**: we conservatively suppose a single namespace at the moment. This is
required for e.g. Python, but it forces vars named like struct fields to be
renamed, which is more verbose in e.g. OCaml. The renaming engine could be
improved to support different namespaces, with a way to select how to route the
different kinds of identifiers into them.
Similarly, customisation for what needs to be uppercase or lowercase is not
available yet.
**Note4**: besides excluding keywords, we should also be careful to exclude (or
namespace):
- the idents used in the runtime (e.g. `o_add_int_int`)
- the dynamically generated idents (e.g. `embed_*`)
**Note5**: module names themselves aren't handled yet. The reason is that they
must be discoverable by the user, and even need to match the filenames, etc. In
other words, imagine that `Mod` is a keyword in the target language. You can't
rename a module called `Mod` to `Mod1` without knowing the whole module context,
because that would destroy the mapping for a module already called `Mod1`.
A reliable solution would be to translate all module names to e.g.
`CatalaModule_*`, which we can assume will never conflict with any built-in, and
forbid idents starting with that prefix. We may also want to restrict their
names to ASCII ? Currently we use a projection, but what if I have two modules
called `Là` and `La` ?
2024-08-05 18:08:36 +03:00
|
|
|
let conditions, _ =
|
2024-02-09 18:48:02 +03:00
|
|
|
BoundList.fold_left
|
|
|
|
~f:(fun vcs item _ ->
|
|
|
|
match item with
|
|
|
|
| Topdef _ -> []
|
|
|
|
| ScopeDef (name, body) ->
|
|
|
|
let is_selected_scope =
|
|
|
|
match s with
|
|
|
|
| Some s when ScopeName.equal s name -> true
|
|
|
|
| None -> true
|
|
|
|
| _ -> false
|
|
|
|
in
|
|
|
|
let new_vcs =
|
|
|
|
if is_selected_scope then
|
|
|
|
let _scope_input_var, scope_body_expr =
|
|
|
|
Bindlib.unbind body.scope_body_expr
|
|
|
|
in
|
|
|
|
let ctx =
|
|
|
|
{
|
|
|
|
current_scope_name = name;
|
|
|
|
decl = decl_ctx;
|
|
|
|
input_vars = [];
|
|
|
|
scope_variables_typs =
|
|
|
|
Var.Map.empty
|
|
|
|
(* We don't need to add the typ of the scope input var here
|
|
|
|
because it will never appear in an expression for which
|
|
|
|
we generate a verification conditions (the big struct is
|
2024-02-12 17:46:37 +03:00
|
|
|
destructured with a series of let bindings just
|
|
|
|
after.) *);
|
2024-02-09 18:48:02 +03:00
|
|
|
}
|
|
|
|
in
|
|
|
|
let _, vcs, asserts =
|
|
|
|
generate_verification_conditions_scope_body_expr ctx
|
|
|
|
scope_body_expr
|
|
|
|
in
|
|
|
|
let combined_assert =
|
|
|
|
conjunction_exprs asserts
|
|
|
|
(Typed
|
|
|
|
{ pos = Pos.no_pos; ty = Mark.add Pos.no_pos (TLit TBool) })
|
|
|
|
in
|
|
|
|
List.map (fun vc -> { vc with vc_asserts = combined_assert }) vcs
|
|
|
|
else []
|
|
|
|
in
|
|
|
|
new_vcs @ vcs)
|
|
|
|
~init:[] code_items
|
|
|
|
in
|
|
|
|
conditions
|
2022-04-04 09:56:48 +03:00
|
|
|
|
2022-08-12 23:42:39 +03:00
|
|
|
let generate_verification_conditions (p : 'm program) (s : ScopeName.t option) :
|
|
|
|
verification_condition list =
|
2023-02-13 17:00:23 +03:00
|
|
|
let vcs =
|
|
|
|
generate_verification_conditions_code_items p.decl_ctx p.code_items s
|
|
|
|
in
|
2022-04-04 18:51:41 +03:00
|
|
|
(* We sort this list by scope name and then variable name to ensure consistent
|
|
|
|
output for testing*)
|
|
|
|
List.sort
|
|
|
|
(fun vc1 vc2 ->
|
|
|
|
let to_str vc =
|
|
|
|
Format.asprintf "%s.%s"
|
2023-07-12 12:48:46 +03:00
|
|
|
(Format.asprintf "%a" ScopeName.format vc.vc_scope)
|
2023-05-17 16:44:57 +03:00
|
|
|
(Bindlib.name_of (Mark.remove vc.vc_variable))
|
2022-04-04 18:51:41 +03:00
|
|
|
in
|
|
|
|
String.compare (to_str vc1) (to_str vc2))
|
|
|
|
vcs
|