Skip to content
Snippets Groups Projects
Commit f4db0245 authored by Robbert Krebbers's avatar Robbert Krebbers
Browse files

Better version of `ident_to_string`, also by @lepigre.

parent 9fdd6724
No related branches found
No related tags found
No related merge requests found
From Ltac2 Require Ltac2. From Ltac2 Require Ltac2.
From Coq Require Import Strings.String. From Coq Require Import Strings.String.
From Coq Require Import Init.Byte. From Coq Require Import Init.Byte Ascii.
From iris.prelude Require Import options. From iris.prelude Require Import options.
Import List.ListNotations. Import List.ListNotations.
...@@ -77,26 +77,26 @@ End StringToIdent. ...@@ -77,26 +77,26 @@ End StringToIdent.
Module IdentToString. Module IdentToString.
Import Ltac2. Import Ltac2.
Ltac2 char_to_coq_byte (c : char) : constr := Ltac2 get_bit (n : int) (i : int) : bool :=
let (ind, inst) := Int.equal (Int.land (Int.lsr n i) 1) 1.
match Constr.Unsafe.kind constr:(byte) with
| Constr.Unsafe.Ind ind inst => (ind, inst) Ltac2 get_bit_coq_bool (n : int) (i : int) : constr :=
| _ => Control.zero (Invalid_argument None) if get_bit n i then constr:(true) else constr:(false).
end
in Ltac2 char_to_coq_ascii (c : char) : constr :=
let c := Constr.Unsafe.constructor ind (Char.to_int c) in let i := Char.to_int c in
Constr.Unsafe.make (Constr.Unsafe.Constructor c inst). let bs := Array.init 8 (get_bit_coq_bool i) in
Constr.Unsafe.make (Constr.Unsafe.App constr:(Ascii) bs).
Ltac2 string_to_coq_string (s : string) : constr := Ltac2 string_to_coq_string (s : string) : constr :=
let len := String.length s in let len := String.length s in
let rec to_string acc i := let rec to_string i :=
if Int.equal i -1 then acc else if Int.equal i len then constr:(EmptyString) else
let c := String.get s i in let tail := to_string (Int.add i 1) in
let byte := char_to_coq_byte c in let head := char_to_coq_ascii (String.get s i) in
let a := compute constr:(Ascii.ascii_of_byte $byte) in constr:(String $head $tail)
to_string constr:(String $a $acc) (Int.sub i 1)
in in
to_string constr:(EmptyString) (Int.sub len 1). to_string 0.
Ltac2 ident_to_string (id : ident) : constr := Ltac2 ident_to_string (id : ident) : constr :=
string_to_coq_string (Ident.to_string id). string_to_coq_string (Ident.to_string id).
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment