1
(************************************************************************)
2
(* v * The Coq Proof Assistant / The Coq Development Team *)
3
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
4
(* \VV/ **************************************************************)
5
(* // * This file is distributed under the terms of the *)
6
(* * GNU Lesser General Public License Version 2.1 *)
7
(************************************************************************)
9
(* $Id: Ascii.v 9245 2006-10-17 12:53:34Z notin $ *)
11
(** Contributed by Laurent Th�ry (INRIA);
12
Adapted to Coq V8 by the Coq Development Team *)
15
Require Import BinPos.
17
(** * Definition of ascii characters *)
19
(** Definition of ascii character as a 8 bits constructor *)
21
Inductive ascii : Set := Ascii (_ _ _ _ _ _ _ _ : bool).
23
Delimit Scope char_scope with char.
24
Bind Scope char_scope with ascii.
26
Definition zero := Ascii false false false false false false false false.
28
Definition one := Ascii true false false false false false false false.
30
Definition app1 (f : bool -> bool) (a : ascii) :=
32
| Ascii a1 a2 a3 a4 a5 a6 a7 a8 =>
33
Ascii (f a1) (f a2) (f a3) (f a4) (f a5) (f a6) (f a7) (f a8)
36
Definition app2 (f : bool -> bool -> bool) (a b : ascii) :=
38
| Ascii a1 a2 a3 a4 a5 a6 a7 a8, Ascii b1 b2 b3 b4 b5 b6 b7 b8 =>
39
Ascii (f a1 b1) (f a2 b2) (f a3 b3) (f a4 b4)
40
(f a5 b5) (f a6 b6) (f a7 b7) (f a8 b8)
43
Definition shift (c : bool) (a : ascii) :=
45
| Ascii a1 a2 a3 a4 a5 a6 a7 a8 => Ascii c a1 a2 a3 a4 a5 a6 a7
48
(** Definition of a decidable function that is effective *)
50
Definition ascii_dec : forall a b : ascii, {a = b} + {a <> b}.
51
decide equality; apply bool_dec.
54
(** * Conversion between natural numbers modulo 256 and ascii characters *)
56
(** Auxillary function that turns a positive into an ascii by
57
looking at the last n bits, ie z mod 2^n *)
59
Fixpoint ascii_of_pos_aux (res acc : ascii) (z : positive)
60
(n : nat) {struct n} : ascii :=
65
| xH => app2 orb res acc
66
| xI z' => ascii_of_pos_aux (app2 orb res acc) (shift false acc) z' n1
67
| xO z' => ascii_of_pos_aux res (shift false acc) z' n1
72
(** Function that turns a positive into an ascii by
73
looking at the last 8 bits, ie a mod 8 *)
75
Definition ascii_of_pos (a : positive) := ascii_of_pos_aux zero one a 8.
77
(** Function that turns a Peano number into an ascii by converting it
80
Definition ascii_of_nat (a : nat) :=
83
| S a' => ascii_of_pos (P_of_succ_nat a')
86
(** The opposite function *)
88
Definition nat_of_ascii (a : ascii) : nat :=
89
let (a1, a2, a3, a4, a5, a6, a7, a8) := a in
96
(2 * (if a8 then 1 else 0)
97
+ (if a7 then 1 else 0))
98
+ (if a6 then 1 else 0))
99
+ (if a5 then 1 else 0))
100
+ (if a4 then 1 else 0))
101
+ (if a3 then 1 else 0))
102
+ (if a2 then 1 else 0))
103
+ (if a1 then 1 else 0).
105
Theorem ascii_nat_embedding :
106
forall a : ascii, ascii_of_nat (nat_of_ascii a) = a.
108
destruct a as [[|][|][|][|][|][|][|][|]]; compute; reflexivity.
111
(** * Concrete syntax *)
114
Ascii characters can be represented in scope char_scope as follows:
115
- ["c"] represents itself if c is a character of code < 128,
116
- [""""] is an exception: it represents the ascii character 34
118
- ["nnn"] represents the ascii character of decimal code nnn.
120
For instance, both ["065"] and ["A"] denote the character `uppercase
121
A', and both ["034"] and [""""] denote the character `double quote'.
123
Notice that the ascii characters of code >= 128 do not denote
124
stand-alone utf8 characters so that only the notation "nnn" is
125
available for them (unless your terminal is able to represent them,
126
which is typically not the case in coqide).
129
Open Local Scope char_scope.
131
Example Space := " ".
132
Example DoubleQuote := """".
133
Example Beep := "007".