Debian Codegraph @

theories/Corelib/ssr/ssreflect.v

raw
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 (* This file is (C) Copyright 2006-2015 Microsoft Corporation and Inria. *)
12
13 (** #<style> .doc { font-family: monospace; white-space: pre; } </style># **)
14
15 Require Import ssrmatching.
16 Declare ML Module "rocq-runtime.plugins.ssreflect".
17
18 (**
19 This file is the Gallina part of the ssreflect plugin implementation.
20 Files that use the ssreflect plugin should always Require ssreflect and
21 either Import ssreflect or Import ssreflect.SsrSyntax.
22 Part of the contents of this file is technical and will only interest
23 advanced developers; in addition the following are defined:
24 #[#the str of v by f#]# == the Canonical s : str such that f s = v.
25 #[#the str of v#]# == the Canonical s : str that coerces to v.
26 argumentType c == the T such that c : forall x : T, P x.
27 returnType c == the R such that c : T -> R.
28 {type of c for s} == P s where c : forall x : T, P x.
29 nonPropType == an interface for non-Prop Types: a nonPropType coerces
30 to a Type, and only types that do _not_ have sort
31 Prop are canonical nonPropType instances. This is
32 useful for applied views (see mid-file comment).
33 notProp T == the nonPropType instance for type T.
34 phantom T v == singleton type with inhabitant Phantom T v.
35 phant T == singleton type with inhabitant Phant v.
36 =^~ r == the converse of rewriting rule r (e.g., in a
37 rewrite multirule).
38 unkeyed t == t, but treated as an unkeyed matching pattern by
39 the ssreflect matching algorithm.
40 nosimpl t == t, but on the right-hand side of Definition C :=
41 nosimpl disables expansion of C by /=.
42 locked t == t, but locked t is not convertible to t.
43 locked_with k t == t, but not convertible to t or locked_with k' t
44 unless k = k' (with k : unit). Rocq type-checking
45 will be much more efficient if locked_with with a
46 bespoke k is used for sealed definitions.
47 unlockable v == interface for sealed constant definitions of v.
48 Unlockable def == the unlockable that registers def : C = v.
49 #[#unlockable of C#]# == a clone for C of the canonical unlockable for the
50 definition of C (e.g., if it uses locked_with).
51 #[#unlockable fun C#]# == #[#unlockable of C#]# with the expansion forced to be
52 an explicit lambda expression.
53 -> The usage pattern for ADT operations is:
54 Definition foo_def x1 .. xn := big_foo_expression.
55 Fact foo_key : unit. Proof. by #[# #]#. Qed.
56 Definition foo := locked_with foo_key foo_def.
57 Canonical foo_unlockable := #[#unlockable fun foo#]#.
58 This minimizes the comparison overhead for foo, while still allowing
59 rewrite unlock to expose big_foo_expression.
60
61 #[#elaborate x#]# == triggers Rocq elaboration to fill the holes of the term x
62 The main use case is to trigger typeclass inference in
63 the body of a ssreflect have := #[#elaborate body#]#.
64
65 Additionally we provide default intro pattern ltac views:
66 - top of the stack actions:
67 => /#[#apply#]# := => hyp {}/hyp
68 => /#[#swap#]# := => x y; move: y x
69 (also swap and preserves let bindings)
70 => /#[#dup#]# := => x; have copy := x; move: copy x
71 (also copies and preserves let bindings)
72 - calling rewrite from an intro pattern, use with parsimony:
73 => /#[#1! rules#]# := rewrite rules
74 => /#[#! rules#]# := rewrite !rules
75
76 More information about these definitions and their use can be found in the
77 ssreflect manual, and in specific comments below. **)
78
79 Set Implicit Arguments.
80 Unset Strict Implicit.
81 Unset Printing Implicit Defensive.
82
83 Module SsrSyntax.
84
85 (** Declare Ssr keywords: "is" "isn't" "of" "//" "/=" and "//=". **)
86
87 Reserved Notation "(* x 'is' y 'of' z 'isn't' // /= //= *)".
88
89 (** Enable SSR features **)
90 #[export] Set SSR Loaded.
91
92 Reserved Notation "<hidden n >" (at level 0, n at level 0,
93 format "<hidden n >").
94 #[warning="-postfix-notation-not-level-1"]
95 Reserved Notation "T (* n *)" (at level 200, format "T (* n *)").
96
97 End SsrSyntax.
98
99 Export SsrMatchingSyntax.
100 Export SsrSyntax.
101
102 (** Save primitive notation that will be overloaded. **)
103 Local Abbreviation RocqGenericIf c vT vF := (if c then vT else vF) (only parsing).
104 Local Abbreviation RocqGenericDependentIf c x R vT vF :=
105 (if c as x return R then vT else vF) (only parsing).
106
107 (** Reserve notation that introduced in this file. **)
108 Reserved Notation "'if' c 'then' vT 'else' vF" (at level 200,
109 c, vT, vF at level 200).
110 Reserved Notation "'if' c 'return' R 'then' vT 'else' vF" (at level 200,
111 c, R, vT, vF at level 200).
112 Reserved Notation "'if' c 'as' x 'return' R 'then' vT 'else' vF" (at level 200,
113 c, R, vT, vF at level 200, x name).
114
115 Reserved Notation "[ 'the' sT 'of' v 'by' f ]" (at level 0,
116 format "[ 'the' sT 'of' v 'by' f ]").
117 Reserved Notation "[ 'the' sT 'of' v ]" (at level 0,
118 format "[ 'the' sT 'of' v ]").
119 Reserved Notation "{ 'type' 'of' c 'for' s }" (at level 0,
120 format "{ 'type' 'of' c 'for' s }").
121
122 Reserved Notation "=^~ r" (at level 100, format "=^~ r").
123
124 Reserved Notation "[ 'unlockable' 'of' C ]" (at level 0,
125 format "[ 'unlockable' 'of' C ]").
126 Reserved Notation "[ 'unlockable' 'fun' C ]" (at level 0,
127 format "[ 'unlockable' 'fun' C ]").
128
129 Reserved Notation "[ 'elaborate' x ]" (at level 0).
130
131 (**
132 To define notations for tactic in intro patterns.
133 When "=> /t" is parsed, "t:%ssripat" is actually interpreted. **)
134 Declare Scope ssripat_scope.
135 Delimit Scope ssripat_scope with ssripat.
136
137 (**
138 Make the general "if" into a notation, so that we can override it below.
139 The notations are "only parsing" because the Rocq decompiler will not
140 recognize the expansion of the boolean if; using the default printer
141 avoids a spurious trailing %%GEN_IF. **)
142
143 Declare Scope general_if_scope.
144 Delimit Scope general_if_scope with GEN_IF.
145
146 Notation "'if' c 'then' vT 'else' vF" :=
147 (RocqGenericIf c vT vF) (only parsing) : general_if_scope.
148
149 Notation "'if' c 'return' R 'then' vT 'else' vF" :=
150 (RocqGenericDependentIf c c R vT vF) (only parsing) : general_if_scope.
151
152 Notation "'if' c 'as' x 'return' R 'then' vT 'else' vF" :=
153 (RocqGenericDependentIf c x R vT vF) (only parsing) : general_if_scope.
154
155 (** Force boolean interpretation of simple if expressions. **)
156
157 Declare Scope boolean_if_scope.
158 Delimit Scope boolean_if_scope with BOOL_IF.
159
160 Notation "'if' c 'return' R 'then' vT 'else' vF" :=
161 (if c is true as c in bool return R then vT else vF) : boolean_if_scope.
162
163 Notation "'if' c 'then' vT 'else' vF" :=
164 (if c%bool is true as _ in bool return _ then vT else vF) : boolean_if_scope.
165
166 Notation "'if' c 'as' x 'return' R 'then' vT 'else' vF" :=
167 (if c%bool is true as x in bool return R then vT else vF) : boolean_if_scope.
168
169 Open Scope boolean_if_scope.
170
171 (**
172 To allow a wider variety of notations without reserving a large number of
173 of identifiers, the ssreflect library systematically uses "forms" to
174 enclose complex mixfix syntax. A "form" is simply a mixfix expression
175 enclosed in square brackets and introduced by a keyword:
176 #[#keyword ... #]#
177 Because the keyword follows a bracket it does not need to be reserved.
178 Non-ssreflect libraries that do not respect the form syntax (e.g., the Rocq
179 Lists library) should be loaded before ssreflect so that their notations
180 do not mask all ssreflect forms. **)
181 Declare Scope form_scope.
182 Delimit Scope form_scope with FORM.
183 Open Scope form_scope.
184
185 (** Constants for abstract: and #[#: name #]# intro pattern **)
186 Definition abstract_lock := unit.
187 Definition abstract_key := tt.
188
189 Definition abstract (statement : Type) (id : nat) (lock : abstract_lock) :=
190 let: tt := lock in statement.
191
192 Declare Scope ssr_scope.
193 Notation "<hidden n >" := (abstract _ n _) : ssr_scope.
194 Notation "T (* n *)" := (abstract T n abstract_key) : ssr_scope.
195 Open Scope ssr_scope.
196
197 Register abstract_lock as plugins.ssreflect.abstract_lock.
198 Register abstract_key as plugins.ssreflect.abstract_key.
199 Register abstract as plugins.ssreflect.abstract.
200
201 (** Constants for tactic-views **)
202 Inductive external_view : Type := tactic_view of Type.
203
204 (**
205 Syntax for referring to canonical structures:
206 #[#the struct_type of proj_val by proj_fun#]#
207 This form denotes the Canonical instance s of the Structure type
208 struct_type whose proj_fun projection is proj_val, i.e., such that
209 proj_fun s = proj_val.
210 Typically proj_fun will be A record field accessors of struct_type, but
211 this need not be the case; it can be, for instance, a field of a record
212 type to which struct_type coerces; proj_val will likewise be coerced to
213 the return type of proj_fun. In all but the simplest cases, proj_fun
214 should be eta-expanded to allow for the insertion of implicit arguments.
215 In the common case where proj_fun itself is a coercion, the "by" part
216 can be omitted entirely; in this case it is inferred by casting s to the
217 inferred type of proj_val. Obviously the latter can be fixed by using an
218 explicit cast on proj_val, and it is highly recommended to do so when the
219 return type intended for proj_fun is "Type", as the type inferred for
220 proj_val may vary because of sort polymorphism (it could be Set or Prop).
221 Note when using the #[#the _ of _ #]# form to generate a substructure from a
222 telescopes-style canonical hierarchy (implementing inheritance with
223 coercions), one should always project or coerce the value to the BASE
224 structure, because Rocq will only find a Canonical derived structure for
225 the Canonical base structure -- not for a base structure that is specific
226 to proj_value. **)
227
228 Module TheCanonical.
229
230 Variant put vT sT (v1 v2 : vT) (s : sT) : Prop := Put.
231
232 Definition get vT sT v s (p : @put vT sT v v s) := let: Put _ _ _ := p in s.
233
234 Definition get_by vT sT of sT -> vT := @get vT sT.
235
236 End TheCanonical.
237
238 Import TheCanonical. (* Note: no export. *)
239
240 Local Arguments get_by _%_type_scope _%_type_scope _ _ _ _.
241
242 Notation "[ 'the' sT 'of' v 'by' f ]" :=
243 (@get_by _ sT f _ _ ((fun v' (s : sT) => Put v' (f s) s) v _))
244 (only parsing) : form_scope.
245
246 Notation "[ 'the' sT 'of' v ]" := (get ((fun s : sT => Put v (*coerce*) s s) _))
247 (only parsing) : form_scope.
248
249 (**
250 The following are "format only" versions of the above notations.
251 We need to do this to prevent the formatter from being be thrown off by
252 application collapsing, coercion insertion and beta reduction in the right
253 hand side of the notations above. **)
254
255 Notation "[ 'the' sT 'of' v 'by' f ]" := (@get_by _ sT f v _ _)
256 (only printing) : form_scope.
257
258 Notation "[ 'the' sT 'of' v ]" := (@get _ sT v _ _)
259 (only printing) : form_scope.
260
261 (**
262 We would like to recognize
263 Notation " #[# 'the' sT 'of' v : 'Type' #]#" := (@get Type sT v _ _)
264 (at level 0, format " #[# 'the' sT 'of' v : 'Type' #]#") : form_scope.
265 **)
266
267 (**
268 Helper notation for canonical structure inheritance support.
269 This is a workaround for the poor interaction between delta reduction and
270 canonical projections in Rocq's unification algorithm, by which transparent
271 definitions hide canonical instances, i.e., in
272 Canonical a_type_struct := @Struct a_type ...
273 Definition my_type := a_type.
274 my_type doesn't effectively inherit the struct structure from a_type. Our
275 solution is to redeclare the instance as follows
276 Canonical my_type_struct := Eval hnf in #[#struct of my_type#]#.
277 The special notation #[#str of _ #]# must be defined for each Structure "str"
278 with constructor "Str", typically as follows
279 Definition clone_str s :=
280 let: Str _ x y ... z := s return {type of Str for s} -> str in
281 fun k => k _ x y ... z.
282 Notation " #[# 'str' 'of' T 'for' s #]#" := (@clone_str s (@Str T))
283 (at level 0, format " #[# 'str' 'of' T 'for' s #]#") : form_scope.
284 Notation " #[# 'str' 'of' T #]#" := (repack_str (fun x => @Str T x))
285 (at level 0, format " #[# 'str' 'of' T #]#") : form_scope.
286 The notation for the match return predicate is defined below; the eta
287 expansion in the second form serves both to distinguish it from the first
288 and to avoid the delta reduction problem.
289 There are several variations on the notation and the definition of the
290 the "clone" function, for telescopes, mixin classes, and join (multiple
291 inheritance) classes. We describe a different idiom for clones in ssrfun;
292 it uses phantom types (see below) and static unification; see fintype and
293 ssralg for examples. **)
294
295 Definition argumentType T P & forall x : T, P x := T.
296 Definition dependentReturnType T P & forall x : T, P x := P.
297 Definition returnType aT rT & aT -> rT := rT.
298
299 Notation "{ 'type' 'of' c 'for' s }" := (dependentReturnType c s) : type_scope.
300
301 (**
302 A generic "phantom" type (actually, a unit type with a phantom parameter).
303 This type can be used for type definitions that require some Structure
304 on one of their parameters, to allow Rocq to infer said structure so it
305 does not have to be supplied explicitly or via the " #[#the _ of _ #]#" notation
306 (the latter interacts poorly with other Notation).
307 The definition of a (co)inductive type with a parameter p : p_type, that
308 needs to use the operations of a structure
309 Structure p_str : Type := p_Str {p_repr :> p_type; p_op : p_repr -> ...}
310 should be given as
311 Inductive indt_type (p : p_str) := Indt ... .
312 Definition indt_of (p : p_str) & phantom p_type p := indt_type p.
313 Notation "{ 'indt' p }" := (indt_of (Phantom p)).
314 Definition indt p x y ... z : {indt p} := @Indt p x y ... z.
315 Notation " #[# 'indt' x y ... z #]#" := (indt x y ... z).
316 That is, the concrete type and its constructor should be shadowed by
317 definitions that use a phantom argument to infer and display the true
318 value of p (in practice, the "indt" constructor often performs additional
319 functions, like "locking" the representation -- see below).
320 We also define a simpler version ("phant" / "Phant") of phantom for the
321 common case where p_type is Type. **)
322
323 Variant phantom T (p : T) : Prop := Phantom.
324 Arguments phantom : clear implicits.
325 Arguments Phantom : clear implicits.
326 Variant phant (p : Type) : Prop := Phant.
327
328 (** Internal tagging used by the implementation of the ssreflect elim. **)
329
330 Definition protect_term (A : Type) (x : A) : A := x.
331
332 Register protect_term as plugins.ssreflect.protect_term.
333
334 (**
335 The ssreflect idiom for a non-keyed pattern:
336 - unkeyed t will match any subterm that unifies with t, regardless of
337 whether it displays the same head symbol as t.
338 - unkeyed t a b will match any application of a term f unifying with t,
339 to two arguments unifying with a and b, respectively, regardless of
340 apparent head symbols.
341 - unkeyed x where x is a variable will match any subterm with the same
342 type as x (when x would raise the 'indeterminate pattern' error). **)
343
344 Abbreviation unkeyed x := (let flex := x in flex).
345
346 (** Ssreflect converse rewrite rule rule idiom. **)
347 Definition ssr_converse R (r : R) := (Logic.I, r).
348 Notation "=^~ r" := (ssr_converse r) : form_scope.
349
350 (**
351 Term tagging (user-level).
352 The ssreflect library uses four strengths of term tagging to restrict
353 convertibility during type checking:
354 nosimpl t simplifies to t EXCEPT in a definition; more precisely, given
355 Definition foo := nosimpl bar, foo (or foo t') will NOT be expanded by
356 the /= and //= switches unless it is in a forcing context (e.g., in
357 match foo t' with ... end, foo t' will be reduced if this allows the
358 match to be reduced). Note that nosimpl bar is simply notation for a
359 a term that beta-iota reduces to bar; hence rewrite /foo will replace
360 foo by bar, and rewrite -/foo will replace bar by foo.
361 CAVEAT: nosimpl should not be used inside a Section, because the end of
362 section "cooking" removes the iota redex.
363 locked t is provably equal to t, but is not convertible to t; 'locked'
364 provides support for selective rewriting, via the lock t : t = locked t
365 Lemma, and the ssreflect unlock tactic.
366 locked_with k t is equal but not convertible to t, much like locked t,
367 but supports explicit tagging with a value k : unit. This is used to
368 mitigate a flaw in the term comparison heuristic of the Rocq kernel,
369 which treats all terms of the form locked t as equal and compares their
370 arguments recursively, leading to an exponential blowup of comparison.
371 For this reason locked_with should be used rather than locked when
372 defining ADT operations. The unlock tactic does not support locked_with
373 but the unlock rewrite rule does, via the unlockable interface.
374 we also use Module Type ascription to create truly opaque constants,
375 because simple expansion of constants to reveal an unreducible term
376 doubles the time complexity of a negative comparison. Such opaque
377 constants can be expanded generically with the unlock rewrite rule.
378 See the definition of card and subset in fintype for examples of this. **)
379
380 Abbreviation nosimpl t := (let: tt := tt in t).
381
382 Lemma master_key : unit. Proof. exact tt. Qed.
383 Definition locked A := let: tt := master_key in fun x : A => x.
384
385 Register master_key as plugins.ssreflect.master_key.
386 Register locked as plugins.ssreflect.locked.
387
388 Lemma lock A x : x = locked x :> A. Proof. unlock; reflexivity. Qed.
389
390 (** The basic closing tactic "done". **)
391 Ltac done :=
392 trivial; hnf; intros; solve
393 [ do ![solve [trivial | simple refine (@sym_equal _ _ _ _); trivial]
394 | discriminate | contradiction | split]
395 | match goal with H : ~ _ |- _ => solve [case H; trivial] end ].
396
397 (** Quicker done tactic not including split, syntax: /0/ **)
398 Ltac ssrdone0 :=
399 trivial; hnf; intros; solve
400 [ do ![solve [trivial | apply: sym_equal; trivial]
401 | discriminate | contradiction ]
402 | match goal with H : ~ _ |- _ => solve [case H; trivial] end ].
403
404 (** To unlock opaque constants. **)
405 #[universes(template)]
406 Structure unlockable T v := Unlockable {unlocked : T; _ : unlocked = v}.
407 Lemma unlock T x C : @unlocked T x C = x. Proof. by case: C. Qed.
408
409 Notation "[ 'unlockable' 'of' C ]" :=
410 (@Unlockable _ _ C (unlock _)) : form_scope.
411
412 Notation "[ 'unlockable' 'fun' C ]" :=
413 (@Unlockable _ (fun _ => _) C (unlock _)) : form_scope.
414
415 (** Generic keyed constant locking. **)
416
417 (** The argument order ensures that k is always compared before T. **)
418 Definition locked_with k := let: tt := k in fun T x => x : T.
419
420 (**
421 This can be used as a cheap alternative to cloning the unlockable instance
422 below, but with caution as unkeyed matching can be expensive. **)
423 Lemma locked_withE T k x : unkeyed (locked_with k x) = x :> T.
424 Proof. by case: k. Qed.
425
426 (** Intensionaly, this instance will not apply to locked u. **)
427 Canonical locked_with_unlockable T k x :=
428 @Unlockable T x (locked_with k x) (locked_withE k x).
429
430 (** More accurate variant of unlock, and safer alternative to locked_withE. **)
431 Lemma unlock_with T k x : unlocked (locked_with_unlockable k x) = x :> T.
432 Proof. exact: unlock. Qed.
433
434 (** Abbreviation to trigger Rocq elaboration to fill the holes **)
435 Notation "[ 'elaborate' x ]" := (ltac:(refine x)) (only parsing).
436
437 (** The internal lemmas for the have tactics. **)
438
439 Lemma ssr_have
440 (Plemma : Prop) (Pgoal : Prop)
441 (step : Plemma) (rest : Plemma -> Pgoal) : Pgoal.
442 Proof. exact: rest step. Qed.
443
444 Register ssr_have as plugins.ssreflect.ssr_have.
445
446 Polymorphic Lemma ssr_have_upoly@{s1 s2;u1 u2}
447 (Plemma : Type@{s1;u1}) (Pgoal : Type@{s2;u2})
448 (step : Plemma) (rest : Plemma -> Pgoal) : Pgoal.
449 Proof. exact: rest step. Qed.
450
451 Register ssr_have_upoly as plugins.ssreflect.ssr_have_upoly.
452
453 (** Internal N-ary congruence lemmas for the congr tactic. **)
454
455 Fixpoint nary_congruence_statement (n : nat)
456 : (forall B, (B -> B -> Prop) -> Prop) -> Prop :=
457 match n with
458 | O => fun k => forall B, k B (fun x1 x2 : B => x1 = x2)
459 | S n' =>
460 let k' A B e (f1 f2 : A -> B) :=
461 forall x1 x2, x1 = x2 -> (e (f1 x1) (f2 x2) : Prop) in
462 fun k => forall A, nary_congruence_statement n' (fun B e => k _ (k' A B e))
463 end.
464
465 Lemma nary_congruence n (k := fun B e => forall y : B, (e y y : Prop)) :
466 nary_congruence_statement n k.
467 Proof.
468 have: k _ _ := _; rewrite {1}/k.
469 elim: n k => [|n IHn] k k_P /= A; first exact: k_P.
470 by apply: IHn => B e He; apply: k_P => f x1 x2 <-.
471 Qed.
472
473 Lemma ssr_congr_arrow Plemma Pgoal : Plemma = Pgoal -> Plemma -> Pgoal.
474 Proof. by move->. Qed.
475 Arguments ssr_congr_arrow : clear implicits.
476
477 Register nary_congruence as plugins.ssreflect.nary_congruence.
478 Register ssr_congr_arrow as plugins.ssreflect.ssr_congr_arrow.
479
480 (** View lemmas that don't use reflection. **)
481
482 Section ApplyIff.
483
484 Variables P Q : Prop.
485 Hypothesis eqPQ : P <-> Q.
486
487 Lemma iffLR : P -> Q. Proof. by case: eqPQ. Qed.
488 Lemma iffRL : Q -> P. Proof. by case: eqPQ. Qed.
489
490 Lemma iffLRn : ~P -> ~Q. Proof. by move=> nP tQ; case: nP; case: eqPQ tQ. Qed.
491 Lemma iffRLn : ~Q -> ~P. Proof. by move=> nQ tP; case: nQ; case: eqPQ tP. Qed.
492
493 End ApplyIff.
494
495 Hint View for move/ iffLRn|2 iffRLn|2 iffLR|2 iffRL|2.
496 Hint View for apply/ iffRLn|2 iffLRn|2 iffRL|2 iffLR|2.
497
498 (**
499 To focus non-ssreflect tactics on a subterm, eg vm_compute.
500 Usage:
501 elim/abstract_context: (pattern) => G defG.
502 vm_compute; rewrite {}defG {G}.
503 Note that vm_cast are not stored in the proof term
504 for reductions occurring in the context, hence
505 set here := pattern; vm_compute in (value of here)
506 blows up at Qed time. **)
507 Lemma abstract_context T (P : T -> Type) x :
508 (forall Q, Q = P -> Q x) -> P x.
509 Proof. by move=> /(_ P); apply. Qed.
510
511 (*****************************************************************************)
512 (* Material for under/over (to rewrite under binders using "context lemmas") *)
513
514 Require Export ssrunder.
515
516 #[global]
517 Hint Extern 0 (@Under_rel.Over_rel _ _ _ _) =>
518 solve [ apply: Under_rel.over_rel_done ] : core.
519 #[global]
520 Hint Resolve Under_rel.over_rel_done : core.
521
522 Register Under_rel.Under_rel as plugins.ssreflect.Under_rel.
523 Register Under_rel.Under_rel_from_rel as plugins.ssreflect.Under_rel_from_rel.
524
525 (** Closing rewrite rule *)
526 Definition over := over_rel.
527
528 (** Closing tactic *)
529 Ltac over :=
530 by [ apply: Under_rel.under_rel_done
531 | rewrite over
532 ].
533
534 (** Convenience rewrite rule to unprotect evars, e.g., to instantiate
535 them in another way than with reflexivity. *)
536 Definition UnderE := Under_relE.
537
538 (*****************************************************************************)
539
540 (** An interface for non-Prop types; used to avoid improper instantiation
541 of polymorphic lemmas with on-demand implicits when they are used as views.
542 For example: Some_inj {T} : forall x y : T, Some x = Some y -> x = y.
543 Using move/Some_inj on a goal of the form Some n = Some 0 will fail:
544 SSReflect will interpret the view as @Some_inj ?T _top_assumption_
545 since this is the well-typed application of the view with the minimal
546 number of inserted evars (taking ?T := Some n = Some 0), and then will
547 later complain that it cannot erase _top_assumption_ after having
548 abstracted the viewed assumption. Making x and y maximal implicits
549 would avoid this and force the intended @Some_inj nat x y _top_assumption_
550 interpretation, but is undesirable as it makes it harder to use Some_inj
551 with the many SSReflect and MathComp lemmas that have an injectivity
552 premise. Specifying {T : nonPropType} solves this more elegantly, as then
553 (?T : Type) no longer unifies with (Some n = Some 0), which has sort Prop.
554 **)
555
556 Module NonPropType.
557
558 (** Implementation notes:
559 We rely on three interface Structures:
560 - test_of r, the middle structure, performs the actual check: it has two
561 canonical instances whose 'condition' projection are maybeProj (?P : Prop)
562 and tt, and which set r := true and r := false, respectively. Unifying
563 condition (?t : test_of ?r) with maybeProj T will thus set ?r to true if
564 T is in Prop as the test_Prop T instance will apply, and otherwise simplify
565 maybeProp T to tt and use the test_negative instance and set ?r to false.
566 - call_of c r sets up a call to test_of on condition c with expected result r.
567 It has a default instance for its 'callee' projection to Type, which
568 sets c := maybeProj T and r := false when unifying with a type T.
569 - type is a telescope on call_of c r, which checks that unifying test_of ?r1
570 with c indeed sets ?r1 := r; the type structure bundles the 'test' instance
571 and its 'result' value along with its call_of c r projection. The default
572 instance essentially provides eta-expansion for 'type'. This is only
573 essential for the first 'result' projection to bool; using the instance
574 for other projection merely avoids spurious delta expansions that would
575 spoil the notProp T notation.
576 In detail, unifying T =~= ?S with ?S : nonPropType, i.e.,
577 (1) T =~= @callee (@condition (result ?S) (test ?S)) (result ?S) (frame ?S)
578 first uses the default call instance with ?T := T to reduce (1) to
579 (2a) @condition (result ?S) (test ?S) =~= maybeProp T
580 (3) result ?S =~= false
581 (4) frame ?S =~= call T
582 along with some trivial universe-related checks which are irrelevant here.
583 Then the unification tries to use the test_Prop instance to reduce (2a) to
584 (6a) result ?S =~= true
585 (7a) ?P =~= T with ?P : Prop
586 (8a) test ?S =~= test_Prop ?P
587 Now the default 'check' instance with ?result := true resolves (6a) as
588 (9a) ?S := @check true ?test ?frame
589 Then (7a) can be solved precisely if T has sort at most (hence exactly) Prop,
590 and then (8a) is solved by the check instance, yielding ?test := test_Prop T,
591 and completing the solution of (2a), and _committing_ to it. But now (3) is
592 inconsistent with (9a), and this makes the entire problem (1) fails.
593 If on the other hand T does not have sort Prop then (7a) fails and the
594 unification resorts to delta expanding (2a), which gives
595 (2b) @condition (result ?S) (test ?S) =~= tt
596 which is then reduced, using the test_negative instance, to
597 (6b) result ?S =~= false
598 (8b) test ?S =~= test_negative
599 Both are solved using the check default instance, as in the (2a) branch, giving
600 (9b) ?S := @check false test_negative ?frame
601 Then (3) and (4) are similarly solved using check, giving the final assignment
602 (9) ?S := notProp T
603 Observe that we _must_ perform the actual test unification on the arguments
604 of the initial canonical instance, and not on the instance itself as we do
605 in mathcomp/matrix and mathcomp/vector, because we want the unification to
606 fail when T has sort Prop. If both the test_of _and_ the result check
607 unifications were done as part of the structure telescope then the latter
608 would be a sub-problem of the former, and thus failing the check would merely
609 make the test_of unification backtrack and delta-expand and we would not get
610 failure.
611 **)
612
613 Structure call_of (condition : unit) (result : bool) := Call {callee : Type}.
614 Definition maybeProp (T : Type) := tt.
615 Definition call T := Call (maybeProp T) false T.
616
617 Structure test_of (result : bool) := Test {condition :> unit}.
618 Definition test_Prop (P : Prop) := Test true (maybeProp P).
619 Definition test_negative := Test false tt.
620
621 Structure type :=
622 Check {result : bool; test : test_of result; frame : call_of test result}.
623 Definition check result test frame := @Check result test frame.
624
625 Module Exports.
626 Canonical call.
627 Canonical test_Prop.
628 Canonical test_negative.
629 Canonical check.
630 Abbreviation nonPropType := type.
631 Coercion callee : call_of >-> Sortclass.
632 Coercion frame : type >-> call_of.
633 Abbreviation notProp T := (@check false test_negative (call T)).
634 End Exports.
635
636 End NonPropType.
637 Export NonPropType.Exports.
638
639 Module Export ipat.
640
641 Notation "'[' 'apply' ']'" := (ltac:(let f := fresh "_top_" in move=> f {}/f))
642 (at level 0, only parsing) : ssripat_scope.
643
644 (* we try to preserve the naming by matching the names from the goal *)
645 (* we do move to perform a hnf before trying to match *)
646 Notation "'[' 'swap' ']'" := (ltac:(move;
647 let x := lazymatch goal with
648 | |- forall (x : _), _ => fresh x | |- let x := _ in _ => fresh x | _ => fresh "_top_"
649 end in intro x; move;
650 let y := lazymatch goal with
651 | |- forall (y : _), _ => fresh y | |- let y := _ in _ => fresh y | _ => fresh "_top_"
652 end in intro y; revert x; revert y))
653 (at level 0, only parsing) : ssripat_scope.
654
655
656 (* we try to preserve the naming by matching the names from the goal *)
657 (* we do move to perform a hnf before trying to match *)
658 Notation "'[' 'dup' ']'" := (ltac:(move;
659 lazymatch goal with
660 | |- forall (x : _), _ =>
661 let x := fresh x in intro x;
662 let copy := fresh x in have copy := x; revert x; revert copy
663 | |- let x := _ in _ =>
664 let x := fresh x in intro x;
665 let copy := fresh x in pose copy := x;
666 do [unfold x in (value of copy)]; revert x; revert copy
667 | |- _ =>
668 let x := fresh "_top_" in move=> x;
669 let copy := fresh "_top" in have copy := x; revert x; revert copy
670 end))
671 (at level 0, only parsing) : ssripat_scope.
672
673 Notation "'[' '1' '!' rules ']'" := (ltac:(rewrite rules))
674 (at level 0, rules at level 200, only parsing) : ssripat_scope.
675 Notation "'[' '!' rules ']'" := (ltac:(rewrite !rules))
676 (at level 0, rules at level 200, only parsing) : ssripat_scope.
677
678 End ipat.
679
680 (* A class to trigger reduction by rewriting. *)
681 (* Usage: rewrite [pattern]vm_compute. *)
682 (* Alternatively one may redefine a lemma as in algebra/rat.v : *)
683 (* Lemma rat_vm_compute n (x : rat) : vm_compute_eq n%:Q x -> n%:Q = x. *)
684 (* Proof. exact. Qed. *)
685
686 Class vm_compute_eq {T : Type} (x y : T) := vm_compute : x = y.
687
688 #[global]
689 Hint Extern 0 (@vm_compute_eq _ _ _) =>
690 vm_compute; reflexivity : typeclass_instances.