annotate src/Hoas.v @ 162:df02642f93b8

Evaluation contexts ahoy
author Adam Chlipala <adamc@hcoop.net>
date Tue, 04 Nov 2008 12:39:28 -0500
parents 56e205f966cc
children ba306bf9ec80
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@158 60 Section flatten.
adamc@158 61 Variable var : type -> Type.
adamc@158 62
adamc@158 63 Fixpoint flatten t (e : exp (exp var) t) {struct e} : exp var t :=
adamc@158 64 match e in exp _ t return exp _ t with
adamc@159 65 | Const' n => Const' n
adamc@159 66 | Plus' e1 e2 => Plus' (flatten e1) (flatten e2)
adamc@158 67 | Var _ e' => e'
adamc@158 68 | App' _ _ e1 e2 => App' (flatten e1) (flatten e2)
adamc@158 69 | Abs' _ _ e' => Abs' (fun x => flatten (e' (Var x)))
adamc@158 70 end.
adamc@158 71 End flatten.
adamc@158 72
adamc@158 73 Definition Subst t1 t2 (E1 : Exp t1) (E2 : Exp1 t1 t2) : Exp t2 := fun _ =>
adamc@158 74 flatten (E2 _ (E1 _)).
adamc@158 75
adamc@158 76
adamc@158 77 (** * A Type Soundness Proof *)
adamc@158 78
adamc@158 79 Reserved Notation "E1 ==> E2" (no associativity, at level 90).
adamc@158 80
adamc@158 81 Inductive Val : forall t, Exp t -> Prop :=
adamc@159 82 | VConst : forall n, Val (Const n)
adamc@158 83 | VAbs : forall dom ran (B : Exp1 dom ran), Val (Abs B).
adamc@158 84
adamc@158 85 Hint Constructors Val.
adamc@158 86
adamc@162 87 Inductive Ctx : type -> type -> Type :=
adamc@162 88 | AppCong1 : forall (dom ran : type),
adamc@162 89 Exp dom -> Ctx (dom --> ran) ran
adamc@162 90 | AppCong2 : forall (dom ran : type),
adamc@162 91 Exp (dom --> ran) -> Ctx dom ran
adamc@162 92 | PlusCong1 : Exp Nat -> Ctx Nat Nat
adamc@162 93 | PlusCong2 : Exp Nat -> Ctx Nat Nat.
adamc@162 94
adamc@162 95 Inductive isCtx : forall t1 t2, Ctx t1 t2 -> Prop :=
adamc@162 96 | IsApp1 : forall dom ran (X : Exp dom), isCtx (AppCong1 ran X)
adamc@162 97 | IsApp2 : forall dom ran (F : Exp (dom --> ran)), Val F -> isCtx (AppCong2 F)
adamc@162 98 | IsPlus1 : forall E2, isCtx (PlusCong1 E2)
adamc@162 99 | IsPlus2 : forall E1, Val E1 -> isCtx (PlusCong2 E1).
adamc@162 100
adamc@162 101 Definition plug t1 t2 (C : Ctx t1 t2) : Exp t1 -> Exp t2 :=
adamc@162 102 match C in Ctx t1 t2 return Exp t1 -> Exp t2 with
adamc@162 103 | AppCong1 _ _ X => fun F => App F X
adamc@162 104 | AppCong2 _ _ F => fun X => App F X
adamc@162 105 | PlusCong1 E2 => fun E1 => Plus E1 E2
adamc@162 106 | PlusCong2 E1 => fun E2 => Plus E1 E2
adamc@162 107 end.
adamc@162 108
adamc@162 109 Infix "@" := plug (no associativity, at level 60).
adamc@162 110
adamc@158 111 Inductive Step : forall t, Exp t -> Exp t -> Prop :=
adamc@158 112 | Beta : forall dom ran (B : Exp1 dom ran) (X : Exp dom),
adamc@160 113 Val X
adamc@160 114 -> App (Abs B) X ==> Subst X B
adamc@159 115 | Sum : forall n1 n2,
adamc@159 116 Plus (Const n1) (Const n2) ==> Const (n1 + n2)
adamc@162 117 | Cong : forall t t' (C : Ctx t t') E E' E1,
adamc@162 118 isCtx C
adamc@162 119 -> E1 = C @ E
adamc@162 120 -> E ==> E'
adamc@162 121 -> E1 ==> C @ E'
adamc@159 122
adamc@158 123 where "E1 ==> E2" := (Step E1 E2).
adamc@158 124
adamc@162 125 Hint Constructors isCtx Step.
adamc@158 126
adamc@158 127 Inductive Closed : forall t, Exp t -> Prop :=
adamc@158 128 | CConst : forall b,
adamc@158 129 Closed (Const b)
adamc@159 130 | CPlus : forall E1 E2,
adamc@159 131 Closed E1
adamc@159 132 -> Closed E2
adamc@159 133 -> Closed (Plus E1 E2)
adamc@158 134 | CApp : forall dom ran (E1 : Exp (dom --> ran)) E2,
adamc@158 135 Closed E1
adamc@158 136 -> Closed E2
adamc@158 137 -> Closed (App E1 E2)
adamc@158 138 | CAbs : forall dom ran (E1 : Exp1 dom ran),
adamc@158 139 Closed (Abs E1).
adamc@158 140
adamc@158 141 Axiom closed : forall t (E : Exp t), Closed E.
adamc@158 142
adamc@160 143 Ltac my_crush' :=
adamc@158 144 crush;
adamc@158 145 repeat (match goal with
adamc@158 146 | [ H : _ |- _ ] => generalize (inj_pairT2 _ _ _ _ _ H); clear H
adamc@158 147 end; crush).
adamc@158 148
adamc@160 149 Ltac my_crush :=
adamc@160 150 my_crush';
adamc@160 151 try (match goal with
adamc@160 152 | [ H : ?F = ?G |- _ ] =>
adamc@160 153 match goal with
adamc@162 154 (*| [ _ : F (fun _ => unit) = G (fun _ => unit) |- _ ] => fail 1*)
adamc@160 155 | _ =>
adamc@160 156 let H' := fresh "H'" in
adamc@160 157 assert (H' : F (fun _ => unit) = G (fun _ => unit)); [ congruence
adamc@160 158 | discriminate || injection H' ];
adamc@160 159 clear H'
adamc@160 160 end
adamc@160 161 end; my_crush');
adamc@160 162 repeat match goal with
adamc@160 163 | [ H : ?F = ?G, H2 : ?X (fun _ => unit) = ?Y (fun _ => unit) |- _ ] =>
adamc@160 164 match X with
adamc@160 165 | Y => fail 1
adamc@160 166 | _ =>
adamc@160 167 assert (X = Y); [ unfold Exp; apply ext_eq; intro var;
adamc@160 168 let H' := fresh "H'" in
adamc@160 169 assert (H' : F var = G var); [ congruence
adamc@160 170 | match type of H' with
adamc@160 171 | ?X = ?Y =>
adamc@160 172 let X := eval hnf in X in
adamc@160 173 let Y := eval hnf in Y in
adamc@160 174 change (X = Y) in H'
adamc@160 175 end; injection H'; clear H'; my_crush' ]
adamc@160 176 | my_crush'; clear H2 ]
adamc@160 177 end
adamc@160 178 end.
adamc@160 179
adamc@162 180 Hint Extern 1 (_ = _ @ _) => simpl.
adamc@162 181
adamc@158 182 Lemma progress' : forall t (E : Exp t),
adamc@158 183 Closed E
adamc@158 184 -> Val E \/ exists E', E ==> E'.
adamc@158 185 induction 1; crush;
adamc@159 186 repeat match goal with
adamc@159 187 | [ H : Val _ |- _ ] => inversion H; []; clear H; my_crush
adamc@162 188 end; eauto 6.
adamc@158 189 Qed.
adamc@158 190
adamc@158 191 Theorem progress : forall t (E : Exp t),
adamc@158 192 Val E \/ exists E', E ==> E'.
adamc@158 193 intros; apply progress'; apply closed.
adamc@158 194 Qed.
adamc@158 195
adamc@160 196
adamc@160 197 (** * Big-Step Semantics *)
adamc@160 198
adamc@160 199 Reserved Notation "E1 ===> E2" (no associativity, at level 90).
adamc@160 200
adamc@160 201 Inductive BigStep : forall t, Exp t -> Exp t -> Prop :=
adamc@160 202 | SConst : forall n,
adamc@160 203 Const n ===> Const n
adamc@160 204 | SPlus : forall E1 E2 n1 n2,
adamc@160 205 E1 ===> Const n1
adamc@160 206 -> E2 ===> Const n2
adamc@160 207 -> Plus E1 E2 ===> Const (n1 + n2)
adamc@160 208
adamc@160 209 | SApp : forall dom ran (E1 : Exp (dom --> ran)) E2 B V2 V,
adamc@160 210 E1 ===> Abs B
adamc@160 211 -> E2 ===> V2
adamc@160 212 -> Subst V2 B ===> V
adamc@160 213 -> App E1 E2 ===> V
adamc@160 214 | SAbs : forall dom ran (B : Exp1 dom ran),
adamc@160 215 Abs B ===> Abs B
adamc@160 216
adamc@160 217 where "E1 ===> E2" := (BigStep E1 E2).
adamc@160 218
adamc@160 219 Hint Constructors BigStep.
adamc@160 220
adamc@160 221 Reserved Notation "E1 ==>* E2" (no associativity, at level 90).
adamc@160 222
adamc@160 223 Inductive MultiStep : forall t, Exp t -> Exp t -> Prop :=
adamc@160 224 | Done : forall t (E : Exp t), E ==>* E
adamc@160 225 | OneStep : forall t (E E' E'' : Exp t),
adamc@160 226 E ==> E'
adamc@160 227 -> E' ==>* E''
adamc@160 228 -> E ==>* E''
adamc@160 229
adamc@160 230 where "E1 ==>* E2" := (MultiStep E1 E2).
adamc@160 231
adamc@160 232 Hint Constructors MultiStep.
adamc@160 233
adamc@160 234 Theorem MultiStep_trans : forall t (E1 E2 E3 : Exp t),
adamc@160 235 E1 ==>* E2
adamc@160 236 -> E2 ==>* E3
adamc@160 237 -> E1 ==>* E3.
adamc@160 238 induction 1; eauto.
adamc@160 239 Qed.
adamc@160 240
adamc@160 241 Theorem Big_Val : forall t (E V : Exp t),
adamc@160 242 E ===> V
adamc@160 243 -> Val V.
adamc@160 244 induction 1; crush.
adamc@160 245 Qed.
adamc@160 246
adamc@160 247 Theorem Val_Big : forall t (V : Exp t),
adamc@160 248 Val V
adamc@160 249 -> V ===> V.
adamc@160 250 destruct 1; crush.
adamc@160 251 Qed.
adamc@160 252
adamc@160 253 Hint Resolve Big_Val Val_Big.
adamc@160 254
adamc@162 255 Lemma Multi_Cong : forall t t' (C : Ctx t t'),
adamc@162 256 isCtx C
adamc@162 257 -> forall E E', E ==>* E'
adamc@162 258 -> C @ E ==>* C @ E'.
adamc@160 259 induction 2; crush; eauto.
adamc@160 260 Qed.
adamc@160 261
adamc@162 262 Lemma Multi_Cong' : forall t t' (C : Ctx t t') E1 E2 E E',
adamc@162 263 isCtx C
adamc@162 264 -> E1 = C @ E
adamc@162 265 -> E2 = C @ E'
adamc@162 266 -> E ==>* E'
adamc@162 267 -> E1 ==>* E2.
adamc@162 268 crush; apply Multi_Cong; auto.
adamc@162 269 Qed.
adamc@162 270
adamc@162 271 Hint Resolve Multi_Cong'.
adamc@162 272
adamc@162 273 Ltac mtrans E :=
adamc@162 274 match goal with
adamc@162 275 | [ |- E ==>* _ ] => fail 1
adamc@162 276 | _ => apply MultiStep_trans with E; [ solve [ eauto ] | eauto ]
adamc@162 277 end.
adamc@160 278
adamc@160 279 Theorem Big_Multi : forall t (E V : Exp t),
adamc@160 280 E ===> V
adamc@160 281 -> E ==>* V.
adamc@162 282 induction 1; crush; eauto;
adamc@162 283 repeat match goal with
adamc@162 284 | [ n1 : _, E2 : _ |- _ ] => mtrans (Plus (Const n1) E2)
adamc@162 285 | [ n1 : _, n2 : _ |- _ ] => mtrans (Plus (Const n1) (Const n2))
adamc@162 286 | [ B : _, E2 : _ |- _ ] => mtrans (App (Abs B) E2)
adamc@162 287 end.
adamc@160 288 Qed.
adamc@160 289
adamc@160 290 Lemma Big_Val' : forall t (V1 V2 : Exp t),
adamc@160 291 Val V2
adamc@160 292 -> V1 = V2
adamc@160 293 -> V1 ===> V2.
adamc@160 294 crush.
adamc@160 295 Qed.
adamc@160 296
adamc@160 297 Hint Resolve Big_Val'.
adamc@160 298
adamc@160 299 Lemma Multi_Big' : forall t (E E' : Exp t),
adamc@160 300 E ==> E'
adamc@160 301 -> forall E'', E' ===> E''
adamc@160 302 -> E ===> E''.
adamc@160 303 induction 1; crush; eauto;
adamc@160 304 match goal with
adamc@160 305 | [ H : _ ===> _ |- _ ] => inversion H; my_crush; eauto
adamc@162 306 end;
adamc@162 307 match goal with
adamc@162 308 | [ H : isCtx _ |- _ ] => inversion H; my_crush; eauto
adamc@160 309 end.
adamc@160 310 Qed.
adamc@160 311
adamc@160 312 Hint Resolve Multi_Big'.
adamc@160 313
adamc@160 314 Theorem Multi_Big : forall t (E V : Exp t),
adamc@160 315 E ==>* V
adamc@160 316 -> Val V
adamc@160 317 -> E ===> V.
adamc@160 318 induction 1; crush; eauto.
adamc@160 319 Qed.
adamc@160 320
adamc@160 321
adamc@160 322 (** * Constant folding *)
adamc@160 323
adamc@160 324 Section cfold.
adamc@160 325 Variable var : type -> Type.
adamc@160 326
adamc@160 327 Fixpoint cfold t (e : exp var t) {struct e} : exp var t :=
adamc@160 328 match e in exp _ t return exp _ t with
adamc@160 329 | Const' n => Const' n
adamc@160 330 | Plus' e1 e2 =>
adamc@160 331 let e1' := cfold e1 in
adamc@160 332 let e2' := cfold e2 in
adamc@160 333 match e1', e2' with
adamc@160 334 | Const' n1, Const' n2 => Const' (n1 + n2)
adamc@160 335 | _, _ => Plus' e1' e2'
adamc@160 336 end
adamc@160 337
adamc@160 338 | Var _ x => Var x
adamc@160 339 | App' _ _ e1 e2 => App' (cfold e1) (cfold e2)
adamc@160 340 | Abs' _ _ e' => Abs' (fun x => cfold (e' x))
adamc@160 341 end.
adamc@160 342 End cfold.
adamc@160 343
adamc@160 344 Definition Cfold t (E : Exp t) : Exp t := fun _ => cfold (E _).
adamc@160 345
adamc@160 346
adamc@160 347 Definition ExpN (G : list type) (t : type) := forall var, hlist var G -> exp var t.
adamc@160 348
adamc@160 349 Definition ConstN G (n : nat) : ExpN G Nat :=
adamc@160 350 fun _ _ => Const' n.
adamc@160 351 Definition PlusN G (E1 E2 : ExpN G Nat) : ExpN G Nat :=
adamc@160 352 fun _ s => Plus' (E1 _ s) (E2 _ s).
adamc@160 353 Definition AppN G dom ran (F : ExpN G (dom --> ran)) (X : ExpN G dom) : ExpN G ran :=
adamc@160 354 fun _ s => App' (F _ s) (X _ s).
adamc@160 355 Definition AbsN G dom ran (B : ExpN (dom :: G) ran) : ExpN G (dom --> ran) :=
adamc@162 356 fun _ s => Abs' (fun x => B _ (x ::: s)).