83 these annotations are assertions that a particular polymorphic type |
83 these annotations are assertions that a particular polymorphic type |
84 provides definitions for overloaded functions. |
84 provides definitions for overloaded functions. |
85 |
85 |
86 Indeed, type classes not only allow for simple overloading |
86 Indeed, type classes not only allow for simple overloading |
87 but form a generic calculus, an instance of order-sorted |
87 but form a generic calculus, an instance of order-sorted |
88 algebra \cite{Nipkow-Prehofer:1993,Nipkow:1993,Wenzel:1997}. |
88 algebra \cite{Nipkow-Prehofer:1993,nipkow-sorts93,Wenzel:1997:TPHOL}. |
89 |
89 |
90 From a software enigineering point of view, type classes |
90 From a software enigineering point of view, type classes |
91 correspond to interfaces in object-oriented languages like Java; |
91 correspond to interfaces in object-oriented languages like Java; |
92 so, it is naturally desirable that type classes do not only |
92 so, it is naturally desirable that type classes do not only |
93 provide functions (class operations) but also state specifications |
93 provide functions (class operations) but also state specifications |
113 \item specifying abstract operations togehter with |
113 \item specifying abstract operations togehter with |
114 corresponding specifications, |
114 corresponding specifications, |
115 \item instantating those abstract operations by a particular |
115 \item instantating those abstract operations by a particular |
116 type |
116 type |
117 \item in connection with a ``less ad-hoc'' approach to overloading, |
117 \item in connection with a ``less ad-hoc'' approach to overloading, |
118 \item with a direct link to the Isabelle module system (aka locales). |
118 \item with a direct link to the Isabelle module system |
|
119 (aka locales \cite{kammueller-locales}). |
119 \end{enumerate} |
120 \end{enumerate} |
120 |
121 |
121 \noindent Isar type classes also directly support code generation |
122 \noindent Isar type classes also directly support code generation |
122 in a Haskell like fashion. |
123 in a Haskell like fashion. |
123 |
124 |
124 This tutorial demonstrates common elements of structured specifications |
125 This tutorial demonstrates common elements of structured specifications |
125 and abstract reasoning with type classes by the algebraic hierarchy of |
126 and abstract reasoning with type classes by the algebraic hierarchy of |
126 semigroups, monoids and groups. Our background theory is that of |
127 semigroups, monoids and groups. Our background theory is that of |
127 Isabelle/HOL \cite{Nipkow-et-al:2002:tutorial}, for which some |
128 Isabelle/HOL \cite{isa-tutorial}, for which some |
128 familiarity is assumed. |
129 familiarity is assumed. |
129 |
130 |
130 Here we merely present the look-and-feel for end users. |
131 Here we merely present the look-and-feel for end users. |
131 Internally, those are mapped to more primitive Isabelle concepts. |
132 Internally, those are mapped to more primitive Isabelle concepts. |
132 See \cite{haftmann_wenzel2006classes} for more detail.% |
133 See \cite{Haftmann-Wenzel:2006:classes} for more detail.% |
133 \end{isamarkuptext}% |
134 \end{isamarkuptext}% |
134 \isamarkuptrue% |
135 \isamarkuptrue% |
135 % |
136 % |
136 \isamarkupsection{A simple algebra example \label{sec:example}% |
137 \isamarkupsection{A simple algebra example \label{sec:example}% |
137 } |
138 } |
660 use. This local theorem is also lifted to the global one \isa{group{\isachardot}left{\isacharunderscore}cancel{\isacharcolon}} \isa{{\isachardoublequote}{\isasymAnd}x\ y\ z\ {\isasymColon}\ {\isasymalpha}{\isasymColon}group{\isachardot}\ x\ {\isasymotimes}\ y\ {\isacharequal}\ x\ {\isasymotimes}\ z\ {\isasymlongleftrightarrow}\ y\ {\isacharequal}\ z{\isachardoublequote}}. Since type \isa{int} has been made an instance of |
661 use. This local theorem is also lifted to the global one \isa{group{\isachardot}left{\isacharunderscore}cancel{\isacharcolon}} \isa{{\isachardoublequote}{\isasymAnd}x\ y\ z\ {\isasymColon}\ {\isasymalpha}{\isasymColon}group{\isachardot}\ x\ {\isasymotimes}\ y\ {\isacharequal}\ x\ {\isasymotimes}\ z\ {\isasymlongleftrightarrow}\ y\ {\isacharequal}\ z{\isachardoublequote}}. Since type \isa{int} has been made an instance of |
661 \isa{group} before, we may refer to that fact as well: \isa{{\isachardoublequote}{\isasymAnd}x\ y\ z\ {\isasymColon}\ int{\isachardot}\ x\ {\isasymotimes}\ y\ {\isacharequal}\ x\ {\isasymotimes}\ z\ {\isasymlongleftrightarrow}\ y\ {\isacharequal}\ z{\isachardoublequote}}.% |
662 \isa{group} before, we may refer to that fact as well: \isa{{\isachardoublequote}{\isasymAnd}x\ y\ z\ {\isasymColon}\ int{\isachardot}\ x\ {\isasymotimes}\ y\ {\isacharequal}\ x\ {\isasymotimes}\ z\ {\isasymlongleftrightarrow}\ y\ {\isacharequal}\ z{\isachardoublequote}}.% |
662 \end{isamarkuptext}% |
663 \end{isamarkuptext}% |
663 \isamarkuptrue% |
664 \isamarkuptrue% |
664 % |
665 % |
|
666 \isamarkupsubsection{Derived definitions% |
|
667 } |
|
668 \isamarkuptrue% |
|
669 % |
|
670 \begin{isamarkuptext}% |
|
671 Isabelle locales support a concept of local definitions |
|
672 in locales:% |
|
673 \end{isamarkuptext}% |
|
674 \isamarkuptrue% |
|
675 \ \ \ \ \isacommand{fun}\isamarkupfalse% |
|
676 \ {\isacharparenleft}\isakeyword{in}\ monoid{\isacharparenright}\isanewline |
|
677 \ \ \ \ \ \ pow{\isacharunderscore}nat\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ {\isasymalpha}\ {\isasymRightarrow}\ {\isasymalpha}{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
678 \ \ \ \ \ \ {\isachardoublequoteopen}pow{\isacharunderscore}nat\ {\isadigit{0}}\ x\ {\isacharequal}\ \isactrlloc {\isasymone}{\isachardoublequoteclose}\isanewline |
|
679 \ \ \ \ \ \ {\isacharbar}\ {\isachardoublequoteopen}pow{\isacharunderscore}nat\ {\isacharparenleft}Suc\ n{\isacharparenright}\ x\ {\isacharequal}\ x\ \isactrlloc {\isasymotimes}\ pow{\isacharunderscore}nat\ n\ x{\isachardoublequoteclose}% |
|
680 \begin{isamarkuptext}% |
|
681 \noindent If the locale \isa{group} is also a class, this local |
|
682 definition is propagated onto a global definition of |
|
683 \isa{{\isachardoublequote}pow{\isacharunderscore}nat\ {\isasymColon}\ nat\ {\isasymRightarrow}\ {\isasymalpha}{\isasymColon}monoid\ {\isasymRightarrow}\ {\isasymalpha}{\isasymColon}monoid{\isachardoublequote}} |
|
684 with corresponding theorems |
|
685 |
|
686 \isa{pow{\isacharunderscore}nat\ {\isadigit{0}}\ x\ {\isacharequal}\ {\isasymone}\isasep\isanewline% |
|
687 pow{\isacharunderscore}nat\ {\isacharparenleft}Suc\ n{\isacharparenright}\ x\ {\isacharequal}\ x\ {\isasymotimes}\ pow{\isacharunderscore}nat\ n\ x}. |
|
688 |
|
689 \noindent As you can see from this example, for local |
|
690 definitions you may use any specification tool |
|
691 which works together with locales (e.g. \cite{krauss2006}).% |
|
692 \end{isamarkuptext}% |
|
693 \isamarkuptrue% |
|
694 % |
665 \isamarkupsection{Further issues% |
695 \isamarkupsection{Further issues% |
666 } |
696 } |
667 \isamarkuptrue% |
697 \isamarkuptrue% |
668 % |
698 % |
669 \isamarkupsubsection{Code generation% |
699 \isamarkupsubsection{Code generation% |
677 statements naturally maps to Haskell type classes. |
707 statements naturally maps to Haskell type classes. |
678 The code generator framework \cite{isabelle-codegen} |
708 The code generator framework \cite{isabelle-codegen} |
679 takes this into account. Concerning target languages |
709 takes this into account. Concerning target languages |
680 lacking type classes (e.g.~SML), type classes |
710 lacking type classes (e.g.~SML), type classes |
681 are implemented by explicit dictionary construction. |
711 are implemented by explicit dictionary construction. |
682 As example, the natural power function on groups:% |
712 For example, lets go back to the power function:% |
683 \end{isamarkuptext}% |
713 \end{isamarkuptext}% |
684 \isamarkuptrue% |
714 \isamarkuptrue% |
685 \ \ \ \ \isacommand{fun}\isamarkupfalse% |
715 \ \ \ \ \isacommand{fun}\isamarkupfalse% |
686 \isanewline |
716 \isanewline |
687 \ \ \ \ \ \ pow{\isacharunderscore}nat\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ {\isasymalpha}{\isasymColon}monoidl\ {\isasymRightarrow}\ {\isasymalpha}{\isasymColon}monoidl{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
717 \ \ \ \ \ \ pow{\isacharunderscore}nat\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ {\isasymalpha}{\isasymColon}group\ {\isasymRightarrow}\ {\isasymalpha}{\isasymColon}group{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
688 \ \ \ \ \ \ {\isachardoublequoteopen}pow{\isacharunderscore}nat\ {\isadigit{0}}\ x\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\isanewline |
718 \ \ \ \ \ \ {\isachardoublequoteopen}pow{\isacharunderscore}nat\ {\isadigit{0}}\ x\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\isanewline |
689 \ \ \ \ \ \ {\isacharbar}\ {\isachardoublequoteopen}pow{\isacharunderscore}nat\ {\isacharparenleft}Suc\ n{\isacharparenright}\ x\ {\isacharequal}\ x\ {\isasymotimes}\ pow{\isacharunderscore}nat\ n\ x{\isachardoublequoteclose}\isanewline |
719 \ \ \ \ \ \ {\isacharbar}\ {\isachardoublequoteopen}pow{\isacharunderscore}nat\ {\isacharparenleft}Suc\ n{\isacharparenright}\ x\ {\isacharequal}\ x\ {\isasymotimes}\ pow{\isacharunderscore}nat\ n\ x{\isachardoublequoteclose}\isanewline |
690 \isanewline |
720 \isanewline |
691 \ \ \ \ \isacommand{definition}\isamarkupfalse% |
721 \ \ \ \ \isacommand{definition}\isamarkupfalse% |
692 \isanewline |
722 \isanewline |
702 \begin{isamarkuptext}% |
732 \begin{isamarkuptext}% |
703 \noindent This maps to Haskell as:% |
733 \noindent This maps to Haskell as:% |
704 \end{isamarkuptext}% |
734 \end{isamarkuptext}% |
705 \isamarkuptrue% |
735 \isamarkuptrue% |
706 \isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
736 \isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
707 \ example\ \isakeyword{in}\ Haskell\ \isakeyword{file}\ {\isachardoublequoteopen}code{\isacharunderscore}examples{\isacharslash}{\isachardoublequoteclose}% |
737 \ example\ \isakeyword{in}\ Haskell\ \isakeyword{to}\ Classes\ \isakeyword{file}\ {\isachardoublequoteopen}code{\isacharunderscore}examples{\isacharslash}{\isachardoublequoteclose}% |
708 \begin{isamarkuptext}% |
738 \begin{isamarkuptext}% |
709 \lsthaskell{Thy/code_examples/Classes.hs} |
739 \lsthaskell{Thy/code_examples/Classes.hs} |
710 |
740 |
711 \noindent (we have left out all other modules). |
|
712 |
|
713 \noindent The whole code in SML with explicit dictionary passing:% |
741 \noindent The whole code in SML with explicit dictionary passing:% |
714 \end{isamarkuptext}% |
742 \end{isamarkuptext}% |
715 \isamarkuptrue% |
743 \isamarkuptrue% |
716 \isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
744 \isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
717 \ example\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}code{\isacharunderscore}examples{\isacharslash}classes{\isachardot}ML{\isachardoublequoteclose}% |
745 \ example\ \isakeyword{in}\ SML\ \isakeyword{to}\ Classes\ \isakeyword{file}\ {\isachardoublequoteopen}code{\isacharunderscore}examples{\isacharslash}classes{\isachardot}ML{\isachardoublequoteclose}% |
718 \begin{isamarkuptext}% |
746 \begin{isamarkuptext}% |
719 \lstsml{Thy/code_examples/classes.ML}% |
747 \lstsml{Thy/code_examples/classes.ML}% |
720 \end{isamarkuptext}% |
748 \end{isamarkuptext}% |
721 \isamarkuptrue% |
749 \isamarkuptrue% |
722 % |
750 % |