| 1 |
Set Universe Polymorphism. |
| 2 |
|
| 3 |
Module Syntax. |
| 4 |
Fail Definition foo@{| Set < Set } := Set. |
| 5 |
|
| 6 |
Definition foo@{u| Set < u} := Type@{u}. |
| 7 |
|
| 8 |
Definition bar@{s ; u | Set < u} := Type@{u}. |
| 9 |
Set Printing Universes. |
| 10 |
Print bar. |
| 11 |
|
| 12 |
Definition baz@{s ; } := Type@{s ; Set}. |
| 13 |
Print baz. |
| 14 |
|
| 15 |
Definition potato@{s ; + } := Type. |
| 16 |
|
| 17 |
Check eq_refl : Prop = baz@{Prop ; }. |
| 18 |
|
| 19 |
Inductive bob@{s ; } : Prop := . |
| 20 |
End Syntax. |
| 21 |
|
| 22 |
Module Reduction. |
| 23 |
|
| 24 |
Definition qsort@{s ; u } := Type@{s ; u}. |
| 25 |
|
| 26 |
Monomorphic Universe U. |
| 27 |
|
| 28 |
Definition tU := Type@{U}. |
| 29 |
Definition qU := qsort@{Type ; U}. |
| 30 |
|
| 31 |
Definition q1 := Eval lazy in qU. |
| 32 |
Check eq_refl : q1 = tU. |
| 33 |
|
| 34 |
Definition q2 := Eval vm_compute in qU. |
| 35 |
Check eq_refl : q2 = tU. |
| 36 |
|
| 37 |
Definition q3 := Eval native_compute in qU. |
| 38 |
Check eq_refl : q3 = tU. |
| 39 |
|
| 40 |
Definition exfalso@{s;u|} (A:Type@{s;u}) (H:False) : A := match H with end. |
| 41 |
|
| 42 |
Definition exfalsoVM := Eval vm_compute in exfalso@{Type;Set}. |
| 43 |
Definition exfalsoNative := Eval native_compute in exfalso@{Type;Set}. |
| 44 |
|
| 45 |
Fixpoint iter@{s;u|} (A:Type@{s;u}) (f:A -> A) n x := |
| 46 |
match n with |
| 47 |
| 0 => x |
| 48 |
| S k => iter A f k (f x) |
| 49 |
end. |
| 50 |
|
| 51 |
Definition iterType := Eval lazy in iter@{Type;_}. |
| 52 |
Definition iterSProp := Eval lazy in iter@{SProp;_}. |
| 53 |
|
| 54 |
End Reduction. |
| 55 |
|
| 56 |
Module Conversion. |
| 57 |
|
| 58 |
Inductive Box@{s;u|} (A:Type@{s;u}) := box (_:A). |
| 59 |
|
| 60 |
Definition t1@{s;u|} (A:Type@{s;u}) (x y : A) := box _ x. |
| 61 |
Definition t2@{s;u|} (A:Type@{s;u}) (x y : A) := box _ y. |
| 62 |
|
| 63 |
Definition t1'@{s;u|} (A:Type@{s;u}) (x y : A) := x. |
| 64 |
Definition t2'@{s;u|} (A:Type@{s;u}) (x y : A) := y. |
| 65 |
|
| 66 |
Fail Check eq_refl : t1 nat = t2 nat. |
| 67 |
Fail Check eq_refl : t1' nat = t2' nat. |
| 68 |
|
| 69 |
Check fun A:SProp => eq_refl : t1 A = t2 A. |
| 70 |
Check fun A:SProp => eq_refl : box _ (t1' A) = box _ (t2' A). |
| 71 |
|
| 72 |
Fail Definition equi_eliminable@{s1 s2;|s1 -> s2, s2 -> s1} (A : Type@{s1;Set}) : Type@{s2;Set} := A. |
| 73 |
Fail Definition equi_eliminable@{s1 s2;|s1 -> s2, s2 -> s1} (A : Type@{s2;Set}) : Type@{s1;Set} := A. |
| 74 |
|
| 75 |
Definition ignore@{s;u|} {A:Type@{s;u}} (x:A) := tt. |
| 76 |
Definition unfold_ignore@{s;u|} (A:Type@{s;u}) : ignore (t1 A) = ignore (t2 A) := eq_refl. |
| 77 |
|
| 78 |
Definition t (A:SProp) := Eval lazy in t1 A. |
| 79 |
|
| 80 |
Axiom v@{s; |} : forall (A:Type@{s;Set}), bool -> A. |
| 81 |
Fail Check fun P (x:P (v@{Type;} nat true)) => x : P (v nat false). |
| 82 |
Check fun (A:SProp) P (x:P (v A true)) => x : P (v A false). |
| 83 |
|
| 84 |
End Conversion. |
| 85 |
|
| 86 |
Module Inference. |
| 87 |
Definition zog@{s; |} (A:Type@{s;Set}) := A. |
| 88 |
|
| 89 |
(* implicit instance of zog gets a variable which then gets unified with s from the type of A *) |
| 90 |
Definition zag@{s; |} (A:Type@{s;Set}) := zog A. |
| 91 |
|
| 92 |
(* implicit type of A gets unified to Type@{s|Set} *) |
| 93 |
Definition zig@{s; |} A := zog@{s;} A. |
| 94 |
|
| 95 |
(* Unfortunately casting a hole to a sort (while typing A on the |
| 96 |
left of the arrow) produces a rigid univ level. It gets a |
| 97 |
constraint "= Set" but rigids don't get substituted away for (bad) |
| 98 |
reasons. This is why we need the 2 "+". *) |
| 99 |
Definition zig'@{s; + | +} A := A -> zog@{s;} A. |
| 100 |
|
| 101 |
(* different manually bound sort variables don't unify *) |
| 102 |
Fail Definition zog'@{s s'; |} (A:Type@{s;Set}) := zog@{s';} A. |
| 103 |
End Inference. |
| 104 |
|
| 105 |
Module Inductives. |
| 106 |
Inductive foo1@{s; |} : Type@{s;Set} := . |
| 107 |
Fail Check foo1_sind. |
| 108 |
|
| 109 |
Definition foo1_False@{s;+|+} (x:foo1@{s;}) : False := match x return False with end. |
| 110 |
(* s ; |= s -> Prop *) |
| 111 |
|
| 112 |
Inductive foo2@{s; |} := Foo2 : Type@{s;Set} -> foo2. |
| 113 |
Check foo2_rect. |
| 114 |
|
| 115 |
Inductive foo3@{s; |} (A:Type@{s;Set}) := Foo3 : A -> foo3 A. |
| 116 |
Check foo3_rect. |
| 117 |
|
| 118 |
Fail Inductive foo4@{s;u v|v < u} : Type@{v} := C (_:Type@{s;u}). |
| 119 |
|
| 120 |
Inductive foo5@{s; |} (A:Type@{s;Set}) : Prop := Foo5 (_ : A). |
| 121 |
Definition foo5_ind'@{s; |} : forall (A : Type@{s;Set}) (P : Prop), (A -> P) -> foo5 A -> P |
| 122 |
:= foo5_ind. |
| 123 |
|
| 124 |
(* TODO unify sort variable instead of failing *) |
| 125 |
Fail Definition foo5_Prop_rect (A:Prop) (P:foo5 A -> Type) |
| 126 |
(H : forall a, P (Foo5 A a)) |
| 127 |
(f : foo5 A) |
| 128 |
: P f |
| 129 |
:= match f with Foo5 _ a => H a end. |
| 130 |
(* |
| 131 |
Error: The quality constraints are inconsistent: cannot enforce Prop -> Type because it would identify Type and Prop which is inconsistent. |
| 132 |
This is introduced by the constraints Prop -> Type |
| 133 |
*) |
| 134 |
|
| 135 |
Definition foo5_Prop_rect (A:Prop) (P:foo5 A -> Type) |
| 136 |
(H : forall a, P (Foo5 A a)) |
| 137 |
(f : foo5@{Prop;} A) |
| 138 |
: P f |
| 139 |
:= match f with Foo5 _ a => H a end. |
| 140 |
|
| 141 |
(* all sort poly output with nonzero contructors are squashed (avoid interfering with uip) *) |
| 142 |
Inductive foo6@{s; |} : Type@{s;Set} := Foo6. |
| 143 |
Fail Check foo6_sind. |
| 144 |
|
| 145 |
Definition foo6_rect@{s;+|+} (P:foo6@{s;} -> Type) |
| 146 |
(H : P Foo6) |
| 147 |
(f : foo6) |
| 148 |
: P f |
| 149 |
:= match f with Foo6 => H end. |
| 150 |
(* s ; u |= s -> Type *) |
| 151 |
|
| 152 |
Definition foo6_prop_rect (P:foo6 -> Type) |
| 153 |
(H : P Foo6) |
| 154 |
(f : foo6@{Prop;}) |
| 155 |
: P f |
| 156 |
:= match f with Foo6 => H end. |
| 157 |
|
| 158 |
Definition foo6_type_rect (P:foo6 -> Type) |
| 159 |
(H : P Foo6) |
| 160 |
(f : foo6@{Type;}) |
| 161 |
: P f |
| 162 |
:= match f with Foo6 => H end. |
| 163 |
|
| 164 |
Definition foo6_qsort_rect@{s;u|} (P:foo6 -> Type@{s;u}) |
| 165 |
(H : P Foo6) |
| 166 |
(f : foo6@{s;}) |
| 167 |
: P f |
| 168 |
:= match f with Foo6 => H end. |
| 169 |
|
| 170 |
Fail Definition foo6_2qsort_rect@{s s';u|} (P:foo6 -> Type@{s;u}) |
| 171 |
(H : P Foo6) |
| 172 |
(f : foo6@{s';}) |
| 173 |
: P f |
| 174 |
:= match f with Foo6 => H end. |
| 175 |
|
| 176 |
Inductive foo7@{s; |} : Type@{s;Set} := Foo7_1 | Foo7_2. |
| 177 |
Fail Check foo7_sind. |
| 178 |
Fail Check foo7_ind. |
| 179 |
|
| 180 |
Definition foo7_prop_ind (P:foo7 -> Prop) |
| 181 |
(H : P Foo7_1) (H' : P Foo7_2) |
| 182 |
(f : foo7@{Prop;}) |
| 183 |
: P f |
| 184 |
:= match f with Foo7_1 => H | Foo7_2 => H' end. |
| 185 |
|
| 186 |
Fail Definition foo7_prop_rect (P:foo7 -> Type) |
| 187 |
(H : P Foo7_1) (H' : P Foo7_2) |
| 188 |
(f : foo7@{Prop;}) |
| 189 |
: P f |
| 190 |
:= match f with Foo7_1 => H | Foo7_2 => H' end. |
| 191 |
|
| 192 |
Set Primitive Projections. |
| 193 |
Set Warnings "+records". |
| 194 |
|
| 195 |
(* the SProp instantiation may not be primitive so the whole thing must be nonprimitive *) |
| 196 |
Fail Record R1@{s; |} : Type@{s;Set} := {}. |
| 197 |
|
| 198 |
(* the Type instantiation may not be primitive *) |
| 199 |
Fail Record R2@{s; |} (A:SProp) : Type@{s;Set} := { R2f1 : A }. |
| 200 |
|
| 201 |
(* R3@{SProp Type|} may not be primitive *) |
| 202 |
Fail Record R3@{s s'; |} (A:Type@{s;Set}) : Type@{s';Set} := { R3f1 : A }. |
| 203 |
|
| 204 |
Record R4@{s; |} (A:Type@{s;Set}) : Type@{s;Set} := { R4f1 : A}. |
| 205 |
|
| 206 |
(* non SProp instantiation must be squashed *) |
| 207 |
Fail Record R5@{s; |} (A:Type@{s;Set}) : SProp := { R5f1 : A}. |
| 208 |
Fail #[warnings="-non-primitive-record"] |
| 209 |
Record R5@{s; |} (A:Type@{s;Set}) : SProp := { R5f1 : A}. |
| 210 |
#[warnings="-non-primitive-record,-cannot-define-projection"] |
| 211 |
Record R5@{s; |} (A:Type@{s;Set}) : SProp := { R5f1 : A}. |
| 212 |
Fail Check R5f1. |
| 213 |
Definition R5f1_sprop (A:SProp) (r:R5 A) : A := let (f) := r in f. |
| 214 |
Fail Definition R5f1_prop (A:Prop) (r:R5 A) : A := let (f) := r in f. |
| 215 |
|
| 216 |
Record R6@{s; |} (A:Type@{s;Set}) := { R6f1 : A; R6f2 : nat }. |
| 217 |
Check fun (A:SProp) (x y : R6 A) => |
| 218 |
eq_refl : Conversion.box _ x.(R6f1 _) = Conversion.box _ y.(R6f1 _). |
| 219 |
Fail Check fun (A:Prop) (x y : R6 A) => |
| 220 |
eq_refl : Conversion.box _ x.(R6f1 _) = Conversion.box _ y.(R6f1 _). |
| 221 |
Fail Check fun (A:SProp) (x y : R6 A) => |
| 222 |
eq_refl : Conversion.box _ x.(R6f2 _) = Conversion.box _ y.(R6f2 _). |
| 223 |
|
| 224 |
#[projections(primitive=no)] Record R7@{s; |} (A:Type@{s;Set}) := { R7f1 : A; R7f2 : nat }. |
| 225 |
Check R7@{SProp;} : SProp -> Set. |
| 226 |
Check R7@{Type;} : Set -> Set. |
| 227 |
|
| 228 |
Inductive sigma@{s;u v|} (A:Type@{s;u}) (B:A -> Type@{s;v}) : Type@{s;max(u,v)} |
| 229 |
:= pair : forall x : A, B x -> sigma A B. |
| 230 |
|
| 231 |
Definition sigma_srect@{s;k +|} A B |
| 232 |
(P : sigma@{s;_ _} A B -> Type@{s;k}) |
| 233 |
(H : forall x b, P (pair _ _ x b)) |
| 234 |
(s:sigma A B) |
| 235 |
: P s |
| 236 |
:= match s with pair _ _ x b => H x b end. |
| 237 |
|
| 238 |
(* squashed because positive type with >0 constructors *) |
| 239 |
Fail Definition sigma_srect'@{s sk;k +|} A B |
| 240 |
(P : sigma@{s;_ _} A B -> Type@{sk;k}) |
| 241 |
(H : forall x b, P (pair _ _ x b)) |
| 242 |
(s:sigma A B) |
| 243 |
: P s |
| 244 |
:= match s with pair _ _ x b => H x b end. |
| 245 |
|
| 246 |
(* even though it's squashed, we can still define the projections *) |
| 247 |
Definition pr1@{s;+|} {A B} (s:sigma@{s;_ _} A B) : A |
| 248 |
:= match s with pair _ _ x _ => x end. |
| 249 |
|
| 250 |
Definition pr2@{s;+|} {A B} (s:sigma@{s;_ _} A B) : B (pr1 s) |
| 251 |
:= match s with pair _ _ _ y => y end. |
| 252 |
|
| 253 |
(* And we can prove eta with implicit elaboration of elimination constraints. |
| 254 |
We can't prove eta without the constraints. *) |
| 255 |
Inductive seq@{s;u|} (A:Type@{s;u}) (a:A) : A -> Prop := seq_refl : seq A a a. |
| 256 |
Arguments seq_refl {_ _}. |
| 257 |
|
| 258 |
Definition eta@{s;+|+} A B (s:sigma@{s;_ _} A B) : seq _ s (pair A B (pr1 s) (pr2 s)). |
| 259 |
Proof. |
| 260 |
destruct s. simpl. reflexivity. |
| 261 |
Qed. |
| 262 |
|
| 263 |
(* sigma as a primitive record works better *) |
| 264 |
Record Rsigma@{s;u v|} (A:Type@{s;u}) (B:A -> Type@{s;v}) : Type@{s;max(u,v)} |
| 265 |
:= Rpair { Rpr1 : A; Rpr2 : B Rpr1 }. |
| 266 |
|
| 267 |
(* match desugared to primitive projections using definitional eta *) |
| 268 |
Definition Rsigma_srect@{s sk;k +|} A B |
| 269 |
(P : Rsigma@{s;_ _} A B -> Type@{sk;k}) |
| 270 |
(H : forall x b, P (Rpair _ _ x b)) |
| 271 |
(s:Rsigma A B) |
| 272 |
: P s |
| 273 |
:= match s with Rpair _ _ x b => H x b end. |
| 274 |
|
| 275 |
(* sort polymorphic exists (we could also make B sort poly) |
| 276 |
can't be a primitive record since the first projection isn't defined at all sorts *) |
| 277 |
Inductive sexists@{s;u|} (A:Type@{s;u}) (B:A -> Prop) : Prop |
| 278 |
:= sexist : forall a:A, B a -> sexists A B. |
| 279 |
|
| 280 |
(* we can eliminate to Prop *) |
| 281 |
Check sexists_ind. |
| 282 |
|
| 283 |
Inductive sigma3@{s s' s'';u v| } (A:Type@{s;u}) (P:A -> Type@{s';v}) : |
| 284 |
Type@{s'';max(u,v)} := |
| 285 |
exist3 : forall x:A, P x -> sigma3 A P. |
| 286 |
|
| 287 |
Arguments exist3 {_ _}. |
| 288 |
|
| 289 |
Definition π1@{s s';u v|} {A:Type@{s;u}} {P:A -> Type@{s';v}} (p : sigma3@{_ _ Type;_ _} A P) : A := |
| 290 |
match p return A with exist3 a _ => a end. |
| 291 |
|
| 292 |
End Inductives. |