Skip to content

Commit 575ff63

Browse files
committed
Change typing rules for lambda prod and letin
1 parent fe26a41 commit 575ff63

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1743
-1569
lines changed

common/theories/BasicAst.v

+2-1
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,10 @@ Notation TypUniv ty u := (Judge None ty (Some u)).
246246
Notation TermTypUniv tm ty u := (Judge (Some tm) ty (Some u)).
247247

248248
Notation j_vass na ty := (Typ ty (* na.(binder_relevance) *)).
249+
Notation j_vass_s na ty s := (TypUniv ty s (* na.(binder_relevance) *)).
249250
Notation j_vdef na b ty := (TermTyp b ty (* na.(binder_relevance) *)).
250251
Notation j_decl d := (TermoptTyp (decl_body d) (decl_type d) (* (decl_name d).(binder_relevance) *)).
251-
252+
Notation j_decl_s d s := (Judge (decl_body d) (decl_type d) s (* (decl_name d).(binder_relevance) *)).
252253

253254
Definition map_decl {term term'} (f : term -> term') (d : context_decl term) : context_decl term' :=
254255
{| decl_name := d.(decl_name);

common/theories/EnvironmentTyping.v

+120-35
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ Module EnvTyping (T : Term) (E : EnvironmentSig T) (TU : TermUtils T E).
318318

319319
(** Well-formedness of local environments embeds a sorting for each variable *)
320320

321-
Definition on_local_decl (P : context -> judgment -> Type) Γ d :=
322-
P Γ (j_decl d).
321+
Notation on_local_decl P Γ d :=
322+
(P Γ (j_decl d)).
323323

324324
Definition on_def_type (P : context -> judgment -> Type) Γ d :=
325325
P Γ (Typ d.(dtype)).
@@ -332,9 +332,13 @@ Module EnvTyping (T : Term) (E : EnvironmentSig T) (TU : TermUtils T E).
332332
Definition lift_wf_term wf_term (j : judgment) := option_default wf_term (j_term j) (unit : Type) × wf_term (j_typ j).
333333
Notation lift_wf_term1 wf_term := (fun (Γ : context) => lift_wf_term (wf_term Γ)).
334334

335+
Definition lift_wfu_term wf_term wf_univ (j : judgment) := option_default wf_term (j_term j) unit × wf_term (j_typ j) × option_default wf_univ (j_univ j) unit.
336+
335337
Definition lift_wfb_term wfb_term (j : judgment) := option_default wfb_term (j_term j) true && wfb_term (j_typ j).
336338
Notation lift_wfb_term1 wfb_term := (fun (Γ : context) => lift_wfb_term (wfb_term Γ)).
337339

340+
Definition lift_wfbu_term wfb_term wfb_univ (j : judgment) := option_default wfb_term (j_term j) true && wfb_term (j_typ j) && option_default wfb_univ (j_univ j) true.
341+
338342
Definition lift_sorting checking sorting : judgment -> Type :=
339343
fun j => option_default (fun tm => checking tm (j_typ j)) (j_term j) (unit : Type) ×
340344
∑ s, sorting (j_typ j) s × option_default (fun u => (u = s : Type)) (j_univ j) unit.
@@ -389,25 +393,52 @@ Module EnvTyping (T : Term) (E : EnvironmentSig T) (TU : TermUtils T E).
389393
destruct j_term; cbn in *; auto.
390394
Defined.
391395

396+
Lemma lift_wfbu_term_f_impl (P Q : term -> bool) tm t u :
397+
forall f fu,
398+
lift_wfbu_term P (P ∘ tSort) (Judge tm t u) ->
399+
(forall u, f (tSort u) = tSort (fu u)) ->
400+
(forall t, P t -> Q (f t)) ->
401+
lift_wfbu_term Q (Q ∘ tSort) (Judge (option_map f tm) (f t) (option_map fu u)).
402+
Proof.
403+
unfold lift_wfbu_term; cbn.
404+
intros. rtoProp.
405+
repeat split; auto.
406+
1: destruct tm; cbn in *; auto.
407+
destruct u; rewrite //= -H0 //. auto.
408+
Defined.
409+
392410
Lemma unlift_TermTyp {Pc Ps tm ty u} :
393411
lift_sorting Pc Ps (Judge (Some tm) ty u) ->
394412
Pc tm ty.
395413
Proof.
396414
apply fst.
397415
Defined.
398416

399-
Definition lift_sorting_extract {c s tm ty} (w : lift_sorting c s (TermoptTyp tm ty)) :
417+
Definition unlift_TypUniv {Pc Ps tm ty u} :
418+
lift_sorting Pc Ps (Judge tm ty (Some u)) ->
419+
Ps ty u
420+
:= fun H => eq_rect_r _ H.2.π2.1 H.2.π2.2.
421+
422+
Definition lift_sorting_extract {c s tm ty u} (w : lift_sorting c s (Judge tm ty u)) :
400423
lift_sorting c s (Judge tm ty (Some w.2.π1)) :=
401424
(w.1, existT _ w.2.π1 (w.2.π2.1, eq_refl)).
402425

403426
Lemma lift_sorting_forget_univ {Pc Ps tm ty u} :
404-
lift_sorting Pc Ps (Judge tm ty (Some u)) ->
427+
lift_sorting Pc Ps (Judge tm ty u) ->
405428
lift_sorting Pc Ps (TermoptTyp tm ty).
406429
Proof.
407430
intros (? & ? & ? & ?).
408431
repeat (eexists; tea).
409432
Qed.
410433

434+
Lemma lift_sorting_forget_body {Pc Ps tm ty u} :
435+
lift_sorting Pc Ps (Judge tm ty u) ->
436+
lift_sorting Pc Ps (Judge None ty u).
437+
Proof.
438+
intros (? & ? & ? & ?).
439+
repeat (eexists; tea).
440+
Qed.
441+
411442
Lemma lift_sorting_ex_it_impl_gen {Pc Qc Ps Qs} {tm tm' : option term} {t t' : term} :
412443
forall tu: lift_sorting Pc Ps (TermoptTyp tm t),
413444
let s := tu.2.π1 in
@@ -533,6 +564,15 @@ Module EnvTyping (T : Term) (E : EnvironmentSig T) (TU : TermUtils T E).
533564
apply lift_typing_f_impl with (1 := HT) => //.
534565
Qed.
535566

567+
Lemma lift_typing_mapu {P} f fu {tm ty u} :
568+
lift_typing0 (fun t T => P (f t) (f T)) (Judge tm ty u) ->
569+
(forall u, f (tSort u) = tSort (fu u)) ->
570+
lift_typing0 P (Judge (option_map f tm) (f ty) (option_map fu u)).
571+
Proof.
572+
intros HT.
573+
eapply lift_typing_fu_impl with (1 := HT) => //.
574+
Qed.
575+
536576
Lemma lift_sorting_impl {Pc Qc Ps Qs j} :
537577
lift_sorting Pc Ps j ->
538578
(forall t T, Pc t T -> Qc t T) ->
@@ -563,12 +603,12 @@ Module EnvTyping (T : Term) (E : EnvironmentSig T) (TU : TermUtils T E).
563603

564604
| localenv_cons_abs Γ na t :
565605
All_local_env Γ ->
566-
typing Γ (Typ t) ->
606+
typing Γ (j_vass na t) ->
567607
All_local_env (Γ ,, vass na t)
568608

569609
| localenv_cons_def Γ na b t :
570610
All_local_env Γ ->
571-
typing Γ (TermTyp b t) ->
611+
typing Γ (j_vdef na b t) ->
572612
All_local_env (Γ ,, vdef na b t).
573613

574614
Derive Signature NoConfusion for All_local_env.
@@ -621,10 +661,12 @@ Module EnvTyping (T : Term) (E : EnvironmentSig T) (TU : TermUtils T E).
621661

622662
Lemma All_local_env_impl_gen (P Q : context -> judgment -> Type) l :
623663
All_local_env P l ->
624-
(forall Γ bo ty, P Γ (TermoptTyp bo ty) -> Q Γ (TermoptTyp bo ty)) ->
664+
(forall Γ decl, P Γ (j_decl decl) -> Q Γ (j_decl decl)) ->
625665
All_local_env Q l.
626666
Proof.
627-
induction 1; intros; simpl; econstructor; eauto.
667+
intros H X.
668+
induction H using All_local_env_ind1. 1: constructor.
669+
apply All_local_env_snoc; auto.
628670
Qed.
629671

630672
Lemma All_local_env_impl (P Q : context -> judgment -> Type) l :
@@ -671,6 +713,14 @@ Module EnvTyping (T : Term) (E : EnvironmentSig T) (TU : TermUtils T E).
671713
- now eapply IHΓ.
672714
Defined.
673715

716+
Lemma All_local_env_cst {P Γ} : All_local_env (fun _ => P) Γ <~> All (fun d => P (j_decl d)) Γ.
717+
Proof.
718+
split.
719+
- induction 1 using All_local_env_ind1; constructor => //.
720+
- induction 1. 1: constructor.
721+
apply All_local_env_snoc => //.
722+
Defined.
723+
674724
Section All_local_env_rel.
675725

676726
Definition All_local_rel P Γ Γ'
@@ -685,13 +735,13 @@ Module EnvTyping (T : Term) (E : EnvironmentSig T) (TU : TermUtils T E).
685735
:= All_local_env_snoc.
686736

687737
Definition All_local_rel_abs {P Γ Γ' A na} :
688-
All_local_rel P Γ Γ' -> P (Γ ,,, Γ') (Typ A)
738+
All_local_rel P Γ Γ' -> P (Γ ,,, Γ') (j_vass na A)
689739
-> All_local_rel P Γ (Γ',, vass na A)
690740
:= localenv_cons.
691741

692742
Definition All_local_rel_def {P Γ Γ' t A na} :
693743
All_local_rel P Γ Γ' ->
694-
P (Γ ,,, Γ') (TermTyp t A) ->
744+
P (Γ ,,, Γ') (j_vdef na t A) ->
695745
All_local_rel P Γ (Γ',, vdef na t A)
696746
:= localenv_cons.
697747

@@ -803,15 +853,15 @@ Module EnvTyping (T : Term) (E : EnvironmentSig T) (TU : TermUtils T E).
803853
| localenv_over_cons_abs Γ na t
804854
(all : All_local_env (lift_sorting1 checking sorting) Γ) :
805855
All_local_env_over_sorting Γ all ->
806-
forall (tu : lift_sorting1 checking sorting Γ (Typ t))
856+
forall (tu : lift_sorting1 checking sorting Γ (j_vass na t))
807857
(Hs: sproperty Γ all _ _ tu.2.π2.1),
808858
All_local_env_over_sorting (Γ ,, vass na t)
809859
(localenv_cons_abs all tu)
810860

811861
| localenv_over_cons_def Γ na b t
812862
(all : All_local_env (lift_sorting1 checking sorting) Γ) :
813863
All_local_env_over_sorting Γ all ->
814-
forall (tu : lift_sorting1 checking sorting Γ (TermTyp b t))
864+
forall (tu : lift_sorting1 checking sorting Γ (j_vdef na b t))
815865
(Hc: cproperty Γ all _ _ tu.1)
816866
(Hs: sproperty Γ all _ _ tu.2.π2.1),
817867
All_local_env_over_sorting (Γ ,, vdef na b t)
@@ -919,24 +969,24 @@ Module EnvTyping (T : Term) (E : EnvironmentSig T) (TU : TermUtils T E).
919969
| None => fun w => 0
920970
end w.
921971

922-
Section lift_sorting_size.
972+
Section lift_sorting_size_gen.
923973
Context {checking : term -> term -> Type}.
924974
Context {sorting : term -> Universe.t -> Type}.
925975
Context (csize : forall (t T : term), checking t T -> size).
926976
Context (ssize : forall (t : term) (u : Universe.t), sorting t u -> size).
927977

928-
Definition lift_sorting_size j (w : lift_sorting checking sorting j) : size :=
929-
option_default_size (fun tm => csize tm _) (j_term j) w.1 + ssize _ _ w.2.π2.1.
978+
Definition lift_sorting_size_gen base j (w : lift_sorting checking sorting j) : size :=
979+
base + option_default_size (fun tm => csize tm _) (j_term j) w.1 + ssize _ _ w.2.π2.1.
930980

931981

932-
Lemma lift_sorting_size_impl {Qc Qs j} :
982+
Lemma lift_sorting_size_gen_impl {Qc Qs j} :
933983
forall tu: lift_sorting checking sorting j,
934-
(forall t T, forall Hty: checking t T, csize _ _ Hty <= lift_sorting_size _ tu -> Qc t T) ->
935-
(forall t u, forall Hty: sorting t u, ssize _ _ Hty <= lift_sorting_size _ tu -> Qs t u) ->
984+
(forall t T, forall Hty: checking t T, csize _ _ Hty <= lift_sorting_size_gen 0 _ tu -> Qc t T) ->
985+
(forall t u, forall Hty: sorting t u, ssize _ _ Hty <= lift_sorting_size_gen 0 _ tu -> Qs t u) ->
936986
lift_sorting Qc Qs j.
937987
Proof.
938988
intros (Htm & s & Hty & es) HPQc HPQs.
939-
unfold lift_sorting_size in *; cbn in *.
989+
unfold lift_sorting_size_gen in *; cbn in *.
940990
repeat (eexists; tea).
941991
- destruct (j_term j) => //=.
942992
eapply HPQc with (Hty := Htm); cbn.
@@ -945,29 +995,45 @@ Module EnvTyping (T : Term) (E : EnvironmentSig T) (TU : TermUtils T E).
945995
lia.
946996
Qed.
947997

948-
End lift_sorting_size.
998+
End lift_sorting_size_gen.
949999

950-
Definition on_def_type_sorting_size {c s} (ssize : forall Γ t u, s Γ t u -> size)
1000+
Definition on_def_type_size_gen {c s} (ssize : forall Γ t u, s Γ t u -> size) base
9511001
Γ d (w : on_def_type (lift_sorting1 c s) Γ d) : size :=
952-
ssize _ _ _ w.2.π2.1.
953-
Definition on_def_body_sorting_size {c s} (csize : forall Γ t u, c Γ t u -> size) (ssize : forall Γ t u, s Γ t u -> size)
1002+
base + ssize _ _ _ w.2.π2.1.
1003+
Definition on_def_body_size_gen {c s} (csize : forall Γ t u, c Γ t u -> size) (ssize : forall Γ t u, s Γ t u -> size) base
9541004
types Γ d (w : on_def_body (lift_sorting1 c s) types Γ d) : size :=
955-
csize _ _ _ w.1 + ssize _ _ _ w.2.π2.1.
1005+
base + csize _ _ _ w.1 + ssize _ _ _ w.2.π2.1.
9561006

1007+
Notation lift_sorting_size csize ssize := (lift_sorting_size_gen csize ssize 1).
9571008
Notation typing_sort_size typing_size := (fun t s (tu: typing_sort _ t s) => typing_size t (tSort s) tu).
958-
Notation lift_typing_size typing_size := (lift_sorting_size typing_size (typing_sort_size typing_size)).
1009+
Notation lift_typing_size typing_size := (lift_sorting_size_gen typing_size (typing_sort_size typing_size) 0).
9591010
Notation typing_sort_size1 typing_size := (fun Γ t s (tu: typing_sort1 _ Γ t s) => typing_size Γ t (tSort s) tu).
960-
Notation on_def_type_size typing_size := (on_def_type_sorting_size (typing_sort_size1 typing_size)).
961-
Notation on_def_body_size typing_size := (on_def_body_sorting_size typing_size (typing_sort_size1 typing_size)).
1011+
Notation on_def_type_sorting_size ssize := (on_def_type_size_gen ssize 1).
1012+
Notation on_def_type_size typing_size := (on_def_type_size_gen (typing_sort_size1 typing_size) 0).
1013+
Notation on_def_body_sorting_size csize ssize := (on_def_body_size_gen csize ssize 1).
1014+
Notation on_def_body_size typing_size := (on_def_body_size_gen typing_size (typing_sort_size1 typing_size) 0).
9621015
(* Will probably not pass the guard checker if in a list, must be unrolled like in on_def_* *)
9631016

964-
Lemma lift_typing_size_impl {P Q Psize j} :
1017+
Lemma lift_sorting_size_impl {checking sorting Qc Qs j} csize ssize :
1018+
forall tu: lift_sorting checking sorting j,
1019+
(forall t T, forall Hty: checking t T, csize _ _ Hty < lift_sorting_size csize ssize _ tu -> Qc t T) ->
1020+
(forall t u, forall Hty: sorting t u, ssize _ _ Hty < lift_sorting_size csize ssize _ tu -> Qs t u) ->
1021+
lift_sorting Qc Qs j.
1022+
Proof.
1023+
intros tu Xc Xs.
1024+
eapply lift_sorting_size_gen_impl with (tu := tu).
1025+
all: intros.
1026+
1: eapply Xc. 2: eapply Xs.
1027+
all: apply le_n_S, H.
1028+
Qed.
1029+
1030+
Lemma lift_typing_size_impl {P Q j} Psize :
9651031
forall tu: lift_typing0 P j,
9661032
(forall t T, forall Hty: P t T, Psize _ _ Hty <= lift_typing_size Psize _ tu -> Q t T) ->
9671033
lift_typing0 Q j.
9681034
Proof.
9691035
intros.
970-
eapply lift_sorting_size_impl with (csize := Psize).
1036+
eapply lift_sorting_size_gen_impl with (csize := Psize).
9711037
all: intros t T; apply X.
9721038
Qed.
9731039

@@ -1109,17 +1175,19 @@ Module GlobalMaps (T: Term) (E: EnvironmentSig T) (TU : TermUtils T E) (ET: EnvT
11091175
Fixpoint type_local_ctx Σ (Γ Δ : context) (u : Universe.t) : Type :=
11101176
match Δ with
11111177
| [] => wf_universe Σ u
1112-
| {| decl_body := None; decl_type := t |} :: Δ => type_local_ctx Σ Γ Δ u × P Σ (Γ ,,, Δ) (TypUniv t u)
1113-
| {| decl_body := Some b; decl_type := t |} :: Δ => type_local_ctx Σ Γ Δ u × P Σ (Γ ,,, Δ) (TermTyp b t)
1178+
| {| decl_name := na; decl_body := None; decl_type := t |} :: Δ =>
1179+
type_local_ctx Σ Γ Δ u × P Σ (Γ ,,, Δ) (TypUniv t u (* na.(binder_relevance) *))
1180+
| {| decl_body := Some _; |} as d :: Δ =>
1181+
type_local_ctx Σ Γ Δ u × P Σ (Γ ,,, Δ) (j_decl d)
11141182
end.
11151183

11161184
Fixpoint sorts_local_ctx Σ (Γ Δ : context) (us : list Universe.t) : Type :=
11171185
match Δ, us with
11181186
| [], [] => unit
1119-
| {| decl_body := None; decl_type := t |} :: Δ, u :: us =>
1120-
sorts_local_ctx Σ Γ Δ us × P Σ (Γ ,,, Δ) (TypUniv t u)
1121-
| {| decl_body := Some b; decl_type := t |} :: Δ, us =>
1122-
sorts_local_ctx Σ Γ Δ us × P Σ (Γ ,,, Δ) (TermTyp b t)
1187+
| {| decl_name := na; decl_body := None; decl_type := t |} :: Δ, u :: us =>
1188+
sorts_local_ctx Σ Γ Δ us × P Σ (Γ ,,, Δ) (TypUniv t u (* na.(binder_relevance) *))
1189+
| {| decl_body := Some _ |} as d :: Δ, us =>
1190+
sorts_local_ctx Σ Γ Δ us × P Σ (Γ ,,, Δ) (j_decl d)
11231191
| _, _ => False
11241192
end.
11251193

@@ -1640,6 +1708,23 @@ Module GlobalMaps (T: Term) (E: EnvironmentSig T) (TU : TermUtils T E) (ET: EnvT
16401708
Definition on_global_env_ext (Σ : global_env_ext) :=
16411709
on_global_env Σ.1 × on_udecl Σ.(universes) Σ.2.
16421710

1711+
Lemma on_global_env_ext_empty_ext g :
1712+
on_global_env g -> on_global_env_ext (empty_ext g).
1713+
Proof.
1714+
intro H; split => //.
1715+
unfold empty_ext, snd. repeat split.
1716+
- unfold levels_of_udecl. intros x e. lsets.
1717+
- unfold constraints_of_udecl. intros x e. csets.
1718+
- unfold satisfiable_udecl, univs_ext_constraints, constraints_of_udecl, fst_ctx, fst => //.
1719+
destruct H as ((cstrs & _ & consistent) & decls).
1720+
destruct consistent; eexists.
1721+
intros v e. specialize (H v e); tea.
1722+
- unfold valid_on_mono_udecl, constraints_of_udecl, consistent_extension_on.
1723+
intros v sat; exists v; split.
1724+
+ intros x e. csets.
1725+
+ intros x e => //.
1726+
Qed.
1727+
16431728
End GlobalMaps.
16441729

16451730
Arguments cstr_args_length {_ Pcmp P Σ mdecl i idecl ind_indices cdecl cunivs}.

0 commit comments

Comments
 (0)