```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 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 ``` ```Require Import Arith. Inductive term := | Var : nat -> term | App : term -> term -> term | Abs : term -> term. (* de Bruijn indexでのカットオフc, シフト量iのシフト *) Module Index. Fixpoint sh_pos i c t := match t with | Var n => if (le_gt_dec c n) then Var (n+i) else Var n | App u v => App (sh_pos i c u) (sh_pos i c v) | Abs u => Abs (sh_pos i (c+1) u) end. Fixpoint sh_neg i c t := match t with | Var n => if (le_gt_dec c n) then Var (n-i) else Var n | App u v => App (sh_neg i c u) (sh_neg i c v) | Abs u => Abs (sh_neg i (c+1) u) end. (* de Bruijn indexでのsubstitution [n := N]M *) Fixpoint subst n N M := match M with | Var m => if (eq_nat_dec m n) then N else Var m | App u v => App (subst n N u) (subst n N v) | Abs u => Abs (subst (n+1) (sh_pos 1 0 N) u) end. (* de Bruijn indexでのbeta contraction (lambda M)N -> sh -1 0 ([0 := (sh 1 0 N)]M) *) Definition beta M N := sh_neg 1 0 (subst 0 (sh_pos 1 0 N) M). (* de Bruijn indexでのbeta reduction *) Inductive red : term -> term -> Prop := | Cong_App_L : forall t1 t1' t2, (red t1 t1') -> red (App t1 t2) (App t1' t2) | Cong_App_R : forall t1 t2 t2', (red t2 t2') -> red (App t1 t2) (App t1 t2') | Cong_Abs : forall t1 t1', (red t1 t1') -> red (Abs t1) (Abs t1') | Beta : forall M N, red (App (Abs M) N) (beta M N). (* 正しくないCong_Abs節 | Cong_Abs : forall t1 t1', (red t1 t1') -> red (Abs (sh_pos 1 0 t1)) (Abs (sh_pos 1 0 t1')) これではabstractionによってbindされている(Var 0)に対する計算を行うことができない。 *) Fixpoint depth t := match t with | Var _ => 1 | App t1 t2 => 1 + depth t1 + depth t2 | Abs t1 => 1 + depth t1 end. Lemma depth_sh_pos : forall i c t, depth (sh_pos i c t) = depth t. Proof. intros i c t; revert i c. induction t; intros. simpl. destruct (le_gt_dec c n); reflexivity. simpl. rewrite IHt1, IHt2. reflexivity. simpl. rewrite IHt. reflexivity. Qed. Lemma depth_sh_neg : forall i c t, depth (sh_neg i c t) = depth t. Proof. intros i c t; revert i c. induction t; intros. simpl. destruct (le_gt_dec c n); reflexivity. simpl. rewrite IHt1, IHt2. reflexivity. simpl. rewrite IHt. reflexivity. Qed. (* Require Import Recdef. Function reduce M {measure depth} := match M with | Var _ => None | App (Abs u) v => Some (beta u v) | App t1 t2 => let red_t1 := reduce t1 in let red_t2 := reduce t2 in (*match reduce_cbn t1, reduce_cbn t2 with*) match red_t1,red_t2 with | Some t1', _ => Some (App t1' t2) | None, Some t2' => Some (App t1 t2') | None, None => None end | Abs t1 => let red_t1 := reduce t1 in match red_t1 with | Some t1' => Some (Abs t1') | None => None end end. *) Require Import Program. Program Fixpoint reduce M {measure (depth M)} := match M with | Var _ => None | App (Abs u) v => Some (beta u v) | App t1 t2 => let red_t1 := reduce t1 in let red_t2 := reduce t2 in (*match reduce_cbn t1, reduce_cbn t2 with*) match red_t1,red_t2 with | Some t1', _ => Some (App t1' t2) | None, Some t2' => Some (App t1 t2') | None, None => None end | Abs t1 => let red_t1 := reduce t1 in match red_t1 with | Some t1' => Some (Abs t1') | None => None end end. Require Import Omega. Obligations. Obligation 1. Proof. simpl; omega. Qed. Obligation 2. Proof. simpl; omega. Qed. Eval compute in reduce (Abs (App (Abs (Var 0)) (Var 5))). Eval compute in reduce (App (Abs (Abs (Var 1))) (Abs (Var 0))). Eval compute in reduce (Abs (App (Abs (Abs (Var 1))) (Abs (Var 0)))). Eval compute in reduce (App (Abs (App (App (Var 1) (Var 0)) (Var 2))) (Abs (Var 0))). End Index. ```