doc-src/IsarAdvanced/Classes/Thy/Classes.thy
changeset 29705 a1ecdd8cf81c
parent 29513 363f17dee9ca
child 30134 c2640140b951
equal deleted inserted replaced
29704:9a7d84fd83c6 29705:a1ecdd8cf81c
   104   Depending on an arbitrary type @{text "\<alpha>"}, class @{text
   104   Depending on an arbitrary type @{text "\<alpha>"}, class @{text
   105   "semigroup"} introduces a binary operator @{text "(\<otimes>)"} that is
   105   "semigroup"} introduces a binary operator @{text "(\<otimes>)"} that is
   106   assumed to be associative:
   106   assumed to be associative:
   107 *}
   107 *}
   108 
   108 
   109 class %quote semigroup = type +
   109 class %quote semigroup =
   110   fixes mult :: "\<alpha> \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"    (infixl "\<otimes>" 70)
   110   fixes mult :: "\<alpha> \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>"    (infixl "\<otimes>" 70)
   111   assumes assoc: "(x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)"
   111   assumes assoc: "(x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)"
   112 
   112 
   113 text {*
   113 text {*
   114   \noindent This @{command class} specification consists of two
   114   \noindent This @{command class} specification consists of two
   347   in practice.  As stated in the introduction, classes also provide
   347   in practice.  As stated in the introduction, classes also provide
   348   a link to Isar's locale system.  Indeed, the logical core of a class
   348   a link to Isar's locale system.  Indeed, the logical core of a class
   349   is nothing else than a locale:
   349   is nothing else than a locale:
   350 *}
   350 *}
   351 
   351 
   352 class %quote idem = type +
   352 class %quote idem =
   353   fixes f :: "\<alpha> \<Rightarrow> \<alpha>"
   353   fixes f :: "\<alpha> \<Rightarrow> \<alpha>"
   354   assumes idem: "f (f x) = f x"
   354   assumes idem: "f (f x) = f x"
   355 
   355 
   356 text {*
   356 text {*
   357   \noindent essentially introduces the locale
   357   \noindent essentially introduces the locale
   539         before and after establishing the relationship
   539         before and after establishing the relationship
   540         @{text "group \<subseteq> monoid"};  transitive edges left out.}
   540         @{text "group \<subseteq> monoid"};  transitive edges left out.}
   541      \label{fig:subclass}
   541      \label{fig:subclass}
   542    \end{center}
   542    \end{center}
   543   \end{figure}
   543   \end{figure}
   544 
   544 7
   545   For illustration, a derived definition
   545   For illustration, a derived definition
   546   in @{text group} which uses @{text pow_nat}:
   546   in @{text group} which uses @{text pow_nat}:
   547 *}
   547 *}
   548 
   548 
   549 definition %quote (in group) pow_int :: "int \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>" where
   549 definition %quote (in group) pow_int :: "int \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>" where
   581   enforces the global class operation @{text "mult [nat]"}.
   581   enforces the global class operation @{text "mult [nat]"}.
   582   In the global context in example 3, the reference is
   582   In the global context in example 3, the reference is
   583   to the polymorphic global class operation @{text "mult [?\<alpha> \<Colon> semigroup]"}.
   583   to the polymorphic global class operation @{text "mult [?\<alpha> \<Colon> semigroup]"}.
   584 *}
   584 *}
   585 
   585 
   586 section {* Type classes and code generation *}
   586 section {* Further issues *}
       
   587 
       
   588 subsection {* Type classes and code generation *}
   587 
   589 
   588 text {*
   590 text {*
   589   Turning back to the first motivation for type classes,
   591   Turning back to the first motivation for type classes,
   590   namely overloading, it is obvious that overloading
   592   namely overloading, it is obvious that overloading
   591   stemming from @{command class} statements and
   593   stemming from @{command class} statements and
   611   \noindent The whole code in SML with explicit dictionary passing:
   613   \noindent The whole code in SML with explicit dictionary passing:
   612 *}
   614 *}
   613 
   615 
   614 text %quote {*@{code_stmts example (SML)}*}
   616 text %quote {*@{code_stmts example (SML)}*}
   615 
   617 
       
   618 subsection {* Inspecting the type class universe *}
       
   619 
       
   620 text {*
       
   621   To facilitate orientation in complex subclass structures,
       
   622   two diagnostics commands are provided:
       
   623 
       
   624   \begin{description}
       
   625 
       
   626     \item[@{command "print_classes"}] print a list of all classes
       
   627       together with associated operations etc.
       
   628 
       
   629     \item[@{command "class_deps"}] visualizes the subclass relation
       
   630       between all classes as a Hasse diagram.
       
   631 
       
   632   \end{description}
       
   633 *}
       
   634 
   616 end
   635 end