(* Title: HOL/Examples/Adhoc_Overloading.thy Author: Christian Sternagel
*)
section \<open>Ad Hoc Overloading\<close>
theory Adhoc_Overloading imports
Main "HOL-Library.Infinite_Set" begin
text\<open>Adhoc overloading allows to overload a constant depending on
its type. Typically this involves to introduce an uninterpreted
constant (used for input andoutput) andthen add some variants (used
internally).\<close>
subsection \<open>Plain Ad Hoc Overloading\<close>
text\<open>Consider the type of first-order terms.\<close> datatype ('a, 'b) "term" =
Var 'b | Fun'a "('a, 'b) term list"
text\<open>The set of variables of a term might be computed as follows.\<close> fun term_vars :: "('a, 'b) term \ 'b set" where "term_vars (Var x) = {x}" | "term_vars (Fun f ts) = \(set (map term_vars ts))"
text\<open>However, also for \emph{rules} (i.e., pairs of terms) and term
rewrite systems (i.e., sets of rules), the set of variables makes
sense. Thus we introduce an unspecified constant \<open>vars\<close>.\<close>
consts vars :: "'a \ 'b set"
text\<open>Which is then overloaded with variants for terms, rules, and TRSs.\<close>
adhoc_overloading
vars \<rightleftharpoons> term_vars
value [nbe] "vars (Fun ''f'' [Var 0, Var 1])"
fun rule_vars :: "('a, 'b) term \ ('a, 'b) term \ 'b set" where "rule_vars (l, r) = vars l \ vars r"
adhoc_overloading
vars \<rightleftharpoons> rule_vars
value [nbe] "vars (Var 1, Var 0)"
definition trs_vars :: "(('a, 'b) term \ ('a, 'b) term) set \ 'b set" where "trs_vars R = \(rule_vars ` R)"
adhoc_overloading
vars \<rightleftharpoons> trs_vars
value [nbe] "vars {(Var 1, Var 0)}"
text\<open>Sometimes it is necessary to add explicit type constraints
before a variant can be determined.\<close> (*value "vars R" (*has multiple instances*)*) value"vars (R :: (('a, 'b) term \ ('a, 'b) term) set)"
text\<open>It is also possible to remove variants.\<close>
no_adhoc_overloading
vars \<rightleftharpoons> term_vars rule_vars
(*value "vars (Var 1)" (*does not have an instance*)*)
text\<open>As stated earlier, the overloaded constant is only used for
input andoutput. Internally, always a variant is used, as can be
observed by the configuration option \<open>show_variants\<close>.\<close>
adhoc_overloading
vars \<rightleftharpoons> term_vars
text\<open>As example we use permutations that are parametrized over an
atom type \<^typ>\<open>'a\<close>.\<close>
definition perms :: "('a \ 'a) set" where "perms = {f. bij f \ finite {x. f x \ x}}"
typedef'a perm = "perms :: ('a \<Rightarrow> 'a) set" by standard (auto simp: perms_def)
text\<open>First we need some auxiliary lemmas.\<close> lemma permsI [Pure.intro]: assumes"bij f"and"MOST x. f x = x" shows"f \ perms" using assms by (auto simp: perms_def) (metis MOST_iff_finiteNeg)
lemma perms_imp_bij: "f \ perms \ bij f" by (simp add: perms_def)
lemma perms_imp_MOST_eq: "f \ perms \ MOST x. f x = x" by (simp add: perms_def) (metis MOST_iff_finiteNeg)
text\<open>We want to be able to apply permutations to arbitrary types. To
this end we introduce a constant \<open>PERMUTE\<close> together with
convenient infixsyntax.\<close>
text\<open>Then we add a locale for types \<^typ>\<open>'b\<close> that support
appliciation of permutations.\<close> locale permute = fixes permute :: "'a perm \ 'b \ 'b" assumes permute_zero [simp]: "permute 0 x = x" and permute_plus [simp]: "permute (p + q) x = permute p (permute q x)" begin
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.