Nominal/NewAlpha.thy
author Christian Urban <urbanc@in.tum.de>
Tue, 04 May 2010 14:38:07 +0100
changeset 2048 20be95dce643
parent 2010 19fe16dd36c2
child 2073 2bfd5be8578a
permissions -rw-r--r--
increased step counter so that all steps go through
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1992
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     1
theory NewAlpha
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     2
imports "NewFv"
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     3
begin
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     4
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     5
(* Given [fv1, fv2, fv3]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     6
   produces %(x, y, z). fv1 x u fv2 y u fv3 z *)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     7
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     8
fun mk_compound_fv fvs =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     9
let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    10
  val nos = (length fvs - 1) downto 0;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    11
  val fvs_applied = map (fn (fv, no) => fv $ Bound no) (fvs ~~ nos);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    12
  val fvs_union = mk_union fvs_applied;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    13
  val (tyh :: tys) = rev (map (domain_type o fastype_of) fvs);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    14
  fun fold_fun ty t = HOLogic.mk_split (Abs ("", ty, t))
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    15
in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    16
  fold fold_fun tys (Abs ("", tyh, fvs_union))
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    17
end;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    18
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    19
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    20
(* Given [R1, R2, R3]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    21
   produces %(x,x'). %(y,y'). %(z,z'). R x x' \<and> R y y' \<and> R z z' *)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    22
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    23
fun mk_compound_alpha Rs =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    24
let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    25
  val nos = (length Rs - 1) downto 0;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    26
  val nos2 = (2 * length Rs - 1) downto length Rs;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    27
  val Rs_applied = map (fn (R, (no2, no)) => R $ Bound no2 $ Bound no)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    28
    (Rs ~~ (nos2 ~~ nos));
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    29
  val Rs_conj = foldr1 HOLogic.mk_conj Rs_applied;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    30
  val (tyh :: tys) = rev (map (domain_type o fastype_of) Rs);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    31
  fun fold_fun ty t = HOLogic.mk_split (Abs ("", ty, t))
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    32
  val abs_rhs = fold fold_fun tys (Abs ("", tyh, Rs_conj))
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    33
in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    34
  fold fold_fun tys (Abs ("", tyh, abs_rhs))
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    35
end;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    36
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    37
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    38
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    39
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    40
fun alpha_bm_lsts lthy dt_descr sorts dts args args2 alpha_frees fv_frees
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    41
  bn_alphabn alpha_const binds bodys =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    42
let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    43
  val thy = ProofContext.theory_of lthy;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    44
  fun bind_set args (NONE, no) = setify thy (nth args no)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    45
    | bind_set args (SOME f, no) = f $ (nth args no)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    46
  fun bind_lst args (NONE, no) = listify thy (nth args no)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    47
    | bind_lst args (SOME f, no) = f $ (nth args no)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    48
  fun append (t1, t2) =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    49
    Const(@{const_name append}, @{typ "atom list \<Rightarrow> atom list \<Rightarrow> atom list"}) $ t1 $ t2;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    50
  fun binds_fn args nos =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    51
    if alpha_const = @{const_name alpha_lst}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    52
    then foldr1 append (map (bind_lst args) nos)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    53
    else mk_union (map (bind_set args) nos);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    54
  val lhs_binds = binds_fn args binds;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    55
  val rhs_binds = binds_fn args2 binds;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    56
  val lhs_bodys = foldr1 HOLogic.mk_prod (map (nth args) bodys);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    57
  val rhs_bodys = foldr1 HOLogic.mk_prod (map (nth args2) bodys);
2010
19fe16dd36c2 replaced make_pair with library function HOLogic.mk_prod
Christian Urban <urbanc@in.tum.de>
parents: 1996
diff changeset
    58
  val lhs = HOLogic.mk_prod (lhs_binds, lhs_bodys);
19fe16dd36c2 replaced make_pair with library function HOLogic.mk_prod
Christian Urban <urbanc@in.tum.de>
parents: 1996
diff changeset
    59
  val rhs = HOLogic.mk_prod (rhs_binds, rhs_bodys);
1992
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    60
  val body_dts = map (nth dts) bodys;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    61
  fun fv_for_dt dt =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    62
    if Datatype_Aux.is_rec_type dt
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    63
    then nth fv_frees (Datatype_Aux.body_index dt)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    64
    else Const (@{const_name supp},
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    65
      Datatype_Aux.typ_of_dtyp dt_descr sorts dt --> @{typ "atom set"})
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    66
  val fvs = map fv_for_dt body_dts;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    67
  val fv = mk_compound_fv fvs;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    68
  fun alpha_for_dt dt =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    69
    if Datatype_Aux.is_rec_type dt
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    70
    then nth alpha_frees (Datatype_Aux.body_index dt)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    71
    else Const (@{const_name "op ="},
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    72
      Datatype_Aux.typ_of_dtyp dt_descr sorts dt -->
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    73
      Datatype_Aux.typ_of_dtyp dt_descr sorts dt --> @{typ bool})
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    74
  val alphas = map alpha_for_dt body_dts;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    75
  val alpha = mk_compound_alpha alphas;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    76
  val alpha_gen_pre = Const (alpha_const, dummyT) $ lhs $ alpha $ fv $ (Bound 0) $ rhs
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    77
  val alpha_gen_ex = HOLogic.exists_const @{typ perm} $ Abs ("p", @{typ perm}, alpha_gen_pre)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    78
  val t = Syntax.check_term lthy alpha_gen_ex
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    79
in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    80
  case binds of
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    81
    [(SOME bn, i)] => if i mem bodys then [t]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    82
      else [t, ((the (AList.lookup (op=) bn_alphabn bn)) $ nth args i $ nth args2 i)]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    83
    | _ => [t]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    84
end
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    85
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    86
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    87
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    88
fun alpha_bn_bm lthy dt_descr sorts dts args args2 alpha_frees fv_frees bn_alphabn args_in_bn bm =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    89
case bm of
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    90
  BEmy i =>
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    91
    let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    92
      val arg = nth args i;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    93
      val arg2 = nth args2 i;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    94
      val dt = nth dts i;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    95
    in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    96
      case AList.lookup (op=) args_in_bn i of
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    97
        NONE => if Datatype_Aux.is_rec_type dt
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    98
                then [(nth alpha_frees (Datatype_Aux.body_index dt)) $ arg $ arg2]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    99
                else [HOLogic.mk_eq (arg, arg2)]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   100
      | SOME (SOME (f : term)) => [(the (AList.lookup (op=) bn_alphabn f)) $ arg $ arg2]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   101
      | SOME NONE => []
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   102
    end
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   103
| BLst (x, y) => alpha_bm_lsts lthy dt_descr sorts dts args args2 alpha_frees
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   104
    fv_frees bn_alphabn @{const_name alpha_lst} x y
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   105
| BSet (x, y) => alpha_bm_lsts lthy dt_descr sorts dts args args2 alpha_frees
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   106
    fv_frees bn_alphabn @{const_name alpha_gen} x y
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   107
| BRes (x, y) => alpha_bm_lsts lthy dt_descr sorts dts args args2 alpha_frees
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   108
    fv_frees bn_alphabn @{const_name alpha_res} x y
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   109
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   110
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   111
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   112
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   113
fun alpha_bn lthy dt_descr sorts alpha_frees fv_frees bn_alphabn bclausess
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   114
  (alphabn, (_, ith_dtyp, args_in_bns)) =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   115
let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   116
  fun alpha_bn_constr (cname, dts) (args_in_bn, bclauses) =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   117
  let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   118
    val Ts = map (Datatype_Aux.typ_of_dtyp dt_descr sorts) dts;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   119
    val names = Datatype_Prop.make_tnames Ts;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   120
    val names2 = Name.variant_list names (Datatype_Prop.make_tnames Ts);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   121
    val args = map Free (names ~~ Ts);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   122
    val args2 = map Free (names2 ~~ Ts);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   123
    val c = Const (cname, Ts ---> (nth_dtyp dt_descr sorts ith_dtyp));
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   124
    val alpha_bn_bm = alpha_bn_bm lthy dt_descr sorts dts args args2 alpha_frees
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   125
      fv_frees bn_alphabn args_in_bn;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   126
    val rhs = HOLogic.mk_Trueprop
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   127
      (alphabn $ (list_comb (c, args)) $ (list_comb (c, args2)));
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   128
    val lhss = map HOLogic.mk_Trueprop (flat (map alpha_bn_bm bclauses))
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   129
  in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   130
    Library.foldr Logic.mk_implies (lhss, rhs)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   131
  end;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   132
  val (_, (_, _, constrs)) = nth dt_descr ith_dtyp;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   133
in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   134
  map2 alpha_bn_constr constrs (args_in_bns ~~ bclausess)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   135
end
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   136
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   137
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   138
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   139
fun alpha_bns lthy dt_descr sorts alpha_frees fv_frees bn_funs bclausesss =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   140
let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   141
  fun mk_alphabn_free (bn, ith, _) =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   142
    let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   143
      val alphabn_name = "alpha_" ^ (Long_Name.base_name (fst (dest_Const bn)));
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   144
      val ty = nth_dtyp dt_descr sorts ith;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   145
      val alphabn_type = ty --> ty --> @{typ bool};
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   146
      val alphabn_free = Free(alphabn_name, alphabn_type);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   147
    in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   148
      (alphabn_name, alphabn_free)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   149
    end;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   150
  val (alphabn_names, alphabn_frees) = split_list (map mk_alphabn_free bn_funs);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   151
  val bn_alphabn = (map (fn (bn, _, _) => bn) bn_funs) ~~ alphabn_frees
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   152
  val bclausessl = map (fn (_, i, _) => nth bclausesss i) bn_funs;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   153
  val eqs = map2 (alpha_bn lthy dt_descr sorts alpha_frees fv_frees bn_alphabn) bclausessl
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   154
    (alphabn_frees ~~ bn_funs);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   155
in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   156
  (bn_alphabn, alphabn_names, eqs)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   157
end
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   158
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   159
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   160
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   161
fun alpha_bm lthy dt_descr sorts dts args args2 alpha_frees fv_frees bn_alphabn bm =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   162
case bm of
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   163
  BEmy i =>
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   164
    let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   165
      val arg = nth args i;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   166
      val arg2 = nth args2 i;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   167
      val dt = nth dts i;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   168
    in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   169
      if Datatype_Aux.is_rec_type dt
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   170
      then [(nth alpha_frees (Datatype_Aux.body_index dt)) $ arg $ arg2]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   171
      else [HOLogic.mk_eq (arg, arg2)]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   172
    end
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   173
| BLst (x, y) => alpha_bm_lsts lthy dt_descr sorts dts args args2 alpha_frees
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   174
    fv_frees bn_alphabn @{const_name alpha_lst} x y
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   175
| BSet (x, y) => alpha_bm_lsts lthy dt_descr sorts dts args args2 alpha_frees
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   176
    fv_frees bn_alphabn @{const_name alpha_gen} x y
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   177
| BRes (x, y) => alpha_bm_lsts lthy dt_descr sorts dts args args2 alpha_frees
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   178
    fv_frees bn_alphabn @{const_name alpha_res} x y
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   179
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   180
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   181
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   182
fun alpha lthy dt_descr sorts alpha_frees fv_frees bn_alphabn bclausess (alpha_free, ith_dtyp) =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   183
let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   184
  fun alpha_constr (cname, dts) bclauses =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   185
  let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   186
    val Ts = map (Datatype_Aux.typ_of_dtyp dt_descr sorts) dts;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   187
    val names = Datatype_Prop.make_tnames Ts;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   188
    val names2 = Name.variant_list names (Datatype_Prop.make_tnames Ts);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   189
    val args = map Free (names ~~ Ts);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   190
    val args2 = map Free (names2 ~~ Ts);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   191
    val c = Const (cname, Ts ---> (nth_dtyp dt_descr sorts ith_dtyp));
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   192
    val alpha_bm = alpha_bm lthy dt_descr sorts dts args args2 alpha_frees fv_frees bn_alphabn
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   193
    val rhs = HOLogic.mk_Trueprop
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   194
      (alpha_free $ (list_comb (c, args)) $ (list_comb (c, args2)));
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   195
    val lhss = map HOLogic.mk_Trueprop (flat (map alpha_bm bclauses))
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   196
  in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   197
    Library.foldr Logic.mk_implies (lhss, rhs)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   198
  end;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   199
  val (_, (_, _, constrs)) = nth dt_descr ith_dtyp;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   200
in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   201
  map2 alpha_constr constrs bclausess
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   202
end
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   203
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   204
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   205
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   206
fun define_raw_alpha (dt_info : Datatype_Aux.info) bn_funs bclausesss fv_frees lthy =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   207
let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   208
  val {descr as dt_descr, sorts, ...} = dt_info;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   209
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   210
  val alpha_names = prefix_dt_names dt_descr sorts "alpha_";
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   211
  val alpha_types = map (fn (i, _) =>
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   212
    nth_dtyp dt_descr sorts i --> nth_dtyp dt_descr sorts i --> @{typ bool}) dt_descr;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   213
  val alpha_frees = map Free (alpha_names ~~ alpha_types);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   214
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   215
  val (bn_alphabn, alpha_bn_names, alpha_bn_eqs) =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   216
    alpha_bns lthy dt_descr sorts alpha_frees fv_frees bn_funs bclausesss
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   217
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   218
  val alpha_bns = map snd bn_alphabn;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   219
  val alpha_bn_types = map fastype_of alpha_bns;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   220
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   221
  val alpha_nums = 0 upto (length alpha_frees - 1)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   222
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   223
  val alpha_eqs = map2 (alpha lthy dt_descr sorts alpha_frees fv_frees bn_alphabn) bclausesss
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   224
    (alpha_frees ~~ alpha_nums);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   225
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   226
  val all_alpha_names = map2 (fn s => fn ty => ((Binding.name s, ty), NoSyn))
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   227
    (alpha_names @ alpha_bn_names) (alpha_types @ alpha_bn_types)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   228
  val all_alpha_eqs = map (pair Attrib.empty_binding) (flat alpha_eqs @ flat alpha_bn_eqs)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   229
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   230
  val (alphas, lthy') = Inductive.add_inductive_i
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   231
     {quiet_mode = true, verbose = false, alt_name = Binding.empty,
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   232
      coind = false, no_elim = false, no_ind = false, skip_mono = true, fork_mono = false}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   233
     all_alpha_names [] all_alpha_eqs [] lthy
1996
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   234
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   235
  val alpha_ts_loc = #preds alphas;
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   236
  val alpha_induct_loc = #induct alphas;
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   237
  val alpha_intros_loc = #intrs alphas;
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   238
  val alpha_cases_loc = #elims alphas;
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   239
  val morphism = ProofContext.export_morphism lthy' lthy;
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   240
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   241
  val alpha_ts = map (Morphism.term morphism) alpha_ts_loc;
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   242
  val alpha_induct = Morphism.thm morphism alpha_induct_loc;
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   243
  val alpha_intros = Morphism.fact morphism alpha_intros_loc
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   244
  val alpha_cases = Morphism.fact morphism alpha_cases_loc
1992
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   245
in
1996
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   246
  (((alpha_ts, alpha_intros), (alpha_cases, alpha_induct)), lthy')
1992
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   247
end
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   248
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   249
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   250
end