Debian Codegraph @

interp/smartlocate.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* Created by Hugo Herbelin from code formerly dispatched in
12 syntax_def.ml or tacinterp.ml, Sep 2009 *)
13
14 (* This file provides high-level name globalization functions *)
15
16 (* *)
17 open Pp
18 open CErrors
19 open Libnames
20 open Globnames
21 open Notation_term
22
23 let global_of_extended_global_head = function
24 | TrueGlobal ref -> ref
25 | Abbrev kn ->
26 let _, syn_def = Abbreviation.find_interp kn in
27 let rec head_of = function
28 | NRef (ref,None) -> ref
29 | NApp (rc, _) -> head_of rc
30 | NCast (rc, _, _) -> head_of rc
31 | NLetIn (_, _, _, rc) -> head_of rc
32 | _ -> raise Not_found in
33 head_of syn_def
34
35 let global_of_extended_global_exn = function
36 | TrueGlobal ref -> ref
37 | Abbrev kn ->
38 match Abbreviation.find_interp kn with
39 | [],NRef (ref,None) -> ref
40 | [],NApp (NRef (ref,None),[]) -> ref
41 | _ -> raise Not_found
42
43 let locate_global_with_alias ?(head=false) qid =
44 let ref = Nametab.locate_extended qid in
45 try
46 if head then global_of_extended_global_head ref
47 else global_of_extended_global_exn ref
48 with Not_found ->
49 user_err ?loc:qid.CAst.loc (pr_qualid qid ++
50 str " is bound to a notation that does not denote a reference.")
51
52 let global_of_extended_global x =
53 try Some (global_of_extended_global_exn x)
54 with Not_found -> None
55
56 let global_constant_with_alias qid =
57 try match locate_global_with_alias qid with
58 | Names.GlobRef.ConstRef c -> c
59 | ref ->
60 user_err ?loc:qid.CAst.loc
61 (pr_qualid qid ++ spc () ++ str "is not a reference to a constant.")
62 with Not_found as exn ->
63 let _, info = Exninfo.capture exn in
64 Nametab.error_global_not_found ~info qid
65
66 let global_inductive_with_alias qid =
67 try match locate_global_with_alias qid with
68 | Names.GlobRef.IndRef ind -> ind
69 | ref ->
70 user_err ?loc:qid.CAst.loc
71 (pr_qualid qid ++ spc () ++ str "is not an inductive type.")
72 with Not_found as exn ->
73 let _, info = Exninfo.capture exn in
74 Nametab.error_global_not_found ~info qid
75
76 let global_constructor_with_alias qid =
77 try match locate_global_with_alias qid with
78 | Names.GlobRef.ConstructRef c -> c
79 | ref ->
80 user_err ?loc:qid.CAst.loc
81 (pr_qualid qid ++ spc () ++ str "is not a constructor of an inductive type.")
82 with Not_found as exn ->
83 let _, info = Exninfo.capture exn in
84 Nametab.error_global_not_found ~info qid
85
86 let global_with_alias ?head qid =
87 try locate_global_with_alias ?head qid
88 with Not_found as exn ->
89 let _, info = Exninfo.capture exn in
90 Nametab.error_global_not_found ~info qid
91
92 let smart_global ?(head = false) = let open Constrexpr in CAst.with_loc_val (fun ?loc -> function
93 | AN r ->
94 global_with_alias ~head r
95 | ByNotation (ntn,sc) ->
96 Notation.interp_notation_as_global_reference ?loc ~head (fun _ -> true) ntn sc)
97
98 let smart_global_kind f dest is = let open Constrexpr in CAst.with_loc_val (fun ?loc -> function
99 | AN r -> f r
100 | ByNotation (ntn,sc) ->
101 dest
102 (Notation.interp_notation_as_global_reference ?loc ~head:false is ntn sc))
103
104 let smart_global_constant =
105 smart_global_kind global_constant_with_alias destConstRef isConstRef
106
107 let smart_global_inductive =
108 smart_global_kind global_inductive_with_alias destIndRef isIndRef
109
110 let smart_global_constructor =
111 smart_global_kind global_constructor_with_alias destConstructRef isConstructRef