|
| 1 | +module |
| 2 | + |
| 3 | +import Cslib.Foundations.Data.Relation |
| 4 | +import Cslib.Languages.LambdaCalculus.LocallyNameless.Stlc.Basic |
| 5 | +import Cslib.Languages.LambdaCalculus.LocallyNameless.Untyped.FullBeta |
| 6 | +import Cslib.Languages.LambdaCalculus.LocallyNameless.Untyped.StrongNorm |
| 7 | +import Cslib.Languages.LambdaCalculus.LocallyNameless.Untyped.LcAt |
| 8 | +import Cslib.Languages.LambdaCalculus.LocallyNameless.Untyped.MultiSubst |
| 9 | + |
| 10 | +namespace Cslib |
| 11 | + |
| 12 | +universe u v |
| 13 | + |
| 14 | +namespace LambdaCalculus.LocallyNameless.Stlc |
| 15 | + |
| 16 | +open Untyped Typing LambdaCalculus.LocallyNameless.Untyped.Term |
| 17 | + |
| 18 | +variable {Var : Type u} {Base : Type v} [DecidableEq Var] [HasFresh Var] |
| 19 | + |
| 20 | +open LambdaCalculus.LocallyNameless.Stlc |
| 21 | +open scoped Term |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +inductive saturated (S : Set (Term Var)) : Prop := |
| 26 | +| intro : (∀ M ∈ S, LC M) → |
| 27 | + (∀ M ∈ S, SN M) → |
| 28 | + (∀ M, neutral M → LC M → M ∈ S) → |
| 29 | + (∀ M N P, LC N → SN N → multiApp (M ^ N) P ∈ S → multiApp ((Term.abs M).app N) P ∈ S) → |
| 30 | + saturated S |
| 31 | + |
| 32 | + |
| 33 | +@[simp] |
| 34 | +def semanticMap (τ : Ty Base) : Set (Term Var) := |
| 35 | + match τ with |
| 36 | + | Ty.base _ => { t : Term Var | SN t ∧ LC t } |
| 37 | + | Ty.arrow τ₁ τ₂ => |
| 38 | + { t : Term Var | ∀ s : Term Var, s ∈ semanticMap τ₁ → (Term.app t s) ∈ semanticMap τ₂ } |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +def semanticMap_saturated (τ : Ty Base) : |
| 43 | + @saturated Var (semanticMap τ) := by |
| 44 | + induction τ |
| 45 | + · case base b => |
| 46 | + constructor |
| 47 | + · simp_all |
| 48 | + · simp_all |
| 49 | + · simp_all[neutral_sn] |
| 50 | + · intro M N P lc_N sn_N h_app |
| 51 | + constructor |
| 52 | + · simp_all[multiApp_sn] |
| 53 | + · have H := h_app.2 |
| 54 | + rw[multiApp_lc] at * |
| 55 | + grind[open_abs_lc] |
| 56 | + · case arrow τ₁ τ₂ ih₁ ih₂ => |
| 57 | + constructor |
| 58 | + · intro M hM |
| 59 | + have H := ih₁.3 (.fvar (fresh {})) (.fvar (fresh {})) (.fvar (fresh {})) |
| 60 | + specialize (hM (fvar (fresh {})) H) |
| 61 | + apply (ih₂.1) at hM |
| 62 | + cases hM |
| 63 | + assumption |
| 64 | + · intro M hM |
| 65 | + apply sn_app_left M (Term.fvar (fresh {})) |
| 66 | + · constructor |
| 67 | + · have H := ih₁.3 (.fvar (fresh {})) (.fvar (fresh {})) (.fvar (fresh {})) |
| 68 | + specialize (hM (fvar (fresh {})) H) |
| 69 | + apply ih₂.2 ; assumption |
| 70 | + · intro M hneut mlc s hs |
| 71 | + apply ih₂.3 |
| 72 | + · constructor |
| 73 | + · assumption |
| 74 | + · apply ih₁.2 |
| 75 | + assumption |
| 76 | + · constructor |
| 77 | + · assumption |
| 78 | + · apply ih₁.1 |
| 79 | + assumption |
| 80 | + · intro M N P lc_N sn_N h_app s hs |
| 81 | + apply ih₂.4 M N (s::P) |
| 82 | + · apply lc_N |
| 83 | + · apply sn_N |
| 84 | + · apply h_app |
| 85 | + assumption |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +def entails_context (Ns : Term.Environment Var) (Γ : Context Var (Ty Base)) := |
| 92 | + ∀ {x τ}, ⟨ x, τ ⟩ ∈ Γ → (multiSubst Ns (Term.fvar x)) ∈ semanticMap τ |
| 93 | + |
| 94 | +lemma entails_context_empty {Γ : Context Var (Ty Base)} : |
| 95 | + entails_context [] Γ := by |
| 96 | + intro x τ h_mem |
| 97 | + rw[multiSubst] |
| 98 | + apply (semanticMap_saturated τ).3 <;> constructor |
| 99 | + |
| 100 | + |
| 101 | +lemma entails_context_cons (Ns : Term.Environment Var) (Γ : Context Var (Ty Base)) |
| 102 | + (x : Var) (τ : Ty Base) (sub : Term Var) : |
| 103 | + x ∉ Ns.dom ∪ Ns.fv ∪ Γ.dom → |
| 104 | + sub ∈ semanticMap τ → |
| 105 | + entails_context Ns Γ → entails_context (⟨ x, sub ⟩ :: Ns) (⟨ x, τ ⟩ :: Γ) := by |
| 106 | + intro h_fresh h_mem h_entails y σ h_mem |
| 107 | + rw[multiSubst] |
| 108 | + rw[entails_context] at h_entails |
| 109 | + cases h_mem |
| 110 | + · case head => |
| 111 | + rw[multiSubst_fvar_fresh] |
| 112 | + · rw[subst_fvar] |
| 113 | + simp_all |
| 114 | + · simp_all |
| 115 | + · case tail h_mem => |
| 116 | + specialize (h_entails h_mem) |
| 117 | + rw [subst_fresh] |
| 118 | + · assumption |
| 119 | + · apply multiSubst_preserves_not_fvar |
| 120 | + apply List.mem_keys_of_mem at h_mem |
| 121 | + aesop |
| 122 | + |
| 123 | + |
| 124 | +def entails (Γ : Context Var (Ty Base)) (t : Term Var) (τ : Ty Base) := |
| 125 | + ∀ Ns, env_LC Ns → (entails_context Ns Γ) → (multiSubst Ns t) ∈ semanticMap τ |
| 126 | + |
| 127 | + |
| 128 | +theorem soundness {Γ : Context Var (Ty Base)} {t : Term Var} {τ : Ty Base} : |
| 129 | + Γ ⊢ t ∶ τ → entails Γ t τ := by |
| 130 | + intro derivation_t |
| 131 | + induction derivation_t |
| 132 | + · case var Γ xσ_mem_Γ => |
| 133 | + intro Ns lc_Ns hsat |
| 134 | + apply hsat xσ_mem_Γ |
| 135 | + · case' abs σ Γ t τ L IH derivation_t => |
| 136 | + intro Ns lc_Ns hsat s hsat_s |
| 137 | + rw[multiSubst_abs] |
| 138 | + apply (semanticMap_saturated _).4 _ _ [] |
| 139 | + · apply (semanticMap_saturated _).1 |
| 140 | + assumption |
| 141 | + · apply (semanticMap_saturated _).2 |
| 142 | + assumption |
| 143 | + · rw[multiApp] |
| 144 | + set x := fresh (t.fv ∪ L ∪ Ns.dom ∪ Ns.fv ∪ Context.dom Γ ∪ (multiSubst Ns t).fv) |
| 145 | + have hfresh : x ∉ t.fv ∪ L ∪ Ns.dom ∪ Ns.fv ∪ Context.dom Γ ∪ (multiSubst Ns t).fv := by apply fresh_notMem |
| 146 | + have hfreshL : x ∉ L := by simp_all |
| 147 | + have H1 := derivation_t x hfreshL |
| 148 | + rw[entails] at H1 |
| 149 | + specialize H1 (⟨x,s⟩ :: Ns) |
| 150 | + rw [multiSubst, multiSubst_open_var, ←subst_intro] at H1 |
| 151 | + · apply H1 |
| 152 | + · apply env_LC_cons |
| 153 | + · apply (semanticMap_saturated _).1 |
| 154 | + assumption |
| 155 | + · assumption |
| 156 | + · apply entails_context_cons <;> simp_all |
| 157 | + · simp_all |
| 158 | + · apply (semanticMap_saturated σ).1 |
| 159 | + assumption |
| 160 | + · simp_all |
| 161 | + · aesop |
| 162 | + · case app derivation_t derivation_t' IH IH' => |
| 163 | + intro Ns lc_Ns hsat |
| 164 | + rw[multiSubst_app] |
| 165 | + apply IH Ns lc_Ns hsat |
| 166 | + apply IH' Ns lc_Ns hsat |
| 167 | + |
| 168 | +theorem strong_norm {Γ} {t : Term Var} {τ : Ty Base} (der : Γ ⊢ t ∶ τ) : SN t := by |
| 169 | + have H := soundness der [] (by aesop) entails_context_empty |
| 170 | + apply (semanticMap_saturated τ).2 |
| 171 | + assumption |
| 172 | + |
| 173 | +end LambdaCalculus.LocallyNameless.Stlc |
| 174 | + |
| 175 | +end Cslib |
0 commit comments