(***********************************************************************) (* v * The Coq Proof Assistant / The Coq Development Team *) (* (int_of_r_rec a1 plus c)+1 | a when alpha_eq(a,a1) -> 1 | _ -> raise Non_closed_number let int_of_r p = let (_,a1,plus,_) = get_r_sign_ast dummy_loc in try Some (int_of_r_rec a1 plus p) with Non_closed_number -> None let replace_plus p = let (_,a1,_,astnr) = get_r_sign_ast dummy_loc in ope ("REXPR",[ope("APPLIST", [astnr; a1; p])]) let r_printer std_pr p = let (_,a1,plus,_) = get_r_sign dummy_loc in match (int_of_r p) with | Some i -> str (string_of_int (i+1)) | None -> std_pr (replace_plus p) let r_printer_outside std_pr p = let (_,a1,plus,_) = get_r_sign dummy_loc in match (int_of_r p) with | Some i -> str "``" ++ str (string_of_int (i+1)) ++ str "``" | None -> std_pr (replace_plus p) let _ = Esyntax.Ppprim.add ("r_printer", r_printer) let _ = Esyntax.Ppprim.add ("r_printer_outside", r_printer_outside) (**********************************************************************) (* Parsing Z via scopes *) (**********************************************************************) open Libnames open Rawterm open Bignat let make_dir l = make_dirpath (List.map id_of_string (List.rev l)) let rdefinitions = make_dir ["Coq";"Reals";"Rdefinitions"] (* TODO: temporary hack *) let make_path dir id = Libnames.encode_kn dir (id_of_string id) let glob_R1 = ConstRef (make_path rdefinitions "R1") let glob_R0 = ConstRef (make_path rdefinitions "R0") let glob_Ropp = ConstRef (make_path rdefinitions "Ropp") let glob_Rplus = ConstRef (make_path rdefinitions "Rplus") let r_of_posint dloc n = if is_nonzero n then begin if Options.is_verbose () & less_than (of_string "5000") n then begin warning ("You may experiment stack overflow and segmentation fault\ \nwhile parsing numbers in R the absolute value of which is\ \ngreater than 5000"); flush_all () end; let ref_R1 = RRef (dloc, glob_R1) in let ref_Rplus = RRef (dloc, glob_Rplus) in let rec r_of_strictly_pos n = if is_one n then ref_R1 else RApp(dloc, ref_Rplus, [ref_R1; r_of_strictly_pos (sub_1 n)]) in r_of_strictly_pos n end else RRef (dloc, glob_R0) let r_of_int dloc z = match z with | NEG n -> RApp (dloc, RRef(dloc,glob_Ropp), [r_of_posint dloc n]) | POS n -> r_of_posint dloc n (**********************************************************************) (* Printing R via scopes *) (**********************************************************************) let rec bignat_of_r = function | RApp (_,RRef (_,p), [RRef (_,o); a]) when p = glob_Rplus & o = glob_R1 -> add_1 (bignat_of_r a) | RRef (_,a) when a = glob_R1 -> Bignat.one | RRef (_,a) when a = glob_R0 -> Bignat.zero | _ -> raise Non_closed_number let bigint_of_r = function | RApp (_,RRef (_,o), [a]) when o = glob_Ropp -> NEG (bignat_of_r a) | a -> POS (bignat_of_r a) let uninterp_r p = try Some (bigint_of_r p) with Non_closed_number -> None let _ = Symbols.declare_numeral_interpreter "R_scope" ["Coq";"Reals";"Rsyntax"] (r_of_int,None) ([RRef(dummy_loc,glob_Ropp);RRef(dummy_loc,glob_R0); RRef(dummy_loc,glob_Rplus);RRef(dummy_loc,glob_R1)], uninterp_r, None) (***********************************************************************) (* Old ast printers via scope *) exception Non_closed_number let bignat_of_pos p = let (_,one,plus,_) = get_r_sign_ast dummy_loc in let rec transl = function | Node (_,"APPLIST",[p; o; a]) when alpha_eq(p,plus) & alpha_eq(o,one) -> add_1(transl a) | a when alpha_eq(a,one) -> Bignat.one | _ -> raise Non_closed_number in transl p let bignat_option_of_pos p = try Some (bignat_of_pos p) with Non_closed_number -> None let r_printer_Rplus1 p = match bignat_option_of_pos p with | Some n -> Some (str (Bignat.to_string (add_1 n))) | None -> None let r_printer_Ropp p = match bignat_option_of_pos p with | Some n -> Some (str "-" ++ str (Bignat.to_string n)) | None -> None let r_printer_R1 _ = Some (int 1) let r_printer_R0 _ = Some (int 0) (* Declare pretty-printers for integers *) let _ = Esyntax.declare_primitive_printer "r_printer_Ropp" "R_scope" (r_printer_Ropp) let _ = Esyntax.declare_primitive_printer "r_printer_Rplus1" "R_scope" (r_printer_Rplus1) let _ = Esyntax.declare_primitive_printer "r_printer_R1" "R_scope" (r_printer_R1) let _ = Esyntax.declare_primitive_printer "r_printer_R0" "R_scope" r_printer_R0