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