@@ -855,22 +855,20 @@ witness these temporary variables.
855855Sort polymorphic inductives may be declared when every instantiation
856856is valid.
857857
858- Elimination at a given universe instance requires that elimination is
859- allowed at every ground instantiation of the sort variables in the
860- instance. Additionally if the output sort at the given universe
861- instance is sort polymorphic, the return type of the elimination must
862- be at the same quality. These restrictions ignore :flag: `Definitional
863- UIP `.
858+ Elimination of Sort-Polymorphic Inductives
859+ ------------------------------------------
864860
865- For instance
861+ Sort-polymorphic inductives follow rules for their elimination, both
862+ when the target sort is polymorphic or not. We illustrate the first case
863+ on the following example:
866864
867865.. rocqtop :: all reset
868866
869867 Set Universe Polymorphism.
870868
871869 Inductive Squash@{s;u} (A:Type@{s;u}) : Prop := squash (_:A).
872870
873- Elimination to `Prop ` and `SProp ` is always allowed, so `Squash_ind `
871+ Here, elimination to `Prop ` and `SProp ` is always allowed, so `Squash_ind `
874872and `Squash_sind ` are automatically defined.
875873
876874Elimination to `Type ` is not allowed with variable `s `, because the
@@ -890,19 +888,94 @@ However elimination to `Type` or to a polymorphic sort with `s := Prop` is allow
890888 : forall s, P s
891889 := fun s => match s with squash _ x => H x end.
892890
891+ In fact, for a given universe instance, elimination is allowed if:
892+
893+ - it is allowed for every ground instantiation of the sort variables in the instance, or
894+
895+ - the output sort at the given universe instance is sort polymorphic and
896+ the return type of the elimination is at the same quality.
897+
898+ For instance, for the following inductive, elimination is not allowed unless the target sort of
899+ the inductive matches the sort it is eliminated to.
900+
901+ .. rocqtop :: all
902+
903+ Inductive sum@{sl sr s;ul ur} (A:Type@{sl;ul}) (B:Type@{sr;ur}) : Type@{s;max(ul,ur)} :=
904+ | inl (_:A)
905+ | inr (_:B).
906+
907+ .. rocqtop :: none
908+
909+ Arguments inl {_ _} _.
910+ Arguments inr {_ _} _.
911+
912+ .. rocqtop :: all
913+
914+ Fail Definition sum_elim@{sl sr s s';ul ur u'|}
915+ (A:Type@{sl;ul}) (B:Type@{sr;ur}) (P:sum@{sl sr s;ul ur} A B -> Type@{s';u'})
916+ (fl : forall (x : A), P (inl x)) (fr : forall (y : B), P (inr y))
917+ (v : sum@{sl sr s;ul ur} A B) : P v :=
918+ match v with
919+ | inl x => fl x
920+ | inr y => fr y
921+ end.
922+
923+ As this greatly inhibits the possibilities of sort polymorphism, we have introduced *elimination
924+ constraints * to manage these cases. Here, the annotation `s -> s' ` can be added
925+ to tell Rocq that the definition is allowed for *every * sorts `s `, `s' ` such that `s ` eliminates
926+ into `s' `.
927+
928+ .. rocqtop :: all
929+
930+ Definition sum_elim@{sl sr s s';ul ur u'|s -> s'}
931+ (A:Type@{sl;ul}) (B:Type@{sr;ur}) (P:sum@{sl sr s;ul ur} A B -> Type@{s';u'})
932+ (fl : forall (x : A), P (inl x)) (fr : forall (y : B), P (inr y))
933+ (v : sum@{sl sr s;ul ur} A B) : P v :=
934+ match v with
935+ | inl x => fl x
936+ | inr y => fr y
937+ end.
938+
939+ It means that `s ` and `s' ` can respectively be instantiated to e.g., `Type ` and `Prop ` or
940+ `Prop ` and `SProp `, but cannot be instantiated to e.g., `Prop ` and `Type ` or `SProp ` and
941+ `Prop `.
942+
943+ .. rocqtop :: all
944+
945+ Check sum_elim@{_ _ Type Prop;_ _ _}.
946+ Check sum_elim@{_ _ Prop SProp;_ _ _}.
947+ Fail Check sum_elim@{_ _ Prop Type;_ _ _}.
948+ Fail Check sum_elim@{_ _ SProp Prop;_ _ _}.
949+
893950.. note ::
894951
895- Since inductive types with sort polymorphic output may only be
896- polymorphically eliminated to the same sort quality, containers
897- such as sigma types may be better defined as primitive records (which
898- do not have this restriction) when possible.
952+ As with universe level constraints, elimination constraints can be elaborated
953+ automatically if the constraints are denoted extensible with `+ ` **or ** if they
954+ are totally omitted. For instance, the two following definitions are legal.
899955
900956 .. rocqtop :: all
901957
902- Set Primitive Projections.
903- Record sigma@{s;u v} (A:Type@{s;u}) (B:A -> Type@{s;v})
904- : Type@{s;max(u,v)}
905- := pair { pr1 : A; pr2 : B pr1 }.
958+ Definition sum_elim_ext@{sl sr s s';ul ur u'|+}
959+ (A:Type@{sl;ul}) (B:Type@{sr;ur}) (P:sum@{sl sr s;ul ur} A B -> Type@{s';u'})
960+ (fl : forall (x : A), P (inl x)) (fr : forall (y : B), P (inr y))
961+ (v : sum@{sl sr s;ul ur} A B) : P v :=
962+ match v with
963+ | inl x => fl x
964+ | inr y => fr y
965+ end.
966+
967+ Definition sum_elim_elab@{sl sr s s';ul ur u'}
968+ (A:Type@{sl;ul}) (B:Type@{sr;ur}) (P:sum@{sl sr s;ul ur} A B -> Type@{s';u'})
969+ (fl : forall (x : A), P (inl x)) (fr : forall (y : B), P (inr y))
970+ (v : sum@{sl sr s;ul ur} A B) : P v :=
971+ match v with
972+ | inl x => fl x
973+ | inr y => fr y
974+ end.
975+
976+ .. note ::
977+
978+ These restrictions ignore :flag: `Definitional UIP `.
906979
907980.. flag :: Printing Sort Qualities
908981
@@ -915,7 +988,8 @@ However elimination to `Type` or to a polymorphic sort with `s := Prop` is allow
915988Explicit Sorts
916989---------------
917990
918- Similar to universes, fresh global sorts can be declared with the :cmd: `Sort `.
991+ Similar to universes, fresh global sorts can be declared with the :cmd: `Sort `, and elimination
992+ constraint on global sorts can be declared with the :cmd: `Constraint `.
919993
920994.. cmd :: Sort {+ @ident }
921995 Sorts {+ @ident }
0 commit comments