From df6e64fd28e9ba8b12045768869c7f083a15e9c0 Mon Sep 17 00:00:00 2001 From: Matthieu Sozeau Date: Fri, 13 Jun 2014 16:11:03 +0200 Subject: Fix HoTT/coq bug # 14. Now coercion code correctly raises an error instead of an anomaly in case a universe inconsistency occurs when applying a coercion. The statement of the test-suite file cannot check as is, but does check when the correct FunctorCategory is given, instantiating the TypeCat to Set. --- pretyping/coercion.ml | 24 ++-- test-suite/bugs/closed/HoTT_coq_014.v | 200 ++++++++++++++++++++++++++++++++++ test-suite/bugs/opened/HoTT_coq_014.v | 185 ------------------------------- 3 files changed, 212 insertions(+), 197 deletions(-) create mode 100644 test-suite/bugs/closed/HoTT_coq_014.v delete mode 100644 test-suite/bugs/opened/HoTT_coq_014.v diff --git a/pretyping/coercion.ml b/pretyping/coercion.ml index baf98eee72..125517aec5 100644 --- a/pretyping/coercion.ml +++ b/pretyping/coercion.ml @@ -40,14 +40,13 @@ let apply_coercion_args env evd check argl funj = let rec apply_rec acc typ = function | [] -> { uj_val = applist (j_val funj,argl); uj_type = typ } - | h::restl -> - (* On devrait pouvoir s'arranger pour qu'on n'ait pas à faire hnf_constr *) - match kind_of_term (whd_betadeltaiota env evd typ) with - | Prod (_,c1,c2) -> - if check && not (e_cumul env evdref (Retyping.get_type_of env evd h) c1) then - anomaly (Pp.str"apply_coercion_args: mismatch between arguments and coercion"); - apply_rec (h::acc) (subst1 h c2) restl - | _ -> anomaly (Pp.str "apply_coercion_args") + | h::restl -> (* On devrait pouvoir s'arranger pour qu'on n'ait pas a faire hnf_constr *) + match kind_of_term (whd_betadeltaiota env evd typ) with + | Prod (_,c1,c2) -> + if check && not (e_cumul env evdref (Retyping.get_type_of env evd h) c1) then + raise NoCoercion; + apply_rec (h::acc) (subst1 h c2) restl + | _ -> anomaly (Pp.str "apply_coercion_args") in let res = apply_rec [] funj.uj_type argl in !evdref, res @@ -346,7 +345,8 @@ let apply_coercion env sigma p hj typ_cl = jres.uj_type,sigma) (hj,typ_cl,sigma) p in evd, j - with e when Errors.noncritical e -> anomaly (Pp.str "apply_coercion") + with NoCoercion as e -> raise e + | e when Errors.noncritical e -> anomaly (Pp.str "apply_coercion") let inh_app_fun env evd j = let t = whd_betadeltaiota env evd j.uj_type in @@ -359,7 +359,7 @@ let inh_app_fun env evd j = try let t,p = lookup_path_to_fun_from env evd j.uj_type in apply_coercion env evd p j t - with Not_found when Flags.is_program_mode () -> + with Not_found | NoCoercion when Flags.is_program_mode () -> try let evdref = ref evd in let coercef, t = mu env evdref t in @@ -382,7 +382,7 @@ let inh_tosort_force loc env evd j = let evd,j1 = apply_coercion env evd p j t in let j2 = on_judgment_type (whd_evar evd) j1 in (evd,type_judgment env j2) - with Not_found -> + with Not_found | NoCoercion -> error_not_a_type_loc loc env evd j let inh_coerce_to_sort loc env evd j = @@ -421,7 +421,7 @@ let inh_coerce_to_fail env evd rigidonly v t c1 = try let t2,t1,p = lookup_path_between env evd (t,c1) in match v with - Some v -> + | Some v -> let evd,j = apply_coercion env evd p {uj_val = v; uj_type = t} t2 in diff --git a/test-suite/bugs/closed/HoTT_coq_014.v b/test-suite/bugs/closed/HoTT_coq_014.v new file mode 100644 index 0000000000..63548a644d --- /dev/null +++ b/test-suite/bugs/closed/HoTT_coq_014.v @@ -0,0 +1,200 @@ +Set Implicit Arguments. +Generalizable All Variables. +Set Universe Polymorphism. + +Polymorphic Record SpecializedCategory (obj : Type) := Build_SpecializedCategory' { + Object :> _ := obj; + Morphism' : obj -> obj -> Type; + + Identity' : forall o, Morphism' o o; + Compose' : forall s d d', Morphism' d d' -> Morphism' s d -> Morphism' s d' +}. + +Polymorphic Definition Morphism obj (C : @SpecializedCategory obj) : forall s d : C, _ := Eval cbv beta delta [Morphism'] in C.(Morphism'). + +(* eh, I'm not terribly happy. meh. *) +Polymorphic Definition SmallSpecializedCategory (obj : Set) (*mor : obj -> obj -> Set*) := SpecializedCategory@{Set Set} obj. +Polymorphic Identity Coercion SmallSpecializedCategory_LocallySmallSpecializedCategory_Id : SmallSpecializedCategory >-> SpecializedCategory. + +Polymorphic Record Category := { + CObject : Type; + + UnderlyingCategory :> @SpecializedCategory CObject +}. + +Polymorphic Definition GeneralizeCategory `(C : @SpecializedCategory obj) : Category. + refine {| CObject := C.(Object) |}; auto. +Defined. + +Polymorphic Coercion GeneralizeCategory : SpecializedCategory >-> Category. + + + +Section SpecializedFunctor. + Set Universe Polymorphism. + Context `(C : @SpecializedCategory objC). + Context `(D : @SpecializedCategory objD). + Unset Universe Polymorphism. + + Polymorphic Record SpecializedFunctor := { + ObjectOf' : objC -> objD; + MorphismOf' : forall s d, C.(Morphism') s d -> D.(Morphism') (ObjectOf' s) (ObjectOf' d); + FCompositionOf' : forall s d d' (m1 : C.(Morphism') s d) (m2: C.(Morphism') d d'), + MorphismOf' _ _ (C.(Compose') _ _ _ m2 m1) = D.(Compose') _ _ _ (MorphismOf' _ _ m2) (MorphismOf' _ _ m1); + FIdentityOf' : forall o, MorphismOf' _ _ (C.(Identity') o) = D.(Identity') (ObjectOf' o) + }. +End SpecializedFunctor. + +Global Polymorphic Coercion ObjectOf' : SpecializedFunctor >-> Funclass. +Set Universe Polymorphism. +Section Functor. + Variable C D : Category. + + Polymorphic Definition Functor := SpecializedFunctor C D. +End Functor. +Unset Universe Polymorphism. + +Polymorphic Identity Coercion Functor_SpecializedFunctor_Id : Functor >-> SpecializedFunctor. +Polymorphic Definition GeneralizeFunctor objC C objD D (F : @SpecializedFunctor objC C objD D) : Functor C D := F. +Polymorphic Coercion GeneralizeFunctor : SpecializedFunctor >-> Functor. + +Arguments SpecializedFunctor {objC} C {objD} D. + + +Polymorphic Record SmallCategory := { + SObject : Set; + + SUnderlyingCategory :> @SmallSpecializedCategory SObject +}. + +Polymorphic Definition GeneralizeSmallCategory `(C : @SmallSpecializedCategory obj) : SmallCategory. + refine {| SObject := obj |}; destruct C; econstructor; eassumption. +Defined. + +Polymorphic Coercion GeneralizeSmallCategory : SmallSpecializedCategory >-> SmallCategory. + +Bind Scope category_scope with SmallCategory. + + +Polymorphic Definition TypeCat : @SpecializedCategory Type := (@Build_SpecializedCategory' Type + (fun s d => s -> d) + (fun _ => (fun x => x)) + (fun _ _ _ f g => (fun x => f (g x)))). +(*Unset Universe Polymorphism.*) +Polymorphic Definition FunctorCategory objC (C : @SpecializedCategory objC) objD (D : @SpecializedCategory objD) : + @SpecializedCategory (SpecializedFunctor C D). +Admitted. + +Polymorphic Definition DiscreteCategory (O : Type) : @SpecializedCategory O. +Admitted. + +Polymorphic Definition ComputableCategory (I : Type) (Index2Object : I -> Type) (Index2Cat : forall i : I, @SpecializedCategory (@Index2Object i)) : + @SpecializedCategory I. +Admitted. + +Polymorphic Definition is_unique (A : Type) (x : A) := forall x' : A, x' = x. + +Polymorphic Definition InitialObject obj {C : SpecializedCategory obj} (o : C) := + forall o', { m : C.(Morphism) o o' | is_unique m }. + +Polymorphic Definition SmallCat := ComputableCategory _ SUnderlyingCategory. + +Lemma InitialCategory_Initial : InitialObject (C := SmallCat) (DiscreteCategory Empty_set : SmallSpecializedCategory _). + admit. +Qed. + +Set Universe Polymorphism. +Section GraphObj. + Context `(C : @SpecializedCategory objC). + + Inductive GraphIndex := GraphIndexSource | GraphIndexTarget. + + Definition GraphIndex_Morphism (a b : GraphIndex) : Set := + match (a, b) with + | (GraphIndexSource, GraphIndexSource) => unit + | (GraphIndexTarget, GraphIndexTarget) => unit + | (GraphIndexTarget, GraphIndexSource) => Empty_set + | (GraphIndexSource, GraphIndexTarget) => GraphIndex + end. + + Global Arguments GraphIndex_Morphism a b /. + + Definition GraphIndex_Compose s d d' (m1 : GraphIndex_Morphism d d') (m2 : GraphIndex_Morphism s d) : + GraphIndex_Morphism s d'. + Admitted. + + Definition GraphIndexingCategory : @SpecializedCategory GraphIndex. + refine {| + Morphism' := GraphIndex_Morphism; + Identity' := (fun x => match x with GraphIndexSource => tt | GraphIndexTarget => tt end); + Compose' := GraphIndex_Compose + |}; + admit. + Defined. + + Definition UnderlyingGraph_ObjectOf x := + match x with + | GraphIndexSource => { sd : objC * objC & C.(Morphism) (fst sd) (snd sd) } + | GraphIndexTarget => objC + end. + + Global Arguments UnderlyingGraph_ObjectOf x /. + + Definition UnderlyingGraph_MorphismOf s d (m : Morphism GraphIndexingCategory s d) : + UnderlyingGraph_ObjectOf s -> UnderlyingGraph_ObjectOf d. + Admitted. + + Definition UnderlyingGraph : SpecializedFunctor GraphIndexingCategory TypeCat. + Proof. + match goal with + | [ |- SpecializedFunctor ?C ?D ] => + refine (Build_SpecializedFunctor C D + UnderlyingGraph_ObjectOf + UnderlyingGraph_MorphismOf + _ + _ + ) + end; + admit. + Defined. +End GraphObj. + +Set Printing Universes. +Set Printing All. + +Print Coercions. + +Section test. + +Fail Polymorphic Definition UnderlyingGraphFunctor_MorphismOf' (C D : SmallCategory) (F : SpecializedFunctor C D) : + Morphism (FunctorCategory TypeCat GraphIndexingCategory) + (@UnderlyingGraph (SObject C) + (SmallSpecializedCategory_LocallySmallSpecializedCategory_Id (SUnderlyingCategory C))) + (UnderlyingGraph D). + (* Toplevel input, characters 216-249: +Error: +In environment +C : SmallCategory (* Top.594 *) +D : SmallCategory (* Top.595 *) +F : +@SpecializedFunctor (* Top.25 Set Top.25 Set *) (SObject (* Top.25 *) C) + (SUnderlyingCategory (* Top.25 *) C) (SObject (* Top.25 *) D) + (SUnderlyingCategory (* Top.25 *) D) +The term + "SUnderlyingCategory (* Top.25 *) C + :SpecializedCategory (* Top.25 Set *) (SObject (* Top.25 *) C)" has type + "SpecializedCategory (* Top.618 Top.619 *) (SObject (* Top.25 *) C)" +while it is expected to have type + "SpecializedCategory (* Top.224 Top.225 *) (SObject (* Top.617 *) C)" +(Universe inconsistency: Cannot enforce Set = Top.225)). + *) +Fail Admitted. + +Fail Polymorphic Definition UnderlyingGraphFunctor_MorphismOf (C D : SmallCategory) (F : SpecializedFunctor C D) : + Morphism (FunctorCategory TypeCat GraphIndexingCategory) (UnderlyingGraph C) (UnderlyingGraph D). (* Anomaly: apply_coercion. Please report.*) +Fail Admitted. + +Polymorphic Definition UnderlyingGraphFunctor_MorphismOf (C D : SmallCategory) (F : SpecializedFunctor C D) : + Morphism (FunctorCategory GraphIndexingCategory TypeCat) (UnderlyingGraph C) (UnderlyingGraph D). (* Anomaly: apply_coercion. Please report.*) +Proof. +Admitted. \ No newline at end of file diff --git a/test-suite/bugs/opened/HoTT_coq_014.v b/test-suite/bugs/opened/HoTT_coq_014.v deleted file mode 100644 index 109653ca27..0000000000 --- a/test-suite/bugs/opened/HoTT_coq_014.v +++ /dev/null @@ -1,185 +0,0 @@ -Set Implicit Arguments. -Generalizable All Variables. - - -Polymorphic Record SpecializedCategory (obj : Type) := Build_SpecializedCategory' { - Object :> _ := obj; - Morphism' : obj -> obj -> Type; - - Identity' : forall o, Morphism' o o; - Compose' : forall s d d', Morphism' d d' -> Morphism' s d -> Morphism' s d' -}. - -Polymorphic Definition Morphism obj (C : @SpecializedCategory obj) : forall s d : C, _ := Eval cbv beta delta [Morphism'] in C.(Morphism'). - -(* eh, I'm not terribly happy. meh. *) -Polymorphic Definition SmallSpecializedCategory (obj : Set) (*mor : obj -> obj -> Set*) := SpecializedCategory obj. -Identity Coercion SmallSpecializedCategory_LocallySmallSpecializedCategory_Id : SmallSpecializedCategory >-> SpecializedCategory. - -Polymorphic Record Category := { - CObject : Type; - - UnderlyingCategory :> @SpecializedCategory CObject -}. - -Polymorphic Definition GeneralizeCategory `(C : @SpecializedCategory obj) : Category. - refine {| CObject := C.(Object) |}; auto. -Defined. - -Coercion GeneralizeCategory : SpecializedCategory >-> Category. - - - -Section SpecializedFunctor. - Set Universe Polymorphism. - Context `(C : @SpecializedCategory objC). - Context `(D : @SpecializedCategory objD). - Unset Universe Polymorphism. - - Polymorphic Record SpecializedFunctor := { - ObjectOf' : objC -> objD; - MorphismOf' : forall s d, C.(Morphism') s d -> D.(Morphism') (ObjectOf' s) (ObjectOf' d); - FCompositionOf' : forall s d d' (m1 : C.(Morphism') s d) (m2: C.(Morphism') d d'), - MorphismOf' _ _ (C.(Compose') _ _ _ m2 m1) = D.(Compose') _ _ _ (MorphismOf' _ _ m2) (MorphismOf' _ _ m1); - FIdentityOf' : forall o, MorphismOf' _ _ (C.(Identity') o) = D.(Identity') (ObjectOf' o) - }. -End SpecializedFunctor. - -Global Coercion ObjectOf' : SpecializedFunctor >-> Funclass. -(*Unset Universe Polymorphism.*) -Section Functor. - Variable C D : Category. - - Polymorphic Definition Functor := SpecializedFunctor C D. -End Functor. - -Identity Coercion Functor_SpecializedFunctor_Id : Functor >-> SpecializedFunctor. -Polymorphic Definition GeneralizeFunctor objC C objD D (F : @SpecializedFunctor objC C objD D) : Functor C D := F. -Coercion GeneralizeFunctor : SpecializedFunctor >-> Functor. - -Arguments SpecializedFunctor {objC} C {objD} D. - - -Polymorphic Record SmallCategory := { - SObject : Set; - - SUnderlyingCategory :> @SmallSpecializedCategory SObject -}. - -Polymorphic Definition GeneralizeSmallCategory `(C : @SmallSpecializedCategory obj) : SmallCategory. - refine {| SObject := obj |}; destruct C; econstructor; eassumption. -Defined. - -Coercion GeneralizeSmallCategory : SmallSpecializedCategory >-> SmallCategory. - -Bind Scope category_scope with SmallCategory. - - -Polymorphic Definition TypeCat : @SpecializedCategory Type := (@Build_SpecializedCategory' Type - (fun s d => s -> d) - (fun _ => (fun x => x)) - (fun _ _ _ f g => (fun x => f (g x)))). -(*Unset Universe Polymorphism.*) -Polymorphic Definition FunctorCategory objC (C : @SpecializedCategory objC) objD (D : @SpecializedCategory objD) : - @SpecializedCategory (SpecializedFunctor C D). -Admitted. - -Polymorphic Definition DiscreteCategory (O : Type) : @SpecializedCategory O. -Admitted. - -Polymorphic Definition ComputableCategory (I : Type) (Index2Object : I -> Type) (Index2Cat : forall i : I, @SpecializedCategory (@Index2Object i)) : - @SpecializedCategory I. -Admitted. - -Polymorphic Definition is_unique (A : Type) (x : A) := forall x' : A, x' = x. - -Polymorphic Definition InitialObject obj {C : SpecializedCategory obj} (o : C) := - forall o', { m : C.(Morphism) o o' | is_unique m }. - -Polymorphic Definition SmallCat := ComputableCategory _ SUnderlyingCategory. - -Lemma InitialCategory_Initial : InitialObject (C := SmallCat) (DiscreteCategory Empty_set : SmallSpecializedCategory _). - admit. -Qed. - - -Section GraphObj. - Context `(C : @SpecializedCategory objC). - - Inductive GraphIndex := GraphIndexSource | GraphIndexTarget. - - Polymorphic Definition GraphIndex_Morphism (a b : GraphIndex) : Set := - match (a, b) with - | (GraphIndexSource, GraphIndexSource) => unit - | (GraphIndexTarget, GraphIndexTarget) => unit - | (GraphIndexTarget, GraphIndexSource) => Empty_set - | (GraphIndexSource, GraphIndexTarget) => GraphIndex - end. - - Global Arguments GraphIndex_Morphism a b /. - - Polymorphic Definition GraphIndex_Compose s d d' (m1 : GraphIndex_Morphism d d') (m2 : GraphIndex_Morphism s d) : - GraphIndex_Morphism s d'. - Admitted. - - Polymorphic Definition GraphIndexingCategory : @SpecializedCategory GraphIndex. - refine {| - Morphism' := GraphIndex_Morphism; - Identity' := (fun x => match x with GraphIndexSource => tt | GraphIndexTarget => tt end); - Compose' := GraphIndex_Compose - |}; - admit. - Defined. - - Polymorphic Definition UnderlyingGraph_ObjectOf x := - match x with - | GraphIndexSource => { sd : objC * objC & C.(Morphism) (fst sd) (snd sd) } - | GraphIndexTarget => objC - end. - - Global Arguments UnderlyingGraph_ObjectOf x /. - - Polymorphic Definition UnderlyingGraph_MorphismOf s d (m : Morphism GraphIndexingCategory s d) : - UnderlyingGraph_ObjectOf s -> UnderlyingGraph_ObjectOf d. - Admitted. - - Polymorphic Definition UnderlyingGraph : SpecializedFunctor GraphIndexingCategory TypeCat. - Proof. - match goal with - | [ |- SpecializedFunctor ?C ?D ] => - refine (Build_SpecializedFunctor C D - UnderlyingGraph_ObjectOf - UnderlyingGraph_MorphismOf - _ - _ - ) - end; - admit. - Defined. -End GraphObj. - -Set Printing Universes. -Set Printing All. -Fail Polymorphic Definition UnderlyingGraphFunctor_MorphismOf' (C D : SmallCategory) (F : SpecializedFunctor C D) : - Morphism (FunctorCategory TypeCat GraphIndexingCategory) (@UnderlyingGraph (SObject C) (C:SpecializedCategory (SObject C))) (UnderlyingGraph D). (* Toplevel input, characters 216-249: -Error: -In environment -C : SmallCategory (* Top.594 *) -D : SmallCategory (* Top.595 *) -F : -@SpecializedFunctor (* Top.25 Set Top.25 Set *) (SObject (* Top.25 *) C) - (SUnderlyingCategory (* Top.25 *) C) (SObject (* Top.25 *) D) - (SUnderlyingCategory (* Top.25 *) D) -The term - "SUnderlyingCategory (* Top.25 *) C - :SpecializedCategory (* Top.25 Set *) (SObject (* Top.25 *) C)" has type - "SpecializedCategory (* Top.618 Top.619 *) (SObject (* Top.25 *) C)" -while it is expected to have type - "SpecializedCategory (* Top.224 Top.225 *) (SObject (* Top.617 *) C)" -(Universe inconsistency: Cannot enforce Set = Top.225)). - *) -Fail Admitted. - -Fail Polymorphic Definition UnderlyingGraphFunctor_MorphismOf (C D : SmallCategory) (F : SpecializedFunctor C D) : - Morphism (FunctorCategory TypeCat GraphIndexingCategory) (UnderlyingGraph C) (UnderlyingGraph D). (* Anomaly: apply_coercion. Please report.*) -Fail Admitted. -- cgit v1.2.3