Skip to content

Commit 33a6f93

Browse files
committed
Fixed circ_sat SMT input printing, fixed context for bdep solve
1 parent b5ed75f commit 33a6f93

File tree

5 files changed

+109
-36
lines changed

5 files changed

+109
-36
lines changed

examples/mapreduce_paper.ec

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ qed.
9292
lemma xor_left_corr_lanes (w: W8.t) :
9393
hoare [ M.xor_left_proc : w = w1 ==> res = xor_left w].
9494
proof.
95-
proc.
95+
proc.
9696
bdep 1 1 [w] [w1] [w1] xor1_bool predT_bool.
9797
admit.
9898
admit.
@@ -111,3 +111,18 @@ lemma xor_left_eq_xor_right (w: W8.t) : xor_left w = xor_right w.
111111
proof.
112112
bdep solve.
113113
qed.
114+
115+
lemma xor_left_corr_wp (w: W8.t) :
116+
hoare [ M.xor_left_proc : w = w1 ==> res = xor_left w].
117+
proof.
118+
proc.
119+
wp; skip => &hr. by bdep solve.
120+
qed.
121+
122+
lemma xor_left_corr_wp_alt (w: W8.t) :
123+
hoare [ M.xor_left_proc : w = w1 ==> res = xor_left w].
124+
proof.
125+
proc.
126+
wp; skip => &hr eq.
127+
by bdep solve.
128+
qed.

libs/lospecs/hlaig.ml

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ module MakeSMTInterface(SMT: SMTInstance) : SMTInterface = struct
182182

183183
let inps = Option.map (fun inps ->
184184
List.map (fun (id,sz) ->
185-
List.init sz (fun i -> ("BV_" ^ (id |> string_of_int) ^ "_" ^ (Printf.sprintf "%X" (i))))) inps
185+
List.init sz (fun i -> ("BV_" ^ (id |> string_of_int) ^ "_" ^ (Printf.sprintf "%05X" (i))))) inps
186186
) inps in
187187
let inps = Option.map (fun inps ->
188188
List.map (List.map (fun name -> match Map.String.find_opt name !bvvars with
@@ -197,17 +197,12 @@ module MakeSMTInterface(SMT: SMTInstance) : SMTInterface = struct
197197
SMT.assert' @@ form;
198198
if SMT.check_sat () = true then
199199
begin
200-
let terms = Map.String.to_seq !bvvars in
201-
let terms = List.of_seq terms |> List.sort (fun a b -> compare (fst a) (fst b))
202-
|> List.map (fun a -> snd a) in
203-
let term = List.reduce SMT.bvterm_concat terms in
204-
Format.eprintf "input: %a@." SMT.pp_term (SMT.get_value term);
205-
Option.may (fun bvinp ->
206-
List.iteri (fun i bv ->
207-
Format.eprintf "input[%d]: %a@." i SMT.pp_term (SMT.get_value bv)
200+
Format.eprintf "Input BVVars: ";
201+
let () = Enum.iter (Format.eprintf "%s, ") (Map.String.keys !bvvars) in
202+
Format.eprintf "@.";
203+
Option.may (fun bvinp -> List.iteri (fun i bv ->
204+
Format.eprintf "input[%d]: %a@." i SMT.pp_term (SMT.get_value bv)
208205
) bvinp) bvinp;
209-
(* Format.eprintf "fc: %a@." SMT.pp_term (SMT.get_value bvinpt1); *)
210-
(* Format.eprintf "block: %a@." SMT.pp_term (SMT.get_value bvinpt2); *)
211206
true
212207
end
213208
else false

src/ecCircuits.ml

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,11 @@ module type CircuitInterface = sig
157157

158158

159159
(* Circuits representing booleans *)
160-
val circuit_true : (cbool * (cinp list))
161-
val circuit_false : (cbool * (cinp list))
160+
val circuit_true : cbool cfun
161+
val circuit_false : cbool cfun
162+
val circuit_and : cbool cfun -> cbool cfun -> cbool cfun
163+
val circuit_or : cbool cfun -> cbool cfun -> cbool cfun
164+
val circuit_not : cbool cfun -> cbool cfun
162165

163166
(* <=> circuit has not inputs (every input is unbound) *)
164167
val circuit_is_free : circuit -> bool
@@ -877,6 +880,16 @@ module MakeCircuitInterfaceFromCBackend(Backend: CBackend) : CircuitInterface =
877880

878881
let circuit_true = `CBool Backend.true_ , []
879882
let circuit_false = `CBool Backend.false_, []
883+
884+
let circuit_and (`CBool c1, inps1) (`CBool c2, inps2) =
885+
`CBool (Backend.band c1 c2), merge_inputs inps1 inps2
886+
887+
let circuit_or (`CBool c1, inps1) (`CBool c2, inps2) =
888+
`CBool (Backend.bor c1 c2), merge_inputs inps1 inps2
889+
890+
let circuit_not (`CBool c, inps) =
891+
`CBool (Backend.bnot c), inps
892+
880893
let circuit_is_free (f: circuit) : bool = List.is_empty @@ snd f
881894

882895
let circuit_ite ~(c: cbool * (cinp list)) ~(t: circuit) ~(f: circuit) : circuit =
@@ -1428,6 +1441,7 @@ module MakeCircuitInterfaceFromCBackend(Backend: CBackend) : CircuitInterface =
14281441
Backend.sat ~inps c
14291442

14301443
let circ_taut (c: circuit) : bool =
1444+
Format.eprintf "Calling circ_taut on circuit: %a@." pp_circuit c;
14311445
let `CBool c, inps = cbool_of_circuit ~strict:false c in
14321446
let inps = List.map (function
14331447
| { type_ = `CIBool; id } -> (id, 1)
@@ -1863,6 +1877,31 @@ let circuit_of_form
18631877
hyps, (circuit_true :> circuit)
18641878
| Some `False, [] ->
18651879
hyps, (circuit_false :> circuit)
1880+
| Some `Imp, [f1; f2] ->
1881+
let hyps, c1 = doit cache hyps f1 in
1882+
let hyps, c2 = doit cache hyps f2 in
1883+
let c1 = cbool_of_circuit ~strict:false c1 in
1884+
let c2 = cbool_of_circuit ~strict:false c2 in
1885+
hyps, (circuit_or (circuit_not c1) c2 :> circuit)
1886+
| Some (`And _), [f1; f2] ->
1887+
let hyps, c1 = doit cache hyps f1 in
1888+
let hyps, c2 = doit cache hyps f2 in
1889+
let c1 = cbool_of_circuit ~strict:false c1 in
1890+
let c2 = cbool_of_circuit ~strict:false c2 in
1891+
hyps, (circuit_and c1 c2 :> circuit)
1892+
| Some (`Or _), [f1; f2] ->
1893+
let hyps, c1 = doit cache hyps f1 in
1894+
let hyps, c2 = doit cache hyps f2 in
1895+
let c1 = cbool_of_circuit ~strict:false c1 in
1896+
let c2 = cbool_of_circuit ~strict:false c2 in
1897+
hyps, (circuit_or c1 c2 :> circuit)
1898+
| Some `Iff, [f1; f2] ->
1899+
let hyps, c1 = doit cache hyps f1 in
1900+
let hyps, c2 = doit cache hyps f2 in
1901+
let c1 = cbool_of_circuit ~strict:false c1 in
1902+
let c2 = cbool_of_circuit ~strict:false c2 in
1903+
hyps, (circuit_or (circuit_and c1 c2) (circuit_and (circuit_not c1) (circuit_not c2)) :> circuit)
1904+
(* | Some `Not, [f] -> doit cache hyps (f_not f) *)
18661905
| _ -> (* recurse down into definition *)
18671906
let hyps, f_c = doit cache hyps f in
18681907
let hyps, fcs = List.fold_left_map
@@ -2150,6 +2189,47 @@ let circuit_aggregate =
21502189
let circuit_aggregate_inps =
21512190
circuit_aggregate_inputs
21522191

2153-
21542192
let circuit_flatten (c: circuit) =
21552193
(cbitstring_of_circuit ~strict:false c :> circuit)
2194+
2195+
(* TODO: get a better name and uniformize *)
2196+
let circuit_of_form_with_hyps ?(pstate = empty_pstate) ?(cache = empty_cache) hyps f =
2197+
let (pstate, cache, f), bnds = List.fold_left_map (fun (pstate, cache, goal) (id, lk) ->
2198+
Format.eprintf "Processing hyp: %s@." (id.id_symb);
2199+
match lk with
2200+
| EcBaseLogic.LD_mem (Lmt_concrete Some {lmt_decl=decls}) ->
2201+
let pstate, bnds = List.fold_left_map (fun pstate {ov_name; ov_type} ->
2202+
match ov_name with
2203+
| Some v -> let id = create v in
2204+
open_circ_lambda_pstate (toenv hyps) pstate [(id, ov_type)], Some (id, ov_type)
2205+
| None -> (pstate, None)
2206+
) pstate decls in
2207+
(pstate, cache, goal), List.filter_map (fun i -> i) bnds
2208+
| EcBaseLogic.LD_var (t, Some f) ->
2209+
let cache = open_circ_lambda_cache (toenv hyps) cache [(id, t)] in
2210+
begin try
2211+
ignore (circuit_of_form ~pstate ~cache hyps f);
2212+
(pstate, cache, (f_and goal (f_eq (f_local id t) f))), [(id, t)]
2213+
with _ -> (pstate, cache, f_forall [(id, GTty t)] goal), [(id, t)] (* FIXME: do we still add to cache here? *)
2214+
end
2215+
| EcBaseLogic.LD_var (t, None) ->
2216+
(pstate,
2217+
open_circ_lambda_cache (toenv hyps) cache [(id, t)],
2218+
goal), [(id, t)]
2219+
| EcBaseLogic.LD_hyp f_hyp ->
2220+
begin try
2221+
ignore (circuit_of_form ~pstate ~cache hyps f_hyp);
2222+
(pstate, cache, f_imp f_hyp goal), []
2223+
with e ->
2224+
Format.eprintf "Failed to convert hyp %a with error:@.%s@."
2225+
EcPrinting.(pp_form (PPEnv.ofenv (toenv hyps))) f_hyp (Printexc.to_string e);
2226+
(pstate, cache, goal), []
2227+
end
2228+
2229+
| _ -> (pstate, cache, goal), []
2230+
) (pstate, cache, f) (List.rev (tohyps hyps).h_local)
2231+
in
2232+
let bnds = List.flatten bnds in
2233+
Format.eprintf "Converting form %a@." EcPrinting.(pp_form (PPEnv.ofenv (toenv hyps))) f;
2234+
close_circ_lambda (toenv hyps) bnds @@
2235+
circuit_of_form ~pstate ~cache hyps f

src/ecCircuits.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,6 @@ val pstate_of_prog : hyps -> memory -> instr list -> variable list -> pstate
6060
val instrs_equiv : hyps -> memenv -> ?keep:EcPV.PV.t -> ?pstate:pstate -> instr list -> instr list -> bool
6161
val process_instr : hyps -> memory -> pstate -> instr -> pstate
6262
(* val pstate_of_memtype : ?pstate:pstate -> env -> memtype -> pstate * cinput list *)
63+
64+
(* Temporary? *)
65+
val circuit_of_form_with_hyps : ?pstate:pstate -> ?cache:cache -> hyps -> form -> circuit

src/phl/ecPhlBDep.ml

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,27 +1008,7 @@ let t_bdep_solve
10081008
let goal = (FApi.tc1_goal tc) in
10091009
let ctxt = tohyps hyps in
10101010
assert (ctxt.h_tvar = []);
1011-
let ctxt = ctxt.h_local in
1012-
let goal = List.fold_left (fun goal (id, lk) ->
1013-
match lk with
1014-
| EcBaseLogic.LD_var (t, Some f) ->
1015-
begin try
1016-
ignore (circuit_of_form hyps (f_forall [(id, GTty t)] f));
1017-
f_forall [(id, GTty t)] (f_and goal (f_eq (f_local id t) f))
1018-
with _ -> f_forall [(id, GTty t)] goal
1019-
end
1020-
| EcBaseLogic.LD_var (t, None) -> f_forall [(id, GTty t)] goal
1021-
| EcBaseLogic.LD_hyp f ->
1022-
begin try
1023-
ignore (circuit_of_form hyps f);
1024-
f_and f goal
1025-
with _ -> goal
1026-
end
1027-
| EcBaseLogic.LD_mem _
1028-
| EcBaseLogic.LD_modty _
1029-
| EcBaseLogic.LD_abs_st _ -> assert false
1030-
) goal ctxt in
1031-
if circ_taut (circuit_of_form hyps goal) then
1011+
if circ_taut (circuit_of_form_with_hyps hyps goal) then
10321012
FApi.close (!@ tc) VBdep
10331013
else
10341014
tc_error (FApi.tc1_penv tc) "Failed to solve goal through circuit reasoning@\n"

0 commit comments

Comments
 (0)