-
Notifications
You must be signed in to change notification settings - Fork 84
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
Gallina quotation functions for Template #894
Conversation
e2d237c
to
fccb03f
Compare
5973cbc
to
6e37e8b
Compare
<details><summary>Timing</summary> <p> ``` Time | Peak Mem | File Name ------------------------------------------------------------------------------------------------------ 28m36.07s | 1742960 ko | Total Time / Peak Mem ------------------------------------------------------------------------------------------------------ 3m30.41s | 1335360 ko | ToTemplate/QuotationOf/Common/Kernames/KernameMap/Instances.vo 3m21.62s | 1701848 ko | ToTemplate/QuotationOf/Coq/FSets/FMapAVL/Sig.vo 1m50.32s | 1606248 ko | ToTemplate/QuotationOf/Coq/MSets/MSetAVL/Sig.vo 1m25.21s | 1283592 ko | ToTemplate/QuotationOf/Common/Universes/ConstraintSet/Instances.vo 1m21.88s | 1284000 ko | ToTemplate/QuotationOf/Common/Kernames/KernameSet/Instances.vo 1m19.88s | 1742960 ko | ToTemplate/Coq/MSets.vo 1m15.76s | 1282544 ko | ToTemplate/QuotationOf/Common/Universes/LevelSet/Instances.vo 1m12.04s | 1368088 ko | ToTemplate/QuotationOf/Coq/MSets/MSetList/Sig.vo 0m58.84s | 1057848 ko | ToTemplate/QuotationOf/Common/Universes/ConstraintSetOrdProp/Instances.vo 0m56.64s | 1034060 ko | ToTemplate/QuotationOf/Common/Universes/LevelExprSetOrdProp/Instances.vo 0m56.61s | 1004488 ko | ToTemplate/QuotationOf/Common/Kernames/KernameSetOrdProp/Instances.vo 0m53.50s | 1016636 ko | ToTemplate/QuotationOf/Common/Universes/LevelSetOrdProp/Instances.vo 0m51.59s | 1138188 ko | ToTemplate/QuotationOf/Common/Universes/LevelExprSet/Instances.vo 0m49.75s | 1379420 ko | ToTemplate/Coq/FSets.vo 0m49.74s | 1107368 ko | ToTemplate/QuotationOf/Coq/MSets/MSetProperties/Sig.vo 0m42.10s | 1256528 ko | ToTemplate/QuotationOf/Common/EnvironmentTyping/Sig.vo 0m37.84s | 1016312 ko | ToTemplate/QuotationOf/Coq/FSets/FMapList/Sig.vo 0m34.52s | 1013440 ko | ToTemplate/QuotationOf/Common/Environment/Sig.vo 0m22.88s | 852064 ko | ToTemplate/QuotationOf/Template/Ast/Env/Instances.vo 0m21.17s | 877048 ko | ToTemplate/QuotationOf/Coq/Structures/OrdersFacts/Sig.vo 0m19.04s | 1263092 ko | ToTemplate/Common/EnvironmentTyping.vo 0m18.07s | 841212 ko | ToTemplate/QuotationOf/Coq/MSets/MSetDecide/Sig.vo 0m17.21s | 834976 ko | ToTemplate/QuotationOf/Coq/Structures/OrdersAlt/Sig.vo 0m15.02s | 1075516 ko | ToTemplate/Template/Typing.vo 0m14.61s | 905324 ko | ToTemplate/Common/Universes.vo 0m13.40s | 816856 ko | ToTemplate/QuotationOf/Template/Typing/TemplateGlobalMaps/Instances.vo 0m13.34s | 780536 ko | ToTemplate/QuotationOf/Coq/MSets/MSetFacts/Sig.vo 0m12.93s | 774728 ko | ToTemplate/QuotationOf/Coq/FSets/FMapFacts/Sig.vo 0m11.78s | 862636 ko | ToTemplate/Common/Kernames.vo 0m10.00s | 828792 ko | ToTemplate/QuotationOf/Template/Typing/TemplateEnvTyping/Instances.vo 0m08.93s | 725948 ko | ToTemplate/QuotationOf/Common/Kernames/KernameMapFact/Instances.vo 0m07.23s | 728332 ko | ToTemplate/QuotationOf/Coq/MSets/MSetInterface/Sig.vo 0m06.99s | 705852 ko | ToTemplate/QuotationOf/Coq/Structures/Equalities/Sig.vo 0m05.96s | 725548 ko | ToTemplate/QuotationOf/Coq/FSets/FMapInterface/Sig.vo 0m05.11s | 992248 ko | ToTemplate/Template/TermEquality.vo 0m04.96s | 761740 ko | ToTemplate/QuotationOf/Template/Ast/TemplateLookup/Instances.vo 0m04.71s | 932436 ko | ToTemplate/Common/Environment.vo 0m04.53s | 989628 ko | ToTemplate/Template/WfAst.vo 0m03.78s | 769156 ko | ToTemplate/QuotationOf/Template/Typing/TemplateConversion/Instances.vo 0m03.18s | 713016 ko | ToTemplate/QuotationOf/Coq/Structures/OrdersTac/Sig.vo 0m02.95s | 701344 ko | ToTemplate/QuotationOf/Common/Universes/Level/Instances.vo 0m02.90s | 756624 ko | ToTemplate/QuotationOf/Template/Typing/TemplateDeclarationTyping/Instances.vo 0m02.86s | 703056 ko | ToTemplate/QuotationOf/Coq/Structures/Orders/Sig.vo 0m02.64s | 698816 ko | ToTemplate/QuotationOf/Common/Kernames/Kername/Instances.vo 0m02.53s | 922916 ko | ToTemplate/QuotationOf/Template/Ast/EnvHelper/Instances.vo 0m02.49s | 699500 ko | ToTemplate/QuotationOf/Common/Universes/LevelExpr/Instances.vo 0m02.12s | 984776 ko | ToTemplate/Template/Ast.vo 0m02.01s | 700412 ko | ToTemplate/Coq/Init.vo 0m01.99s | 701512 ko | ToTemplate/QuotationOf/Common/Universes/UnivConstraint/Instances.vo 0m01.98s | 704288 ko | ToTemplate/Utils/utils.vo 0m01.87s | 709068 ko | ToTemplate/QuotationOf/Template/Ast/TemplateTerm/Instances.vo 0m01.82s | 696724 ko | ToTemplate/Utils/MCUtils.vo 0m01.63s | 713200 ko | ToTemplate/QuotationOf/Template/ReflectAst/EnvDecide/Instances.vo 0m01.62s | 695876 ko | ToTemplate/Init.vo 0m01.53s | 696780 ko | ToTemplate/Coq/Lists.vo 0m01.49s | 699684 ko | ToTemplate/Utils/All_Forall.vo 0m01.43s | 695080 ko | ToTemplate/Utils/MCProd.vo 0m01.34s | 696508 ko | ToTemplate/Utils/ReflectEq.vo 0m01.27s | 697288 ko | ToTemplate/Utils/MCOption.vo 0m01.26s | 980920 ko | ToTemplate/Template/AstUtils.vo 0m01.23s | 693944 ko | ToTemplate/Coq/Bool.vo 0m01.22s | 755464 ko | ToTemplate/QuotationOf/Template/Typing/TemplateConversionPar/Instances.vo 0m01.20s | 757612 ko | ToTemplate/QuotationOf/Template/Typing/TemplateTyping/Instances.vo 0m01.19s | 710540 ko | ToTemplate/QuotationOf/Template/ReflectAst/TemplateTermDecide/Instances.vo 0m01.18s | 694060 ko | ToTemplate/Utils/MCList.vo 0m01.15s | 697976 ko | CommonUtils.vo 0m01.12s | 955240 ko | ToTemplate/QuotationOf/Template/Ast/Instances.vo 0m01.12s | 706104 ko | ToTemplate/QuotationOf/Template/Ast/TemplateTermUtils/Instances.vo 0m01.08s | 867856 ko | ToTemplate/Common/BasicAst.vo 0m01.07s | 695148 ko | ToTemplate/Utils/bytestring.vo 0m01.06s | 693992 ko | ToTemplate/Utils/MCArith.vo 0m01.03s | 703564 ko | ToTemplate/Coq/Floats.vo 0m00.98s | 700584 ko | ToTemplate/Coq/Numbers.vo 0m00.98s | 694564 ko | ToTemplate/Utils/MCReflect.vo 0m00.88s | 756228 ko | ToTemplate/QuotationOf/Template/Typing/Instances.vo 0m00.87s | 695256 ko | ToTemplate/Coq/Strings.vo 0m00.86s | 766292 ko | ToTemplate/QuotationOf/Common/Kernames/Instances.vo 0m00.84s | 696340 ko | ToTemplate/Coq/ssr.vo 0m00.83s | 694736 ko | ToTemplate/Common/Primitive.vo 0m00.83s | 698224 ko | ToTemplate/Common/config.vo 0m00.81s | 745564 ko | ToTemplate/QuotationOf/Common/Universes/Instances.vo 0m00.80s | 700668 ko | ToTemplate/QuotationOf/Template/ReflectAst/Instances.vo 0m00.20s | 63332 ko | ToTemplate/Utils/monad_utils.vo 0m00.08s | 64744 ko | ToTemplate/Utils/MCRelations.vo 0m00.06s | 64376 ko | ToTemplate/Utils/MCString.vo 0m00.05s | 64560 ko | ToTemplate/Common/Reflect.vo 0m00.05s | 63284 ko | ToTemplate/Template/ReflectAst.vo 0m00.05s | 65008 ko | ToTemplate/Utils/MCPrelude.vo 0m00.05s | 65068 ko | ToTemplate/Utils/MCSquash.vo 0m00.04s | 64248 ko | ToTemplate/Template/Induction.vo 0m00.04s | 64996 ko | ToTemplate/Template/LiftSubst.vo 0m00.04s | 64736 ko | ToTemplate/Template/UnivSubst.vo 0m00.04s | 64308 ko | ToTemplate/Utils/MCCompare.vo 0m00.04s | 63324 ko | ToTemplate/Utils/MCEquality.vo 0m00.04s | 65020 ko | ToTemplate/Utils/wGraph.vo 0m00.03s | 64724 ko | ToTemplate/Utils/ByteCompare.vo 0m00.03s | 63432 ko | ToTemplate/Utils/ByteCompareSpec.vo 0m00.03s | 64856 ko | ToTemplate/Utils/LibHypsNaming.vo 0m00.03s | 64768 ko | ToTemplate/Utils/MCPred.vo 0m00.02s | 65064 ko | ToTemplate/Common/Transform.vo 0m00.01s | 64484 ko | ToTemplate/Utils/ByteCompare_opt.vo ``` </p> </details>
Unclear why recent changes resulted in swapping the order of `quote_prod` and `destruct` result in universe inconsistencies...
We cannot unset universe checking in 8.16 due to COQBUG(coq/coq#17361), and quick does not buy much in quotation anyway, where almost everything is transparent
Not currently needed, but will be needed for TypingWf compatiblity
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JasonGross it seems it is ready to merge now. Is it right ?
Yes, indeed, this is ready for merge, assuming there's no objection to the 30 minutes added to build time nor to me adding myself to the authors lists in the .opam files. And thank you for reviewing and merging all my PRs! |
Have you found any obvious bottleneck in compilation time that we can try to understand and fix, or is it more uniformly slow compilation time ? |
There are some obvious bottlenecks that unfortunately live inside TemplateMonad code that I'm not sure how to profile. (The secondary bottlenecks are functor instantiation lines, where I do More than 2/3 of the time is spent in the TemplateMonad code for taking a module name, getting the list of identifiers under that module name, and emitting, effectively, The files taking more than 50s are
I can probably speed up The other files are all of roughly the same shape, though. Consider the second slowest, From Coq.FSets Require Import FMapAVL.
From Coq.Structures Require Import Equalities OrdersAlt.
From MetaCoq.Quotation.ToTemplate Require Import Init.
From MetaCoq.Quotation.ToTemplate.QuotationOf.Coq.Structures Require Import OrdersAlt.Sig.
From MetaCoq.Quotation.ToTemplate.QuotationOf.Coq.FSets Require Import FMapList.Sig.
Import List.ListNotations.
Local Open Scope list_scope.
Module FMapAVL.
Module Type MakeSig (T : OrderedTypeOrig) := Nop <+ FMapAVL.Make T.
Module Type QuotationOfMake (T : OrderedTypeOrig) (M : MakeSig T).
Module qRaw.
Module qProofs.
Module qMX := Nop <+ QuotationOfOrderedTypeOrigFacts T M.Raw.Proofs.MX.
Module qPX := Nop <+ QuotationOfKeyOrderedTypeOrig T M.Raw.Proofs.PX.
Module qL := Nop <+ FMapList.QuotationOfRaw T M.Raw.Proofs.L.
Export (hints) qMX qPX qL.
MetaCoq Run (tmDeclareQuotationOfModule (all_submodules_except [["MX"]; ["PX"]; ["L"]]%bs) (Some export) "M.Raw.Proofs").
End qProofs.
Export (hints) qProofs.
MetaCoq Run (tmDeclareQuotationOfModule (all_submodules_except [["Proofs"]]%bs) (Some export) "M.Raw").
End qRaw.
Export (hints) qRaw.
MetaCoq Run (tmDeclareQuotationOfModule (all_submodules_except [["Raw"]]%bs) (Some export) "M").
End QuotationOfMake.
End FMapAVL. The template program
code for checking if a kername is globalizable(* returns true if a modpath is suitable for quotation, i.e., does not mention functor-bound arguments *)
Fixpoint modpath_is_absolute (mp : modpath) : bool
:= match mp with
| MPfile _ => true
| MPbound _ _ _ => false
| MPdot mp _ => modpath_is_absolute mp
end.
Definition kername_is_absolute (kn : kername) : bool
:= modpath_is_absolute (fst kn).
(* gives the dirpath and the reversed list of idents, or None if bound *)
Fixpoint split_common_prefix_ls (mp1 mp2 : list ident) :=
match mp1, mp2 with
| [], _ | _, [] => ([], mp1, mp2)
| i1 :: mp1, i2 :: mp2
=> if i1 == i2
then let '(common, mp1, mp2) := split_common_prefix_ls mp1 mp2 in
(i1 :: common, mp1, mp2)
else ([], mp1, mp2)
end.
Definition common_prefix_ls (mp1 mp2 : list ident) :=
let '(common, _, _) := split_common_prefix_ls mp1 mp2 in common.
Fixpoint split_modpath (mp : modpath) : (list ident * (dirpath * option (ident * nat)))
:= match mp with
| MPfile f => ([], (f, None))
| MPbound f i idx => ([], (f, Some (i, idx)))
| MPdot mp i => let '(l, d) := split_modpath mp in (i :: l, d)
end.
(* returns None if either [mp] shares no prefix with [mp] or either modpath is bound, otherwise returns the list of idents of the common prefix *)
Definition split_common_prefix (mp1 mp2 : modpath) : option ((dirpath * option (ident * nat)) * (list ident * list ident * list ident))
:= match split_modpath mp1, split_modpath mp2 with
| (mp1, f1), (mp2, f2)
=> if f1 == f2
then Some (f1, split_common_prefix_ls (rev mp1) (rev mp2))
else None
end.
Definition common_prefix (mp1 mp2 : modpath) : option ((dirpath * option (ident * nat)) * list ident)
:= option_map (fun '(f, (common, _, _)) => (f, common)) (split_common_prefix mp1 mp2).
(* Kludge for not having https://github.com/MetaCoq/metacoq/issues/839 *)
Definition modpath_is_okay (cur_modpath : modpath) (mp : modpath) : bool
:= andb (modpath_is_absolute mp)
match mp with
| MPfile _ => true
| MPbound _ _ _ => false
| MPdot _ _
=> match common_prefix cur_modpath mp with
| None => true (* it's not part of the current module, so it's fine *)
| Some (_, []) => true (* only share the top-level, so it can't be a functor *)
| Some _ => false
end
end.
Definition kername_is_okay (cur_modpath : modpath) (kn : kername) : bool
:= modpath_is_okay cur_modpath (fst kn). |
(Also, do @mattam82 @yforster @TheoWinterhalter @MevenBertrand want to weigh in?) |
I don't have anything to contribute in profiling I fear. Regarding the content of the PR, I'm very much in favour. We've been discussing a proof like that for a while and I've been scared that it might be very hard. This PR shows it's indeed very hard, but doable :) On the other hand, the almost doubled build time is a bit of a problem. We spent a lot of energy on getting MetaCoq's build time down. In the end, it wouldn't be a problem for users installing MetaCoq, because it's in its own package (which is a bit oddly named btw, if I'd just read |
I was scared for a minute that it would slow down install for users or compile time for developers of other bits, but if not then I guess it's not so terrible. |
Ok, changing the CI so that it does run on every push is something we should do anyway. |
I don't have a lot more to add, I think. I'm very happy too that someone took up the challenge of doing this quotation work, well done Jason! And if the slowdown is confined to CI that we run less aggressively than we currently do, but does not affect users or developers that don't touch this new addition it seems like a reasonable price to pay to me – that me can try and mitigate later. |
No, the recent refactorings are preparatory and do not conflict with this PR. I'll make some follow-up PRs for improving performance, etc. |
Thank you for merging! 🎉
It's also possible to make pushes cancel previous instances of the CI that are still running by adding something like
(remove |
This is currently on top of many other PRs, and hence is a draft PR. But other than that it's basically ready for review.It's unfortunately kind-of slow(timing info coming soon).Timing
This is the easier half of establishing the quotation function (required, e.g., for proving Löb's theorem). The ultimate goal is, where
x : □X
means roughlyΣ ;;; [] |- x : X
, to prove□(□X → X) → □X
. The quotation function should ultimately be□X → □□X
, and this PR establishes the ability to go fromΣ ;;; [] |- x : X
toAst.term
(in Template land; we could probably do the same in PCUIC land without much difficulty).There's one additional wart, which is that the universe constraints introduced here are inconsistent with those in
Template.TypingWf
. This also prevents us from writing a quotation function forwf_inductive_body
. Working out this kink might require #893, which is still a work in progress, so I figure I'll try to get this merged first, while I try to work out that kink on the side.Currently blocked on coq/coq#17462, but once I finish minimizing that I expect it should be pretty easy to fix this to work with-vos
.