src/HOL/ex/Adhoc_Overloading_Examples.thy
author wenzelm
Thu, 01 Sep 2016 16:05:22 +0200
changeset 63750 9c8a366778e1
parent 63532 b01154b74314
child 66345 882abe912da9
permissions -rw-r--r--
more robust persistent storage; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53538
4e9e150422d5 tuned whitespace;
wenzelm
parents: 52894
diff changeset
     1
(*  Title:      HOL/ex/Adhoc_Overloading_Examples.thy
4e9e150422d5 tuned whitespace;
wenzelm
parents: 52894
diff changeset
     2
    Author:     Christian Sternagel
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
     3
*)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
     4
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
     5
section \<open>Ad Hoc Overloading\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
     6
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
     7
theory Adhoc_Overloading_Examples
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
     8
imports
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
     9
  Main
63532
b01154b74314 provide Pure.simp/simp_all, which only know about meta-equality;
wenzelm
parents: 61933
diff changeset
    10
  "~~/src/HOL/Library/Infinite_Set"
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    11
  "~~/src/Tools/Adhoc_Overloading"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    12
begin
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    13
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
    14
text \<open>Adhoc overloading allows to overload a constant depending on
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    15
its type. Typically this involves to introduce an uninterpreted
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    16
constant (used for input and output) and then add some variants (used
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
    17
internally).\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    18
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
    19
subsection \<open>Plain Ad Hoc Overloading\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    20
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
    21
text \<open>Consider the type of first-order terms.\<close>
58310
91ea607a34d8 updated news
blanchet
parents: 58249
diff changeset
    22
datatype ('a, 'b) "term" =
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    23
  Var 'b |
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    24
  Fun 'a "('a, 'b) term list"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    25
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
    26
text \<open>The set of variables of a term might be computed as follows.\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    27
fun term_vars :: "('a, 'b) term \<Rightarrow> 'b set" where
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    28
  "term_vars (Var x) = {x}" |
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    29
  "term_vars (Fun f ts) = \<Union>set (map term_vars ts)"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    30
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
    31
text \<open>However, also for \emph{rules} (i.e., pairs of terms) and term
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    32
rewrite systems (i.e., sets of rules), the set of variables makes
61933
cf58b5b794b2 isabelle update_cartouches -c -t;
wenzelm
parents: 61343
diff changeset
    33
sense. Thus we introduce an unspecified constant \<open>vars\<close>.\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    34
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    35
consts vars :: "'a \<Rightarrow> 'b set"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    36
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
    37
text \<open>Which is then overloaded with variants for terms, rules, and TRSs.\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    38
adhoc_overloading
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    39
  vars term_vars
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    40
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    41
value "vars (Fun ''f'' [Var 0, Var 1])"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    42
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    43
fun rule_vars :: "('a, 'b) term \<times> ('a, 'b) term \<Rightarrow> 'b set" where
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    44
  "rule_vars (l, r) = vars l \<union> vars r"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    45
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    46
adhoc_overloading
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    47
  vars rule_vars
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    48
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    49
value "vars (Var 1, Var 0)"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    50
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    51
definition trs_vars :: "(('a, 'b) term \<times> ('a, 'b) term) set \<Rightarrow> 'b set" where
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    52
  "trs_vars R = \<Union>(rule_vars ` R)"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    53
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    54
adhoc_overloading
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    55
  vars trs_vars
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    56
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    57
value "vars {(Var 1, Var 0)}"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    58
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
    59
text \<open>Sometimes it is necessary to add explicit type constraints
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
    60
before a variant can be determined.\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    61
(*value "vars R" (*has multiple instances*)*)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    62
value "vars (R :: (('a, 'b) term \<times> ('a, 'b) term) set)"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    63
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
    64
text \<open>It is also possible to remove variants.\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    65
no_adhoc_overloading
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    66
  vars term_vars rule_vars 
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    67
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    68
(*value "vars (Var 1)" (*does not have an instance*)*)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    69
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
    70
text \<open>As stated earlier, the overloaded constant is only used for
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    71
input and output. Internally, always a variant is used, as can be
61933
cf58b5b794b2 isabelle update_cartouches -c -t;
wenzelm
parents: 61343
diff changeset
    72
observed by the configuration option \<open>show_variants\<close>.\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    73
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    74
adhoc_overloading
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    75
  vars term_vars
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    76
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    77
declare [[show_variants]]
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    78
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    79
term "vars (Var 1)" (*which yields: "term_vars (Var 1)"*)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    80
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    81
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
    82
subsection \<open>Adhoc Overloading inside Locales\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    83
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
    84
text \<open>As example we use permutations that are parametrized over an
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
    85
atom type @{typ "'a"}.\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    86
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    87
definition perms :: "('a \<Rightarrow> 'a) set" where
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    88
  "perms = {f. bij f \<and> finite {x. f x \<noteq> x}}"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    89
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    90
typedef 'a perm = "perms :: ('a \<Rightarrow> 'a) set"
61169
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 58889
diff changeset
    91
  by standard (auto simp: perms_def)
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    92
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
    93
text \<open>First we need some auxiliary lemmas.\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    94
lemma permsI [Pure.intro]:
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    95
  assumes "bij f" and "MOST x. f x = x"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    96
  shows "f \<in> perms"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    97
  using assms by (auto simp: perms_def) (metis MOST_iff_finiteNeg)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    98
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
    99
lemma perms_imp_bij:
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   100
  "f \<in> perms \<Longrightarrow> bij f"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   101
  by (simp add: perms_def)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   102
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   103
lemma perms_imp_MOST_eq:
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   104
  "f \<in> perms \<Longrightarrow> MOST x. f x = x"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   105
  by (simp add: perms_def) (metis MOST_iff_finiteNeg)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   106
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   107
lemma id_perms [simp]:
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   108
  "id \<in> perms"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   109
  "(\<lambda>x. x) \<in> perms"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   110
  by (auto simp: perms_def bij_def)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   111
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   112
lemma perms_comp [simp]:
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   113
  assumes f: "f \<in> perms" and g: "g \<in> perms"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   114
  shows "(f \<circ> g) \<in> perms"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   115
  apply (intro permsI bij_comp)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   116
  apply (rule perms_imp_bij [OF g])
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   117
  apply (rule perms_imp_bij [OF f])
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   118
  apply (rule MOST_rev_mp [OF perms_imp_MOST_eq [OF g]])
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   119
  apply (rule MOST_rev_mp [OF perms_imp_MOST_eq [OF f]])
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   120
  by simp
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   121
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   122
lemma perms_inv:
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   123
  assumes f: "f \<in> perms"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   124
  shows "inv f \<in> perms"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   125
  apply (rule permsI)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   126
  apply (rule bij_imp_bij_inv)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   127
  apply (rule perms_imp_bij [OF f])
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   128
  apply (rule MOST_mono [OF perms_imp_MOST_eq [OF f]])
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   129
  apply (erule subst, rule inv_f_f)
57507
a609065c9e15 revived unchecked theory (see cebaf814ca6e);
wenzelm
parents: 53538
diff changeset
   130
  apply (rule bij_is_inj [OF perms_imp_bij [OF f]])
a609065c9e15 revived unchecked theory (see cebaf814ca6e);
wenzelm
parents: 53538
diff changeset
   131
  done
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   132
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   133
lemma bij_Rep_perm: "bij (Rep_perm p)"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   134
  using Rep_perm [of p] unfolding perms_def by simp
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   135
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   136
instantiation perm :: (type) group_add
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   137
begin
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   138
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   139
definition "0 = Abs_perm id"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   140
definition "- p = Abs_perm (inv (Rep_perm p))"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   141
definition "p + q = Abs_perm (Rep_perm p \<circ> Rep_perm q)"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   142
definition "(p1::'a perm) - p2 = p1 + - p2"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   143
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   144
lemma Rep_perm_0: "Rep_perm 0 = id"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   145
  unfolding zero_perm_def by (simp add: Abs_perm_inverse)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   146
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   147
lemma Rep_perm_add:
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   148
  "Rep_perm (p1 + p2) = Rep_perm p1 \<circ> Rep_perm p2"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   149
  unfolding plus_perm_def by (simp add: Abs_perm_inverse Rep_perm)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   150
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   151
lemma Rep_perm_uminus:
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   152
  "Rep_perm (- p) = inv (Rep_perm p)"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   153
  unfolding uminus_perm_def by (simp add: Abs_perm_inverse perms_inv Rep_perm)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   154
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   155
instance
61169
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 58889
diff changeset
   156
  apply standard
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   157
  unfolding Rep_perm_inject [symmetric]
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   158
  unfolding minus_perm_def
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   159
  unfolding Rep_perm_add
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   160
  unfolding Rep_perm_uminus
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   161
  unfolding Rep_perm_0
61169
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 58889
diff changeset
   162
  apply (simp_all add: o_assoc inv_o_cancel [OF bij_is_inj [OF bij_Rep_perm]])
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 58889
diff changeset
   163
  done
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   164
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   165
end
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   166
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   167
lemmas Rep_perm_simps =
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   168
  Rep_perm_0
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   169
  Rep_perm_add
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   170
  Rep_perm_uminus
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   171
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   172
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
   173
section \<open>Permutation Types\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   174
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
   175
text \<open>We want to be able to apply permutations to arbitrary types. To
61933
cf58b5b794b2 isabelle update_cartouches -c -t;
wenzelm
parents: 61343
diff changeset
   176
this end we introduce a constant \<open>PERMUTE\<close> together with
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
   177
convenient infix syntax.\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   178
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   179
consts PERMUTE :: "'a perm \<Rightarrow> 'b \<Rightarrow> 'b" (infixr "\<bullet>" 75)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   180
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
   181
text \<open>Then we add a locale for types @{typ 'b} that support
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
   182
appliciation of permutations.\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   183
locale permute =
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   184
  fixes permute :: "'a perm \<Rightarrow> 'b \<Rightarrow> 'b"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   185
  assumes permute_zero [simp]: "permute 0 x = x"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   186
    and permute_plus [simp]: "permute (p + q) x = permute p (permute q x)"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   187
begin
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   188
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   189
adhoc_overloading
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   190
  PERMUTE permute
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   191
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   192
end
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   193
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
   194
text \<open>Permuting atoms.\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   195
definition permute_atom :: "'a perm \<Rightarrow> 'a \<Rightarrow> 'a" where
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   196
  "permute_atom p a = (Rep_perm p) a"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   197
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   198
adhoc_overloading
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   199
  PERMUTE permute_atom
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   200
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   201
interpretation atom_permute: permute permute_atom
61169
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 58889
diff changeset
   202
  by standard (simp_all add: permute_atom_def Rep_perm_simps)
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   203
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
   204
text \<open>Permuting permutations.\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   205
definition permute_perm :: "'a perm \<Rightarrow> 'a perm \<Rightarrow> 'a perm" where
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   206
  "permute_perm p q = p + q - p"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   207
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   208
adhoc_overloading
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   209
  PERMUTE permute_perm
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   210
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   211
interpretation perm_permute: permute permute_perm
61169
4de9ff3ea29a tuned proofs -- less legacy;
wenzelm
parents: 58889
diff changeset
   212
  apply standard
57507
a609065c9e15 revived unchecked theory (see cebaf814ca6e);
wenzelm
parents: 53538
diff changeset
   213
  unfolding permute_perm_def
a609065c9e15 revived unchecked theory (see cebaf814ca6e);
wenzelm
parents: 53538
diff changeset
   214
  apply simp
57512
cc97b347b301 reduced name variants for assoc and commute on plus and mult
haftmann
parents: 57507
diff changeset
   215
  apply (simp only: diff_conv_add_uminus minus_add add.assoc)
57507
a609065c9e15 revived unchecked theory (see cebaf814ca6e);
wenzelm
parents: 53538
diff changeset
   216
  done
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   217
61343
5b5656a63bd6 isabelle update_cartouches;
wenzelm
parents: 61169
diff changeset
   218
text \<open>Permuting functions.\<close>
52894
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   219
locale fun_permute =
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   220
  dom: permute perm1 + ran: permute perm2
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   221
  for perm1 :: "'a perm \<Rightarrow> 'b \<Rightarrow> 'b"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   222
  and perm2 :: "'a perm \<Rightarrow> 'c \<Rightarrow> 'c"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   223
begin
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   224
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   225
adhoc_overloading
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   226
  PERMUTE perm1 perm2
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   227
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   228
definition permute_fun :: "'a perm \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> ('b \<Rightarrow> 'c)" where
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   229
  "permute_fun p f = (\<lambda>x. p \<bullet> (f (-p \<bullet> x)))"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   230
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   231
adhoc_overloading
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   232
  PERMUTE permute_fun
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   233
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   234
end
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   235
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   236
sublocale fun_permute \<subseteq> permute permute_fun
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   237
  by (unfold_locales, auto simp: permute_fun_def)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   238
     (metis dom.permute_plus minus_add)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   239
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   240
lemma "(Abs_perm id :: nat perm) \<bullet> Suc 0 = Suc 0"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   241
  unfolding permute_atom_def
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   242
  by (metis Rep_perm_0 id_apply zero_perm_def)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   243
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   244
interpretation atom_fun_permute: fun_permute permute_atom permute_atom
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   245
  by (unfold_locales)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   246
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   247
adhoc_overloading
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   248
  PERMUTE atom_fun_permute.permute_fun
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   249
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   250
lemma "(Abs_perm id :: 'a perm) \<bullet> id = id"
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   251
  unfolding atom_fun_permute.permute_fun_def
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   252
  unfolding permute_atom_def
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   253
  by (metis Rep_perm_0 id_def inj_imp_inv_eq inj_on_id uminus_perm_def zero_perm_def)
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   254
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   255
end
cebaf814ca6e added examples of adhoc overloading
Christian Sternagel
parents:
diff changeset
   256