LPTerm.v 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Require Import PeanoNat.
Require Import List.

Parameter Var : Set.

Axiom var_dec : forall (x y:Var), {x = y} + {x <> y}.

Axiom var_inf : forall l, exists x:Var, ~ (In x l).

(* ************ Terms ************ *)

Inductive term : Set :=
| TKind  : term
| TType  : term
| TSymb  : Var  -> term
| TBound : nat    -> term
| TPi    : term -> term -> term
| TAbs   : term -> term -> term
| TApp   : term -> term -> term.

Notation "'#' x" := (TSymb  x) (at level 20).
Notation "'?' x" := (TBound x) (at level 20).
Notation "'Π' A '~' B" := (TPi A B) (at level 22, right associativity).
Notation "'λ' A '~' u" := (TAbs A u) (at level 22).
Notation "t '@' u" := (TApp t u) (at level 21, left associativity).

Definition type : term := TType.
Definition kind : term := TKind.

Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
(* ************ Subterms ************ *)

Reserved Notation "t '▷' u" (at level 85, u at level 25).
Inductive R_Subterm : term -> term -> Prop :=
| HSubRefl : forall x    , x  x
| HSubPi1  : forall x A B, A  x -> (Π A ~ B)  x
| HSubPi2  : forall x A B, B  x -> (Π A ~ B)  x
| HSubAbs1 : forall x A t, A  x -> (λ A ~ t)  x
| HSubAbs2 : forall x A t, t  x -> (λ A ~ t)  x
| HSubApp1 : forall x t u, t  x -> (t @ u)    x
| HSubApp2 : forall x t u, u  x -> (t @ u)    x
where "t '▷' u" := (R_Subterm t u).

Lemma ST_trans : forall t u v, t  u -> u  v -> t  v.
Proof.
  intros. generalize dependent v. induction H; subst; intros.
  - easy.
  - apply HSubPi1. apply IHR_Subterm. easy.
  - apply HSubPi2. apply IHR_Subterm. easy.
  - apply HSubAbs1. apply IHR_Subterm. easy.
  - apply HSubAbs2. apply IHR_Subterm. easy.
  - apply HSubApp1. apply IHR_Subterm. easy.
  - apply HSubApp2. apply IHR_Subterm. easy.
Qed.

Definition close (t:term) : Prop := forall u k, t  u -> u <> TBound k.

Theorem close_subterm_comp : forall t u, t  u -> close t -> close u.
Proof. intros. intro. intros. apply H0. apply (ST_trans _ _ _ H H1). Qed.

Gaspard Ferey's avatar
Gaspard Ferey committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74

(* ************ Free variables ************ *)

Fixpoint FV (t:term) : list Var :=
  match t with
  | TKind    => nil
  | TType    => nil
  | TSymb v  => cons v nil
  | TBound _ => nil
  | TPi  A B => (FV A) ++ (FV B)
  | TAbs A B => (FV A) ++ (FV B)
  | TApp t u => (FV t) ++ (FV u)
  end.

Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
75
Definition free_var_free (t:term) := forall v, ~ (t  #v).
Gaspard Ferey's avatar
Gaspard Ferey committed
76

Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
77 78 79
Theorem free_var_free_subterm : forall t u, t  u -> free_var_free t -> free_var_free u.
Proof.
Admitted.
Gaspard Ferey's avatar
Gaspard Ferey committed
80 81 82

Theorem closed_FV : forall t, (free_var_free t) <-> (FV t) = nil.
Proof.
Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
83
  (*
Gaspard Ferey's avatar
Gaspard Ferey committed
84 85
  intro. split ; intros.
  - induction t ; intros ; try easy.
Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
86
    + destruct (H v). constructor.
Gaspard Ferey's avatar
Gaspard Ferey committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    + destruct H. simpl.
      assert (FV t1 = nil); auto.
      assert (FV t2 = nil); auto.
      rewrite H1. rewrite H2. easy.
    + destruct H. simpl.
      assert (FV t1 = nil); auto.
      assert (FV t2 = nil); auto.
      rewrite H1. rewrite H2. easy.
  - induction t ; try easy.
    + econstructor.
      * apply IHt1. apply (proj1 (app_eq_nil (FV t1) (FV t2) H)).
      * apply IHt2. apply (proj2 (app_eq_nil (FV t1) (FV t2) H)).
    + econstructor.
      * apply IHt1. apply (proj1 (app_eq_nil (FV t1) (FV t2) H)).
      * apply IHt2. apply (proj2 (app_eq_nil (FV t1) (FV t2) H)).
    + econstructor.
      * apply IHt1. apply (proj1 (app_eq_nil (FV t1) (FV t2) H)).
      * apply IHt2. apply (proj2 (app_eq_nil (FV t1) (FV t2) H)).
Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
105 106
*)
Admitted.
Gaspard Ferey's avatar
Gaspard Ferey committed
107 108


109 110 111 112 113 114 115 116
(* ************ Contexts ************ *)

Inductive signature : Set :=
| SEmpty : signature
| SVar   : Var -> term -> signature -> signature
| SRel   : term -> term -> signature -> signature.

Notation "[ ]" := SEmpty (at level 0).
Gaspard Ferey's avatar
Gaspard Ferey committed
117 118
Notation "Σ ',' x ':' A" := (SVar x A Σ) (at level 50, x at level 25, A at level 25).
Notation "Σ ',' t '≡' u" := (SRel t u Σ) (at level 50, t at level 25, u at level 25).
119 120 121

Definition local_context : Set := list term.

Gaspard Ferey's avatar
Gaspard Ferey committed
122
Notation "Σ ',' T" := (cons T Σ) (at level 50, T at level 25).
123

Gaspard Ferey's avatar
Gaspard Ferey committed
124
Definition pop_context (Γ:local_context) : local_context := List.tl Γ.
125

Gaspard Ferey's avatar
Gaspard Ferey committed
126 127
Fixpoint defined_symb (Σ:signature) : list Var :=
  match Σ with
128
  | SEmpty     => nil
Gaspard Ferey's avatar
Gaspard Ferey committed
129 130
  | SVar x _ Σ => cons x (defined_symb Σ)
  | SRel _ _ Σ => (defined_symb Σ)
131 132
  end.

Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
133
Reserved Notation "x ':' A '∈' Σ" (at level 85, A at level 25, Σ at level 50).
134
Inductive InCtx : signature -> Var -> term -> Prop :=
Gaspard Ferey's avatar
Gaspard Ferey committed
135 136 137 138
| INow      : forall Σ x A, x : A  (Σ, x : A)
| IAfterV   : forall Σ A x y B, x : A  Σ -> x <> y -> x : A  (Σ, y : B)
| IAfterR   : forall Σ A x t u, x : A  Σ -> x : A  (Σ, t  u)
where "x ':' A ∈ Σ" := (InCtx Σ x A).
139

Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
140
Reserved Notation "t '≡' u '∈' Σ" (at level 85, u at level 25, Σ at level 50).
141
Inductive InRelCtx : signature -> term -> term -> Prop :=
Gaspard Ferey's avatar
Gaspard Ferey committed
142 143 144 145
| IRNow      : forall Σ t u, t  u  (Σ, t  u)
| IRAfterV   : forall Σ t u y B, t  u  Σ -> t  u  (Σ, y : B)
| IRAfterR   : forall Σ t u v w, t  u  Σ -> t  u  (Σ, v  w)
where "t '≡' u ∈ Σ" := (InRelCtx Σ t u).
146

Gaspard Ferey's avatar
Gaspard Ferey committed
147 148
Definition Defined_var Σ v := exists T, v : T  Σ.
Definition   Fresh_var Σ v := ~ (Defined_var Σ v).
149

Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
150 151
Notation "x '∈' Σ" := (Defined_var Σ x) (at level 85, Σ at level 50).
Notation "x '∉' Σ" := (Fresh_var   Σ x) (at level 85, Σ at level 50).
152

Gaspard Ferey's avatar
Gaspard Ferey committed
153
Theorem defined_eq : forall Σ v, Defined_var Σ v <-> In v (defined_symb Σ).
154 155 156 157 158 159 160
Proof.
  intros.
  split; intros.
  - induction H. induction H.
    + left. easy.
    + right. easy.
    + easy.
Gaspard Ferey's avatar
Gaspard Ferey committed
161
  - induction Σ.
162 163 164
    + contradiction H.
    + destruct (var_dec v v0).
      * subst. econstructor. econstructor.
Gaspard Ferey's avatar
Gaspard Ferey committed
165
      * assert (v  Σ). apply IHΣ. destruct H. contradiction n. easy. easy.
166
        destruct H0. econstructor. apply IAfterV. apply H0. easy.
Gaspard Ferey's avatar
Gaspard Ferey committed
167
    + assert (v  Σ). apply IHΣ. exact H.
168 169 170 171 172 173
      destruct H0. econstructor. apply IAfterR. apply H0.
Qed.


(* ************ Substitution ************ *)

Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
Fixpoint shift (k:nat) (t:term) :=
  match t with
  | TBound n   => TBound (n + k)
  | TApp l r   => TApp (shift k l) (shift k r)
  | TAbs ty te => TAbs (shift k ty) (shift (S k) te)
  | TPi  ty te => TPi  (shift k ty) (shift (S k) te)
  | _          => t
  end.

Theorem close_shift : forall t, close t -> forall k, shift k t = t.
Proof.
  intro. intro. induction t; intros; simpl; try easy.
  - contradict H. intro. apply (H (? n) n (HSubRefl (? n))). easy.
  - erewrite (IHt1 _ _). erewrite (IHt2 _ _). easy.
  - erewrite (IHt1 _ _). erewrite (IHt2 _ _). easy.
  - erewrite (IHt1 _ _). erewrite (IHt2 _ _). easy.
  Unshelve.
  eapply (close_subterm_comp _ _ _ H).
  eapply (close_subterm_comp _ _ _ H).
  eapply (close_subterm_comp _ _ _ H).
  eapply (close_subterm_comp _ _ _ H).
  eapply (close_subterm_comp _ _ _ H).
  eapply (close_subterm_comp _ _ _ H).
  Unshelve.
  apply HSubPi1 ; apply HSubRefl.
  apply HSubPi2 ; apply HSubRefl.
  apply HSubAbs1; apply HSubRefl.
  apply HSubAbs2; apply HSubRefl.
  apply HSubApp1; apply HSubRefl.
  apply HSubApp2; apply HSubRefl.
Qed.

Theorem ST_shift_r : forall t u k, close u -> t  u -> shift k t  u.
Proof.
Admitted.

Theorem ST_shift_l : forall t u k, close u -> shift k t  u -> t  u.
Proof.
Admitted.
213 214 215

Fixpoint subst_k (t u : term) (k : nat) :=
  match t with
Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
216
  | TBound i   => if Nat.eqb k i then shift k u else t
217 218 219
  | TApp l r   => TApp (subst_k l  u k) (subst_k r  u k    )
  | TAbs ty te => TAbs (subst_k ty u k) (subst_k te u (S k))
  | TPi ty te  => TPi  (subst_k ty u k) (subst_k te u (S k))
Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
220
  | _          => t
221 222 223
  end.
Notation "t '[' k '<-' u ']'" := (subst_k t u k) (at level 24).

Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
224

225 226 227 228 229 230 231 232 233 234 235
Definition subst (t u : term) := t [ 0 <- u ].
Notation "t '[' v ']'" := (subst t v) (at level 24).

Theorem subst_k_FV : forall t u v k, subst_k t u k  # v ->  t  (# v)  \/  u  (# v).
Proof.
  intro.
  induction t; intros.
  - inversion H.
  - inversion H.
  - inversion H; subst. left. easy.
  - destruct (k =? n) eqn:H3; subst.
Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
236
    + right. simpl in H. rewrite H3 in H. eapply ST_shift_l. intro. intros. inversion H0. easy. apply H.
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    + simpl in H. rewrite H3 in H. inversion H.
  - inversion H; subst.
    + pose proof (IHt1 _ _ _ H3).
      destruct H0.
      * left. apply HSubPi1. easy.
      * right. easy.
    + pose proof (IHt2 _ _ _ H3).
      destruct H0.
      * left. apply HSubPi2. easy.
      * right. easy.
  - inversion H; subst.
    + pose proof (IHt1 _ _ _ H3).
      destruct H0.
      * left. apply HSubAbs1. easy.
      * right. easy.
    + pose proof (IHt2 _ _ _ H3).
      destruct H0.
      * left. apply HSubAbs2. easy.
      * right. easy.
  - inversion H; subst.
    + pose proof (IHt1 _ _ _ H3).
      destruct H0.
      * left. apply HSubApp1. easy.
      * right. easy.
    + pose proof (IHt2 _ _ _ H3).
      destruct H0.
      * left. apply HSubApp2. easy.
      * right. easy.
Qed.  


(* ************ Locally named variable ************ *)

Fixpoint close_k (t : term) (k : nat) (z : Var) : term :=
  match t with
  | TSymb  x   => if var_dec x z then TBound k else t
  | TApp l r   => TApp (close_k l  k z) (close_k r  k     z)
  | TAbs ty te => TAbs (close_k ty k z) (close_k te (S k) z)
  | TPi ty te  => TPi  (close_k ty k z) (close_k te (S k) z)
Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
276
  | _          => t
277 278
  end.

Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
279
Definition subst_symb (t : term) (z : Var) : term := close_k t 0 z.
280

Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
281 282
Notation "'λ' x ':' A '~'  B" := (TPi A (subst_symb B x))  (at level 22, A at level 21, right associativity).
Notation "'Π' x ':' A '=>' B" := (TAbs A (subst_symb B x)) (at level 22, A at level 21, right associativity).
283 284


Gaspard Ferey's avatar
Gaspard Ferey committed
285
Definition ConvScheme := signature -> term -> term -> Prop.
286 287

Inductive RW_Head_Beta : ConvScheme :=
Gaspard Ferey's avatar
Gaspard Ferey committed
288
  RWBeta : forall Σ A t u,           RW_Head_Beta  Σ ((λ A ~ t) @ u) (subst_k t u 0).
289
Inductive RW_Head_Gamma : ConvScheme :=
Gaspard Ferey's avatar
Gaspard Ferey committed
290
  RWRel  : forall Σ t u, t  u  Σ -> RW_Head_Gamma Σ t u.
291 292

Inductive ContextClosure (Cv:ConvScheme) : ConvScheme :=
Gaspard Ferey's avatar
Gaspard Ferey committed
293 294 295 296 297 298 299
| CCHere  : forall Σ t u  , Cv Σ t u -> ContextClosure Cv Σ t u
| CCApp1  : forall Σ f g t, Cv Σ f g -> ContextClosure Cv Σ (TApp f t) (TApp g t)
| CCApp2  : forall Σ f t u, Cv Σ t u -> ContextClosure Cv Σ (TApp f t) (TApp f u)
| CCAbs1  : forall Σ A B t, Cv Σ A B -> ContextClosure Cv Σ (TAbs A t) (TApp B t)
| CCAbs2  : forall Σ A t u, Cv Σ t u -> ContextClosure Cv Σ (TAbs A t) (TAbs A u)
| CCPi1   : forall Σ A B C, Cv Σ A B -> ContextClosure Cv Σ (TPi  A C) (TApp B C)
| CCPi2   : forall Σ A B C, Cv Σ B C -> ContextClosure Cv Σ (TPi  A B) (TApp A C).
300 301

Inductive ConvUnion (C1:ConvScheme) (C2:ConvScheme) : ConvScheme :=
Gaspard Ferey's avatar
Gaspard Ferey committed
302 303
| CV1 : forall Σ t u  , C1 Σ t u -> ConvUnion C1 C2 Σ t u
| CV2 : forall Σ t u  , C2 Σ t u -> ConvUnion C1 C2 Σ t u.
304 305

Inductive RTClosure (C:ConvScheme) : ConvScheme :=
Gaspard Ferey's avatar
Gaspard Ferey committed
306 307 308
| RWRule  : forall Σ t u  , C Σ t u -> RTClosure C Σ t u
| RWRefl  : forall Σ t    , RTClosure C Σ t t
| RWTrans : forall Σ t u v, RTClosure C Σ t u -> RTClosure C Σ u v -> RTClosure C Σ t v.
309 310

Inductive RSTClosure (C:ConvScheme) : ConvScheme :=
Gaspard Ferey's avatar
Gaspard Ferey committed
311 312 313 314
| CVRefl  : forall Σ t    , RSTClosure C Σ t t
| CVSym   : forall Σ t u  , RSTClosure C Σ t u -> RSTClosure C Σ u t
| CVTrans : forall Σ t u v, RSTClosure C Σ t u -> RSTClosure C Σ u v -> RSTClosure C Σ t v
| CVRule  : forall Σ t u  , C Σ t u -> RSTClosure C Σ t u.
315 316 317 318 319

Definition RW_Beta       : ConvScheme := ContextClosure RW_Head_Beta.
Definition RW_Gamma      : ConvScheme := ContextClosure RW_Head_Gamma.
Definition RW_Beta_Gamma : ConvScheme := ConvUnion RW_Beta RW_Gamma.

Gaspard Ferey's avatar
Gaspard Ferey committed
320 321 322 323 324 325 326 327 328 329 330 331 332
Notation "Σ '⊢' t '→β'   u" := ( RW_Head_Beta              Σ t u) (at level 40, t at level 25).
Notation "Σ '⊢' t '→Γ'   u" := ( RW_Head_Gamma             Σ t u) (at level 40, t at level 25).
Notation "Σ '⊢' t '↪β'   u" := ( RW_Beta                   Σ t u) (at level 40, t at level 25).
Notation "Σ '⊢' t '↪Γ'   u" := ( RW_Gamma                  Σ t u) (at level 40, t at level 25).
Notation "Σ '⊢' t '↪βΓ'  u" := ( RW_Beta_Gamma             Σ t u) (at level 40, t at level 25).
Notation "Σ '⊢' t '↪β*'  u" := ((RTClosure  RW_Beta      ) Σ t u) (at level 40, t at level 25).
Notation "Σ '⊢' t '↪Γ*'  u" := ((RTClosure  RW_Gamma     ) Σ t u) (at level 40, t at level 25).
Notation "Σ '⊢' t '↪βΓ*' u" := ((RTClosure  RW_Beta_Gamma) Σ t u) (at level 40, t at level 25).
Notation "Σ '⊢' t '≡β'   u" := ((RSTClosure RW_Beta      ) Σ t u) (at level 40, t at level 25).
Notation "Σ '⊢' t '≡Γ'   u" := ((RSTClosure RW_Gamma     ) Σ t u) (at level 40, t at level 25).
Notation "Σ '⊢' t '≡βΓ'  u" := ((RSTClosure RW_Beta_Gamma) Σ t u) (at level 40, t at level 25).

Notation "Σ '⊢' t '≡' u" := (Σ  t ≡βΓ u) (at level 40, t at level 25).
Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
333 334 335
Reserved Notation "Σ ';' Γ '⊢' t ':' A"   (at level 40, Γ at level 30, t at level 25, A at level 25).
Reserved Notation "Σ 'WF'"                (at level 40).
Reserved Notation "Σ ';' Γ '✓'"           (at level 40, Γ at level 30).
Gaspard Ferey's avatar
Gaspard Ferey committed
336 337

Inductive typing : signature -> local_context -> term -> term -> Prop :=
Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
338 339 340 341 342 343 344
| TyAxiom  : forall Σ Γ x A, Σ;Γ  -> x : A  Σ -> Σ;Γ  (# x) : A
| TyBoundH : forall Σ Γ A    , Σ;Γ,A  -> Σ;(Γ,A)  (? 0) : A
| TyBoundN : forall Σ Γ n T A, Σ;Γ,A  -> Σ ; Γ  (? n) : T -> Σ;(Γ,A)  (? (S n)) : T
| TyType   : forall Σ Γ    , Σ;Γ  -> Σ;Γ  type : kind
| TyPi     : forall Σ Γ A B s  , Σ;Γ  A : type -> Σ;(Γ,A)  B : s -> Σ;Γ  Π A ~ B : s
| TyAbs    : forall Σ Γ A B t s, Σ;Γ  A : type -> Σ;(Γ,A)  B : s ->
               Σ;(Γ,A)  t : B -> Σ;Γ  λ A ~ t : Π A ~ B
Gaspard Ferey's avatar
Gaspard Ferey committed
345
| TyApp   : forall Σ Γ t u A B, Σ;Γ  t : Π A ~ B -> Σ;Γ  u : A -> Σ;Γ  t @ u : B [0 <- u]
Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
346
| TyConv  : forall Σ Γ t A B s, Σ;Γ  t : A -> Σ;Γ  B : s -> Σ  A  B -> Σ;Γ  t : B
Gaspard Ferey's avatar
Gaspard Ferey committed
347 348 349 350 351 352 353 354
where "Σ ';' Γ '⊢' t ':' A" := (typing Σ Γ t A)
with well_formed : signature -> Prop :=
     | WFEmpty : [ ] WF
     | WFVarT  : forall Σ x A, x  Σ -> Σ;nil  A : type -> Σ WF -> Σ, x : A WF
     | WFVarK  : forall Σ x A, x  Σ -> Σ;nil  A : kind -> Σ WF -> Σ, x : A WF
     | WFRel   : forall Σ A s t u, Σ;nil  A : s -> Σ;nil  t : A -> Σ;nil  u : A -> Σ WF -> Σ, t  u WF
where "Σ 'WF'" := (well_formed Σ)
with cwell_formed : signature -> local_context -> Prop :=
Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
355 356 357
     | CWFEmpty : forall Σ, Σ WF -> Σ;nil 
     | CWFLVar  : forall Σ Γ A, Σ;Γ  A : type -> Σ;Γ  -> Σ;Γ,A 
where "Σ ';' Γ '✓'" := (cwell_formed Σ Γ).
Gaspard Ferey's avatar
WIP  
Gaspard Ferey committed
358 359 360 361 362 363


Definition is_kind   Γ t : Prop :=      Γ;nil  t : TKind.
Definition is_type   Γ t : Prop := exists T, Γ;nil  t : T /\ is_kind Γ T.
Definition is_object Γ t : Prop := exists T, Γ;nil  t : T /\ is_type Γ T.