Skip to content

Conversation

@gares
Copy link
Member

@gares gares commented Feb 12, 2021

We now run type inference but we first turn all id_phant into _. The problem is that if you don't do that the term does not type check, e.g. the factory requires are not postulated (yet). Similarly in structures, before we were ignoring (no type inference for the parameters) parameters and then we were postulating all requires.

IMO this PR makes things a bit more predictable (and similar to the usual way Coq works), but this is a sharp weapon since inference goes left to right, so you should put the most demanding factory (on parameters) first (in a structure declaration).

There is also another subtle problem with indexed that stays there in some terms and type inference can put it in the type of a parameter breaking things (see the commit in the test suite).

Fix #134
Fix #133

@gares gares mentioned this pull request Feb 12, 2021
We elaborate the last item by purging id_phant so that the term can
type check even if the requirements of the factory were not postulated
it "infers" indexes (in the wrong place)
@gares
Copy link
Member Author

gares commented Feb 13, 2021

diff --git a/mathcomp/algebra/ssralg.v b/mathcomp/algebra/ssralg.v
index 16c4c58..0cd3e00 100644
--- a/mathcomp/algebra/ssralg.v
+++ b/mathcomp/algebra/ssralg.v
@@ -4569,18 +4569,16 @@ Qed.
 
 End ClosedFieldTheory.
 
-Implicit Type V : zmodType.
-
 HB.mixin
-Record is_subZmodule V (S : {pred V}) (U : indexed Type) of SUB V S U & Zmodule U := {
+Record is_subZmodule (V : zmodType) (S : {pred V}) (U : indexed Type) of SUB V S U & Zmodule U := {
   valB : additive (val : U -> V);
 }.
 
 #[mathcomp]
-HB.structure Definition subZmodule V S := { U of subChoice V S U & Zmodule U & is_subZmodule V S U }.
+HB.structure Definition subZmodule V S := { U of is_subZmodule V S U  & subChoice V S U & Zmodule U }.
 
 Section additive.
-Context V (S : {pred V}) (U : subZmodule.type S).
+Context (V : zmodType) (S : {pred V}) (U : subZmodule.type S).
 Notation val := (val : U -> V).
 Canonical val_additive := Additive (valB : additive val).
 Lemma valD : {morph val : x y / x + y}. Proof. exact: raddfD. Qed.
@@ -4588,15 +4586,12 @@ Lemma val0 : val 0 = 0. Proof. exact: raddf0. Qed.
 Lemma valN : {morph val : x / - x}. Proof. exact: raddfN. Qed.
 End additive.
 
-HB.factory Record pred_subZmodule V (S : {pred V})
+HB.factory Record pred_subZmodule (V : zmodType) (S : {pred V})
     (subS : zmodPred S)
     (kS : keyed_pred subS)
     (U : indexed Type) of subChoice V S U := {}.
 
-HB.builders Context (V : zmodType) (S : {pred V})
-  (subS : zmodPred S)
-  (kS : keyed_pred subS)
-  (U : indexed Type) of pred_subZmodule V S subS kS U.
+HB.builders Context V S subS kS U of pred_subZmodule V S subS kS U.
 
 Let kS_S v : v \in kS -> v \in S. Proof. by rewrite keyed_predE. Qed.
 Let S_kS v : v \in S -> v \in kS. Proof. by rewrite keyed_predE. Qed.
@@ -4625,12 +4620,12 @@ HB.instance Definition _ := is_subZmodule.Build V S U raddf_val.
 HB.end.
 
 HB.mixin
-Record is_subRing (R : ringType) (S : {pred R}) (U : indexed Type) of subZmodule R S U & Ring U := {
+Record is_subRing (R : ringType) S (U : indexed Type) of subZmodule R S U & Ring U := {
   valM : multiplicative (val : U -> R);
 }.
 
 HB.structure
-Definition subRing R S := { U of subZmodule R S U & Ring U & is_subRing R S U}.
+Definition subRing R S := { U of is_subRing R S U & subZmodule R S U & Ring U }.
 
 Section multiplicative.
 Context (R : ringType) (S : {pred R}) (U : subRing.type S).
@@ -4643,9 +4638,7 @@ HB.factory Record pred_subRing (R : ringType) (S : {pred R})
   (ringS : subringPred S) (kS : keyed_pred ringS)
   (U : indexed Type) of subZmodule R S U := {}.
 
-HB.builders Context (R : ringType) (S : {pred R})
-(ringS : subringPred S) (kS : keyed_pred ringS)
-(U : indexed Type) of pred_subRing R S ringS kS U.
+HB.builders Context R S ringS kS U of pred_subRing R S ringS kS U.
 
 Let kS_S v : v \in kS -> v \in S. Proof. by rewrite keyed_predE. Qed.
 Let S_kS v : v \in S -> v \in kS. Proof. by rewrite keyed_predE. Qed.
@@ -4680,10 +4673,9 @@ HB.mixin Record is_subLmodule  (R : ringType) (V : lmodType R) (S : {pred V})
  valZ : scalable (val : W -> V);
 }.
 
-(* BUG: coercions *)
 HB.structure
-Definition subLmodule (R : ringType) (V : lmodType (Ring.sort R)) (S : {pred (Lmodule.sort V)}) :=
-  { W of subZmodule V S W & is_Lmodule_of_Zmodule R W & is_subLmodule R V S W}.
+Definition subLmodule R V S :=
+  { W of is_subLmodule R V S W & subZmodule V S W & is_Lmodule_of_Zmodule R W}.
 

@gares gares changed the title fix elaboration of parameters in HB.structure Elaboration of parameters in HB.structure and HB.builders Feb 13, 2021
@gares gares requested a review from CohenCyril February 13, 2021 16:45
phantom T1 t1 -> phantom T2 t2.
Definition id_phant {T} {t : T} (x : phantom T t) := x.
Definition nomsg : option (string * Type) := None.
Definition is_not_canonically_a : string := "is not canonically a".
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing!

Copy link
Member

@CohenCyril CohenCyril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice!

@CohenCyril CohenCyril merged commit 9822b52 into master Feb 14, 2021
@gares gares deleted the fix-elab-params branch February 14, 2021 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HB.structure does not always insert coercions. HB commands telescope inferrence is dumb

3 participants