Skip to content

Commit

Permalink
Coq: fix pure loops inside a monadic expression
Browse files Browse the repository at this point in the history
Needed for current RISC-V.
Requires recent coq-sail.
  • Loading branch information
bacam committed Jan 21, 2025
1 parent 16afc9c commit f8de74b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/sail_coq_backend/pretty_print_coq.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,7 @@ let doc_exp, doc_let =
in
let combinator = combinator ^ dir in
let body_ctxt = add_single_kid_id_rename ctxt loopvar (mk_kid ("loop_" ^ string_of_id loopvar)) in
let body_ctxt = { body_ctxt with is_monadic = effectful (effect_of body) } in
let from_exp_pp, to_exp_pp, step_exp_pp = (expY from_exp, expY to_exp, expY step_exp) in
(* The body has the right type for deciding whether a proof is necessary *)
let vartuple_retyped = check_exp env (strip_exp vartuple) (general_typ_of body) in
Expand All @@ -1601,7 +1602,9 @@ let doc_exp, doc_let =
(parens (prefix 2 1 (group body_lambda) body_pp))
)
in
loop_pp
if ctxt.is_monadic && (not body_ctxt.is_monadic) && has_early_return body then
parens (string "pure_early_return_embed" ^/^ loop_pp)
else loop_pp
| _ -> raise (Reporting.err_unreachable l __POS__ "Unexpected number of arguments for loop combinator")
end
| Id_aux (Id (("while#" | "until#" | "while#t" | "until#t") as combinator), _) ->
Expand Down
11 changes: 11 additions & 0 deletions test/coq/pass/pure_loop_in_monad.sail
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
default Order dec
$include <prelude.sail>

register R0 : bits(32)

function test(v : vector(16, bits(32))) -> bool = {
let r = R0;
foreach (i from 0 to 15)
if v[i] == r then return true;
false
}

0 comments on commit f8de74b

Please sign in to comment.