annotate src/Impure.v @ 190:094bd1e353dd

Import predicative Impure example
author Adam Chlipala <adamc@hcoop.net>
date Tue, 18 Nov 2008 12:44:46 -0500
parents
children cf5ddf078858
rev   line source
adamc@190 1 (* Copyright (c) 2008, Adam Chlipala
adamc@190 2 *
adamc@190 3 * This work is licensed under a
adamc@190 4 * Creative Commons Attribution-Noncommercial-No Derivative Works 3.0
adamc@190 5 * Unported License.
adamc@190 6 * The license text is available at:
adamc@190 7 * http://creativecommons.org/licenses/by-nc-nd/3.0/
adamc@190 8 *)
adamc@190 9
adamc@190 10 (* begin hide *)
adamc@190 11 Require Import Arith List Omega.
adamc@190 12
adamc@190 13 Require Import Axioms Tactics.
adamc@190 14
adamc@190 15 Set Implicit Arguments.
adamc@190 16 (* end hide *)
adamc@190 17
adamc@190 18
adamc@190 19 (** %\chapter{Modeling Impure Languages}% *)
adamc@190 20
adamc@190 21 (** TODO: Prose for this chapter *)
adamc@190 22
adamc@190 23 Section var.
adamc@190 24 Variable var : Type.
adamc@190 25
adamc@190 26 Inductive term : Type :=
adamc@190 27 | Var : var -> term
adamc@190 28 | App : term -> term -> term
adamc@190 29 | Abs : (var -> term) -> term
adamc@190 30 | Unit : term.
adamc@190 31 End var.
adamc@190 32
adamc@190 33 Implicit Arguments Unit [var].
adamc@190 34
adamc@190 35 Notation "# v" := (Var v) (at level 70).
adamc@190 36 Notation "()" := Unit.
adamc@190 37
adamc@190 38 Infix "@" := App (left associativity, at level 72).
adamc@190 39 Notation "\ x , e" := (Abs (fun x => e)) (at level 73).
adamc@190 40 Notation "\ ? , e" := (Abs (fun _ => e)) (at level 73).
adamc@190 41
adamc@190 42
adamc@190 43 Module predicative.
adamc@190 44
adamc@190 45 Inductive val : Type :=
adamc@190 46 | Func : nat -> val
adamc@190 47 | VUnit.
adamc@190 48
adamc@190 49 Inductive computation : Type :=
adamc@190 50 | Return : val -> computation
adamc@190 51 | Bind : computation -> (val -> computation) -> computation
adamc@190 52 | CAbs : (val -> computation) -> computation
adamc@190 53 | CApp : val -> val -> computation.
adamc@190 54
adamc@190 55 Definition func := val -> computation.
adamc@190 56
adamc@190 57 Fixpoint get (n : nat) (ls : list func) {struct ls} : option func :=
adamc@190 58 match ls with
adamc@190 59 | nil => None
adamc@190 60 | x :: ls' =>
adamc@190 61 if eq_nat_dec n (length ls')
adamc@190 62 then Some x
adamc@190 63 else get n ls'
adamc@190 64 end.
adamc@190 65
adamc@190 66 Inductive eval : list func -> computation -> list func -> val -> Prop :=
adamc@190 67 | EvalReturn : forall ds d,
adamc@190 68 eval ds (Return d) ds d
adamc@190 69 | EvalBind : forall ds c1 c2 ds' d1 ds'' d2,
adamc@190 70 eval ds c1 ds' d1
adamc@190 71 -> eval ds' (c2 d1) ds'' d2
adamc@190 72 -> eval ds (Bind c1 c2) ds'' d2
adamc@190 73 | EvalCAbs : forall ds f,
adamc@190 74 eval ds (CAbs f) (f :: ds) (Func (length ds))
adamc@190 75 | EvalCApp : forall ds i d2 f ds' d3,
adamc@190 76 get i ds = Some f
adamc@190 77 -> eval ds (f d2) ds' d3
adamc@190 78 -> eval ds (CApp (Func i) d2) ds' d3.
adamc@190 79
adamc@190 80 Fixpoint termDenote (e : term val) : computation :=
adamc@190 81 match e with
adamc@190 82 | Var v => Return v
adamc@190 83 | App e1 e2 => Bind (termDenote e1) (fun f =>
adamc@190 84 Bind (termDenote e2) (fun x =>
adamc@190 85 CApp f x))
adamc@190 86 | Abs e' => CAbs (fun x => termDenote (e' x))
adamc@190 87
adamc@190 88 | Unit => Return VUnit
adamc@190 89 end.
adamc@190 90
adamc@190 91 Definition Term := forall var, term var.
adamc@190 92 Definition TermDenote (E : Term) := termDenote (E _).
adamc@190 93
adamc@190 94 Definition ident : Term := fun _ => \x, #x.
adamc@190 95 Eval compute in TermDenote ident.
adamc@190 96
adamc@190 97 Definition unite : Term := fun _ => ().
adamc@190 98 Eval compute in TermDenote unite.
adamc@190 99
adamc@190 100 Definition ident_self : Term := fun _ => ident _ @ ident _.
adamc@190 101 Eval compute in TermDenote ident_self.
adamc@190 102
adamc@190 103 Definition ident_unit : Term := fun _ => ident _ @ unite _.
adamc@190 104 Eval compute in TermDenote ident_unit.
adamc@190 105
adamc@190 106 Theorem eval_ident_unit : exists ds, eval nil (TermDenote ident_unit) ds VUnit.
adamc@190 107 compute.
adamc@190 108 repeat econstructor.
adamc@190 109 simpl.
adamc@190 110 rewrite (eta Return).
adamc@190 111 reflexivity.
adamc@190 112 Qed.
adamc@190 113
adamc@190 114 Hint Constructors eval.
adamc@190 115
adamc@190 116 Lemma app_nil_start : forall A (ls : list A),
adamc@190 117 ls = nil ++ ls.
adamc@190 118 reflexivity.
adamc@190 119 Qed.
adamc@190 120
adamc@190 121 Lemma app_cons : forall A (x : A) (ls : list A),
adamc@190 122 x :: ls = (x :: nil) ++ ls.
adamc@190 123 reflexivity.
adamc@190 124 Qed.
adamc@190 125
adamc@190 126 Theorem eval_monotone : forall ds c ds' d,
adamc@190 127 eval ds c ds' d
adamc@190 128 -> exists ds'', ds' = ds'' ++ ds.
adamc@190 129 Hint Resolve app_nil_start app_ass app_cons.
adamc@190 130
adamc@190 131 induction 1; firstorder; subst; eauto.
adamc@190 132 Qed.
adamc@190 133
adamc@190 134 Lemma length_app : forall A (ds2 ds1 : list A),
adamc@190 135 length (ds1 ++ ds2) = length ds1 + length ds2.
adamc@190 136 induction ds1; simpl; intuition.
adamc@190 137 Qed.
adamc@190 138
adamc@190 139 Lemma get_app : forall ds2 d ds1,
adamc@190 140 get (length ds2) (ds1 ++ d :: ds2) = Some d.
adamc@190 141 Hint Rewrite length_app : cpdt.
adamc@190 142
adamc@190 143 induction ds1; crush;
adamc@190 144 match goal with
adamc@190 145 | [ |- context[if ?E then _ else _] ] => destruct E
adamc@190 146 end; crush.
adamc@190 147 Qed.
adamc@190 148
adamc@190 149 Theorem invert_ident : forall (E : Term) ds ds' d,
adamc@190 150 eval ds (TermDenote (fun _ => ident _ @ E _)) ds' d
adamc@190 151 -> eval ((fun x => Return x) :: ds) (TermDenote E) ds' d.
adamc@190 152 inversion 1; subst.
adamc@190 153 clear H.
adamc@190 154 inversion H3; clear H3; subst.
adamc@190 155 inversion H6; clear H6; subst.
adamc@190 156 generalize (eval_monotone H2); crush.
adamc@190 157 inversion H5; clear H5; subst.
adamc@190 158 rewrite get_app in H3.
adamc@190 159 inversion H3; clear H3; subst.
adamc@190 160 inversion H7; clear H7; subst.
adamc@190 161 assumption.
adamc@190 162 Qed.
adamc@190 163
adamc@190 164 End predicative.