| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open CErrors |
| 12 |
open Util |
| 13 |
open Pp |
| 14 |
open Names |
| 15 |
open Constr |
| 16 |
open Libnames |
| 17 |
open Constrexpr |
| 18 |
open Glob_term |
| 19 |
open Glob_ops |
| 20 |
open NumTok |
| 21 |
|
| 22 |
let mkRef (env,sigmaref) r = |
| 23 |
let sigma, c = Evd.fresh_global env !sigmaref r in |
| 24 |
sigmaref := sigma; |
| 25 |
EConstr.Unsafe.to_constr c |
| 26 |
|
| 27 |
let mkConstruct esig c = mkRef esig (ConstructRef c) |
| 28 |
let mkInd esig i = mkRef esig (IndRef i) |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
type rawnum = NumTok.Signed.t |
| 34 |
|
| 35 |
type prim_token_uid = string |
| 36 |
|
| 37 |
type 'a prim_token_interpreter = ?loc:Loc.t -> 'a -> glob_constr |
| 38 |
type 'a prim_token_uninterpreter = any_glob_constr -> 'a option |
| 39 |
|
| 40 |
type 'a prim_token_interpretation = |
| 41 |
'a prim_token_interpreter * 'a prim_token_uninterpreter |
| 42 |
|
| 43 |
module InnerPrimToken = struct |
| 44 |
|
| 45 |
type interpreter = |
| 46 |
| RawNumInterp of (?loc:Loc.t -> rawnum -> glob_constr) |
| 47 |
| BigNumInterp of (?loc:Loc.t -> Z.t -> glob_constr) |
| 48 |
| StringInterp of (?loc:Loc.t -> string -> glob_constr) |
| 49 |
|
| 50 |
let interp_eq f f' = match f,f' with |
| 51 |
| RawNumInterp f, RawNumInterp f' -> f == f' |
| 52 |
| BigNumInterp f, BigNumInterp f' -> f == f' |
| 53 |
| StringInterp f, StringInterp f' -> f == f' |
| 54 |
| _ -> false |
| 55 |
|
| 56 |
let do_interp ?loc interp primtok = |
| 57 |
match primtok, interp with |
| 58 |
| Number n, RawNumInterp interp -> interp ?loc n |
| 59 |
| Number n, BigNumInterp interp -> |
| 60 |
(match NumTok.Signed.to_bigint n with |
| 61 |
| Some n -> interp ?loc n |
| 62 |
| None -> raise Not_found) |
| 63 |
| String s, StringInterp interp -> interp ?loc s |
| 64 |
| (Number _ | String _), |
| 65 |
(RawNumInterp _ | BigNumInterp _ | StringInterp _) -> raise Not_found |
| 66 |
|
| 67 |
type uninterpreter = |
| 68 |
| RawNumUninterp of (any_glob_constr -> rawnum option) |
| 69 |
| BigNumUninterp of (any_glob_constr -> Z.t option) |
| 70 |
| StringUninterp of (any_glob_constr -> string option) |
| 71 |
|
| 72 |
let uninterp_eq f f' = match f,f' with |
| 73 |
| RawNumUninterp f, RawNumUninterp f' -> f == f' |
| 74 |
| BigNumUninterp f, BigNumUninterp f' -> f == f' |
| 75 |
| StringUninterp f, StringUninterp f' -> f == f' |
| 76 |
| _ -> false |
| 77 |
|
| 78 |
let mkNumber n = |
| 79 |
Number (NumTok.Signed.of_bigint CDec n) |
| 80 |
|
| 81 |
let mkString = function |
| 82 |
| None -> None |
| 83 |
| Some s -> if Unicode.is_utf8 s then Some (String s) else None |
| 84 |
|
| 85 |
let do_uninterp uninterp g = match uninterp with |
| 86 |
| RawNumUninterp u -> Option.map (fun (s,n) -> Number (s,n)) (u g) |
| 87 |
| BigNumUninterp u -> Option.map mkNumber (u g) |
| 88 |
| StringUninterp u -> mkString (u g) |
| 89 |
|
| 90 |
end |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
let prim_token_interpreters = |
| 101 |
(Hashtbl.create 7 : (prim_token_uid, InnerPrimToken.interpreter) Hashtbl.t) |
| 102 |
|
| 103 |
let prim_token_uninterpreters = |
| 104 |
(Hashtbl.create 7 : (prim_token_uid, InnerPrimToken.uninterpreter) Hashtbl.t) |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
type prim_token_notation_error = |
| 109 |
| UnexpectedTerm of Constr.t |
| 110 |
| UnexpectedNonOptionTerm of Constr.t |
| 111 |
|
| 112 |
exception PrimTokenNotationError of string * Environ.env * Evd.evar_map * prim_token_notation_error |
| 113 |
|
| 114 |
type numnot_option = |
| 115 |
| Nop |
| 116 |
| Warning of NumTok.UnsignedNat.t |
| 117 |
| Abstract of NumTok.UnsignedNat.t |
| 118 |
|
| 119 |
type int_ty = |
| 120 |
{ dec_uint : Names.inductive; |
| 121 |
dec_int : Names.inductive; |
| 122 |
hex_uint : Names.inductive; |
| 123 |
hex_int : Names.inductive; |
| 124 |
uint : Names.inductive; |
| 125 |
int : Names.inductive } |
| 126 |
|
| 127 |
type z_pos_ty = |
| 128 |
{ z_ty : Names.inductive; |
| 129 |
pos_ty : Names.inductive } |
| 130 |
|
| 131 |
type number_ty = |
| 132 |
{ int : int_ty; |
| 133 |
decimal : Names.inductive; |
| 134 |
hexadecimal : Names.inductive; |
| 135 |
number : Names.inductive } |
| 136 |
|
| 137 |
type pos_neg_int63_ty = |
| 138 |
{ pos_neg_int63_ty : Names.inductive } |
| 139 |
|
| 140 |
type target_kind = |
| 141 |
| Int of int_ty |
| 142 |
| UInt of int_ty |
| 143 |
| Z of z_pos_ty |
| 144 |
| Int63 of pos_neg_int63_ty |
| 145 |
| Float64 |
| 146 |
| Number of number_ty |
| 147 |
|
| 148 |
type string_target_kind = |
| 149 |
| ListByte |
| 150 |
| Byte |
| 151 |
| PString |
| 152 |
|
| 153 |
type option_kind = Direct | Option | Error |
| 154 |
type 'target conversion_kind = 'target * option_kind |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
type to_post_arg = ToPostCopy | ToPostAs of int | ToPostHole of Id.t | ToPostCheck of Constr.t |
| 172 |
type ('target, 'warning) prim_token_notation_obj = |
| 173 |
{ to_kind : 'target conversion_kind; |
| 174 |
to_ty : GlobRef.t; |
| 175 |
to_post : ((GlobRef.t * GlobRef.t * to_post_arg list) list) array; |
| 176 |
of_kind : 'target conversion_kind; |
| 177 |
of_ty : GlobRef.t; |
| 178 |
ty_name : Libnames.qualid; |
| 179 |
warning : 'warning } |
| 180 |
|
| 181 |
type number_notation_obj = (target_kind, numnot_option) prim_token_notation_obj |
| 182 |
type string_notation_obj = (string_target_kind, unit) prim_token_notation_obj |
| 183 |
|
| 184 |
type 'a token_kind = |
| 185 |
| TVar of Id.t * 'a list |
| 186 |
| TSort of Sorts.t |
| 187 |
| TConst of Constant.t * 'a list |
| 188 |
| TInd of inductive * 'a list |
| 189 |
| TConstruct of constructor * 'a list |
| 190 |
| TInt of Uint63.t |
| 191 |
| TFloat of Float64.t |
| 192 |
| TString of Pstring.t |
| 193 |
| TArray of 'a array * 'a * 'a |
| 194 |
| TOther |
| 195 |
|
| 196 |
module TokenValue : |
| 197 |
sig |
| 198 |
type t |
| 199 |
val kind : t -> t token_kind |
| 200 |
val make : Environ.env -> Evd.evar_map -> EConstr.unsafe_judgment -> t |
| 201 |
val repr : t -> Constr.t |
| 202 |
end = |
| 203 |
struct |
| 204 |
|
| 205 |
type t = Constr.t |
| 206 |
|
| 207 |
let kind c = |
| 208 |
let hd, args = decompose_app_list c in |
| 209 |
match Constr.kind hd with |
| 210 |
| Var id -> TVar (id, args) |
| 211 |
| Sort s -> TSort s |
| 212 |
| Const (c, _) -> TConst (c, args) |
| 213 |
| Ind (ind, _) -> TInd (ind, args) |
| 214 |
| Construct (c, _) -> TConstruct (c, args) |
| 215 |
| Int i -> TInt i |
| 216 |
| Float f -> TFloat f |
| 217 |
| String s -> TString s |
| 218 |
| Array (_, t, u, v) -> TArray (t, u, v) |
| 219 |
| Rel _ | Meta _ | Evar _ | Cast _ | Prod _ | Lambda _ | LetIn _ | App _ |
| 220 |
| Proj _ | Case _ | Fix _ | CoFix _ -> TOther |
| 221 |
|
| 222 |
let make env sigma c = |
| 223 |
let c' = Tacred.compute env sigma c.Environ.uj_val in |
| 224 |
EConstr.Unsafe.to_constr @@ c' |
| 225 |
|
| 226 |
let repr c = c |
| 227 |
|
| 228 |
end |
| 229 |
|
| 230 |
module PrimTokenNotation = struct |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
let eval_constr env sigma (c : Constr.t) = |
| 251 |
let c = EConstr.of_constr c in |
| 252 |
let sigma, t = Typing.type_of env sigma c in |
| 253 |
TokenValue.make env sigma { Environ.uj_val = c; Environ.uj_type = t } |
| 254 |
|
| 255 |
let eval_constr_app env sigma c1 c2 = |
| 256 |
eval_constr env sigma (mkApp (c1,[| c2 |])) |
| 257 |
|
| 258 |
exception NotAValidPrimToken |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
let constr_of_globref ?(allow_constant=true) env sigma = |
| 271 |
let open EConstr in |
| 272 |
function |
| 273 |
| GlobRef.ConstructRef c -> |
| 274 |
let sigma,c = Evd.fresh_constructor_instance env sigma c in |
| 275 |
sigma,mkConstructU c |
| 276 |
| GlobRef.IndRef c -> |
| 277 |
let sigma,c = Evd.fresh_inductive_instance env sigma c in |
| 278 |
sigma,mkIndU c |
| 279 |
| GlobRef.ConstRef c when allow_constant || Environ.is_primitive_type env c -> |
| 280 |
let sigma,c = Evd.fresh_constant_instance env sigma c in |
| 281 |
sigma,mkConstU c |
| 282 |
| _ -> raise NotAValidPrimToken |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
let rec check_glob env sigma g c = |
| 288 |
let open EConstr in |
| 289 |
match DAst.get g, Constr.kind c with |
| 290 |
| Glob_term.GRef (GlobRef.ConstructRef c as g, _), Constr.Construct (c', _) |
| 291 |
when Environ.QConstruct.equal env c c' -> constr_of_globref env sigma g |
| 292 |
| Glob_term.GRef (GlobRef.IndRef c as g, _), Constr.Ind (c', _) |
| 293 |
when Environ.QInd.equal env c c' -> constr_of_globref env sigma g |
| 294 |
| Glob_term.GRef (GlobRef.ConstRef c as g, _), Constr.Const (c', _) |
| 295 |
when Environ.QConstant.equal env c c' -> constr_of_globref env sigma g |
| 296 |
| Glob_term.GApp (gc, gcl), Constr.App (gc', gc'a) -> |
| 297 |
let sigma,c = check_glob env sigma gc gc' in |
| 298 |
let sigma,cl = |
| 299 |
try List.fold_left2_map (check_glob env) sigma gcl (Array.to_list gc'a) |
| 300 |
with Invalid_argument _ -> raise NotAValidPrimToken in |
| 301 |
sigma, mkApp (c, Array.of_list cl) |
| 302 |
| Glob_term.GInt i, Constr.Int i' when Uint63.equal i i' -> sigma, mkInt i |
| 303 |
| Glob_term.GFloat f, Constr.Float f' when Float64.equal f f' -> sigma, mkFloat f |
| 304 |
| Glob_term.GString s, Constr.String s' when Pstring.equal s s' -> sigma, mkString s |
| 305 |
| Glob_term.GArray (_,t,def,ty), Constr.Array (_,t',def',ty') -> |
| 306 |
let sigma,u = Evd.fresh_array_instance env sigma in |
| 307 |
let sigma,def = check_glob env sigma def def' in |
| 308 |
let sigma,t = |
| 309 |
try Array.fold_left2_map (check_glob env) sigma t t' |
| 310 |
with Invalid_argument _ -> raise NotAValidPrimToken in |
| 311 |
let sigma,ty = check_glob env sigma ty ty' in |
| 312 |
sigma, mkArray (u,t,def,ty) |
| 313 |
| Glob_term.GSort s, Constr.Sort s' -> |
| 314 |
let sigma,s = Glob_ops.fresh_glob_sort_in_quality sigma s in |
| 315 |
let s' = ESorts.make s' in |
| 316 |
if not (ESorts.equal sigma s s') then raise NotAValidPrimToken; |
| 317 |
sigma,mkSort s |
| 318 |
| _ -> raise NotAValidPrimToken |
| 319 |
|
| 320 |
let rec constr_of_glob to_post post env sigma g = |
| 321 |
let open EConstr in |
| 322 |
match DAst.get g with |
| 323 |
| Glob_term.GRef (r, _) -> |
| 324 |
let o = List.find_opt (fun (_,r',_) -> Environ.QGlobRef.equal env r r') post in |
| 325 |
begin match o with |
| 326 |
| None -> constr_of_globref ~allow_constant:false env sigma r |
| 327 |
| Some (r, _, a) -> |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
if List.exists (function ToPostHole _ -> false | _ -> true) a then raise NotAValidPrimToken; |
| 332 |
constr_of_globref env sigma r |
| 333 |
end |
| 334 |
| Glob_term.GApp (gc, gcl) -> |
| 335 |
let o = match DAst.get gc with |
| 336 |
| Glob_term.GRef (r, _) -> List.find_opt (fun (_,r',_) -> Environ.QGlobRef.equal env r r') post |
| 337 |
| _ -> None in |
| 338 |
begin match o with |
| 339 |
| None -> |
| 340 |
let sigma,c = constr_of_glob to_post post env sigma gc in |
| 341 |
let sigma,cl = List.fold_left_map (constr_of_glob to_post post env) sigma gcl in |
| 342 |
sigma,mkApp (c, Array.of_list cl) |
| 343 |
| Some (r, _, a) -> |
| 344 |
let sigma,c = constr_of_globref env sigma r in |
| 345 |
let rec aux sigma a gcl = match a, gcl with |
| 346 |
| [], [] -> sigma,[] |
| 347 |
| ToPostCopy :: a, gc :: gcl -> |
| 348 |
let sigma,c = constr_of_glob [||] [] env sigma gc in |
| 349 |
let sigma,cl = aux sigma a gcl in |
| 350 |
sigma, c :: cl |
| 351 |
| ToPostCheck r :: a, gc :: gcl -> |
| 352 |
let sigma,c = check_glob env sigma gc r in |
| 353 |
let sigma,cl = aux sigma a gcl in |
| 354 |
sigma, c :: cl |
| 355 |
| ToPostAs i :: a, gc :: gcl -> |
| 356 |
let sigma,c = constr_of_glob to_post to_post.(i) env sigma gc in |
| 357 |
let sigma,cl = aux sigma a gcl in |
| 358 |
sigma, c :: cl |
| 359 |
| ToPostHole _ :: post, _ :: gcl -> aux sigma post gcl |
| 360 |
| [], _ :: _ | _ :: _, [] -> raise NotAValidPrimToken |
| 361 |
in |
| 362 |
let sigma,cl = aux sigma a gcl in |
| 363 |
sigma,mkApp (c, Array.of_list cl) |
| 364 |
end |
| 365 |
| Glob_term.GInt i -> sigma, mkInt i |
| 366 |
| Glob_term.GFloat f -> sigma, mkFloat f |
| 367 |
| Glob_term.GString s -> sigma, mkString s |
| 368 |
| Glob_term.GArray (_,t,def,ty) -> |
| 369 |
let sigma, u' = Evd.fresh_array_instance env sigma in |
| 370 |
let sigma, def' = constr_of_glob to_post post env sigma def in |
| 371 |
let sigma, t' = Array.fold_left_map (constr_of_glob to_post post env) sigma t in |
| 372 |
let sigma, ty' = constr_of_glob to_post post env sigma ty in |
| 373 |
sigma, mkArray (u',t',def',ty') |
| 374 |
| Glob_term.GSort gs -> |
| 375 |
let sigma,c = Glob_ops.fresh_glob_sort_in_quality sigma gs in |
| 376 |
sigma,mkSort c |
| 377 |
| _ -> |
| 378 |
raise NotAValidPrimToken |
| 379 |
|
| 380 |
let constr_of_glob to_post env sigma (Glob_term.AnyGlobConstr g) = |
| 381 |
let post = match to_post with [||] -> [] | _ -> to_post.(0) in |
| 382 |
constr_of_glob to_post post env sigma g |
| 383 |
|
| 384 |
let rec glob_of_constr token_kind ?loc env sigma c = match Constr.kind c with |
| 385 |
| App (c, ca) -> |
| 386 |
let c = glob_of_constr token_kind ?loc env sigma c in |
| 387 |
let cel = List.map (glob_of_constr token_kind ?loc env sigma) (Array.to_list ca) in |
| 388 |
DAst.make ?loc (Glob_term.GApp (c, cel)) |
| 389 |
| Construct (c, _) -> DAst.make ?loc (Glob_term.GRef (GlobRef.ConstructRef c, None)) |
| 390 |
| Const (c, _) -> DAst.make ?loc (Glob_term.GRef (GlobRef.ConstRef c, None)) |
| 391 |
| Ind (ind, _) -> DAst.make ?loc (Glob_term.GRef (GlobRef.IndRef ind, None)) |
| 392 |
| Var id -> DAst.make ?loc (Glob_term.GRef (GlobRef.VarRef id, None)) |
| 393 |
| Int i -> DAst.make ?loc (Glob_term.GInt i) |
| 394 |
| Float f -> DAst.make ?loc (Glob_term.GFloat f) |
| 395 |
| String s -> DAst.make ?loc (Glob_term.GString s) |
| 396 |
| Array (u,t,def,ty) -> |
| 397 |
let def' = glob_of_constr token_kind ?loc env sigma def |
| 398 |
and t' = Array.map (glob_of_constr token_kind ?loc env sigma) t |
| 399 |
and ty' = glob_of_constr token_kind ?loc env sigma ty in |
| 400 |
DAst.make ?loc (Glob_term.GArray (None,t',def',ty')) |
| 401 |
| Sort Sorts.SProp -> DAst.make ?loc (Glob_term.GSort Glob_ops.glob_SProp_sort) |
| 402 |
| Sort Sorts.Prop -> DAst.make ?loc (Glob_term.GSort Glob_ops.glob_Prop_sort) |
| 403 |
| Sort Sorts.Set -> DAst.make ?loc (Glob_term.GSort Glob_ops.glob_Set_sort) |
| 404 |
| Sort (Sorts.Type _) -> DAst.make ?loc (Glob_term.GSort Glob_ops.glob_Type_sort) |
| 405 |
| _ -> Loc.raise ?loc (PrimTokenNotationError(token_kind,env,sigma,UnexpectedTerm c)) |
| 406 |
|
| 407 |
let mkGApp ?loc hd args = match args with |
| 408 |
| [] -> hd |
| 409 |
| _ :: _ -> mkGApp ?loc hd args |
| 410 |
|
| 411 |
let rec glob_of_token token_kind ?loc env sigma c = match TokenValue.kind c with |
| 412 |
| TConstruct (c, l) -> |
| 413 |
let ce = DAst.make ?loc (Glob_term.GRef (GlobRef.ConstructRef c, None)) in |
| 414 |
let cel = List.map (glob_of_token token_kind ?loc env sigma) l in |
| 415 |
mkGApp ?loc ce cel |
| 416 |
| TConst (c, l) -> |
| 417 |
let ce = DAst.make ?loc (Glob_term.GRef (GlobRef.ConstRef c, None)) in |
| 418 |
let cel = List.map (glob_of_token token_kind ?loc env sigma) l in |
| 419 |
mkGApp ?loc ce cel |
| 420 |
| TInd (ind, l) -> |
| 421 |
let ce = DAst.make ?loc (Glob_term.GRef (GlobRef.IndRef ind, None)) in |
| 422 |
let cel = List.map (glob_of_token token_kind ?loc env sigma) l in |
| 423 |
mkGApp ?loc ce cel |
| 424 |
| TVar (id, l) -> |
| 425 |
let ce = DAst.make ?loc (Glob_term.GRef (GlobRef.VarRef id, None)) in |
| 426 |
let cel = List.map (glob_of_token token_kind ?loc env sigma) l in |
| 427 |
mkGApp ?loc ce cel |
| 428 |
| TInt i -> DAst.make ?loc (Glob_term.GInt i) |
| 429 |
| TFloat f -> DAst.make ?loc (Glob_term.GFloat f) |
| 430 |
| TString s -> DAst.make ?loc (Glob_term.GString s) |
| 431 |
| TArray (t,def,ty) -> |
| 432 |
let def' = glob_of_token token_kind ?loc env sigma def |
| 433 |
and t' = Array.map (glob_of_token token_kind ?loc env sigma) t |
| 434 |
and ty' = glob_of_token token_kind ?loc env sigma ty in |
| 435 |
DAst.make ?loc (Glob_term.GArray (None,t',def',ty')) |
| 436 |
| TSort Sorts.SProp -> DAst.make ?loc (Glob_term.GSort Glob_ops.glob_SProp_sort) |
| 437 |
| TSort Sorts.Prop -> DAst.make ?loc (Glob_term.GSort Glob_ops.glob_Prop_sort) |
| 438 |
| TSort Sorts.Set -> DAst.make ?loc (Glob_term.GSort Glob_ops.glob_Set_sort) |
| 439 |
| TSort (Sorts.Type _ | Sorts.QSort _) -> DAst.make ?loc (Glob_term.GSort Glob_ops.glob_Type_sort) |
| 440 |
| TOther -> |
| 441 |
let c = TokenValue.repr c in |
| 442 |
Loc.raise ?loc (PrimTokenNotationError(token_kind,env,sigma,UnexpectedTerm c)) |
| 443 |
|
| 444 |
let no_such_prim_token uninterpreted_token_kind ?loc ?errmsg ty = |
| 445 |
CErrors.user_err ?loc |
| 446 |
(str ("Cannot interpret this "^uninterpreted_token_kind^" as a value of type ") ++ |
| 447 |
pr_qualid ty ++ |
| 448 |
pr_opt (fun errmsg -> surround errmsg) errmsg) |
| 449 |
|
| 450 |
let rec postprocess env token_kind ?loc ty to_post post g = |
| 451 |
let g', gl = match DAst.get g with Glob_term.GApp (g, gl) -> g, gl | _ -> g, [] in |
| 452 |
let o = |
| 453 |
match DAst.get g' with |
| 454 |
| Glob_term.GRef (r, None) -> |
| 455 |
List.find_opt (fun (r',_,_) -> Environ.QGlobRef.equal env r r') post |
| 456 |
| _ -> None in |
| 457 |
match o with None -> g | Some (_, r, a) -> |
| 458 |
let rec f n a gl = match a, gl with |
| 459 |
| [], [] -> [] |
| 460 |
| ToPostHole id :: a, gl -> |
| 461 |
let e = GImplicitArg (r, (n, id), true) in |
| 462 |
let h = DAst.make ?loc (Glob_term.GHole e) in |
| 463 |
h :: f (n+1) a gl |
| 464 |
| (ToPostCopy | ToPostCheck _) :: a, g :: gl -> g :: f (n+1) a gl |
| 465 |
| ToPostAs c :: a, g :: gl -> |
| 466 |
postprocess env token_kind ?loc ty to_post to_post.(c) g :: f (n+1) a gl |
| 467 |
| [], _::_ | _::_, [] -> |
| 468 |
no_such_prim_token token_kind ?loc ty |
| 469 |
in |
| 470 |
let gl = f 1 a gl in |
| 471 |
let g = DAst.make ?loc (Glob_term.GRef (r, None)) in |
| 472 |
DAst.make ?loc (Glob_term.GApp (g, gl)) |
| 473 |
|
| 474 |
let glob_of_constr token_kind ty ?loc env sigma to_post c = |
| 475 |
let g = glob_of_constr token_kind ?loc env sigma c in |
| 476 |
match to_post with [||] -> g | _ -> |
| 477 |
postprocess env token_kind ?loc ty to_post to_post.(0) g |
| 478 |
|
| 479 |
let glob_of_token token_kind ty ?loc env sigma to_post c = |
| 480 |
let g = glob_of_token token_kind ?loc env sigma c in |
| 481 |
match to_post with [||] -> g | _ -> |
| 482 |
postprocess env token_kind ?loc ty to_post to_post.(0) g |
| 483 |
|
| 484 |
let interp_option uninterpreted_token_kind token_kind ty ?loc env sigma to_post c = |
| 485 |
match TokenValue.kind c with |
| 486 |
| TConstruct (_Some, [_; c]) -> glob_of_token token_kind ty ?loc env sigma to_post c |
| 487 |
| TConstruct (_None, [_]) -> no_such_prim_token uninterpreted_token_kind ?loc ty |
| 488 |
| x -> |
| 489 |
let c = TokenValue.repr c in |
| 490 |
Loc.raise ?loc (PrimTokenNotationError(token_kind,env,sigma,UnexpectedNonOptionTerm c)) |
| 491 |
|
| 492 |
let interp_error uninterpreted_token_kind token_kind ty ?loc env sigma to_post c = |
| 493 |
match TokenValue.kind c with |
| 494 |
| TConstruct ((_, 1), [_; _; c]) -> glob_of_token token_kind ty ?loc env sigma to_post c |
| 495 |
| TConstruct ((_, 2), [_; _; msg]) -> |
| 496 |
let errmsg = |
| 497 |
Termops.Internal.print_constr_env env sigma |
| 498 |
(EConstr.of_constr @@ TokenValue.repr msg) |
| 499 |
in |
| 500 |
no_such_prim_token uninterpreted_token_kind ?loc ~errmsg ty |
| 501 |
| x -> |
| 502 |
let c = TokenValue.repr c in |
| 503 |
Loc.raise ?loc (PrimTokenNotationError(token_kind,env,sigma,UnexpectedNonOptionTerm c)) |
| 504 |
|
| 505 |
let uninterp_option c = |
| 506 |
match TokenValue.kind c with |
| 507 |
| TConstruct (_Some, [_; x]) -> x |
| 508 |
| _ -> raise NotAValidPrimToken |
| 509 |
|
| 510 |
let uninterp_error c = |
| 511 |
match TokenValue.kind c with |
| 512 |
| TConstruct ((_, 1), [_; _; x]) -> x |
| 513 |
| _ -> raise NotAValidPrimToken |
| 514 |
|
| 515 |
let uninterp to_raw o n = |
| 516 |
let env = Global.env () in |
| 517 |
let sigma = Evd.from_env env in |
| 518 |
let sigma,of_ty = Evd.fresh_global env sigma o.of_ty in |
| 519 |
let of_ty = EConstr.Unsafe.to_constr of_ty in |
| 520 |
try |
| 521 |
let sigma,n = constr_of_glob o.to_post env sigma n in |
| 522 |
let n = EConstr.Unsafe.to_constr n in |
| 523 |
let c = eval_constr_app env sigma of_ty n in |
| 524 |
let c = match snd o.of_kind with |
| 525 |
| Direct -> c |
| 526 |
| Option -> uninterp_option c |
| 527 |
| Error -> uninterp_error c |
| 528 |
in |
| 529 |
Some (to_raw (fst o.of_kind, c)) |
| 530 |
with |
| 531 |
| Type_errors.TypeError _ | Pretype_errors.PretypeError _ -> None |
| 532 |
| NotAValidPrimToken -> None |
| 533 |
|
| 534 |
end |
| 535 |
|
| 536 |
let z_two = Z.of_int 2 |
| 537 |
|
| 538 |
|
| 539 |
let int63_of_pos_bigint i = Uint63.of_int64 (Z.to_int64 i) |
| 540 |
|
| 541 |
module Numbers = struct |
| 542 |
|
| 543 |
open PrimTokenNotation |
| 544 |
|
| 545 |
let warn_large_num = |
| 546 |
CWarnings.create ~name:"large-number" ~category:CWarnings.CoreCategories.numbers |
| 547 |
(fun ty -> |
| 548 |
strbrk "Stack overflow or segmentation fault happens when " ++ |
| 549 |
strbrk "working with large numbers in " ++ pr_qualid ty ++ |
| 550 |
strbrk " (threshold may vary depending" ++ |
| 551 |
strbrk " on your system limits and on the command executed).") |
| 552 |
|
| 553 |
let warn_abstract_large_num = |
| 554 |
CWarnings.create ~name:"abstract-large-number" ~category:CWarnings.CoreCategories.numbers |
| 555 |
(fun (ty,f) -> |
| 556 |
strbrk "To avoid stack overflow, large numbers in " ++ |
| 557 |
pr_qualid ty ++ strbrk " are interpreted as applications of " ++ |
| 558 |
Termops.pr_global_env (Global.env ()) f ++ strbrk ".") |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
let digit_of_char c = |
| 567 |
assert ('0' <= c && c <= '9' || 'a' <= c && c <= 'f'); |
| 568 |
if c <= '9' then Char.code c - Char.code '0' + 2 |
| 569 |
else Char.code c - Char.code 'a' + 12 |
| 570 |
|
| 571 |
let char_of_digit n = |
| 572 |
assert (2<=n && n<=17); |
| 573 |
if n <= 11 then Char.chr (n-2 + Char.code '0') |
| 574 |
else Char.chr (n-12 + Char.code 'a') |
| 575 |
|
| 576 |
let rocquint_of_rawnum esig inds c n = |
| 577 |
let uint = match c with CDec -> inds.dec_uint | CHex -> inds.hex_uint in |
| 578 |
let nil = mkConstruct esig (uint,1) in |
| 579 |
match n with None -> nil | Some n -> |
| 580 |
let str = NumTok.UnsignedNat.to_string n in |
| 581 |
let str = match c with |
| 582 |
| CDec -> str |
| 583 |
| CHex -> String.sub str 2 (String.length str - 2) in |
| 584 |
let rec do_chars s i acc = |
| 585 |
if i < 0 then acc |
| 586 |
else |
| 587 |
let dg = mkConstruct esig (uint, digit_of_char s.[i]) in |
| 588 |
do_chars s (i-1) (mkApp(dg,[|acc|])) |
| 589 |
in |
| 590 |
do_chars str (String.length str - 1) nil |
| 591 |
|
| 592 |
let rocqint_of_rawnum esig inds c (sign,n) = |
| 593 |
let ind = match c with CDec -> inds.dec_int | CHex -> inds.hex_int in |
| 594 |
let uint = rocquint_of_rawnum esig inds c (Some n) in |
| 595 |
let pos_neg = match sign with SPlus -> 1 | SMinus -> 2 in |
| 596 |
mkApp (mkConstruct esig (ind, pos_neg), [|uint|]) |
| 597 |
|
| 598 |
let rocqnumber_of_rawnum esig inds c n = |
| 599 |
let ind = match c with CDec -> inds.decimal | CHex -> inds.hexadecimal in |
| 600 |
let i, f, e = NumTok.Signed.to_int_frac_and_exponent n in |
| 601 |
let i = rocqint_of_rawnum esig inds.int c i in |
| 602 |
let f = rocquint_of_rawnum esig inds.int c f in |
| 603 |
match e with |
| 604 |
| None -> mkApp (mkConstruct esig (ind, 1), [|i; f|]) |
| 605 |
| Some e -> |
| 606 |
let e = rocqint_of_rawnum esig inds.int CDec e in |
| 607 |
mkApp (mkConstruct esig (ind, 2), [|i; f; e|]) |
| 608 |
|
| 609 |
let mkDecHex esig ind c n = match c with |
| 610 |
| CDec -> mkApp (mkConstruct esig (ind, 1), [|n|]) |
| 611 |
| CHex -> mkApp (mkConstruct esig (ind, 2), [|n|]) |
| 612 |
|
| 613 |
let rocqnumber_of_rawnum esig inds n = |
| 614 |
let c = NumTok.Signed.classify n in |
| 615 |
let n = rocqnumber_of_rawnum esig inds c n in |
| 616 |
mkDecHex esig inds.number c n |
| 617 |
|
| 618 |
let rocquint_of_rawnum esig inds n = |
| 619 |
let c = NumTok.UnsignedNat.classify n in |
| 620 |
let n = rocquint_of_rawnum esig inds c (Some n) in |
| 621 |
mkDecHex esig inds.uint c n |
| 622 |
|
| 623 |
let rocqint_of_rawnum esig inds n = |
| 624 |
let c = NumTok.SignedNat.classify n in |
| 625 |
let n = rocqint_of_rawnum esig inds c n in |
| 626 |
mkDecHex esig inds.int c n |
| 627 |
|
| 628 |
let rawnum_of_rocquint cl c = |
| 629 |
let rec of_uint_loop c buf = |
| 630 |
match TokenValue.kind c with |
| 631 |
| TConstruct ((_, 1), _) -> () |
| 632 |
| TConstruct ((_, n), [a]) -> |
| 633 |
let () = Buffer.add_char buf (char_of_digit n) in |
| 634 |
of_uint_loop a buf |
| 635 |
| _ -> raise NotAValidPrimToken |
| 636 |
in |
| 637 |
let buf = Buffer.create 64 in |
| 638 |
if cl = CHex then (Buffer.add_char buf '0'; Buffer.add_char buf 'x'); |
| 639 |
let () = of_uint_loop c buf in |
| 640 |
if Int.equal (Buffer.length buf) (match cl with CDec -> 0 | CHex -> 2) then |
| 641 |
|
| 642 |
|
| 643 |
raise NotAValidPrimToken |
| 644 |
else NumTok.UnsignedNat.of_string (Buffer.contents buf) |
| 645 |
|
| 646 |
let rawnum_of_rocqint cl c = |
| 647 |
match TokenValue.kind c with |
| 648 |
| TConstruct ((_, 1), [c']) -> (SPlus, rawnum_of_rocquint cl c') |
| 649 |
| TConstruct ((_, 2), [c']) -> (SMinus, rawnum_of_rocquint cl c') |
| 650 |
| _ -> raise NotAValidPrimToken |
| 651 |
|
| 652 |
let rawnum_of_rocqnumber cl c = |
| 653 |
let of_ife i f e = |
| 654 |
let n = rawnum_of_rocqint cl i in |
| 655 |
let f = try Some (rawnum_of_rocquint cl f) with NotAValidPrimToken -> None in |
| 656 |
let e = match e with None -> None | Some e -> Some (rawnum_of_rocqint CDec e) in |
| 657 |
NumTok.Signed.of_int_frac_and_exponent n f e in |
| 658 |
match TokenValue.kind c with |
| 659 |
| TConstruct (_, [i; f]) -> of_ife i f None |
| 660 |
| TConstruct (_, [i; f; e]) -> of_ife i f (Some e) |
| 661 |
| _ -> raise NotAValidPrimToken |
| 662 |
|
| 663 |
let destDecHex c = match TokenValue.kind c with |
| 664 |
| TConstruct ((_, 1), [c']) -> CDec, c' |
| 665 |
| TConstruct ((_, 2), [c']) -> CHex, c' |
| 666 |
| _ -> raise NotAValidPrimToken |
| 667 |
|
| 668 |
let rawnum_of_rocqnumber c = |
| 669 |
let cl, c = destDecHex c in |
| 670 |
rawnum_of_rocqnumber cl c |
| 671 |
|
| 672 |
let rawnum_of_rocquint c = |
| 673 |
let cl, c = destDecHex c in |
| 674 |
rawnum_of_rocquint cl c |
| 675 |
|
| 676 |
let rawnum_of_rocqint c = |
| 677 |
let cl, c = destDecHex c in |
| 678 |
rawnum_of_rocqint cl c |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
let rec pos_of_bigint esig posty n = |
| 687 |
match Z.div_rem n z_two with |
| 688 |
| (q, rem) when rem = Z.zero -> |
| 689 |
let c = mkConstruct esig (posty, 2) in |
| 690 |
mkApp (c, [| pos_of_bigint esig posty q |]) |
| 691 |
| (q, _) when not (Z.equal q Z.zero) -> |
| 692 |
let c = mkConstruct esig (posty, 1) in |
| 693 |
mkApp (c, [| pos_of_bigint esig posty q |]) |
| 694 |
| (q, _) -> |
| 695 |
mkConstruct esig (posty, 3) |
| 696 |
|
| 697 |
let rec bigint_of_pos c = match TokenValue.kind c with |
| 698 |
| TConstruct ((_, 3), []) -> Z.one |
| 699 |
| TConstruct ((_, 1), [d]) -> Z.add Z.one (Z.mul z_two (bigint_of_pos d)) |
| 700 |
| TConstruct ((_, 2), [d]) -> Z.mul z_two (bigint_of_pos d) |
| 701 |
| x -> raise NotAValidPrimToken |
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
let z_of_bigint esig { z_ty; pos_ty } n = |
| 706 |
if Z.(equal n zero) then |
| 707 |
mkConstruct esig (z_ty, 1) |
| 708 |
else |
| 709 |
let (s, n) = |
| 710 |
if Z.(leq zero n) then (2, n) |
| 711 |
else (3, Z.neg n) |
| 712 |
in |
| 713 |
let c = mkConstruct esig (z_ty, s) in |
| 714 |
mkApp (c, [| pos_of_bigint esig pos_ty n |]) |
| 715 |
|
| 716 |
let bigint_of_z z = match TokenValue.kind z with |
| 717 |
| TConstruct ((_, 1), []) -> Z.zero |
| 718 |
| TConstruct ((_, 2), [d]) -> bigint_of_pos d |
| 719 |
| TConstruct ((_, 3), [d]) -> Z.neg (bigint_of_pos d) |
| 720 |
| _ -> raise NotAValidPrimToken |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
let int63_of_pos_bigint ?loc n = |
| 725 |
let i = int63_of_pos_bigint n in |
| 726 |
mkInt i |
| 727 |
|
| 728 |
let error_overflow ?loc n = |
| 729 |
CErrors.user_err ?loc Pp.(str "Overflow in int63 literal: " ++ str (Z.to_string n) |
| 730 |
++ str ".") |
| 731 |
|
| 732 |
let rocqpos_neg_int63_of_bigint ?loc esig ind (sign,n) = |
| 733 |
let uint = int63_of_pos_bigint ?loc n in |
| 734 |
let pos_neg = match sign with SPlus -> 1 | SMinus -> 2 in |
| 735 |
mkApp (mkConstruct esig (ind, pos_neg), [|uint|]) |
| 736 |
|
| 737 |
let interp_int63 ?loc esig ind n = |
| 738 |
let sign = if Z.(compare n zero >= 0) then SPlus else SMinus in |
| 739 |
let an = Z.abs n in |
| 740 |
if Z.(lt an (pow z_two 63)) |
| 741 |
then rocqpos_neg_int63_of_bigint ?loc esig ind (sign,an) |
| 742 |
else error_overflow ?loc n |
| 743 |
|
| 744 |
let warn_inexact_float = |
| 745 |
CWarnings.create ~name:"inexact-float" ~category:CWarnings.CoreCategories.parsing |
| 746 |
(fun (sn, f) -> |
| 747 |
Pp.strbrk |
| 748 |
(Printf.sprintf |
| 749 |
"The constant %s is not a binary64 floating-point value. \ |
| 750 |
A closest value %s will be used and unambiguously printed %s." |
| 751 |
sn (Float64.to_hex_string f) (Float64.to_string f))) |
| 752 |
|
| 753 |
let interp_float64 ?loc n = |
| 754 |
let sn = NumTok.Signed.to_string n in |
| 755 |
let f = Float64.of_string sn in |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
let inexact () = match Float64.classify f with |
| 760 |
| Float64.(PInf | NInf | NaN) -> true |
| 761 |
| Float64.(PZero | NZero) -> not (NumTok.Signed.is_zero n) |
| 762 |
| Float64.(PNormal | NNormal | PSubn | NSubn) -> |
| 763 |
let m, e = |
| 764 |
let (_, i), f, e = NumTok.Signed.to_int_frac_and_exponent n in |
| 765 |
let i = NumTok.UnsignedNat.to_string i in |
| 766 |
let f = match f with |
| 767 |
| None -> "" | Some f -> NumTok.UnsignedNat.to_string f in |
| 768 |
let e = match e with |
| 769 |
| None -> "0" | Some e -> NumTok.SignedNat.to_string e in |
| 770 |
Z.of_string (i ^ f), |
| 771 |
(try int_of_string e with Failure _ -> 0) - String.length f in |
| 772 |
let m', e' = |
| 773 |
let m', e' = Float64.frshiftexp f in |
| 774 |
let m' = Float64.normfr_mantissa m' in |
| 775 |
let e' = Uint63.to_int_min e' 4096 - Float64.eshift - 53 in |
| 776 |
Z.of_string (Uint63.to_string m'), |
| 777 |
e' in |
| 778 |
let c2, c5 = Z.(of_int 2, of_int 5) in |
| 779 |
|
| 780 |
let check m e m' e' = |
| 781 |
not (Z.(equal (mul m (pow c5 e)) (mul m' (pow c2 e')))) in |
| 782 |
|
| 783 |
let check' m e e' m' = |
| 784 |
not (Z.(equal (mul (mul m (pow c5 e)) (pow c2 e')) m')) in |
| 785 |
|
| 786 |
if e >= 0 then |
| 787 |
if e <= e' then check m e m' (e' - e) |
| 788 |
else check' m e (e - e') m' |
| 789 |
else |
| 790 |
if e' <= e then check m' (-e) m (e - e') |
| 791 |
else check' m' (-e) (e' - e) m in |
| 792 |
if NumTok.(Signed.classify n = CDec) && inexact () then |
| 793 |
warn_inexact_float ?loc (sn, f); |
| 794 |
mkFloat f |
| 795 |
|
| 796 |
let bigint_of_int63 c = match TokenValue.kind c with |
| 797 |
| TInt i -> Z.of_int64 (Uint63.to_int64 i) |
| 798 |
| _ -> raise NotAValidPrimToken |
| 799 |
|
| 800 |
let bigint_of_rocqpos_neg_int63 c = match TokenValue.kind c with |
| 801 |
| TConstruct ((_, 1), [c']) -> bigint_of_int63 c' |
| 802 |
| TConstruct ((_, 2), [c']) -> Z.neg (bigint_of_int63 c') |
| 803 |
| _ -> raise NotAValidPrimToken |
| 804 |
|
| 805 |
let uninterp_float64 ~print_float c = match TokenValue.kind c with |
| 806 |
| TFloat f when not (Float64.is_infinity f || Float64.is_neg_infinity f |
| 807 |
|| Float64.is_nan f) && print_float -> |
| 808 |
NumTok.Signed.of_string (Float64.to_string f) |
| 809 |
| _ -> raise NotAValidPrimToken |
| 810 |
|
| 811 |
let interp o ?loc n = |
| 812 |
begin match o.warning, n with |
| 813 |
| Warning threshold, n when NumTok.Signed.is_bigger_int_than n threshold -> |
| 814 |
warn_large_num o.ty_name |
| 815 |
| _ -> () |
| 816 |
end; |
| 817 |
let env = Global.env () in |
| 818 |
let sigma = ref (Evd.from_env env) in |
| 819 |
let esig = env, sigma in |
| 820 |
let c = match fst o.to_kind, NumTok.Signed.to_int n with |
| 821 |
| Int int_ty, Some n -> |
| 822 |
rocqint_of_rawnum esig int_ty n |
| 823 |
| UInt int_ty, Some (SPlus, n) -> |
| 824 |
rocquint_of_rawnum esig int_ty n |
| 825 |
| Z z_pos_ty, Some n -> |
| 826 |
z_of_bigint esig z_pos_ty (NumTok.SignedNat.to_bigint n) |
| 827 |
| Int63 pos_neg_int63_ty, Some n -> |
| 828 |
interp_int63 ?loc esig pos_neg_int63_ty.pos_neg_int63_ty (NumTok.SignedNat.to_bigint n) |
| 829 |
| (Int _ | UInt _ | Z _ | Int63 _), _ -> |
| 830 |
no_such_prim_token "number" ?loc o.ty_name |
| 831 |
| Float64, _ -> interp_float64 ?loc n |
| 832 |
| Number number_ty, _ -> rocqnumber_of_rawnum esig number_ty n |
| 833 |
in |
| 834 |
let sigma = !sigma in |
| 835 |
let sigma,to_ty = Evd.fresh_global env sigma o.to_ty in |
| 836 |
let to_ty = EConstr.Unsafe.to_constr to_ty in |
| 837 |
match o.warning, snd o.to_kind with |
| 838 |
| Abstract threshold, Direct when NumTok.Signed.is_bigger_int_than n threshold -> |
| 839 |
warn_abstract_large_num (o.ty_name,o.to_ty); |
| 840 |
assert (Array.length o.to_post = 0); |
| 841 |
glob_of_constr "number" o.ty_name ?loc env sigma o.to_post (mkApp (to_ty,[|c|])) |
| 842 |
| _ -> |
| 843 |
let res = eval_constr_app env sigma to_ty c in |
| 844 |
match snd o.to_kind with |
| 845 |
| Direct -> glob_of_token "number" o.ty_name ?loc env sigma o.to_post res |
| 846 |
| Option -> interp_option "number" "number" o.ty_name ?loc env sigma o.to_post res |
| 847 |
| Error -> interp_error "number" "number" o.ty_name ?loc env sigma o.to_post res |
| 848 |
|
| 849 |
let uninterp ~print_float o n = |
| 850 |
PrimTokenNotation.uninterp |
| 851 |
begin function |
| 852 |
| (Int _, c) -> NumTok.Signed.of_int (rawnum_of_rocqint c) |
| 853 |
| (UInt _, c) -> NumTok.Signed.of_nat (rawnum_of_rocquint c) |
| 854 |
| (Z _, c) -> NumTok.Signed.of_bigint CDec (bigint_of_z c) |
| 855 |
| (Int63 _, c) -> NumTok.Signed.of_bigint CDec (bigint_of_rocqpos_neg_int63 c) |
| 856 |
| (Float64, c) -> uninterp_float64 ~print_float c |
| 857 |
| (Number _, c) -> rawnum_of_rocqnumber c |
| 858 |
end o n |
| 859 |
end |
| 860 |
|
| 861 |
module Strings = struct |
| 862 |
|
| 863 |
open PrimTokenNotation |
| 864 |
|
| 865 |
let q_ref n = |
| 866 |
n |> Rocqlib.lib_ref |
| 867 |
|
| 868 |
let q_list () = q_ref "core.list.type" |
| 869 |
let q_byte () = q_ref "core.byte.type" |
| 870 |
|
| 871 |
let force_ind q = |
| 872 |
match q with |
| 873 |
| GlobRef.IndRef i -> i |
| 874 |
| _ -> raise Not_found |
| 875 |
|
| 876 |
let locate_list () = force_ind (q_list ()) |
| 877 |
let locate_byte () = force_ind (q_byte ()) |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
let rocqbyte_of_char_code esig byte c = |
| 884 |
mkConstruct esig (byte, 1 + c) |
| 885 |
|
| 886 |
let rocqbyte_of_string ?loc esig byte s = |
| 887 |
let p = |
| 888 |
if Int.equal (String.length s) 1 then int_of_char s.[0] |
| 889 |
else |
| 890 |
let n = |
| 891 |
if Int.equal (String.length s) 3 && is_digit s.[0] && is_digit s.[1] && is_digit s.[2] |
| 892 |
then int_of_string s else 256 in |
| 893 |
if n < 256 then n else |
| 894 |
user_err ?loc |
| 895 |
(str "Expects a single character or a three-digit ASCII code.") in |
| 896 |
rocqbyte_of_char_code esig byte p |
| 897 |
|
| 898 |
let rocqbyte_of_char esig byte c = rocqbyte_of_char_code esig byte (Char.code c) |
| 899 |
|
| 900 |
let pstring_of_string ?loc s = |
| 901 |
match Pstring.of_string s with |
| 902 |
| Some s -> Constr.mkString s |
| 903 |
| None -> user_err ?loc (str "String literal would be too large on a 32-bits system.") |
| 904 |
|
| 905 |
let make_ascii_string n = |
| 906 |
if n>=32 && n<=126 then String.make 1 (char_of_int n) |
| 907 |
else Printf.sprintf "%03d" n |
| 908 |
|
| 909 |
let char_code_of_rocqbyte c = match TokenValue.kind c with |
| 910 |
| TConstruct ((_,c), []) -> c - 1 |
| 911 |
| _ -> raise NotAValidPrimToken |
| 912 |
|
| 913 |
let string_of_rocqbyte c = make_ascii_string (char_code_of_rocqbyte c) |
| 914 |
|
| 915 |
let string_of_pstring c = |
| 916 |
match TokenValue.kind c with |
| 917 |
| TString s -> Pstring.to_string s |
| 918 |
| _ -> raise NotAValidPrimToken |
| 919 |
|
| 920 |
let rocqlist_byte_of_string esig byte_ty list_ty str = |
| 921 |
let cbyte = mkInd esig byte_ty in |
| 922 |
let nil = mkApp (mkConstruct esig (list_ty, 1), [|cbyte|]) in |
| 923 |
let cons x xs = mkApp (mkConstruct esig (list_ty, 2), [|cbyte; x; xs|]) in |
| 924 |
let rec do_chars s i acc = |
| 925 |
if i < 0 then acc |
| 926 |
else |
| 927 |
let b = rocqbyte_of_char esig byte_ty s.[i] in |
| 928 |
do_chars s (i-1) (cons b acc) |
| 929 |
in |
| 930 |
do_chars str (String.length str - 1) nil |
| 931 |
|
| 932 |
|
| 933 |
let string_of_rocqlist_byte c = |
| 934 |
let rec of_rocqlist_byte_loop c buf = |
| 935 |
match TokenValue.kind c with |
| 936 |
| TConstruct (_nil, [_ty]) -> () |
| 937 |
| TConstruct (_cons, [_ty;b;c]) -> |
| 938 |
let () = Buffer.add_char buf (Char.chr (char_code_of_rocqbyte b)) in |
| 939 |
of_rocqlist_byte_loop c buf |
| 940 |
| _ -> raise NotAValidPrimToken |
| 941 |
in |
| 942 |
let buf = Buffer.create 64 in |
| 943 |
let () = of_rocqlist_byte_loop c buf in |
| 944 |
Buffer.contents buf |
| 945 |
|
| 946 |
let interp o ?loc n = |
| 947 |
let byte_ty = locate_byte () in |
| 948 |
let list_ty = locate_list () in |
| 949 |
let env = Global.env () in |
| 950 |
let sigma = ref (Evd.from_env env) in |
| 951 |
let esig = env, sigma in |
| 952 |
let c = match fst o.to_kind with |
| 953 |
| ListByte -> rocqlist_byte_of_string esig byte_ty list_ty n |
| 954 |
| Byte -> rocqbyte_of_string ?loc esig byte_ty n |
| 955 |
| PString -> pstring_of_string ?loc n |
| 956 |
in |
| 957 |
let sigma = !sigma in |
| 958 |
let sigma,to_ty = Evd.fresh_global env sigma o.to_ty in |
| 959 |
let to_ty = EConstr.Unsafe.to_constr to_ty in |
| 960 |
let res = eval_constr_app env sigma to_ty c in |
| 961 |
match snd o.to_kind with |
| 962 |
| Direct -> glob_of_token "string" o.ty_name ?loc env sigma o.to_post res |
| 963 |
| Option -> interp_option "string" "string" o.ty_name ?loc env sigma o.to_post res |
| 964 |
| Error -> interp_error "string" "string" o.ty_name ?loc env sigma o.to_post res |
| 965 |
|
| 966 |
let uninterp o n = |
| 967 |
PrimTokenNotation.uninterp |
| 968 |
begin function |
| 969 |
| (ListByte, c) -> string_of_rocqlist_byte c |
| 970 |
| (Byte, c) -> string_of_rocqbyte c |
| 971 |
| (PString, c) -> string_of_pstring c |
| 972 |
end o n |
| 973 |
end |
| 974 |
|
| 975 |
let hashtbl_check_and_set allow_overwrite uid f h eq = |
| 976 |
match Hashtbl.find h uid with |
| 977 |
| exception Not_found -> Hashtbl.add h uid f |
| 978 |
| _ when allow_overwrite -> Hashtbl.add h uid f |
| 979 |
| g when eq f g -> () |
| 980 |
| _ -> |
| 981 |
user_err |
| 982 |
(str "Unique identifier " ++ str uid ++ |
| 983 |
str " already used to register a number or string (un)interpreter.") |
| 984 |
|
| 985 |
let register_gen_interpretation allow_overwrite (uid:string) (interp, uninterp) : prim_token_uid = |
| 986 |
hashtbl_check_and_set |
| 987 |
allow_overwrite uid interp prim_token_interpreters InnerPrimToken.interp_eq; |
| 988 |
hashtbl_check_and_set |
| 989 |
allow_overwrite uid uninterp prim_token_uninterpreters InnerPrimToken.uninterp_eq; |
| 990 |
uid |
| 991 |
|
| 992 |
let register_rawnumeral_interpretation ?(allow_overwrite=false) uid (interp, uninterp) = |
| 993 |
register_gen_interpretation allow_overwrite uid |
| 994 |
(InnerPrimToken.RawNumInterp interp, InnerPrimToken.RawNumUninterp uninterp) |
| 995 |
|
| 996 |
let register_bignumeral_interpretation ?(allow_overwrite=false) uid (interp, uninterp) = |
| 997 |
register_gen_interpretation allow_overwrite uid |
| 998 |
(InnerPrimToken.BigNumInterp interp, InnerPrimToken.BigNumUninterp uninterp) |
| 999 |
|
| 1000 |
let register_string_interpretation ?(allow_overwrite=false) uid (interp, uninterp) = |
| 1001 |
register_gen_interpretation allow_overwrite uid |
| 1002 |
(InnerPrimToken.StringInterp interp, InnerPrimToken.StringUninterp uninterp) |
| 1003 |
|
| 1004 |
type prim_token_interp_info = |
| 1005 |
| Uid of prim_token_uid |
| 1006 |
| NumberNotation of number_notation_obj |
| 1007 |
| StringNotation of string_notation_obj |
| 1008 |
|
| 1009 |
let do_interp ?loc info p = |
| 1010 |
let interp = match info with |
| 1011 |
| Uid uid -> Hashtbl.find prim_token_interpreters uid |
| 1012 |
| NumberNotation o -> InnerPrimToken.RawNumInterp (Numbers.interp o) |
| 1013 |
| StringNotation o -> InnerPrimToken.StringInterp (Strings.interp o) |
| 1014 |
in |
| 1015 |
InnerPrimToken.do_interp ?loc interp p |
| 1016 |
|
| 1017 |
let do_uninterp ~print_float info c = |
| 1018 |
try |
| 1019 |
let uninterp = match info with |
| 1020 |
| Uid uid -> Hashtbl.find prim_token_uninterpreters uid |
| 1021 |
| NumberNotation o -> InnerPrimToken.RawNumUninterp (Numbers.uninterp ~print_float o) |
| 1022 |
| StringNotation o -> InnerPrimToken.StringUninterp (Strings.uninterp o) |
| 1023 |
in |
| 1024 |
InnerPrimToken.do_uninterp uninterp (AnyGlobConstr c) |
| 1025 |
with Not_found -> None |
| 1026 |
|
| 1027 |
let can_interp uid n = |
| 1028 |
let open InnerPrimToken in |
| 1029 |
match n, uid with |
| 1030 |
| Constrexpr.Number _, NumberNotation _ -> true |
| 1031 |
| _, NumberNotation _ -> false |
| 1032 |
| String _, StringNotation _ -> true |
| 1033 |
| _, StringNotation _ -> false |
| 1034 |
| _, Uid uid -> |
| 1035 |
let interp = Hashtbl.find_opt prim_token_interpreters uid in |
| 1036 |
match n, interp with |
| 1037 |
| Constrexpr.Number _, Some (RawNumInterp _ | BigNumInterp _) -> true |
| 1038 |
| String _, Some (StringInterp _) -> true |
| 1039 |
| _ -> false |