Skip to content

Commit

Permalink
read Ltac2 integer bits using bitwise right shift (#355)
Browse files Browse the repository at this point in the history
* read Ltac2 integer bits using bitwise right shift

Now that Ltac2 [supports](coq/coq#15637) integer
operations like the bitwise right shift (`Int.lsr`), we can use it
directly for converting an Ltac2 integer into a bool list representation
of its bits, obviating the original workaround.

* simplify int_to_bits_rec

* fix typo and elaborate on comment

* minor refactor in LiveParsing.v
  • Loading branch information
robertzhidealx authored Jul 4, 2023
1 parent 0893c1f commit 64beea5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Makefile.coq*
/end2end/admits.txt
/end2end/admits1perLine.txt

.vscode

# _CoqProject is generated by the Makefile
_CoqProject

Expand Down
2 changes: 1 addition & 1 deletion LiveVerif/src/LiveVerif/LiveParsing.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Require Import Ltac2.Ltac2.

Ltac2 exact_basename_string_of_ref_constr(c: constr) :=
let ref := reference_of_constr c in
let s := ident_to_string.string_to_coq (Ident.to_string (List.last (Env.path ref))) in
let s := ident_to_coq (List.last (Env.path ref)) in
exact $s.

Ltac exact_basename_string_of_ref_constr :=
Expand Down
28 changes: 7 additions & 21 deletions bedrock2/src/bedrock2/ident_to_string.v
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
Require Import Coq.Strings.String.
Require Import Ltac2.Ltac2.
Require Ltac2.Option.

(* Takes a list of powers instead of one power and dividing because
Ltac2 does not have integer division: https://github.com/coq/coq/issues/13802 *)
Ltac2 rec int_to_bits_rec(powers: int list)(val: int) :=
match powers with
| p :: rest =>
if Int.le p val
then true :: int_to_bits_rec rest (Int.sub val p)
else false :: int_to_bits_rec rest val
| [] => []
end.

(* [2^(n-1); ..., 2^0] *)
Ltac2 rec powers_of_two(n: int) :=
if Int.equal n 1 then [1] else
let r := powers_of_two (Int.sub n 1) in
match r with
| h :: t => Int.mul 2 h :: r
| [] => []
end.
(* Converts the lower i + 1 bits of an Ltac2 integer into a bool list
(lower bits to the left) where 0 is false and 1 is true *)
Ltac2 rec int_to_bits_rec(val: int)(i: int) :=
if Int.lt i 0 then [] else
(Int.equal (Int.land (Int.lsr val i) 1) 1)
:: int_to_bits_rec val (Int.sub i 1).

Ltac2 char_to_bits(c: char) :=
int_to_bits_rec (powers_of_two 8) (Char.to_int c).
int_to_bits_rec (Char.to_int c) 7.

Ltac2 bool_to_coq(b: bool) :=
if b then constr:(true) else constr:(false).
Expand Down

0 comments on commit 64beea5

Please sign in to comment.