diff --git a/src/phl/README.md b/src/phl/README.md index 28a35b665..d87abc58b 100644 --- a/src/phl/README.md +++ b/src/phl/README.md @@ -143,7 +143,8 @@ src/phl/ transforms/ EcTr: catalogue entries of the program transformations ecPl*.ml computations shared by the rules of every logic: EcPlFrame, EcPlSp, EcPlWp, EcPlRndSem, EcPlRCond, EcPlTransform, - EcPlMatch, EcPlFun, EcPlCall, EcPlWeakMem + EcPlMatch, EcPlFun, EcPlCall, EcPlWeakMem, EcPlExists, + EcPlECall ecPlRecheck.ml checker scaffolding ecPhl.ml legacy: thin dispatchers and adapters, not-yet-migrated tactics @@ -162,6 +163,10 @@ src/phl/ judgement by fresh locals it does not mention; the `weakmem` tactic is derived (cut of the weakened hypothesis, closed by the rule and the hypothesis). +- The existential rules (`EcExists`) eliminate the existentials of + a precondition (`elim*`); `exists*` / `exlim` (consequence + rule) and + `ecall` (`EcECall`: existential rules, `seq` and the `call` rule + of the logic) are derived. - A rule relating two logics (e.g. the `pr` bridges) lives with the logic of its conclusion. - A rule concluding a probability statement from a judgement (`byphoare`, diff --git a/src/phl/REFACTORING.md b/src/phl/REFACTORING.md index d4ab01682..e45b5ff33 100644 --- a/src/phl/REFACTORING.md +++ b/src/phl/REFACTORING.md @@ -159,7 +159,10 @@ src/phl/ the single-call statement of `proc*`), EcPlCall (the result assignment and argument substitution of a call), EcPlWeakMem (restricting a memory weakened by fresh local - variables) + variables), EcPlExists (pulling the existentials of a + precondition to the front, quantifying a precondition + over values), EcPlECall (the elaboration of `ecall`: + typing the contract, abstracting its program variables) ecPlRecheck.ml checker scaffolding ecPhl.ml legacy: thin dispatchers and adapters, not-yet-migrated tactics @@ -190,6 +193,16 @@ uniform across logics or relate several judgements: variables it does not mention (the shared computation in `EcPlWeakMem`); the `weakmem` tactic is derived: a cut of the weakened hypothesis, closed by the rule and the hypothesis; +- the existential rules (`EcExists`, statement and procedure + forms) eliminate the existentials of a precondition (`elim*`): + `forall xs, J [P']` concludes `J [P]`, `P'` being `P` with its + existential binders pulled to the front (`EcPlExists`); `exists*` / + `exlim` are derived (the consequence rule, its premise closed by the + witnesses, then the elimination rule), and so is `ecall` + (`EcECall`, hoare, bdhoare and equiv: the program variables of + the contract abstracted by the existential rules, `seq`, then the + `call` rule of the logic; the forward hoare form frames the + precondition with the split and frame rules); - a rule relating judgements of two logics (the `pr` bridges, `hoare` from `phoare`, …) lives with the logic of its conclusion; - a rule concluding a statement on probabilities from a judgement diff --git a/src/phl/ecPhlExists.ml b/src/phl/ecPhlExists.ml index 108ffebbc..012060bbe 100644 --- a/src/phl/ecPhlExists.ml +++ b/src/phl/ecPhlExists.ml @@ -1,797 +1,72 @@ (* -------------------------------------------------------------------- *) -open EcUtils open EcAst -open EcFol -open EcEnv -open EcSubst open EcCoreGoal -open EcLowGoal open EcLowPhlGoal -open EcMatching.Position - -module L = EcLocation -module APT = EcParsetree -module TTC = EcProofTyping -module PT = EcProofTerm (* -------------------------------------------------------------------- *) -let get_to_gens fs = - let do_id (f: inv) = - let id = - match f with - | Inv_ss f -> begin match f.inv.f_node with - | Fpvar (pv, m) -> - id_of_pv pv m - | _ -> EcIdent.create "f" - end - | Inv_ts f -> begin match f.inv.f_node with - | Fpvar (pv, m) -> - id_of_pv ~mc:(f.ml, f.mr) pv m - | _ -> EcIdent.create "f" - end - | Inv_hs _ -> assert false - in - id, f in - List.map do_id fs +(* The existential rules live, one module per logic, in [rules//] + ([EcHoareExists], [EcEHoareExists], [EcBdHoareExists], + [EcEquivExists]), their shared computations in [EcPlExists]; [ecall] + is derived, in [EcHoareECall], [EcBdHoareECall] and [EcEquivECall], + with its shared elaboration in [EcPlECall]. This module only keeps the + legacy entry points (adapters onto those modules, so external callers + and this module's interface are unchanged) and the logic-agnostic + dispatchers. *) (* -------------------------------------------------------------------- *) let t_hr_exists_elim_r ?(bound : int option) (tc : tcenv1) = - let pre = tc1_get_pre tc in - let bd, pre' = - try destr_exists_prenex ?bound (inv_of_inv pre) - with DestrError _ -> [], (inv_of_inv pre) in - let pre = map_inv1 (fun _ -> pre') pre in - let concl = f_forall bd (set_pre ~pre (FApi.tc1_goal tc)) in - FApi.xmutate1 tc `HlExists [concl] - -(* -------------------------------------------------------------------- *) -let t_hr_exists_intro_r fs tc = - let hyps = FApi.tc1_hyps tc in - let concl = FApi.tc1_goal tc in - let pre1 = tc1_get_pre tc in - let post = tc1_get_post tc in - let gen = get_to_gens fs in - let eqs = List.map (fun (id, f) -> map_inv1 (f_eq (f_local id (inv_of_inv f).f_ty)) f) gen in - let bd = List.map (fun (id, f) -> (id, GTty (inv_of_inv f).f_ty)) gen in - let is_ehoare = - match concl.f_node with - | FeHoareF _ | FeHoareS _ -> true - | _ -> false in - let pre = - let eqs = match eqs with - | [] -> map_inv1 (fun _ -> f_true) pre1 - | _ -> map_inv f_ands eqs in - if is_ehoare then - map_inv2 f_interp_ehoare_form (map_inv1 (f_exists bd) eqs) pre1 - else - map_inv1 (f_exists bd) (map_inv2 f_and eqs pre1) in - - let h = LDecl.fresh_id hyps "h" in - let ml, mr = as_seq2 (LDecl.fresh_ids hyps ["&ml"; "&mr"]) in - let m = LDecl.fresh_id hyps "&m" in - let ms = - match pre1 with - | Inv_ts _ -> [ml; mr] - | Inv_ss _ -> [m] - | Inv_hs _ -> assert false - in - - let inv_rebind f = - match f with - | Inv_ts f -> Inv_ts (ts_inv_rebind f ml mr) - | Inv_ss f -> Inv_ss (ss_inv_rebind f m) - | Inv_hs f -> Inv_hs (hs_inv_rebind f m) - in - - let args = - let do1 (_, f) = PAFormula (inv_of_inv (inv_rebind f)) in - List.map do1 gen - in - - let t_exists = - if is_ehoare then - t_intros_i ms @! - EcHiGoal.t_apply_prept (PT.Prept.uglob EcCoreLib.CI_Xreal.p_xle_cxr_l) @+ - [ t_exists_intro_s args @! t_trivial; - t_trivial] - else FApi.t_seqs [t_intros_i (ms@[h]); t_exists_intro_s args; t_apply_hyp h] - in - - let tactic = - (EcPhlConseq.t_conseq pre post) @+ - [ t_exists; - t_trivial; - t_id] - in - FApi.t_internal tactic tc - -(* -------------------------------------------------------------------- *) -let t_hr_exists_elim = FApi.t_low0 "hr-exists-elim" t_hr_exists_elim_r -let t_hr_exists_intro = FApi.t_low1 "hr-exists-intro" t_hr_exists_intro_r - -(* -------------------------------------------------------------------- *) -let process_exists_intro ~(elim : bool) fs tc = - let (hyps, concl) = FApi.tc1_flat tc in - let penv, f_tr = - match concl.f_node with - | FhoareF {hf_f=f;hf_m=m} | FeHoareF {ehf_f=f; ehf_m=m} - | FbdHoareF {bhf_f=f; bhf_m=m} -> - fst (LDecl.hoareF m f hyps), Inv_ss {m; inv = f_true} - | FhoareS {hs_m=(m,_) as me} | FeHoareS {ehs_m=(m,_) as me} - | FbdHoareS {bhs_m=(m,_) as me} -> - LDecl.push_active_ss me hyps, Inv_ss {m; inv = f_true} - | FequivF ef -> fst (LDecl.equivF ef.ef_ml ef.ef_mr ef.ef_fl ef.ef_fr hyps), - Inv_ts {ml=ef.ef_ml; mr=ef.ef_mr; inv=f_true} - | FequivS es -> LDecl.push_all [es.es_ml; es.es_mr] hyps, - Inv_ts {ml=(fst es.es_ml); mr=(fst es.es_mr); inv=f_true} - | _ -> tc_error_noXhl ~kinds:hlkinds_Xhl !!tc - in - - let fs = - List.map - (fun f -> map_inv1 (fun _ -> TTC.pf_process_form_opt !!tc penv None f) f_tr) - fs - in - - let tc = t_hr_exists_intro_r fs tc in - - if elim then - t_hr_exists_elim_r ~bound:(List.length fs) (FApi.as_tcenv1 tc) - else tc - -(* -------------------------------------------------------------------- *) -type call = EcModules.lvalue option * EcPath.xpath * EcTypes.expr list - -type calls = [ - | `Single of call - | `Double of call * call -] - -(* -------------------------------------------------------------------- *) -let check_contract_type - ?(loc : L.t option) - ?(phoare : bool = false) - ?(noexn : bool = true) - ~(name : EcSymbols.qsymbol) - (pe : proofenv) - (hyps : LDecl.hyps) - (calls : calls) - (contract : form) -= - let env = LDecl.toenv hyps in - - let contract = - EcReduction.h_red_opt EcReduction.full_red hyps contract - |> odfl contract in - - match calls with - | `Single (_, funname, _) -> begin - let cttfname = - match contract.f_node with - | FhoareF hf -> - if noexn then begin - if not (POE.is_empty (hf_po hf).hsi_inv) then - tc_error ?loc pe - "contract must have an empty exception post-condition"; - end; - hf.hf_f - | FbdHoareF hf when phoare -> hf.bhf_f - | _ -> - tc_error_lazy ?loc pe (fun fmt -> - Format.fprintf fmt - "contract %a should be a Hoare statement" - EcSymbols.pp_qsymbol name - ) - in - if not (EcReduction.EqTest.for_xp env funname cttfname) then begin - tc_error_lazy ?loc pe (fun fmt -> - let ppe = EcPrinting.PPEnv.ofenv env in - Format.fprintf fmt - "contract %a should be for the procedure %a, not %a" - EcSymbols.pp_qsymbol name - (EcPrinting.pp_funname ppe) funname - (EcPrinting.pp_funname ppe) cttfname - ) - end; - end - - | `Double ((_, fl, _), (_, fr, _)) -> - let contract = - try - destr_equivF contract - with DestrError _ -> - tc_error_lazy ?loc pe (fun fmt -> - Format.fprintf fmt - "contract %a should be an Equiv statement" - EcSymbols.pp_qsymbol name - ) - in - List.iter (fun (f, ef_f, side) -> - if not (EcReduction.EqTest.for_xp env f ef_f) then begin - tc_error_lazy ?loc pe (fun fmt -> - let ppe = EcPrinting.PPEnv.ofenv env in - Format.fprintf fmt - "%s-side of contract %a should be for the procedure %a, not %a" - side - EcSymbols.pp_qsymbol name - (EcPrinting.pp_funname ppe) f - (EcPrinting.pp_funname ppe) ef_f - ) - end - ) [(fl, contract.ef_fl, "left"); (fr, contract.ef_fr, "right")] - -(* -------------------------------------------------------------------- *) -let abstract_pvs - (hyps : LDecl.hyps) - (ms : memory list) - (pvs : ((prog_var * ty) list) EcIdent.Mid.t) -= - let env = LDecl.toenv hyps in - - let mkinv = - match ms with - | [m] -> fun inv -> Inv_ss { m; inv; } - | [ml; mr] -> fun inv -> Inv_ts { ml; mr; inv; } - | _ -> assert false in - - let for_memory ((subst, hyps) : EcPV.PVM.subst * LDecl.hyps) (m : memory) = - let pvs = EcIdent.Mid.find_def [] m pvs in - - let ids = List.map (fun (pv, ty) -> - (Format.sprintf "%s_" (EcTypes.symbol_of_pv pv)), ty) pvs in - let fresh = LDecl.fresh_ids hyps (List.fst ids) in - let hyps = - List.fold_left2 (fun hyps id (_, ty) -> - LDecl.add_local id (LD_var (ty, None)) hyps) - hyps fresh ids in - let ids = List.combine fresh (List.snd ids) in - - let pvs_as_inv = List.map (fun (pv, ty) -> - mkinv (f_pvar pv ty m).inv - ) pvs in - let subst = List.fold_left (fun subst ((pv, ty), x) -> - EcPV.PVM.add env pv m (f_local x ty) subst - ) subst (List.combine pvs (List.fst ids)) in - - ((subst, hyps), (ids, pvs, pvs_as_inv)) - in - - let (subst, _), ids = - List.fold_left_map for_memory (EcPV.PVM.empty, hyps) ms in - - let ids = List.map proj3_1 ids - and pvs = List.map proj3_2 ids - and pvs_as_inv = List.map proj3_3 ids in - let ids = List.flatten ids in - let pvs = List.flatten pvs in - let pvs_as_inv = List.flatten pvs_as_inv in - - ids, pvs, pvs_as_inv, subst - -(* -------------------------------------------------------------------- *) -(* Forward ecall on Hoare goals (ecall ->>). - * - * Given a goal hoare[c; s : P ==> Q] where c is a call lv <@ f(e) - * and a contract hoare[f : Pf ==> Qf], this tactic: - * - * 1. Computes seqf from the contract postcondition Qf: - * - If lv is present: seqf = Qf[res / lv] - * - If lv is absent: seqf = Qf with conjuncts mentioning res removed - * - * 2. Auto-frames precondition conjuncts independent of the call's - * writes (lv ∪ writes(f)): - * frame = /\{ Pi | Pi in toplevel-conjuncts(P), - * reads(Pi) # (writes(lv) ∪ writes(f)) } - * - * 3. Produces two subgoals: - * {v - * (a) P => Pf[arg / e] (contract precondition holds) - * (b) hoare[s : seqf /\ frame ==> Q] (continuation) - * v} - * - * When the contract has universally-quantified parameters instantiated - * with program variables, these are abstracted into local variables and - * re-generalized in both subgoals. - *) -let t_ecall_hoare_fwd ((cttpt, ctt) : (proofterm * form)) (tc : tcenv1) = - let hyps = FApi.tc1_hyps tc in - let env = EcEnv.LDecl.toenv hyps in - let concl = destr_hoareS (FApi.tc1_goal tc) in - let m = (fst concl.hs_m) in - let (lvalue, funname, _), _ = pf_first_call !!tc concl.hs_s in - - let pvs = PT.collect_pvars_from_pt cttpt in - let ids, _, pvs_as_inv, subst = abstract_pvs hyps [m] pvs in - - let tc = t_hr_exists_intro_r pvs_as_inv tc in - let tc = FApi.t_focus (t_hr_exists_elim_r ~bound:(List.length ids)) tc in - let tc = FApi.t_focus (t_intros_i (List.fst ids)) tc in - - let cttpt = PT.subst_pv_pt env subst cttpt in - let ctt = EcPV.PVM.subst env subst ctt in - - let ctt = - EcReduction.h_red_opt EcReduction.full_red hyps ctt - |> odfl ctt in - - let seqf = - let inv = destr_hoareF ctt in - let _ = assert (POE.is_empty (hf_po inv).hsi_inv) in - let inv = POE.lower (hf_po inv) in - let inv = ss_inv_rebind inv m in - - match lvalue with - | None -> - let not_contains_res (f : form) = - let pvs = EcPV.form_read env EcPV.PMVS.empty f in - let pvs = EcIdent.Mid.find_def EcPV.PV.empty m pvs in - not (EcPV.PV.mem_pv env EcTypes.pv_res pvs) in - map_ss_inv1 - (fun f -> filter_topand_form not_contains_res f |> odfl f_true) - inv - - | Some lvalue -> - let lv = - List.map - (fun (pv, ty) -> (f_pvar pv ty inv.m).inv) - (EcModules.lv_to_ty_list lvalue) in - let sres = - EcPV.PVM.add - env EcTypes.pv_res inv.m - (f_tuple lv) EcPV.PVM.empty in - - { inv = EcPV.PVM.subst env sres inv.inv; m = inv.m; } in - - let seqf_frame = - let wr = lvalue |> omap (EcPV.lp_write env) |> odfl EcPV.PV.empty in - let wr = EcPV.f_write_r env wr funname in - let inv = - filter_topand_form - (fun f -> - let pvs = EcPV.form_read env EcPV.PMVS.empty f in - let pvs = EcIdent.Mid.find_def EcPV.PV.empty m pvs in - EcPV.PV.indep env wr pvs) - (hs_pr concl).inv in - { inv = odfl f_true inv; m = (hs_pr concl).m; } in - - let tc = - FApi.t_first - (EcPhlSeq.t_hoare_seq (GapAfter cpos1_first) (map_ss_inv2 f_and seqf seqf_frame)) - tc in - - let tc = FApi.t_first EcPhlHoare.t_hoaresplit tc in - let tc = FApi.t_first (EcPhlConseq.t_conseqauto ~delta:false ?tsolve:None) tc in - let tc = FApi.t_first EcPhlTAuto.t_hoare_true tc in - - let tc = FApi.t_first (EcPhlCall.t_call None ctt) tc in - let tc = FApi.t_sub [ - EcLowGoal.Apply.t_apply_bwd_hi ~dpe:true cttpt; - EcPhlSkip.t_skip; - t_id - ] tc in - - let tc = - FApi.t_firsts - (t_generalize_hyps ~clear:`Yes (List.fst ids)) 2 tc in - - tc - -(* -------------------------------------------------------------------- *) -(* Backward ecall on Hoare goals (ecall without ->>). - * - * Given a goal hoare[s; c : P ==> Q] where c is a call lv <@ f(e) - * and a contract hoare[f : Pf ==> Qf], this tactic: - * - * 1. Computes the weakest precondition of the call w.r.t. Q using - * compute_hoare_call_post, yielding an intermediate assertion R. - * - * 2. Produces three subgoals: - * {v - * (a) hoare[s : P ==> R] (prefix establishes R) - * (b) hoare[f : Pf ==> Qf] (contract holds) - * (c) (call WP matches R) - * v} - * - * When the contract has universally-quantified parameters instantiated - * with program variables, these are abstracted into local variables and - * re-generalized in the subgoals. Subgoals (b) and (c) are closed - * automatically, leaving only (a) for the user. - *) -let t_ecall_hoare_bwd ((cttpt, _) : proofterm * form) (tc : tcenv1) = - let hyps = FApi.tc1_hyps tc in - let env = EcEnv.LDecl.toenv hyps in - let concl = destr_hoareS (FApi.tc1_goal tc) in - let m = fst (concl.hs_m) in - let call, _ = pf_last_call !!tc concl.hs_s in - - let pvs = PT.collect_pvars_from_pt cttpt in - let ids, _, pvs_as_inv, subst = abstract_pvs hyps [m] pvs in - - let cttpt = - let pt_head, pt_args = - match cttpt with - | PTApply { pt_head; pt_args } -> (pt_head, pt_args) - | _ -> assert false in - let pt_args = List.map (PT.subst_pv_pt_arg env subst) pt_args in - PTApply { pt_head; pt_args } in - - let cttpt, ctt = EcLowGoal.LowApply.check `Elim cttpt (`Hyps (hyps, !!tc)) in - - let ctt = - EcReduction.h_red_opt EcReduction.full_red hyps ctt - |> odfl ctt in - - let ids_subst = - List.fold_left2 - (fun s (id, _) pv -> EcSubst.add_flocal s id (inv_of_inv pv)) - EcSubst.empty ids pvs_as_inv in - - let fpre, fpost = - let hf = destr_hoareF ctt in - (ss_inv_rebind (hf_pr hf) m).inv, (hs_inv_rebind (hf_po hf) m).hsi_inv - in - - let post = - EcPhlCall.compute_hoare_call_post - hyps m (fpre, fpost) call (hs_po concl).hsi_inv in - let post = EcSubst.subst_form ids_subst post in - - let tc = EcPhlSeq.t_hoare_seq (GapBefore cpos1_last) { m = m; inv = post; } tc in - let tc = FApi.t_last (t_hr_exists_intro_r pvs_as_inv) tc in - let tc = FApi.t_last (t_hr_exists_elim_r ~bound:(List.length ids)) tc in - let tc = FApi.t_last (t_intros_i (List.fst ids)) tc in - let tc = FApi.t_last (EcPhlCall.t_call None ctt) tc in - - FApi.t_sub [ - EcLowGoal.t_id; (* initial hoare statement without the call *) - EcLowGoal.Apply.t_apply_bwd_hi ~dpe:true cttpt; (* Prove the Hoare contract *) - EcPhlAuto.t_auto ?conv:None; (* Kill the conseq from the call rule *) - ] tc - -(* -------------------------------------------------------------------- *) -(* Backward ecall on bdHoare/phoare goals (ecall without ->>). - * - * Mirrors t_ecall_hoare_bwd but for a bdHoare goal and a bdHoare/phoare - * contract. We abstract program-variable arguments of the contract pterm - * into fresh local idents, dispatch to [t_call None ctt] (which routes - * via [t_call]'s (FbdHoareF, FbdHoareS) arm to [t_bdhoare_call]), then - * re-generalize over the abstracted idents in the residual goals. - * - * The prefix is split from the call with [t_bdhoare_seq] under a fixed - * trivial probability split (f1=f2=1%r, g1=g2=0%r, pR=true). That split - * only discharges the resulting bound side-conditions when the goal — and - * the contract — are lossless [= 1%r]; both are checked up-front so that - * out-of-scope goals are rejected cleanly rather than failing deep inside - * [t_call]. The residual goal handed back to the user is the prefix, lifted - * to a lossless bdHoare goal so that further ecalls keep dispatching here. - *) -let t_ecall_bdhoare_bwd ((cttpt, _) : proofterm * form) (tc : tcenv1) = - let hyps = FApi.tc1_hyps tc in - let env = EcEnv.LDecl.toenv hyps in - let concl = destr_bdHoareS (FApi.tc1_goal tc) in - let m = fst (concl.bhs_m) in - let call, _ = pf_last_call !!tc concl.bhs_s in - - (* The trivial probability split below (f1=f2=1, g1=g2=0) only discharges - * the bound side-conditions when the goal is [= 1%r] (lossless). For any - * other comparison or bound the construction would fail opaquely deep in - * [t_call]/[apply]; reject up-front with an explanatory message instead. *) - if concl.bhs_cmp <> FHeq || not (f_equal (bhs_bd concl).inv f_r1) then - tc_error !!tc - "backward ecall on phoare goals is only supported for lossless `= 1%%r' goals"; - - let pvs = PT.collect_pvars_from_pt cttpt in - let ids, _, pvs_as_inv, subst = abstract_pvs hyps [m] pvs in - - let cttpt = - let pt_head, pt_args = - match cttpt with - | PTApply { pt_head; pt_args } -> (pt_head, pt_args) - | _ -> assert false in - let pt_args = List.map (PT.subst_pv_pt_arg env subst) pt_args in - PTApply { pt_head; pt_args } in - - let cttpt, ctt = EcLowGoal.LowApply.check `Elim cttpt (`Hyps (hyps, !!tc)) in - - let ctt = - EcReduction.h_red_opt EcReduction.full_red hyps ctt - |> odfl ctt in - - let ids_subst = - List.fold_left2 - (fun s (id, _) pv -> EcSubst.add_flocal s id (inv_of_inv pv)) - EcSubst.empty ids pvs_as_inv in - - let hf = destr_bdHoareF ctt in - - (* The suffix call subgoal is synthesized with bound [= 1%r]; only a - * lossless [= 1%r] contract applies to it. *) - if hf.bhf_cmp <> FHeq || not (f_equal (bhf_bd hf).inv f_r1) then - tc_error !!tc - "backward ecall on phoare goals requires a lossless `= 1%%r' contract"; - - let fpre, fpost = - (ss_inv_rebind (bhf_pr hf) m).inv, (ss_inv_rebind (bhf_po hf) m).inv - in - - let post = - EcPhlCall.compute_hoare_call_post - hyps m (fpre, POE.empty fpost) call - (POE.empty (bhs_po concl).inv) in - let post = EcSubst.subst_form ids_subst post in - - (* Trivial probability split for FHeq with bound 1%r (lossless body): - pR = true, f1=f2=1%r, g1=g2=0%r. Yields: - - cond_phi : f_hoareS pre s1 post (user-level prefix, as hoare) - - condf1 : pre s1 true 1%r (lossless prefix, trivial for det. code) - - condf2 : phi s2 bhs_po 1%r (suffix bdhoare, target of t_call) - - condg1 : pre s1 false 0%r (trivial) - - condbd : 1*1+0*0 = bd (trivial when bd=1%r) - - condnm : forall r1 r2 ... (trivial) *) - let seq_info = - let phi = { m; inv = post } in - let pR = { m; inv = f_true } in - let f1 = { m; inv = f_r1 } in - let f2 = { m; inv = f_r1 } in - let g1 = { m; inv = f_r0 } in - let g2 = { m; inv = f_r0 } in - (phi, pR, f1, f2, g1, g2) - in - - let tc = EcPhlSeq.t_bdhoare_seq (GapBefore cpos1_last) seq_info tc in - (* Subgoal order from t_bdhoare_seq (with f1≠0, f2≠0, g1=0): - [cond_phi; condf1; condf2; condg1; condbd; condnm] - We apply the exists+intros+t_call pipeline to condf2 (the suffix). - The intros chain leaves a single bdhoare goal on which t_call produces - two subgoals (contract obligation + conseq); we then dispatch via - t_seqsub. *) - let condf2_tactic = - FApi.t_seqs [ - t_hr_exists_intro_r pvs_as_inv; - t_hr_exists_elim_r ~bound:(List.length ids); - t_intros_i (List.fst ids); - FApi.t_seqsub - (EcPhlCall.t_call None ctt) - [ EcLowGoal.Apply.t_apply_bwd_hi ~dpe:true cttpt; - EcPhlAuto.t_auto ?conv:None; ]; - ] - in - - (* t_bdhoare_seq_r auto-closes condnm via t_try, so the count is 5 (or 6 if - that auto fails). Use t_onalli with an index-based dispatcher to be - robust to either count. Order (0-indexed): - 0 -> cond_phi (HOARE prefix): lift to BDHOARE [pre ==> phi] FHeq 1%r - via t_hoareS_conseq_bdhoare. This is sound under losslessness of - the prefix — which the user proves as part of the residual goal. - Subsequent ecalls then dispatch through our bdhoare arm and accept - phoare contracts. - 2 -> condf2 (suffix call dance) - others -> auto (trivial bound conditions). *) - let tc = - FApi.t_onalli - (fun i -> - if i = 0 then EcPhlConseq.t_hoareS_conseq_bdhoare - else if i = 2 then condf2_tactic - else EcPhlAuto.t_auto ?conv:None) - tc - in - - tc - -(* -------------------------------------------------------------------- *) -let process_ecall_bdhoare - (dir : APT.pdirection) - (pterm : APT.pecall) - (tc : tcenv1) -= - if dir <> `Backward then - tc_error !!tc "forward ecall on bdHoare/phoare goals is not supported"; - - let (ctt_path, ctt_tvi, ctt_args) = pterm in - let hyps = FApi.tc1_hyps tc in - let concl = destr_bdHoareS (FApi.tc1_goal tc) in - - (* Type-check contract (lemma) - apply it fully to find the bdHoare contract *) - let ptenv = PT.ptenv_of_penv (LDecl.push_active_ss concl.bhs_m hyps) !!tc in - let contract = PT.process_pterm ptenv (APT.FPNamed (ctt_path, ctt_tvi)) in - let contract, _ = PT.process_pterm_args_app contract ctt_args in - let contract = PT.apply_pterm_to_max_holes hyps contract in - - assert (PT.can_concretize contract.PT.ptev_env); - let contract = PT.concretize contract in - - let call, _ = pf_last_call !!tc concl.bhs_s in - - check_contract_type - ~phoare:true ~noexn:false ~loc:(L.loc ctt_path) ~name:(L.unloc ctt_path) - !!tc hyps (`Single call) (snd contract); - - t_ecall_bdhoare_bwd contract tc - -(* -------------------------------------------------------------------- *) -let process_ecall_hoare - (dir : APT.pdirection) - (pterm : APT.pecall) - (tc : tcenv1) -= - let (ctt_path, ctt_tvi, ctt_args) = pterm in - let hyps = FApi.tc1_hyps tc in - let concl = destr_hoareS (FApi.tc1_goal tc) in - - (* Type-check contract (lemma) - apply it fully to find the Hoare contract *) - let ptenv = PT.ptenv_of_penv (LDecl.push_active_ss concl.hs_m hyps) !!tc in - let contract = PT.process_pterm ptenv (APT.FPNamed (ctt_path, ctt_tvi)) in - let contract, _ = PT.process_pterm_args_app contract ctt_args in - let contract = PT.apply_pterm_to_max_holes hyps contract in - - assert (PT.can_concretize contract.PT.ptev_env); - let contract = PT.concretize contract in - - let call, _ = - match dir with - | `Forward -> pf_first_call !!tc concl.hs_s - | `Backward -> pf_last_call !!tc concl.hs_s in - - check_contract_type - ~noexn:(dir <> `Backward) ~loc:(L.loc ctt_path) ~name:(L.unloc ctt_path) - !!tc hyps (`Single call) (snd contract); - - match dir with - | `Forward -> t_ecall_hoare_fwd contract tc - | `Backward -> t_ecall_hoare_bwd contract tc - -(* -------------------------------------------------------------------- *) -let process_ecall_equiv - (dir : APT.pdirection) - (oside : side option) - (pterm : APT.pecall) - (tc : tcenv1) -= - if dir <> `Backward then - tc_error !!tc "unsupported direction for ecall on an equiv. goal"; - - let (ctt_path, ctt_tvi, ctt_args) = pterm in - let hyps = FApi.tc1_hyps tc in - let env = EcEnv.LDecl.toenv hyps in - let concl = destr_equivS (FApi.tc1_goal tc) in - let (ml, _), (mr, _) = concl.es_ml, concl.es_mr in - - (* Type-check contract (lemma) - apply it fully to find the Hoare/Equiv contract *) - let cttpt, _ = - let ptenv = PT.ptenv_of_penv (LDecl.push_active_ts concl.es_ml concl.es_mr hyps) !!tc in - let contract = PT.process_pterm ptenv (APT.FPNamed (ctt_path, ctt_tvi)) in - let contract, _ = PT.process_pterm_args_app contract ctt_args in - let contract = PT.apply_pterm_to_max_holes hyps contract in - assert (PT.can_concretize contract.PT.ptev_env); - PT.concretize contract in - - let pvs = PT.collect_pvars_from_pt cttpt in - let ids, _, pvs_as_inv, subst = abstract_pvs hyps [ml; mr] pvs in - - let cttpt = - let pt_head, pt_args = - match cttpt with - | PTApply { pt_head; pt_args } -> (pt_head, pt_args) - | _ -> assert false in - let pt_args = List.map (PT.subst_pv_pt_arg env subst) pt_args in - PTApply { pt_head; pt_args } in - - let cttpt, ctt = EcLowGoal.LowApply.check `Elim cttpt (`Hyps (hyps, !!tc)) in - - let ctt = - EcReduction.h_red_opt EcReduction.full_red hyps ctt - |> odfl ctt in - - let ids_subst = - List.fold_left2 - (fun s (id, _) pv -> EcSubst.add_flocal s id (inv_of_inv pv)) - EcSubst.empty ids pvs_as_inv in - - let calls = - match oside with - | None -> - let call_l, _ = pf_last_call !!tc concl.es_sl in - let call_r, _ = pf_last_call !!tc concl.es_sr in - `Double (call_l, call_r) - | Some side -> - let call, _ = - pf_last_call !!tc (APT.sideif side concl.es_sl concl.es_sr) - in `Single call - in - - check_contract_type - ~phoare:true ~loc:(L.loc ctt_path) ~name:(L.unloc ctt_path) - !!tc hyps calls ctt; - - match calls with - | `Single call -> begin - let side = oget oside in - let m = APT.sideif side ml mr in - - let fpre, fpost = - match ctt.f_node with - | FhoareF hf -> - assert (POE.is_empty (hf_po hf).hsi_inv); - (ss_inv_rebind (hf_pr hf) m).inv, - (hs_inv_rebind (hf_po hf) m).hsi_inv.main - | FbdHoareF hf -> - (ss_inv_rebind (bhf_pr hf) m).inv, - (ss_inv_rebind (bhf_po hf) m).inv - | _ -> assert false - in - - let post = - EcPhlCall.compute_equiv1_call_post - hyps side (ml, mr) (fpre, fpost) call (es_po concl).inv in - let post = EcSubst.subst_form ids_subst post in - - let pos = - APT.sideif side - (GapBefore cpos1_last, GapAfter cpos1_last) - (GapAfter cpos1_last, GapBefore cpos1_last) in - - let tc = EcPhlSeq.t_equiv_seq pos { ml; mr; inv = post; } tc in - let tc = FApi.t_last (t_hr_exists_intro_r pvs_as_inv) tc in - let tc = FApi.t_last (t_hr_exists_elim_r ~bound:(List.length ids)) tc in - let tc = FApi.t_last (t_intros_i (List.fst ids)) tc in - let tc = FApi.t_last (EcPhlCall.t_call (Some side) ctt) tc in - - FApi.t_sub [ - EcLowGoal.t_id; (* initial equiv statement without the call *) - EcLowGoal.Apply.t_apply_bwd_hi ~dpe:true cttpt; (* Prove the Hoare contract *) - EcPhlAuto.t_auto ?conv:None; (* Kill the conseq from the call rule *) - ] tc - end - - | `Double (call_l, call_r) -> begin - let fpre, fpost = - let hf = destr_equivF ctt in - (ts_inv_rebind (ef_pr hf) ml mr).inv, - (ts_inv_rebind (ef_po hf) ml mr).inv - in - - let post = - EcPhlCall.compute_equiv_call_post - hyps (ml, mr) (fpre, fpost) call_l call_r (es_po concl).inv in - let post = EcSubst.subst_form ids_subst post in - - let tc = - EcPhlSeq.t_equiv_seq - (GapBefore cpos1_last, GapBefore cpos1_last) - { ml; mr; inv = post; } tc in - - let tc = FApi.t_last (t_hr_exists_intro_r pvs_as_inv) tc in - let tc = FApi.t_last (t_hr_exists_elim_r ~bound:(List.length ids)) tc in - let tc = FApi.t_last (t_intros_i (List.fst ids)) tc in - let tc = FApi.t_last (EcPhlCall.t_call None ctt) tc in - - FApi.t_sub [ - EcLowGoal.t_id; (* initial equiv statement without the call *) - EcLowGoal.Apply.t_apply_bwd_hi ~dpe:true cttpt; (* Prove the Hoare contract *) - EcPhlAuto.t_auto ?conv:None; (* Kill the conseq from the call rule *) - ] tc - end - -(* -------------------------------------------------------------------- *) -let process_ecall - (dir : APT.pdirection) - (oside : side option) - (pterm : APT.pecall) - (tc : tcenv1) -= match (FApi.tc1_goal tc).f_node with - | FhoareS _ -> - if Option.is_some oside then - tc_error !!tc "cannot provide a side for Hoare goals"; - process_ecall_hoare dir pterm tc - - | FbdHoareS _ -> - if Option.is_some oside then - tc_error !!tc "cannot provide a side for bdHoare/phoare goals"; - process_ecall_bdhoare dir pterm tc - - | FequivS _ -> - process_ecall_equiv dir oside pterm tc + | FhoareF _ | FhoareS _ -> + EcHoareExists.t_hoare_exists_elim { hxe_bound = bound } tc + | FeHoareF _ | FeHoareS _ -> + EcEHoareExists.t_ehoare_exists_elim { ehxe_bound = bound } tc + | FbdHoareF _ | FbdHoareS _ -> + EcBdHoareExists.t_bdhoare_exists_elim { bxe_bound = bound } tc + | FequivF _ | FequivS _ -> + EcEquivExists.t_equiv_exists_elim { exe_bound = bound } tc + | _ -> tc_error_noXhl ~kinds:hlkinds_Xhl !!tc + +let t_hr_exists_elim (tc : tcenv1) = + t_hr_exists_elim_r tc + +(* -------------------------------------------------------------------- *) +let t_hr_exists_intro (fs : inv list) (tc : tcenv1) = + let as_ss = function + | Inv_ss f -> f + | _ -> failwith "expected all invariants to have same kind" in + let as_ts = function + | Inv_ts f -> f + | _ -> failwith "expected all invariants to have same kind" in + match (FApi.tc1_goal tc).f_node with + | FhoareF _ | FhoareS _ -> + EcHoareExists.t_hoare_exists_intro (List.map as_ss fs) tc + | FeHoareF _ | FeHoareS _ -> + EcEHoareExists.t_ehoare_exists_intro (List.map as_ss fs) tc + | FbdHoareF _ | FbdHoareS _ -> + EcBdHoareExists.t_bdhoare_exists_intro (List.map as_ss fs) tc + | FequivF _ | FequivS _ -> + EcEquivExists.t_equiv_exists_intro (List.map as_ts fs) tc + | _ -> tc_error_noXhl ~kinds:hlkinds_Xhl !!tc + +(* -------------------------------------------------------------------- *) +let process_exists_intro ~(elim : bool) fs (tc : tcenv1) = + match (FApi.tc1_goal tc).f_node with + | FhoareF _ | FhoareS _ -> + EcHoareExists.process_hoare_exists_intro ~elim fs tc + | FeHoareF _ | FeHoareS _ -> + EcEHoareExists.process_ehoare_exists_intro ~elim fs tc + | FbdHoareF _ | FbdHoareS _ -> + EcBdHoareExists.process_bdhoare_exists_intro ~elim fs tc + | FequivF _ | FequivS _ -> + EcEquivExists.process_equiv_exists_intro ~elim fs tc + | _ -> tc_error_noXhl ~kinds:hlkinds_Xhl !!tc + +(* -------------------------------------------------------------------- *) +let process_ecall dir oside pterm (tc : tcenv1) = + match (FApi.tc1_goal tc).f_node with + | FhoareS _ -> EcHoareECall.process_hoare_ecall dir oside pterm tc + | FbdHoareS _ -> EcBdHoareECall.process_bdhoare_ecall dir oside pterm tc + | FequivS _ -> EcEquivECall.process_equiv_ecall dir oside pterm tc | _ -> tc_error_noXhl ~kinds:[`Hoare `Stmt; `PHoare `Stmt; `Equiv `Stmt] !!tc diff --git a/src/phl/rules/bdhoare/ecBdHoareECall.ml b/src/phl/rules/bdhoare/ecBdHoareECall.ml new file mode 100644 index 000000000..bcb7b5038 --- /dev/null +++ b/src/phl/rules/bdhoare/ecBdHoareECall.ml @@ -0,0 +1,136 @@ +(* -------------------------------------------------------------------- *) +open EcUtils +open EcAst +open EcFol +open EcEnv +open EcSubst + +open EcCoreGoal +open EcLowGoal +open EcLowPhlGoal +open EcMatching.Position + +module L = EcLocation +module APT = EcParsetree +module PT = EcProofTerm + +(* -------------------------------------------------------------------- *) +(* Derived (no proof-node). *) +let t_bdhoare_ecall_bwd ((cttpt, _) : proofterm * form) (tc : tcenv1) = + let hyps = FApi.tc1_hyps tc in + let env = LDecl.toenv hyps in + let bhs = tc1_as_bdhoareS tc in + let m = fst bhs.bhs_m in + let call, _ = pf_last_call !!tc bhs.bhs_s in + + (* The trivial probability split below (f1 = f2 = 1, g1 = g2 = 0) only + discharges the bound side conditions when the goal is [= 1%r]. *) + if bhs.bhs_cmp <> FHeq || not (f_equal (bhs_bd bhs).inv f_r1) then + tc_error !!tc + "backward ecall on phoare goals is only supported for lossless `= 1%%r' goals"; + + let pvs = PT.collect_pvars_from_pt cttpt in + let ids, pvs, subst = EcPlECall.abstract_pvs hyps [m] pvs in + + let cttpt = EcPlECall.subst_pt_args env subst cttpt in + let cttpt, ctt = LowApply.check `Elim cttpt (`Hyps (hyps, !!tc)) in + + let ctt = + EcReduction.h_red_opt EcReduction.full_red hyps ctt + |> odfl ctt in + + let hf = destr_bdHoareF ctt in + + (* The suffix is given the bound [= 1%r]: only a lossless contract + applies to it. *) + if hf.bhf_cmp <> FHeq || not (f_equal (bhf_bd hf).inv f_r1) then + tc_error !!tc + "backward ecall on phoare goals requires a lossless `= 1%%r' contract"; + + let fpre, fpost = + (ss_inv_rebind (bhf_pr hf) m).inv, (ss_inv_rebind (bhf_po hf) m).inv + in + + let mid = + EcHoareCall.hoare_call_wp + hyps m (fpre, POE.empty fpost) call + (POE.empty (bhs_po bhs).inv) in + let mid = EcSubst.subst_form (EcPlECall.restore_pvs ids pvs) mid in + + (* [seq] before the call, with the trivial split + [phi = mid, R = true, f1 = f2 = 1%r, g1 = g2 = 0%r]: premises + (H) hoare [c : P ==> mid], (F1) phoare [c : P ==> true] = 1%r, + (F2) phoare [lv <@ f(a) : mid /\ true ==> Q] = 1%r, + (G1) phoare [c : P ==> false] = 0%r, (B), and (N) when not closed. *) + let tc = + EcBdHoareSeq.t_bdhoare_seq_full + { bsr_at = GapBefore cpos1_last; + bsr_phi = { m; inv = mid }; + bsr_r = { m; inv = f_true }; + bsr_f1 = { m; inv = f_r1 }; + bsr_f2 = { m; inv = f_r1 }; + bsr_g1 = { m; inv = f_r0 }; + bsr_g2 = { m; inv = f_r0 }; } + tc in + + (* (F2): the exists rules, abstracting the program variables of the + contract, then the call rule. *) + let t_suffix = + FApi.t_seqs [ + EcBdHoareExists.t_bdhoare_exists_intro + (List.map (fun inv -> { m; inv }) pvs); + EcBdHoareExists.t_bdhoareS_exists_elim + { bxe_bound = Some (List.length ids) }; + t_intros_i (List.fst ids); + FApi.t_seqsub + (EcBdHoareCall.t_bdhoare_call + { bhcall_pre = bhf_pr hf; bhcall_post = bhf_po hf; + bhcall_bd = None; }) + [ Apply.t_apply_bwd_hi ~dpe:true cttpt; + EcPhlAuto.t_auto ?conv:None; ]; + ] + in + + (* (H) is lifted to [phoare [c : P ==> mid] = 1%r], so that further + [ecall]s on it are on a phoare goal (the prefix must then be proved + lossless); (F2) as above; the other premises (the count of which + depends on whether (N) was closed) by [auto]. + + TEMPORARY: the lifting still comes from the not-yet-migrated + [EcPhlConseq]. *) + FApi.t_onalli + (fun i -> + if i = 0 then EcPhlConseq.t_hoareS_conseq_bdhoare + else if i = 2 then t_suffix + else EcPhlAuto.t_auto ?conv:None) + tc + +(* -------------------------------------------------------------------- *) +(* Elaboration: the goal is known to be a [bdHoareS]. *) +let process_bdhoare_ecall + (dir : APT.pdirection) + (oside : APT.oside) + (pterm : APT.pecall) + (tc : tcenv1) += + if Option.is_some oside then + tc_error !!tc "cannot provide a side for bdHoare/phoare goals"; + + if dir <> `Backward then + tc_error !!tc "forward ecall on bdHoare/phoare goals is not supported"; + + let (ctt_path, _, _) = pterm in + let hyps = FApi.tc1_hyps tc in + let bhs = tc1_as_bdhoareS tc in + + let contract = + EcPlECall.process_contract + !!tc (LDecl.push_active_ss bhs.bhs_m hyps) hyps pterm in + + let call, _ = pf_last_call !!tc bhs.bhs_s in + + EcPlECall.check_contract_type + ~phoare:true ~noexn:false ~loc:(L.loc ctt_path) ~name:(L.unloc ctt_path) + !!tc hyps (`Single call) (snd contract); + + t_bdhoare_ecall_bwd contract tc diff --git a/src/phl/rules/bdhoare/ecBdHoareECall.mli b/src/phl/rules/bdhoare/ecBdHoareECall.mli new file mode 100644 index 000000000..51a09e639 --- /dev/null +++ b/src/phl/rules/bdhoare/ecBdHoareECall.mli @@ -0,0 +1,59 @@ +(* -------------------------------------------------------------------- *) +open EcParsetree +open EcCoreGoal +open EcCoreGoal.FApi +open EcAst + +(* ==================================================================== *) +(* Rules (trusted) *) + +(* None: [ecall] is derived from the [seq], [call], [exists] and + consequence rules. *) + +(* ==================================================================== *) +(* Derived tactics *) + +(* [t_bdhoare_ecall_bwd (pt, C)] ([ecall]) — on + [phoare [c; lv <@ f(a) : P ==> Q] = 1%r], [(pt, C)] being the + contract: a proof term [pt] of [C = phoare [f : Pf ==> Qf] = 1%r], + whose arguments may mention program variables [vs] of the goal's + memory, abstracted as in [EcHoareECall] (fresh locals [ids], [X(ids)] + for [X] with [vs] replaced by [ids]): + 1. [EcBdHoareSeq.t_bdhoare_seq_full] before the call, with + [phi := W(vs)] ([W(ids)] being the weakest precondition of the call + for [C(ids)], as for hoare), [R := true], [f1 = f2 := 1%r], + [g1 = g2 := 0%r]; + its premises are, in order: + (H) hoare [c : P ==> W(vs)], + (F1) phoare [c : P ==> true] = 1%r, + (F2) phoare [lv <@ f(a) : W(vs) /\ true ==> Q] = 1%r, + (G1) phoare [c : P ==> !true] = 0%r, + (B) the bound condition, + (N) the non-modification condition, when not closed; + 2. (H) is lifted (currently by + [EcPhlConseq.t_hoareS_conseq_bdhoare]) to + [phoare [c : P ==> W(vs)] = 1%r] — left open, so that further + [ecall]s on it apply; + 3. (F2): the program variables [vs] are abstracted with + [EcBdHoareExists.t_bdhoare_exists_intro] and + [EcBdHoareExists.t_bdhoareS_exists_elim], then + [EcBdHoareCall.t_bdhoare_call] with [C(ids)] (on an empty prefix): + its specification premise is closed by [pt(ids)], its other + premise by [EcPhlAuto.t_auto] (left open if it does not); + 4. the other premises by [EcPhlAuto.t_auto] (left open if it does + not). + Side conditions: the goal and the contract are [= 1%r] (otherwise + fails, before acting). Visible goals: the lifted (H), then what 3. + and 4. leave open. Emits no node of its own. The call rule keeps its + composite statement ([EcBdHoareCall]): here on an empty prefix. *) +val t_bdhoare_ecall_bwd : proofterm * form -> backward + +(* ==================================================================== *) +(* Elaboration *) + +(* [ecall (c args)] on a [bdHoareS] goal: no side, no [->>]; the + contract typed with the goal's memory, applied to as many holes as + possible; it must be a specification of the procedure called last + ([EcPlECall.check_contract_type], [hoare] or [phoare]). Applies + [t_bdhoare_ecall_bwd]. *) +val process_bdhoare_ecall : pdirection -> oside -> pecall -> backward diff --git a/src/phl/rules/bdhoare/ecBdHoareExists.ml b/src/phl/rules/bdhoare/ecBdHoareExists.ml new file mode 100644 index 000000000..22d9a0d00 --- /dev/null +++ b/src/phl/rules/bdhoare/ecBdHoareExists.ml @@ -0,0 +1,111 @@ +(* -------------------------------------------------------------------- *) +open EcFol +open EcAst +open EcEnv +open EcSubst + +open EcCoreGoal +open EcLowGoal +open EcLowPhlGoal + +module TTC = EcProofTyping + +(* -------------------------------------------------------------------- *) +(* Parameter of the bdhoare [exists] elimination rules: the maximal + number of binders to eliminate ([None]: all of them, see + [EcPlExists.prenex_exists]). Nothing to resolve: the same record is the + rule argument and the node payload. *) +type bdhoare_exists_elim = { + bxe_bound : int option; +} + +type EcCoreGoal.rule += + | RBdHoareSExistsElim of bdhoare_exists_elim + | RBdHoareFExistsElim of bdhoare_exists_elim + +(* -------------------------------------------------------------------- *) +(* Pure core shared by the rules and their checkers. *) +let bdhoareS_exists_elim_subgoals (bhs : bdHoareS) (n : bdhoare_exists_elim) = + let pre = bhs_pr bhs in + let bd, inv = EcPlExists.prenex_exists ?bound:n.bxe_bound pre.inv in + [f_forall bd + (f_bdHoareS (snd bhs.bhs_m) { pre with inv } bhs.bhs_s + (bhs_po bhs) bhs.bhs_cmp (bhs_bd bhs))] + +let bdhoareF_exists_elim_subgoals (bhf : bdHoareF) (n : bdhoare_exists_elim) = + let pre = bhf_pr bhf in + let bd, inv = EcPlExists.prenex_exists ?bound:n.bxe_bound pre.inv in + [f_forall bd + (f_bdHoareF { pre with inv } bhf.bhf_f + (bhf_po bhf) bhf.bhf_cmp (bhf_bd bhf))] + +(* -------------------------------------------------------------------- *) +(* Rules (TCB). *) +let t_bdhoareS_exists_elim (r : bdhoare_exists_elim) (tc : tcenv1) = + let bhs = tc1_as_bdhoareS tc in + FApi.xrule1 tc (RBdHoareSExistsElim r) (bdhoareS_exists_elim_subgoals bhs r) + +let t_bdhoareF_exists_elim (r : bdhoare_exists_elim) (tc : tcenv1) = + let bhf = tc1_as_bdhoareF tc in + FApi.xrule1 tc (RBdHoareFExistsElim r) (bdhoareF_exists_elim_subgoals bhf r) + +(* -------------------------------------------------------------------- *) +let () = + register_rule_checker + (function + | RBdHoareSExistsElim n -> + Some (EcPlRecheck.checker_of "bdhoareS-exists-elim" pf_as_bdhoareS + (fun _hyps bhs -> bdhoareS_exists_elim_subgoals bhs n)) + | RBdHoareFExistsElim n -> + Some (EcPlRecheck.checker_of "bdhoareF-exists-elim" pf_as_bdhoareF + (fun _hyps bhf -> bdhoareF_exists_elim_subgoals bhf n)) + | _ -> None) + +(* -------------------------------------------------------------------- *) +(* Derived (no proof-node). *) +let t_bdhoare_exists_elim (r : bdhoare_exists_elim) (tc : tcenv1) = + match (FApi.tc1_goal tc).f_node with + | FbdHoareS _ -> t_bdhoareS_exists_elim r tc + | FbdHoareF _ -> t_bdhoareF_exists_elim r tc + | _ -> tc_error_noXhl ~kinds:hlkinds_Xhl !!tc + +(* The consequence rule, whose premise on the precondition is closed by + giving the values of [fs] as witnesses. + + TEMPORARY: the consequence rule still comes from the not-yet-migrated + [EcPhlConseq]. *) +let t_bdhoare_exists_intro (fs : ss_inv list) (tc : tcenv1) = + let hyps = FApi.tc1_hyps tc in + let xs = EcPlExists.intro_binders (List.map (fun f -> Inv_ss f) fs) in + let pre = EcPlExists.intro_pre ~ehoare:false xs (tc1_get_pre tc) in + let h = LDecl.fresh_id hyps "h" in + let m = LDecl.fresh_id hyps "&m" in + let args = List.map (fun f -> PAFormula (ss_inv_rebind f m).inv) fs in + let t_pre = + FApi.t_seqs [t_intros_i [m; h]; t_exists_intro_s args; t_apply_hyp h] in + FApi.t_seqsub + (EcPhlConseq.t_conseq pre (tc1_get_post tc)) + [t_pre; t_trivial; t_id] + tc + +(* -------------------------------------------------------------------- *) +(* Elaboration: the goal is known to be a [bdHoareS] or a [bdHoareF]. *) +let process_bdhoare_exists_intro + ~(elim : bool) (fs : EcParsetree.pformula list) (tc : tcenv1) += + let hyps, concl = FApi.tc1_flat tc in + let penv, m = + match concl.f_node with + | FbdHoareF bhf -> fst (LDecl.hoareF bhf.bhf_m bhf.bhf_f hyps), bhf.bhf_m + | FbdHoareS bhs -> LDecl.push_active_ss bhs.bhs_m hyps, fst bhs.bhs_m + | _ -> tc_error_noXhl ~kinds:hlkinds_Xhl !!tc in + let fs = + List.map + (fun f -> { m; inv = TTC.pf_process_form_opt !!tc penv None f }) + fs in + if elim then + FApi.t_seq + (t_bdhoare_exists_intro fs) + (t_bdhoare_exists_elim { bxe_bound = Some (List.length fs) }) + tc + else t_bdhoare_exists_intro fs tc diff --git a/src/phl/rules/bdhoare/ecBdHoareExists.mli b/src/phl/rules/bdhoare/ecBdHoareExists.mli new file mode 100644 index 000000000..264ef1872 --- /dev/null +++ b/src/phl/rules/bdhoare/ecBdHoareExists.mli @@ -0,0 +1,65 @@ +(* -------------------------------------------------------------------- *) +open EcCoreGoal.FApi +open EcAst + +(* ==================================================================== *) +(* Rules (trusted) *) + +type bdhoare_exists_elim = { + bxe_bound : int option; (* at most that many binders; all if none *) +} + +(* [t_bdhoareS_exists_elim { bxe_bound = b }] — elimination of the + existentials of the precondition, [~ d] the goal's comparison and + bound (any comparison): + + forall xs, phoare [c : P' ==> Q] ~ d + ------------------------------------ (xs, P') = prenex_b(P) + phoare [c : P ==> Q] ~ d + + where [prenex_b(P)] is [EcPlExists.prenex_exists ?bound:b P]: [P] + with (at most [b] of) its existential binders [xs] pulled to the + front, freshened; [P] entails [exists xs, P'], and [xs] occur nowhere + else (in particular, not in [d]). When [P] has no binder to pull, [xs] + is empty and the premise is the conclusion. + + Node: [RBdHoareSExistsElim { bxe_bound = b }]. + Checker: "bdhoareS-exists-elim". *) +val t_bdhoareS_exists_elim : bdhoare_exists_elim -> backward + +(* [t_bdhoareF_exists_elim { bxe_bound = b }] — same for a procedure: + + forall xs, phoare [f : P' ==> Q] ~ d + ------------------------------------ (xs, P') = prenex_b(P) + phoare [f : P ==> Q] ~ d + + Node: [RBdHoareFExistsElim { bxe_bound = b }]. + Checker: "bdhoareF-exists-elim". *) +val t_bdhoareF_exists_elim : bdhoare_exists_elim -> backward + +(* ==================================================================== *) +(* Derived tactics *) + +(* [t_bdhoare_exists_elim r] — [t_bdhoareS_exists_elim r] or + [t_bdhoareF_exists_elim r], depending on the goal. *) +val t_bdhoare_exists_elim : bdhoare_exists_elim -> backward + +(* [t_bdhoare_exists_intro fs] — on [phoare [c : P ==> Q] ~ d] (or on a + procedure [f]), [fs] formulas in the memory of the judgement, with + [xs] fresh locals named after [fs] ([EcPlExists.intro_binders]): + 1. the consequence rule (currently [EcPhlConseq.t_conseq]) to + [phoare [c : exists xs, (/\_i xs_i = fs_i) /\ P ==> Q] ~ d]; + 2. its premise [forall &m, P => exists xs, ...] is closed by giving + [fs] as witnesses, its premise on the postcondition by [trivial]. + Visible goal: the judgement of 1. Emits no node of its own. *) +val t_bdhoare_exists_intro : ss_inv list -> backward + +(* ==================================================================== *) +(* Elaboration *) + +(* [exists* fs] / [exlim fs] ([~elim]) on a [bdHoareS] or [bdHoareF] + goal: [fs] typed in the memory of the judgement; + [t_bdhoare_exists_intro], followed for [exlim] by + [t_bdhoare_exists_elim] on as many binders as there are formulas. *) +val process_bdhoare_exists_intro : + elim:bool -> EcParsetree.pformula list -> backward diff --git a/src/phl/rules/ecPlECall.ml b/src/phl/rules/ecPlECall.ml new file mode 100644 index 000000000..bc9a4e09d --- /dev/null +++ b/src/phl/rules/ecPlECall.ml @@ -0,0 +1,156 @@ +(* -------------------------------------------------------------------- *) +open EcUtils +open EcAst +open EcFol +open EcEnv + +open EcCoreGoal + +module L = EcLocation +module APT = EcParsetree +module PT = EcProofTerm + +(* -------------------------------------------------------------------- *) +type call = EcModules.lvalue option * EcPath.xpath * EcTypes.expr list + +type calls = [ + | `Single of call + | `Double of call * call +] + +(* -------------------------------------------------------------------- *) +let process_contract + (pe : proofenv) + (penv : LDecl.hyps) + (hyps : LDecl.hyps) + ((ctt_path, ctt_tvi, ctt_args) : APT.pecall) += + let ptenv = PT.ptenv_of_penv penv pe in + let contract = PT.process_pterm ptenv (APT.FPNamed (ctt_path, ctt_tvi)) in + let contract, _ = PT.process_pterm_args_app contract ctt_args in + let contract = PT.apply_pterm_to_max_holes hyps contract in + assert (PT.can_concretize contract.PT.ptev_env); + PT.concretize contract + +(* -------------------------------------------------------------------- *) +let check_contract_type + ?(loc : L.t option) + ?(phoare : bool = false) + ?(noexn : bool = true) + ~(name : EcSymbols.qsymbol) + (pe : proofenv) + (hyps : LDecl.hyps) + (calls : calls) + (contract : form) += + let env = LDecl.toenv hyps in + + let contract = + EcReduction.h_red_opt EcReduction.full_red hyps contract + |> odfl contract in + + match calls with + | `Single (_, funname, _) -> begin + let cttfname = + match contract.f_node with + | FhoareF hf -> + if noexn then begin + if not (POE.is_empty (hf_po hf).hsi_inv) then + tc_error ?loc pe + "contract must have an empty exception post-condition"; + end; + hf.hf_f + | FbdHoareF hf when phoare -> hf.bhf_f + | _ -> + tc_error_lazy ?loc pe (fun fmt -> + Format.fprintf fmt + "contract %a should be a Hoare statement" + EcSymbols.pp_qsymbol name + ) + in + if not (EcReduction.EqTest.for_xp env funname cttfname) then begin + tc_error_lazy ?loc pe (fun fmt -> + let ppe = EcPrinting.PPEnv.ofenv env in + Format.fprintf fmt + "contract %a should be for the procedure %a, not %a" + EcSymbols.pp_qsymbol name + (EcPrinting.pp_funname ppe) funname + (EcPrinting.pp_funname ppe) cttfname + ) + end; + end + + | `Double ((_, fl, _), (_, fr, _)) -> + let contract = + try + destr_equivF contract + with DestrError _ -> + tc_error_lazy ?loc pe (fun fmt -> + Format.fprintf fmt + "contract %a should be an Equiv statement" + EcSymbols.pp_qsymbol name + ) + in + List.iter (fun (f, ef_f, side) -> + if not (EcReduction.EqTest.for_xp env f ef_f) then begin + tc_error_lazy ?loc pe (fun fmt -> + let ppe = EcPrinting.PPEnv.ofenv env in + Format.fprintf fmt + "%s-side of contract %a should be for the procedure %a, not %a" + side + EcSymbols.pp_qsymbol name + (EcPrinting.pp_funname ppe) f + (EcPrinting.pp_funname ppe) ef_f + ) + end + ) [(fl, contract.ef_fl, "left"); (fr, contract.ef_fr, "right")] + +(* -------------------------------------------------------------------- *) +let abstract_pvs + (hyps : LDecl.hyps) + (ms : memory list) + (pvs : ((prog_var * ty) list) EcIdent.Mid.t) += + let env = LDecl.toenv hyps in + + let for_memory ((subst, hyps) : EcPV.PVM.subst * LDecl.hyps) (m : memory) = + let pvs = EcIdent.Mid.find_def [] m pvs in + + let ids = List.map (fun (pv, ty) -> + (Format.sprintf "%s_" (EcTypes.symbol_of_pv pv)), ty) pvs in + let fresh = LDecl.fresh_ids hyps (List.fst ids) in + let hyps = + List.fold_left2 (fun hyps id (_, ty) -> + LDecl.add_local id (LD_var (ty, None)) hyps) + hyps fresh ids in + let ids = List.combine fresh (List.snd ids) in + + let pvfs = List.map (fun (pv, ty) -> (f_pvar pv ty m).inv) pvs in + let subst = List.fold_left (fun subst ((pv, ty), x) -> + EcPV.PVM.add env pv m (f_local x ty) subst + ) subst (List.combine pvs (List.fst ids)) in + + ((subst, hyps), (ids, pvfs)) + in + + let (subst, _), ids = + List.fold_left_map for_memory (EcPV.PVM.empty, hyps) ms in + + let pvfs = List.flatten (List.snd ids) in + let ids = List.flatten (List.fst ids) in + + ids, pvfs, subst + +(* -------------------------------------------------------------------- *) +let restore_pvs (ids : (EcIdent.t * ty) list) (pvfs : form list) = + List.fold_left2 + (fun s (id, _) pv -> EcSubst.add_flocal s id pv) + EcSubst.empty ids pvfs + +(* -------------------------------------------------------------------- *) +let subst_pt_args (env : env) (subst : EcPV.PVM.subst) (pt : proofterm) = + match pt with + | PTApply { pt_head; pt_args } -> + let pt_args = List.map (PT.subst_pv_pt_arg env subst) pt_args in + PTApply { pt_head; pt_args } + | _ -> assert false diff --git a/src/phl/rules/ecPlECall.mli b/src/phl/rules/ecPlECall.mli new file mode 100644 index 000000000..2ac5b70d1 --- /dev/null +++ b/src/phl/rules/ecPlECall.mli @@ -0,0 +1,60 @@ +(* -------------------------------------------------------------------- *) +open EcAst +open EcEnv +open EcCoreGoal + +(* -------------------------------------------------------------------- *) +(* Elaboration shared by the [ecall] tactics of every logic + ([EcECall]): a call whose specification is given as a proof + term (the contract), whose program variables are abstracted. *) + +type call = EcModules.lvalue option * EcPath.xpath * EcTypes.expr list + +type calls = [ + | `Single of call + | `Double of call * call +] + +(* [process_contract pe penv hyps (c, tvi, args)] types the contract [c] + applied to [args] in [penv] (the hypotheses with the memories of the + goal), applies it to as many holes as possible in [hyps], and returns + it as a proof term and its conclusion. *) +val process_contract : + proofenv -> LDecl.hyps -> LDecl.hyps -> EcParsetree.pecall + -> proofterm * form + +(* [check_contract_type ?loc ?phoare ?noexn ~name pe hyps calls ctt] + checks that [ctt] (after a head reduction) is a specification of the + called procedure(s): [hoare [f : _ ==> _]] (with no exceptional + postcondition when [noexn], the default), or [phoare [f : _ ==> _]] if + [phoare], for a [`Single] call; [equiv [fL ~ fR : _ ==> _]] for a + [`Double] call. Fails with a user-facing error otherwise. *) +val check_contract_type : + ?loc:EcLocation.t + -> ?phoare:bool + -> ?noexn:bool + -> name:EcSymbols.qsymbol + -> proofenv + -> LDecl.hyps + -> calls + -> form + -> unit + +(* [abstract_pvs hyps ms pvs] is [(ids, fs, s)]: for each memory of + [ms], in order, each program variable [x] of [pvs] in that memory is + given a fresh local [x_] (typed, in [ids]); [fs] are the program + variables as formulas, [s] the substitution of the program variables by + their locals. *) +val abstract_pvs : + LDecl.hyps + -> memory list + -> ((prog_var * ty) list) EcIdent.Mid.t + -> (EcIdent.t * ty) list * form list * EcPV.PVM.subst + +(* [restore_pvs ids fs] substitutes back the program variables [fs] for + their locals [ids]. *) +val restore_pvs : (EcIdent.t * ty) list -> form list -> EcSubst.subst + +(* [subst_pt_args env s pt] applies the substitution [s] of program + variables to the arguments of the proof term [pt] (an application). *) +val subst_pt_args : env -> EcPV.PVM.subst -> proofterm -> proofterm diff --git a/src/phl/rules/ecPlExists.ml b/src/phl/rules/ecPlExists.ml new file mode 100644 index 000000000..318db109c --- /dev/null +++ b/src/phl/rules/ecPlExists.ml @@ -0,0 +1,44 @@ +(* -------------------------------------------------------------------- *) +open EcAst +open EcFol + +open EcLowPhlGoal + +(* -------------------------------------------------------------------- *) +let prenex_exists ?(bound : int option) (pre : form) = + try destr_exists_prenex ?bound pre + with DestrError _ -> ([], pre) + +(* -------------------------------------------------------------------- *) +let intro_binders (fs : inv list) = + let do1 (f : inv) = + let id = + match f with + | Inv_ss f -> begin + match f.inv.f_node with + | Fpvar (pv, m) -> id_of_pv pv m + | _ -> EcIdent.create "f" + end + | Inv_ts f -> begin + match f.inv.f_node with + | Fpvar (pv, m) -> id_of_pv ~mc:(f.ml, f.mr) pv m + | _ -> EcIdent.create "f" + end + | Inv_hs _ -> assert false + in (id, f) + in List.map do1 fs + +(* -------------------------------------------------------------------- *) +let intro_pre ~(ehoare : bool) (xs : (EcIdent.t * inv) list) (pre : inv) = + let eqs = + List.map + (fun (x, f) -> map_inv1 (f_eq (f_local x (inv_of_inv f).f_ty)) f) + xs in + let bd = List.map (fun (x, f) -> (x, GTty (inv_of_inv f).f_ty)) xs in + let eqs = + match eqs with + | [] -> map_inv1 (fun _ -> f_true) pre + | _ -> map_inv f_ands eqs in + if ehoare + then map_inv2 f_interp_ehoare_form (map_inv1 (f_exists bd) eqs) pre + else map_inv1 (f_exists bd) (map_inv2 f_and eqs pre) diff --git a/src/phl/rules/ecPlExists.mli b/src/phl/rules/ecPlExists.mli new file mode 100644 index 000000000..06ed9d3bb --- /dev/null +++ b/src/phl/rules/ecPlExists.mli @@ -0,0 +1,29 @@ +(* -------------------------------------------------------------------- *) +open EcAst + +(* -------------------------------------------------------------------- *) +(* Computations shared by the existential rules of every logic + ([EcExists]): eliminating the existentials of a precondition, + and quantifying a precondition over the values of formulas. *) + +(* [prenex_exists ?bound P] is [(xs, P')], [exists xs, P'] being [P] with + its existential binders pulled to the front, as computed by + [EcFol.destr_exists_prenex] (binders freshened): with [bound = n], at + most [n] leading binders (also under the left operand of an ehoare + precondition [_ `|` _]); with no bound, every binder reachable through + [/\], [\/], the right operand of [=>] and the branches of [if] too. It + is [([], P)] when there is no binder to pull. [P] entails [exists xs, + P'] (types being inhabited), which makes the elimination rules sound. *) +val prenex_exists : ?bound:int -> form -> bindings * form + +(* [intro_binders fs] pairs each formula of [fs] with a fresh local named + after it when it is a program variable ([x] or [x_L] / [x_R]), [f] + otherwise. *) +val intro_binders : inv list -> (EcIdent.t * inv) list + +(* [intro_pre ~ehoare xs P], [xs] given by [intro_binders fs], is the + precondition quantified over the values of [fs]: + - [exists xs, (/\_i xs_i = fs_i) /\ P] ([true] for an empty + conjunction); + - [(exists xs, /\_i xs_i = fs_i) `|` P] when [ehoare]. *) +val intro_pre : ehoare:bool -> (EcIdent.t * inv) list -> inv -> inv diff --git a/src/phl/rules/ehoare/ecEHoareExists.ml b/src/phl/rules/ehoare/ecEHoareExists.ml new file mode 100644 index 000000000..d39234cb7 --- /dev/null +++ b/src/phl/rules/ehoare/ecEHoareExists.ml @@ -0,0 +1,113 @@ +(* -------------------------------------------------------------------- *) +open EcFol +open EcAst +open EcEnv +open EcSubst + +open EcCoreGoal +open EcLowGoal +open EcLowPhlGoal + +module PT = EcProofTerm +module TTC = EcProofTyping + +(* -------------------------------------------------------------------- *) +(* Parameter of the ehoare [exists] elimination rules: the maximal number + of binders to eliminate ([None]: all of them, see + [EcPlExists.prenex_exists]). Nothing to resolve: the same record is the + rule argument and the node payload. *) +type ehoare_exists_elim = { + ehxe_bound : int option; +} + +type EcCoreGoal.rule += + | REHoareSExistsElim of ehoare_exists_elim + | REHoareFExistsElim of ehoare_exists_elim + +(* -------------------------------------------------------------------- *) +(* Pure core shared by the rules and their checkers. *) +let ehoareS_exists_elim_subgoals (hs : eHoareS) (n : ehoare_exists_elim) = + let pre = ehs_pr hs in + let bd, inv = EcPlExists.prenex_exists ?bound:n.ehxe_bound pre.inv in + [f_forall bd + (f_eHoareS (snd hs.ehs_m) { pre with inv } hs.ehs_s (ehs_po hs))] + +let ehoareF_exists_elim_subgoals (hf : eHoareF) (n : ehoare_exists_elim) = + let pre = ehf_pr hf in + let bd, inv = EcPlExists.prenex_exists ?bound:n.ehxe_bound pre.inv in + [f_forall bd (f_eHoareF { pre with inv } hf.ehf_f (ehf_po hf))] + +(* -------------------------------------------------------------------- *) +(* Rules (TCB). *) +let t_ehoareS_exists_elim (r : ehoare_exists_elim) (tc : tcenv1) = + let hs = tc1_as_ehoareS tc in + FApi.xrule1 tc (REHoareSExistsElim r) (ehoareS_exists_elim_subgoals hs r) + +let t_ehoareF_exists_elim (r : ehoare_exists_elim) (tc : tcenv1) = + let hf = tc1_as_ehoareF tc in + FApi.xrule1 tc (REHoareFExistsElim r) (ehoareF_exists_elim_subgoals hf r) + +(* -------------------------------------------------------------------- *) +let () = + register_rule_checker + (function + | REHoareSExistsElim n -> + Some (EcPlRecheck.checker_of "ehoareS-exists-elim" pf_as_ehoareS + (fun _hyps hs -> ehoareS_exists_elim_subgoals hs n)) + | REHoareFExistsElim n -> + Some (EcPlRecheck.checker_of "ehoareF-exists-elim" pf_as_ehoareF + (fun _hyps hf -> ehoareF_exists_elim_subgoals hf n)) + | _ -> None) + +(* -------------------------------------------------------------------- *) +(* Derived (no proof-node). *) +let t_ehoare_exists_elim (r : ehoare_exists_elim) (tc : tcenv1) = + match (FApi.tc1_goal tc).f_node with + | FeHoareS _ -> t_ehoareS_exists_elim r tc + | FeHoareF _ -> t_ehoareF_exists_elim r tc + | _ -> tc_error_noXhl ~kinds:hlkinds_Xhl !!tc + +(* The consequence rule, whose premise on the precondition is closed by + [xle_cxr_l] and the values of [fs] as witnesses. + + TEMPORARY: the consequence rule still comes from the not-yet-migrated + [EcPhlConseq]. *) +let t_ehoare_exists_intro (fs : ss_inv list) (tc : tcenv1) = + let hyps = FApi.tc1_hyps tc in + let xs = EcPlExists.intro_binders (List.map (fun f -> Inv_ss f) fs) in + let pre = EcPlExists.intro_pre ~ehoare:true xs (tc1_get_pre tc) in + let m = LDecl.fresh_id hyps "&m" in + let args = List.map (fun f -> PAFormula (ss_inv_rebind f m).inv) fs in + let t_pre = + FApi.t_seq + (t_intros_i [m]) + (FApi.t_seqsub + (EcHiGoal.t_apply_prept + (PT.Prept.uglob EcCoreLib.CI_Xreal.p_xle_cxr_l)) + [FApi.t_seq (t_exists_intro_s args) t_trivial; t_trivial]) in + FApi.t_seqsub + (EcPhlConseq.t_conseq pre (tc1_get_post tc)) + [t_pre; t_trivial; t_id] + tc + +(* -------------------------------------------------------------------- *) +(* Elaboration: the goal is known to be an [eHoareS] or an [eHoareF]. *) +let process_ehoare_exists_intro + ~(elim : bool) (fs : EcParsetree.pformula list) (tc : tcenv1) += + let hyps, concl = FApi.tc1_flat tc in + let penv, m = + match concl.f_node with + | FeHoareF hf -> fst (LDecl.hoareF hf.ehf_m hf.ehf_f hyps), hf.ehf_m + | FeHoareS hs -> LDecl.push_active_ss hs.ehs_m hyps, fst hs.ehs_m + | _ -> tc_error_noXhl ~kinds:hlkinds_Xhl !!tc in + let fs = + List.map + (fun f -> { m; inv = TTC.pf_process_form_opt !!tc penv None f }) + fs in + if elim then + FApi.t_seq + (t_ehoare_exists_intro fs) + (t_ehoare_exists_elim { ehxe_bound = Some (List.length fs) }) + tc + else t_ehoare_exists_intro fs tc diff --git a/src/phl/rules/ehoare/ecEHoareExists.mli b/src/phl/rules/ehoare/ecEHoareExists.mli new file mode 100644 index 000000000..44cbe96a2 --- /dev/null +++ b/src/phl/rules/ehoare/ecEHoareExists.mli @@ -0,0 +1,68 @@ +(* -------------------------------------------------------------------- *) +open EcCoreGoal.FApi +open EcAst + +(* ==================================================================== *) +(* Rules (trusted) *) + +type ehoare_exists_elim = { + ehxe_bound : int option; (* at most that many binders; all if none *) +} + +(* [t_ehoareS_exists_elim { ehxe_bound = b }] — elimination of the + existentials of the precondition: + + forall xs, ehoare [c : P' ==> Q] + -------------------------------- (xs, P') = prenex_b(P) + ehoare [c : P ==> Q] + + where [prenex_b(P)] is [EcPlExists.prenex_exists ?bound:b P]: the + only preconditions with binders to pull are [B `|` F] ([F] if the + boolean [B] holds, [+inf] otherwise), for which [P'] is [B' `|` F], + [B'] being [B] with (at most [b] of) its existential binders [xs] + pulled to the front, freshened. [B] entails [exists xs, B'], so that + [P] is not below the infimum of the [P'] over [xs]; [xs] occur nowhere + else. + When [P] has no binder to pull, [xs] is empty and the premise is the + conclusion. + + Node: [REHoareSExistsElim { ehxe_bound = b }]. + Checker: "ehoareS-exists-elim". *) +val t_ehoareS_exists_elim : ehoare_exists_elim -> backward + +(* [t_ehoareF_exists_elim { ehxe_bound = b }] — same for a procedure: + + forall xs, ehoare [f : P' ==> Q] + -------------------------------- (xs, P') = prenex_b(P) + ehoare [f : P ==> Q] + + Node: [REHoareFExistsElim { ehxe_bound = b }]. + Checker: "ehoareF-exists-elim". *) +val t_ehoareF_exists_elim : ehoare_exists_elim -> backward + +(* ==================================================================== *) +(* Derived tactics *) + +(* [t_ehoare_exists_elim r] — [t_ehoareS_exists_elim r] or + [t_ehoareF_exists_elim r], depending on the goal. *) +val t_ehoare_exists_elim : ehoare_exists_elim -> backward + +(* [t_ehoare_exists_intro fs] — on [ehoare [c : P ==> Q]] (or on a + procedure [f]), [fs] formulas in the memory of the judgement, with + [xs] fresh locals named after [fs] ([EcPlExists.intro_binders]): + 1. the consequence rule (currently [EcPhlConseq.t_conseq]) to + [ehoare [c : (exists xs, /\_i xs_i = fs_i) `|` P ==> Q]]; + 2. its premise on the precondition is closed by [xle_cxr_l] and [fs] + as witnesses, its premise on the postcondition by [trivial]. + Visible goal: the judgement of 1. Emits no node of its own. *) +val t_ehoare_exists_intro : ss_inv list -> backward + +(* ==================================================================== *) +(* Elaboration *) + +(* [exists* fs] / [exlim fs] ([~elim]) on an [eHoareS] or [eHoareF] + goal: [fs] typed in the memory of the judgement; + [t_ehoare_exists_intro], followed for [exlim] by + [t_ehoare_exists_elim] on as many binders as there are formulas. *) +val process_ehoare_exists_intro : + elim:bool -> EcParsetree.pformula list -> backward diff --git a/src/phl/rules/equiv/ecEquivECall.ml b/src/phl/rules/equiv/ecEquivECall.ml new file mode 100644 index 000000000..d3ade9045 --- /dev/null +++ b/src/phl/rules/equiv/ecEquivECall.ml @@ -0,0 +1,183 @@ +(* -------------------------------------------------------------------- *) +open EcUtils +open EcAst +open EcFol +open EcEnv +open EcSubst + +open EcCoreGoal +open EcLowGoal +open EcLowPhlGoal +open EcMatching.Position + +module L = EcLocation +module APT = EcParsetree +module PT = EcProofTerm + +(* -------------------------------------------------------------------- *) +(* The exists rules, quantifying the precondition over the values of the + program variables [pvs] of memories [ml] / [mr] and introducing them as + [ids]. *) +let t_abstract_pvs + ((ml, mr) : memory * memory) + (ids : (EcIdent.t * ty) list) + (pvs : form list) += + FApi.t_seqs [ + EcEquivExists.t_equiv_exists_intro + (List.map (fun inv -> { ml; mr; inv }) pvs); + EcEquivExists.t_equivS_exists_elim + { exe_bound = Some (List.length ids) }; + t_intros_i (List.fst ids); + ] + +(* -------------------------------------------------------------------- *) +(* Derived (no proof-node). *) +let t_equiv_ecall_onesided + (side : APT.side) + ((cttpt, ctt) : proofterm * form) + ((ids, pvs) : (EcIdent.t * ty) list * form list) + (call : EcPlECall.call) + (tc : tcenv1) += + let hyps = FApi.tc1_hyps tc in + let es = tc1_as_equivS tc in + let (ml, _), (mr, _) = es.es_ml, es.es_mr in + let m = APT.sideif side ml mr in + + let fpre, fpost = + match ctt.f_node with + | FhoareF hf -> + assert (POE.is_empty (hf_po hf).hsi_inv); + (ss_inv_rebind (hf_pr hf) m).inv, + (hs_inv_rebind (hf_po hf) m).hsi_inv.main + | FbdHoareF hf -> + (ss_inv_rebind (bhf_pr hf) m).inv, + (ss_inv_rebind (bhf_po hf) m).inv + | _ -> assert false + in + + let mid = + EcEquivCall.equiv_call_onesided_wp + hyps side (ml, mr) (fpre, fpost) call (es_po es).inv in + let mid = EcSubst.subst_form (EcPlECall.restore_pvs ids pvs) mid in + + (* The call rule, as [call{side}] applies it: the one-sided rule takes a + phoare specification. *) + let t_call (tc : tcenv1) = + match ctt.f_node with + | FbdHoareF hf -> + EcEquivCall.(t_equiv_call_onesided_last + { ecallos_side = side; + ecallos_pre = bhf_pr hf; + ecallos_post = bhf_po hf; }) tc + | _ -> tc_error !!tc "call: invalid goal shape" in + + let at = + APT.sideif side + (GapBefore cpos1_last, GapAfter cpos1_last) + (GapAfter cpos1_last, GapBefore cpos1_last) in + + let tc = + EcEquivSeq.t_equiv_seq + { esr_at = at; esr_mid = { ml; mr; inv = mid; }; } tc in + let tc = FApi.t_last (t_abstract_pvs (ml, mr) ids pvs) tc in + let tc = FApi.t_last t_call tc in + + FApi.t_sub [ + t_id; (* the prefixes *) + Apply.t_apply_bwd_hi ~dpe:true cttpt; (* the contract *) + EcPhlAuto.t_auto ?conv:None; (* the skips before the calls *) + ] tc + +(* -------------------------------------------------------------------- *) +(* Derived (no proof-node). *) +let t_equiv_ecall + ((cttpt, ctt) : proofterm * form) + ((ids, pvs) : (EcIdent.t * ty) list * form list) + ((call_l, call_r) : EcPlECall.call * EcPlECall.call) + (tc : tcenv1) += + let hyps = FApi.tc1_hyps tc in + let es = tc1_as_equivS tc in + let (ml, _), (mr, _) = es.es_ml, es.es_mr in + + let ef = destr_equivF ctt in + let fpre, fpost = + (ts_inv_rebind (ef_pr ef) ml mr).inv, + (ts_inv_rebind (ef_po ef) ml mr).inv + in + + let mid = + EcEquivCall.equiv_call_wp + hyps (ml, mr) (fpre, fpost) call_l call_r (es_po es).inv in + let mid = EcSubst.subst_form (EcPlECall.restore_pvs ids pvs) mid in + + let tc = + EcEquivSeq.t_equiv_seq + { esr_at = (GapBefore cpos1_last, GapBefore cpos1_last); + esr_mid = { ml; mr; inv = mid; }; } tc in + let tc = FApi.t_last (t_abstract_pvs (ml, mr) ids pvs) tc in + let tc = + FApi.t_last + EcEquivCall.(t_equiv_call_last + { ecall_pre = ef_pr ef; ecall_post = ef_po ef; }) + tc in + + FApi.t_sub [ + t_id; (* the prefixes *) + Apply.t_apply_bwd_hi ~dpe:true cttpt; (* the contract *) + EcPhlAuto.t_auto ?conv:None; (* the skips before the calls *) + ] tc + +(* -------------------------------------------------------------------- *) +(* Elaboration: the goal is known to be an [equivS]. *) +let process_equiv_ecall + (dir : APT.pdirection) + (oside : APT.oside) + (pterm : APT.pecall) + (tc : tcenv1) += + if dir <> `Backward then + tc_error !!tc "unsupported direction for ecall on an equiv. goal"; + + let (ctt_path, _, _) = pterm in + let hyps = FApi.tc1_hyps tc in + let env = LDecl.toenv hyps in + let es = tc1_as_equivS tc in + let (ml, _), (mr, _) = es.es_ml, es.es_mr in + + let cttpt, _ = + EcPlECall.process_contract + !!tc (LDecl.push_active_ts es.es_ml es.es_mr hyps) hyps pterm in + + let pvs = PT.collect_pvars_from_pt cttpt in + let ids, pvs, subst = EcPlECall.abstract_pvs hyps [ml; mr] pvs in + + let cttpt = EcPlECall.subst_pt_args env subst cttpt in + let cttpt, ctt = LowApply.check `Elim cttpt (`Hyps (hyps, !!tc)) in + + let ctt = + EcReduction.h_red_opt EcReduction.full_red hyps ctt + |> odfl ctt in + + let calls = + match oside with + | None -> + let call_l, _ = pf_last_call !!tc es.es_sl in + let call_r, _ = pf_last_call !!tc es.es_sr in + `Double (call_l, call_r) + | Some side -> + let call, _ = pf_last_call !!tc (APT.sideif side es.es_sl es.es_sr) in + `Single call + in + + EcPlECall.check_contract_type + ~phoare:true ~loc:(L.loc ctt_path) ~name:(L.unloc ctt_path) + !!tc hyps calls ctt; + + match calls with + | `Single call -> + t_equiv_ecall_onesided (oget oside) (cttpt, ctt) (ids, pvs) call tc + | `Double calls -> + t_equiv_ecall (cttpt, ctt) (ids, pvs) calls tc diff --git a/src/phl/rules/equiv/ecEquivECall.mli b/src/phl/rules/equiv/ecEquivECall.mli new file mode 100644 index 000000000..4c824f9e7 --- /dev/null +++ b/src/phl/rules/equiv/ecEquivECall.mli @@ -0,0 +1,79 @@ +(* -------------------------------------------------------------------- *) +open EcParsetree +open EcCoreGoal +open EcCoreGoal.FApi +open EcAst + +(* ==================================================================== *) +(* Rules (trusted) *) + +(* None: [ecall] is derived from the [seq], [call], [exists] and + consequence rules. *) + +(* ==================================================================== *) +(* Derived tactics *) + +(* In both forms, [(pt, C)] is the contract, a proof term [pt] of the + specification [C] of the called procedure(s), whose arguments may + mention program variables [vs] of the goal's memories, abstracted by + the caller ([EcPlECall.abstract_pvs]) as fresh locals [ids]; [pt] and + [C] are already stated over [ids] ([X(vs)] below is [X] with [ids] + replaced by [vs]). "Abstracting [vs]" is, on + [equiv [_ ~ _ : P ==> _]]: [EcEquivExists.t_equiv_exists_intro vs], + [EcEquivExists.t_equivS_exists_elim], and the introduction of [ids]. + Neither form emits a node of its own. *) + +(* [t_equiv_ecall_onesided side (pt, C) (ids, vs) call] ([ecall{1}], + [side = `Left]; symmetrically for [`Right]) — on + [equiv [c; lv <@ f(a) ~ c' : P ==> Q]], with + [C = phoare [f : Pf ==> Qf] = 1%r]: + 1. [EcEquivSeq.t_equiv_seq] before the call and at the end of [c'], + with intermediate relation [W(vs)], [W] the weakest precondition of + the one-sided call for [C] ([EcEquivCall.equiv_call_onesided_wp]); + giving + (a) equiv [c ~ c' : P ==> W(vs)] — left open, + (b) equiv [lv <@ f(a) ~ skip : W(vs) ==> Q]; + 2. on (b): abstracting [vs], then + [EcEquivCall.t_equiv_call_onesided_last] with [C]: its + specification premise is closed by [pt], the premise on the empty + prefixes by [EcPhlAuto.t_auto] (left open if it does not). + Fails at 2. when [C] is a [hoare] specification (the one-sided call + rule takes a phoare one), as [call{1}] does. Visible goals: (a), and + the premise of 2. if not closed. *) +val t_equiv_ecall_onesided : + side + -> proofterm * form + -> (EcIdent.t * ty) list * form list + -> EcPlECall.call + -> backward + +(* [t_equiv_ecall (pt, C) (ids, vs) (callL, callR)] ([ecall]) — on + [equiv [c; lvL <@ fL(aL) ~ c'; lvR <@ fR(aR) : P ==> Q]], with + [C = equiv [fL ~ fR : Pf ==> Qf]]: + 1. [EcEquivSeq.t_equiv_seq] before both calls, with intermediate + relation [W(vs)], [W] the weakest precondition of the calls for [C] + ([EcEquivCall.equiv_call_wp]); giving + (a) equiv [c ~ c' : P ==> W(vs)] — left open, + (b) equiv [lvL <@ fL(aL) ~ lvR <@ fR(aR) : W(vs) ==> Q]; + 2. on (b): abstracting [vs], then [EcEquivCall.t_equiv_call_last] + with [C]: its specification premise is closed by [pt], the premise + on the empty prefixes by [EcPhlAuto.t_auto] (left open if it does + not). + Visible goals: (a), and the premise of 2. if not closed. *) +val t_equiv_ecall : + proofterm * form + -> (EcIdent.t * ty) list * form list + -> EcPlECall.call * EcPlECall.call + -> backward + +(* ==================================================================== *) +(* Elaboration *) + +(* [ecall (c args)] / [ecall{i} (c args)] on an [equivS] goal: no + [->>]; the contract typed with the goal's memories, applied to as many + holes as possible, its program variables abstracted + ([EcPlECall.abstract_pvs]); it must be an [equiv] specification of the + procedures called last ([hoare] or [phoare] for the procedure called + last on side [i]; [EcPlECall.check_contract_type]). Applies + [t_equiv_ecall] / [t_equiv_ecall_onesided]. *) +val process_equiv_ecall : pdirection -> oside -> pecall -> backward diff --git a/src/phl/rules/equiv/ecEquivExists.ml b/src/phl/rules/equiv/ecEquivExists.ml new file mode 100644 index 000000000..d3d556518 --- /dev/null +++ b/src/phl/rules/equiv/ecEquivExists.ml @@ -0,0 +1,115 @@ +(* -------------------------------------------------------------------- *) +open EcUtils +open EcFol +open EcAst +open EcEnv +open EcSubst + +open EcCoreGoal +open EcLowGoal +open EcLowPhlGoal + +module TTC = EcProofTyping + +(* -------------------------------------------------------------------- *) +(* Parameter of the equiv [exists] elimination rules: the maximal number + of binders to eliminate ([None]: all of them, see + [EcPlExists.prenex_exists]). Nothing to resolve: the same record is the + rule argument and the node payload. *) +type equiv_exists_elim = { + exe_bound : int option; +} + +type EcCoreGoal.rule += + | REquivSExistsElim of equiv_exists_elim + | REquivFExistsElim of equiv_exists_elim + +(* -------------------------------------------------------------------- *) +(* Pure core shared by the rules and their checkers. *) +let equivS_exists_elim_subgoals (es : equivS) (n : equiv_exists_elim) = + let pre = es_pr es in + let bd, inv = EcPlExists.prenex_exists ?bound:n.exe_bound pre.inv in + [f_forall bd + (f_equivS (snd es.es_ml) (snd es.es_mr) { pre with inv } + es.es_sl es.es_sr (es_po es))] + +let equivF_exists_elim_subgoals (ef : equivF) (n : equiv_exists_elim) = + let pre = ef_pr ef in + let bd, inv = EcPlExists.prenex_exists ?bound:n.exe_bound pre.inv in + [f_forall bd (f_equivF { pre with inv } ef.ef_fl ef.ef_fr (ef_po ef))] + +(* -------------------------------------------------------------------- *) +(* Rules (TCB). *) +let t_equivS_exists_elim (r : equiv_exists_elim) (tc : tcenv1) = + let es = tc1_as_equivS tc in + FApi.xrule1 tc (REquivSExistsElim r) (equivS_exists_elim_subgoals es r) + +let t_equivF_exists_elim (r : equiv_exists_elim) (tc : tcenv1) = + let ef = tc1_as_equivF tc in + FApi.xrule1 tc (REquivFExistsElim r) (equivF_exists_elim_subgoals ef r) + +(* -------------------------------------------------------------------- *) +let () = + register_rule_checker + (function + | REquivSExistsElim n -> + Some (EcPlRecheck.checker_of "equivS-exists-elim" pf_as_equivS + (fun _hyps es -> equivS_exists_elim_subgoals es n)) + | REquivFExistsElim n -> + Some (EcPlRecheck.checker_of "equivF-exists-elim" pf_as_equivF + (fun _hyps ef -> equivF_exists_elim_subgoals ef n)) + | _ -> None) + +(* -------------------------------------------------------------------- *) +(* Derived (no proof-node). *) +let t_equiv_exists_elim (r : equiv_exists_elim) (tc : tcenv1) = + match (FApi.tc1_goal tc).f_node with + | FequivS _ -> t_equivS_exists_elim r tc + | FequivF _ -> t_equivF_exists_elim r tc + | _ -> tc_error_noXhl ~kinds:hlkinds_Xhl !!tc + +(* The consequence rule, whose premise on the precondition is closed by + giving the values of [fs] as witnesses. + + TEMPORARY: the consequence rule still comes from the not-yet-migrated + [EcPhlConseq]. *) +let t_equiv_exists_intro (fs : ts_inv list) (tc : tcenv1) = + let hyps = FApi.tc1_hyps tc in + let xs = EcPlExists.intro_binders (List.map (fun f -> Inv_ts f) fs) in + let pre = EcPlExists.intro_pre ~ehoare:false xs (tc1_get_pre tc) in + let h = LDecl.fresh_id hyps "h" in + let ml, mr = as_seq2 (LDecl.fresh_ids hyps ["&ml"; "&mr"]) in + let args = List.map (fun f -> PAFormula (ts_inv_rebind f ml mr).inv) fs in + let t_pre = + FApi.t_seqs + [t_intros_i [ml; mr; h]; t_exists_intro_s args; t_apply_hyp h] in + FApi.t_seqsub + (EcPhlConseq.t_conseq pre (tc1_get_post tc)) + [t_pre; t_trivial; t_id] + tc + +(* -------------------------------------------------------------------- *) +(* Elaboration: the goal is known to be an [equivS] or an [equivF]. *) +let process_equiv_exists_intro + ~(elim : bool) (fs : EcParsetree.pformula list) (tc : tcenv1) += + let hyps, concl = FApi.tc1_flat tc in + let penv, (ml, mr) = + match concl.f_node with + | FequivF ef -> + fst (LDecl.equivF ef.ef_ml ef.ef_mr ef.ef_fl ef.ef_fr hyps), + (ef.ef_ml, ef.ef_mr) + | FequivS es -> + LDecl.push_all [es.es_ml; es.es_mr] hyps, + (fst es.es_ml, fst es.es_mr) + | _ -> tc_error_noXhl ~kinds:hlkinds_Xhl !!tc in + let fs = + List.map + (fun f -> { ml; mr; inv = TTC.pf_process_form_opt !!tc penv None f }) + fs in + if elim then + FApi.t_seq + (t_equiv_exists_intro fs) + (t_equiv_exists_elim { exe_bound = Some (List.length fs) }) + tc + else t_equiv_exists_intro fs tc diff --git a/src/phl/rules/equiv/ecEquivExists.mli b/src/phl/rules/equiv/ecEquivExists.mli new file mode 100644 index 000000000..d5dea60cd --- /dev/null +++ b/src/phl/rules/equiv/ecEquivExists.mli @@ -0,0 +1,64 @@ +(* -------------------------------------------------------------------- *) +open EcCoreGoal.FApi +open EcAst + +(* ==================================================================== *) +(* Rules (trusted) *) + +type equiv_exists_elim = { + exe_bound : int option; (* at most that many binders; all if none *) +} + +(* [t_equivS_exists_elim { exe_bound = b }] — elimination of the + existentials of the precondition: + + forall xs, equiv [c1 ~ c2 : P' ==> Q] + ------------------------------------- (xs, P') = prenex_b(P) + equiv [c1 ~ c2 : P ==> Q] + + where [prenex_b(P)] is [EcPlExists.prenex_exists ?bound:b P]: [P] + with (at most [b] of) its existential binders [xs] pulled to the + front, freshened; [P] entails [exists xs, P'], and [xs] occur nowhere + else. When [P] has no binder to pull, [xs] is empty and the premise is + the conclusion. + + Node: [REquivSExistsElim { exe_bound = b }]. + Checker: "equivS-exists-elim". *) +val t_equivS_exists_elim : equiv_exists_elim -> backward + +(* [t_equivF_exists_elim { exe_bound = b }] — same for procedures: + + forall xs, equiv [f1 ~ f2 : P' ==> Q] + ------------------------------------- (xs, P') = prenex_b(P) + equiv [f1 ~ f2 : P ==> Q] + + Node: [REquivFExistsElim { exe_bound = b }]. + Checker: "equivF-exists-elim". *) +val t_equivF_exists_elim : equiv_exists_elim -> backward + +(* ==================================================================== *) +(* Derived tactics *) + +(* [t_equiv_exists_elim r] — [t_equivS_exists_elim r] or + [t_equivF_exists_elim r], depending on the goal. *) +val t_equiv_exists_elim : equiv_exists_elim -> backward + +(* [t_equiv_exists_intro fs] — on [equiv [c1 ~ c2 : P ==> Q]] (or on + procedures [f1 ~ f2]), [fs] formulas in the memories of the judgement, + with [xs] fresh locals named after [fs] ([EcPlExists.intro_binders]): + 1. the consequence rule (currently [EcPhlConseq.t_conseq]) to + [equiv [c1 ~ c2 : exists xs, (/\_i xs_i = fs_i) /\ P ==> Q]]; + 2. its premise [forall &1 &2, P => exists xs, ...] is closed by giving + [fs] as witnesses, its premise [Q => Q] by [trivial]. + Visible goal: the judgement of 1. Emits no node of its own. *) +val t_equiv_exists_intro : ts_inv list -> backward + +(* ==================================================================== *) +(* Elaboration *) + +(* [exists* fs] / [exlim fs] ([~elim]) on an [equivS] or [equivF] goal: + [fs] typed in the memories of the judgement; [t_equiv_exists_intro], + followed for [exlim] by [t_equiv_exists_elim] on as many binders as + there are formulas. *) +val process_equiv_exists_intro : + elim:bool -> EcParsetree.pformula list -> backward diff --git a/src/phl/rules/hoare/ecHoareECall.ml b/src/phl/rules/hoare/ecHoareECall.ml new file mode 100644 index 000000000..5a902a468 --- /dev/null +++ b/src/phl/rules/hoare/ecHoareECall.ml @@ -0,0 +1,197 @@ +(* -------------------------------------------------------------------- *) +open EcUtils +open EcAst +open EcFol +open EcEnv +open EcSubst + +open EcCoreGoal +open EcLowGoal +open EcLowPhlGoal +open EcMatching.Position + +module L = EcLocation +module APT = EcParsetree +module PT = EcProofTerm + +(* -------------------------------------------------------------------- *) +(* The call rule, as [call] applies it, for the contract [ctt]. *) +let t_call (ctt : form) = + let hf = destr_hoareF ctt in + EcHoareCall.(t_hoare_call_last + { hcall_pre = hf_pr hf; hcall_post = hf_po hf; }) + +(* The exists rules, quantifying the precondition over the values of the + program variables [pvs] of memory [m] and introducing them as [ids]. *) +let t_abstract_pvs + (m : memory) + (ids : (EcIdent.t * ty) list) + (pvs : form list) += + FApi.t_seqs [ + EcHoareExists.t_hoare_exists_intro + (List.map (fun inv -> { m; inv }) pvs); + EcHoareExists.t_hoareS_exists_elim + { hxe_bound = Some (List.length ids) }; + t_intros_i (List.fst ids); + ] + +(* -------------------------------------------------------------------- *) +(* Derived (no proof-node). *) +let t_hoare_ecall_fwd ((cttpt, ctt) : proofterm * form) (tc : tcenv1) = + let hyps = FApi.tc1_hyps tc in + let env = LDecl.toenv hyps in + let hs = tc1_as_hoareS tc in + let m = fst hs.hs_m in + let (lvalue, funname, _), _ = pf_first_call !!tc hs.hs_s in + + let pvs = PT.collect_pvars_from_pt cttpt in + let ids, pvs, subst = EcPlECall.abstract_pvs hyps [m] pvs in + + let tc = t_abstract_pvs m ids pvs tc in + + let cttpt = PT.subst_pv_pt env subst cttpt in + let ctt = EcPV.PVM.subst env subst ctt in + + let ctt = + EcReduction.h_red_opt EcReduction.full_red hyps ctt + |> odfl ctt in + + (* The postcondition of the contract, its result assigned to [lvalue] + (or its conjuncts mentioning [res] dropped when there is none). *) + let seqf = + let inv = destr_hoareF ctt in + let _ = assert (POE.is_empty (hf_po inv).hsi_inv) in + let inv = POE.lower (hf_po inv) in + let inv = ss_inv_rebind inv m in + + match lvalue with + | None -> + let not_contains_res (f : form) = + let pvs = EcPV.form_read env EcPV.PMVS.empty f in + let pvs = EcIdent.Mid.find_def EcPV.PV.empty m pvs in + not (EcPV.PV.mem_pv env EcTypes.pv_res pvs) in + map_ss_inv1 + (fun f -> filter_topand_form not_contains_res f |> odfl f_true) + inv + + | Some lvalue -> + let lv = + List.map + (fun (pv, ty) -> (f_pvar pv ty inv.m).inv) + (EcModules.lv_to_ty_list lvalue) in + let sres = + EcPV.PVM.add + env EcTypes.pv_res inv.m + (f_tuple lv) EcPV.PVM.empty in + + { inv = EcPV.PVM.subst env sres inv.inv; m = inv.m; } in + + (* The conjuncts of the precondition independent of the variables + written by the call. *) + let seqf_frame = + let wr = lvalue |> omap (EcPV.lp_write env) |> odfl EcPV.PV.empty in + let wr = EcPV.f_write_r env wr funname in + let inv = + filter_topand_form + (fun f -> + let pvs = EcPV.form_read env EcPV.PMVS.empty f in + let pvs = EcIdent.Mid.find_def EcPV.PV.empty m pvs in + EcPV.PV.indep env wr pvs) + (hs_pr hs).inv in + { inv = odfl f_true inv; m = (hs_pr hs).m; } in + + let tc = + FApi.t_first + (EcHoareSeq.t_hoare_seq + { hsr_at = GapAfter cpos1_first; + hsr_mid = map_ss_inv2 f_and seqf seqf_frame; }) + tc in + + let tc = FApi.t_first EcHoareSplit.t_hoare_split tc in + (* TEMPORARY: the automated framed consequence still comes from the + not-yet-migrated [EcPhlConseq] (its framing is the frame rule). *) + let tc = + FApi.t_first (EcPhlConseq.t_conseqauto ~delta:false ?tsolve:None) tc in + let tc = FApi.t_first EcHoareTrue.t_hoare_true tc in + + let tc = FApi.t_first (t_call ctt) tc in + let tc = FApi.t_sub [ + Apply.t_apply_bwd_hi ~dpe:true cttpt; + EcHoareSkip.t_hoare_skip; + t_id + ] tc in + + FApi.t_firsts (t_generalize_hyps ~clear:`Yes (List.fst ids)) 2 tc + +(* -------------------------------------------------------------------- *) +(* Derived (no proof-node). *) +let t_hoare_ecall_bwd ((cttpt, _) : proofterm * form) (tc : tcenv1) = + let hyps = FApi.tc1_hyps tc in + let env = LDecl.toenv hyps in + let hs = tc1_as_hoareS tc in + let m = fst hs.hs_m in + let call, _ = pf_last_call !!tc hs.hs_s in + + let pvs = PT.collect_pvars_from_pt cttpt in + let ids, pvs, subst = EcPlECall.abstract_pvs hyps [m] pvs in + + let cttpt = EcPlECall.subst_pt_args env subst cttpt in + let cttpt, ctt = LowApply.check `Elim cttpt (`Hyps (hyps, !!tc)) in + + let ctt = + EcReduction.h_red_opt EcReduction.full_red hyps ctt + |> odfl ctt in + + let fpre, fpost = + let hf = destr_hoareF ctt in + (ss_inv_rebind (hf_pr hf) m).inv, (hs_inv_rebind (hf_po hf) m).hsi_inv + in + + let mid = + EcHoareCall.hoare_call_wp hyps m (fpre, fpost) call (hs_po hs).hsi_inv in + let mid = EcSubst.subst_form (EcPlECall.restore_pvs ids pvs) mid in + + let tc = + EcHoareSeq.t_hoare_seq + { hsr_at = GapBefore cpos1_last; hsr_mid = { m; inv = mid; }; } tc in + let tc = FApi.t_last (t_abstract_pvs m ids pvs) tc in + let tc = FApi.t_last (t_call ctt) tc in + + FApi.t_sub [ + t_id; (* the prefix *) + Apply.t_apply_bwd_hi ~dpe:true cttpt; (* the contract *) + EcPhlAuto.t_auto ?conv:None; (* the skip before the call *) + ] tc + +(* -------------------------------------------------------------------- *) +(* Elaboration: the goal is known to be a [hoareS]. *) +let process_hoare_ecall + (dir : APT.pdirection) + (oside : APT.oside) + (pterm : APT.pecall) + (tc : tcenv1) += + if Option.is_some oside then + tc_error !!tc "cannot provide a side for Hoare goals"; + + let (ctt_path, _, _) = pterm in + let hyps = FApi.tc1_hyps tc in + let hs = tc1_as_hoareS tc in + + let contract = + EcPlECall.process_contract + !!tc (LDecl.push_active_ss hs.hs_m hyps) hyps pterm in + + let call, _ = + match dir with + | `Forward -> pf_first_call !!tc hs.hs_s + | `Backward -> pf_last_call !!tc hs.hs_s in + + EcPlECall.check_contract_type + ~noexn:(dir <> `Backward) ~loc:(L.loc ctt_path) ~name:(L.unloc ctt_path) + !!tc hyps (`Single call) (snd contract); + + match dir with + | `Forward -> t_hoare_ecall_fwd contract tc + | `Backward -> t_hoare_ecall_bwd contract tc diff --git a/src/phl/rules/hoare/ecHoareECall.mli b/src/phl/rules/hoare/ecHoareECall.mli new file mode 100644 index 000000000..fd306d669 --- /dev/null +++ b/src/phl/rules/hoare/ecHoareECall.mli @@ -0,0 +1,80 @@ +(* -------------------------------------------------------------------- *) +open EcParsetree +open EcCoreGoal +open EcCoreGoal.FApi +open EcAst + +(* ==================================================================== *) +(* Rules (trusted) *) + +(* None: [ecall] is derived from the [seq], [call], [exists] and + consequence rules. *) + +(* ==================================================================== *) +(* Derived tactics *) + +(* In both forms, [(pt, C)] is the contract: a proof term [pt] of + [C = hoare [f : Pf ==> Qf]], the specification of the called procedure + [f], whose arguments may mention program variables [vs] of the goal's + memory. These are abstracted: [ids] are fresh locals for [vs], and + [X(ids)] is [X] with [vs] replaced by [ids]. "Abstracting [vs]" below + is, on a judgement [J [_ : P ==> _]]: + - [EcHoareExists.t_hoare_exists_intro vs], to + [J [_ : exists xs, xs = vs /\ P ==> _]], + - [EcHoareExists.t_hoareS_exists_elim], to + [forall xs, J [_ : xs = vs /\ P ==> _]], + - and the introduction of [xs] as [ids]. + Neither form emits a node of its own. *) + +(* [t_hoare_ecall_fwd (pt, C)] ([ecall ->>]) — on + [hoare [lv <@ f(a); s : P ==> Q]], [C] with no exceptional + postcondition: + 1. abstracting [vs], giving [hoare [lv <@ f(a); s : P1 ==> Q]], + [P1 := ids = vs /\ P]; + 2. [EcHoareSeq.t_hoare_seq] after the call, with intermediate + assertion [S /\ F], where [S] is [Qf(ids)] with [res] replaced by + [lv] (with its conjuncts mentioning [res] dropped when there is no + [lv]), and [F] the conjunction of the top-level conjuncts of [P] + that do not read the variables written by the call (the + precondition is framed); giving + (a) hoare [lv <@ f(a) : P1 ==> S /\ F], + (b) hoare [s : S /\ F ==> Q]; + 3. on (a): [EcHoareSplit.t_hoare_split], then + [EcPhlConseq.t_conseqauto] (the frame rule + [EcHoareFrame.t_hoareS_frame], its condition closed automatically) + and [EcHoareTrue.t_hoare_true] close + [hoare [lv <@ f(a) : P1 ==> F]]; + 4. [EcHoareCall.t_hoare_call_last] with [C(ids)] on + [hoare [lv <@ f(a) : P1 ==> S]], whose specification premise is + closed by [pt(ids)], and the premise [hoare [skip : P1 ==> W]] (W + the weakest precondition of the call) is reduced by + [EcHoareSkip.t_hoare_skip] to + (c) forall &m, P1 => W; + 5. [ids] generalized back in (c) and (b). + Visible goals: [forall ids, (c)], then [forall ids, (b)]. *) +val t_hoare_ecall_fwd : proofterm * form -> backward + +(* [t_hoare_ecall_bwd (pt, C)] ([ecall]) — on + [hoare [c; lv <@ f(a) : P ==> Q | E]]: + 1. [EcHoareSeq.t_hoare_seq] before the call, with intermediate + assertion [W(vs)], [W(ids)] being the weakest precondition of the + call for [C(ids)] ([EcHoareCall.hoare_call_wp]); giving + (a) hoare [c : P ==> W(vs) | E] — left open, + (b) hoare [lv <@ f(a) : W(vs) ==> Q | E]; + 2. on (b): abstracting [vs], then [EcHoareCall.t_hoare_call_last] + with [C(ids)]: its specification premise is closed by [pt(ids)], + the premise [hoare [skip : ids = vs /\ W(vs) ==> W(ids)]] by + [EcPhlAuto.t_auto] (left open if it does not). + Visible goals: (a), and the premise of 2. if not closed. *) +val t_hoare_ecall_bwd : proofterm * form -> backward + +(* ==================================================================== *) +(* Elaboration *) + +(* [ecall ->> (c args)] / [ecall (c args)] on a [hoareS] goal: no side; + the contract typed with the goal's memory, applied to as many holes as + possible; it must be a [hoare] specification of the procedure called + first ([->>]) / last, with no exceptional postcondition for [->>] + ([EcPlECall.check_contract_type]). Applies [t_hoare_ecall_fwd] / + [t_hoare_ecall_bwd]. *) +val process_hoare_ecall : pdirection -> oside -> pecall -> backward diff --git a/src/phl/rules/hoare/ecHoareExists.ml b/src/phl/rules/hoare/ecHoareExists.ml new file mode 100644 index 000000000..e886a08a0 --- /dev/null +++ b/src/phl/rules/hoare/ecHoareExists.ml @@ -0,0 +1,107 @@ +(* -------------------------------------------------------------------- *) +open EcFol +open EcAst +open EcEnv +open EcSubst + +open EcCoreGoal +open EcLowGoal +open EcLowPhlGoal + +module TTC = EcProofTyping + +(* -------------------------------------------------------------------- *) +(* Parameter of the hoare [exists] elimination rules: the maximal number + of binders to eliminate ([None]: all of them, see + [EcPlExists.prenex_exists]). Nothing to resolve: the same record is the + rule argument and the node payload. *) +type hoare_exists_elim = { + hxe_bound : int option; +} + +type EcCoreGoal.rule += + | RHoareSExistsElim of hoare_exists_elim + | RHoareFExistsElim of hoare_exists_elim + +(* -------------------------------------------------------------------- *) +(* Pure core shared by the rules and their checkers. *) +let hoareS_exists_elim_subgoals (hs : sHoareS) (n : hoare_exists_elim) = + let pre = hs_pr hs in + let bd, inv = EcPlExists.prenex_exists ?bound:n.hxe_bound pre.inv in + [f_forall bd (f_hoareS (snd hs.hs_m) { pre with inv } hs.hs_s (hs_po hs))] + +let hoareF_exists_elim_subgoals (hf : sHoareF) (n : hoare_exists_elim) = + let pre = hf_pr hf in + let bd, inv = EcPlExists.prenex_exists ?bound:n.hxe_bound pre.inv in + [f_forall bd (f_hoareF { pre with inv } hf.hf_f (hf_po hf))] + +(* -------------------------------------------------------------------- *) +(* Rules (TCB). *) +let t_hoareS_exists_elim (r : hoare_exists_elim) (tc : tcenv1) = + let hs = tc1_as_hoareS tc in + FApi.xrule1 tc (RHoareSExistsElim r) (hoareS_exists_elim_subgoals hs r) + +let t_hoareF_exists_elim (r : hoare_exists_elim) (tc : tcenv1) = + let hf = tc1_as_hoareF tc in + FApi.xrule1 tc (RHoareFExistsElim r) (hoareF_exists_elim_subgoals hf r) + +(* -------------------------------------------------------------------- *) +let () = + register_rule_checker + (function + | RHoareSExistsElim n -> + Some (EcPlRecheck.checker_of "hoareS-exists-elim" pf_as_hoareS + (fun _hyps hs -> hoareS_exists_elim_subgoals hs n)) + | RHoareFExistsElim n -> + Some (EcPlRecheck.checker_of "hoareF-exists-elim" pf_as_hoareF + (fun _hyps hf -> hoareF_exists_elim_subgoals hf n)) + | _ -> None) + +(* -------------------------------------------------------------------- *) +(* Derived (no proof-node). *) +let t_hoare_exists_elim (r : hoare_exists_elim) (tc : tcenv1) = + match (FApi.tc1_goal tc).f_node with + | FhoareS _ -> t_hoareS_exists_elim r tc + | FhoareF _ -> t_hoareF_exists_elim r tc + | _ -> tc_error_noXhl ~kinds:hlkinds_Xhl !!tc + +(* The consequence rule, whose premise on the precondition is closed by + giving the values of [fs] as witnesses. + + TEMPORARY: the consequence rule still comes from the not-yet-migrated + [EcPhlConseq]. *) +let t_hoare_exists_intro (fs : ss_inv list) (tc : tcenv1) = + let hyps = FApi.tc1_hyps tc in + let xs = EcPlExists.intro_binders (List.map (fun f -> Inv_ss f) fs) in + let pre = EcPlExists.intro_pre ~ehoare:false xs (tc1_get_pre tc) in + let h = LDecl.fresh_id hyps "h" in + let m = LDecl.fresh_id hyps "&m" in + let args = List.map (fun f -> PAFormula (ss_inv_rebind f m).inv) fs in + let t_pre = + FApi.t_seqs [t_intros_i [m; h]; t_exists_intro_s args; t_apply_hyp h] in + FApi.t_seqsub + (EcPhlConseq.t_conseq pre (tc1_get_post tc)) + [t_pre; t_trivial; t_id] + tc + +(* -------------------------------------------------------------------- *) +(* Elaboration: the goal is known to be a [hoareS] or a [hoareF]. *) +let process_hoare_exists_intro + ~(elim : bool) (fs : EcParsetree.pformula list) (tc : tcenv1) += + let hyps, concl = FApi.tc1_flat tc in + let penv, m = + match concl.f_node with + | FhoareF hf -> fst (LDecl.hoareF hf.hf_m hf.hf_f hyps), hf.hf_m + | FhoareS hs -> LDecl.push_active_ss hs.hs_m hyps, fst hs.hs_m + | _ -> tc_error_noXhl ~kinds:hlkinds_Xhl !!tc in + let fs = + List.map + (fun f -> { m; inv = TTC.pf_process_form_opt !!tc penv None f }) + fs in + if elim then + FApi.t_seq + (t_hoare_exists_intro fs) + (t_hoare_exists_elim { hxe_bound = Some (List.length fs) }) + tc + else t_hoare_exists_intro fs tc diff --git a/src/phl/rules/hoare/ecHoareExists.mli b/src/phl/rules/hoare/ecHoareExists.mli new file mode 100644 index 000000000..b25512a1f --- /dev/null +++ b/src/phl/rules/hoare/ecHoareExists.mli @@ -0,0 +1,64 @@ +(* -------------------------------------------------------------------- *) +open EcCoreGoal.FApi +open EcAst + +(* ==================================================================== *) +(* Rules (trusted) *) + +type hoare_exists_elim = { + hxe_bound : int option; (* at most that many binders; all if none *) +} + +(* [t_hoareS_exists_elim { hxe_bound = b }] — elimination of the + existentials of the precondition: + + forall xs, hoare [c : P' ==> Q | E] + ----------------------------------- (xs, P') = prenex_b(P) + hoare [c : P ==> Q | E] + + where [prenex_b(P)] is [EcPlExists.prenex_exists ?bound:b P]: [P] + with (at most [b] of) its existential binders [xs] pulled to the + front, freshened; [P] entails [exists xs, P'], and [xs] occur nowhere + else. When [P] has no binder to pull, [xs] is empty and the premise is + the conclusion. + + Node: [RHoareSExistsElim { hxe_bound = b }]. + Checker: "hoareS-exists-elim". *) +val t_hoareS_exists_elim : hoare_exists_elim -> backward + +(* [t_hoareF_exists_elim { hxe_bound = b }] — same for a procedure: + + forall xs, hoare [f : P' ==> Q | E] + ----------------------------------- (xs, P') = prenex_b(P) + hoare [f : P ==> Q | E] + + Node: [RHoareFExistsElim { hxe_bound = b }]. + Checker: "hoareF-exists-elim". *) +val t_hoareF_exists_elim : hoare_exists_elim -> backward + +(* ==================================================================== *) +(* Derived tactics *) + +(* [t_hoare_exists_elim r] — [t_hoareS_exists_elim r] or + [t_hoareF_exists_elim r], depending on the goal. *) +val t_hoare_exists_elim : hoare_exists_elim -> backward + +(* [t_hoare_exists_intro fs] — on [hoare [c : P ==> Q | E]] (or on a + procedure [f]), [fs] formulas in the memory of the judgement, with + [xs] fresh locals named after [fs] ([EcPlExists.intro_binders]): + 1. the consequence rule (currently [EcPhlConseq.t_conseq]) to + [hoare [c : exists xs, (/\_i xs_i = fs_i) /\ P ==> Q | E]]; + 2. its premise [forall &m, P => exists xs, ...] is closed by giving + [fs] as witnesses, its premise [Q => Q] by [trivial]. + Visible goal: the judgement of 1. Emits no node of its own. *) +val t_hoare_exists_intro : ss_inv list -> backward + +(* ==================================================================== *) +(* Elaboration *) + +(* [exists* fs] / [exlim fs] ([~elim]) on a [hoareS] or [hoareF] goal: + [fs] typed in the memory of the judgement; [t_hoare_exists_intro], + followed for [exlim] by [t_hoare_exists_elim] on as many binders as + there are formulas. *) +val process_hoare_exists_intro : + elim:bool -> EcParsetree.pformula list -> backward diff --git a/tests/exists-ecall.ec b/tests/exists-ecall.ec new file mode 100644 index 000000000..8adb9c728 --- /dev/null +++ b/tests/exists-ecall.ec @@ -0,0 +1,356 @@ +(* The existential rules on program-logic judgements: `elim*` + (eliminating the existentials of the precondition), `exists*` and + `exlim` (quantifying the precondition over the values of formulas), in + every logic, on statements and on procedures; then `ecall` (hoare + forward and backward, phoare backward, equiv on both sides and on one + side); and the error paths. Each tactic is a separate sentence, so that + the goals it leaves can be compared across builds. *) +require import AllCore Xreal. + +module M = { + var g : int + + proc f(x : int) : int = { + return x + 1; + } + + proc h(x : int) : int = { + x <@ f(x); + x <- x + M.g; + return x; + } + + proc k(x : int) : int = { + var y : int; + y <- x; + x <@ f(x); + return x + y; + } + + proc u() : unit = { + M.g <- M.g + 1; + } +}. + +(* ==================================================================== *) +(* elim* *) + +(* -------------------------------------------------------------------- *) +(* hoare *) +lemma L_elim_hoareF : hoare [M.f : exists y, x = y /\ 0 <= y ==> 0 < res]. +proof. +elim*. +move=> y. +proc. +by auto=> /#. +qed. + +(* binders under a conjunction, all eliminated *) +lemma L_elim_hoareS : + hoare [M.f : (exists y, x = y /\ 0 <= y) /\ (exists z, z = M.g) ==> 0 < res]. +proof. +proc. +elim*. +move=> y z. +by auto=> /#. +qed. + +(* no existential: the goal is unchanged *) +lemma L_elim_hoare_none : hoare [M.f : 0 <= x ==> 0 < res]. +proof. +proc. +elim*. +by auto=> /#. +qed. + +(* -------------------------------------------------------------------- *) +(* ehoare *) +lemma L_elim_ehoareF : + ehoare [M.u : (exists y, M.g = y) `|` (M.g + 1)%xr ==> M.g%xr]. +proof. +elim*. +move=> y. +proc. +by auto=> &hr; apply xle_cxr_r. +qed. + +lemma L_elim_ehoareS : + ehoare [M.u : (exists y z, M.g = y /\ z = y) `|` (M.g + 1)%xr ==> M.g%xr]. +proof. +proc. +elim*. +move=> y z. +by auto=> &hr; apply xle_cxr_r. +qed. + +(* -------------------------------------------------------------------- *) +(* phoare *) +lemma L_elim_bdhoareF : + phoare [M.f : exists y, x = y /\ 0 <= y ==> 0 < res] = 1%r. +proof. +elim*. +move=> y. +proc. +by auto=> /#. +qed. + +lemma L_elim_bdhoareS : + phoare [M.f : exists y, x = y /\ 0 <= y ==> 0 < res] <= 1%r. +proof. +proc. +elim*. +move=> y. +by auto. +qed. + +(* -------------------------------------------------------------------- *) +(* equiv *) +lemma L_elim_equivF : + equiv [M.f ~ M.f : exists y, x{1} = y /\ x{2} = y ==> ={res}]. +proof. +elim*. +move=> y. +proc. +by auto. +qed. + +lemma L_elim_equivS : + equiv [M.f ~ M.f : + 0 <= x{1} /\ (0 <= x{1} => exists y, x{1} = y /\ x{2} = y) ==> ={res}]. +proof. +proc. +elim*. +move=> y. +by auto=> /#. +qed. + +(* ==================================================================== *) +(* exists* / exlim *) + +(* -------------------------------------------------------------------- *) +(* hoare *) +lemma L_intro_hoareF : hoare [M.f : 0 <= x ==> 0 < res]. +proof. +exists* x. +elim*. +move=> x0. +proc. +by auto=> /#. +qed. + +lemma L_intro_hoareS : hoare [M.f : 0 <= x ==> 0 < res]. +proof. +proc. +exlim x, M.g => x0 g0. +by auto=> /#. +qed. + +(* -------------------------------------------------------------------- *) +(* ehoare *) +lemma L_intro_ehoareF : ehoare [M.u : (M.g + 1)%xr ==> M.g%xr]. +proof. +exists* M.g. +elim*. +move=> g0. +proc. +by auto=> &hr; apply xle_cxr_r. +qed. + +lemma L_intro_ehoareS : ehoare [M.u : (M.g + 1)%xr ==> M.g%xr]. +proof. +proc. +exlim M.g => g0. +by auto=> &hr; apply xle_cxr_r. +qed. + +(* -------------------------------------------------------------------- *) +(* phoare *) +lemma L_intro_bdhoareF : phoare [M.f : 0 <= x ==> 0 < res] = 1%r. +proof. +exlim x => x0. +proc. +by auto=> /#. +qed. + +lemma L_intro_bdhoareS : phoare [M.f : 0 <= x ==> 0 < res] = 1%r. +proof. +proc. +exists* x, (x + 1). +elim*. +move=> x0 x1. +by auto=> /#. +qed. + +(* -------------------------------------------------------------------- *) +(* equiv *) +lemma L_intro_equivF : equiv [M.f ~ M.f : ={x} ==> ={res}]. +proof. +exlim x{1}, x{2} => x1 x2. +proc. +by auto. +qed. + +lemma L_intro_equivS : equiv [M.f ~ M.f : ={x} ==> ={res}]. +proof. +proc. +exists* x{1}. +elim*. +move=> x1. +by auto. +qed. + +(* -------------------------------------------------------------------- *) +(* not a program-logic goal *) +lemma L_exists_err : true. +proof. +fail elim*. +fail exists* 0. +fail exlim 0 => x. +by []. +qed. + +(* ==================================================================== *) +(* ecall *) + +lemma f_spec (x0 : int) : hoare [M.f : x = x0 ==> res = x0 + 1]. +proof. by proc; auto. qed. + +lemma f_spec_nox : hoare [M.f : 0 <= x ==> 0 < res]. +proof. by proc; auto=> /#. qed. + +lemma u_spec : hoare [M.u : true ==> true]. +proof. by proc; auto. qed. + +lemma f_pspec (x0 : int) : phoare [M.f : x = x0 ==> res = x0 + 1] = 1%r. +proof. by proc; auto. qed. + +lemma f_pspec_le (x0 : int) : phoare [M.f : x = x0 ==> res = x0 + 1] <= 1%r. +proof. by proc; auto. qed. + +lemma f_espec (x0 : int) : + equiv [M.f ~ M.f : ={x} /\ x{1} = x0 ==> ={res} /\ res{1} = x0 + 1]. +proof. by proc; auto. qed. + +exception E. + +lemma f_spec_exn (x0 : int) : + hoare [M.f : x = x0 ==> res = x0 + 1 | E => false]. +proof. by proc; auto. qed. + +(* -------------------------------------------------------------------- *) +(* hoare, forward: an argument abstracting a program variable, and the + framing of the precondition *) +lemma L_ecall_fwd : hoare [M.h : 0 <= x /\ M.g = 0 ==> M.g = 0]. +proof. +proc. +ecall ->> (f_spec x). +- by move=> x_ &hr />. +move=> x_. +by auto. +qed. + +(* hoare, backward *) +lemma L_ecall_bwd : hoare [M.k : 0 <= x ==> 0 < res]. +proof. +proc. +ecall (f_spec x). +by auto=> /#. +qed. + +(* hoare, backward, no argument *) +lemma L_ecall_bwd_nox : hoare [M.k : 0 <= x ==> 0 < res]. +proof. +proc. +ecall (f_spec_nox). +by auto=> /#. +qed. + +(* hoare, backward, a contract with an exceptional postcondition *) +lemma L_ecall_bwd_exn : hoare [M.k : 0 <= x ==> 0 < res]. +proof. +proc. +ecall (f_spec_exn x). +by auto=> /#. +qed. + +(* hoare, errors *) +lemma L_ecall_hoare_err : hoare [M.h : 0 <= x ==> 0 < res]. +proof. +proc. +fail ecall{1} (f_spec x). +fail ecall ->> (f_spec_exn x). +fail ecall ->> (u_spec). +fail ecall ->> (f_pspec x). +fail ecall ->> (f_espec x). +fail ecall (f_spec x). +abort. + +lemma L_ecall_hoareF_err : hoare [M.h : 0 <= x ==> 0 < res]. +proof. +fail ecall (f_spec x). +abort. + +(* -------------------------------------------------------------------- *) +(* phoare, backward *) +lemma L_ecall_phoare : phoare [M.k : 0 <= x ==> 0 < res] = 1%r. +proof. +proc. +ecall (f_pspec x). +by auto=> /#. +qed. + +(* phoare, errors *) +lemma L_ecall_phoare_err : phoare [M.k : 0 <= x ==> 0 < res] = 1%r. +proof. +proc. +fail ecall{1} (f_pspec x). +fail ecall ->> (f_pspec x). +fail ecall (f_pspec_le x). +fail ecall (u_spec). +fail ecall (f_espec x). +(* a hoare contract passes the contract check, then fails (an anomaly) *) +fail ecall (f_spec x). +abort. + +lemma L_ecall_phoare_le_err : phoare [M.k : 0 <= x ==> 0 < res] <= 1%r. +proof. +proc. +fail ecall (f_pspec x). +abort. + +(* -------------------------------------------------------------------- *) +(* equiv, both sides *) +lemma L_ecall_equiv : equiv [M.k ~ M.k : ={x} ==> ={res}]. +proof. +proc. +ecall (f_espec x{1}). +by auto. +qed. + +(* equiv, one side at a time *) +lemma L_ecall_equiv1 : equiv [M.k ~ M.k : ={x} ==> ={res}]. +proof. +proc. +ecall{1} (f_pspec x{1}). +ecall{2} (f_pspec x{2}). +by auto=> /#. +qed. + +(* equiv, errors *) +lemma L_ecall_equiv_err : equiv [M.k ~ M.k : ={x} ==> ={res}]. +proof. +proc. +fail ecall ->> (f_espec x{1}). +fail ecall (f_pspec x{1}). +fail ecall{1} (f_espec x{1}). +fail ecall{1} (u_spec). +fail ecall{1} (f_spec x{1}). +abort. + +(* -------------------------------------------------------------------- *) +(* not a hoare, phoare or equiv goal on a statement *) +lemma L_ecall_ehoare_err : ehoare [M.u : (M.g + 1)%xr ==> M.g%xr]. +proof. +proc. +fail ecall (u_spec). +abort.