src/HOL/Examples/Adhoc_Overloading.thy
changeset 81995 d67dadd69d07
parent 81989 96afb0707532
equal deleted inserted replaced
81994:5e50a2b52809 81995:d67dadd69d07
    33 
    33 
    34 consts vars :: "'a \<Rightarrow> 'b set"
    34 consts vars :: "'a \<Rightarrow> 'b set"
    35 
    35 
    36 text \<open>Which is then overloaded with variants for terms, rules, and TRSs.\<close>
    36 text \<open>Which is then overloaded with variants for terms, rules, and TRSs.\<close>
    37 adhoc_overloading
    37 adhoc_overloading
    38   vars term_vars
    38   vars \<rightleftharpoons> term_vars
    39 
    39 
    40 value [nbe] "vars (Fun ''f'' [Var 0, Var 1])"
    40 value [nbe] "vars (Fun ''f'' [Var 0, Var 1])"
    41 
    41 
    42 fun rule_vars :: "('a, 'b) term \<times> ('a, 'b) term \<Rightarrow> 'b set" where
    42 fun rule_vars :: "('a, 'b) term \<times> ('a, 'b) term \<Rightarrow> 'b set" where
    43   "rule_vars (l, r) = vars l \<union> vars r"
    43   "rule_vars (l, r) = vars l \<union> vars r"
    44 
    44 
    45 adhoc_overloading
    45 adhoc_overloading
    46   vars rule_vars
    46   vars \<rightleftharpoons> rule_vars
    47 
    47 
    48 value [nbe] "vars (Var 1, Var 0)"
    48 value [nbe] "vars (Var 1, Var 0)"
    49 
    49 
    50 definition trs_vars :: "(('a, 'b) term \<times> ('a, 'b) term) set \<Rightarrow> 'b set" where
    50 definition trs_vars :: "(('a, 'b) term \<times> ('a, 'b) term) set \<Rightarrow> 'b set" where
    51   "trs_vars R = \<Union>(rule_vars ` R)"
    51   "trs_vars R = \<Union>(rule_vars ` R)"
    52 
    52 
    53 adhoc_overloading
    53 adhoc_overloading
    54   vars trs_vars
    54   vars \<rightleftharpoons> trs_vars
    55 
    55 
    56 value [nbe] "vars {(Var 1, Var 0)}"
    56 value [nbe] "vars {(Var 1, Var 0)}"
    57 
    57 
    58 text \<open>Sometimes it is necessary to add explicit type constraints
    58 text \<open>Sometimes it is necessary to add explicit type constraints
    59 before a variant can be determined.\<close>
    59 before a variant can be determined.\<close>
    60 (*value "vars R" (*has multiple instances*)*)
    60 (*value "vars R" (*has multiple instances*)*)
    61 value "vars (R :: (('a, 'b) term \<times> ('a, 'b) term) set)"
    61 value "vars (R :: (('a, 'b) term \<times> ('a, 'b) term) set)"
    62 
    62 
    63 text \<open>It is also possible to remove variants.\<close>
    63 text \<open>It is also possible to remove variants.\<close>
    64 no_adhoc_overloading
    64 no_adhoc_overloading
    65   vars term_vars rule_vars 
    65   vars \<rightleftharpoons> term_vars rule_vars 
    66 
    66 
    67 (*value "vars (Var 1)" (*does not have an instance*)*)
    67 (*value "vars (Var 1)" (*does not have an instance*)*)
    68 
    68 
    69 text \<open>As stated earlier, the overloaded constant is only used for
    69 text \<open>As stated earlier, the overloaded constant is only used for
    70 input and output. Internally, always a variant is used, as can be
    70 input and output. Internally, always a variant is used, as can be
    71 observed by the configuration option \<open>show_variants\<close>.\<close>
    71 observed by the configuration option \<open>show_variants\<close>.\<close>
    72 
    72 
    73 adhoc_overloading
    73 adhoc_overloading
    74   vars term_vars
    74   vars \<rightleftharpoons> term_vars
    75 
    75 
    76 declare [[show_variants]]
    76 declare [[show_variants]]
    77 
    77 
    78 term "vars (Var 1)" (*which yields: "term_vars (Var 1)"*)
    78 term "vars (Var 1)" (*which yields: "term_vars (Var 1)"*)
    79 
    79 
   184   assumes permute_zero [simp]: "permute 0 x = x"
   184   assumes permute_zero [simp]: "permute 0 x = x"
   185     and permute_plus [simp]: "permute (p + q) x = permute p (permute q x)"
   185     and permute_plus [simp]: "permute (p + q) x = permute p (permute q x)"
   186 begin
   186 begin
   187 
   187 
   188 adhoc_overloading
   188 adhoc_overloading
   189   PERMUTE permute
   189   PERMUTE \<rightleftharpoons> permute
   190 
   190 
   191 end
   191 end
   192 
   192 
   193 text \<open>Permuting atoms.\<close>
   193 text \<open>Permuting atoms.\<close>
   194 definition permute_atom :: "'a perm \<Rightarrow> 'a \<Rightarrow> 'a" where
   194 definition permute_atom :: "'a perm \<Rightarrow> 'a \<Rightarrow> 'a" where
   195   "permute_atom p a = (Rep_perm p) a"
   195   "permute_atom p a = (Rep_perm p) a"
   196 
   196 
   197 adhoc_overloading
   197 adhoc_overloading
   198   PERMUTE permute_atom
   198   PERMUTE \<rightleftharpoons> permute_atom
   199 
   199 
   200 interpretation atom_permute: permute permute_atom
   200 interpretation atom_permute: permute permute_atom
   201   by standard (simp_all add: permute_atom_def Rep_perm_simps)
   201   by standard (simp_all add: permute_atom_def Rep_perm_simps)
   202 
   202 
   203 text \<open>Permuting permutations.\<close>
   203 text \<open>Permuting permutations.\<close>
   204 definition permute_perm :: "'a perm \<Rightarrow> 'a perm \<Rightarrow> 'a perm" where
   204 definition permute_perm :: "'a perm \<Rightarrow> 'a perm \<Rightarrow> 'a perm" where
   205   "permute_perm p q = p + q - p"
   205   "permute_perm p q = p + q - p"
   206 
   206 
   207 adhoc_overloading
   207 adhoc_overloading
   208   PERMUTE permute_perm
   208   PERMUTE \<rightleftharpoons> permute_perm
   209 
   209 
   210 interpretation perm_permute: permute permute_perm
   210 interpretation perm_permute: permute permute_perm
   211   apply standard
   211   apply standard
   212   unfolding permute_perm_def
   212   unfolding permute_perm_def
   213   apply simp
   213   apply simp
   220   for perm1 :: "'a perm \<Rightarrow> 'b \<Rightarrow> 'b"
   220   for perm1 :: "'a perm \<Rightarrow> 'b \<Rightarrow> 'b"
   221   and perm2 :: "'a perm \<Rightarrow> 'c \<Rightarrow> 'c"
   221   and perm2 :: "'a perm \<Rightarrow> 'c \<Rightarrow> 'c"
   222 begin
   222 begin
   223 
   223 
   224 adhoc_overloading
   224 adhoc_overloading
   225   PERMUTE perm1 perm2
   225   PERMUTE \<rightleftharpoons> perm1 perm2
   226 
   226 
   227 definition permute_fun :: "'a perm \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> ('b \<Rightarrow> 'c)" where
   227 definition permute_fun :: "'a perm \<Rightarrow> ('b \<Rightarrow> 'c) \<Rightarrow> ('b \<Rightarrow> 'c)" where
   228   "permute_fun p f = (\<lambda>x. p \<bullet> (f (-p \<bullet> x)))"
   228   "permute_fun p f = (\<lambda>x. p \<bullet> (f (-p \<bullet> x)))"
   229 
   229 
   230 adhoc_overloading
   230 adhoc_overloading
   231   PERMUTE permute_fun
   231   PERMUTE \<rightleftharpoons> permute_fun
   232 
   232 
   233 end
   233 end
   234 
   234 
   235 sublocale fun_permute \<subseteq> permute permute_fun
   235 sublocale fun_permute \<subseteq> permute permute_fun
   236   by (unfold_locales, auto simp: permute_fun_def)
   236   by (unfold_locales, auto simp: permute_fun_def)
   242 
   242 
   243 interpretation atom_fun_permute: fun_permute permute_atom permute_atom
   243 interpretation atom_fun_permute: fun_permute permute_atom permute_atom
   244   by (unfold_locales)
   244   by (unfold_locales)
   245 
   245 
   246 adhoc_overloading
   246 adhoc_overloading
   247   PERMUTE atom_fun_permute.permute_fun
   247   PERMUTE \<rightleftharpoons> atom_fun_permute.permute_fun
   248 
   248 
   249 lemma "(Abs_perm id :: 'a perm) \<bullet> id = id"
   249 lemma "(Abs_perm id :: 'a perm) \<bullet> id = id"
   250   unfolding atom_fun_permute.permute_fun_def
   250   unfolding atom_fun_permute.permute_fun_def
   251   unfolding permute_atom_def
   251   unfolding permute_atom_def
   252   by (metis Rep_perm_0 id_def inj_imp_inv_eq inj_on_id uminus_perm_def zero_perm_def)
   252   by (metis Rep_perm_0 id_def inj_imp_inv_eq inj_on_id uminus_perm_def zero_perm_def)