Skip to content

Commit d4bb337

Browse files
authored
Merge pull request #2241 from patrick-nicodemus/rapply_timing
Alternate definitions of rapply
2 parents 239bcf4 + 6cba73d commit d4bb337

File tree

177 files changed

+2764
-2694
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

177 files changed

+2764
-2694
lines changed

STYLE.md

+42-15
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ Here are some acceptable tactics to use in transparent definitions
932932
- `case`, `elim`, `destruct`, `induction`
933933
- `apply`, `eapply`, `assumption`, `eassumption`, `exact`
934934
- `refine`, `nrefine`, `srefine`, `snrefine` (see below for the last three)
935-
- `rapply`, `nrapply`, `srapply`, `snrapply` (see below)
935+
- `rapply`, `napply`, `tapply`, `srapply`, `snapply`, `stapply` (see below)
936936
- `lhs`, `lhs_V`, `rhs`, `rhs_V`
937937
- `reflexivity`, `symmetry`, `transitivity`, `etransitivity`
938938
- `by`, `done`
@@ -1197,31 +1197,58 @@ They are described more fully, usually with examples, in the files
11971197
where they are defined.
11981198

11991199
- `nrefine`, `srefine`, `snrefine`:
1200-
Defined in `Basics/Overture`, these are shorthands for
1200+
Defined in `Basics/Tactics`, these are shorthands for
12011201
`notypeclasses refine`, `simple refine`, and `simple notypeclasses refine`.
12021202
It's good to avoid typeclass search if it isn't needed.
12031203

1204-
- `rapply`, `nrapply`, `srapply`, `snrapply`:
1205-
Defined in `Basics/Overture`, these tactics use `refine`,
1206-
`nrefine`, `srefine` and `snrefine`, except that additional holes
1207-
are added to the function so they behave like `apply` does.
1208-
The unification algorithm used by `apply` is different and often
1209-
less powerful than the one used by `refine`, though it is
1210-
occasionally better at pattern matching.
1204+
- `napply`, `rapply`, `tapply`:
1205+
Defined in `Basics/Tactics`, each of these is similar to `apply`,
1206+
except that they use the unification engine of `refine`, which
1207+
is different and often stronger than that of `apply`.
1208+
`napply t` computes the type of `t`
1209+
(possibly with holes, if `t` has holes) and tries to unify the type
1210+
of `t` with the goal; if this succeeds, it generates goals for each
1211+
hole in `t` not solved by unification; otherwise, it repeats this
1212+
process with `t _`, `t _ _ `, and so on until it has the correct
1213+
number of arguments to unify with the goal.
1214+
`rapply` is like `napply`, (`rapply` succeeds iff
1215+
`napply` does) except that after it succeeds in unifying with the
1216+
goal, it solves all typeclass goals it can. `tapply` is stronger
1217+
than `rapply`: if Coq cannot compute a type for `t` or successfully
1218+
unify the type of `t` with the goal, it will elaborate all typeclass
1219+
holes in `t` that it can, and then try again to compute the type of
1220+
`t` and unify it with the goal. (Like `rapply`, `tapply` also
1221+
instantiates typeclass goals after successful unification with the
1222+
goal as well: if `rapply` succeeds, so does `tapply` and their
1223+
outcomes are equivalent.)
1224+
1225+
- `snapply`, `srapply`, `stapply`: Sibling tactics to `napply`,
1226+
`rapply` and `tapply`, except that they use `simple refine` instead
1227+
of `refine` (beta reduction is not attempted when unifying with the
1228+
goal, and no new goals are shelved)
12111229

12121230
Here are some tips:
12131231
- If `apply` fails with a unification error you think it shouldn't
1214-
have, try `nrapply`.
1215-
- If you want to use type class resolution as well, try `rapply`.
1216-
But it's better to use `nrapply` if it works.
1232+
have, try `napply`, and then `rapply` if `napply` generates
1233+
typeclass goals.
1234+
- If you want to use type class resolution as well, try `tapply`.
1235+
But it's better to use `napply` if it works.
12171236
- You could add a prime to the tactic, to try with many arguments
12181237
first, decreasing the number on each try.
1219-
- If you don't want Coq to create evars for certain subgoals,
1220-
add an `s` to the tactic name to make it use `simple refine`.
1238+
- If you are trying to construct an element of a sum type `sig (A :
1239+
Type) (P: A->Type)` (or something equivalent, such as a `NatTrans`
1240+
record consisting of a `Transformation` and a proof that it
1241+
`Is1Natural`) and you want to manually construct `t : A` first and
1242+
then prove that `P t` holds for the given `t`, then use the
1243+
`simple` version of the tactic, like `srapply exist` or `srapply
1244+
Build_Record`, so that the first goal generated is `A`. If it's
1245+
more convenient to instantiate `a : A` while proving `P`, then use
1246+
`rapply exist` - for example, `Goal { x & x = 0 }. rapply exist;
1247+
reflexivity. Qed.`
12211248

12221249
- `lhs`, `lhs_V`, `rhs`, `rhs_V`: Defined in `Basics/Tactics`.
12231250
These are tacticals that apply a specified tactic to one side
1224-
of an equality. E.g. `lhs nrapply concat_1p.`
1251+
of an equality. E.g. `lhs napply concat_1p.`
12251252

12261253
- `transparent assert`: Defined in `Basics/Overture`, this tactic is
12271254
like `assert` but produces a transparent subterm rather than an

contrib/HoTTBookExercises.v

+19-19
Original file line numberDiff line numberDiff line change
@@ -723,8 +723,8 @@ Section Book_2_17_prod.
723723
: Book_2_17_ii_prod = Book_2_17_i_prod.
724724
Proof.
725725
apply path_equiv; simpl.
726-
lhs nrapply Book_2_17_eq_prod'.
727-
snrapply ap011.
726+
lhs napply Book_2_17_eq_prod'.
727+
snapply ap011.
728728
- apply transport_idmap_path_universe_uncurried.
729729
- apply transport_idmap_path_universe_uncurried.
730730
Qed.
@@ -748,7 +748,7 @@ Section Book_2_17_sigma.
748748
: {a : A & B a} <~> {a' : A' & B' a'}.
749749
Proof.
750750
apply equiv_path.
751-
snrapply ap011D.
751+
snapply ap011D.
752752
- exact (path_universe_uncurried f).
753753
- apply Book_2_17_path_fibr_1.
754754
apply path_arrow; intro a.
@@ -773,12 +773,12 @@ Section Book_2_17_sigma.
773773
: Book_2_17_ii_sigma = Book_2_17_i_sigma.
774774
Proof.
775775
apply path_equiv; simpl.
776-
lhs nrapply Book_2_17_eq_sigma'.
777-
snrapply ap011D.
776+
lhs napply Book_2_17_eq_sigma'.
777+
snapply ap011D.
778778
- apply transport_idmap_path_universe_uncurried.
779779
- destruct (transport_idmap_path_universe_uncurried f); simpl.
780780
apply path_forall; intro a.
781-
lhs nrapply (ap (fun h => _ (h _)) (eisretr _ _)).
781+
lhs napply (ap (fun h => _ (h _)) (eisretr _ _)).
782782
apply transport_idmap_path_universe_uncurried.
783783
Defined.
784784
End Book_2_17_sigma.
@@ -808,8 +808,8 @@ Section Book_2_17_arrow.
808808
: Book_2_17_ii_arrow = Book_2_17_i_arrow.
809809
Proof.
810810
apply path_equiv; simpl.
811-
lhs nrapply Book_2_17_eq_arrow'.
812-
snrapply ap011.
811+
lhs napply Book_2_17_eq_arrow'.
812+
snapply ap011.
813813
- apply transport_idmap_path_universe_uncurried.
814814
- exact (ap (fun g0 _ => g0)
815815
(transport_idmap_path_universe_uncurried g)).
@@ -833,12 +833,12 @@ Section Book_2_17_forall.
833833
: (forall a, B a) <~> (forall a', B' a').
834834
Proof.
835835
apply equiv_path.
836-
snrapply (ap011D (fun A0 B0 => forall a0, B0 a0)).
836+
snapply (ap011D (fun A0 B0 => forall a0, B0 a0)).
837837
- exact (path_universe_uncurried f)^.
838838
- apply Book_2_17_path_fibr_2.
839839
apply path_arrow; intro a.
840840
apply path_universe_uncurried.
841-
nrapply (transport (fun f0 => B (f0 _) <~> _)
841+
napply (transport (fun f0 => B (f0 _) <~> _)
842842
(transport_idmap_path_universe_uncurried f)^).
843843
apply g.
844844
Defined.
@@ -859,12 +859,12 @@ Section Book_2_17_forall.
859859
: Book_2_17_ii_forall = Book_2_17_i_forall.
860860
Proof.
861861
apply path_equiv; simpl.
862-
lhs nrapply Book_2_17_eq_forall'.
863-
snrapply ap011D.
862+
lhs napply Book_2_17_eq_forall'.
863+
snapply ap011D.
864864
- apply transport_idmap_path_universe_uncurried.
865865
- destruct (transport_idmap_path_universe_uncurried f); simpl.
866866
apply path_forall; intro a.
867-
lhs nrapply (ap (fun h => _ (h _)) (eisretr _ _)).
867+
lhs napply (ap (fun h => _ (h _)) (eisretr _ _)).
868868
apply transport_idmap_path_universe_uncurried.
869869
Defined.
870870
End Book_2_17_forall.
@@ -894,8 +894,8 @@ Section Book_2_17_sum.
894894
: Book_2_17_ii_sum = Book_2_17_i_sum.
895895
Proof.
896896
apply path_equiv; simpl.
897-
lhs nrapply Book_2_17_eq_sum'.
898-
snrapply ap011.
897+
lhs napply Book_2_17_eq_sum'.
898+
snapply ap011.
899899
- apply transport_idmap_path_universe_uncurried.
900900
- apply transport_idmap_path_universe_uncurried.
901901
Qed.
@@ -1064,7 +1064,7 @@ Section Book_3_8_ctx.
10641064
Definition Book_3_8_equiv {A B : Type} (f : A -> B)
10651065
: Trunc (-1) (Book_3_8_qinv f) <~> (isequiv _ _ f).
10661066
Proof.
1067-
snrapply equiv_iff_hprop.
1067+
snapply equiv_iff_hprop.
10681068
- exact _.
10691069
- apply cond_iii.
10701070
- apply Trunc_rec, cond_ii.
@@ -1115,7 +1115,7 @@ Definition Book_3_10_impred@{i j k | i < j, j < k} `{Univalence}
11151115
(LEM : Book_3_10_LEM@{j})
11161116
: IsEquiv@{j k} Book_3_10_Lift@{i j}.
11171117
Proof.
1118-
snrapply isequiv_adjointify.
1118+
snapply isequiv_adjointify.
11191119
{ intro A. destruct (LEM A).
11201120
- exact (Build_HProp Unit).
11211121
- exact (Build_HProp Empty).
@@ -1342,7 +1342,7 @@ Definition Book_3_17 {A : Type} {B : Trunc (-1) A -> HProp}
13421342
: (forall a : A, B (tr a)) -> (forall x : Trunc (-1) A, B x).
13431343
Proof.
13441344
intros f x.
1345-
nrapply (Trunc_rec _ x).
1345+
napply (Trunc_rec _ x).
13461346
- exact _.
13471347
- intro a.
13481348
exact (transport B (path_ishprop (tr a) x) (f a)).
@@ -1693,7 +1693,7 @@ Defined.
16931693
Definition Book_4_6_iii (qua1 qua2 : QInv_Univalence_type) : Empty.
16941694
Proof.
16951695
apply (Book_4_6_ii qua1 qua2).
1696-
nrapply istrunc_succ.
1696+
napply istrunc_succ.
16971697
apply (Build_Contr _ (fun A => 1)); intros u.
16981698
exact (allqinv_coherent qua2 _ _ (idmap; (idmap; (fun A => 1, u)))).
16991699
Defined.

test/Tactics/napply.v

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
From HoTT Require Import Basics.Overture Basics.Tactics.
2+
From HoTT Require Import Spaces.Nat.Core.
3+
4+
Local Open Scope nat_scope.
5+
6+
(** Testing the different [apply] tacitcs in the library. *)
7+
8+
Definition test1_type := {n : nat & n < 3}.
9+
Fail Definition test1 : test1_type := ltac:(apply exist).
10+
Fail Definition test1 : test1_type := ltac:(rapply exist).
11+
Fail Definition test1 : test1_type := ltac:(napply exist).
12+
Fail Definition test1 : test1_type := ltac:(tapply exist).
13+
Succeed Definition test1 : test1_type := ltac:(napply exist; exact _).
14+
15+
(** Testing deprecated tactics *)
16+
Fail Definition test1 : test1_type := ltac:(nrapply exist).
17+
Fail Definition test1 : test1_type := ltac:(snrapply exist).
18+
19+

theories/Algebra/AbGroups/AbHom.v

+20-20
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Instance inverse_hom {A : Group} {B : AbGroup}
2121
(** For [A] and [B] groups, with [B] abelian, homomorphisms [A $-> B] form an abelian group. *)
2222
Definition grp_hom `{Funext} (A : Group) (B : AbGroup) : Group.
2323
Proof.
24-
snrapply (Build_Group' (GroupHomomorphism A B) sgop_hom grp_homo_const inverse_hom).
24+
snapply (Build_Group' (GroupHomomorphism A B) sgop_hom grp_homo_const inverse_hom).
2525
1: exact _.
2626
all: hnf; intros; apply equiv_path_grouphomomorphism; intro; cbn.
2727
- apply associativity.
@@ -31,7 +31,7 @@ Defined.
3131

3232
Definition ab_hom `{Funext} (A : Group) (B : AbGroup) : AbGroup.
3333
Proof.
34-
snrapply (Build_AbGroup (grp_hom A B)).
34+
snapply (Build_AbGroup (grp_hom A B)).
3535
intros f g; cbn.
3636
apply equiv_path_grouphomomorphism; intro x; cbn.
3737
apply commutativity.
@@ -52,7 +52,7 @@ Definition ab_coeq_glue {A B : AbGroup} {f g : A $-> B}
5252
: ab_coeq_in (f:=f) (g:=g) $o f $== ab_coeq_in $o g.
5353
Proof.
5454
intros x.
55-
nrapply qglue.
55+
napply qglue.
5656
apply tr.
5757
by exists x.
5858
Defined.
@@ -61,14 +61,14 @@ Definition ab_coeq_rec {A B : AbGroup} {f g : A $-> B}
6161
{C : AbGroup} (i : B $-> C) (p : i $o f $== i $o g)
6262
: ab_coeq f g $-> C.
6363
Proof.
64-
snrapply (grp_quotient_rec _ _ i).
64+
snapply (grp_quotient_rec _ _ i).
6565
cbn.
6666
intros b H.
6767
strip_truncations.
6868
destruct H as [a q].
6969
destruct q; simpl.
70-
lhs nrapply grp_homo_op.
71-
lhs nrapply (ap (+ _)).
70+
lhs napply grp_homo_op.
71+
lhs napply (ap (+ _)).
7272
1: apply grp_homo_inv.
7373
apply grp_moveL_M1^-1.
7474
exact (p a)^.
@@ -101,7 +101,7 @@ Definition functor_ab_coeq {A B : AbGroup} {f g : A $-> B} {A' B' : AbGroup} {f'
101101
(a : A $-> A') (b : B $-> B') (p : f' $o a $== b $o f) (q : g' $o a $== b $o g)
102102
: ab_coeq f g $-> ab_coeq f' g'.
103103
Proof.
104-
snrapply ab_coeq_rec.
104+
snapply ab_coeq_rec.
105105
1: exact (ab_coeq_in $o b).
106106
refine (cat_assoc _ _ _ $@ _ $@ cat_assoc_opp _ _ _).
107107
refine ((_ $@L p^$) $@ _ $@ (_ $@L q)).
@@ -116,7 +116,7 @@ Definition functor2_ab_coeq {A B : AbGroup} {f g : A $-> B} {A' B' : AbGroup} {f
116116
(s : b $== b')
117117
: functor_ab_coeq a b p q $== functor_ab_coeq a' b' p' q'.
118118
Proof.
119-
snrapply ab_coeq_ind_homotopy.
119+
snapply ab_coeq_ind_homotopy.
120120
intros x.
121121
exact (ap ab_coeq_in (s x)).
122122
Defined.
@@ -130,14 +130,14 @@ Definition functor_ab_coeq_compose {A B : AbGroup} {f g : A $-> B}
130130
: functor_ab_coeq a' b' p' q' $o functor_ab_coeq a b p q
131131
$== functor_ab_coeq (a' $o a) (b' $o b) (hconcat p p') (hconcat q q').
132132
Proof.
133-
snrapply ab_coeq_ind_homotopy.
133+
snapply ab_coeq_ind_homotopy.
134134
simpl; reflexivity.
135135
Defined.
136136

137137
Definition functor_ab_coeq_id {A B : AbGroup} (f g : A $-> B)
138138
: functor_ab_coeq (f:=f) (g:=g) (Id _) (Id _) (hrefl _) (hrefl _) $== Id _.
139139
Proof.
140-
snrapply ab_coeq_ind_homotopy.
140+
snapply ab_coeq_ind_homotopy.
141141
reflexivity.
142142
Defined.
143143

@@ -146,24 +146,24 @@ Definition grp_iso_ab_coeq {A B : AbGroup} {f g : A $-> B}
146146
(a : A $<~> A') (b : B $<~> B') (p : f' $o a $== b $o f) (q : g' $o a $== b $o g)
147147
: ab_coeq f g $<~> ab_coeq f' g'.
148148
Proof.
149-
snrapply cate_adjointify.
149+
snapply cate_adjointify.
150150
- exact (functor_ab_coeq a b p q).
151151
- exact (functor_ab_coeq a^-1$ b^-1$ (hinverse _ _ p) (hinverse _ _ q)).
152152
- nrefine (functor_ab_coeq_compose _ _ _ _ _ _ _ _
153153
$@ functor2_ab_coeq _ _ _ _ _ $@ functor_ab_coeq_id _ _).
154-
rapply cate_isretr.
154+
tapply cate_isretr.
155155
- nrefine (functor_ab_coeq_compose _ _ _ _ _ _ _ _
156156
$@ functor2_ab_coeq _ _ _ _ _ $@ functor_ab_coeq_id _ _).
157-
rapply cate_issect.
157+
tapply cate_issect.
158158
Defined.
159159

160160
(** ** The bifunctor [ab_hom] *)
161161

162162
Instance is0functor_ab_hom01 `{Funext} {A : Group^op}
163163
: Is0Functor (ab_hom A).
164164
Proof.
165-
snrapply (Build_Is0Functor _ AbGroup); intros B B' f.
166-
snrapply Build_GroupHomomorphism.
165+
snapply (Build_Is0Functor _ AbGroup); intros B B' f.
166+
snapply Build_GroupHomomorphism.
167167
1: exact (fun g => grp_homo_compose f g).
168168
intros phi psi.
169169
apply equiv_path_grouphomomorphism; intro a; cbn.
@@ -173,8 +173,8 @@ Defined.
173173
Instance is0functor_ab_hom10 `{Funext} {B : AbGroup@{u}}
174174
: Is0Functor (flip (ab_hom : Group^op -> AbGroup -> AbGroup) B).
175175
Proof.
176-
snrapply (Build_Is0Functor (Group^op) AbGroup); intros A A' f.
177-
snrapply Build_GroupHomomorphism.
176+
snapply (Build_Is0Functor (Group^op) AbGroup); intros A A' f.
177+
snapply Build_GroupHomomorphism.
178178
1: exact (fun g => grp_homo_compose g f).
179179
intros phi psi.
180180
by apply equiv_path_grouphomomorphism.
@@ -183,7 +183,7 @@ Defined.
183183
Instance is1functor_ab_hom01 `{Funext} {A : Group^op}
184184
: Is1Functor (ab_hom A).
185185
Proof.
186-
nrapply Build_Is1Functor.
186+
napply Build_Is1Functor.
187187
- intros B B' f g p phi.
188188
apply equiv_path_grouphomomorphism; intro a; cbn.
189189
exact (p (phi a)).
@@ -196,7 +196,7 @@ Defined.
196196
Instance is1functor_ab_hom10 `{Funext} {B : AbGroup@{u}}
197197
: Is1Functor (flip (ab_hom : Group^op -> AbGroup -> AbGroup) B).
198198
Proof.
199-
nrapply Build_Is1Functor.
199+
napply Build_Is1Functor.
200200
- intros A A' f g p phi.
201201
apply equiv_path_grouphomomorphism; intro a; cbn.
202202
exact (ap phi (p a)).
@@ -215,7 +215,7 @@ Defined.
215215
Instance is1bifunctor_ab_hom `{Funext}
216216
: Is1Bifunctor (ab_hom : Group^op -> AbGroup -> AbGroup).
217217
Proof.
218-
nrapply Build_Is1Bifunctor''.
218+
napply Build_Is1Bifunctor''.
219219
1,2: exact _.
220220
intros A A' f B B' g phi; cbn.
221221
by apply equiv_path_grouphomomorphism.

0 commit comments

Comments
 (0)