annotate src/Hoas.v @ 167:ef485f86a7b6

Cleaning up BigStep proofs
author Adam Chlipala <adamc@hcoop.net>
date Wed, 05 Nov 2008 09:29:15 -0500
parents 73279a8aac71
children 0c5a41e9e508
rev   line source
adamc@158 1 (* Copyright (c) 2008, Adam Chlipala
adamc@158 2 *
adamc@158 3 * This work is licensed under a
adamc@158 4 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
adamc@158 5 * Unported License.
adamc@158 6 * The license text is available at:
adamc@158 7 * http://creativecommons.org/licenses/by-nc-nd/3.0/
adamc@158 8 *)
adamc@158 9
adamc@158 10 (* begin hide *)
adamc@158 11 Require Import Arith Eqdep String List.
adamc@158 12
adamc@160 13 Require Import Axioms DepList Tactics.
adamc@158 14
adamc@158 15 Set Implicit Arguments.
adamc@158 16 (* end hide *)
adamc@158 17
adamc@158 18
adamc@158 19 (** %\chapter{Higher-Order Abstract Syntax}% *)
adamc@158 20
adamc@158 21 (** TODO: Prose for this chapter *)
adamc@158 22
adamc@158 23
adamc@158 24 (** * Parametric Higher-Order Abstract Syntax *)
adamc@158 25
adamc@158 26 Inductive type : Type :=
adamc@159 27 | Nat : type
adamc@158 28 | Arrow : type -> type -> type.
adamc@158 29
adamc@158 30 Infix "-->" := Arrow (right associativity, at level 60).
adamc@158 31
adamc@158 32 Section exp.
adamc@158 33 Variable var : type -> Type.
adamc@158 34
adamc@158 35 Inductive exp : type -> Type :=
adamc@159 36 | Const' : nat -> exp Nat
adamc@159 37 | Plus' : exp Nat -> exp Nat -> exp Nat
adamc@159 38
adamc@158 39 | Var : forall t, var t -> exp t
adamc@158 40 | App' : forall dom ran, exp (dom --> ran) -> exp dom -> exp ran
adamc@158 41 | Abs' : forall dom ran, (var dom -> exp ran) -> exp (dom --> ran).
adamc@158 42 End exp.
adamc@158 43
adamc@158 44 Implicit Arguments Const' [var].
adamc@158 45 Implicit Arguments Var [var t].
adamc@158 46 Implicit Arguments Abs' [var dom ran].
adamc@158 47
adamc@158 48 Definition Exp t := forall var, exp var t.
adamc@158 49 Definition Exp1 t1 t2 := forall var, var t1 -> exp var t2.
adamc@158 50
adamc@159 51 Definition Const (n : nat) : Exp Nat :=
adamc@159 52 fun _ => Const' n.
adamc@159 53 Definition Plus (E1 E2 : Exp Nat) : Exp Nat :=
adamc@159 54 fun _ => Plus' (E1 _) (E2 _).
adamc@158 55 Definition App dom ran (F : Exp (dom --> ran)) (X : Exp dom) : Exp ran :=
adamc@158 56 fun _ => App' (F _) (X _).
adamc@158 57 Definition Abs dom ran (B : Exp1 dom ran) : Exp (dom --> ran) :=
adamc@158 58 fun _ => Abs' (B _).
adamc@158 59
adamc@166 60 Definition zero := Const 0.
adamc@166 61 Definition one := Const 1.
adamc@166 62 Definition one_again := Plus zero one.
adamc@166 63 Definition ident : Exp (Nat --> Nat) := Abs (fun _ X => Var X).
adamc@166 64 Definition app_ident := App ident one_again.
adamc@166 65 Definition app : Exp ((Nat --> Nat) --> Nat --> Nat) := fun _ =>
adamc@166 66 Abs' (fun f => Abs' (fun x => App' (Var f) (Var x))).
adamc@166 67 Definition app_ident' := App (App app ident) one_again.
adamc@166 68
adamc@166 69 Fixpoint countVars t (e : exp (fun _ => unit) t) {struct e} : nat :=
adamc@166 70 match e with
adamc@166 71 | Const' _ => 0
adamc@166 72 | Plus' e1 e2 => countVars e1 + countVars e2
adamc@166 73 | Var _ _ => 1
adamc@166 74 | App' _ _ e1 e2 => countVars e1 + countVars e2
adamc@166 75 | Abs' _ _ e' => countVars (e' tt)
adamc@166 76 end.
adamc@166 77
adamc@166 78 Definition CountVars t (E : Exp t) : nat := countVars (E _).
adamc@166 79
adamc@166 80 Eval compute in CountVars zero.
adamc@166 81 Eval compute in CountVars one.
adamc@166 82 Eval compute in CountVars one_again.
adamc@166 83 Eval compute in CountVars ident.
adamc@166 84 Eval compute in CountVars app_ident.
adamc@166 85 Eval compute in CountVars app.
adamc@166 86 Eval compute in CountVars app_ident'.
adamc@166 87
adamc@166 88 Fixpoint countOne t (e : exp (fun _ => bool) t) {struct e} : nat :=
adamc@166 89 match e with
adamc@166 90 | Const' _ => 0
adamc@166 91 | Plus' e1 e2 => countOne e1 + countOne e2
adamc@166 92 | Var _ true => 1
adamc@166 93 | Var _ false => 0
adamc@166 94 | App' _ _ e1 e2 => countOne e1 + countOne e2
adamc@166 95 | Abs' _ _ e' => countOne (e' false)
adamc@166 96 end.
adamc@166 97
adamc@166 98 Definition CountOne t1 t2 (E : Exp1 t1 t2) : nat :=
adamc@166 99 countOne (E _ true).
adamc@166 100
adamc@166 101 Definition ident1 : Exp1 Nat Nat := fun _ X => Var X.
adamc@166 102 Definition add_self : Exp1 Nat Nat := fun _ X => Plus' (Var X) (Var X).
adamc@166 103 Definition app_zero : Exp1 (Nat --> Nat) Nat := fun _ X => App' (Var X) (Const' 0).
adamc@166 104 Definition app_ident1 : Exp1 Nat Nat := fun _ X => App' (Abs' (fun Y => Var Y)) (Var X).
adamc@166 105
adamc@166 106 Eval compute in CountOne ident1.
adamc@166 107 Eval compute in CountOne add_self.
adamc@166 108 Eval compute in CountOne app_zero.
adamc@166 109 Eval compute in CountOne app_ident1.
adamc@166 110
adamc@166 111 Section ToString.
adamc@166 112 Open Scope string_scope.
adamc@166 113
adamc@166 114 Fixpoint natToString (n : nat) : string :=
adamc@166 115 match n with
adamc@166 116 | O => "O"
adamc@166 117 | S n' => "S(" ++ natToString n' ++ ")"
adamc@166 118 end.
adamc@166 119
adamc@166 120 Fixpoint toString t (e : exp (fun _ => string) t) (cur : string) {struct e} : string * string :=
adamc@166 121 match e with
adamc@166 122 | Const' n => (cur, natToString n)
adamc@166 123 | Plus' e1 e2 =>
adamc@166 124 let (cur', s1) := toString e1 cur in
adamc@166 125 let (cur'', s2) := toString e2 cur' in
adamc@166 126 (cur'', "(" ++ s1 ++ ") + (" ++ s2 ++ ")")
adamc@166 127 | Var _ s => (cur, s)
adamc@166 128 | App' _ _ e1 e2 =>
adamc@166 129 let (cur', s1) := toString e1 cur in
adamc@166 130 let (cur'', s2) := toString e2 cur' in
adamc@166 131 (cur'', "(" ++ s1 ++ ") (" ++ s2 ++ ")")
adamc@166 132 | Abs' _ _ e' =>
adamc@166 133 let (cur', s) := toString (e' cur) (cur ++ "'") in
adamc@166 134 (cur', "(\" ++ cur ++ ", " ++ s ++ ")")
adamc@166 135 end.
adamc@166 136
adamc@166 137 Definition ToString t (E : Exp t) : string := snd (toString (E _) "x").
adamc@166 138 End ToString.
adamc@166 139
adamc@166 140 Eval compute in ToString zero.
adamc@166 141 Eval compute in ToString one.
adamc@166 142 Eval compute in ToString one_again.
adamc@166 143 Eval compute in ToString ident.
adamc@166 144 Eval compute in ToString app_ident.
adamc@166 145 Eval compute in ToString app.
adamc@166 146 Eval compute in ToString app_ident'.
adamc@166 147
adamc@158 148 Section flatten.
adamc@158 149 Variable var : type -> Type.
adamc@158 150
adamc@158 151 Fixpoint flatten t (e : exp (exp var) t) {struct e} : exp var t :=
adamc@158 152 match e in exp _ t return exp _ t with
adamc@159 153 | Const' n => Const' n
adamc@159 154 | Plus' e1 e2 => Plus' (flatten e1) (flatten e2)
adamc@158 155 | Var _ e' => e'
adamc@158 156 | App' _ _ e1 e2 => App' (flatten e1) (flatten e2)
adamc@158 157 | Abs' _ _ e' => Abs' (fun x => flatten (e' (Var x)))
adamc@158 158 end.
adamc@158 159 End flatten.
adamc@158 160
adamc@158 161 Definition Subst t1 t2 (E1 : Exp t1) (E2 : Exp1 t1 t2) : Exp t2 := fun _ =>
adamc@158 162 flatten (E2 _ (E1 _)).
adamc@158 163
adamc@166 164 Eval compute in Subst one ident1.
adamc@166 165 Eval compute in Subst one add_self.
adamc@166 166 Eval compute in Subst ident app_zero.
adamc@166 167 Eval compute in Subst one app_ident1.
adamc@166 168
adamc@158 169
adamc@158 170 (** * A Type Soundness Proof *)
adamc@158 171
adamc@158 172 Reserved Notation "E1 ==> E2" (no associativity, at level 90).
adamc@158 173
adamc@158 174 Inductive Val : forall t, Exp t -> Prop :=
adamc@159 175 | VConst : forall n, Val (Const n)
adamc@158 176 | VAbs : forall dom ran (B : Exp1 dom ran), Val (Abs B).
adamc@158 177
adamc@158 178 Hint Constructors Val.
adamc@158 179
adamc@162 180 Inductive Ctx : type -> type -> Type :=
adamc@162 181 | AppCong1 : forall (dom ran : type),
adamc@162 182 Exp dom -> Ctx (dom --> ran) ran
adamc@162 183 | AppCong2 : forall (dom ran : type),
adamc@162 184 Exp (dom --> ran) -> Ctx dom ran
adamc@162 185 | PlusCong1 : Exp Nat -> Ctx Nat Nat
adamc@162 186 | PlusCong2 : Exp Nat -> Ctx Nat Nat.
adamc@162 187
adamc@162 188 Inductive isCtx : forall t1 t2, Ctx t1 t2 -> Prop :=
adamc@162 189 | IsApp1 : forall dom ran (X : Exp dom), isCtx (AppCong1 ran X)
adamc@162 190 | IsApp2 : forall dom ran (F : Exp (dom --> ran)), Val F -> isCtx (AppCong2 F)
adamc@162 191 | IsPlus1 : forall E2, isCtx (PlusCong1 E2)
adamc@162 192 | IsPlus2 : forall E1, Val E1 -> isCtx (PlusCong2 E1).
adamc@162 193
adamc@162 194 Definition plug t1 t2 (C : Ctx t1 t2) : Exp t1 -> Exp t2 :=
adamc@162 195 match C in Ctx t1 t2 return Exp t1 -> Exp t2 with
adamc@162 196 | AppCong1 _ _ X => fun F => App F X
adamc@162 197 | AppCong2 _ _ F => fun X => App F X
adamc@162 198 | PlusCong1 E2 => fun E1 => Plus E1 E2
adamc@162 199 | PlusCong2 E1 => fun E2 => Plus E1 E2
adamc@162 200 end.
adamc@162 201
adamc@162 202 Infix "@" := plug (no associativity, at level 60).
adamc@162 203
adamc@158 204 Inductive Step : forall t, Exp t -> Exp t -> Prop :=
adamc@158 205 | Beta : forall dom ran (B : Exp1 dom ran) (X : Exp dom),
adamc@160 206 Val X
adamc@160 207 -> App (Abs B) X ==> Subst X B
adamc@159 208 | Sum : forall n1 n2,
adamc@159 209 Plus (Const n1) (Const n2) ==> Const (n1 + n2)
adamc@162 210 | Cong : forall t t' (C : Ctx t t') E E' E1,
adamc@162 211 isCtx C
adamc@162 212 -> E1 = C @ E
adamc@162 213 -> E ==> E'
adamc@162 214 -> E1 ==> C @ E'
adamc@159 215
adamc@158 216 where "E1 ==> E2" := (Step E1 E2).
adamc@158 217
adamc@162 218 Hint Constructors isCtx Step.
adamc@158 219
adamc@158 220 Inductive Closed : forall t, Exp t -> Prop :=
adamc@164 221 | CConst : forall n,
adamc@164 222 Closed (Const n)
adamc@159 223 | CPlus : forall E1 E2,
adamc@159 224 Closed E1
adamc@159 225 -> Closed E2
adamc@159 226 -> Closed (Plus E1 E2)
adamc@158 227 | CApp : forall dom ran (E1 : Exp (dom --> ran)) E2,
adamc@158 228 Closed E1
adamc@158 229 -> Closed E2
adamc@158 230 -> Closed (App E1 E2)
adamc@158 231 | CAbs : forall dom ran (E1 : Exp1 dom ran),
adamc@158 232 Closed (Abs E1).
adamc@158 233
adamc@158 234 Axiom closed : forall t (E : Exp t), Closed E.
adamc@158 235
adamc@160 236 Ltac my_crush' :=
adamc@167 237 crush;
adamc@158 238 repeat (match goal with
adamc@158 239 | [ H : _ |- _ ] => generalize (inj_pairT2 _ _ _ _ _ H); clear H
adamc@167 240 end; crush).
adamc@160 241
adamc@162 242 Hint Extern 1 (_ = _ @ _) => simpl.
adamc@162 243
adamc@158 244 Lemma progress' : forall t (E : Exp t),
adamc@158 245 Closed E
adamc@158 246 -> Val E \/ exists E', E ==> E'.
adamc@158 247 induction 1; crush;
adamc@159 248 repeat match goal with
adamc@167 249 | [ H : Val _ |- _ ] => inversion H; []; clear H; my_crush'
adamc@162 250 end; eauto 6.
adamc@158 251 Qed.
adamc@158 252
adamc@158 253 Theorem progress : forall t (E : Exp t),
adamc@158 254 Val E \/ exists E', E ==> E'.
adamc@158 255 intros; apply progress'; apply closed.
adamc@158 256 Qed.
adamc@158 257
adamc@160 258
adamc@160 259 (** * Big-Step Semantics *)
adamc@160 260
adamc@160 261 Reserved Notation "E1 ===> E2" (no associativity, at level 90).
adamc@160 262
adamc@160 263 Inductive BigStep : forall t, Exp t -> Exp t -> Prop :=
adamc@160 264 | SConst : forall n,
adamc@160 265 Const n ===> Const n
adamc@160 266 | SPlus : forall E1 E2 n1 n2,
adamc@160 267 E1 ===> Const n1
adamc@160 268 -> E2 ===> Const n2
adamc@160 269 -> Plus E1 E2 ===> Const (n1 + n2)
adamc@160 270
adamc@160 271 | SApp : forall dom ran (E1 : Exp (dom --> ran)) E2 B V2 V,
adamc@160 272 E1 ===> Abs B
adamc@160 273 -> E2 ===> V2
adamc@160 274 -> Subst V2 B ===> V
adamc@160 275 -> App E1 E2 ===> V
adamc@160 276 | SAbs : forall dom ran (B : Exp1 dom ran),
adamc@160 277 Abs B ===> Abs B
adamc@160 278
adamc@160 279 where "E1 ===> E2" := (BigStep E1 E2).
adamc@160 280
adamc@160 281 Hint Constructors BigStep.
adamc@160 282
adamc@160 283 Reserved Notation "E1 ==>* E2" (no associativity, at level 90).
adamc@160 284
adamc@160 285 Inductive MultiStep : forall t, Exp t -> Exp t -> Prop :=
adamc@160 286 | Done : forall t (E : Exp t), E ==>* E
adamc@160 287 | OneStep : forall t (E E' E'' : Exp t),
adamc@160 288 E ==> E'
adamc@160 289 -> E' ==>* E''
adamc@160 290 -> E ==>* E''
adamc@160 291
adamc@160 292 where "E1 ==>* E2" := (MultiStep E1 E2).
adamc@160 293
adamc@160 294 Hint Constructors MultiStep.
adamc@160 295
adamc@160 296 Theorem MultiStep_trans : forall t (E1 E2 E3 : Exp t),
adamc@160 297 E1 ==>* E2
adamc@160 298 -> E2 ==>* E3
adamc@160 299 -> E1 ==>* E3.
adamc@160 300 induction 1; eauto.
adamc@160 301 Qed.
adamc@160 302
adamc@160 303 Theorem Big_Val : forall t (E V : Exp t),
adamc@160 304 E ===> V
adamc@160 305 -> Val V.
adamc@160 306 induction 1; crush.
adamc@160 307 Qed.
adamc@160 308
adamc@160 309 Theorem Val_Big : forall t (V : Exp t),
adamc@160 310 Val V
adamc@160 311 -> V ===> V.
adamc@160 312 destruct 1; crush.
adamc@160 313 Qed.
adamc@160 314
adamc@160 315 Hint Resolve Big_Val Val_Big.
adamc@160 316
adamc@162 317 Lemma Multi_Cong : forall t t' (C : Ctx t t'),
adamc@162 318 isCtx C
adamc@162 319 -> forall E E', E ==>* E'
adamc@162 320 -> C @ E ==>* C @ E'.
adamc@160 321 induction 2; crush; eauto.
adamc@160 322 Qed.
adamc@160 323
adamc@162 324 Lemma Multi_Cong' : forall t t' (C : Ctx t t') E1 E2 E E',
adamc@162 325 isCtx C
adamc@162 326 -> E1 = C @ E
adamc@162 327 -> E2 = C @ E'
adamc@162 328 -> E ==>* E'
adamc@162 329 -> E1 ==>* E2.
adamc@162 330 crush; apply Multi_Cong; auto.
adamc@162 331 Qed.
adamc@162 332
adamc@162 333 Hint Resolve Multi_Cong'.
adamc@162 334
adamc@162 335 Ltac mtrans E :=
adamc@162 336 match goal with
adamc@162 337 | [ |- E ==>* _ ] => fail 1
adamc@162 338 | _ => apply MultiStep_trans with E; [ solve [ eauto ] | eauto ]
adamc@162 339 end.
adamc@160 340
adamc@160 341 Theorem Big_Multi : forall t (E V : Exp t),
adamc@160 342 E ===> V
adamc@160 343 -> E ==>* V.
adamc@162 344 induction 1; crush; eauto;
adamc@162 345 repeat match goal with
adamc@162 346 | [ n1 : _, E2 : _ |- _ ] => mtrans (Plus (Const n1) E2)
adamc@162 347 | [ n1 : _, n2 : _ |- _ ] => mtrans (Plus (Const n1) (Const n2))
adamc@162 348 | [ B : _, E2 : _ |- _ ] => mtrans (App (Abs B) E2)
adamc@162 349 end.
adamc@160 350 Qed.
adamc@160 351
adamc@160 352 Lemma Big_Val' : forall t (V1 V2 : Exp t),
adamc@160 353 Val V2
adamc@160 354 -> V1 = V2
adamc@160 355 -> V1 ===> V2.
adamc@160 356 crush.
adamc@160 357 Qed.
adamc@160 358
adamc@160 359 Hint Resolve Big_Val'.
adamc@160 360
adamc@167 361 Ltac equate_conj F G :=
adamc@167 362 match constr:(F, G) with
adamc@167 363 | (_ ?x1, _ ?x2) => constr:(x1 = x2)
adamc@167 364 | (_ ?x1 ?y1, _ ?x2 ?y2) => constr:(x1 = x2 /\ y1 = y2)
adamc@167 365 | (_ ?x1 ?y1 ?z1, _ ?x2 ?y2 ?z2) => constr:(x1 = x2 /\ y1 = y2 /\ z1 = z2)
adamc@167 366 | (_ ?x1 ?y1 ?z1 ?u1, _ ?x2 ?y2 ?z2 ?u2) => constr:(x1 = x2 /\ y1 = y2 /\ z1 = z2 /\ u1 = u2)
adamc@167 367 | (_ ?x1 ?y1 ?z1 ?u1 ?v1, _ ?x2 ?y2 ?z2 ?u2 ?v2) => constr:(x1 = x2 /\ y1 = y2 /\ z1 = z2 /\ u1 = u2 /\ v1 = v2)
adamc@167 368 end.
adamc@167 369
adamc@167 370 Ltac my_crush :=
adamc@167 371 my_crush';
adamc@167 372 repeat (match goal with
adamc@167 373 | [ H : ?F = ?G |- _ ] =>
adamc@167 374 (let H' := fresh "H'" in
adamc@167 375 assert (H' : F (fun _ => unit) = G (fun _ => unit)); [ congruence
adamc@167 376 | discriminate || injection H'; clear H' ];
adamc@167 377 my_crush';
adamc@167 378 repeat match goal with
adamc@167 379 | [ H : context[fun _ => unit] |- _ ] => clear H
adamc@167 380 end;
adamc@167 381 match type of H with
adamc@167 382 | ?F = ?G =>
adamc@167 383 let ec := equate_conj F G in
adamc@167 384 let var := fresh "var" in
adamc@167 385 assert ec; [ intuition; unfold Exp; apply ext_eq; intro var;
adamc@167 386 assert (H' : F var = G var); try congruence;
adamc@167 387 match type of H' with
adamc@167 388 | ?X = ?Y =>
adamc@167 389 let X := eval hnf in X in
adamc@167 390 let Y := eval hnf in Y in
adamc@167 391 change (X = Y) in H'
adamc@167 392 end; injection H'; my_crush'; tauto
adamc@167 393 | intuition; subst ]
adamc@167 394 end);
adamc@167 395 clear H
adamc@167 396 end; my_crush');
adamc@167 397 my_crush'.
adamc@167 398
adamc@160 399 Lemma Multi_Big' : forall t (E E' : Exp t),
adamc@160 400 E ==> E'
adamc@160 401 -> forall E'', E' ===> E''
adamc@160 402 -> E ===> E''.
adamc@160 403 induction 1; crush; eauto;
adamc@160 404 match goal with
adamc@160 405 | [ H : _ ===> _ |- _ ] => inversion H; my_crush; eauto
adamc@162 406 end;
adamc@162 407 match goal with
adamc@162 408 | [ H : isCtx _ |- _ ] => inversion H; my_crush; eauto
adamc@160 409 end.
adamc@160 410 Qed.
adamc@160 411
adamc@160 412 Hint Resolve Multi_Big'.
adamc@160 413
adamc@160 414 Theorem Multi_Big : forall t (E V : Exp t),
adamc@160 415 E ==>* V
adamc@160 416 -> Val V
adamc@160 417 -> E ===> V.
adamc@160 418 induction 1; crush; eauto.
adamc@160 419 Qed.