Debian Codegraph @

test-suite/output/NumberNotations.v

raw
1 (* Test that we fail, rather than raising anomalies, on opaque terms during interpretation *)
2
3 Declare Scope opaque_scope.
4
5 (* https://github.com/rocq-prover/rocq/pull/8064#discussion_r202497516 *)
6 Module Test1.
7 Axiom hold : forall {A B C}, A -> B -> C.
8 Definition opaque3 (x : Number.int) : Number.int := hold x (fix f (x : nat) : nat := match x with O => O | S n => S (f n) end).
9 Number Notation Number.int opaque3 opaque3 : opaque_scope.
10 Delimit Scope opaque_scope with opaque.
11 Fail Check 1%opaque.
12 End Test1.
13
14 (* https://github.com/rocq-prover/rocq/pull/8064#discussion_r202497990 *)
15 Module Test2.
16 Axiom opaque4 : option Number.int.
17 Definition opaque6 (x : Number.int) : option Number.int := opaque4.
18 Number Notation Number.int opaque6 opaque6 : opaque_scope.
19 Delimit Scope opaque_scope with opaque.
20 Open Scope opaque_scope.
21 Fail Check 1%opaque.
22 End Test2.
23
24 Declare Scope silly_scope.
25
26 Module Test3.
27 Inductive silly := SILLY (v : Number.uint) (f : forall A, A -> A).
28 Definition to_silly (v : Number.uint) := SILLY v (fun _ x => x).
29 Definition of_silly (v : silly) := match v with SILLY v _ => v end.
30 Number Notation silly to_silly of_silly : silly_scope.
31 Delimit Scope silly_scope with silly.
32 Fail Check 1%silly.
33 End Test3.
34
35 Module Test4.
36 Declare Scope opaque_scope.
37 Declare Scope silly_scope.
38 Declare Scope pto.
39 Declare Scope ppo.
40 Declare Scope ptp.
41 Declare Scope ppp.
42 Declare Scope uto.
43 Declare Scope upo.
44 Declare Scope utp.
45 Declare Scope upp.
46 Declare Scope ppps.
47 Polymorphic NonCumulative Inductive punit := ptt.
48 Polymorphic Definition pto_punit (v : Number.uint) : option punit := match Nat.of_num_uint v with O => Some ptt | _ => None end.
49 Polymorphic Definition pto_punit_all (v : Number.uint) : punit := ptt.
50 Polymorphic Definition pof_punit (v : punit) : Number.uint := Nat.to_num_uint 0.
51 Definition to_punit (v : Number.uint) : option punit := match Nat.of_num_uint v with O => Some ptt | _ => None end.
52 Definition of_punit (v : punit) : Number.uint := Nat.to_num_uint 0.
53 Polymorphic Definition pto_unit (v : Number.uint) : option unit := match Nat.of_num_uint v with O => Some tt | _ => None end.
54 Polymorphic Definition pof_unit (v : unit) : Number.uint := Nat.to_num_uint 0.
55 Definition to_unit (v : Number.uint) : option unit := match Nat.of_num_uint v with O => Some tt | _ => None end.
56 Definition of_unit (v : unit) : Number.uint := Nat.to_num_uint 0.
57 Number Notation punit to_punit of_punit : pto.
58 Number Notation punit pto_punit of_punit : ppo.
59 Number Notation punit to_punit pof_punit : ptp.
60 Number Notation punit pto_punit pof_punit : ppp.
61 Number Notation unit to_unit of_unit : uto.
62 Delimit Scope pto with pto.
63 Delimit Scope ppo with ppo.
64 Delimit Scope ptp with ptp.
65 Delimit Scope ppp with ppp.
66 Delimit Scope uto with uto.
67 Check let v := 0%pto in v : punit.
68 Check let v := 0%ppo in v : punit.
69 Check let v := 0%ptp in v : punit.
70 Check let v := 0%ppp in v : punit.
71 Check let v := 0%uto in v : unit.
72 Fail Check 1%uto.
73 Fail Check (-1)%uto.
74 Number Notation unit pto_unit of_unit : upo.
75 Number Notation unit to_unit pof_unit : utp.
76 Number Notation unit pto_unit pof_unit : upp.
77 Delimit Scope upo with upo.
78 Delimit Scope utp with utp.
79 Delimit Scope upp with upp.
80 Check let v := 0%upo in v : unit.
81 Check let v := 0%utp in v : unit.
82 Check let v := 0%upp in v : unit.
83
84 Polymorphic Definition pto_punits := pto_punit_all@{Set}.
85 Polymorphic Definition pof_punits := pof_punit@{Set}.
86 Number Notation punit pto_punits pof_punits (abstract after 0) : ppps.
87 Delimit Scope ppps with ppps.
88 Universe u.
89 Constraint Set < u.
90 Check let v := 0%ppps in v : punit@{u}. (* Check that universes are refreshed *)
91 Fail Check let v := 1%ppps in v : punit@{u}. (* Note that universes are not refreshed here *)
92 End Test4.
93
94 Module Test5.
95 Check S. (* At one point gave Error: Anomaly "Uncaught exception Pretype_errors.PretypeError(_, _, _)." Please report at http://coq.inria.fr/bugs/. *)
96 End Test5.
97
98 Module Test6.
99 (* Check that number notations on enormous terms don't take forever to print/parse *)
100 (* Ackerman definition from https://stackoverflow.com/a/10303475/377022 *)
101 Fixpoint ack (n m : nat) : nat :=
102 match n with
103 | O => S m
104 | S p => let fix ackn (m : nat) :=
105 match m with
106 | O => ack p 1
107 | S q => ack p (ackn q)
108 end
109 in ackn m
110 end.
111
112 Timeout 1 Check (S (ack 4 4)). (* should be instantaneous *)
113
114 Local Set Primitive Projections.
115 Record > wnat := wrap { unwrap :> nat }.
116 Definition to_uint (x : wnat) : Number.uint := Nat.to_num_uint x.
117 Definition of_uint (x : Number.uint) : wnat := Nat.of_num_uint x.
118 Module Export Scopes.
119 Declare Scope wnat_scope.
120 Delimit Scope wnat_scope with wnat.
121 End Scopes.
122 Module Export Notations.
123 Export Scopes.
124 Number Notation wnat of_uint to_uint (abstract after 4999) : wnat_scope.
125 End Notations.
126 Set Printing Coercions.
127 Check let v := 0%wnat in v : wnat.
128 Check wrap O.
129 Timeout 1 Check wrap (ack 4 4). (* should be instantaneous *)
130 End Test6.
131
132 Module Test6_2.
133 Import Test6.Scopes.
134 Check Test6.wrap 0.
135 Import Test6.Notations.
136 Check let v := 0%wnat in v : Test6.wnat.
137 End Test6_2.
138
139 Module Test7.
140 Local Set Primitive Projections.
141 Record wuint := wrap { unwrap : Number.uint }.
142 Declare Scope wuint_scope.
143 Delimit Scope wuint_scope with wuint.
144 Number Notation wuint wrap unwrap : wuint_scope.
145 Check let v := 0%wuint in v : wuint.
146 Check let v := 1%wuint in v : wuint.
147 End Test7.
148
149 Module Test8.
150 Local Set Primitive Projections.
151 Record wuint := wrap { unwrap : Number.uint }.
152 Declare Scope wuint8_scope.
153 Declare Scope wuint8'_scope.
154 Delimit Scope wuint8_scope with wuint8.
155 Delimit Scope wuint8'_scope with wuint8'.
156 Section with_var.
157 Context (dummy : unit).
158 Definition wrap' := let __ := dummy in wrap.
159 Definition unwrap' := let __ := dummy in unwrap.
160 Number Notation wuint wrap' unwrap' : wuint8_scope.
161 Check let v := 0%wuint8 in v : wuint.
162 End with_var.
163 Check let v := 0%wuint8 in v : nat.
164 Fail Check let v := 0%wuint8 in v : wuint.
165 Compute wrap (Nat.to_num_uint 0).
166
167 Abbreviation wrap'' := wrap.
168 Abbreviation unwrap'' := unwrap.
169 Number Notation wuint wrap'' unwrap'' : wuint8'_scope.
170 Check let v := 0%wuint8' in v : wuint.
171 End Test8.
172
173 Module Test9.
174 Declare Scope wuint9_scope.
175 Declare Scope wuint9'_scope.
176 Delimit Scope wuint9_scope with wuint9.
177 Delimit Scope wuint9'_scope with wuint9'.
178 Section with_let.
179 Local Set Primitive Projections.
180 Record wuint := wrap { unwrap : Number.uint }.
181 Let wrap' := wrap.
182 Let unwrap' := unwrap.
183 Local Abbreviation wrap'' := wrap.
184 Local Abbreviation unwrap'' := unwrap.
185 Number Notation wuint wrap' unwrap' : wuint9_scope.
186 Check let v := 0%wuint9 in v : wuint.
187 Number Notation wuint wrap'' unwrap'' : wuint9'_scope.
188 Check let v := 0%wuint9' in v : wuint.
189 End with_let.
190 Check let v := 0%wuint9 in v : nat.
191 Fail Check let v := 0%wuint9 in v : wuint.
192 End Test9.
193
194 Module Test10.
195 (* Test that it is only a warning to add abstract after to an optional parsing function *)
196 Definition to_uint (v : unit) := Nat.to_num_uint 0.
197 Definition of_uint (v : Number.uint) := match Nat.of_num_uint v with O => Some tt | _ => None end.
198 Definition of_any_uint (v : Number.uint) := tt.
199 Declare Scope unit_scope.
200 Declare Scope unit2_scope.
201 Delimit Scope unit_scope with unit.
202 Delimit Scope unit2_scope with unit2.
203 Number Notation unit of_uint to_uint (abstract after 0) : unit_scope.
204 Local Set Warnings "+abstract-large-number-no-op".
205 (* Check that there is actually a warning here *)
206 Fail Number Notation unit of_uint to_uint (abstract after 0) : unit2_scope.
207 (* Check that there is no warning here *)
208 Number Notation unit of_any_uint to_uint (abstract after 0) : unit2_scope.
209 End Test10.
210
211 Module Test12.
212 (* Test for number notations on context variables *)
213 Declare Scope test12_scope.
214 Delimit Scope test12_scope with test12.
215 Section test12.
216 Context (to_uint : unit -> Number.uint) (of_uint : Number.uint -> unit).
217
218 Number Notation unit of_uint to_uint : test12_scope.
219 Check let v := 1%test12 in v : unit.
220 End test12.
221 End Test12.
222
223 Module Test13.
224 (* Test for number notations on notations which do not denote references *)
225 Declare Scope test13_scope.
226 Declare Scope test13'_scope.
227 Declare Scope test13''_scope.
228 Delimit Scope test13_scope with test13.
229 Delimit Scope test13'_scope with test13'.
230 Delimit Scope test13''_scope with test13''.
231 Definition to_uint (x y : unit) : Number.uint := Nat.to_num_uint O.
232 Definition of_uint (x : Number.uint) : unit := tt.
233 Definition to_uint_good := to_uint tt.
234 Abbreviation to_uint' := (to_uint tt).
235 Abbreviation to_uint'' := (to_uint _).
236 Number Notation unit of_uint to_uint_good : test13_scope.
237 Check let v := 0%test13 in v : unit.
238 Fail Number Notation unit of_uint to_uint' : test13'_scope.
239 Fail Check let v := 0%test13' in v : unit.
240 Fail Number Notation unit of_uint to_uint'' : test13''_scope.
241 Fail Check let v := 0%test13'' in v : unit.
242 End Test13.
243
244 Module Test14.
245 (* Test that number notations follow [Import], not [Require], and
246 also test that [Local Number Notation]s do not escape modules
247 nor sections. *)
248 Declare Scope test14_scope.
249 Declare Scope test14'_scope.
250 Declare Scope test14''_scope.
251 Declare Scope test14'''_scope.
252 Delimit Scope test14_scope with test14.
253 Delimit Scope test14'_scope with test14'.
254 Delimit Scope test14''_scope with test14''.
255 Delimit Scope test14'''_scope with test14'''.
256 Module Inner.
257 Definition to_uint (x : unit) : Number.uint := Nat.to_num_uint O.
258 Definition of_uint (x : Number.uint) : unit := tt.
259 Local Number Notation unit of_uint to_uint : test14_scope.
260 Global Number Notation unit of_uint to_uint : test14'_scope.
261 Check let v := 0%test14 in v : unit.
262 Check let v := 0%test14' in v : unit.
263 End Inner.
264 Fail Check let v := 0%test14 in v : unit.
265 Fail Check let v := 0%test14' in v : unit.
266 Import Inner.
267 Fail Check let v := 0%test14 in v : unit.
268 Check let v := 0%test14' in v : unit.
269 Section InnerSection.
270 Definition to_uint (x : unit) : Number.uint := Nat.to_num_uint O.
271 Definition of_uint (x : Number.uint) : unit := tt.
272 Local Number Notation unit of_uint to_uint : test14''_scope.
273 Fail Global Number Notation unit of_uint to_uint : test14'''_scope.
274 Check let v := 0%test14'' in v : unit.
275 Fail Check let v := 0%test14''' in v : unit.
276 End InnerSection.
277 Fail Check let v := 0%test14'' in v : unit.
278 Fail Check let v := 0%test14''' in v : unit.
279 End Test14.
280
281 Module Test15.
282 (** Test module include *)
283 Declare Scope test15_scope.
284 Delimit Scope test15_scope with test15.
285 Module Inner.
286 Definition to_uint (x : unit) : Number.uint := Nat.to_num_uint O.
287 Definition of_uint (x : Number.uint) : unit := tt.
288 Number Notation unit of_uint to_uint : test15_scope.
289 Check let v := 0%test15 in v : unit.
290 End Inner.
291 Module Inner2.
292 Include Inner.
293 Check let v := 0%test15 in v : unit.
294 End Inner2.
295 Import Inner Inner2.
296 Check let v := 0%test15 in v : unit.
297 End Test15.
298
299 Module Test16.
300 (** Test functors *)
301 Declare Scope test16_scope.
302 Delimit Scope test16_scope with test16.
303 Module Type A.
304 Axiom T : Set.
305 Axiom t : T.
306 End A.
307 Module F (a : A).
308 Inductive Foo := foo (_ : a.T).
309 Definition to_uint (x : Foo) : Number.uint := Nat.to_num_uint O.
310 Definition of_uint (x : Number.uint) : Foo := foo a.t.
311 Global Number Notation Foo of_uint to_uint : test16_scope.
312 Check let v := 0%test16 in v : Foo.
313 End F.
314 Module a <: A.
315 Definition T : Set := unit.
316 Definition t : T := tt.
317 End a.
318 Module Import f := F a.
319 (** Ideally this should work, but it should definitely not anomaly *)
320 Fail Check let v := 0%test16 in v : Foo.
321 End Test16.
322
323 Require Import PrimInt63.
324 Module Test17.
325 (** Test uint63 *)
326 Declare Scope test17_scope.
327 Declare Scope test17_scope.
328 Delimit Scope test17_scope with test17.
329 Local Set Primitive Projections.
330 Record myint63 := of_int { to_int : int }.
331 Definition parse x :=
332 match x with Pos x => Some (of_int x) | Neg _ => None end.
333 Definition print x := Pos (to_int x).
334 Number Notation myint63 parse print : test17_scope.
335 Check let v := 0%test17 in v : myint63.
336 End Test17.
337
338 Module Test18.
339 (** Test https://github.com/rocq-prover/rocq/issues/9840 *)
340 Record Q := { num : nat ; den : nat ; reduced : Nat.gcd num den = 1 }.
341 Declare Scope Q_scope.
342 Delimit Scope Q_scope with Q.
343
344 Definition nat_eq_dec (x y : nat) : {x = y} + {x <> y}.
345 Proof. decide equality. Defined.
346
347 Definition transparentify {A} (D : {A} + {not A}) (H : A) : A :=
348 match D with
349 | left pf => pf
350 | right npf => match npf H with end
351 end.
352
353 Axiom gcd_good : forall x, Nat.gcd x 1 = 1.
354
355 Definition Q_of_nat (x : nat) : Q := {| num := x ; den := 1 ; reduced := transparentify (nat_eq_dec _ _) (gcd_good _) |}.
356 Definition nat_of_Q (x : Q) : option nat
357 := if Nat.eqb x.(den) 1 then Some (x.(num)) else None.
358 Definition Q_of_uint (x : Number.uint) : Q := Q_of_nat (Nat.of_num_uint x).
359 Definition uint_of_Q (x : Q) : option Number.uint
360 := option_map Nat.to_num_uint (nat_of_Q x).
361
362 Number Notation Q Q_of_uint uint_of_Q : Q_scope.
363
364 Check let v := 0%Q in v : Q.
365 Check let v := 1%Q in v : Q.
366 Check let v := 2%Q in v : Q.
367 Check let v := 3%Q in v : Q.
368 Check let v := 4%Q in v : Q.
369 Compute let v := 0%Q in (num v, den v).
370 Compute let v := 1%Q in (num v, den v).
371 Compute let v := 2%Q in (num v, den v).
372 Compute let v := 3%Q in (num v, den v).
373 Compute let v := 4%Q in (num v, den v).
374 End Test18.
375
376 Require Import Corelib.Lists.ListDef.
377 Require Import BinNums IntDef.
378 Module Test19.
379 (** Test another thing related to https://github.com/rocq-prover/rocq/issues/9840 *)
380 Record Zlike := { summands : list Z }.
381 Declare Scope Zlike_scope.
382 Delimit Scope Zlike_scope with Zlike.
383
384 Section Fold_Right_Recursor.
385 Variables (A : Type) (B : Type).
386 Variable f : B -> A -> A.
387 Variable a0 : A.
388
389 Fixpoint fold_right (l:list B) : A :=
390 match l with
391 | nil => a0
392 | cons b t => f b (fold_right t)
393 end.
394 End Fold_Right_Recursor.
395
396 Definition Z_of_Zlike (x : Zlike) := fold_right _ _ Z.add Z0 (summands x).
397 Definition Zlike_of_Z (x : Z) := {| summands := cons x nil |}.
398
399 Number Notation Zlike Zlike_of_Z Z_of_Zlike : Zlike_scope.
400
401 Check let v := (-1)%Zlike in v : Zlike.
402 Check let v := 0%Zlike in v : Zlike.
403 Check let v := 1%Zlike in v : Zlike.
404 Check let v := 2%Zlike in v : Zlike.
405 Check let v := 3%Zlike in v : Zlike.
406 Check let v := 4%Zlike in v : Zlike.
407 Check {| summands := cons (Zpos xH) (cons (Zpos (xO xH)) (cons (Zneg xH) nil)) |}.
408 Check {| summands := nil |}.
409 End Test19.
410
411 Module Test20.
412 (** Test Sorts *)
413 Local Set Universe Polymorphism.
414 Inductive known_type : Type -> Type :=
415 | prop : known_type Prop
416 | set : known_type Set
417 | type : known_type Type
418 | zero : known_type Empty_set
419 | one : known_type unit
420 | two : known_type bool.
421
422 Existing Class known_type.
423 #[global]
424 Existing Instances zero one two prop.
425 #[global]
426 Existing Instance set | 2.
427 #[global]
428 Existing Instance type | 4.
429
430 Record > ty := { t : Type ; kt : known_type t }.
431
432 Definition ty_of_uint (x : Number.uint) : option ty
433 := match Nat.of_num_uint x with
434 | 0 => @Some ty zero
435 | 1 => @Some ty one
436 | 2 => @Some ty two
437 | 3 => @Some ty prop
438 | 4 => @Some ty set
439 | 5 => @Some ty type
440 | _ => None
441 end.
442 Definition uint_of_ty (x : ty) : Number.uint
443 := Nat.to_num_uint match kt x with
444 | prop => 3
445 | set => 4
446 | type => 5
447 | zero => 0
448 | one => 1
449 | two => 2
450 end.
451
452 Declare Scope kt_scope.
453 Delimit Scope kt_scope with kt.
454
455 Number Notation ty ty_of_uint uint_of_ty : kt_scope.
456
457 Check let v := 0%kt in v : ty.
458 Check let v := 1%kt in v : ty.
459 Check let v := 2%kt in v : ty.
460 Check let v := 3%kt in v : ty.
461 Check let v := 4%kt in v : ty.
462 Check let v := 5%kt in v : ty.
463 Fail Check let v := 6%kt in v : ty.
464 Eval cbv in (_ : known_type Empty_set) : ty.
465 Eval cbv in (_ : known_type unit) : ty.
466 Eval cbv in (_ : known_type bool) : ty.
467 Eval cbv in (_ : known_type Prop) : ty.
468 Eval cbv in (_ : known_type Set) : ty.
469 Eval cbv in (_ : known_type Type) : ty.
470 Local Set Printing All.
471 Check let v := 0%kt in v : ty.
472 Check let v := 1%kt in v : ty.
473 Check let v := 2%kt in v : ty.
474 Check let v := 3%kt in v : ty.
475 Check let v := 4%kt in v : ty.
476 Check let v := 5%kt in v : ty.
477 End Test20.
478
479 Module Test21.
480
481 Check 00001.
482 Check 1_000.
483
484 End Test21.
485
486 Module Test22.
487
488 Notation "0" := False.
489 Notation "+0" := true.
490 Notation "-0" := false.
491 Notation "00" := (0%nat, 0%nat).
492 Check 0.
493 Check +0.
494 Check -0.
495 Check 00.
496
497 Notation "1000" := True.
498 Notation "1_000" := (cons 1 nil).
499 Check 1000.
500 Check 1_000.
501
502 (* To do: preserve parsing of -0:
503 Require Import ZArith.
504 Check (-0)%Z.
505 *)
506
507 End Test22.
508
509 (* Test the via ... mapping ... option *)
510 Module Test23.
511
512 Inductive sum (A : Set) (B : Set) : Set := pair : A -> B -> sum A B.
513
514 Inductive I :=
515 | Iempty : I
516 | Iunit : I
517 | Isum : I -> I -> I.
518
519 Definition of_uint (x : Number.uint) : I :=
520 let fix f n :=
521 match n with
522 | O => Iempty
523 | S O => Iunit
524 | S n => Isum Iunit (f n)
525 end in
526 f (Nat.of_num_uint x).
527
528 Definition to_uint (x : I) : Number.uint :=
529 let fix f i :=
530 match i with
531 | Iempty => O
532 | Iunit => 1
533 | Isum i1 i2 => f i1 + f i2
534 end in
535 Nat.to_num_uint (f x).
536
537 Abbreviation nSet := (Set) (only parsing). (* needed as a reference is expected in Number Notation and Set is syntactically not a reference *)
538 Number Notation nSet of_uint to_uint (via I
539 mapping [Empty_set => Iempty, unit => Iunit, sum => Isum])
540 : type_scope.
541
542 Local Open Scope type_scope.
543
544 Check Empty_set.
545 Check unit.
546 Check sum unit unit.
547 Check sum unit (sum unit unit).
548 Set Printing All.
549 Check 0.
550 Check 1.
551 Check 2.
552 Check 3.
553 Unset Printing All.
554
555 (* Test error messages *)
556
557 (* missing constructor *)
558 Fail Number Notation nSet of_uint to_uint (via I
559 mapping [Empty_set => Iempty, unit => Iunit])
560 : type_scope.
561
562 (* duplicate constructor *)
563 Fail Number Notation nSet of_uint to_uint (via I
564 mapping [Empty_set => Iempty, unit => Iunit, sum => Isum, unit => Iunit])
565 : type_scope.
566
567 (* not an inductive *)
568 Fail Number Notation nSet of_uint to_uint (via add
569 mapping [Empty_set => Iempty, unit => Iunit, sum => Isum])
570 : type_scope.
571
572 (* not a constructor *)
573 Fail Number Notation nSet of_uint to_uint (via I
574 mapping [Empty_set => Iempty, unit => add, sum => Isum])
575 : type_scope.
576
577 (* put constructors of the wrong inductive ~~> missing constructors *)
578 Fail Number Notation nSet of_uint to_uint (via I
579 mapping [Empty_set => O, unit => S])
580 : type_scope.
581
582 (* Test warnings *)
583
584 (* wrong type *)
585 Inductive I' :=
586 | I'empty : I'
587 | I'unit : I'
588 | I'sum : I -> I' -> I'.
589 Definition of_uint' (x : Number.uint) : I' := I'empty.
590 Definition to_uint' (x : I') : Number.uint := Number.UIntDecimal Decimal.Nil.
591 Number Notation nSet of_uint' to_uint' (via I'
592 mapping [Empty_set => I'empty, unit => I'unit, sum => I'sum])
593 : type_scope.
594
595 (* wrong type mapping *)
596 Number Notation nSet of_uint to_uint (via I
597 mapping [Empty_set => Iempty, O => Iunit, sum => Isum])
598 : type_scope.
599
600 (* incompatibility with abstract (but warning is fine) *)
601 Fail Number Notation nSet of_uint to_uint (via I
602 mapping [Empty_set => Iempty, unit => Iunit, sum => Isum],
603 abstract after 11)
604 : type_scope.
605 Number Notation nSet of_uint to_uint (via I
606 mapping [Empty_set => Iempty, unit => Iunit, sum => Isum],
607 warning after 12)
608 : type_scope.
609
610 (* Test reduction of types when building the notation *)
611
612 Inductive foo := bar : match (true <: bool) with true => nat -> foo | false => True end.
613
614 Definition foo_of_uint (x : Number.uint) : foo := bar (Nat.of_num_uint x).
615 Definition foo_to_uint (x : foo) : Number.uint :=
616 match x with
617 | bar x => Nat.to_num_uint x
618 end.
619
620 Number Notation foo foo_of_uint foo_to_uint (via foo mapping [bar => bar])
621 : type_scope.
622
623 Inductive foo' := bar' : let n := nat in n -> foo'.
624
625 Definition foo'_of_uint (x : Number.uint) : foo' := bar' (Nat.of_num_uint x).
626 Definition foo'_to_uint (x : foo') : Number.uint :=
627 match x with
628 | bar' x => Nat.to_num_uint x
629 end.
630
631 Number Notation foo' foo'_of_uint foo'_to_uint (via foo' mapping [bar' => bar'])
632 : type_scope.
633
634 Inductive foo'' := bar'' : (nat <: Type) -> (foo'' <: Type).
635
636 Definition foo''_of_uint (x : Number.uint) : foo'' := bar'' (Nat.of_num_uint x).
637 Definition foo''_to_uint (x : foo'') : Number.uint :=
638 match x with
639 | bar'' x => Nat.to_num_uint x
640 end.
641
642 Number Notation foo'' foo''_of_uint foo''_to_uint (via foo'' mapping [bar'' => bar''])
643 : type_scope.
644
645 End Test23.
646
647 (* Test the via ... mapping ... option with implicit arguments *)
648 Module Test24.
649
650 Inductive I :=
651 | I1 : I
652 | IS : I -> I.
653
654 Definition of_uint (x : Number.uint) : I :=
655 let fix f n :=
656 match n with
657 | O => I1
658 | S n => IS (f n)
659 end in
660 f (Nat.of_num_uint x).
661
662 Definition to_uint (x : I) : Number.uint :=
663 let fix f i :=
664 match i with
665 | I1 => O
666 | IS n => S (f n)
667 end in
668 Nat.to_num_uint (f x).
669
670 Local Open Scope type_scope.
671
672 Module Fin.
673 Inductive t : nat -> Set :=
674 |F1 : forall {n}, t (S n)
675 |FS : forall {n}, t n -> t (S n).
676 End Fin.
677
678 (* ignoring implicit arguments doesn't work *)
679 Number Notation Fin.t of_uint to_uint (via I
680 mapping [Fin.F1 => I1, Fin.FS => IS])
681 : type_scope.
682
683 Fail Check 1.
684
685 Number Notation Fin.t of_uint to_uint (via I
686 mapping [[Fin.F1] => I1, [Fin.FS] => IS])
687 : type_scope.
688
689 Check Fin.F1.
690 Check Fin.FS Fin.F1.
691 Check Fin.FS (Fin.FS Fin.F1).
692 Check Fin.FS (Fin.FS (Fin.FS Fin.F1)).
693 Check Fin.F1 : Fin.t 3.
694 Check Fin.FS Fin.F1 : Fin.t 3.
695 Check Fin.FS (Fin.FS Fin.F1) : Fin.t 3.
696 Fail Check Fin.FS (Fin.FS (Fin.FS Fin.F1)) : Fin.t 3.
697 Set Printing All.
698 Check 0.
699 Check 1.
700 Check 2.
701 Check 3.
702 Check 0 : Fin.t 3.
703 Check 1 : Fin.t 3.
704 Check 2 : Fin.t 3.
705 Fail Check 3 : Fin.t 3.
706 Unset Printing All.
707
708 End Test24.
709
710 (* Test number notations for parameterized inductives *)
711 Module Test25.
712
713 Definition of_uint (u : Number.uint) : list unit :=
714 let fix f n :=
715 match n with
716 | O => nil
717 | S n => cons tt (f n)
718 end in
719 f (Nat.of_num_uint u).
720
721 Definition to_uint (l : list unit) : Number.uint :=
722 let fix f n :=
723 match n with
724 | nil => O
725 | cons tt l => S (f l)
726 end in
727 Nat.to_num_uint (f l).
728
729 Abbreviation listunit := (list unit) (only parsing).
730 Number Notation listunit of_uint to_uint : nat_scope.
731
732 Check 0.
733 Check 1.
734 Check 2.
735
736 Check cons tt (cons tt nil).
737 Check cons O (cons O nil). (* printer not called on list nat *)
738
739 (* inductive with multiple parameters that are not the first
740 parameters and not in the same order for each constructor *)
741 Inductive Ip : Type -> Type -> Type :=
742 | Ip0 : forall T T', nat -> Ip T T'
743 | Ip1 : forall T' T, nat -> Ip T T'
744 | Ip2 : forall T, nat -> forall T', Ip T T'
745 | Ip3 : nat -> forall T T', Ip T T'.
746
747 Definition Ip_of_uint (u : Number.uint) : option (Ip nat bool) :=
748 let f n :=
749 match n with
750 | O => Some (Ip0 nat bool O)
751 | S O => Some (Ip1 bool nat (S O))
752 | S (S O) => Some (Ip2 nat (S (S O)) bool)
753 | S (S (S O)) => Some (Ip3 (S (S (S O))) nat bool)
754 | _ => None
755 end in
756 f (Nat.of_num_uint u).
757
758 Definition Ip_to_uint (l : Ip nat bool) : Number.uint :=
759 let f n :=
760 match n with
761 | Ip0 _ _ n => n
762 | Ip1 _ _ n => n
763 | Ip2 _ n _ => n
764 | Ip3 n _ _ => n
765 end in
766 Nat.to_num_uint (f l).
767
768 Abbreviation Ip_nat_bool := (Ip nat bool) (only parsing).
769 Number Notation Ip_nat_bool Ip_of_uint Ip_to_uint : nat_scope.
770
771 Check 0.
772 Check 1.
773 Check 2.
774 Check 3.
775 Check Ip0 nat bool (S O).
776 Check Ip1 bool nat (S O).
777 Check Ip2 nat (S O) bool.
778 Check Ip3 (S O) nat bool.
779 Check Ip0 nat nat (S O). (* not printed *)
780 Check Ip0 bool bool (S O). (* not printed *)
781 Check Ip1 nat nat (S O). (* not printed *)
782 Check Ip3 (S O) nat nat. (* not printed *)
783 Set Printing All.
784 Check 0.
785 Check 1.
786 Check 2.
787 Check 3.
788 Unset Printing All.
789
790 Abbreviation eqO := (eq _ O) (only parsing).
791 Definition eqO_of_uint (x : Number.uint) : eqO := eq_refl O.
792 Definition eqO_to_uint (x : O = O) : Number.uint :=
793 match x with
794 | eq_refl _ => Nat.to_num_uint O
795 end.
796 Number Notation eqO eqO_of_uint eqO_to_uint : nat_scope.
797
798 Check 42.
799 Check eq_refl (S O). (* doesn't match eq _ O, printer not called *)
800 Check eq_refl O. (* matches eq _ O, printer called *)
801 Check eq_refl (id O). (* doesn't match eq _ O, printer not called *)
802
803 Abbreviation eq_ := (eq _ _) (only parsing).
804 Number Notation eq_ eqO_of_uint eqO_to_uint : nat_scope.
805
806 Check eq_refl (S O). (* matches eq _ _, printer called, but type incorrect *)
807 Check eq_refl O. (* matches eq _ _, printer called *)
808 Check eq_refl (id O). (* matches eq _ _, but contains a global constant, printer not called *)
809
810 Inductive extra_list : Type -> Type :=
811 | nil (n : nat) (v : Type) : extra_list v
812 | cons (n : nat) (t : Type) (x : t) : extra_list t -> extra_list t.
813
814 Definition extra_list_unit_of_uint (x : Number.uint) : extra_list unit :=
815 let fix f n :=
816 match n with
817 | O => nil O unit
818 | S n => cons O unit tt (f n)
819 end in
820 f (Nat.of_num_uint x).
821
822 Definition extra_list_unit_to_uint (x : extra_list unit) : Number.uint :=
823 let fix f T (x : extra_list T) :=
824 match x with
825 | nil _ _ => O
826 | cons _ T _ x => S (f T x)
827 end in
828 Nat.to_num_uint (f unit x).
829
830 Abbreviation extra_list_unit := (extra_list unit).
831 Number Notation extra_list_unit
832 extra_list_unit_of_uint extra_list_unit_to_uint : nat_scope.
833
834 Check 2.
835 Set Printing All.
836 Check 2.
837 Unset Printing All.
838
839 End Test25.
840
841 (* Test the via ... mapping ... option with let-binders, beta-redexes, delta-redexes, etc *)
842 Module Test26.
843
844 Inductive sum (A : Set) (B : Set) : Set := pair : A -> B -> sum A B.
845
846 Inductive I (dummy:=O) :=
847 | Iempty : let v := I in id v
848 | Iunit : (fun x => x) I
849 | Isum : let v := I in (fun A B => A -> B) (let v' := v in v') (forall x : match O with O => I | _ => Empty_set end, let dummy2 := x in I).
850
851 Definition of_uint (x : (fun x => let v := I in x) Number.uint) : (fun x => let v := I in x) I :=
852 let fix f n :=
853 match n with
854 | O => Iempty
855 | S O => Iunit
856 | S n => Isum Iunit (f n)
857 end in
858 f (Nat.of_num_uint x).
859
860 Definition to_uint (x : (fun x => let v := x in v) I) : match O with O => Number.uint | _ => Empty_set end :=
861 let fix f i :=
862 match i with
863 | Iempty => O
864 | Iunit => 1
865 | Isum i1 i2 => f i1 + f i2
866 end in
867 Nat.to_num_uint (f x).
868
869 Abbreviation nSet := (Set) (only parsing). (* needed as a reference is expected in Number Notation and Set is syntactically not a reference *)
870 Number Notation nSet of_uint to_uint (via I
871 mapping [Empty_set => Iempty, unit => Iunit, sum => Isum])
872 : type_scope.
873
874 Local Open Scope type_scope.
875
876 Check Empty_set.
877 Check unit.
878 Check sum unit unit.
879 Check sum unit (sum unit unit).
880 Set Printing All.
881 Check 0.
882 Check 1.
883 Check 2.
884 Check 3.
885 Unset Printing All.
886 End Test26.
887
888 (* Test the via ... mapping ... option with implicit arguments with let binders, etc *)
889 Module Test27.
890
891 Module Fin.
892 Inductive t0 (x:=O) :=
893 with
894 t (x:=O) : forall y : nat, let z := y in Set :=
895 | F1 (y:=O) {n} : match y with O => t (S n) | _ => Empty_set end
896 | FS (y:=x) {n} (v:=n+y) (m:=n) : id (match y with O => id (t n) | _ => Empty_set end -> (fun x => x) t (S m))
897 with t' (x:=O) := .
898 End Fin.
899
900 Inductive I (dummy:=O) :=
901 | I1 : I
902 | IS : let x := I in id x -> I.
903
904 Definition of_uint (x : Number.uint) : I :=
905 let fix f n :=
906 match n with
907 | O => I1
908 | S n => IS (f n)
909 end in
910 f (Nat.of_num_uint x).
911
912 Definition to_uint (x : I) : Number.uint :=
913 let fix f i :=
914 match i with
915 | I1 => O
916 | IS n => S (f n)
917 end in
918 Nat.to_num_uint (f x).
919
920 Local Open Scope type_scope.
921
922 Number Notation Fin.t of_uint to_uint (via I
923 mapping [[Fin.F1] => I1, [Fin.FS] => IS])
924 : type_scope.
925
926 Check Fin.F1.
927 Check Fin.FS Fin.F1.
928 Check Fin.FS (Fin.FS Fin.F1).
929 Check Fin.FS (Fin.FS (Fin.FS Fin.F1)).
930 Check Fin.F1 : Fin.t 3.
931 Check Fin.FS Fin.F1 : Fin.t 3.
932 Check Fin.FS (Fin.FS Fin.F1) : Fin.t 3.
933 Fail Check Fin.FS (Fin.FS (Fin.FS Fin.F1)) : Fin.t 3.
934 Set Printing All.
935 Check 0.
936 Check 1.
937 Check 2.
938 Check 3.
939 Check 0 : Fin.t 3.
940 Check 1 : Fin.t 3.
941 Check 2 : Fin.t 3.
942 Fail Check 3 : Fin.t 3.
943 Unset Printing All.
944
945 End Test27.
946
947 Module Test28.
948 Module Fin.
949 Inductive t : nat -> Set :=
950 | F1 {n : (nat : Set)} : (t (S n) : Set)
951 | FS {n : (nat : Set)} : (t n : Set) -> (t (S n) : Set).
952 End Fin.
953
954 Inductive I :=
955 | I1 : I
956 | IS : I -> I.
957
958 Definition of_uint (x : Number.uint) : I :=
959 let fix f n :=
960 match n with
961 | O => I1
962 | S n => IS (f n)
963 end in
964 f (Nat.of_num_uint x).
965
966 Definition to_uint (x : I) : Number.uint :=
967 let fix f i :=
968 match i with
969 | I1 => O
970 | IS n => S (f n)
971 end in
972 Nat.to_num_uint (f x).
973
974 Local Open Scope type_scope.
975
976 Number Notation Fin.t of_uint to_uint (via I
977 mapping [[Fin.F1] => I1, [Fin.FS] => IS])
978 : type_scope.
979
980 Check Fin.F1.
981 Check Fin.FS Fin.F1.
982 Check Fin.FS (Fin.FS Fin.F1).
983 Check Fin.FS (Fin.FS (Fin.FS Fin.F1)).
984 Check Fin.F1 : Fin.t 3.
985 Check Fin.FS Fin.F1 : Fin.t 3.
986 Check Fin.FS (Fin.FS Fin.F1) : Fin.t 3.
987 Fail Check Fin.FS (Fin.FS (Fin.FS Fin.F1)) : Fin.t 3.
988 Set Printing All.
989 Check 0.
990 Check 1.
991 Check 2.
992 Check 3.
993 Check 0 : Fin.t 3.
994 Check 1 : Fin.t 3.
995 Check 2 : Fin.t 3.
996 Fail Check 3 : Fin.t 3.
997 Unset Printing All.
998
999 End Test28.
1000
1001 Require Import PrimFloat.
1002
1003 Module Test29.
1004
1005 Definition printer (x : float_wrapper) : Number.uint :=
1006 if get_sign (float_wrap x) then Number.UIntDecimal (Decimal.D1 Decimal.Nil)
1007 else Number.UIntDecimal (Decimal.D0 Decimal.Nil).
1008
1009 Definition parser (x : float) : float := x.
1010
1011 Number Notation float parser printer : float_scope.
1012
1013 Check 12%float.
1014 Check (-12)%float.
1015 Check infinity.
1016 Check neg_infinity.
1017 Check nan.
1018
1019 End Test29.
1020
1021 Module Test30.
1022
1023 Inductive nunit : nat -> Type := NUnit n : nunit n.
1024
1025 Definition printer2 (x : nunit 2) : Number.uint :=
1026 Number.UIntDecimal (Decimal.D2 Decimal.Nil).
1027
1028 Definition parser2 (_ : Number.uint) : nunit 2 := NUnit 2.
1029
1030 Abbreviation nunit2 := (nunit 2).
1031 Number Notation nunit2 parser2 printer2 : nat_scope.
1032
1033 Check 2.
1034 Check NUnit (S (S O)).
1035 Check NUnit (S (S (S O))).
1036 Check NUnit O.
1037
1038 Check NUnit (S O + S O).
1039 (* doesn't print as 2, because (S O + S O) is not syntactically equal
1040 to (S (S O)), we could want to use a convertibility test rather
1041 than a syntactic equality, but this could be more costly *)
1042
1043 End Test30.
1044
1045 Module Bug10878.
1046
1047 Definition Zto_pos_opt (v : Z) : option positive
1048 := match v with
1049 | Zpos v => Some v
1050 | _ => None
1051 end.
1052 Declare Scope mypos_scope.
1053 Declare Scope mypos_scope2.
1054 Number Notation positive Zto_pos_opt Zpos : mypos_scope. (* success *)
1055 Arguments option {_}.
1056 Number Notation positive Zto_pos_opt Zpos : mypos_scope2. (* was failing *)
1057
1058 End Bug10878.