-
Notifications
You must be signed in to change notification settings - Fork 662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve discrimination of lambdas in typeclass search #19977
base: master
Are you sure you want to change the base?
Conversation
@coqbot bench |
No check suite 31373 for head commit efebfcc. |
@coqbot bench |
🏁 Bench results:
INFO: failed to install 🐢 Top 25 slow downs┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ TOP 25 SLOW DOWNS │ │ │ │ OLD NEW DIFF %DIFF Ln FILE │ ├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ 64.6490 66.2530 1.6040 2.48% 609 coq-fiat-crypto-with-bedrock/rupicola/bedrock2/bedrock2/src/bedrock2Examples/lightbulb.v.html │ │ 118.3310 119.5180 1.1870 1.00% 22 coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/ArithWithCasts.v.html │ │ 47.7840 48.3800 0.5960 1.25% 376 coq-unimath/UniMath/ModelCategories/Generated/LNWFSMonoidalStructure.v.html │ │ 46.2620 46.8160 0.5540 1.20% 110 coq-fiat-crypto-with-bedrock/rupicola/bedrock2/bedrock2/src/bedrock2Examples/full_mul.v.html │ │ 64.7520 65.2210 0.4690 0.72% 609 coq-bedrock2/bedrock2/src/bedrock2Examples/lightbulb.v.html │ │ 56.4300 56.8970 0.4670 0.83% 731 coq-fiat-crypto-with-bedrock/src/Bedrock/Secp256k1/JacobianCoZ.v.html │ │ 12.5160 12.9510 0.4350 3.48% 1555 coq-fiat-crypto-with-bedrock/src/Util/FSets/FMapTrie.v.html │ │ 18.1900 18.6030 0.4130 2.27% 459 coq-unimath/UniMath/CategoryTheory/Hyperdoctrines/PartialEqRels/ExponentialPER.v.html │ │ 31.3600 31.7640 0.4040 1.29% 97 coq-vst/veric/binop_lemmas5.v.html │ │ 46.2510 46.6490 0.3980 0.86% 110 coq-bedrock2/bedrock2/src/bedrock2Examples/full_mul.v.html │ │ 24.4310 24.8210 0.3900 1.60% 558 coq-fiat-crypto-with-bedrock/rupicola/bedrock2/bedrock2/src/bedrock2Examples/insertionsort.v.html │ │ 20.2810 20.6630 0.3820 1.88% 297 coq-unimath/UniMath/CategoryTheory/Hyperdoctrines/PartialEqRels/Logic/Universal.v.html │ │ 25.5500 25.9270 0.3770 1.48% 375 coq-unimath/UniMath/ModelCategories/Generated/LNWFSMonoidalStructure.v.html │ │ 25.5710 25.9290 0.3580 1.40% 374 coq-unimath/UniMath/ModelCategories/Generated/LNWFSMonoidalStructure.v.html │ │ 24.5370 24.8850 0.3480 1.42% 558 coq-bedrock2/bedrock2/src/bedrock2Examples/insertionsort.v.html │ │ 42.3470 42.6750 0.3280 0.77% 834 coq-vst/veric/binop_lemmas4.v.html │ │ 38.9720 39.2820 0.3100 0.80% 548 coq-fiat-crypto-with-bedrock/rupicola/bedrock2/compiler/src/compiler/MMIO.v.html │ │ 26.5430 26.8450 0.3020 1.14% 68 coq-fiat-crypto-with-bedrock/rupicola/bedrock2/deps/riscv-coq/src/riscv/Proofs/VerifyDecode.v.html │ │ 5.9660 6.2610 0.2950 4.94% 5 coq-fiat-crypto-with-bedrock/src/Assembly/Parse/Examples/fiat_p256_mul_optimised_seed12.v.html │ │ 18.0770 18.3550 0.2780 1.54% 481 coq-verdi-raft/theories/RaftProofs/EndToEndLinearizability.v.html │ │ 15.9790 16.2520 0.2730 1.71% 197 coq-fiat-crypto-with-bedrock/src/Demo.v.html │ │ 14.5260 14.7890 0.2630 1.81% 3158 coq-fiat-crypto-with-bedrock/src/Assembly/WithBedrock/Proofs.v.html │ │ 39.1480 39.4020 0.2540 0.65% 835 coq-fiat-crypto-with-bedrock/src/Fancy/Compiler.v.html │ │ 199.5870 199.8260 0.2390 0.12% 8 coq-neural-net-interp-computed-lite/theories/MaxOfTwoNumbersSimpler/Computed/AllLogits.v.html │ │ 7.9940 8.2300 0.2360 2.95% 559 coq-unimath/UniMath/CategoryTheory/Hyperdoctrines/PartialEqRels/ExponentialPER.v.html │ └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ 🐇 Top 25 speed ups┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ TOP 25 SPEED UPS │ │ │ │ OLD NEW DIFF %DIFF Ln FILE │ ├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ 177.9460 175.1050 -2.8410 -1.60% 233 coq-fiat-crypto-with-bedrock/rupicola/bedrock2/deps/riscv-coq/src/riscv/Proofs/DecodeByExtension.v.html │ │ 38.5450 37.3780 -1.1670 -3.03% 3 coq-fiat-crypto-with-bedrock/src/ExtractionJsOfOCaml/bedrock2_fiat_crypto.v.html │ │ 37.0650 36.0950 -0.9700 -2.62% 2 coq-fiat-crypto-with-bedrock/src/ExtractionJsOfOCaml/fiat_crypto.v.html │ │ 38.3790 37.4100 -0.9690 -2.52% 3 coq-fiat-crypto-with-bedrock/src/ExtractionJsOfOCaml/WithBedrock/fiat_crypto.v.html │ │ 102.3830 101.7670 -0.6160 -0.60% 999 coq-performance-tests-lite/src/fiat_crypto_via_setoid_rewrite_standalone.v.html │ │ 22.5100 21.9450 -0.5650 -2.51% 49 coq-fiat-crypto-with-bedrock/src/Curves/Weierstrass/AffineProofs.v.html │ │ 29.0010 28.4650 -0.5360 -1.85% 12 coq-fourcolor/theories/proof/job563to588.v.html │ │ 30.2930 29.7570 -0.5360 -1.77% 12 coq-fourcolor/theories/proof/job001to106.v.html │ │ 25.6660 25.1310 -0.5350 -2.08% 12 coq-fourcolor/theories/proof/job535to541.v.html │ │ 23.6770 23.1680 -0.5090 -2.15% 12 coq-fourcolor/theories/proof/job495to498.v.html │ │ 28.7110 28.2270 -0.4840 -1.69% 32 coq-fiat-crypto-with-bedrock/src/Bedrock/End2End/X25519/MontgomeryLadderRISCV.v.html │ │ 29.1200 28.6440 -0.4760 -1.63% 144 coq-fiat-crypto-with-bedrock/src/Bedrock/End2End/X25519/GarageDoorTop.v.html │ │ 102.3730 101.9100 -0.4630 -0.45% 968 coq-performance-tests-lite/src/fiat_crypto_via_setoid_rewrite_standalone.v.html │ │ 24.4000 23.9460 -0.4540 -1.86% 12 coq-fourcolor/theories/proof/job486to489.v.html │ │ 33.2810 32.8420 -0.4390 -1.32% 12 coq-fourcolor/theories/proof/job439to465.v.html │ │ 27.1630 26.7400 -0.4230 -1.56% 147 coq-fiat-crypto-with-bedrock/src/Bedrock/End2End/X25519/GarageDoorTop.v.html │ │ 26.7250 26.3030 -0.4220 -1.58% 35 coq-fiat-crypto-with-bedrock/src/Bedrock/End2End/X25519/MontgomeryLadderRISCV.v.html │ │ 20.9620 20.5650 -0.3970 -1.89% 213 coq-fiat-crypto-with-bedrock/src/Curves/EdwardsMontgomery.v.html │ │ 1.2110 0.8560 -0.3550 -29.31% 2498 coq-metacoq-pcuic/pcuic/theories/PCUICConversion.v.html │ │ 2.1940 1.8410 -0.3530 -16.09% 957 coq-metacoq-safechecker/safechecker/theories/PCUICSafeRetyping.v.html │ │ 25.4840 25.1530 -0.3310 -1.30% 12 coq-fourcolor/theories/proof/job517to530.v.html │ │ 27.0320 26.7050 -0.3270 -1.21% 12 coq-fourcolor/theories/proof/job531to534.v.html │ │ 9.2900 8.9740 -0.3160 -3.40% 550 coq-metacoq-erasure/erasure/theories/ErasureFunctionProperties.v.html │ │ 26.6190 26.3060 -0.3130 -1.18% 12 coq-fourcolor/theories/proof/job190to206.v.html │ │ 24.7380 24.4380 -0.3000 -1.21% 12 coq-fourcolor/theories/proof/job503to506.v.html │ └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ |
The failure in |
@Janno would you mind sharing your analysis. Are the instances something like |
No, it's simpler than that. All the instances are |
|
@Janno did you run this on your private bench? |
Yes. But we do not currently use classes such as For now, the results are mildly positive. There are some files that see bigger speedups because of typeclasses that interact with
|
Even though syntactic pattern matching does not respect eta we must nonetheless honor the eta invariant, i.e. terms that could potentially be eta reduced must be eta reduced.
@coqbot bench |
🏁 Bench results:
INFO: failed to install 🐢 Top 25 slow downs┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ TOP 25 SLOW DOWNS │ │ │ │ OLD NEW DIFF %DIFF Ln FILE │ ├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ 62.3990 66.1870 3.7880 6.07% 609 coq-fiat-crypto-with-bedrock/rupicola/bedrock2/bedrock2/src/bedrock2Examples/lightbulb.v.html │ │ 64.2080 65.5940 1.3860 2.16% 609 coq-bedrock2/bedrock2/src/bedrock2Examples/lightbulb.v.html │ │ 91.2320 92.6120 1.3800 1.51% 20 coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/NBE.v.html │ │ 117.1230 117.9170 0.7940 0.68% 22 coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/ArithWithCasts.v.html │ │ 17.7910 18.2660 0.4750 2.67% 481 coq-verdi-raft/theories/RaftProofs/EndToEndLinearizability.v.html │ │ 38.1040 38.5340 0.4300 1.13% 3 coq-fiat-crypto-with-bedrock/src/ExtractionJsOfOCaml/WithBedrock/fiat_crypto.v.html │ │ 46.1420 46.5560 0.4140 0.90% 110 coq-bedrock2/bedrock2/src/bedrock2Examples/full_mul.v.html │ │ 11.7680 12.1790 0.4110 3.49% 521 coq-unimath/UniMath/CategoryTheory/Hyperdoctrines/PartialEqRels/PERCategory.v.html │ │ 4.8370 5.2010 0.3640 7.53% 19 coq-fiat-crypto-with-bedrock/src/Language/IdentifiersBasicGENERATED.v.html │ │ 47.7000 48.0640 0.3640 0.76% 376 coq-unimath/UniMath/ModelCategories/Generated/LNWFSMonoidalStructure.v.html │ │ 31.0020 31.3420 0.3400 1.10% 12 coq-fourcolor/theories/proof/job254to270.v.html │ │ 10.5900 10.9300 0.3400 3.21% 551 coq-unimath/UniMath/CategoryTheory/Hyperdoctrines/PartialEqRels/PERCategory.v.html │ │ 13.6630 13.9760 0.3130 2.29% 249 coq-fiat-crypto-with-bedrock/rupicola/bedrock2/compiler/src/compiler/memory_mapped_ext_calls_compiler.v.html │ │ 59.0570 59.3430 0.2860 0.48% 27 coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/ToFancyWithCasts.v.html │ │ 29.0550 29.3360 0.2810 0.97% 12 coq-fourcolor/theories/proof/job589to610.v.html │ │ 18.3300 18.6080 0.2780 1.52% 12 coq-fourcolor/theories/proof/job311to314.v.html │ │ 20.7500 21.0280 0.2780 1.34% 23 coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/Arith.v.html │ │ 9.1990 9.4370 0.2380 2.59% 403 coq-unimath/UniMath/CategoryTheory/Hyperdoctrines/PartialEqRels/ExponentialLam.v.html │ │ 0.0000 0.2340 0.2340 inf% 574 coq-mathcomp-analysis/theories/sequences.v.html │ │ 29.2760 29.5070 0.2310 0.79% 12 coq-fourcolor/theories/proof/job323to383.v.html │ │ 38.3780 38.6080 0.2300 0.60% 3 coq-fiat-crypto-with-bedrock/src/ExtractionJsOfOCaml/bedrock2_fiat_crypto.v.html │ │ 0.0000 0.2250 0.2250 inf% 130 coq-metacoq-erasure/erasure/theories/Typed/ExtractionCorrectness.v.html │ │ 14.9080 15.1270 0.2190 1.47% 615 coq-fiat-crypto-with-bedrock/rupicola/bedrock2/compiler/src/compiler/FlattenExpr.v.html │ │ 0.2400 0.4540 0.2140 89.17% 273 coq-metacoq-erasure/erasure/theories/Typed/Erasure.v.html │ │ 0.0000 0.2120 0.2120 inf% 201 coq-mathcomp-analysis/theories/derive.v.html │ └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ 🐇 Top 25 speed ups┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ TOP 25 SPEED UPS │ │ │ │ OLD NEW DIFF %DIFF Ln FILE │ ├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │ 180.2190 177.1830 -3.0360 -1.68% 233 coq-fiat-crypto-with-bedrock/rupicola/bedrock2/deps/riscv-coq/src/riscv/Proofs/DecodeByExtension.v.html │ │ 79.2920 78.1780 -1.1140 -1.40% 48 coq-fiat-crypto-with-bedrock/src/Curves/Weierstrass/AffineProofs.v.html │ │ 56.3320 55.4420 -0.8900 -1.58% 731 coq-fiat-crypto-with-bedrock/src/Bedrock/Secp256k1/JacobianCoZ.v.html │ │ 102.5120 101.8190 -0.6930 -0.68% 999 coq-performance-tests-lite/src/fiat_crypto_via_setoid_rewrite_standalone.v.html │ │ 2.4520 1.9130 -0.5390 -21.98% 32 coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/NBE.v.html │ │ 102.4330 101.9090 -0.5240 -0.51% 968 coq-performance-tests-lite/src/fiat_crypto_via_setoid_rewrite_standalone.v.html │ │ 42.6130 42.1300 -0.4830 -1.13% 834 coq-vst/veric/binop_lemmas4.v.html │ │ 9.3550 8.8940 -0.4610 -4.93% 550 coq-metacoq-erasure/erasure/theories/ErasureFunctionProperties.v.html │ │ 24.4610 24.0530 -0.4080 -1.67% 12 coq-fourcolor/theories/proof/job231to234.v.html │ │ 30.4160 30.0100 -0.4060 -1.33% 607 coq-fiat-crypto-with-bedrock/src/Bedrock/Secp256k1/JacobianCoZ.v.html │ │ 21.6220 21.2320 -0.3900 -1.80% 12 coq-fourcolor/theories/proof/job511to516.v.html │ │ 2.2030 1.8150 -0.3880 -17.61% 957 coq-metacoq-safechecker/safechecker/theories/PCUICSafeRetyping.v.html │ │ 1.9480 1.5610 -0.3870 -19.87% 20 coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/NBE.v.html │ │ 23.3580 23.0030 -0.3550 -1.52% 373 coq-fiat-crypto-with-bedrock/src/Bedrock/Secp256k1/Addchain.v.html │ │ 1.1640 0.8150 -0.3490 -29.98% 2498 coq-metacoq-pcuic/pcuic/theories/PCUICConversion.v.html │ │ 25.4130 25.0750 -0.3380 -1.33% 12 coq-fourcolor/theories/proof/job618to622.v.html │ │ 21.2340 20.9030 -0.3310 -1.56% 213 coq-fiat-crypto-with-bedrock/src/Curves/EdwardsMontgomery.v.html │ │ 9.6490 9.3200 -0.3290 -3.41% 434 coq-mathcomp-odd-order/theories/PFsection12.v.html │ │ 4.1540 3.8330 -0.3210 -7.73% 1127 coq-unimath/UniMath/CategoryTheory/Hyperdoctrines/PartialEqRels/PERSubobjectClassifier.v.html │ │ 28.9670 28.6550 -0.3120 -1.08% 144 coq-fiat-crypto-with-bedrock/src/Bedrock/End2End/X25519/GarageDoorTop.v.html │ │ 200.7630 200.4660 -0.2970 -0.15% 8 coq-neural-net-interp-computed-lite/theories/MaxOfTwoNumbersSimpler/Computed/AllLogits.v.html │ │ 9.0040 8.7100 -0.2940 -3.27% 199 coq-mathcomp-odd-order/theories/PFsection12.v.html │ │ 21.3940 21.1060 -0.2880 -1.35% 40 coq-fiat-crypto-with-bedrock/src/PushButtonSynthesis/SolinasReductionReificationCache.v.html │ │ 26.6320 26.3570 -0.2750 -1.03% 35 coq-fiat-crypto-with-bedrock/src/Bedrock/End2End/X25519/MontgomeryLadderRISCV.v.html │ │ 22.2780 22.0150 -0.2630 -1.18% 12 coq-fourcolor/theories/proof/job384to398.v.html │ └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ |
I cherry-picked the change that keeps results from recursive I suppose I won't touch the |
The (bounded) discrimination nets used in TC search currently consider all
fun
terms to match any term. This is a performance issue for typeclasses that are keyed onfun
terms. One example is theFractional
class in iris which even in iris itself has 22 instances of the formFractional (fun q => ..)
. All of those instances will always match anyFractional
query. (I am ignoring the first parameter of the class which in practice has very little discriminating power).The reason for the current state of affairs is that the discrimination nets must over-approximate (tactic) unification. This includes eta equivalence of functions. The following cases need to be considered. I am using
x ~ y
to indicate thatx
matchesy
in the discrimination net.(fun x .. z => f x .. z) ~ f
whenf
isHint Opaque
(fun x .. z => f x .. z) ~ _
whenf
isHint Transparent
This is easily achieved by the current algorithm which simply translates all
fun
to_
.To strengthen the discriminating power, we would like refine these two conditions into the following way. We use
n
to designate the number of binders infun x .. z
and postfix↓
to indicate a term lifted by-n
. (We do not care that the negative lifting can introduce negativeRel
s but the algorithm could be changed to avoid that if necessary.)(fun x .. z => f _ .. _) ~ f
a) when
f↓ ~/~ _
,b)
f _ .. _
is an application off
to exactlyn
arguments of any shape.(fun x .. z => t1) ~ (fun x .. z => t2)
a) when neither
t1
nort2
match_
b) neither
t1↓
nort1↓
are applications withn
or more arguments.(fun x .. z => t) ~ _
a) when
t ~ _
.These rules were developed based on ideas by and in collaboration with @cipher1024. They aim to over-approximate eta in a way that should retain almost all available discriminating power of
fun
terms. We would ideally like to rely solely on eta contraction in patterns and constrs and use all non-contractiblefun
s for discrimination. However, standard eta contraction (as is used in the current algorithm) is too specific. It misses cases such asfun x => f (id x)
which, assumingid
isHint Transparent
, will unify with the termf
.Being more clever in the attempt to recognize true eta expansions (reducing arguments, actually calling unification, etc.) seems difficult and likely expensive performance-wise. So we instead opted to be much dumber about it and simply treat all terms of the form
fun x => f _
as potential eta expansions off
. This includes examples such asfun x y => f y x
(note the swapped order of arguments) andfun _ => @None A
(translated to just@None
). This is the basis of case 1.The flip side of this extremely broad over-approximation of eta is that we can be quite sure that
fun
terms that do not fit this pattern will never be eta equivalent to other terms. In particular, we can use thefun
itself as well as the type of its binder(s) for discrimination. This is case 2.However, we must be careful if the body of the
fun
is flexible in the sense that it can be unified with arbitrary terms. For example, the termfun _ => ?A
must not be treated as a discriminatingfun
because?A
could well end up beingf _
, yieldingfun _ => f _
which violates our "eta normal" form and would wrongly end up not matchingf
. This is case 3.TODO:
decomp_lambda_(constr|pattern)
is bad and the overall translation of afun
term has cubic runtime in the number of binders infun
terms. Some of that can be fixed but the fact that we must return only one node at a time makes it impossible to fully fix it. I don't think this is super important but I hate contributing bad code. 94ce659 has a solution to the issue of only generating one label at a time but @ppedrot did not like last time I introduced it. With more recursive work being performed now maybe the verdict would be different?Rel
s when we chop off binders that might still be used could be problematic but works fine in practice becauseRel
s are translated toNone
(for constrs) andNothing
(for patterns). An alternative suggested by @cipher1024 finds occurrences ofdead
variables and replaces terms that contain them with_
(or, rather, some term that translates to_
). This ends up re-implementing part of the translation so I would like to avoid it.The algorithm for constrs is very confusing because of the way that it uses(Out of scope.)Everything
andNothing
to introduce a distinction (almost) without difference. This PR adds to the confusion and interpretsNothing
differently from my previous change (which introducedCaseLabel
) in Refine approximation of matches in the bnet #18677, where I treat it as an unique label. In this change it is treated the same asEverything
, which I think is the correct interpretation.I thinkWe must go through the same eta-reduction approximation steps for syntactic patterns so that the resulting dnets matchdecomp_pat_discr_syntactic
(used forHint Extern
) does not have to respect eta (and can thus always discriminatefun
s) becauseConstr_matching
does not respect eta. It this correct?constr
s which are always eta-reduced.Fixes / closes #????
make doc_gram_rsts
.