Debian Codegraph @

plugins/extraction/miniml.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 (*s Target language for extraction: a core ML called MiniML. *)
12
13 open Names
14
15 module InfvInst =
16 struct
17 type t = bool array (* true if informative *)
18 let empty = [||]
19 let compare i1 i2 = CArray.compare Bool.compare i1 i2
20 let equal i1 i2 = Int.equal (compare i1 i2) 0
21
22 let generate actx =
23 let names = UVars.AbstractContext.names actx in
24 let quals = Array.length names.UVars.quals in
25 let init n =
26 let ans = Array.make quals true in
27 let n = ref n in
28 for i = 0 to quals - 1 do
29 let b = !n mod 2 = 0 in
30 ans.(i) <- b;
31 n := !n / 2
32 done;
33 ans
34 in
35 List.init (1 lsl quals) init
36
37 let default actx =
38 let names = UVars.AbstractContext.names actx in
39 let quals = Array.length names.UVars.quals in
40 Array.make quals true
41
42 let ground inst =
43 let qvars, _ = UVars.Instance.to_array inst in
44 let map q = match q with
45 | Sorts.Quality.QConstant (QProp | QSProp) -> false
46 | Sorts.Quality.QConstant QType -> true
47 | Sorts.Quality.QVar qv ->
48 match Sorts.QVar.repr qv with
49 | Var _ -> CErrors.anomaly (Pp.str "Non-ground instance")
50 | Unif _ | Global _ -> true (* informative by default *)
51 in
52 Array.map map qvars
53
54 let instantiate actx inst =
55 let u = UVars.make_abstract_instance actx in
56 let fl l = l in
57 let fq q = match Sorts.QVar.var_index q with
58 | None -> assert false
59 | Some i ->
60 if inst.(i) then Sorts.Quality.qtype
61 else Sorts.Quality.qsprop
62 in
63 UVars.Instance.subst_fn (fq, fl) u
64
65 let encode inst =
66 if Array.for_all (fun b -> b) inst then None
67 else
68 let len = Array.length inst in
69 Some (String.init len (fun i -> if inst.(i) then 'X' else 'O'))
70
71 end
72
73 type global = { glob : GlobRef.t; inst : InfvInst.t }
74
75 (* The [signature] type is used to know how many arguments a CIC
76 object expects, and what these arguments will become in the ML
77 object. *)
78
79 (* We eliminate from terms:
80 1) types
81 2) logical parts
82 3) user-declared implicit arguments of a constant of constructor
83 *)
84
85 type kill_reason =
86 | Ktype
87 | Kprop
88 | Kimplicit of global * int (* n-th arg of a cst or construct *)
89
90 type sign = Keep | Kill of kill_reason
91
92 (* Convention: outmost lambda/product gives the head of the list. *)
93
94 type signature = sign list
95
96 (*s ML type expressions. *)
97
98 type ml_type =
99 | Tarr of ml_type * ml_type
100 | Tglob of global * ml_type list
101 | Tvar of int
102 | Tvar' of int (* same as Tvar, used to avoid clash *)
103 | Tmeta of ml_meta (* used during ML type reconstruction *)
104 | Tdummy of kill_reason
105 | Tunknown
106 | Taxiom
107
108 and ml_meta = { id : int; mutable contents : ml_type option }
109
110 (* ML type schema.
111 The integer is the number of variable in the schema. *)
112
113 type ml_schema = int * ml_type
114
115 (*s ML inductive types. *)
116
117 type inductive_kind =
118 | Singleton
119 | Coinductive
120 | Standard
121 | Record of global option list (* None for anonymous field *)
122
123 (* A [ml_ind_packet] is the miniml counterpart of a [one_inductive_body].
124 If the inductive is logical ([ip_logical = false]), then all other fields
125 are unused. Otherwise,
126 [ip_sign] is a signature concerning the arguments of the inductive,
127 [ip_vars] contains the names of the type variables surviving in ML,
128 [ip_types] contains the ML types of all constructors.
129 *)
130
131 type ml_ind_packet = {
132 ip_typename : Id.t;
133 ip_typename_ref : global;
134 ip_consnames : Id.t array;
135 ip_consnames_ref : global array;
136 ip_logical : bool;
137 ip_sign : signature;
138 ip_vars : Id.t list;
139 ip_types : (ml_type list) array
140 }
141
142 (* [ip_nparams] contains the number of parameters. *)
143
144 type equiv =
145 | NoEquiv
146 | Equiv of KerName.t
147 | RenEquiv of string
148
149 type ml_ind = {
150 ind_kind : inductive_kind;
151 ind_nparams : int;
152 ind_packets : ml_ind_packet array;
153 ind_equiv : equiv
154 }
155
156 (*s ML terms. *)
157
158 type ml_ident =
159 | Dummy
160 | Id of Id.t
161 | Tmp of Id.t
162
163 (** We now store some typing information on constructors
164 and cases to avoid type-unsafe optimisations. This will be
165 either the type of the applied constructor or the type
166 of the head of the match.
167 *)
168
169 (** Nota : the constructor [MLtuple] and the extension of [MLcase]
170 to general patterns have been proposed by P.N. Tollitte for
171 his Relation Extraction plugin. [MLtuple] is currently not
172 used by the main extraction, as well as deep patterns. *)
173
174 type ml_branch = ml_ident list * ml_pattern * ml_ast
175
176 and ml_ast =
177 | MLrel of int
178 | MLapp of ml_ast * ml_ast list
179 | MLlam of ml_ident * ml_ast
180 | MLletin of ml_ident * ml_ast * ml_ast
181 | MLglob of global
182 | MLcons of ml_type * global * ml_ast list
183 | MLtuple of ml_ast list
184 | MLcase of ml_type * ml_ast * ml_branch array
185 | MLfix of int * Id.t array * ml_ast array
186 | MLexn of string
187 | MLdummy of kill_reason
188 | MLaxiom of string
189 | MLmagic of ml_ast
190 | MLuint of Uint63.t
191 | MLfloat of Float64.t
192 | MLstring of Pstring.t
193 | MLparray of ml_ast array * ml_ast
194
195 and ml_pattern =
196 | Pcons of global * ml_pattern list
197 | Ptuple of ml_pattern list
198 | Prel of int (** Cf. the idents in the branch. [Prel 1] is the last one. *)
199 | Pwild
200 | Pusual of global (** Shortcut for Pcons (r,[Prel n;...;Prel 1]) **)
201
202 (*s ML declarations. *)
203
204 type ml_decl =
205 | Dind of ml_ind
206 | Dtype of global * Id.t list * ml_type
207 | Dterm of global * ml_ast * ml_type
208 | Dfix of global array * ml_ast array * ml_type array
209
210 type ml_spec =
211 | Sind of ml_ind
212 | Stype of global * Id.t list * ml_type option
213 | Sval of global * ml_type
214
215 type ml_specif =
216 | Spec of ml_spec
217 | Smodule of ml_module_type
218 | Smodtype of ml_module_type
219
220 and ml_module_type =
221 | MTident of ModPath.t
222 | MTfunsig of MBId.t * ml_module_type * ml_module_type
223 | MTsig of ModPath.t * ml_module_sig
224 | MTwith of ml_module_type * ml_with_declaration
225
226 and ml_with_declaration =
227 | ML_With_type of InfvInst.t * Id.t list * Id.t list * ml_type
228 | ML_With_module of Id.t list * ModPath.t
229
230 and ml_module_sig = (Id.t * ml_specif) list
231
232 type ml_structure_elem =
233 | SEdecl of ml_decl
234 | SEmodule of ml_module
235 | SEmodtype of ml_module_type
236
237 and ml_module_expr =
238 | MEident of ModPath.t
239 | MEfunctor of MBId.t * ml_module_type * ml_module_expr
240 | MEstruct of ModPath.t * ml_module_structure
241 | MEapply of ml_module_expr * ml_module_expr
242
243 and ml_module_structure = (Id.t * ml_structure_elem) list
244
245 and ml_module =
246 { ml_mod_expr : ml_module_expr;
247 ml_mod_type : ml_module_type }
248
249 (* NB: we do not translate the [mod_equiv] field, since [mod_equiv = mp]
250 implies that [mod_expr = MEBident mp]. Same with [msb_equiv]. *)
251
252 type ml_structure = (ModPath.t * ml_module_structure) list
253
254 type ml_signature = (ModPath.t * ml_module_sig) list
255
256 type unsafe_needs = {
257 mldummy : bool;
258 tdummy : bool;
259 tunknown : bool;
260 magic : bool
261 }
262
263 type 's language_descr = {
264 keywords : Id.Set.t;
265
266 (* Concerning the source file *)
267 file_suffix : string;
268 file_naming : 's -> DirPath.t -> string;
269 (* the second argument is a comment to add to the preamble *)
270 preamble :
271 's -> Id.t -> Pp.t option -> DirPath.Set.t -> unsafe_needs ->
272 Pp.t;
273 pp_struct : 's -> ml_structure -> Pp.t;
274
275 (* Concerning a possible interface file *)
276 sig_suffix : string option;
277 (* the second argument is a comment to add to the preamble *)
278 sig_preamble :
279 's -> Id.t -> Pp.t option -> DirPath.Set.t -> unsafe_needs ->
280 Pp.t;
281 pp_sig : 's -> ml_signature -> Pp.t;
282
283 (* for an isolated declaration print *)
284 pp_decl : 's -> ml_decl -> Pp.t;
285
286 }