| 1 |
(* coq-prog-args: ("-async-proofs" "off") *) |
| 2 |
Module applydestruct. |
| 3 |
Class Foo (A : Type) := |
| 4 |
{ bar : nat -> A; |
| 5 |
baz : A -> nat }. |
| 6 |
#[export] Hint Mode Foo + : typeclass_instances. |
| 7 |
|
| 8 |
Class C (A : Type). |
| 9 |
#[export] Hint Mode C + : typeclass_instances. |
| 10 |
|
| 11 |
Parameter fool : forall {A} {F : Foo A} (x : A), C A -> bar 0 = x. |
| 12 |
(* apply leaves non-dependent subgoals of typeclass type |
| 13 |
alone *) |
| 14 |
Goal forall {A} {F : Foo A} (x : A), bar 0 = x. |
| 15 |
Proof. |
| 16 |
intros. apply fool. |
| 17 |
match goal with |
| 18 |
|[ |- C A ] => idtac |
| 19 |
end. |
| 20 |
Abort. |
| 21 |
|
| 22 |
Parameter fooli : forall {A} {F : Foo A} {c : C A} (x : A), bar 0 = x. |
| 23 |
(* apply tries to resolve implicit argument typeclass |
| 24 |
constraints. *) |
| 25 |
Goal forall {A} {F : Foo A} (x : A), bar 0 = x. |
| 26 |
Proof. |
| 27 |
intros. |
| 28 |
Fail apply fooli. |
| 29 |
Fail unshelve eapply fooli; solve [typeclasses eauto]. |
| 30 |
eapply fooli. |
| 31 |
Abort. |
| 32 |
|
| 33 |
(* It applies resolution after unification of the goal *) |
| 34 |
Goal forall {A} {F : Foo A} {C : C A} (x : A), bar 0 = x. |
| 35 |
Proof. |
| 36 |
intros. apply fooli. |
| 37 |
Abort. |
| 38 |
Set Typeclasses Debug Verbosity 2. |
| 39 |
|
| 40 |
Inductive bazdestr {A} (F : Foo A) : nat -> Prop := |
| 41 |
| isbas : bazdestr F 1. |
| 42 |
|
| 43 |
Parameter fooinv : forall {A} {F : Foo A} (x : A), |
| 44 |
bazdestr F (baz x). |
| 45 |
|
| 46 |
(* Destruct applies resolution early, before finding |
| 47 |
occurrences to abstract. *) |
| 48 |
Goal forall {A} {F : Foo A} {C : C A} (x : A), baz x = 0. |
| 49 |
Proof. |
| 50 |
intros. Fail destruct (fooinv _). |
| 51 |
destruct (fooinv x). |
| 52 |
Abort. |
| 53 |
|
| 54 |
Goal forall {A} {F : Foo A} (x y : A), x = y. |
| 55 |
Proof. |
| 56 |
intros. rewrite <- (fool x). rewrite <- (fool y). reflexivity. |
| 57 |
match goal with |
| 58 |
|[ |- C A ] => idtac |
| 59 |
end. |
| 60 |
Abort. |
| 61 |
End applydestruct. |
| 62 |
|
| 63 |
Module onlyclasses. |
| 64 |
|
| 65 |
(* In 8.6 we still allow non-class subgoals *) |
| 66 |
Parameter Foo : Type. |
| 67 |
Parameter foo : Foo. |
| 68 |
#[export] Hint Extern 0 Foo => exact foo : typeclass_instances. |
| 69 |
Goal Foo * Foo. |
| 70 |
split. shelve. |
| 71 |
Set Typeclasses Debug. |
| 72 |
typeclasses eauto. |
| 73 |
Unshelve. typeclasses eauto. |
| 74 |
Qed. |
| 75 |
|
| 76 |
Module RJung. |
| 77 |
Class Foo (x : nat). |
| 78 |
|
| 79 |
#[export] Instance foo x : x = 2 -> Foo x := {}. |
| 80 |
#[export] Hint Extern 0 (_ = _) => reflexivity : typeclass_instances. |
| 81 |
Typeclasses eauto := debug. |
| 82 |
Check (_ : Foo 2). |
| 83 |
|
| 84 |
|
| 85 |
Fail Definition foo := (_ : 0 = 0). |
| 86 |
|
| 87 |
End RJung. |
| 88 |
End onlyclasses. |
| 89 |
|
| 90 |
Module shelve_non_class_subgoals. |
| 91 |
Parameter Foo : Type. |
| 92 |
Parameter foo : Foo. |
| 93 |
#[export] Hint Extern 0 Foo => exact foo : typeclass_instances. |
| 94 |
Class Bar := {}. |
| 95 |
#[export] Instance bar1 (f:Foo) : Bar := {}. |
| 96 |
|
| 97 |
Typeclasses eauto := debug. |
| 98 |
Set Typeclasses Debug Verbosity 2. |
| 99 |
Goal Bar. |
| 100 |
(* Solution has shelved subgoals (of non typeclass type) *) |
| 101 |
typeclasses eauto. |
| 102 |
Abort. |
| 103 |
End shelve_non_class_subgoals. |
| 104 |
|
| 105 |
Module RefineVsNoTceauto. |
| 106 |
|
| 107 |
Class Foo (A : Type) := foo : A. |
| 108 |
#[export] Instance: Foo nat := { foo := 0 }. |
| 109 |
#[export] Instance: Foo nat := { foo := 42 }. |
| 110 |
#[export] Hint Extern 0 (_ = _) => refine eq_refl : typeclass_instances. |
| 111 |
Goal exists (f : Foo nat), @foo _ f = 0. |
| 112 |
Proof. |
| 113 |
unshelve (notypeclasses refine (ex_intro _ _ _)). |
| 114 |
Set Typeclasses Debug. Set Printing All. |
| 115 |
all:once (typeclasses eauto). |
| 116 |
Fail idtac. (* Check no subgoals are left *) |
| 117 |
Undo 3. |
| 118 |
(** In this case, the (_ = _) subgoal is not considered |
| 119 |
by typeclass resolution *) |
| 120 |
refine (ex_intro _ _ _). Fail reflexivity. |
| 121 |
Abort. |
| 122 |
|
| 123 |
End RefineVsNoTceauto. |
| 124 |
|
| 125 |
Module Leivantex2PR339. |
| 126 |
(** Was a bug preventing to find hints associated with no pattern *) |
| 127 |
Class Bar := {}. |
| 128 |
#[export] Instance bar1 (t:Type) : Bar := {}. |
| 129 |
Local Hint Extern 0 => exact True : typeclass_instances. |
| 130 |
Typeclasses eauto := debug. |
| 131 |
Goal Bar. |
| 132 |
Set Typeclasses Debug Verbosity 2. |
| 133 |
typeclasses eauto. (* Relies on resolution of a non-class subgoal *) |
| 134 |
Undo 1. |
| 135 |
typeclasses eauto with typeclass_instances. |
| 136 |
Qed. |
| 137 |
End Leivantex2PR339. |
| 138 |
|
| 139 |
Module HintMode_NonStuck_Failure_Refine_DoNotShelve. |
| 140 |
|
| 141 |
Class test (x : nat) := testv : True. |
| 142 |
Local Hint Mode test ! : typeclass_instances. |
| 143 |
Record foo := { n : nat ; t : test n ; h : t = t }. |
| 144 |
Goal True. |
| 145 |
(* This tests that non-stuck classes whose resolution fails |
| 146 |
are left as proper subgoals and not shelved if failure is allowed. |
| 147 |
*) |
| 148 |
simple refine (let name := (_ : test 5) in _); [|]. |
| 149 |
Abort. |
| 150 |
End HintMode_NonStuck_Failure_Refine_DoNotShelve. |
| 151 |
|
| 152 |
Module bt. |
| 153 |
Require Import Classes.Init. |
| 154 |
|
| 155 |
Record Equ (A : Type) (R : A -> A -> Prop). |
| 156 |
Definition equiv {A} R (e : Equ A R) := R. |
| 157 |
Record Refl (A : Type) (R : A -> A -> Prop). |
| 158 |
Axiom equ_refl : forall A R (e : Equ A R), Refl _ (@equiv A R e). |
| 159 |
#[export] Hint Extern 0 (Refl _ _) => unshelve class_apply @equ_refl; [shelve|] : foo. |
| 160 |
|
| 161 |
Parameter R : nat -> nat -> Prop. |
| 162 |
Lemma bas : Equ nat R. |
| 163 |
Admitted. |
| 164 |
#[export] Hint Resolve bas : foo. |
| 165 |
#[export] Hint Extern 1 => match goal with |- (_ -> _ -> Prop) => shelve end : foo. |
| 166 |
|
| 167 |
Goal exists R, @Refl nat R. |
| 168 |
eexists. |
| 169 |
solve [typeclasses eauto with foo]. |
| 170 |
Qed. |
| 171 |
|
| 172 |
End bt. |
| 173 |
Generalizable All Variables. |
| 174 |
|
| 175 |
Module mon. |
| 176 |
|
| 177 |
Reserved Notation "'return' t" (at level 1). |
| 178 |
Reserved Notation "x >>= y" (at level 65, left associativity). |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
Record Monad {m : Type -> Type} := { |
| 183 |
unit : forall {α}, α -> m α where "'return' t" := (unit t) ; |
| 184 |
bind : forall {α β}, m α -> (α -> m β) -> m β where "x >>= y" := (bind x y) ; |
| 185 |
bind_unit_left : forall {α β} (a : α) (f : α -> m β), return a >>= f = f a }. |
| 186 |
|
| 187 |
Print Visibility. |
| 188 |
Print unit. |
| 189 |
Arguments unit {m _ α}. |
| 190 |
Arguments Monad : clear implicits. |
| 191 |
Notation "'return' t" := (unit t). |
| 192 |
|
| 193 |
(* Test correct handling of existentials and defined fields. *) |
| 194 |
|
| 195 |
Class A `(e: T) := { a := True }. |
| 196 |
Class B `(e_: T) := { e := e_; sg_ass :: A e }. |
| 197 |
|
| 198 |
(* Set Typeclasses Debug. *) |
| 199 |
(* Set Typeclasses Debug Verbosity 2. *) |
| 200 |
|
| 201 |
Goal forall `{B T}, Prop. |
| 202 |
intros. apply a. |
| 203 |
Defined. |
| 204 |
|
| 205 |
Goal forall `{B T}, Prop. |
| 206 |
intros. refine (@a _ _ _). |
| 207 |
Defined. |
| 208 |
|
| 209 |
Class B' `(e_: T) := { e' := e_; sg_ass' :: A e_ }. |
| 210 |
|
| 211 |
Goal forall `{B' T}, a. |
| 212 |
intros. exact I. |
| 213 |
Defined. |
| 214 |
|
| 215 |
End mon. |
| 216 |
|
| 217 |
Module deftwice. |
| 218 |
Class C (A : Type) := c : A -> Type. |
| 219 |
|
| 220 |
Record Inhab (A : Type) := { witness : A }. |
| 221 |
|
| 222 |
#[export] Instance inhab_C : C Type := Inhab. |
| 223 |
|
| 224 |
Axiom full : forall A (X : C A), forall x : A, c x. |
| 225 |
|
| 226 |
Definition truc {A : Type} : Inhab A := (full _ _ _). |
| 227 |
|
| 228 |
End deftwice. |
| 229 |
|
| 230 |
(* Correct treatment of dependent goals *) |
| 231 |
|
| 232 |
(* First some preliminaries: *) |
| 233 |
|
| 234 |
Section sec. |
| 235 |
Context {N: Type}. |
| 236 |
Class C (f: N->N) := {}. |
| 237 |
Class E := { e: N -> N }. |
| 238 |
Context |
| 239 |
(g: N -> N) `(E) `(C e) |
| 240 |
`(forall (f: N -> N), C f -> C (fun x => f x)) |
| 241 |
(U: forall f: N -> N, C f -> False). |
| 242 |
|
| 243 |
(* Now consider the following: *) |
| 244 |
|
| 245 |
Let foo := U (fun x => e x). |
| 246 |
Check foo _. |
| 247 |
|
| 248 |
(* This type checks fine, so far so good. But now |
| 249 |
let's try to get rid of the intermediate constant foo. |
| 250 |
Surely we can just expand it inline, right? Wrong!: *) |
| 251 |
Check U (fun x => e x) _. |
| 252 |
End sec. |
| 253 |
|
| 254 |
Module UniqueSolutions. |
| 255 |
Set Typeclasses Unique Solutions. |
| 256 |
Class Eq (A : Type) : Set. |
| 257 |
#[export] Instance eqa : Eq nat := {}. |
| 258 |
#[export] Instance eqb : Eq nat := {}. |
| 259 |
|
| 260 |
Goal Eq nat. |
| 261 |
try apply _. |
| 262 |
Fail exactly_once typeclasses eauto. |
| 263 |
Abort. |
| 264 |
End UniqueSolutions. |
| 265 |
|
| 266 |
|
| 267 |
Module UniqueInstances. |
| 268 |
(** Optimize proof search on this class by never backtracking on (closed) goals |
| 269 |
for it. *) |
| 270 |
Set Typeclasses Unique Instances. |
| 271 |
Class Eq (A : Type) : Set. |
| 272 |
#[export] Instance eqa : Eq nat. Qed. |
| 273 |
#[export] Instance eqb : Eq nat := {}. |
| 274 |
Class Foo (A : Type) (e : Eq A) : Set. |
| 275 |
#[export] Instance fooa : Foo _ eqa := {}. |
| 276 |
|
| 277 |
Tactic Notation "refineu" open_constr(c) := unshelve refine c. |
| 278 |
|
| 279 |
Set Typeclasses Debug. |
| 280 |
Goal { e : Eq nat & Foo nat e }. |
| 281 |
unshelve refineu (existT _ _ _). |
| 282 |
all:simpl. |
| 283 |
(** Does not backtrack on the (wrong) solution eqb *) |
| 284 |
Fail all:typeclasses eauto. |
| 285 |
Abort. |
| 286 |
End UniqueInstances. |
| 287 |
|
| 288 |
Module IterativeDeepening. |
| 289 |
|
| 290 |
Class A. |
| 291 |
Class B. |
| 292 |
Class C. |
| 293 |
|
| 294 |
#[export] Instance: B -> A | 0 := {}. |
| 295 |
#[export] Instance: C -> A | 0 := {}. |
| 296 |
#[export] Instance: C -> B -> A | 0 := {}. |
| 297 |
#[export] Instance: A -> A | 0 := {}. |
| 298 |
|
| 299 |
Goal C -> A. |
| 300 |
intros. |
| 301 |
Fail Timeout 1 typeclasses eauto. |
| 302 |
Set Typeclasses Iterative Deepening. |
| 303 |
Fail typeclasses eauto 1. |
| 304 |
typeclasses eauto 2. |
| 305 |
Undo. |
| 306 |
Unset Typeclasses Iterative Deepening. |
| 307 |
Fail Timeout 1 typeclasses eauto. |
| 308 |
Set Typeclasses Iterative Deepening. |
| 309 |
typeclasses eauto. |
| 310 |
Qed. |
| 311 |
|
| 312 |
End IterativeDeepening. |
| 313 |
|
| 314 |
Module AxiomsAreNotInstances. |
| 315 |
|
| 316 |
Class TestClass2 := {}. |
| 317 |
Axiom testax2 : TestClass2. |
| 318 |
Fail Definition testdef2 : TestClass2 := _. |
| 319 |
|
| 320 |
(* we didn't break typeclasses *) |
| 321 |
#[export] Existing Instance testax2. |
| 322 |
Definition testdef2 : TestClass2 := _. |
| 323 |
|
| 324 |
End AxiomsAreNotInstances. |
| 325 |
|
| 326 |
Module InternalHintBacktracking. |
| 327 |
|
| 328 |
Class A (T : Type) := mkA { ofA : T }. |
| 329 |
Definition a0 : A nat := {| ofA := 0 |}. |
| 330 |
Definition a1 : A bool := {| ofA := true |}. |
| 331 |
|
| 332 |
(** This defines an instance that returns an A bool on first success and A nat |
| 333 |
on second success *) |
| 334 |
Local Hint Extern 0 (A _) => exact a1 + exact a0 : typeclass_instances. |
| 335 |
|
| 336 |
Class B (T : Type). |
| 337 |
#[export] Instance b0 : B nat := {}. |
| 338 |
|
| 339 |
Definition foo {T} {x : A T} {b : B T} : T := ofA. |
| 340 |
(* This definition only passes because we backtrack on [exact a1] above and try a0 : A nat *) |
| 341 |
Definition test := foo. |
| 342 |
Check test : nat. |
| 343 |
End InternalHintBacktracking. |