[ create a new paste ] login | about

Link: http://codepad.org/zgJrrbND    [ raw code | fork ]

OCaml, pasted on Jul 27:
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.


Create a new paste based on this one


Comments: