123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177(* This file is part of the Catala compiler, a specification language for tax
and social benefits computation rules. Copyright (C) 2022 Inria, contributor:
Aymeric Fromherz <aymeric.fromherz@inria.fr>, 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. *)openCatala_utilsopenShared_astmoduletypeBackend=sigvalinit_backend:unit->unittypebackend_contextvalmake_context:decl_ctx->backend_contexttypevc_encodingvalprint_encoding:vc_encoding->stringtypemodeltypesolver_result=ProvenTrue|ProvenFalseofmodeloption|Unknownvalsolve_vc_encoding:backend_context->vc_encoding->solver_resultvalprint_model:backend_context->model->stringvalis_model_empty:model->boolvaltranslate_expr:backend_context->typedDcalc.Ast.expr->backend_context*vc_encodingvalencode_asserts:backend_context->typedDcalc.Ast.expr->backend_contextendmoduletypeBackendIO=sigvalinit_backend:unit->unittypebackend_contextvalmake_context:decl_ctx->backend_contexttypevc_encodingvaltranslate_expr:backend_context->typedDcalc.Ast.expr->backend_context*vc_encodingvalencode_asserts:backend_context->typedDcalc.Ast.expr->backend_contexttypemodeltypevc_encoding_result=|Successofvc_encoding*backend_context|Failofstringvalprint_negative_result:Conditions.verification_condition->backend_context->modeloption->stringvalencode_and_check_vc:decl_ctx->Conditions.verification_condition*vc_encoding_result->boolendmoduleMakeBackendIO(B:Backend)=structletinit_backend=B.init_backendtypebackend_context=B.backend_contextletmake_context=B.make_contexttypevc_encoding=B.vc_encodinglettranslate_expr=B.translate_exprletencode_asserts=B.encode_assertstypemodel=B.modeltypevc_encoding_result=|SuccessofB.vc_encoding*B.backend_context|Failofstringletprint_negative_result(vc:Conditions.verification_condition)(ctx:B.backend_context)(model:B.modeloption):string=letvar_and_pos=matchvc.Conditions.vc_kindwith|Conditions.NoEmptyError->Format.asprintf"@[<v>@{<yellow>[%a.%s]@} This variable might return an empty error:@,\
%a@]"ScopeName.formatvc.vc_scope(Bindlib.name_of(Mark.removevc.vc_variable))Pos.format_loc_text(Mark.getvc.vc_variable)|Conditions.NoOverlappingExceptions->Format.asprintf"@[<v>@{<yellow>[%a.%s]@} At least two exceptions overlap for this \
variable:@,\
%a@]"ScopeName.formatvc.vc_scope(Bindlib.name_of(Mark.removevc.vc_variable))Pos.format_loc_text(Mark.getvc.vc_variable)inletcounterexample:stringoption=ifGlobals.disable_counterexamples()thenSome"Counterexample generation is disabled so none was generated."elsematchmodelwith|None->Some"The solver did not manage to generate a counterexample to explain \
the faulty behavior."|Somemodel->ifB.is_model_emptymodelthenNoneelseSome(Format.asprintf"The solver generated the following counterexample to explain \
the faulty behavior:\n\
%s"(B.print_modelctxmodel))invar_and_pos^matchcounterexamplewith|None->""|Somecounterexample->"\n"^counterexampleletencode_and_check_vc(_decl_ctx:decl_ctx)(vc:Conditions.verification_condition*vc_encoding_result):bool=letvc,z3_vc=vcinMessage.debug"@[<v>For this variable:@,%a@,@]"Pos.format_loc_text(Expr.posvc.Conditions.vc_guard);Message.debug"@[<v>This verification condition was generated for @{<yellow>%s@}:@,\
%a@,\
with assertions:@,\
%a@]"(matchvc.vc_kindwith|Conditions.NoEmptyError->"the variable definition never to return an empty error"|NoOverlappingExceptions->"no two exceptions to ever overlap")(Print.expr())vc.vc_guard(Print.expr())vc.vc_asserts;matchz3_vcwith|Success(encoding,backend_ctx)->(Message.debug"@[<v>The translation to Z3 is the following:@,%s@]"(B.print_encodingencoding);matchB.solve_vc_encodingbackend_ctxencodingwith|ProvenTrue->true|ProvenFalsemodel->Message.warning"%s"(print_negative_resultvcbackend_ctxmodel);false|Unknown->failwith"The solver failed at proving or disproving the VC")|Failmsg->Message.warning"@[<v>@{<yellow>[%a.%s]@} The translation to Z3 failed:@,%s@]"ScopeName.formatvc.vc_scope(Bindlib.name_of(Mark.removevc.vc_variable))msg;falseend