Nominal/NewAlpha.thy
author Cezary Kaliszyk <kaliszyk@in.tum.de>
Thu, 06 May 2010 14:13:05 +0200
changeset 2073 2bfd5be8578a
parent 2010 19fe16dd36c2
child 2074 1c866b53eb3c
permissions -rw-r--r--
compound versions with prod_rel and prod_fun, not made default yet.
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
2073
2bfd5be8578a compound versions with prod_rel and prod_fun, not made default yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2010
diff changeset
    38
ML {*
2bfd5be8578a compound versions with prod_rel and prod_fun, not made default yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2010
diff changeset
    39
fun mk_binop2 ctxt s (l, r) =
2bfd5be8578a compound versions with prod_rel and prod_fun, not made default yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2010
diff changeset
    40
  Syntax.check_term ctxt (Const (s, dummyT) $ l $ r)
2bfd5be8578a compound versions with prod_rel and prod_fun, not made default yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2010
diff changeset
    41
*}
2bfd5be8578a compound versions with prod_rel and prod_fun, not made default yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2010
diff changeset
    42
2bfd5be8578a compound versions with prod_rel and prod_fun, not made default yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2010
diff changeset
    43
ML {*
2bfd5be8578a compound versions with prod_rel and prod_fun, not made default yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2010
diff changeset
    44
fun mk_compound_fv' ctxt = foldr1 (mk_binop2 ctxt @{const_name prod_fv})
2bfd5be8578a compound versions with prod_rel and prod_fun, not made default yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2010
diff changeset
    45
fun mk_compound_alpha' ctxt = foldr1 (mk_binop2 ctxt @{const_name prod_rel})
2bfd5be8578a compound versions with prod_rel and prod_fun, not made default yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2010
diff changeset
    46
*}
1992
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    47
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    48
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    49
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
    50
  bn_alphabn alpha_const binds bodys =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    51
let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    52
  val thy = ProofContext.theory_of lthy;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    53
  fun bind_set args (NONE, no) = setify thy (nth args no)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    54
    | bind_set args (SOME f, no) = f $ (nth args no)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    55
  fun bind_lst args (NONE, no) = listify thy (nth args no)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    56
    | bind_lst args (SOME f, no) = f $ (nth args no)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    57
  fun append (t1, t2) =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    58
    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
    59
  fun binds_fn args nos =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    60
    if alpha_const = @{const_name alpha_lst}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    61
    then foldr1 append (map (bind_lst args) nos)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    62
    else mk_union (map (bind_set args) nos);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    63
  val lhs_binds = binds_fn args binds;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    64
  val rhs_binds = binds_fn args2 binds;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    65
  val lhs_bodys = foldr1 HOLogic.mk_prod (map (nth args) bodys);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    66
  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
    67
  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
    68
  val rhs = HOLogic.mk_prod (rhs_binds, rhs_bodys);
1992
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    69
  val body_dts = map (nth dts) bodys;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    70
  fun fv_for_dt dt =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    71
    if Datatype_Aux.is_rec_type dt
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    72
    then nth fv_frees (Datatype_Aux.body_index dt)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    73
    else Const (@{const_name supp},
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    74
      Datatype_Aux.typ_of_dtyp dt_descr sorts dt --> @{typ "atom set"})
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    75
  val fvs = map fv_for_dt body_dts;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    76
  val fv = mk_compound_fv fvs;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    77
  fun alpha_for_dt dt =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    78
    if Datatype_Aux.is_rec_type dt
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    79
    then nth alpha_frees (Datatype_Aux.body_index dt)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    80
    else Const (@{const_name "op ="},
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    81
      Datatype_Aux.typ_of_dtyp dt_descr sorts dt -->
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    82
      Datatype_Aux.typ_of_dtyp dt_descr sorts dt --> @{typ bool})
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    83
  val alphas = map alpha_for_dt body_dts;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    84
  val alpha = mk_compound_alpha alphas;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    85
  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
    86
  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
    87
  val t = Syntax.check_term lthy alpha_gen_ex
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    88
in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    89
  case binds of
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    90
    [(SOME bn, i)] => if i mem bodys then [t]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    91
      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
    92
    | _ => [t]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    93
end
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    94
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    95
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    96
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    97
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
    98
case bm of
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    99
  BEmy i =>
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   100
    let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   101
      val arg = nth args i;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   102
      val arg2 = nth args2 i;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   103
      val dt = nth dts i;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   104
    in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   105
      case AList.lookup (op=) args_in_bn i of
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   106
        NONE => if Datatype_Aux.is_rec_type dt
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   107
                then [(nth alpha_frees (Datatype_Aux.body_index dt)) $ arg $ arg2]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   108
                else [HOLogic.mk_eq (arg, arg2)]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   109
      | 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
   110
      | SOME NONE => []
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   111
    end
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   112
| 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
   113
    fv_frees bn_alphabn @{const_name alpha_lst} x y
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   114
| 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
   115
    fv_frees bn_alphabn @{const_name alpha_gen} x y
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   116
| 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
   117
    fv_frees bn_alphabn @{const_name alpha_res} x y
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   118
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   119
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   120
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   121
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   122
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
   123
  (alphabn, (_, ith_dtyp, args_in_bns)) =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   124
let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   125
  fun alpha_bn_constr (cname, dts) (args_in_bn, bclauses) =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   126
  let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   127
    val Ts = map (Datatype_Aux.typ_of_dtyp dt_descr sorts) dts;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   128
    val names = Datatype_Prop.make_tnames Ts;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   129
    val names2 = Name.variant_list names (Datatype_Prop.make_tnames Ts);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   130
    val args = map Free (names ~~ Ts);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   131
    val args2 = map Free (names2 ~~ Ts);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   132
    val c = Const (cname, Ts ---> (nth_dtyp dt_descr sorts ith_dtyp));
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   133
    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
   134
      fv_frees bn_alphabn args_in_bn;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   135
    val rhs = HOLogic.mk_Trueprop
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   136
      (alphabn $ (list_comb (c, args)) $ (list_comb (c, args2)));
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   137
    val lhss = map HOLogic.mk_Trueprop (flat (map alpha_bn_bm bclauses))
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   138
  in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   139
    Library.foldr Logic.mk_implies (lhss, rhs)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   140
  end;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   141
  val (_, (_, _, constrs)) = nth dt_descr ith_dtyp;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   142
in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   143
  map2 alpha_bn_constr constrs (args_in_bns ~~ bclausess)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   144
end
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   145
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   146
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   147
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   148
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
   149
let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   150
  fun mk_alphabn_free (bn, ith, _) =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   151
    let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   152
      val alphabn_name = "alpha_" ^ (Long_Name.base_name (fst (dest_Const bn)));
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   153
      val ty = nth_dtyp dt_descr sorts ith;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   154
      val alphabn_type = ty --> ty --> @{typ bool};
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   155
      val alphabn_free = Free(alphabn_name, alphabn_type);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   156
    in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   157
      (alphabn_name, alphabn_free)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   158
    end;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   159
  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
   160
  val bn_alphabn = (map (fn (bn, _, _) => bn) bn_funs) ~~ alphabn_frees
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   161
  val bclausessl = map (fn (_, i, _) => nth bclausesss i) bn_funs;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   162
  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
   163
    (alphabn_frees ~~ bn_funs);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   164
in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   165
  (bn_alphabn, alphabn_names, eqs)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   166
end
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   167
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   168
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   169
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   170
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
   171
case bm of
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   172
  BEmy i =>
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   173
    let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   174
      val arg = nth args i;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   175
      val arg2 = nth args2 i;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   176
      val dt = nth dts i;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   177
    in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   178
      if Datatype_Aux.is_rec_type dt
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   179
      then [(nth alpha_frees (Datatype_Aux.body_index dt)) $ arg $ arg2]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   180
      else [HOLogic.mk_eq (arg, arg2)]
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   181
    end
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   182
| 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
   183
    fv_frees bn_alphabn @{const_name alpha_lst} x y
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   184
| 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
   185
    fv_frees bn_alphabn @{const_name alpha_gen} x y
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   186
| 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
   187
    fv_frees bn_alphabn @{const_name alpha_res} x y
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   188
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   189
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   190
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   191
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
   192
let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   193
  fun alpha_constr (cname, dts) bclauses =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   194
  let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   195
    val Ts = map (Datatype_Aux.typ_of_dtyp dt_descr sorts) dts;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   196
    val names = Datatype_Prop.make_tnames Ts;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   197
    val names2 = Name.variant_list names (Datatype_Prop.make_tnames Ts);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   198
    val args = map Free (names ~~ Ts);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   199
    val args2 = map Free (names2 ~~ Ts);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   200
    val c = Const (cname, Ts ---> (nth_dtyp dt_descr sorts ith_dtyp));
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   201
    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
   202
    val rhs = HOLogic.mk_Trueprop
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   203
      (alpha_free $ (list_comb (c, args)) $ (list_comb (c, args2)));
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   204
    val lhss = map HOLogic.mk_Trueprop (flat (map alpha_bm bclauses))
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   205
  in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   206
    Library.foldr Logic.mk_implies (lhss, rhs)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   207
  end;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   208
  val (_, (_, _, constrs)) = nth dt_descr ith_dtyp;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   209
in
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   210
  map2 alpha_constr constrs bclausess
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   211
end
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   212
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   213
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   214
ML {*
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   215
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
   216
let
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   217
  val {descr as dt_descr, sorts, ...} = dt_info;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   218
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   219
  val alpha_names = prefix_dt_names dt_descr sorts "alpha_";
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   220
  val alpha_types = map (fn (i, _) =>
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   221
    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
   222
  val alpha_frees = map Free (alpha_names ~~ alpha_types);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   223
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   224
  val (bn_alphabn, alpha_bn_names, alpha_bn_eqs) =
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   225
    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
   226
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   227
  val alpha_bns = map snd bn_alphabn;
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   228
  val alpha_bn_types = map fastype_of alpha_bns;
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 alpha_nums = 0 upto (length alpha_frees - 1)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   231
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   232
  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
   233
    (alpha_frees ~~ alpha_nums);
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   234
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   235
  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
   236
    (alpha_names @ alpha_bn_names) (alpha_types @ alpha_bn_types)
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   237
  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
   238
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   239
  val (alphas, lthy') = Inductive.add_inductive_i
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   240
     {quiet_mode = true, verbose = false, alt_name = Binding.empty,
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   241
      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
   242
     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
   243
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   244
  val alpha_ts_loc = #preds alphas;
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   245
  val alpha_induct_loc = #induct alphas;
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   246
  val alpha_intros_loc = #intrs alphas;
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   247
  val alpha_cases_loc = #elims alphas;
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   248
  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
   249
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   250
  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
   251
  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
   252
  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
   253
  val alpha_cases = Morphism.fact morphism alpha_cases_loc
1992
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   254
in
1996
953f74f40727 Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1992
diff changeset
   255
  (((alpha_ts, alpha_intros), (alpha_cases, alpha_induct)), lthy')
1992
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   256
end
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   257
*}
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   258
e306247b5ce3 New Alpha.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   259
end