|
| 1 | +From Coq Require Import Utf8. |
| 2 | +From Coq Require Import Arith Lia. |
| 3 | + |
| 4 | +Inductive sort : Type := |
| 5 | +| Star : sort |
| 6 | +| Box : sort. |
| 7 | + |
| 8 | +Inductive term (n : nat) : Type := |
| 9 | +| Var : {k | k < n} → term n |
| 10 | +| Pi : term n → term (S n) → term n |
| 11 | +| Abs : term n → term (S n) → term n |
| 12 | +| App : term n → term n → term n |
| 13 | +| Srt : sort → term n |
| 14 | +. |
| 15 | + |
| 16 | +Arguments Var {n}. |
| 17 | +Arguments Pi {n}. |
| 18 | +Arguments Abs {n}. |
| 19 | +Arguments App {n}. |
| 20 | +Arguments Srt {n}. |
| 21 | + |
| 22 | +Definition weaken {k : nat} (i : {i | i < k}) : {i | i < S k} := |
| 23 | + exist _ (proj1_sig i) (Nat.lt_lt_succ_r _ _ (proj2_sig i)). |
| 24 | + |
| 25 | +Definition lift |
| 26 | + {k n : nat} (σ : {i | i < k} → {i | i < n}) |
| 27 | + (i : {i | i < S k}) : {i | i < S n} |
| 28 | + := |
| 29 | + match lt_dec (proj1_sig i) k with |
| 30 | + | left Hi => weaken (σ (exist _ (proj1_sig i) Hi)) |
| 31 | + | right _ => exist _ n (Nat.lt_succ_diag_r n) |
| 32 | + end. |
| 33 | + |
| 34 | +Lemma sig_lt_ext {k : nat} (p q : {i | i < k}) : |
| 35 | + proj1_sig p = proj1_sig q → p = q. |
| 36 | +Proof. |
| 37 | + destruct p, q. |
| 38 | + simpl. |
| 39 | + intros ->. |
| 40 | + f_equal. |
| 41 | + apply le_unique. |
| 42 | +Qed. |
| 43 | + |
| 44 | +Lemma lift_lt {k n : nat} (σ : {i | i < k} → {i | i < n}) (i : nat) |
| 45 | + : ∀ Hik HiSk, proj1_sig (lift σ (exist _ i HiSk)) = proj1_sig (σ (exist _ i Hik)). |
| 46 | +Proof. |
| 47 | + intros Hik HiSk. |
| 48 | + simpl. |
| 49 | + unfold lift. |
| 50 | + destruct (lt_dec _ _); [|now contradict Hik]. |
| 51 | + simpl. |
| 52 | + do 2 f_equal. |
| 53 | + now apply sig_lt_ext. |
| 54 | +Qed. |
| 55 | + |
| 56 | +Lemma lift_eq {k n : nat} (σ : {i | i < k} → {i | i < n}) |
| 57 | + : ∀ Hk, proj1_sig (lift σ (exist _ k Hk)) = n. |
| 58 | +Proof. |
| 59 | + intros. |
| 60 | + unfold lift. |
| 61 | + destruct (lt_dec _ _); simpl in *; [lia|reflexivity]. |
| 62 | +Qed. |
| 63 | + |
| 64 | +Fixpoint subst {k n : nat} (σ : {i | i < k} → {i | i < n}) (t : term k) : term n := |
| 65 | + match t with |
| 66 | + | Var i => Var (σ i) |
| 67 | + | Pi ty fam => Pi (subst σ ty) (subst (lift σ) fam) |
| 68 | + | Abs ty tm => Abs (subst σ ty) (subst (lift σ) tm) |
| 69 | + | App tm1 tm2 => App (subst σ tm1) (subst σ tm2) |
| 70 | + | Srt s => Srt s |
| 71 | + end. |
| 72 | + |
| 73 | +Definition lift_bind {k n : nat} (σ : {i | i < k} → term n) (i : {i | i < S k}) : term (S n) := |
| 74 | + match lt_dec (proj1_sig i) k with |
| 75 | + | left Hi => subst weaken (σ (exist _ (proj1_sig i) Hi)) |
| 76 | + | right _ => Var (exist _ n (Nat.lt_succ_diag_r n)) |
| 77 | + end. |
| 78 | + |
| 79 | +Fixpoint bind {k n : nat} (σ : {i | i < k} → term n) (t : term k) : term n |
| 80 | + := |
| 81 | + match t with |
| 82 | + | Var i => σ i |
| 83 | + | Pi ty fam => Pi (bind σ ty) (bind (lift_bind σ) fam) |
| 84 | + | Abs ty tm => Abs (bind σ ty) (bind (lift_bind σ) tm) |
| 85 | + | App tm1 tm2 => App (bind σ tm1) (bind σ tm2) |
| 86 | + | Srt s => Srt s |
| 87 | + end. |
| 88 | + |
| 89 | +Definition bind_first {k : nat} (t : term k) (i : {i | i < S k}) : term k |
| 90 | + := |
| 91 | + match lt_dec (proj1_sig i) k with |
| 92 | + | left Hi => Var (exist _ (proj1_sig i) Hi) |
| 93 | + | right _ => t |
| 94 | + end. |
| 95 | + |
| 96 | +Inductive conv {n : nat} : term n → term n → Prop := |
| 97 | +| conv_var (k : {k | k < n}) : conv (Var k) (Var k) |
| 98 | +| conv_srt (s : sort) : conv (Srt s) (Srt s) |
| 99 | +| conv_pi (A1 A2 : term n) (B1 B2 : term (S n)) : |
| 100 | + conv A1 A2 → conv B1 B2 → conv (Pi A1 B1) (Pi A2 B2) |
| 101 | +| conv_abs (A1 A2 : term n) (B1 B2 : term (S n)) : |
| 102 | + conv A1 A2 → conv B1 B2 → conv (Abs A1 B1) (Abs A2 B2) |
| 103 | +| conv_app (A1 A2 B1 B2 : term n) : |
| 104 | + conv A1 A2 → conv B1 B2 → conv (App A1 B1) (App A2 B2) |
| 105 | +| conv_redex_l (A1 A2 A3 : term n) (B : term (S n)) : |
| 106 | + conv (bind (bind_first A2) B) A3 → conv (App (Abs A1 B) A2) A3 |
| 107 | +| conv_redex_r (A1 A2 A3 : term n) (B : term (S n)) : |
| 108 | + conv A3 (bind (bind_first A2) B) → conv A3 (App (Abs A1 B) A2) |
| 109 | +. |
0 commit comments