Nominal/Perm.thy
author Christian Urban <urbanc@in.tum.de>
Tue, 20 Apr 2010 08:57:13 +0200
changeset 1901 93dfd5a10e92
parent 1900 57db4ff0893b
child 1902 c68a154adca4
permissions -rw-r--r--
removed dead code (nominal cannot deal with argument types of constructors that are functions)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1159
3c6bee89d826 Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     1
theory Perm
1774
c34347ec7ab3 separated general nominal theory into separate folder
Christian Urban <urbanc@in.tum.de>
parents: 1683
diff changeset
     2
imports "../Nominal-General/Nominal2_Atoms"
1159
3c6bee89d826 Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     3
begin
3c6bee89d826 Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     4
1170
a7b4160ef463 Wrapped the permutation code.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1164
diff changeset
     5
ML {*
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
     6
fun quotient_lift_consts_export qtys spec ctxt =
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
     7
let
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
     8
  val (result, ctxt') = fold_map (Quotient_Def.quotient_lift_const qtys) spec ctxt;
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
     9
  val (ts_loc, defs_loc) = split_list result;
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
    10
  val morphism = ProofContext.export_morphism ctxt' ctxt;
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
    11
  val ts = map (Morphism.term morphism) ts_loc
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
    12
  val defs = Morphism.fact morphism defs_loc
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
    13
in
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
    14
  (ts, defs, ctxt')
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
    15
end
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
    16
*}
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
    17
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
    18
ML {*
1248
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    19
fun prove_perm_empty lthy induct perm_def perm_frees =
1247
a728e199851d Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1189
diff changeset
    20
let
1248
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    21
  val perm_types = map fastype_of perm_frees;
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    22
  val perm_indnames = Datatype_Prop.make_tnames (map body_type perm_types);
1256
6c938f84880c Restructuring the code in Perm
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1253
diff changeset
    23
  fun glc ((perm, T), x) =
1896
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1871
diff changeset
    24
    HOLogic.mk_eq (perm $ @{term "0::perm"} $ Free (x, T), Free (x, T))
1247
a728e199851d Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1189
diff changeset
    25
  val gl =
a728e199851d Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1189
diff changeset
    26
    HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
1256
6c938f84880c Restructuring the code in Perm
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1253
diff changeset
    27
      (map glc (perm_frees ~~ map body_type perm_types ~~ perm_indnames)));
1247
a728e199851d Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1189
diff changeset
    28
  fun tac _ =
a728e199851d Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1189
diff changeset
    29
    EVERY [
1896
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1871
diff changeset
    30
      Datatype_Aux.indtac induct perm_indnames 1,
1247
a728e199851d Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1189
diff changeset
    31
      ALLGOALS (asm_full_simp_tac (HOL_ss addsimps (@{thm permute_zero} :: perm_def)))
a728e199851d Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1189
diff changeset
    32
    ];
a728e199851d Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1189
diff changeset
    33
in
1896
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1871
diff changeset
    34
  Datatype_Aux.split_conj_thm (Goal.prove lthy perm_indnames [] gl tac)
1247
a728e199851d Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1189
diff changeset
    35
end;
a728e199851d Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1189
diff changeset
    36
*}
a728e199851d Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1189
diff changeset
    37
1248
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    38
ML {*
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    39
fun prove_perm_append lthy induct perm_def perm_frees =
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    40
let
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    41
  val pi1 = Free ("pi1", @{typ perm});
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    42
  val pi2 = Free ("pi2", @{typ perm});
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    43
  val perm_types = map fastype_of perm_frees
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    44
  val perm_indnames = Datatype_Prop.make_tnames (map body_type perm_types);
1256
6c938f84880c Restructuring the code in Perm
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1253
diff changeset
    45
  fun glc ((perm, T), x) =
6c938f84880c Restructuring the code in Perm
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1253
diff changeset
    46
    HOLogic.mk_eq (
1896
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1871
diff changeset
    47
      perm $ (mk_plus pi1 pi2) $ Free (x, T),
1256
6c938f84880c Restructuring the code in Perm
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1253
diff changeset
    48
      perm $ pi1 $ (perm $ pi2 $ Free (x, T)))
1896
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1871
diff changeset
    49
  val goal =
1248
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    50
    (HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
1256
6c938f84880c Restructuring the code in Perm
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1253
diff changeset
    51
      (map glc (perm_frees ~~ map body_type perm_types ~~ perm_indnames))))
1248
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    52
  fun tac _ =
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    53
    EVERY [
1896
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1871
diff changeset
    54
      Datatype_Aux.indtac induct perm_indnames 1,
1248
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    55
      ALLGOALS (asm_full_simp_tac (HOL_ss addsimps (@{thm permute_plus} :: perm_def)))
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    56
    ]
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    57
in
1896
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1871
diff changeset
    58
  Datatype_Aux.split_conj_thm (Goal.prove lthy ("pi1" :: "pi2" :: perm_indnames) [] goal tac)
1248
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    59
end;
705afaaf6fb4 More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1247
diff changeset
    60
*}
1247
a728e199851d Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1189
diff changeset
    61
1899
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    62
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    63
(* definitions of the permute function for a raw nominal datatype *)
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    64
1247
a728e199851d Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1189
diff changeset
    65
ML {*
1899
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    66
fun nth_dtyp dt_descr sorts i = 
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    67
  Datatype_Aux.typ_of_dtyp dt_descr sorts (Datatype_Aux.DtRec i);
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    68
*}
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    69
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    70
ML {*
1900
57db4ff0893b added comment about abstraction in raw permuations
Christian Urban <urbanc@in.tum.de>
parents: 1899
diff changeset
    71
(* permutation function for one argument 
57db4ff0893b added comment about abstraction in raw permuations
Christian Urban <urbanc@in.tum.de>
parents: 1899
diff changeset
    72
   
1901
93dfd5a10e92 removed dead code (nominal cannot deal with argument types of constructors that are functions)
Christian Urban <urbanc@in.tum.de>
parents: 1900
diff changeset
    73
    - in case the argument is recursive it returns 
93dfd5a10e92 removed dead code (nominal cannot deal with argument types of constructors that are functions)
Christian Urban <urbanc@in.tum.de>
parents: 1900
diff changeset
    74
93dfd5a10e92 removed dead code (nominal cannot deal with argument types of constructors that are functions)
Christian Urban <urbanc@in.tum.de>
parents: 1900
diff changeset
    75
         permute_fn p arg
93dfd5a10e92 removed dead code (nominal cannot deal with argument types of constructors that are functions)
Christian Urban <urbanc@in.tum.de>
parents: 1900
diff changeset
    76
93dfd5a10e92 removed dead code (nominal cannot deal with argument types of constructors that are functions)
Christian Urban <urbanc@in.tum.de>
parents: 1900
diff changeset
    77
    - in case the argument is non-recursive it will build
1900
57db4ff0893b added comment about abstraction in raw permuations
Christian Urban <urbanc@in.tum.de>
parents: 1899
diff changeset
    78
57db4ff0893b added comment about abstraction in raw permuations
Christian Urban <urbanc@in.tum.de>
parents: 1899
diff changeset
    79
         p o arg
57db4ff0893b added comment about abstraction in raw permuations
Christian Urban <urbanc@in.tum.de>
parents: 1899
diff changeset
    80
57db4ff0893b added comment about abstraction in raw permuations
Christian Urban <urbanc@in.tum.de>
parents: 1899
diff changeset
    81
*)
1899
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    82
fun perm_arg permute_fns pi (arg_dty, arg) =
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    83
  if Datatype_Aux.is_rec_type arg_dty 
1901
93dfd5a10e92 removed dead code (nominal cannot deal with argument types of constructors that are functions)
Christian Urban <urbanc@in.tum.de>
parents: 1900
diff changeset
    84
  then Free (nth permute_fns (Datatype_Aux.body_index arg_dty)) $ pi $ arg
1900
57db4ff0893b added comment about abstraction in raw permuations
Christian Urban <urbanc@in.tum.de>
parents: 1899
diff changeset
    85
  else mk_perm pi arg
1899
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    86
*}
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    87
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    88
ML {*
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    89
(* equation for permutation function for one constructor *)
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    90
fun perm_eq_constr thy dt_descr sorts permute_fns i (cnstr_name, dts) =
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    91
let
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    92
  val pi = Free ("p", @{typ perm})
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    93
  val arg_tys = map (Datatype_Aux.typ_of_dtyp dt_descr sorts) dts
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    94
  val arg_names = Name.variant_list ["p"] (Datatype_Prop.make_tnames arg_tys)
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    95
  val args = map Free (arg_names ~~ arg_tys)
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    96
  val cnstr = Const (cnstr_name, arg_tys ---> (nth_dtyp dt_descr sorts i))
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    97
  val lhs = Free (nth permute_fns i) $ pi $ list_comb (cnstr, args)
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    98
  val rhs = list_comb (cnstr, map (perm_arg permute_fns pi) (dts ~~ args))
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
    99
  val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   100
in
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   101
  (Attrib.empty_binding, eq)
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   102
end
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   103
*}
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   104
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   105
ML {*
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   106
(* defines the permutation functions for raw datatypes and
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   107
   proves that they are instances of pt
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   108
*)
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   109
fun define_raw_perms (dt_info : Datatype_Aux.info) dt_nos thy =
1170
a7b4160ef463 Wrapped the permutation code.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1164
diff changeset
   110
let
1899
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   111
  val {descr as dt_descr, induct, sorts, ...} = dt_info;
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   112
  val all_full_tnames = map (fn (_, (n, _, _)) => n) dt_descr;
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   113
  val full_tnames = List.take (all_full_tnames, dt_nos);
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   114
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   115
  val perm_fn_names = Datatype_Prop.indexify_names (map (fn (i, _) =>
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   116
    "permute_" ^ Datatype_Aux.name_of_typ (nth_dtyp dt_descr sorts i)) dt_descr);
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   117
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   118
  val perm_types = map (fn (i, _) => perm_ty (nth_dtyp dt_descr sorts i)) dt_descr
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   119
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   120
  val permute_fns = perm_fn_names ~~ perm_types
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   121
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   122
  fun perm_eq (i, (_, _, constrs)) = 
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   123
    map (perm_eq_constr thy dt_descr sorts permute_fns i) constrs;
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   124
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   125
  val perm_eqs = maps perm_eq dt_descr;
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   126
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   127
  val lthy =
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   128
    Theory_Target.instantiation (full_tnames, [], @{sort pt}) thy;
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   129
   
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   130
  val ((perm_frees, perm_ldef), lthy') =
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   131
    Primrec.add_primrec
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   132
      (map (fn s => (Binding.name s, NONE, NoSyn)) perm_fn_names) perm_eqs lthy;
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   133
    
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   134
  val perm_empty_thms = List.take (prove_perm_empty lthy' induct perm_ldef perm_frees, dt_nos);
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   135
  val perm_append_thms = List.take (prove_perm_append lthy' induct perm_ldef perm_frees, dt_nos)
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   136
  val perms_name = space_implode "_" perm_fn_names
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   137
  val perms_zero_bind = Binding.name (perms_name ^ "_zero")
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   138
  val perms_append_bind = Binding.name (perms_name ^ "_append")
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   139
  fun tac _ (_, simps, _) =
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   140
    (Class.intro_classes_tac []) THEN (ALLGOALS (resolve_tac simps));
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   141
  fun morphism phi (dfs, simps, fvs) =
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   142
    (map (Morphism.thm phi) dfs, map (Morphism.thm phi) simps, map (Morphism.term phi) fvs);
1170
a7b4160ef463 Wrapped the permutation code.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1164
diff changeset
   143
  in
1899
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   144
    lthy'
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   145
    |> snd o (Local_Theory.note ((perms_zero_bind, []), perm_empty_thms))
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   146
    |> snd o (Local_Theory.note ((perms_append_bind, []), perm_append_thms))
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   147
    |> Class_Target.prove_instantiation_exit_result morphism tac 
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   148
         (perm_ldef, (perm_empty_thms @ perm_append_thms), perm_frees)
1170
a7b4160ef463 Wrapped the permutation code.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1164
diff changeset
   149
  end
1899
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   150
*}
1159
3c6bee89d826 Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   151
1899
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   152
(* Test *)
1900
57db4ff0893b added comment about abstraction in raw permuations
Christian Urban <urbanc@in.tum.de>
parents: 1899
diff changeset
   153
(*atom_decl name
1899
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   154
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   155
datatype trm =
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   156
  Var "name"
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   157
| App "trm" "trm list"
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   158
| Lam "name" "trm"
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   159
| Let "bp" "trm" "trm"
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   160
and bp =
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   161
  BUnit
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   162
| BVar "name"
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   163
| BPair "bp" "bp"
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   164
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   165
setup {* fn thy =>
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   166
let 
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   167
  val info = Datatype.the_info thy "Perm.trm"
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   168
in
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   169
  define_raw_perms info 2 thy |> snd
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   170
end
1159
3c6bee89d826 Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   171
*}
3c6bee89d826 Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   172
1899
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   173
print_theorems
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   174
*)
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   175
1253
cff8a67691d2 Define lifted perms.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1249
diff changeset
   176
ML {*
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
   177
fun define_lifted_perms qtys full_tnames name_term_pairs thms thy =
1253
cff8a67691d2 Define lifted perms.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1249
diff changeset
   178
let
cff8a67691d2 Define lifted perms.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1249
diff changeset
   179
  val lthy =
cff8a67691d2 Define lifted perms.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1249
diff changeset
   180
    Theory_Target.instantiation (full_tnames, [], @{sort pt}) thy;
1683
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
   181
  val (_, _, lthy') = quotient_lift_consts_export qtys name_term_pairs lthy;
f78c820f67c3 Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1503
diff changeset
   182
  val lifted_thms = map (Quotient_Tacs.lifted qtys lthy') thms;
1253
cff8a67691d2 Define lifted perms.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1249
diff changeset
   183
  fun tac _ =
cff8a67691d2 Define lifted perms.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1249
diff changeset
   184
    Class.intro_classes_tac [] THEN
cff8a67691d2 Define lifted perms.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1249
diff changeset
   185
    (ALLGOALS (resolve_tac lifted_thms))
cff8a67691d2 Define lifted perms.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1249
diff changeset
   186
  val lthy'' = Class.prove_instantiation_instance tac lthy'
cff8a67691d2 Define lifted perms.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1249
diff changeset
   187
in
cff8a67691d2 Define lifted perms.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1249
diff changeset
   188
  Local_Theory.exit_global lthy''
cff8a67691d2 Define lifted perms.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1249
diff changeset
   189
end
cff8a67691d2 Define lifted perms.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1249
diff changeset
   190
*}
cff8a67691d2 Define lifted perms.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1249
diff changeset
   191
1342
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   192
ML {*
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   193
fun neq_to_rel r neq =
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   194
let
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   195
  val neq = HOLogic.dest_Trueprop (prop_of neq)
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   196
  val eq = HOLogic.dest_not neq
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   197
  val (lhs, rhs) = HOLogic.dest_eq eq
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   198
  val rel = r $ lhs $ rhs
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   199
  val nrel = HOLogic.mk_not rel
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   200
in
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   201
  HOLogic.mk_Trueprop nrel
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   202
end
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   203
*}
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   204
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   205
ML {*
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   206
fun neq_to_rel_tac cases distinct =
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   207
  rtac notI THEN' eresolve_tac cases THEN_ALL_NEW asm_full_simp_tac (HOL_ss addsimps distinct)
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   208
*}
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   209
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   210
ML {*
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   211
fun distinct_rel ctxt cases (dists, rel) =
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   212
let
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   213
  val ((_, thms), ctxt') = Variable.import false dists ctxt
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   214
  val terms = map (neq_to_rel rel) thms
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   215
  val nrels = map (fn t => Goal.prove ctxt' [] [] t (fn _ => neq_to_rel_tac cases dists 1)) terms
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   216
in
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   217
  Variable.export ctxt' ctxt nrels
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   218
end
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   219
*}
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   220
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   221
2b98012307f7 Lift distinct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1277
diff changeset
   222
1899
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   223
(* Test *)
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   224
(*atom_decl name
1159
3c6bee89d826 Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   225
1896
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1871
diff changeset
   226
datatype trm =
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1871
diff changeset
   227
  Var "name"
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1871
diff changeset
   228
| App "trm" "trm list"
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1871
diff changeset
   229
| Lam "name" "trm"
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1871
diff changeset
   230
| Let "bp" "trm" "trm"
1170
a7b4160ef463 Wrapped the permutation code.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1164
diff changeset
   231
and bp =
a7b4160ef463 Wrapped the permutation code.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1164
diff changeset
   232
  BUnit
1896
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1871
diff changeset
   233
| BVar "name"
996d4411e95e tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
parents: 1871
diff changeset
   234
| BPair "bp" "bp"
1159
3c6bee89d826 Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   235
1899
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   236
setup {* fn thy =>
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   237
let 
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   238
  val inf = Datatype.the_info thy "Perm.trm"
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   239
in
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   240
  define_raw_perms inf 2 thy |> snd
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   241
end
8e0bfb14f6bf optimised the code of define_raw_perm
Christian Urban <urbanc@in.tum.de>
parents: 1896
diff changeset
   242
*}
1170
a7b4160ef463 Wrapped the permutation code.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1164
diff changeset
   243
a7b4160ef463 Wrapped the permutation code.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1164
diff changeset
   244
print_theorems
a7b4160ef463 Wrapped the permutation code.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1164
diff changeset
   245
*)
a7b4160ef463 Wrapped the permutation code.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1164
diff changeset
   246
1159
3c6bee89d826 Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   247
end