doc-src/Classes/Thy/Classes.thy
changeset 38812 e527a34bf69d
parent 38322 5888841c38da
child 39664 0afaf89ab591
equal deleted inserted replaced
38811:c3511b112595 38812:e527a34bf69d
     6 
     6 
     7 text {*
     7 text {*
     8   Type classes were introduced by Wadler and Blott \cite{wadler89how}
     8   Type classes were introduced by Wadler and Blott \cite{wadler89how}
     9   into the Haskell language to allow for a reasonable implementation
     9   into the Haskell language to allow for a reasonable implementation
    10   of overloading\footnote{throughout this tutorial, we are referring
    10   of overloading\footnote{throughout this tutorial, we are referring
    11   to classical Haskell 1.0 type classes, not considering
    11   to classical Haskell 1.0 type classes, not considering later
    12   later additions in expressiveness}.
    12   additions in expressiveness}.  As a canonical example, a polymorphic
    13   As a canonical example, a polymorphic equality function
    13   equality function @{text "eq \<Colon> \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> bool"} which is overloaded on
    14   @{text "eq \<Colon> \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> bool"} which is overloaded on different
    14   different types for @{text "\<alpha>"}, which is achieved by splitting
    15   types for @{text "\<alpha>"}, which is achieved by splitting introduction
    15   introduction of the @{text eq} function from its overloaded
    16   of the @{text eq} function from its overloaded definitions by means
    16   definitions by means of @{text class} and @{text instance}
    17   of @{text class} and @{text instance} declarations:
    17   declarations: \footnote{syntax here is a kind of isabellized
    18   \footnote{syntax here is a kind of isabellized Haskell}
    18   Haskell}
    19 
    19 
    20   \begin{quote}
    20   \begin{quote}
    21 
    21 
    22   \noindent@{text "class eq where"} \\
    22   \noindent@{text "class eq where"} \\
    23   \hspace*{2ex}@{text "eq \<Colon> \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> bool"}
    23   \hspace*{2ex}@{text "eq \<Colon> \<alpha> \<Rightarrow> \<alpha> \<Rightarrow> bool"}
    39 
    39 
    40   \noindent Type variables are annotated with (finitely many) classes;
    40   \noindent Type variables are annotated with (finitely many) classes;
    41   these annotations are assertions that a particular polymorphic type
    41   these annotations are assertions that a particular polymorphic type
    42   provides definitions for overloaded functions.
    42   provides definitions for overloaded functions.
    43 
    43 
    44   Indeed, type classes not only allow for simple overloading
    44   Indeed, type classes not only allow for simple overloading but form
    45   but form a generic calculus, an instance of order-sorted
    45   a generic calculus, an instance of order-sorted algebra
    46   algebra \cite{nipkow-sorts93,Nipkow-Prehofer:1993,Wenzel:1997:TPHOL}.
    46   \cite{nipkow-sorts93,Nipkow-Prehofer:1993,Wenzel:1997:TPHOL}.
    47 
    47 
    48   From a software engineering point of view, type classes
    48   From a software engineering point of view, type classes roughly
    49   roughly correspond to interfaces in object-oriented languages like Java;
    49   correspond to interfaces in object-oriented languages like Java; so,
    50   so, it is naturally desirable that type classes do not only
    50   it is naturally desirable that type classes do not only provide
    51   provide functions (class parameters) but also state specifications
    51   functions (class parameters) but also state specifications
    52   implementations must obey.  For example, the @{text "class eq"}
    52   implementations must obey.  For example, the @{text "class eq"}
    53   above could be given the following specification, demanding that
    53   above could be given the following specification, demanding that
    54   @{text "class eq"} is an equivalence relation obeying reflexivity,
    54   @{text "class eq"} is an equivalence relation obeying reflexivity,
    55   symmetry and transitivity:
    55   symmetry and transitivity:
    56 
    56 
    63   \hspace*{2ex}@{text "sym: eq x y \<longleftrightarrow> eq x y"} \\
    63   \hspace*{2ex}@{text "sym: eq x y \<longleftrightarrow> eq x y"} \\
    64   \hspace*{2ex}@{text "trans: eq x y \<and> eq y z \<longrightarrow> eq x z"}
    64   \hspace*{2ex}@{text "trans: eq x y \<and> eq y z \<longrightarrow> eq x z"}
    65 
    65 
    66   \end{quote}
    66   \end{quote}
    67 
    67 
    68   \noindent From a theoretical point of view, type classes are lightweight
    68   \noindent From a theoretical point of view, type classes are
    69   modules; Haskell type classes may be emulated by
    69   lightweight modules; Haskell type classes may be emulated by SML
    70   SML functors \cite{classes_modules}. 
    70   functors \cite{classes_modules}.  Isabelle/Isar offers a discipline
    71   Isabelle/Isar offers a discipline of type classes which brings
    71   of type classes which brings all those aspects together:
    72   all those aspects together:
       
    73 
    72 
    74   \begin{enumerate}
    73   \begin{enumerate}
    75     \item specifying abstract parameters together with
    74     \item specifying abstract parameters together with
    76        corresponding specifications,
    75        corresponding specifications,
    77     \item instantiating those abstract parameters by a particular
    76     \item instantiating those abstract parameters by a particular
    79     \item in connection with a ``less ad-hoc'' approach to overloading,
    78     \item in connection with a ``less ad-hoc'' approach to overloading,
    80     \item with a direct link to the Isabelle module system:
    79     \item with a direct link to the Isabelle module system:
    81       locales \cite{kammueller-locales}.
    80       locales \cite{kammueller-locales}.
    82   \end{enumerate}
    81   \end{enumerate}
    83 
    82 
    84   \noindent Isar type classes also directly support code generation
    83   \noindent Isar type classes also directly support code generation in
    85   in a Haskell like fashion. Internally, they are mapped to more primitive 
    84   a Haskell like fashion. Internally, they are mapped to more
    86   Isabelle concepts \cite{Haftmann-Wenzel:2006:classes}.
    85   primitive Isabelle concepts \cite{Haftmann-Wenzel:2006:classes}.
    87 
    86 
    88   This tutorial demonstrates common elements of structured specifications
    87   This tutorial demonstrates common elements of structured
    89   and abstract reasoning with type classes by the algebraic hierarchy of
    88   specifications and abstract reasoning with type classes by the
    90   semigroups, monoids and groups.  Our background theory is that of
    89   algebraic hierarchy of semigroups, monoids and groups.  Our
    91   Isabelle/HOL \cite{isa-tutorial}, for which some
    90   background theory is that of Isabelle/HOL \cite{isa-tutorial}, for
    92   familiarity is assumed.
    91   which some familiarity is assumed.
    93 *}
    92 *}
    94 
    93 
    95 section {* A simple algebra example \label{sec:example} *}
    94 section {* A simple algebra example \label{sec:example} *}
    96 
    95 
    97 subsection {* Class definition *}
    96 subsection {* Class definition *}
   105 class %quote semigroup =
   104 class %quote semigroup =
   106   fixes mult :: "\<alpha> \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"    (infixl "\<otimes>" 70)
   105   fixes mult :: "\<alpha> \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"    (infixl "\<otimes>" 70)
   107   assumes assoc: "(x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)"
   106   assumes assoc: "(x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)"
   108 
   107 
   109 text {*
   108 text {*
   110   \noindent This @{command class} specification consists of two
   109   \noindent This @{command class} specification consists of two parts:
   111   parts: the \qn{operational} part names the class parameter
   110   the \qn{operational} part names the class parameter (@{element
   112   (@{element "fixes"}), the \qn{logical} part specifies properties on them
   111   "fixes"}), the \qn{logical} part specifies properties on them
   113   (@{element "assumes"}).  The local @{element "fixes"} and
   112   (@{element "assumes"}).  The local @{element "fixes"} and @{element
   114   @{element "assumes"} are lifted to the theory toplevel,
   113   "assumes"} are lifted to the theory toplevel, yielding the global
   115   yielding the global
       
   116   parameter @{term [source] "mult \<Colon> \<alpha>\<Colon>semigroup \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"} and the
   114   parameter @{term [source] "mult \<Colon> \<alpha>\<Colon>semigroup \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"} and the
   117   global theorem @{fact "semigroup.assoc:"}~@{prop [source] "\<And>x y
   115   global theorem @{fact "semigroup.assoc:"}~@{prop [source] "\<And>x y z \<Colon>
   118   z \<Colon> \<alpha>\<Colon>semigroup. (x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)"}.
   116   \<alpha>\<Colon>semigroup. (x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)"}.
   119 *}
   117 *}
   120 
   118 
   121 
   119 
   122 subsection {* Class instantiation \label{sec:class_inst} *}
   120 subsection {* Class instantiation \label{sec:class_inst} *}
   123 
   121 
   124 text {*
   122 text {*
   125   The concrete type @{typ int} is made a @{class semigroup}
   123   The concrete type @{typ int} is made a @{class semigroup} instance
   126   instance by providing a suitable definition for the class parameter
   124   by providing a suitable definition for the class parameter @{text
   127   @{text "(\<otimes>)"} and a proof for the specification of @{fact assoc}.
   125   "(\<otimes>)"} and a proof for the specification of @{fact assoc}.  This is
   128   This is accomplished by the @{command instantiation} target:
   126   accomplished by the @{command instantiation} target:
   129 *}
   127 *}
   130 
   128 
   131 instantiation %quote int :: semigroup
   129 instantiation %quote int :: semigroup
   132 begin
   130 begin
   133 
   131 
   141 qed
   139 qed
   142 
   140 
   143 end %quote
   141 end %quote
   144 
   142 
   145 text {*
   143 text {*
   146   \noindent @{command instantiation} defines class parameters
   144   \noindent @{command instantiation} defines class parameters at a
   147   at a particular instance using common specification tools (here,
   145   particular instance using common specification tools (here,
   148   @{command definition}).  The concluding @{command instance}
   146   @{command definition}).  The concluding @{command instance} opens a
   149   opens a proof that the given parameters actually conform
   147   proof that the given parameters actually conform to the class
   150   to the class specification.  Note that the first proof step
   148   specification.  Note that the first proof step is the @{method
   151   is the @{method default} method,
   149   default} method, which for such instance proofs maps to the @{method
   152   which for such instance proofs maps to the @{method intro_classes} method.
   150   intro_classes} method.  This reduces an instance judgement to the
   153   This reduces an instance judgement to the relevant primitive
   151   relevant primitive proof goals; typically it is the first method
   154   proof goals; typically it is the first method applied
   152   applied in an instantiation proof.
   155   in an instantiation proof.
   153 
   156 
   154   From now on, the type-checker will consider @{typ int} as a @{class
   157   From now on, the type-checker will consider @{typ int}
   155   semigroup} automatically, i.e.\ any general results are immediately
   158   as a @{class semigroup} automatically, i.e.\ any general results
   156   available on concrete instances.
   159   are immediately available on concrete instances.
   157 
   160 
   158   \medskip Another instance of @{class semigroup} yields the natural
   161   \medskip Another instance of @{class semigroup} yields the natural numbers:
   159   numbers:
   162 *}
   160 *}
   163 
   161 
   164 instantiation %quote nat :: semigroup
   162 instantiation %quote nat :: semigroup
   165 begin
   163 begin
   166 
   164 
   175 qed
   173 qed
   176 
   174 
   177 end %quote
   175 end %quote
   178 
   176 
   179 text {*
   177 text {*
   180   \noindent Note the occurence of the name @{text mult_nat}
   178   \noindent Note the occurence of the name @{text mult_nat} in the
   181   in the primrec declaration;  by default, the local name of
   179   primrec declaration; by default, the local name of a class operation
   182   a class operation @{text f} to be instantiated on type constructor
   180   @{text f} to be instantiated on type constructor @{text \<kappa>} is
   183   @{text \<kappa>} is mangled as @{text f_\<kappa>}.  In case of uncertainty,
   181   mangled as @{text f_\<kappa>}.  In case of uncertainty, these names may be
   184   these names may be inspected using the @{command "print_context"} command
   182   inspected using the @{command "print_context"} command or the
   185   or the corresponding ProofGeneral button.
   183   corresponding ProofGeneral button.
   186 *}
   184 *}
   187 
   185 
   188 subsection {* Lifting and parametric types *}
   186 subsection {* Lifting and parametric types *}
   189 
   187 
   190 text {*
   188 text {*
   191   Overloaded definitions given at a class instantiation
   189   Overloaded definitions given at a class instantiation may include
   192   may include recursion over the syntactic structure of types.
   190   recursion over the syntactic structure of types.  As a canonical
   193   As a canonical example, we model product semigroups
   191   example, we model product semigroups using our simple algebra:
   194   using our simple algebra:
       
   195 *}
   192 *}
   196 
   193 
   197 instantiation %quote prod :: (semigroup, semigroup) semigroup
   194 instantiation %quote prod :: (semigroup, semigroup) semigroup
   198 begin
   195 begin
   199 
   196 
   209 end %quote
   206 end %quote
   210 
   207 
   211 text {*
   208 text {*
   212   \noindent Associativity of product semigroups is established using
   209   \noindent Associativity of product semigroups is established using
   213   the definition of @{text "(\<otimes>)"} on products and the hypothetical
   210   the definition of @{text "(\<otimes>)"} on products and the hypothetical
   214   associativity of the type components;  these hypotheses
   211   associativity of the type components; these hypotheses are
   215   are legitimate due to the @{class semigroup} constraints imposed
   212   legitimate due to the @{class semigroup} constraints imposed on the
   216   on the type components by the @{command instance} proposition.
   213   type components by the @{command instance} proposition.  Indeed,
   217   Indeed, this pattern often occurs with parametric types
   214   this pattern often occurs with parametric types and type classes.
   218   and type classes.
       
   219 *}
   215 *}
   220 
   216 
   221 
   217 
   222 subsection {* Subclassing *}
   218 subsection {* Subclassing *}
   223 
   219 
   224 text {*
   220 text {*
   225   We define a subclass @{text monoidl} (a semigroup with a left-hand neutral)
   221   We define a subclass @{text monoidl} (a semigroup with a left-hand
   226   by extending @{class semigroup}
   222   neutral) by extending @{class semigroup} with one additional
   227   with one additional parameter @{text neutral} together
   223   parameter @{text neutral} together with its characteristic property:
   228   with its characteristic property:
       
   229 *}
   224 *}
   230 
   225 
   231 class %quote monoidl = semigroup +
   226 class %quote monoidl = semigroup +
   232   fixes neutral :: "\<alpha>" ("\<one>")
   227   fixes neutral :: "\<alpha>" ("\<one>")
   233   assumes neutl: "\<one> \<otimes> x = x"
   228   assumes neutl: "\<one> \<otimes> x = x"
   234 
   229 
   235 text {*
   230 text {*
   236   \noindent Again, we prove some instances, by
   231   \noindent Again, we prove some instances, by providing suitable
   237   providing suitable parameter definitions and proofs for the
   232   parameter definitions and proofs for the additional specifications.
   238   additional specifications.  Observe that instantiations
   233   Observe that instantiations for types with the same arity may be
   239   for types with the same arity may be simultaneous:
   234   simultaneous:
   240 *}
   235 *}
   241 
   236 
   242 instantiation %quote nat and int :: monoidl
   237 instantiation %quote nat and int :: monoidl
   243 begin
   238 begin
   244 
   239 
   307 qed
   302 qed
   308 
   303 
   309 end %quote
   304 end %quote
   310 
   305 
   311 text {*
   306 text {*
   312   \noindent To finish our small algebra example, we add a @{text group} class
   307   \noindent To finish our small algebra example, we add a @{text
   313   with a corresponding instance:
   308   group} class with a corresponding instance:
   314 *}
   309 *}
   315 
   310 
   316 class %quote group = monoidl +
   311 class %quote group = monoidl +
   317   fixes inverse :: "\<alpha> \<Rightarrow> \<alpha>"    ("(_\<div>)" [1000] 999)
   312   fixes inverse :: "\<alpha> \<Rightarrow> \<alpha>"    ("(_\<div>)" [1000] 999)
   318   assumes invl: "x\<div> \<otimes> x = \<one>"
   313   assumes invl: "x\<div> \<otimes> x = \<one>"
   336 section {* Type classes as locales *}
   331 section {* Type classes as locales *}
   337 
   332 
   338 subsection {* A look behind the scenes *}
   333 subsection {* A look behind the scenes *}
   339 
   334 
   340 text {*
   335 text {*
   341   The example above gives an impression how Isar type classes work
   336   The example above gives an impression how Isar type classes work in
   342   in practice.  As stated in the introduction, classes also provide
   337   practice.  As stated in the introduction, classes also provide a
   343   a link to Isar's locale system.  Indeed, the logical core of a class
   338   link to Isar's locale system.  Indeed, the logical core of a class
   344   is nothing other than a locale:
   339   is nothing other than a locale:
   345 *}
   340 *}
   346 
   341 
   347 class %quote idem =
   342 class %quote idem =
   348   fixes f :: "\<alpha> \<Rightarrow> \<alpha>"
   343   fixes f :: "\<alpha> \<Rightarrow> \<alpha>"
   400   assume "y = z"
   395   assume "y = z"
   401   then show "x \<otimes> y = x \<otimes> z" by simp
   396   then show "x \<otimes> y = x \<otimes> z" by simp
   402 qed
   397 qed
   403 
   398 
   404 text {*
   399 text {*
   405   \noindent Here the \qt{@{keyword "in"} @{class group}} target specification
   400   \noindent Here the \qt{@{keyword "in"} @{class group}} target
   406   indicates that the result is recorded within that context for later
   401   specification indicates that the result is recorded within that
   407   use.  This local theorem is also lifted to the global one @{fact
   402   context for later use.  This local theorem is also lifted to the
   408   "group.left_cancel:"} @{prop [source] "\<And>x y z \<Colon> \<alpha>\<Colon>group. x \<otimes> y = x \<otimes>
   403   global one @{fact "group.left_cancel:"} @{prop [source] "\<And>x y z \<Colon>
   409   z \<longleftrightarrow> y = z"}.  Since type @{text "int"} has been made an instance of
   404   \<alpha>\<Colon>group. x \<otimes> y = x \<otimes> z \<longleftrightarrow> y = z"}.  Since type @{text "int"} has been
   410   @{text "group"} before, we may refer to that fact as well: @{prop
   405   made an instance of @{text "group"} before, we may refer to that
   411   [source] "\<And>x y z \<Colon> int. x \<otimes> y = x \<otimes> z \<longleftrightarrow> y = z"}.
   406   fact as well: @{prop [source] "\<And>x y z \<Colon> int. x \<otimes> y = x \<otimes> z \<longleftrightarrow> y =
       
   407   z"}.
   412 *}
   408 *}
   413 
   409 
   414 
   410 
   415 subsection {* Derived definitions *}
   411 subsection {* Derived definitions *}
   416 
   412 
   422   "pow_nat 0 x = \<one>"
   418   "pow_nat 0 x = \<one>"
   423   | "pow_nat (Suc n) x = x \<otimes> pow_nat n x"
   419   | "pow_nat (Suc n) x = x \<otimes> pow_nat n x"
   424 
   420 
   425 text {*
   421 text {*
   426   \noindent If the locale @{text group} is also a class, this local
   422   \noindent If the locale @{text group} is also a class, this local
   427   definition is propagated onto a global definition of
   423   definition is propagated onto a global definition of @{term [source]
   428   @{term [source] "pow_nat \<Colon> nat \<Rightarrow> \<alpha>\<Colon>monoid \<Rightarrow> \<alpha>\<Colon>monoid"}
   424   "pow_nat \<Colon> nat \<Rightarrow> \<alpha>\<Colon>monoid \<Rightarrow> \<alpha>\<Colon>monoid"} with corresponding theorems
   429   with corresponding theorems
       
   430 
   425 
   431   @{thm pow_nat.simps [no_vars]}.
   426   @{thm pow_nat.simps [no_vars]}.
   432 
   427 
   433   \noindent As you can see from this example, for local
   428   \noindent As you can see from this example, for local definitions
   434   definitions you may use any specification tool
   429   you may use any specification tool which works together with
   435   which works together with locales, such as Krauss's recursive function package
   430   locales, such as Krauss's recursive function package
   436   \cite{krauss2006}.
   431   \cite{krauss2006}.
   437 *}
   432 *}
   438 
   433 
   439 
   434 
   440 subsection {* A functor analogy *}
   435 subsection {* A functor analogy *}
   441 
   436 
   442 text {*
   437 text {*
   443   We introduced Isar classes by analogy to type classes in
   438   We introduced Isar classes by analogy to type classes in functional
   444   functional programming;  if we reconsider this in the
   439   programming; if we reconsider this in the context of what has been
   445   context of what has been said about type classes and locales,
   440   said about type classes and locales, we can drive this analogy
   446   we can drive this analogy further by stating that type
   441   further by stating that type classes essentially correspond to
   447   classes essentially correspond to functors that have
   442   functors that have a canonical interpretation as type classes.
   448   a canonical interpretation as type classes.
   443   There is also the possibility of other interpretations.  For
   449   There is also the possibility of other interpretations.
   444   example, @{text list}s also form a monoid with @{text append} and
   450   For example, @{text list}s also form a monoid with
   445   @{term "[]"} as operations, but it seems inappropriate to apply to
   451   @{text append} and @{term "[]"} as operations, but it
   446   lists the same operations as for genuinely algebraic types.  In such
   452   seems inappropriate to apply to lists
   447   a case, we can simply make a particular interpretation of monoids
   453   the same operations as for genuinely algebraic types.
   448   for lists:
   454   In such a case, we can simply make a particular interpretation
       
   455   of monoids for lists:
       
   456 *}
   449 *}
   457 
   450 
   458 interpretation %quote list_monoid: monoid append "[]"
   451 interpretation %quote list_monoid: monoid append "[]"
   459   proof qed auto
   452   proof qed auto
   460 
   453 
   508   with assoc [symmetric] neutl invl have "x\<div> \<otimes> (x \<otimes> \<one>) = x\<div> \<otimes> x" by simp
   501   with assoc [symmetric] neutl invl have "x\<div> \<otimes> (x \<otimes> \<one>) = x\<div> \<otimes> x" by simp
   509   with left_cancel show "x \<otimes> \<one> = x" by simp
   502   with left_cancel show "x \<otimes> \<one> = x" by simp
   510 qed
   503 qed
   511 
   504 
   512 text {*
   505 text {*
   513   The logical proof is carried out on the locale level.
   506   The logical proof is carried out on the locale level.  Afterwards it
   514   Afterwards it is propagated
   507   is propagated to the type system, making @{text group} an instance
   515   to the type system, making @{text group} an instance of
   508   of @{text monoid} by adding an additional edge to the graph of
   516   @{text monoid} by adding an additional edge
   509   subclass relations (\figref{fig:subclass}).
   517   to the graph of subclass relations
       
   518   (\figref{fig:subclass}).
       
   519 
   510 
   520   \begin{figure}[htbp]
   511   \begin{figure}[htbp]
   521    \begin{center}
   512    \begin{center}
   522      \small
   513      \small
   523      \unitlength 0.6mm
   514      \unitlength 0.6mm
   545         @{text "group \<subseteq> monoid"};  transitive edges are left out.}
   536         @{text "group \<subseteq> monoid"};  transitive edges are left out.}
   546      \label{fig:subclass}
   537      \label{fig:subclass}
   547    \end{center}
   538    \end{center}
   548   \end{figure}
   539   \end{figure}
   549 
   540 
   550   For illustration, a derived definition
   541   For illustration, a derived definition in @{text group} using @{text
   551   in @{text group} using @{text pow_nat}
   542   pow_nat}
   552 *}
   543 *}
   553 
   544 
   554 definition %quote (in group) pow_int :: "int \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>" where
   545 definition %quote (in group) pow_int :: "int \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>" where
   555   "pow_int k x = (if k >= 0
   546   "pow_int k x = (if k >= 0
   556     then pow_nat (nat k) x
   547     then pow_nat (nat k) x
   557     else (pow_nat (nat (- k)) x)\<div>)"
   548     else (pow_nat (nat (- k)) x)\<div>)"
   558 
   549 
   559 text {*
   550 text {*
   560   \noindent yields the global definition of
   551   \noindent yields the global definition of @{term [source] "pow_int \<Colon>
   561   @{term [source] "pow_int \<Colon> int \<Rightarrow> \<alpha>\<Colon>group \<Rightarrow> \<alpha>\<Colon>group"}
   552   int \<Rightarrow> \<alpha>\<Colon>group \<Rightarrow> \<alpha>\<Colon>group"} with the corresponding theorem @{thm
   562   with the corresponding theorem @{thm pow_int_def [no_vars]}.
   553   pow_int_def [no_vars]}.
   563 *}
   554 *}
   564 
   555 
   565 subsection {* A note on syntax *}
   556 subsection {* A note on syntax *}
   566 
   557 
   567 text {*
   558 text {*
   568   As a convenience, class context syntax allows references
   559   As a convenience, class context syntax allows references to local
   569   to local class operations and their global counterparts
   560   class operations and their global counterparts uniformly; type
   570   uniformly;  type inference resolves ambiguities.  For example:
   561   inference resolves ambiguities.  For example:
   571 *}
   562 *}
   572 
   563 
   573 context %quote semigroup
   564 context %quote semigroup
   574 begin
   565 begin
   575 
   566 
   579 end  %quote
   570 end  %quote
   580 
   571 
   581 term %quote "x \<otimes> y" -- {* example 3 *}
   572 term %quote "x \<otimes> y" -- {* example 3 *}
   582 
   573 
   583 text {*
   574 text {*
   584   \noindent Here in example 1, the term refers to the local class operation
   575   \noindent Here in example 1, the term refers to the local class
   585   @{text "mult [\<alpha>]"}, whereas in example 2 the type constraint
   576   operation @{text "mult [\<alpha>]"}, whereas in example 2 the type
   586   enforces the global class operation @{text "mult [nat]"}.
   577   constraint enforces the global class operation @{text "mult [nat]"}.
   587   In the global context in example 3, the reference is
   578   In the global context in example 3, the reference is to the
   588   to the polymorphic global class operation @{text "mult [?\<alpha> \<Colon> semigroup]"}.
   579   polymorphic global class operation @{text "mult [?\<alpha> \<Colon> semigroup]"}.
   589 *}
   580 *}
   590 
   581 
   591 section {* Further issues *}
   582 section {* Further issues *}
   592 
   583 
   593 subsection {* Type classes and code generation *}
   584 subsection {* Type classes and code generation *}
   594 
   585 
   595 text {*
   586 text {*
   596   Turning back to the first motivation for type classes,
   587   Turning back to the first motivation for type classes, namely
   597   namely overloading, it is obvious that overloading
   588   overloading, it is obvious that overloading stemming from @{command
   598   stemming from @{command class} statements and
   589   class} statements and @{command instantiation} targets naturally
   599   @{command instantiation}
   590   maps to Haskell type classes.  The code generator framework
   600   targets naturally maps to Haskell type classes.
   591   \cite{isabelle-codegen} takes this into account.  If the target
   601   The code generator framework \cite{isabelle-codegen} 
   592   language (e.g.~SML) lacks type classes, then they are implemented by
   602   takes this into account.  If the target language (e.g.~SML)
   593   an explicit dictionary construction.  As example, let's go back to
   603   lacks type classes, then they
   594   the power function:
   604   are implemented by an explicit dictionary construction.
       
   605   As example, let's go back to the power function:
       
   606 *}
   595 *}
   607 
   596 
   608 definition %quote example :: int where
   597 definition %quote example :: int where
   609   "example = pow_int 10 (-2)"
   598   "example = pow_int 10 (-2)"
   610 
   599 
   617 text {*
   606 text {*
   618   \noindent The code in SML has explicit dictionary passing:
   607   \noindent The code in SML has explicit dictionary passing:
   619 *}
   608 *}
   620 text %quote {*@{code_stmts example (SML)}*}
   609 text %quote {*@{code_stmts example (SML)}*}
   621 
   610 
       
   611 text {*
       
   612   \noindent In Scala, implicts are used as dictionaries:
       
   613 *}
       
   614 (*<*)code_include %invisible Scala "Natural" -(*>*)
       
   615 text %quote {*@{code_stmts example (Scala)}*}
       
   616 
       
   617 
   622 subsection {* Inspecting the type class universe *}
   618 subsection {* Inspecting the type class universe *}
   623 
   619 
   624 text {*
   620 text {*
   625   To facilitate orientation in complex subclass structures,
   621   To facilitate orientation in complex subclass structures, two
   626   two diagnostics commands are provided:
   622   diagnostics commands are provided:
   627 
   623 
   628   \begin{description}
   624   \begin{description}
   629 
   625 
   630     \item[@{command "print_classes"}] print a list of all classes
   626     \item[@{command "print_classes"}] print a list of all classes
   631       together with associated operations etc.
   627       together with associated operations etc.