Skip to content
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

Add rewrite rules to Coq #18038

Merged
merged 31 commits into from Feb 21, 2024
Merged

Add rewrite rules to Coq #18038

merged 31 commits into from Feb 21, 2024

Conversation

yannl35133
Copy link
Contributor

@yannl35133 yannl35133 commented Sep 12, 2023

NB: last bench #18038 (comment)

This adds support for rewrite rules (Coq roadmap), (associated CEP).

What is currently missing :

  • A toggle for enabling rewrite rules (which makes it explicit that support is experimental and extremely unsafe)
  • Documentation (needs proof-reading)
  • More comprehensive tests
  • Changelog entry instead of README paragraph

@coqbot-app coqbot-app bot added the needs: full CI The latest GitLab pipeline that ran was a light CI. Say "@coqbot run full ci" to get a full CI. label Sep 12, 2023
clib/cList.ml Outdated Show resolved Hide resolved
@yannl35133 yannl35133 force-pushed the rewrite-rules branch 4 times, most recently from a0bfcb1 to e64bbb5 Compare September 14, 2023 20:47
@gares
Copy link
Member

gares commented Sep 16, 2023

I think we could discuss at a call a strategy to review and merge this one. It is large and could be split into smaller pieces.

I know it is some work but it would also mean we can integrate it step by step (and finding what a good step is, is part of the job to be done). Maybe what I'd like to see is a more detailed technical roadmap, at the level of detail of a stream of PRs.

@yannl35133 yannl35133 force-pushed the rewrite-rules branch 3 times, most recently from 4cca8a7 to b7af997 Compare September 18, 2023 16:35
@yannl35133
Copy link
Contributor Author

The commits are now organised thematically so that they can be read individually

@github-actions github-actions bot added the needs: rebase Should be rebased on the latest master to solve conflicts or have a newer CI run. label Sep 19, 2023
@coqbot-app coqbot-app bot removed the needs: rebase Should be rebased on the latest master to solve conflicts or have a newer CI run. label Sep 19, 2023
@yannl35133 yannl35133 force-pushed the rewrite-rules branch 2 times, most recently from f09b1da to 83d8688 Compare September 22, 2023 14:39
@SkySkimmer
Copy link
Contributor

@coqbot bench

@coqbot-app
Copy link
Contributor

coqbot-app bot commented Sep 27, 2023

🏁 Bench results:

┌──────────────────────────────┬─────────────────────────┬───────────────────────────────────────┬───────────────────────────────────────┬──────────────────────────┐
│                              │      user time [s]      │              CPU cycles               │           CPU instructions            │  max resident mem [KB]   │
│                              │                         │                                       │                                       │                          │
│         package_name         │   NEW      OLD    PDIFF │      NEW             OLD        PDIFF │      NEW             OLD        PDIFF │   NEW      OLD    PDIFF  │
├──────────────────────────────┼─────────────────────────┼───────────────────────────────────────┼───────────────────────────────────────┼──────────────────────────┤
│                coq-equations │   11.01    11.25  -2.13 │    48369690568     48565908449  -0.40 │    76401011720     76292114169   0.14 │  417164   416784    0.09 │
│                 coq-compcert │  284.13   287.87  -1.30 │  1287871343688   1303465170337  -1.20 │  1968203084721   1961508241448   0.34 │ 1143652  1142064    0.14 │
│                   coq-stdlib │  383.54   386.56  -0.78 │  1648048077712   1664002950372  -0.96 │  1408416245860   1405558082518   0.20 │  760008   750880    1.22 │
│          coq-category-theory │  752.11   756.57  -0.59 │  3434368289663   3453350175361  -0.55 │  5881700307031   5868997800359   0.22 │  949560   883732    7.45 │
│                 coq-coqprime │   47.73    48.00  -0.56 │   215990623513    216577206967  -0.27 │   330399802715    328483954319   0.58 │  781084   778932    0.28 │
│             coq-fiat-parsers │  331.15   332.50  -0.41 │  1489595331317   1494681853963  -0.34 │  2471380564229   2464107846088   0.30 │ 2424316  2424300    0.00 │
│                    coq-verdi │   49.50    49.63  -0.26 │   224431578474    224769111491  -0.15 │   341004667397    339962221963   0.31 │  530440   527668    0.53 │
│ coq-fiat-crypto-with-bedrock │ 6426.54  6443.07  -0.26 │ 29208991925564  29283015649049  -0.25 │ 53544257029244  53391556417435   0.29 │ 2310552  2422624   -4.63 │
│                    coq-color │  235.34   235.76  -0.18 │  1064390406159   1067795307949  -0.32 │  1547003705501   1543997584784   0.19 │ 1140748  1127820    1.15 │
│       coq-mathcomp-ssreflect │  189.32   189.43  -0.06 │   864808449452    865966248690  -0.13 │  1479716122418   1478619456294   0.07 │ 1287272  1286988    0.02 │
│  coq-rewriter-perf-SuperFast │  736.94   737.18  -0.03 │  3344887405866   3349494879820  -0.14 │  5705578206513   5684807280078   0.37 │ 1327072  1416844   -6.34 │
│                 coq-rewriter │  355.10   355.19  -0.03 │  1615920434490   1617913867227  -0.12 │  2668881352357   2662404601309   0.24 │ 1224972  1301748   -5.90 │
│                  coq-bignums │   28.90    28.90   0.00 │   132288595791    131851579423   0.33 │   190538045715    188370765613   1.15 │  490740   487988    0.56 │
│               coq-verdi-raft │  587.60   587.39   0.04 │  2681505215889   2679715120601   0.07 │  4173020731261   4142521196519   0.74 │  817196   815256    0.24 │
│                      coq-vst │  855.57   855.24   0.04 │  3891496551759   3888238162165   0.08 │  6414235447591   6361664840614   0.83 │ 2148328  1903180   12.88 │
│             coq-math-classes │   87.95    87.87   0.09 │   398069612078    398306879226  -0.06 │   559606198100    552711394268   1.25 │  522940   523460   -0.10 │
│                     coq-hott │  154.23   154.01   0.14 │   697981628939    697660854703   0.05 │  1106332386826   1102857473131   0.32 │  590672   519600   13.68 │
│                coq-perennial │ 6459.07  6448.63   0.16 │ 29457313764352  29406816277182   0.17 │ 48979811022783  48763607375260   0.44 │ 2206544  2221544   -0.68 │
│                 coq-bedrock2 │  356.40   355.33   0.30 │  1629136853800   1624256596324   0.30 │  3178382584595   3157411336101   0.66 │  915144   865412    5.75 │
│                  coq-unimath │ 1639.16  1634.17   0.31 │  7470912306850   7451588395656   0.26 │ 14031848710832  13888054552005   1.04 │ 1335224  1298640    2.82 │
│        coq-mathcomp-solvable │  146.30   145.79   0.35 │   667301676224    665008080125   0.34 │  1067733512647   1056533530559   1.06 │  919316   909152    1.12 │
│   coq-performance-tests-lite │  725.57   722.84   0.38 │  3287095666737   3276035158903   0.34 │  5788413528814   5772953989095   0.27 │ 1691076  1690672    0.02 │
│       coq-mathcomp-character │  136.75   136.23   0.38 │   623433824750    621019243560   0.39 │  1007048746474   1000245590402   0.68 │ 1217180  1231916   -1.20 │
│                     coq-corn │  803.45   800.26   0.40 │  3660083272343   3645360029574   0.40 │  5717105508235   5632877251768   1.50 │  832032   777316    7.04 │
│            coq-iris-examples │  491.59   488.92   0.55 │  2233883939076   2221716389682   0.55 │  3393353452452   3373217178028   0.60 │ 1082496  1080692    0.17 │
│                     coq-core │  115.77   115.06   0.62 │   487501092322    484638351277   0.59 │   495941982056    491784613420   0.85 │  373924   368492    1.47 │
│        coq-engine-bench-lite │  161.68   160.53   0.72 │   693934950512    689902827285   0.58 │  1282729781528   1287660851293  -0.38 │ 1082304  1277128  -15.25 │
│       coq-mathcomp-odd-order │  885.26   878.84   0.73 │  4048222607533   4018874684576   0.73 │  6806648090484   6750689441284   0.83 │ 1629100  1631844   -0.17 │
│                coq-fiat-core │   60.59    60.14   0.75 │   260050560638    258869924242   0.46 │   382374946502    379673527705   0.71 │  492516   492940   -0.09 │
│      coq-metacoq-safechecker │  379.45   376.06   0.90 │  1734543365664   1719972207984   0.85 │  2880226872042   2872282845443   0.28 │ 1983476  1965704    0.90 │
│           coq-mathcomp-field │  220.04   218.03   0.92 │  1002072240920    993032934242   0.91 │  1753658740994   1743798366825   0.57 │ 1505616  1504776    0.06 │
│            coq-metacoq-pcuic │  626.71   620.46   1.01 │  2842668319577   2817203966608   0.90 │  4151201568445   4131555324987   0.48 │ 1990328  1990484   -0.01 │
│                coq-fourcolor │ 1530.46  1513.69   1.11 │  6975203773866   6909662302616   0.95 │ 12483633903418  12470289817637   0.11 │ 2293112  2060632   11.28 │
│                  coq-coqutil │   41.05    40.53   1.28 │   183117170021    182009935703   0.61 │   265424300648    263328999600   0.80 │  561656   561364    0.05 │
│        coq-mathcomp-fingroup │   40.05    39.51   1.37 │   182224905165    180125929699   1.17 │   267714089684    264996928308   1.03 │  560396   560412   -0.00 │
│               coq-coquelicot │   39.16    38.38   2.03 │   174499043889    171428041089   1.79 │   242545736486    238171522464   1.84 │  792232   806236   -1.74 │
│         coq-mathcomp-algebra │  485.91   474.36   2.43 │  2214361819381   2160972822552   2.47 │  3965158719260   3961919945635   0.08 │ 1416368  1403264    0.93 │
└──────────────────────────────┴─────────────────────────┴───────────────────────────────────────┴───────────────────────────────────────┴──────────────────────────┘

INFO: failed to install
coq-metacoq-template

coq-metacoq-erasure (dependency coq-metacoq-template failed)
coq-metacoq-translations (dependency coq-metacoq-template failed)

🐢 Top 25 slow downs
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                                 TOP 25 SLOW DOWNS                                                                 │
│                                                                                                                                                   │
│   OLD       NEW      DIFF   %DIFF    Ln                      FILE                                                                                 │
├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ 143.3660  145.9360  2.5700   1.79%  1190  coq-unimath/UniMath/CategoryTheory/GrothendieckConstruction/IsPullback.v.html                           │
│  19.7980   21.9770  2.1790  11.01%   413  coq-perennial/src/program_proof/vrsm/replica/setstate_proof.v.html                                      │
│  55.0730   56.9480  1.8750   3.40%   609  coq-fiat-crypto-with-bedrock/rupicola/bedrock2/bedrock2/src/bedrock2Examples/lightbulb.v.html           │
│ 161.7940  163.6660  1.8720   1.16%   233  coq-fiat-crypto-with-bedrock/rupicola/bedrock2/deps/riscv-coq/src/riscv/Proofs/DecodeByExtension.v.html │
│  54.9540   56.2540  1.3000   2.37%   609  coq-bedrock2/bedrock2/src/bedrock2Examples/lightbulb.v.html                                             │
│  53.7380   54.9310  1.1930   2.22%   915  coq-fiat-crypto-with-bedrock/src/Bedrock/End2End/X25519/GarageDoor.v.html                               │
│   8.6800    9.5970  0.9170  10.56%   192  coq-vst/veric/binop_lemmas5.v.html                                                                      │
│  16.3760   17.2270  0.8510   5.20%   642  coq-perennial/src/program_proof/vrsm/paxos/tryacquire_proof.v.html                                      │
│  11.5020   12.3360  0.8340   7.25%   462  coq-perennial/src/program_proof/simple/write.v.html                                                     │
│  36.0890   36.8990  0.8100   2.24%    12  coq-fourcolor/theories/job439to465.v.html                                                               │
│  28.8270   29.5990  0.7720   2.68%    12  coq-fourcolor/theories/job531to534.v.html                                                               │
│   8.6520    9.4230  0.7710   8.91%  1508  coq-perennial/src/program_proof/wal/recovery_proof.v.html                                               │
│  17.5260   18.2840  0.7580   4.33%  3269  coq-fiat-crypto-with-bedrock/src/Assembly/WithBedrock/Proofs.v.html                                     │
│   1.8770    2.6200  0.7430  39.58%   209  coq-stdlib/setoid_ring/Ncring_tac.v.html                                                                │
│  27.3170   28.0110  0.6940   2.54%    12  coq-fourcolor/theories/job291to294.v.html                                                               │
│  32.0620   32.7380  0.6760   2.11%    12  coq-fourcolor/theories/job589to610.v.html                                                               │
│  27.2960   27.9000  0.6040   2.21%    12  coq-fourcolor/theories/job618to622.v.html                                                               │
│  25.3500   25.9350  0.5850   2.31%    12  coq-fourcolor/theories/job495to498.v.html                                                               │
│ 111.4550  112.0330  0.5780   0.52%   999  coq-performance-tests-lite/src/fiat_crypto_via_setoid_rewrite_standalone.v.html                         │
│  26.5810   27.1500  0.5690   2.14%    12  coq-fourcolor/theories/job503to506.v.html                                                               │
│  20.1140   20.6760  0.5620   2.79%    12  coq-fourcolor/theories/job315to318.v.html                                                               │
│  28.3470   28.8870  0.5400   1.90%    12  coq-fourcolor/theories/job466to485.v.html                                                               │
│  28.7300   29.2660  0.5360   1.87%   147  coq-vst/veric/expr_lemmas4.v.html                                                                       │
│  19.1520   19.6800  0.5280   2.76%    12  coq-fourcolor/theories/job623to633.v.html                                                               │
│  27.9040   28.4060  0.5020   1.80%   194  coq-vst/veric/expr_lemmas4.v.html                                                                       │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
🐇 Top 25 speed ups
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                           TOP 25 SPEED UPS                                                           │
│                                                                                                                                      │
│   OLD       NEW      DIFF    %DIFF    Ln                    FILE                                                                     │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│  45.2810   41.7100  -3.5710  -7.89%   236  coq-rewriter-perf-SuperFast/src/Rewriter/Rewriter/Examples/PerfTesting/LiftLetsMap.v.html │
│  42.5700   41.0690  -1.5010  -3.53%   224  coq-performance-tests-lite/PerformanceExperiments/rewrite_lift_lets_map.v.html            │
│  23.5140   22.0180  -1.4960  -6.36%   660  coq-perennial/src/program_proof/vrsm/replica/roapply_proof.v.html                         │
│  61.4880   60.2880  -1.2000  -1.95%    27  coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/ToFancyWithCasts.v.html                  │
│  25.5100   24.3690  -1.1410  -4.47%    25  coq-fiat-crypto-with-bedrock/src/Spec/Test/X25519.v.html                                  │
│  42.8100   41.6720  -1.1380  -2.66%   236  coq-rewriter/src/Rewriter/Rewriter/Examples/PerfTesting/LiftLetsMap.v.html                │
│  25.4880   24.3560  -1.1320  -4.44%    20  coq-fiat-crypto-with-bedrock/src/Spec/Test/X25519.v.html                                  │
│  34.7260   33.6750  -1.0510  -3.03%   522  coq-perennial/src/program_proof/txn/twophase_refinement_proof.v.html                      │
│ 127.6270  126.6000  -1.0270  -0.80%    22  coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/ArithWithCasts.v.html                    │
│  23.0700   22.1780  -0.8920  -3.87%   233  coq-fiat-crypto-with-bedrock/src/Curves/EdwardsMontgomery.v.html                          │
│  19.9110   19.0700  -0.8410  -4.22%    28  coq-fiat-crypto-with-bedrock/src/Spec/Test/X25519.v.html                                  │
│  43.2890   42.4500  -0.8390  -1.94%   368  coq-mathcomp-odd-order/theories/PFsection4.v.html                                         │
│ 212.8040  211.9720  -0.8320  -0.39%   141  coq-fiat-crypto-with-bedrock/src/UnsaturatedSolinasHeuristics/Tests.v.html                │
│   8.9120    8.1790  -0.7330  -8.22%  1510  coq-perennial/src/program_proof/vrsm/configservice/config_proof.v.html                    │
│ 113.9190  113.2540  -0.6650  -0.58%    48  coq-fiat-crypto-with-bedrock/src/Curves/Weierstrass/AffineProofs.v.html                   │
│  82.9430   82.3390  -0.6040  -0.73%    20  coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/NBE.v.html                               │
│  13.2950   12.6910  -0.6040  -4.54%   187  coq-perennial/src/goose_lang/interpreter/disk_interpreter.v.html                          │
│  52.7730   52.1800  -0.5930  -1.12%    50  coq-fiat-crypto-with-bedrock/src/Bedrock/Field/Synthesis/Examples/p224_64_new.v.html      │
│  13.2620   12.6990  -0.5630  -4.25%    15  coq-fiat-crypto-with-bedrock/src/Spec/Test/X25519.v.html                                  │
│  11.4080   10.8760  -0.5320  -4.66%   601  coq-perennial/src/program_proof/grove_shared/urpc_proof.v.html                            │
│  14.5320   14.0000  -0.5320  -3.66%   194  coq-fiat-crypto-with-bedrock/src/Fancy/Barrett256.v.html                                  │
│  28.8530   28.3230  -0.5300  -1.84%   129  coq-fiat-crypto-with-bedrock/src/Curves/Weierstrass/Projective.v.html                     │
│  52.1290   51.6110  -0.5180  -0.99%    50  coq-fiat-crypto-with-bedrock/src/Bedrock/Field/Synthesis/Examples/p224_64_new.v.html      │
│  16.0770   15.5780  -0.4990  -3.10%   262  coq-fiat-crypto-with-bedrock/src/Curves/EdwardsMontgomery.v.html                          │
│  21.6340   21.1350  -0.4990  -2.31%    49  coq-fiat-crypto-with-bedrock/src/Curves/Weierstrass/AffineProofs.v.html                   │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

@yannl35133
Copy link
Contributor Author

The last two commits are somewhat experimental :

  • the first one adds a flag to prevent pattern variables from being unified as evars. It is quite invasive as a flag, and I wonder if there is a simpler way to do this
  • the second one outright changes two behaviours of Coq at elaboration, but I don't understand why the existing behaviour was as it was :
    • pattern variables would now be named as evars (previously their given names was only stored as part of the location field)
    • evar instances bodies and types are now unified against the expected ones, instead of only being checked for conversion

Please tell me if that broke anything, or if you have suggestions

@SkySkimmer
Copy link
Contributor

unfold_fix produces non termination even without any rewrite rules:

Set Allow Rewrite Rules.

#[unfold_fix] Symbol raise : forall P: Type, P.

Definition bad : False := @Wf.Fix_F unit (fun _ _ => True) (fun _ => False) (fun x f => f x I) tt (raise _).

Eval lazy in bad.

Note that this relies on Fix_F being defined as a fixpoint which does not start with a match on the structural argument, ie something that can't be done with recursors.

@yannl35133
Copy link
Contributor Author

unfold_fix produces non termination even without any rewrite rules

This behaviour is now removed, with unfold_fix symbols unfolding a fixpoint only in an app context / only when the eliminations are all applications

@yannl35133
Copy link
Contributor Author

I cannot see a way for simpl to be able to reduce on rules with a match without either slowing it down even when no symbols are involved (i.e. I look for symbols at each step) or changing its general behaviour on match

@SkySkimmer
Copy link
Contributor

Did I make a mistake in my rewrite rule or is it a bug that

Set Allow Rewrite Rules.

#[unfold_fix] Symbol raise : forall P: Type, P.

Rewrite Rules raise_rew :=
  match raise (@Acc ?A ?R ?x) as a return ?P with
  | Acc_intro _ _ => _
  end ==> raise (?P@{a := raise (@Acc ?A ?R ?x)}).

Definition bad : False := @Wf.Fix_F unit (fun _ _ => True) (fun _ => False) (fun x f => f x I) tt (raise _).

Definition bad' := Eval lazy in bad.

produces Error: The term "raise (Acc (fun _ _ : unit => True) tt)" has type "Acc (fun _ _ : unit => True) tt" which should be Set, Prop or Type.?
ie Eval lazy produces an ill typed term which contains forall y : raise (Acc (fun _ _ : unit => True) tt), ... as a subterm.

@yannl35133
Copy link
Contributor Author

yannl35133 commented Oct 12, 2023

The bug is fixed, but now I see that the types for lambdas to abstract the extended contexts may be wrong in instances where substitutions happen at the same time as the rewriting. They are immediately erased by beta reduction in most cases, but if a user wants to enable rewriting but not beta it will show.

Set Allow Rewrite Rules.
Symbol raise : forall P: Type, P.
Rewrite Rule raise_rew :=
  match raise (@eq ?A ?a ?b) as e in _ = b return ?P with
  | eq_refl => _
  end ==> raise ?P@{b := _; e := raise (?a = ?b)}.

Definition bad :=
  Eval lazy delta in
  let n := 0 in
  match raise (n = n) in (_ = a) return (n = a) with
  | eq_refl => raise _
  end.

(that's let n := 0 in eq_trans n n n (raise _) (raise _) after successively delta and beta)
I am afraid that computing the correct types would be slow, or would need a significant rewrite of the machine, similar in size as the substitution of evars with extended context the hack replaces

@SkySkimmer
Copy link
Contributor

Is this expected?

Set Allow Rewrite Rules.

#[unfold_fix] Symbol raise : forall P: Type, P.

Rewrite Rules raise_rew :=
  raise (forall x:?A, ?B) ==> fun x:?A => raise ?B
.

Eval lazy in raise (forall x:nat, x = 0). (* works: fun x => raise (x = 0) *)

Eval lazy in raise (forall x y:nat, x = y). (* does nothing *)

Lemma gen A B : raise (forall x:A, B x) = fun x => raise (B x).
Proof. lazy. reflexivity. Defined.

Lemma hide T : T -> T. Proof. auto. Qed.

Lemma specific : raise (forall x y:nat, x = y) = fun x => raise (forall y, x = y).
Proof.
  apply hide,gen.
Defined.

Definition specific' := Eval lazy in specific.
(* type error (SR broken) *)

@yannl35133
Copy link
Contributor Author

Is this expected?

No, it is now fixed

@SkySkimmer
Copy link
Contributor

Now we have non termination with

Set Allow Rewrite Rules.

#[unfold_fix] Symbol raise : forall P: Type, P.

Rewrite Rules raise_rew :=
  raise (forall x:?A, ?B) ==> fun x:?A => raise ?B
with
  match raise (@Acc ?A ?R ?x) as a return ?P with
  | Acc_intro _ _ => _
  end ==> raise (?P@{a := raise (@Acc ?A ?R ?x)}).

Definition bad : False := @Wf.Fix_F unit (fun _ _ => True) (fun _ => False) (fun x f => f x I) tt (raise _).

Eval lazy in bad.

I don't know if this one can be fixed.

(meanwhile

Definition Fix_F' {A} {R:A->A->Prop} P
  (F:forall x : A, (forall y : A, R y x -> P y) -> P x)
  : forall x, Acc R x -> P x
  := fix Fix_F x a {struct a} :=
    match a with
    | Acc_intro _ b => F x (fun y h => Fix_F y (b y h))
    end.

Definition good : False := @Fix_F' unit (fun _ _ => True) (fun _ => False) (fun x f => f x I) tt (raise _).

Eval lazy in good.
(* raise False *)

does the right thing)

@github-actions github-actions bot added the needs: rebase Should be rebased on the latest master to solve conflicts or have a newer CI run. label Oct 20, 2023
@Alizter
Copy link
Contributor

Alizter commented Feb 20, 2024

FTR am I correct in thinking this doesn't support rewriting arbitrary terms but only symbols? For example, something I wanted to try was:

Inductive paths {A} (x : A) : A -> Type :=
| idpath : paths x x.

Axiom Circle : Set.
Symbol base : Circle.
Symbol loop : paths base base.
Symbol Circle_rec : forall (P : Type), P -> (paths base base) -> Circle -> P.

Definition ap {A B} (f : A -> B) {x y} (p : paths x y) : paths (f x) (f y) :=
  match p in paths _ y return paths (f x) (f y) with
  | idpath _ => idpath (f x)
  end.

Rewrite Rule loop_rew :=
| Circle_rec _ ?b ?loop base ==> ?b
| ap (Circle_rec _ ?b ?loop) loop ==> ?loop
.

but that doesn't seem to work. IIRC Agda is able to accept that definition, so is it something that might be possible with the right backdoor?

@TheoWinterhalter
Copy link
Contributor

No, you can only add rewrite rules to symbols. Otherwise, reduction would trigger unfolding before it triggers the rule. There might some workarounds but I don't think anything has been implemented. If you want the rule for ap then I think you probably should define it with a symbol and rewrite rules.

@yannl35133
Copy link
Contributor Author

You can also define your rewrite rule using the normal form for the left-hand side, it can be made into a pattern

| match loop with idpath _ => idpath (Circle_rec ?B ?b ?loop base) end ==> ?loop

@SkySkimmer
Copy link
Contributor

@coqbot run full ci

@coqbot-app coqbot-app bot removed the needs: full CI The latest GitLab pipeline that ran was a light CI. Say "@coqbot run full ci" to get a full CI. label Feb 20, 2024
@SkySkimmer
Copy link
Contributor

I'll probably merge tomorrow.

@Alizter
Copy link
Contributor

Alizter commented Feb 20, 2024

@yannl35133 With that pattern Coq complains about an unknown existential variable. Is that expected?

@yannl35133
Copy link
Contributor Author

This is indeed not expected. I found the reason and I'll see how I can fix it

@SkySkimmer
Copy link
Contributor

Do you want to wait for the fix to merge or do you prefer doing it after merge?

@SkySkimmer
Copy link
Contributor

offline: don't care

@coqbot merge now

@coqbot-app coqbot-app bot merged commit deaf849 into coq:master Feb 21, 2024
6 of 8 checks passed
Copy link
Contributor

coqbot-app bot commented Feb 21, 2024

@SkySkimmer: Please take care of the following overlays:

  • 18038-Yann-Leray-rewrite-rules.sh

yannl35133 added a commit to MetaCoq/metacoq that referenced this pull request Feb 21, 2024
LasseBlaauwbroek added a commit to coq-tactician/coq-tactician that referenced this pull request Feb 21, 2024
lukaszcz pushed a commit to lukaszcz/coqhammer that referenced this pull request Feb 22, 2024
Janno added a commit to Mtac2/Mtac2 that referenced this pull request Feb 22, 2024
herbelin added a commit to herbelin/template-coq that referenced this pull request Feb 25, 2024
@Alizter
Copy link
Contributor

Alizter commented Mar 4, 2024

@yannl35133 Should I create an issue about the unexpected error?

@yannl35133
Copy link
Contributor Author

@Alizter Sorry for the delay, I opened the issue and a PR to fix half of the issue, the other half will need more work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: feature New user-facing feature request or implementation. part: kernel
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants