Library Coq.Strings.Ascii
Contributed by Laurent Théry (INRIA);
Adapted to Coq V8 by the Coq Development Team
Inductive ascii : Set := Ascii (_ _ _ _ _ _ _ _ : bool).
Delimit Scope char_scope with char.
Definition zero := Ascii false false false false false false false false.
Definition one := Ascii true false false false false false false false.
Definition shift (c : bool) (a : ascii) :=
match a with
| Ascii a1 a2 a3 a4 a5 a6 a7 a8 => Ascii c a1 a2 a3 a4 a5 a6 a7
end.
Definition of a decidable function that is effective
Conversion between natural numbers modulo 256 and ascii characters
Definition ascii_of_pos : positive -> ascii :=
let loop := fix loop n p :=
match n with
| O => zero
| S n´ =>
match p with
| xH => one
| xI p´ => shift true (loop n´ p´)
| xO p´ => shift false (loop n´ p´)
end
end
in loop 8.
Conversion from N to ascii
Same for nat
The opposite functions
Local Open Scope list_scope.
Fixpoint N_of_digits (l:list bool) : N :=
match l with
| nil => 0
| b :: l´ => (if b then 1 else 0) + 2*(N_of_digits l´)
end%N.
Definition N_of_ascii (a : ascii) : N :=
let (a0,a1,a2,a3,a4,a5,a6,a7) := a in
N_of_digits (a0::a1::a2::a3::a4::a5::a6::a7::nil).
Definition nat_of_ascii (a : ascii) : nat := N.to_nat (N_of_ascii a).
Proofs that we have indeed opposite function (below 256)
Theorem ascii_N_embedding :
forall a : ascii, ascii_of_N (N_of_ascii a) = a.
Theorem N_ascii_embedding :
forall n:N, (n < 256)%N -> N_of_ascii (ascii_of_N n) = n.
Theorem ascii_nat_embedding :
forall a : ascii, ascii_of_nat (nat_of_ascii a) = a.
Theorem nat_ascii_embedding :
forall n : nat, n < 256 -> nat_of_ascii (ascii_of_nat n) = n.
Concrete syntax
- "c" represents itself if c is a character of code < 128,
- """" is an exception: it represents the ascii character 34 (double quote),
- "nnn" represents the ascii character of decimal code nnn.
Local Open Scope char_scope.
Example Space := " ".
Example DoubleQuote := """".
Example Beep := "007".