Debian Codegraph @

kernel/cPrimitives.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 (* Note: don't forget to update v_primitive in checker/values.ml if the *)
12 (* number of primitives is changed. *)
13
14 open Univ
15 open UVars
16
17 type t =
18 | Int63head0
19 | Int63tail0
20 | Int63add
21 | Int63sub
22 | Int63mul
23 | Int63div
24 | Int63mod
25 | Int63divs
26 | Int63mods
27 | Int63lsr
28 | Int63lsl
29 | Int63asr
30 | Int63land
31 | Int63lor
32 | Int63lxor
33 | Int63addc
34 | Int63subc
35 | Int63addCarryC
36 | Int63subCarryC
37 | Int63mulc
38 | Int63diveucl
39 | Int63div21
40 | Int63addMulDiv
41 | Int63eq
42 | Int63lt
43 | Int63le
44 | Int63lts
45 | Int63les
46 | Int63compare
47 | Int63compares
48 | Float64opp
49 | Float64abs
50 | Float64eq
51 | Float64lt
52 | Float64le
53 | Float64compare
54 | Float64equal
55 | Float64classify
56 | Float64add
57 | Float64sub
58 | Float64mul
59 | Float64div
60 | Float64sqrt
61 | Float64ofUint63
62 | Float64normfr_mantissa
63 | Float64frshiftexp
64 | Float64ldshiftexp
65 | Float64next_up
66 | Float64next_down
67 | Arraymake
68 | Arrayget
69 | Arraydefault
70 | Arrayset
71 | Arraycopy
72 | Arraylength
73 | Stringmake
74 | Stringlength
75 | Stringget
76 | Stringsub
77 | Stringcat
78 | Stringcompare
79
80 let parse = function
81 | "int63_head0" -> Int63head0
82 | "int63_tail0" -> Int63tail0
83 | "int63_add" -> Int63add
84 | "int63_sub" -> Int63sub
85 | "int63_mul" -> Int63mul
86 | "int63_div" -> Int63div
87 | "int63_mod" -> Int63mod
88 | "int63_divs" -> Int63divs
89 | "int63_mods" -> Int63mods
90 | "int63_lsr" -> Int63lsr
91 | "int63_lsl" -> Int63lsl
92 | "int63_asr" -> Int63asr
93 | "int63_land" -> Int63land
94 | "int63_lor" -> Int63lor
95 | "int63_lxor" -> Int63lxor
96 | "int63_addc" -> Int63addc
97 | "int63_subc" -> Int63subc
98 | "int63_addcarryc" -> Int63addCarryC
99 | "int63_subcarryc" -> Int63subCarryC
100 | "int63_mulc" -> Int63mulc
101 | "int63_diveucl" -> Int63diveucl
102 | "int63_div21" -> Int63div21
103 | "int63_addmuldiv" -> Int63addMulDiv
104 | "int63_eq" -> Int63eq
105 | "int63_lt" -> Int63lt
106 | "int63_le" -> Int63le
107 | "int63_lts" -> Int63lts
108 | "int63_les" -> Int63les
109 | "int63_compare" -> Int63compare
110 | "int63_compares" -> Int63compares
111 | "float64_opp" -> Float64opp
112 | "float64_abs" -> Float64abs
113 | "float64_eq" -> Float64eq
114 | "float64_lt" -> Float64lt
115 | "float64_le" -> Float64le
116 | "float64_compare" -> Float64compare
117 | "float64_equal" -> Float64equal
118 | "float64_classify" -> Float64classify
119 | "float64_add" -> Float64add
120 | "float64_sub" -> Float64sub
121 | "float64_mul" -> Float64mul
122 | "float64_div" -> Float64div
123 | "float64_sqrt" -> Float64sqrt
124 | "float64_of_uint63" -> Float64ofUint63
125 | "float64_normfr_mantissa" -> Float64normfr_mantissa
126 | "float64_frshiftexp" -> Float64frshiftexp
127 | "float64_ldshiftexp" -> Float64ldshiftexp
128 | "float64_next_up" -> Float64next_up
129 | "float64_next_down" -> Float64next_down
130 | "array_make" -> Arraymake
131 | "array_get" -> Arrayget
132 | "array_default" -> Arraydefault
133 | "array_set" -> Arrayset
134 | "array_length" -> Arraylength
135 | "array_copy" -> Arraycopy
136 | "string_make" -> Stringmake
137 | "string_length" -> Stringlength
138 | "string_get" -> Stringget
139 | "string_sub" -> Stringsub
140 | "string_cat" -> Stringcat
141 | "string_compare" -> Stringcompare
142 | _ -> raise Not_found
143
144 let equal (p1 : t) (p2 : t) =
145 p1 == p2
146
147 let hash = function
148 | Int63head0 -> 1
149 | Int63tail0 -> 2
150 | Int63add -> 3
151 | Int63sub -> 4
152 | Int63mul -> 5
153 | Int63div -> 6
154 | Int63mod -> 7
155 | Int63lsr -> 8
156 | Int63lsl -> 9
157 | Int63land -> 10
158 | Int63lor -> 11
159 | Int63lxor -> 12
160 | Int63addc -> 13
161 | Int63subc -> 14
162 | Int63addCarryC -> 15
163 | Int63subCarryC -> 16
164 | Int63mulc -> 17
165 | Int63diveucl -> 18
166 | Int63div21 -> 19
167 | Int63addMulDiv -> 20
168 | Int63eq -> 21
169 | Int63lt -> 22
170 | Int63le -> 23
171 | Int63compare -> 24
172 | Float64opp -> 25
173 | Float64abs -> 26
174 | Float64compare -> 27
175 | Float64classify -> 28
176 | Float64add -> 29
177 | Float64sub -> 30
178 | Float64mul -> 31
179 | Float64div -> 32
180 | Float64sqrt -> 33
181 | Float64ofUint63 -> 34
182 | Float64normfr_mantissa -> 35
183 | Float64frshiftexp -> 36
184 | Float64ldshiftexp -> 37
185 | Float64next_up -> 38
186 | Float64next_down -> 39
187 | Float64eq -> 40
188 | Float64lt -> 41
189 | Float64le -> 42
190 | Arraymake -> 43
191 | Arrayget -> 44
192 | Arraydefault -> 45
193 | Arrayset -> 46
194 | Arraycopy -> 47
195 | Arraylength -> 48
196 | Int63lts -> 49
197 | Int63les -> 50
198 | Int63divs -> 51
199 | Int63mods -> 52
200 | Int63asr -> 53
201 | Int63compares -> 54
202 | Float64equal -> 55
203 | Stringmake -> 56
204 | Stringlength -> 57
205 | Stringget -> 58
206 | Stringsub -> 59
207 | Stringcat -> 60
208 | Stringcompare -> 61
209
210 (* Should match names in nativevalues.ml *)
211 let to_string = function
212 | Int63head0 -> "head0"
213 | Int63tail0 -> "tail0"
214 | Int63add -> "add"
215 | Int63sub -> "sub"
216 | Int63mul -> "mul"
217 | Int63div -> "div"
218 | Int63mod -> "rem"
219 | Int63divs -> "divs"
220 | Int63mods -> "rems"
221 | Int63lsr -> "l_sr"
222 | Int63lsl -> "l_sl"
223 | Int63asr -> "a_sr"
224 | Int63land -> "l_and"
225 | Int63lor -> "l_or"
226 | Int63lxor -> "l_xor"
227 | Int63addc -> "addc"
228 | Int63subc -> "subc"
229 | Int63addCarryC -> "addCarryC"
230 | Int63subCarryC -> "subCarryC"
231 | Int63mulc -> "mulc"
232 | Int63diveucl -> "diveucl"
233 | Int63div21 -> "div21"
234 | Int63addMulDiv -> "addMulDiv"
235 | Int63eq -> "eq"
236 | Int63lt -> "lt"
237 | Int63le -> "le"
238 | Int63lts -> "lts"
239 | Int63les -> "les"
240 | Int63compare -> "compare"
241 | Int63compares -> "compares"
242 | Float64opp -> "fopp"
243 | Float64abs -> "fabs"
244 | Float64eq -> "feq"
245 | Float64lt -> "flt"
246 | Float64le -> "fle"
247 | Float64compare -> "fcompare"
248 | Float64equal -> "fequal"
249 | Float64classify -> "fclassify"
250 | Float64add -> "fadd"
251 | Float64sub -> "fsub"
252 | Float64mul -> "fmul"
253 | Float64div -> "fdiv"
254 | Float64sqrt -> "fsqrt"
255 | Float64ofUint63 -> "float_of_int"
256 | Float64normfr_mantissa -> "normfr_mantissa"
257 | Float64frshiftexp -> "frshiftexp"
258 | Float64ldshiftexp -> "ldshiftexp"
259 | Float64next_up -> "next_up"
260 | Float64next_down -> "next_down"
261 | Arraymake -> "arraymake"
262 | Arrayget -> "arrayget"
263 | Arraydefault -> "arraydefault"
264 | Arrayset -> "arrayset"
265 | Arraycopy -> "arraycopy"
266 | Arraylength -> "arraylength"
267 | Stringmake -> "string_make"
268 | Stringlength -> "string_length"
269 | Stringget -> "string_get"
270 | Stringsub -> "string_sub"
271 | Stringcat -> "string_cat"
272 | Stringcompare -> "string_compare"
273
274 type const =
275 | Arraymaxlength
276 | Stringmaxlength
277
278 let const_to_string = function
279 | Arraymaxlength -> "arraymaxlength"
280 | Stringmaxlength -> "stringmaxlength"
281
282 let const_of_string = function
283 | "array_max_length" -> Arraymaxlength
284 | "string_max_length" -> Stringmaxlength
285 | _ -> raise Not_found
286
287 let const_univs = function
288 | Arraymaxlength
289 | Stringmaxlength -> AbstractContext.empty
290
291 type 'a prim_type =
292 | PT_int63 : unit prim_type
293 | PT_float64 : unit prim_type
294 | PT_string : unit prim_type
295 | PT_array : (Instance.t * ind_or_type) prim_type
296
297 and 'a prim_ind =
298 | PIT_bool : unit prim_ind
299 | PIT_carry : ind_or_type prim_ind
300 | PIT_pair : (ind_or_type * ind_or_type) prim_ind
301 | PIT_cmp : unit prim_ind
302 | PIT_f_cmp : unit prim_ind
303 | PIT_f_class : unit prim_ind
304
305 and ind_or_type =
306 | PITT_ind : 'a prim_ind * 'a -> ind_or_type
307 | PITT_type : 'a prim_type * 'a -> ind_or_type
308 | PITT_param : int -> ind_or_type (* DeBruijn index referring to prenex type quantifiers *)
309
310 let one_univ =
311 AbstractContext.make { quals = [||]; univs = Names.[|Name (Id.of_string "u")|]} PConstraints.empty
312
313 let typ_univs (type a) (t : a prim_type) = match t with
314 | PT_int63 -> AbstractContext.empty
315 | PT_float64 -> AbstractContext.empty
316 | PT_string -> AbstractContext.empty
317 | PT_array -> one_univ
318
319 type prim_type_ex = PTE : 'a prim_type -> prim_type_ex
320
321 type prim_ind_ex = PIE : 'a prim_ind -> prim_ind_ex
322
323 let types =
324 let int_ty = PITT_type (PT_int63, ()) in
325 let float_ty = PITT_type (PT_float64, ()) in
326 let string_ty = PITT_type (PT_string, ()) in
327 let array_ty =
328 PITT_type
329 (PT_array,
330 (Instance.of_array ([||],[|Level.var 0|]),
331 PITT_param 1))
332 in
333 function
334 | Int63head0 | Int63tail0 ->
335 [int_ty], int_ty
336 | Int63add | Int63sub | Int63mul
337 | Int63div | Int63mod
338 | Int63divs | Int63mods
339 | Int63lsr | Int63lsl | Int63asr
340 | Int63land | Int63lor | Int63lxor ->
341 [int_ty; int_ty], int_ty
342 | Int63addc | Int63subc | Int63addCarryC | Int63subCarryC ->
343 [int_ty; int_ty], PITT_ind (PIT_carry, int_ty)
344 | Int63mulc | Int63diveucl ->
345 [int_ty; int_ty], PITT_ind (PIT_pair, (int_ty, int_ty))
346 | Int63eq | Int63lt | Int63le | Int63lts | Int63les ->
347 [int_ty; int_ty], PITT_ind (PIT_bool, ())
348 | Int63compare | Int63compares ->
349 [int_ty; int_ty], PITT_ind (PIT_cmp, ())
350 | Int63div21 ->
351 [int_ty; int_ty; int_ty], PITT_ind (PIT_pair, (int_ty, int_ty))
352 | Int63addMulDiv ->
353 [int_ty; int_ty; int_ty], int_ty
354 | Float64opp | Float64abs | Float64sqrt
355 | Float64next_up | Float64next_down ->
356 [float_ty], float_ty
357 | Float64ofUint63 ->
358 [int_ty], float_ty
359 | Float64normfr_mantissa ->
360 [float_ty], int_ty
361 | Float64frshiftexp ->
362 [float_ty], PITT_ind (PIT_pair, (float_ty, int_ty))
363 | Float64eq | Float64lt | Float64le | Float64equal ->
364 [float_ty; float_ty], PITT_ind (PIT_bool, ())
365 | Float64compare ->
366 [float_ty; float_ty], PITT_ind (PIT_f_cmp, ())
367 | Float64classify ->
368 [float_ty], PITT_ind (PIT_f_class, ())
369 | Float64add | Float64sub | Float64mul | Float64div ->
370 [float_ty; float_ty], float_ty
371 | Float64ldshiftexp ->
372 [float_ty; int_ty], float_ty
373 | Arraymake ->
374 [int_ty; PITT_param 1], array_ty
375 | Arrayget ->
376 [array_ty; int_ty], PITT_param 1
377 | Arraydefault ->
378 [array_ty], PITT_param 1
379 | Arrayset ->
380 [array_ty; int_ty; PITT_param 1], array_ty
381 | Arraycopy ->
382 [array_ty], array_ty
383 | Arraylength ->
384 [array_ty], int_ty
385 | Stringmake ->
386 [int_ty; int_ty], string_ty
387 | Stringlength ->
388 [string_ty], int_ty
389 | Stringget ->
390 [string_ty; int_ty], int_ty
391 | Stringsub ->
392 [string_ty; int_ty; int_ty], string_ty
393 | Stringcat ->
394 [string_ty; string_ty], string_ty
395 | Stringcompare ->
396 [string_ty; string_ty], PITT_ind (PIT_cmp, ())
397
398 let one_param =
399 (* currently if there's a parameter it's always this *)
400 let a_annot = Context.nameR (Names.Id.of_string "A") in
401 let ty = Constr.mkType (Universe.make (Level.var 0)) in
402 Context.Rel.Declaration.[LocalAssum (a_annot, ty)]
403
404 let params = function
405 | Int63head0
406 | Int63tail0
407 | Int63add
408 | Int63sub
409 | Int63mul
410 | Int63div
411 | Int63mod
412 | Int63divs
413 | Int63mods
414 | Int63lsr
415 | Int63lsl
416 | Int63asr
417 | Int63land
418 | Int63lor
419 | Int63lxor
420 | Int63addc
421 | Int63subc
422 | Int63addCarryC
423 | Int63subCarryC
424 | Int63mulc
425 | Int63diveucl
426 | Int63div21
427 | Int63addMulDiv
428 | Int63eq
429 | Int63lt
430 | Int63le
431 | Int63lts
432 | Int63les
433 | Int63compare
434 | Int63compares
435 | Float64opp
436 | Float64abs
437 | Float64eq
438 | Float64lt
439 | Float64le
440 | Float64compare
441 | Float64equal
442 | Float64classify
443 | Float64add
444 | Float64sub
445 | Float64mul
446 | Float64div
447 | Float64sqrt
448 | Float64ofUint63
449 | Float64normfr_mantissa
450 | Float64frshiftexp
451 | Float64ldshiftexp
452 | Float64next_up
453 | Float64next_down
454 | Stringmake
455 | Stringlength
456 | Stringget
457 | Stringsub
458 | Stringcat
459 | Stringcompare -> []
460
461 | Arraymake
462 | Arrayget
463 | Arraydefault
464 | Arrayset
465 | Arraycopy
466 | Arraylength -> one_param
467
468 let nparams x = List.length (params x)
469
470 let univs = function
471 | Int63head0
472 | Int63tail0
473 | Int63add
474 | Int63sub
475 | Int63mul
476 | Int63div
477 | Int63mod
478 | Int63divs
479 | Int63mods
480 | Int63lsr
481 | Int63lsl
482 | Int63asr
483 | Int63land
484 | Int63lor
485 | Int63lxor
486 | Int63addc
487 | Int63subc
488 | Int63addCarryC
489 | Int63subCarryC
490 | Int63mulc
491 | Int63diveucl
492 | Int63div21
493 | Int63addMulDiv
494 | Int63eq
495 | Int63lt
496 | Int63le
497 | Int63lts
498 | Int63les
499 | Int63compare
500 | Int63compares
501 | Float64opp
502 | Float64abs
503 | Float64eq
504 | Float64lt
505 | Float64le
506 | Float64compare
507 | Float64equal
508 | Float64classify
509 | Float64add
510 | Float64sub
511 | Float64mul
512 | Float64div
513 | Float64sqrt
514 | Float64ofUint63
515 | Float64normfr_mantissa
516 | Float64frshiftexp
517 | Float64ldshiftexp
518 | Float64next_up
519 | Float64next_down
520 | Stringmake
521 | Stringlength
522 | Stringget
523 | Stringsub
524 | Stringcat
525 | Stringcompare -> AbstractContext.empty
526
527 | Arraymake
528 | Arrayget
529 | Arraydefault
530 | Arrayset
531 | Arraycopy
532 | Arraylength -> one_univ
533
534 type arg_kind =
535 | Kparam (* not needed for the evaluation of the primitive when it reduces *)
536 | Kwhnf (* need to be reduced in whnf before reducing the primitive *)
537 | Karg (* no need to be reduced in whnf. example: [v] in [Array.set t i v] *)
538
539 type args_red = arg_kind list
540
541 (* Invariant only argument of type int63, float or an inductive can
542 have kind Kwhnf *)
543
544 let arity t =
545 nparams t + List.length (fst (types t))
546
547 let kind t =
548 let rec params n = if n <= 0 then [] else Kparam :: params (n - 1) in
549 let args = function PITT_type _ | PITT_ind _ -> Kwhnf | PITT_param _ -> Karg in
550 params (nparams t) @ List.map args (fst (types t))
551
552 let types t =
553 let args_ty, ret_ty = types t in
554 params t, args_ty, ret_ty
555
556 (** Special Entries for Register **)
557
558 type op_or_type =
559 | OT_op of t
560 | OT_type : 'a prim_type -> op_or_type
561 | OT_const of const
562
563 let prim_ind_to_string (type a) (p : a prim_ind) = match p with
564 | PIT_bool -> "bool"
565 | PIT_carry -> "carry"
566 | PIT_pair -> "pair"
567 | PIT_cmp -> "cmp"
568 | PIT_f_cmp -> "f_cmp"
569 | PIT_f_class -> "f_class"
570
571 let prim_type_to_string (type a) (ty : a prim_type) = match ty with
572 | PT_int63 -> "int63_type"
573 | PT_float64 -> "float64_type"
574 | PT_string -> "string_type"
575 | PT_array -> "array_type"
576
577 let op_or_type_to_string = function
578 | OT_op op -> to_string op
579 | OT_type t -> prim_type_to_string t
580 | OT_const c -> const_to_string c
581
582 let prim_type_of_string = function
583 | "int63_type" -> PTE PT_int63
584 | "float64_type" -> PTE PT_float64
585 | "string_type" -> PTE PT_string
586 | "array_type" -> PTE PT_array
587 | _ -> raise Not_found
588
589 let op_or_type_of_string s =
590 match prim_type_of_string s with
591 | PTE ty -> OT_type ty
592 | exception Not_found ->
593 begin try OT_op (parse s)
594 with Not_found -> OT_const (const_of_string s)
595 end
596
597 let parse_op_or_type ?loc s =
598 try op_or_type_of_string s
599 with Not_found ->
600 CErrors.user_err ?loc Pp.(str ("Built-in #"^s^" does not exist."))
601
602 let op_or_type_univs = function
603 | OT_op t -> univs t
604 | OT_type t -> typ_univs t
605 | OT_const c -> const_univs c
606
607 let body_of_prim_const = function
608 | Arraymaxlength ->
609 Constr.mkInt (Parray.max_length)
610 | Stringmaxlength ->
611 Constr.mkInt (Pstring.max_length)