src/Doc/Tutorial/Types/Overloading.thy
author wenzelm
Sat, 05 Jan 2019 17:24:33 +0100
changeset 69597 ff784d5a5bfb
parent 69505 cc2d676d5395
child 72991 d0a0b74f0ad7
permissions -rw-r--r--
isabelle update -u control_cartouches;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
     1
(*<*)theory Overloading imports Main Setup begin
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
     2
36176
3fe7e97ccca8 replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents: 31707
diff changeset
     3
hide_class (open) plus (*>*)
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
     4
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
     5
text \<open>Type classes allow \emph{overloading}; thus a constant may
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
     6
have multiple definitions at non-overlapping types.\<close>
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
     7
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
     8
subsubsection \<open>Overloading\<close>
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
     9
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    10
text \<open>We can introduce a binary infix addition operator \<open>\<oplus>\<close>
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
    11
for arbitrary types by means of a type class:\<close>
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    12
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    13
class plus =
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    14
  fixes plus :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<oplus>" 70)
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    15
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
    16
text \<open>\noindent This introduces a new class @{class [source] plus},
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    17
along with a constant @{const [source] plus} with nice infix syntax.
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    18
@{const [source] plus} is also named \emph{class operation}.  The type
31682
358cdcdf56d2 corrected some issues
haftmann
parents: 31680
diff changeset
    19
of @{const [source] plus} carries a class constraint @{typ [source] "'a
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    20
:: plus"} on its type variable, meaning that only types of class
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    21
@{class [source] plus} can be instantiated for @{typ [source] "'a"}.
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    22
To breathe life into @{class [source] plus} we need to declare a type
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
    23
to be an \bfindex{instance} of @{class [source] plus}:\<close>
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    24
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    25
instantiation nat :: plus
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    26
begin
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    27
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
    28
text \<open>\noindent Command \isacommand{instantiation} opens a local
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    29
theory context.  Here we can now instantiate @{const [source] plus} on
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    30
\<^typ>\<open>nat\<close>:\<close>
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    31
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    32
primrec plus_nat :: "nat \<Rightarrow> nat \<Rightarrow> nat" where
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    33
    "(0::nat) \<oplus> n = n"
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    34
  | "Suc m \<oplus> n = Suc (m \<oplus> n)"
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    35
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
    36
text \<open>\noindent Note that the name @{const [source] plus} carries a
69505
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    37
suffix \<open>_nat\<close>; by default, the local name of a class operation
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    38
\<open>f\<close> to be instantiated on type constructor \<open>\<kappa>\<close> is mangled
cc2d676d5395 isabelle update_cartouches -t;
wenzelm
parents: 67406
diff changeset
    39
as \<open>f_\<kappa>\<close>.  In case of uncertainty, these names may be inspected
58842
22b87ab47d3b discontinued Proof General;
wenzelm
parents: 54665
diff changeset
    40
using the @{command "print_context"} command.
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    41
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    42
Although class @{class [source] plus} has no axioms, the instantiation must be
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
    43
formally concluded by a (trivial) instantiation proof ``..'':\<close>
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    44
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    45
instance ..
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    46
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
    47
text \<open>\noindent More interesting \isacommand{instance} proofs will
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    48
arise below.
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    49
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
    50
The instantiation is finished by an explicit\<close>
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    51
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    52
end
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    53
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    54
text \<open>\noindent From now on, terms like \<^term>\<open>Suc (m \<oplus> 2)\<close> are
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
    55
legal.\<close>
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    56
38325
6daf896bca5e * -> prod
haftmann
parents: 36176
diff changeset
    57
instantiation prod :: (plus, plus) plus
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    58
begin
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    59
69597
ff784d5a5bfb isabelle update -u control_cartouches;
wenzelm
parents: 69505
diff changeset
    60
text \<open>\noindent Here we instantiate the product type \<^type>\<open>prod\<close> to
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    61
class @{class [source] plus}, given that its type arguments are of
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
    62
class @{class [source] plus}:\<close>
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    63
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    64
fun plus_prod :: "'a \<times> 'b \<Rightarrow> 'a \<times> 'b \<Rightarrow> 'a \<times> 'b" where
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    65
  "(x, y) \<oplus> (w, z) = (x \<oplus> w, y \<oplus> z)"
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    66
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
    67
text \<open>\noindent Obviously, overloaded specifications may include
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
    68
recursion over the syntactic structure of types.\<close>
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    69
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    70
instance ..
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    71
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    72
end
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    73
67406
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
    74
text \<open>\noindent This way we have encoded the canonical lifting of
23307fd33906 isabelle update_cartouches -c;
wenzelm
parents: 58842
diff changeset
    75
binary operations to products by means of type classes.\<close>
31680
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    76
614a8c4c9c0f reworked section on type classes
haftmann
parents:
diff changeset
    77
(*<*)end(*>*)