|
| 1 | +(* -------------------------------------------------------------------- *) |
| 2 | +open EcUtils |
| 3 | +open EcPath |
| 4 | +open EcAst |
| 5 | + |
| 6 | +(* -------------------------------------------------------------------- *) |
| 7 | +let eval (env : EcEnv.env) = |
| 8 | + let exception NotAValue in |
| 9 | + |
| 10 | + let hyps = EcEnv.LDecl.init env [] in |
| 11 | + let cbv = EcCallbyValue.norm_cbv EcReduction.full_red hyps in |
| 12 | + |
| 13 | + let rec get_function (fpath : xpath) = |
| 14 | + let fun_ = EcEnv.Fun.by_xpath fpath env in |
| 15 | + |
| 16 | + match fun_.f_def with |
| 17 | + | FBdef def -> fun_, def |
| 18 | + | FBalias alias -> let _, def = get_function alias in fun_, def |
| 19 | + | _ -> raise NotAValue in |
| 20 | + |
| 21 | + let rec is_literal (f : form) = |
| 22 | + match EcFol.sform_of_form f with |
| 23 | + | SFtrue | SFfalse | SFint _ -> true |
| 24 | + | SFtuple fs -> List.for_all is_literal fs |
| 25 | + | _ -> false in |
| 26 | + |
| 27 | + let eval (subst : EcPV.PVM.subst) (v : form) = |
| 28 | + let aout = cbv (EcPV.PVM.subst env subst v) in |
| 29 | + if not (is_literal aout) then raise NotAValue; |
| 30 | + aout in |
| 31 | + |
| 32 | + let rec doit (fpath : xpath) (args : form list) = |
| 33 | + let fun_, body = get_function fpath in |
| 34 | + let subst = |
| 35 | + List.fold_left2 (fun (subst : EcPV.PVM.subst) (var : ovariable) (arg : form) -> |
| 36 | + var.ov_name |> Option.fold ~none:subst ~some:(fun name -> |
| 37 | + let pv = EcTypes.pv_loc name in |
| 38 | + EcPV.PVM.add env pv mhr arg subst |
| 39 | + ) |
| 40 | + ) EcPV.PVM.empty fun_.f_sig.fs_anames args in |
| 41 | + |
| 42 | + let subst = for_stmt subst body.f_body in |
| 43 | + |
| 44 | + Option.map |
| 45 | + (eval subst -| EcFol.form_of_expr mhr) |
| 46 | + body.f_ret |
| 47 | + |
| 48 | + and for_instr (subst : EcPV.PVM.subst) (instr : EcModules.instr) = |
| 49 | + match instr.i_node with |
| 50 | + | Sasgn (lvalue, rvalue) -> begin |
| 51 | + let rvalue = eval subst (EcFol.form_of_expr mhr rvalue) in |
| 52 | + match lvalue with |
| 53 | + | LvVar (pv, _) -> |
| 54 | + EcPV.PVM.add env pv mhr rvalue subst |
| 55 | + | LvTuple pvs -> |
| 56 | + let rvalue = EcFol.destr_tuple rvalue in |
| 57 | + List.fold_left2 (fun subst (pv, _) rvalue -> |
| 58 | + EcPV.PVM.add env pv mhr rvalue subst |
| 59 | + ) subst pvs rvalue |
| 60 | + end |
| 61 | + |
| 62 | + | Scall (lv, f, args) -> begin |
| 63 | + let args = List.map (eval subst -| EcFol.form_of_expr mhr) args in |
| 64 | + let aout = doit f args in |
| 65 | + |
| 66 | + match lv with |
| 67 | + | None -> |
| 68 | + subst |
| 69 | + |
| 70 | + | Some (LvVar (pv, _)) -> |
| 71 | + EcPV.PVM.add env pv mhr (Option.get aout) subst |
| 72 | + |
| 73 | + | Some (LvTuple pvs) -> |
| 74 | + List.fold_left2 (fun subst (pv, _) aout -> |
| 75 | + EcPV.PVM.add env pv mhr aout subst |
| 76 | + ) subst pvs (EcFol.destr_tuple (Option.get aout)) |
| 77 | + end |
| 78 | + |
| 79 | + | Sif (cond, strue, sfalse) -> |
| 80 | + let cond = eval subst (EcFol.form_of_expr mhr cond) in |
| 81 | + let branch = |
| 82 | + match EcFol.sform_of_form cond with |
| 83 | + | SFtrue -> strue |
| 84 | + | SFfalse -> sfalse |
| 85 | + | _ -> raise NotAValue in |
| 86 | + |
| 87 | + for_stmt subst branch |
| 88 | + |
| 89 | + | Swhile (cond, body) -> begin |
| 90 | + let cond = eval subst (EcFol.form_of_expr mhr cond) in |
| 91 | + |
| 92 | + match EcFol.sform_of_form cond with |
| 93 | + | SFtrue -> |
| 94 | + let subst = for_stmt subst body in |
| 95 | + let subst = for_instr subst instr in |
| 96 | + subst |
| 97 | + |
| 98 | + | SFfalse -> |
| 99 | + subst |
| 100 | + |
| 101 | + | _ -> |
| 102 | + raise NotAValue |
| 103 | + end |
| 104 | + |
| 105 | + | Sabstract _ |
| 106 | + | Sassert _ |
| 107 | + | Smatch _ |
| 108 | + | Srnd _ -> raise NotAValue |
| 109 | + |
| 110 | + and for_stmt (subst : EcPV.PVM.subst) (stmt : stmt) = |
| 111 | + List.fold_left for_instr subst stmt.s_node |
| 112 | + |
| 113 | + in |
| 114 | + |
| 115 | + fun ((fpath, args) : xpath * expr list) -> |
| 116 | + try |
| 117 | + let aout = |
| 118 | + doit fpath (List.map (cbv -| EcFol.form_of_expr mhr) args) |
| 119 | + in Option.map (EcFol.expr_of_form mhr) aout |
| 120 | + with NotAValue -> None |
0 commit comments