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: g_zsyntax.ml 11309 2008-08-06 10:30:35Z herbelin $ *)
19
exception Non_closed_number
21
(**********************************************************************)
22
(* Parsing positive via scopes *)
23
(**********************************************************************)
27
let make_dir l = make_dirpath (List.map id_of_string (List.rev l))
28
let positive_module = ["Coq";"NArith";"BinPos"]
29
let make_path dir id = Libnames.make_path (make_dir dir) (id_of_string id)
31
let positive_path = make_path positive_module "positive"
33
(* TODO: temporary hack *)
34
let make_kn dir id = Libnames.encode_kn dir id
37
make_kn (make_dir positive_module) (id_of_string "positive")
38
let glob_positive = IndRef (positive_kn,0)
39
let path_of_xI = ((positive_kn,0),1)
40
let path_of_xO = ((positive_kn,0),2)
41
let path_of_xH = ((positive_kn,0),3)
42
let glob_xI = ConstructRef path_of_xI
43
let glob_xO = ConstructRef path_of_xO
44
let glob_xH = ConstructRef path_of_xH
46
let pos_of_bignat dloc x =
47
let ref_xI = RRef (dloc, glob_xI) in
48
let ref_xH = RRef (dloc, glob_xH) in
49
let ref_xO = RRef (dloc, glob_xO) in
51
match div2_with_rest x with
52
| (q,false) -> RApp (dloc, ref_xO,[pos_of q])
53
| (q,true) when q <> zero -> RApp (dloc,ref_xI,[pos_of q])
58
let error_non_positive dloc =
59
user_err_loc (dloc, "interp_positive",
60
str "Only strictly positive numbers in type \"positive\".")
62
let interp_positive dloc n =
63
if is_strictly_pos n then pos_of_bignat dloc n
64
else error_non_positive dloc
66
(**********************************************************************)
67
(* Printing positive via scopes *)
68
(**********************************************************************)
70
let rec bignat_of_pos = function
71
| RApp (_, RRef (_,b),[a]) when b = glob_xO -> mult_2(bignat_of_pos a)
72
| RApp (_, RRef (_,b),[a]) when b = glob_xI -> add_1(mult_2(bignat_of_pos a))
73
| RRef (_, a) when a = glob_xH -> Bigint.one
74
| _ -> raise Non_closed_number
76
let uninterp_positive p =
78
Some (bignat_of_pos p)
79
with Non_closed_number ->
82
(************************************************************************)
83
(* Declaring interpreters and uninterpreters for positive *)
84
(************************************************************************)
86
let _ = Notation.declare_numeral_interpreter "positive_scope"
87
(positive_path,positive_module)
89
([RRef (dummy_loc, glob_xI);
90
RRef (dummy_loc, glob_xO);
91
RRef (dummy_loc, glob_xH)],
95
(**********************************************************************)
96
(* Parsing N via scopes *)
97
(**********************************************************************)
99
let binnat_module = ["Coq";"NArith";"BinNat"]
100
let n_kn = make_kn (make_dir binnat_module) (id_of_string "N")
101
let glob_n = IndRef (n_kn,0)
102
let path_of_N0 = ((n_kn,0),1)
103
let path_of_Npos = ((n_kn,0),2)
104
let glob_N0 = ConstructRef path_of_N0
105
let glob_Npos = ConstructRef path_of_Npos
107
let n_path = make_path binnat_module "N"
109
let n_of_binnat dloc pos_or_neg n =
111
RApp(dloc, RRef (dloc,glob_Npos), [pos_of_bignat dloc n])
115
let error_negative dloc =
116
user_err_loc (dloc, "interp_N", str "No negative numbers in type \"N\".")
118
let n_of_int dloc n =
119
if is_pos_or_zero n then n_of_binnat dloc true n
120
else error_negative dloc
122
(**********************************************************************)
123
(* Printing N via scopes *)
124
(**********************************************************************)
126
let bignat_of_n = function
127
| RApp (_, RRef (_,b),[a]) when b = glob_Npos -> bignat_of_pos a
128
| RRef (_, a) when a = glob_N0 -> Bigint.zero
129
| _ -> raise Non_closed_number
132
try Some (bignat_of_n p)
133
with Non_closed_number -> None
135
(************************************************************************)
136
(* Declaring interpreters and uninterpreters for N *)
138
let _ = Notation.declare_numeral_interpreter "N_scope"
139
(n_path,binnat_module)
141
([RRef (dummy_loc, glob_N0);
142
RRef (dummy_loc, glob_Npos)],
146
(**********************************************************************)
147
(* Parsing Z via scopes *)
148
(**********************************************************************)
150
let binint_module = ["Coq";"ZArith";"BinInt"]
151
let z_path = make_path binint_module "Z"
152
let z_kn = make_kn (make_dir binint_module) (id_of_string "Z")
153
let glob_z = IndRef (z_kn,0)
154
let path_of_ZERO = ((z_kn,0),1)
155
let path_of_POS = ((z_kn,0),2)
156
let path_of_NEG = ((z_kn,0),3)
157
let glob_ZERO = ConstructRef path_of_ZERO
158
let glob_POS = ConstructRef path_of_POS
159
let glob_NEG = ConstructRef path_of_NEG
161
let z_of_int dloc n =
164
if is_pos_or_zero n then glob_POS, n else glob_NEG, Bigint.neg n in
165
RApp(dloc, RRef (dloc,sgn), [pos_of_bignat dloc n])
167
RRef (dloc, glob_ZERO)
169
(**********************************************************************)
170
(* Printing Z via scopes *)
171
(**********************************************************************)
173
let bigint_of_z = function
174
| RApp (_, RRef (_,b),[a]) when b = glob_POS -> bignat_of_pos a
175
| RApp (_, RRef (_,b),[a]) when b = glob_NEG -> Bigint.neg (bignat_of_pos a)
176
| RRef (_, a) when a = glob_ZERO -> Bigint.zero
177
| _ -> raise Non_closed_number
182
with Non_closed_number -> None
184
(************************************************************************)
185
(* Declaring interpreters and uninterpreters for Z *)
187
let _ = Notation.declare_numeral_interpreter "Z_scope"
188
(z_path,binint_module)
190
([RRef (dummy_loc, glob_ZERO);
191
RRef (dummy_loc, glob_POS);
192
RRef (dummy_loc, glob_NEG)],