Debian Codegraph @

interp/modintern.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 open Declarations
12 open Mod_declarations
13 open Libnames
14 open Constrexpr
15 open Constrintern
16
17 type module_internalization_error =
18 | NotAModuleNorModtype of qualid
19 | NotAModuleType of qualid
20 | NotAModule of qualid
21 | IncorrectWithInModule
22 | IncorrectModuleApplication
23
24 exception ModuleInternalizationError of module_internalization_error
25
26 type module_kind = Module | ModType | ModAny
27
28 type module_struct_expr = (universe_decl_expr option * constr_expr) Declarations.module_alg_expr
29
30 let error_not_a_module_loc ~info kind loc qid =
31 let e = match kind with
32 | Module -> NotAModule qid
33 | ModType -> NotAModuleType qid
34 | ModAny -> NotAModuleNorModtype qid
35 in
36 let info = Option.cata (Loc.add_loc info) info loc in
37 Exninfo.iraise (ModuleInternalizationError e,info)
38
39 let error_incorrect_with_in_module loc =
40 Loc.raise ?loc (ModuleInternalizationError IncorrectWithInModule)
41
42 let error_application_to_module_type loc =
43 Loc.raise ?loc (ModuleInternalizationError IncorrectModuleApplication)
44
45 (** Searching for a module name in the Nametab.
46
47 According to the input module kind, modules or module types
48 or both are searched. The returned kind is never ModAny, and
49 it is equal to the input kind when this one isn't ModAny. *)
50
51 let lookup_module_or_modtype kind qid =
52 let loc = qid.CAst.loc in
53 try
54 if kind == ModType then raise Not_found;
55 let mp = Nametab.locate_module qid in
56 Dumpglob.dump_modref ?loc mp "mod"; (mp,Module)
57 with Not_found ->
58 try
59 if kind == Module then raise Not_found;
60 let mp = Nametab.locate_modtype qid in
61 Dumpglob.dump_modref ?loc mp "modtype"; (mp,ModType)
62 with Not_found as exn ->
63 let _, info = Exninfo.capture exn in
64 error_not_a_module_loc ~info kind loc qid
65
66 let lookup_module lqid = fst (lookup_module_or_modtype Module lqid)
67
68 let lookup_polymorphism env base kind fqid =
69 let m = match kind with
70 | Module -> mod_type (Environ.lookup_module base env)
71 | ModType -> mod_type (Environ.lookup_modtype base env)
72 | ModAny -> assert false
73 in
74 let rec defunctor = function
75 | NoFunctor m -> m
76 | MoreFunctor (_,_,m) -> defunctor m
77 in
78 let rec aux m fqid =
79 let open Names in
80 match fqid with
81 | [] -> assert false
82 | [id] ->
83 let test (lab,obj) =
84 match Id.equal lab id, obj with
85 | false, _ | _, (SFBrules _ | SFBmodule _ | SFBmodtype _) -> None
86 | true, SFBmind mind -> Some (Declareops.inductive_is_polymorphic mind)
87 | true, SFBconst const -> Some (Declareops.constant_is_polymorphic const)
88 in
89 (match CList.find_map test m with Some v -> v | None -> false (* error later *))
90 | id::rem ->
91 let next = function
92 | MoreFunctor _ -> false (* error later *)
93 | NoFunctor body -> aux body rem
94 in
95 let test (lab,obj) =
96 match Id.equal lab id, obj with
97 | false, _ | _, (SFBrules _ | SFBconst _ | SFBmind _) -> None
98 | true, SFBmodule body -> Some (next @@ mod_type body)
99 | true, SFBmodtype body -> (* XXX is this valid? If not error later *)
100 Some (next @@ mod_type body)
101 in
102 (match CList.find_map test m with Some v -> v | None -> false (* error later *))
103 in
104 aux (defunctor m) fqid
105
106 let intern_with_decl = function
107 | CWith_Module ({CAst.v=fqid},qid) ->
108 WithMod (fqid,lookup_module qid)
109 | CWith_Definition ({CAst.v=fqid},udecl,c) ->
110 WithDef (fqid,(udecl,c))
111
112 let loc_of_module l = l.CAst.loc
113
114 (* Invariant : the returned kind is never ModAny, and it is
115 equal to the input kind when this one isn't ModAny. *)
116
117 let rec intern_module_ast kind m = match m with
118 | {CAst.loc;v=CMident qid} ->
119 let (mp,kind) = lookup_module_or_modtype kind qid in
120 (MEident mp, mp, kind)
121 | {CAst.loc;v=CMapply (me1,me2)} ->
122 let me1', base, kind1 = intern_module_ast kind me1 in
123 let mp2, kind2 = lookup_module_or_modtype ModAny me2 in
124 if kind2 == ModType then
125 error_application_to_module_type (loc_of_module me2);
126 (MEapply (me1',mp2), base, kind1)
127 | {CAst.loc;v=CMwith (me,decl)} ->
128 let me,base,kind = intern_module_ast kind me in
129 if kind == Module then error_incorrect_with_in_module m.CAst.loc;
130 let decl = intern_with_decl decl in
131 (MEwith(me,decl), base, kind)
132
133 let interp_with_decl env base kind = function
134 | WithMod (fqid,mp) -> WithMod (fqid,mp), Univ.ContextSet.empty
135 | WithDef(fqid,(udecl,c)) ->
136 let sigma, udecl = interp_univ_decl_opt env udecl in
137 let c, ectx = interp_constr env sigma c in
138 let poly = lookup_polymorphism env base kind fqid in
139 let poly =
140 PolyFlags.of_univ_poly poly (* MS: FIXME: no sortpoly/cumulative support *)
141 in
142 begin match fst (UState.check_univ_decl ~poly ectx udecl) with
143 | UState.Polymorphic_entry ctx ->
144 let inst, ctx = UVars.abstract_universes ctx in
145 let c = EConstr.Vars.subst_univs_level_constr (UVars.make_instance_subst inst) c in
146 let c = EConstr.to_constr sigma c in
147 WithDef (fqid,(c, Some ctx)), Univ.ContextSet.empty
148 | UState.Monomorphic_entry ctx ->
149 let c = EConstr.to_constr sigma c in
150 WithDef (fqid,(c, None)), ctx
151 end
152
153 let rec interp_module_ast env kind base m cst = match m with
154 | MEident mp ->
155 MEident mp, cst
156 | MEapply (me,mp) ->
157 let me', cst = interp_module_ast env kind base me cst in
158 MEapply(me',mp), cst
159 | MEwith(me,decl) ->
160 let me, cst = interp_module_ast env kind base me cst in
161 let decl, cst' = interp_with_decl env base kind decl in
162 let cst = Univ.ContextSet.union cst cst' in
163 MEwith(me,decl), cst
164
165 let interp_module_ast env kind base m =
166 interp_module_ast env kind base m Univ.ContextSet.empty