| 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 |
Class A (A : Type). |
| 12 |
#[export] Instance an: A nat := {}. |
| 13 |
|
| 14 |
Class B (A : Type) (a : A). |
| 15 |
#[export] Instance bn0: B nat 0 := {}. |
| 16 |
#[export] Instance bn1: B nat 1 := {}. |
| 17 |
|
| 18 |
Goal A nat. |
| 19 |
Proof. |
| 20 |
typeclasses eauto. |
| 21 |
Qed. |
| 22 |
|
| 23 |
Goal B nat 2. |
| 24 |
Proof. |
| 25 |
Fail typeclasses eauto. |
| 26 |
Abort. |
| 27 |
|
| 28 |
Goal exists T : Type, A T. |
| 29 |
Proof. |
| 30 |
eexists. typeclasses eauto. |
| 31 |
Defined. |
| 32 |
|
| 33 |
#[export] Hint Extern 0 (_ /\ _) => constructor : typeclass_instances. |
| 34 |
|
| 35 |
Existing Class and. |
| 36 |
|
| 37 |
Goal exists (T : Type) (t : T), A T /\ B T t. |
| 38 |
Proof. |
| 39 |
eexists. eexists. typeclasses eauto. |
| 40 |
Defined. |
| 41 |
|
| 42 |
#[export] Instance ab: A bool := {}. (* Backtrack on A instance *) |
| 43 |
Goal exists (T : Type) (t : T), A T /\ B T t. |
| 44 |
Proof. |
| 45 |
eexists. eexists. typeclasses eauto. |
| 46 |
Defined. |
| 47 |
|
| 48 |
Class C {T} `(a : A T) (t : T). |
| 49 |
Require Import Classes.Init. |
| 50 |
#[export] Hint Extern 0 { x : ?A & _ } => |
| 51 |
unshelve class_apply @existT : typeclass_instances. |
| 52 |
Existing Class sigT. |
| 53 |
Set Typeclasses Debug. |
| 54 |
#[export] Instance can: C an 0 := {}. |
| 55 |
(* Backtrack on instance implementation *) |
| 56 |
Goal exists (T : Type) (t : T), { x : A T & C x t }. |
| 57 |
Proof. |
| 58 |
eexists. eexists. typeclasses eauto. |
| 59 |
Defined. |
| 60 |
|
| 61 |
Class D T `(a: A T). |
| 62 |
#[export] Instance: D _ an := {}. |
| 63 |
Goal exists (T : Type), { x : A T & D T x }. |
| 64 |
Proof. |
| 65 |
eexists. typeclasses eauto. |
| 66 |
Defined. |
| 67 |
|
| 68 |
|
| 69 |
(* Example from Nicolas Magaud on coq-club - Jul 2000 *) |
| 70 |
|
| 71 |
Definition Nat : Set := nat. |
| 72 |
Parameter S' : Nat -> Nat. |
| 73 |
Parameter plus' : Nat -> Nat -> Nat. |
| 74 |
|
| 75 |
Lemma simpl_plus_l_rr1 : |
| 76 |
(forall n0 : Nat, |
| 77 |
(forall m p : Nat, plus' n0 m = plus' n0 p -> m = p) -> |
| 78 |
forall m p : Nat, S' (plus' n0 m) = S' (plus' n0 p) -> m = p) -> |
| 79 |
forall n : Nat, |
| 80 |
(forall m p : Nat, plus' n m = plus' n p -> m = p) -> |
| 81 |
forall m p : Nat, S' (plus' n m) = S' (plus' n p) -> m = p. |
| 82 |
intros. |
| 83 |
apply H0. apply f_equal_nat. |
| 84 |
Time info_eauto. |
| 85 |
Undo. |
| 86 |
Set Typeclasses Debug. |
| 87 |
Set Typeclasses Iterative Deepening. |
| 88 |
Time typeclasses eauto 6 with nocore. Show Proof. |
| 89 |
Undo. |
| 90 |
Time eauto. (* does EApply H *) |
| 91 |
Qed. |
| 92 |
|
| 93 |
(* Example from Nicolas Tabareau on coq-club - Feb 2016. |
| 94 |
Full backtracking on dependent subgoals. |
| 95 |
*) |
| 96 |
Require Import Corelib.Classes.Init. |
| 97 |
|
| 98 |
Module NTabareau. |
| 99 |
|
| 100 |
Set Typeclasses Dependency Order. |
| 101 |
Unset Typeclasses Iterative Deepening. |
| 102 |
Notation "x .1" := (projT1 x). |
| 103 |
Notation "x .2" := (projT2 x). |
| 104 |
|
| 105 |
Parameter myType: Type. |
| 106 |
|
| 107 |
Class Foo (a:myType) := {}. |
| 108 |
|
| 109 |
Class Bar (a:myType) := {}. |
| 110 |
|
| 111 |
Class Qux (a:myType) := {}. |
| 112 |
|
| 113 |
Parameter fooTobar : forall a (H : Foo a), {b: myType & Bar b}. |
| 114 |
|
| 115 |
Parameter barToqux : forall a (H : Bar a), {b: myType & Qux b}. |
| 116 |
|
| 117 |
#[export] Hint Extern 5 (Bar ?D.1) => |
| 118 |
destruct D; simpl : typeclass_instances. |
| 119 |
|
| 120 |
#[export] Hint Extern 5 (Qux ?D.1) => |
| 121 |
destruct D; simpl : typeclass_instances. |
| 122 |
|
| 123 |
#[export] Hint Extern 1 myType => |
| 124 |
unshelve refine (fooTobar _ _).1 : typeclass_instances. |
| 125 |
|
| 126 |
#[export] Hint Extern 1 myType => unshelve refine (barToqux _ _).1 : typeclass_instances. |
| 127 |
|
| 128 |
#[export] Hint Extern 0 { x : _ & _ } => simple refine (existT _ _ _) : typeclass_instances. |
| 129 |
|
| 130 |
Unset Typeclasses Debug. |
| 131 |
Definition trivial a (H : Foo a) : {b : myType & Qux b}. |
| 132 |
Proof. |
| 133 |
Time typeclasses eauto 10 with typeclass_instances. |
| 134 |
Undo. Set Typeclasses Iterative Deepening. |
| 135 |
Time typeclasses eauto with typeclass_instances. |
| 136 |
Defined. |
| 137 |
|
| 138 |
End NTabareau. |
| 139 |
|
| 140 |
Module NTabareauClasses. |
| 141 |
|
| 142 |
Set Typeclasses Dependency Order. |
| 143 |
Unset Typeclasses Iterative Deepening. |
| 144 |
Notation "x .1" := (projT1 x). |
| 145 |
Notation "x .2" := (projT2 x). |
| 146 |
|
| 147 |
Parameter myType: Type. |
| 148 |
Existing Class myType. |
| 149 |
|
| 150 |
Class Foo (a:myType) := {}. |
| 151 |
|
| 152 |
Class Bar (a:myType) := {}. |
| 153 |
|
| 154 |
Class Qux (a:myType) := {}. |
| 155 |
|
| 156 |
Parameter fooTobar : forall a (H : Foo a), {b: myType & Bar b}. |
| 157 |
|
| 158 |
Parameter barToqux : forall a (H : Bar a), {b: myType & Qux b}. |
| 159 |
|
| 160 |
#[export] Hint Extern 5 (Bar ?D.1) => |
| 161 |
destruct D; simpl : typeclass_instances. |
| 162 |
|
| 163 |
#[export] Hint Extern 5 (Qux ?D.1) => |
| 164 |
destruct D; simpl : typeclass_instances. |
| 165 |
|
| 166 |
#[export] Hint Extern 1 myType => |
| 167 |
unshelve notypeclasses refine (fooTobar _ _).1 : typeclass_instances. |
| 168 |
|
| 169 |
#[export] Hint Extern 1 myType => |
| 170 |
unshelve notypeclasses refine (barToqux _ _).1 : typeclass_instances. |
| 171 |
|
| 172 |
#[export] Hint Extern 0 { x : _ & _ } => |
| 173 |
unshelve notypeclasses refine (existT _ _ _) : typeclass_instances. |
| 174 |
|
| 175 |
Unset Typeclasses Debug. |
| 176 |
|
| 177 |
Definition trivial a (H : Foo a) : {b : myType & Qux b}. |
| 178 |
Proof. |
| 179 |
Time typeclasses eauto 10 with typeclass_instances. |
| 180 |
Undo. Set Typeclasses Iterative Deepening. |
| 181 |
(* Much faster in iteratove deepening mode *) |
| 182 |
Time typeclasses eauto with typeclass_instances. |
| 183 |
Defined. |
| 184 |
|
| 185 |
End NTabareauClasses. |
| 186 |
|
| 187 |
|
| 188 |
Require Import ListDef. |
| 189 |
|
| 190 |
Parameter in_list : list (nat * nat) -> nat -> Prop. |
| 191 |
Definition not_in_list (l : list (nat * nat)) (n : nat) : Prop := |
| 192 |
~ in_list l n. |
| 193 |
|
| 194 |
(* Hints Unfold not_in_list. *) |
| 195 |
|
| 196 |
Axiom |
| 197 |
lem1 : |
| 198 |
forall (l1 l2 : list (nat * nat)) (n : nat), |
| 199 |
not_in_list (l1 ++ l2) n -> not_in_list l1 n. |
| 200 |
|
| 201 |
Axiom |
| 202 |
lem2 : |
| 203 |
forall (l1 l2 : list (nat * nat)) (n : nat), |
| 204 |
not_in_list (l1 ++ l2) n -> not_in_list l2 n. |
| 205 |
|
| 206 |
Axiom |
| 207 |
lem3 : |
| 208 |
forall (l : list (nat * nat)) (n p q : nat), |
| 209 |
not_in_list ((p, q) :: l) n -> not_in_list l n. |
| 210 |
|
| 211 |
Axiom |
| 212 |
lem4 : |
| 213 |
forall (l1 l2 : list (nat * nat)) (n : nat), |
| 214 |
not_in_list l1 n -> not_in_list l2 n -> not_in_list (l1 ++ l2) n. |
| 215 |
|
| 216 |
#[export] Hint Resolve lem1 lem2 lem3 lem4: essai. |
| 217 |
|
| 218 |
Goal |
| 219 |
forall (l : list (nat * nat)) (n p q : nat), |
| 220 |
not_in_list ((p, q) :: l) n -> not_in_list l n. |
| 221 |
intros. |
| 222 |
eauto with essai. |
| 223 |
Qed. |
| 224 |
|
| 225 |
Module StrictlyUnique. |
| 226 |
Inductive thing := | List. |
| 227 |
Module Problem. |
| 228 |
Class WhatIsThis (T : Type) (t : T) : Type := what_is : thing. |
| 229 |
Arguments what_is {T} t _. |
| 230 |
Instance WhatIsThis_list {X} ls : @WhatIsThis (list X) ls := List. |
| 231 |
|
| 232 |
Class DynamicType := { dyn_type : Type }. |
| 233 |
|
| 234 |
(* [WhatIsThis ls] cannot be resolved despite having a clear answer. The |
| 235 |
fact that the [DynamicType] instance cannot be found makes Coq abandon |
| 236 |
the search for [WhatIsThis] entirely. *) |
| 237 |
Fail Example test := Eval lazy in (fun (ls : list dyn_type) => what_is ls _) _. |
| 238 |
End Problem. |
| 239 |
|
| 240 |
Module Solution. |
| 241 |
(* Inform Coq that [WhatIsThis] will not/must not instantiate evars. *) |
| 242 |
#[local] Set Typeclasses Strict Resolution. |
| 243 |
(* Inform Coq that [WhatIsThis] should not backtrack. *) |
| 244 |
#[local] Set Typeclasses Unique Instances. |
| 245 |
Class WhatIsThis (T : Type) (t : T) : Type := what_is : thing. |
| 246 |
Arguments what_is {T} t _. |
| 247 |
#[local] Unset Typeclasses Strict Resolution. |
| 248 |
#[local] Unset Typeclasses Unique Instances. |
| 249 |
|
| 250 |
Instance WhatIsThis_list {X} ls : @WhatIsThis (list X) ls := List. |
| 251 |
|
| 252 |
Class DynamicType := { dyn_type : Type }. |
| 253 |
|
| 254 |
(* We can now resolve [WhatIsThis] even though we don't have a [DynamicType] *) |
| 255 |
Example test := Eval lazy in (fun (ls : list dyn_type) => what_is ls _) _. |
| 256 |
End Solution. |
| 257 |
|
| 258 |
Module Dependencies. |
| 259 |
(* Other evars can still depend on strictly unique evars by using them |
| 260 |
directly in their type. We must make sure to not forget those dependencies. *) |
| 261 |
|
| 262 |
#[local] Set Typeclasses Strict Resolution. |
| 263 |
#[local] Set Typeclasses Unique Instances. |
| 264 |
Class SU : Type := su : unit. |
| 265 |
Arguments su : clear implicits. |
| 266 |
#[local] Unset Typeclasses Strict Resolution. |
| 267 |
#[local] Unset Typeclasses Unique Instances. |
| 268 |
|
| 269 |
Instance SU_inst : SU := tt. |
| 270 |
|
| 271 |
Class Depends (t : unit) := {}. |
| 272 |
Hint Mode Depends + : typeclass_instances. |
| 273 |
|
| 274 |
Instance Depends_inst {t} : Depends t := {}. |
| 275 |
|
| 276 |
Example test := Eval lazy in (fun (s : SU) (d : Depends (su s)) => d) _ _. |
| 277 |
End Dependencies. |
| 278 |
End StrictlyUnique. |