Quelle Classes.thy
Sprache: Isabelle
|
|
theory Classes
imports Main Setup
begin
section ‹Introduction›
text ‹
Type classes were introduced by Wadler and Blott cite‹wadler89how›
into the Haskell language to allow for a reasonable implementation
of overloading\footnote{throughout this tutorial, we are referring
to classical Haskell 1.0 type classes, not considering later
additions in expressiveness}. As a canonical example, a polymorphic
equality function ‹eq :: α → α → bool› which is overloaded on
different types for ‹α›, which is achieved by splitting
introduction of the ‹eq› function from its overloaded
definitions by means of ‹class› and ‹instance›
declarations: \footnote{syntax here is a kind of isabellized
Haskell}
\begin{quote}
🚫 ‹class eq where› \\
\hspace*{2ex}‹eq :: α → α → bool›
┉🚫 ‹instance nat :: eq where› \\
\hspace*{2ex}‹eq 0 0 = True› \\
\hspace*{2ex}‹eq 0 _ = False› \\
\hspace*{2ex}‹eq _ 0 = False› \\
\hspace*{2ex}‹eq (Suc n) (Suc m) = eq n m›
┉🚫 ‹instance (α::eq, β::eq) pair :: eq where› \\
\hspace*{2ex}‹eq (x1, y1) (x2, y2) = eq x1 x2 ∧ eq y1 y2›
┉🚫 ‹class ord extends eq where› \\
\hspace*{2ex}‹less_eq :: α → α → bool› \\
\hspace*{2ex}‹less :: α → α → bool›
\end{quote}
🚫 Type variables are annotated with (finitely many) classes;
these annotations are assertions that a particular polymorphic type
provides definitions for overloaded functions.
Indeed, type classes not only allow for simple overloading but form
a generic calculus, an instance of order-sorted algebra
cite‹"nipkow-sorts93" and "Nipkow-Prehofer:1993" and "Wenzel:1997:TPHOL"›.
From a software engineering point of view, type classes roughly
correspond to interfaces in object-oriented languages like Java; so,
it is naturally desirable that type classes do not only provide
functions (class parameters) but also state specifications
implementations must obey. For example, the ‹class eq›
above could be given the following specification, demanding that
‹class eq› is an equivalence relation obeying reflexivity,
symmetry and transitivity:
\begin{quote}
🚫 ‹class eq where› \\
\hspace*{2ex}‹eq :: α → α → bool› \\
‹satisfying› \\
\hspace*{2ex}‹refl: eq x x› \\
\hspace*{2ex}‹sym: eq x y ⟷ eq x y› \\
\hspace*{2ex}‹trans: eq x y ∧ eq y z ⟶ eq x z›
\end{quote}
🚫 From a theoretical point of view, type classes are
lightweight modules; Haskell type classes may be emulated by SML
functors \<^cite>\<open>classes_modules\<close>. Isabelle/Isar offers a discipline
of type classes which brings all those aspects together:
\begin{enumerate}
\item specifying abstract parameters together with
corresponding specifications,
\item instantiating those abstract parameters by a particular
type
\item in connection with a ``less ad-hoc'' approach to overloading,
\item with a direct link to the Isabelle module system:
locales \<^cite>\<open>"kammueller-locales"\<close>.
\end{enumerate}
\<^noindent> Isar type classes also directly support code generation in
a Haskell like fashion. Internally, they are mapped to more
primitive Isabelle concepts \<^cite>\<open>"Haftmann-Wenzel:2006:classes"\<close>.
This tutorial demonstrates common elements of structured
specifications and abstract reasoning with type classes by the
algebraic hierarchy of semigroups, monoids and groups. Our
background theory is that of Isabelle/HOL \<^cite>\<open>"isa-tutorial"\<close>, for
which some familiarity is assumed.
\<close>
section \<open>A simple algebra example \label{sec:example}\<close>
subsection \<open>Class definition\<close>
text \<open>
Depending on an arbitrary type \<open>\<alpha>\<close>, class \<open>semigroup\<close> introduces a binary operator \<open>(\<otimes>)\<close> that is
assumed to be associative:
\<close>
class %quote semigroup =
fixes mult :: \<open>\<alpha> \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>\<close> (infixl \<open>\<otimes>\<close> 70)
assumes assoc: \<open>(x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)\<close>
text \<open>
\<^noindent> This @{command class} specification consists of two parts:
the \qn{operational} part names the class parameter (@{element
"fixes"}), the \qn{logical} part specifies properties on them
(@{element "assumes"}). The local @{element "fixes"} and @{element
"assumes"} are lifted to the theory toplevel, yielding the global
parameter @{term [source] \<open>mult :: \<alpha>::semigroup \<Rightarrow> \<alpha> \<Rightarrow> \<alpha>\<close>} and the
global theorem @{fact "semigroup.assoc:"}~@{prop [source] \<open>\<And>x y z ::
\<alpha>::semigroup. (x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)\<close>}.
\<close>
subsection \<open>Class instantiation \label{sec:class_inst}\<close>
text \<open>
The concrete type \<^typ>\<open>int\<close> is made a \<^class>\<open>semigroup\<close> instance
by providing a suitable definition for the class parameter \<open>(\<otimes>)\<close> and a proof for the specification of @{fact assoc}. This is
accomplished by the @{command instantiation} target:
\<close>
instantiation %quote int :: semigroup
begin
definition %quote
mult_int_def: \<open>i \<otimes> j = i + (j::int)\<close>
instance %quote proof
fix i j k :: int have \<open>(i + j) + k = i + (j + k)\<close> by simp
then show \<open>(i \<otimes> j) \<otimes> k = i \<otimes> (j \<otimes> k)\<close>
unfolding mult_int_def .
qed
end %quote
text \<open>
\<^noindent> @{command instantiation} defines class parameters at a
particular instance using common specification tools (here,
@{command definition}). The concluding @{command instance} opens a
proof that the given parameters actually conform to the class
specification. Note that the first proof step is the @{method
standard} method, which for such instance proofs maps to the @{method
intro_classes} method. This reduces an instance judgement to the
relevant primitive proof goals; typically it is the first method
applied in an instantiation proof.
From now on, the type-checker will consider \<^typ>\<open>int\<close> as a \<^class>\<open>semigroup\<close> automatically, i.e.\ any general results are immediately
available on concrete instances.
\<^medskip> Another instance of \<^class>\<open>semigroup\<close> yields the natural
numbers:
\<close>
instantiation %quote nat :: semigroup
begin
primrec %quote mult_nat where
\<open>(0::nat) \<otimes> n = n\<close>
| \<open>Suc m \<otimes> n = Suc (m \<otimes> n)\<close>
instance %quote proof
fix m n q :: nat
show \<open>m \<otimes> n \<otimes> q = m \<otimes> (n \<otimes> q)\<close>
by (induct m) auto
qed
end %quote
text \<open>
\<^noindent> Note the occurrence of the name \<open>mult_nat\<close> in the
primrec declaration; by default, the local name of a class operation
\<open>f\<close> to be instantiated on type constructor \<open>\<kappa>\<close> is
mangled as \<open>f_\<kappa>\<close>. In case of uncertainty, these names may be
inspected using the @{command "print_context"} command.
\<close>
subsection \<open>Lifting and parametric types\<close>
text \<open>
Overloaded definitions given at a class instantiation may include
recursion over the syntactic structure of types. As a canonical
example, we model product semigroups using our simple algebra:
\<close>
instantiation %quote prod :: (semigroup, semigroup) semigroup
begin
definition %quote
mult_prod_def: \<open>p\<^sub>1 \<otimes> p\<^sub>2 = (fst p\<^sub>1 \<otimes> fst p\<^sub>2, snd p\<^sub>1 \<otimes> snd p\<^sub>2)\<close>
instance %quote proof
fix p\<^sub>1 p\<^sub>2 p\<^sub>3 :: \<open>\<alpha>::semigroup \<times> \<beta>::semigroup\<close>
show \<open>p\<^sub>1 \<otimes> p\<^sub>2 \<otimes> p\<^sub>3 = p\<^sub>1 \<otimes> (p\<^sub>2 \<otimes> p\<^sub>3)\<close>
unfolding mult_prod_def by (simp add: assoc)
qed
end %quote
text \<open>
\<^noindent> Associativity of product semigroups is established using
the definition of \<open>(\<otimes>)\<close> on products and the hypothetical
associativity of the type components; these hypotheses are
legitimate due to the \<^class>\<open>semigroup\<close> constraints imposed on the
type components by the @{command instance} proposition. Indeed,
this pattern often occurs with parametric types and type classes.
\<close>
subsection \<open>Subclassing\<close>
text \<open>
We define a subclass \<open>monoidl\<close> (a semigroup with a left-hand
neutral) by extending \<^class>\<open>semigroup\<close> with one additional
parameter \<open>neutral\<close> together with its characteristic property:
\<close>
class %quote monoidl = semigroup +
fixes neutral :: \<open>\<alpha>\<close> (\<open>\<one>\<close>)
assumes neutl: \<open>\<one> \<otimes> x = x\<close>
text \<open>
\<^noindent> Again, we prove some instances, by providing suitable
parameter definitions and proofs for the additional specifications.
Observe that instantiations for types with the same arity may be
simultaneous:
\<close>
instantiation %quote nat and int :: monoidl
begin
definition %quote
neutral_nat_def: \<open>\<one> = (0::nat)\<close>
definition %quote
neutral_int_def: \<open>\<one> = (0::int)\<close>
instance %quote proof
fix n :: nat
show \<open>\<one> \<otimes> n = n\<close>
unfolding neutral_nat_def by simp
next
fix k :: int
show \<open>\<one> \<otimes> k = k\<close>
unfolding neutral_int_def mult_int_def by simp
qed
end %quote
instantiation %quote prod :: (monoidl, monoidl) monoidl
begin
definition %quote
neutral_prod_def: \<open>\<one> = (\<one>, \<one>)\<close>
instance %quote proof
fix p :: \<open>\<alpha>::monoidl \<times> \<beta>::monoidl\<close>
show \<open>\<one> \<otimes> p = p\<close>
unfolding neutral_prod_def mult_prod_def by (simp add: neutl)
qed
end %quote
text \<open>
\<^noindent> Fully-fledged monoids are modelled by another subclass,
which does not add new parameters but tightens the specification:
\<close>
class %quote monoid = monoidl +
assumes neutr: \<open>x \<otimes> \<one> = x\<close>
instantiation %quote nat and int :: monoid
begin
instance %quote proof
fix n :: nat
show \<open>n \<otimes> \<one> = n\<close>
unfolding neutral_nat_def by (induct n) simp_all
next
fix k :: int
show \<open>k \<otimes> \<one> = k\<close>
unfolding neutral_int_def mult_int_def by simp
qed
end %quote
instantiation %quote prod :: (monoid, monoid) monoid
begin
instance %quote proof
fix p :: \<open>\<alpha>::monoid \<times> \<beta>::monoid\<close>
show \<open>p \<otimes> \<one> = p\<close>
unfolding neutral_prod_def mult_prod_def by (simp add: neutr)
qed
end %quote
text \<open>
\<^noindent> To finish our small algebra example, we add a \<open>group\<close> class with a corresponding instance:
\<close>
class %quote group = monoidl +
fixes inverse :: \<open>\<alpha> \<Rightarrow> \<alpha>\<close> (\<open>(_\<div>)\<close> [1000] 999)
requires
instantiation int:
begin
definition %quote
inverse_int_def: \<open>i\<div> = - (i::int)\<close>
instance %
:java.lang.StringIndexOutOfBoundsException: Range [14, 15) out of bounds for length 14
java.lang.StringIndexOutOfBoundsException: Range [7, 6) out of bounds for length 40
<>i< <> <><lose
inverse_int_defjava.lang.StringIndexOutOfBoundsException: Index 60 out of bounds for length 60
qed
end %quote
section \<open>Type java.lang.StringIndexOutOfBoundsException: Range [48, 47) out of bounds for length 64
beUNIXjava.lang.StringIndexOutOfBoundsException: Range [23, 22) out of bounds for length 54
text\>
exampleabove givesanimpressionhowIsar classes in
java.lang.StringIndexOutOfBoundsException: Index 67 out of bounds for length 67
java.lang.StringIndexOutOfBoundsException: Range [10, 9) out of bounds for length 68
is nothing other than a locale:
\<contactjava.lang.StringIndexOutOfBoundsException: Range [24, 23) out of bounds for length 68
lassqidem=
<blockq>
textstrong/java.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 26
<>java.lang.StringIndexOutOfBoundsException: Range [27, 26) out of bounds for length 48
*% <>Sign f\close
locale %strongNote/java.lang.StringIndexOutOfBoundsException: Range [21, 22) out of bounds for length 21
:\open<><>\alpha<>
assumes idem: \<open>f (f x) = f x\<close>
java.lang.StringIndexOutOfBoundsException: Range [61, 4) out of bounds for length 73
consts %quote f :: \<open>\<alpha>runningasuser<postfixcode can the
text\<pen
\<^noindent> The backendcodeetcshadowcode iflike:/>
primitive type class \
interpretation:
\<close>
%quoteidem_class
idem \<open>are stored stored in an LDAP database </td> </tr>
(*<*)sorry((blockquote>
\<
conclusions in credentialsthatarestoredina database The
m<>
\<close> (*<*)setup java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
subsection \<open>Abstract reasoning\<close>
text \<musernameexamplecom<emcode. <p
java.lang.StringIndexOutOfBoundsException: Index 9 out of bounds for length 0
are > Run the followingtoreusethePostfix code>< ="postconf5html#ydomain"mydomain/>/ode
now establish the \<open>java.lang.StringIndexOutOfBoundsException: Index 36 out of bounds for length 5
ion\<pen(x\<otimes)<>is
\<close>
lemma %quote (in group) left_cancel: \<open>x \<otimes> yhelponhowto usethecommand/>
proof
assume \<open>x ujava.lang.StringIndexOutOfBoundsException: Range [10, 9) out of bounds for length 32
pwcheck_method: auxprop
then have \<open>(x/>
then show
next
java.lang.StringIndexOutOfBoundsException: Range [21, 8) out of bounds for length 29
then show \<open>x \<otimes> y = x \<otimes> z\<close> by simp
qed
text \<open>
\<^noindent> Here the \qt{@{keyword "in"} \<^class>\<open>group\<close>} target
specification indicates that the result is recorded within that
context for later use. This local theorem is also lifted to the
global one @{fact "group.left_cancel:"} @{prop [source] \<open>\<And>x y z ::
\<alpha>::group. x \<otimes> y = x \<access to credentials stored in a MySQL, PostgreSQL or SQLite
made an instance of \<open>group\<close> before, we may java.lang.StringIndexOutOfBoundsException: Index 63 out of bounds for length 25
factpluginInstead, seesection"ahref=#saslauthd_pam"Using
z\close>}java.lang.StringIndexOutOfBoundsException: Range [13, 14) out of bounds for length 13
\<close>
subsection \<open>Derived java.lang.StringIndexOutOfBoundsException: Index 36 out of bounds for length 22
>
Isabelle locales are targets which />
\<lose>
open>at <Rightarrow><alpha <Rightarrow><lpha><lose>here
\<open>pow_nat 0blockquote
|blockquote>
text \<open>
\<noindent>f locale <open>roup<close> is alsoaclass thislocal
definition is propagated onto a global definition of @{term [source]
\<open>pow_nat :dtjava.lang.StringIndexOutOfBoundsException: Range [21, 17) out of bounds for length 22
@{thm java.lang.StringIndexOutOfBoundsException: Index 11 out of bounds for length 0
\<^noindentblockquotejava.lang.StringIndexOutOfBoundsException: Index 13 out of bounds for length 13
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 4
locales, such as Krauss's recursive function package
sql_passwd</dt>
\<close>
subsection \<open>functor analogy<java.lang.StringIndexOutOfBoundsException: Range [43, 42) out of bounds for length 43
text \<open>
We introduced Isar classes by analogy to type classes in functional
java.lang.StringIndexOutOfBoundsException: Index 7 out of bounds for length 0
this analogy
further that java.lang.StringIndexOutOfBoundsException: Range [31, 30) out of bounds for length 64
functors that auxprop_plugin: ldapdb
There is also the possibility of other interpretations. For
example, \<open>list\<close>s alsoldapdb_id: proxyuser
\<^blockquote>
lists the same operations as for genuinely algebraic types. In such
acase,we simply make aparticular of
for lists:
\<close>
interpretation %quote list_monoid: monoid append \<open>
proof qed auto
text \<open>
\<^noindent> This enables us to apply facts on monoids
to lists, e.g. @{thm list_monoid.neutl [no_vars]}.
When using this interpretation pattern, it maya UNIX-domain socket, <code><a href="ldap_table.5.html">ldap</a>://</code> for an unencrypted TCP
be appropriate to map derived definitions accordingly:
\<close>
java.lang.StringIndexOutOfBoundsException: Index 4 out of bounds for length 0
\<open>replicate 0 _ = []\<close>
<replicate (Suc n) xs = xs @ replicate n xs\<close>
interpretation %quote java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 17
\<open>monoid.pow_nat append [] = p> Specify a mechanism here that is supported by the LDAP server.
proof -
interpret monoid dt>ldapdb_rc (optional)</dt>
<dd> <> The toa file containing individual configuration
proof
fix n
show \<pen>monoid.pow_nat append [] n = replicate n\<close>
strong>Note</strong>
qed
qed intro_locales
text \<open>
\<^noindent> This pattern is also helpful to reuse abstract
specifications java.lang.StringIndexOutOfBoundsException: Range [33, 22) out of bounds for length 64
class \<pen>preorder\close>; for type \<^typ>\<open>nat\<close>, there are at least two
possible instances: the natural order or the order induced by the
divides relation. But only one of these instances can be used for
@{command instantiation}; using the locale behind the class /l>
specification again using @{java.lang.StringIndexOutOfBoundsException: Index 37 out of bounds for length 0
\<close>
subsection \<open>Additional subclass relations\<close>
text \<open>
Any \<open>group\<close> is also a \<open>monoid\<close>; this can beuid=(.*,cn=.*cn=auth
explicit by claiming an additional subclass relation, together with
a proof of the logical difference:
\<close>
subclass %quote (in group) monoid
proof
fix x
from invl have \<open>x\<div> \<otimes> x = \<one>\<close> by simp
\<open>\<div> <otimes> (x \<times><>=x\div> \otimes> x\close>by simp
pen>x \<otimes>\<ne> =x\<close> by simp
qed
text \<pen>
The logical java.lang.StringIndexOutOfBoundsException: Index 16 out of bounds for length 0
is propagated to the type system, making \<open>group\<close> an instance
of \<open>monoid\<close> by adding an additional edge to the graphp>Read the chapter "Using SASL" in the <a
subclass relations (\figref{fig:ubclass}.
\begin{figure}[htbp]
\begin{center}
\small
\begin{picture}(40,60)(0,0)
\put(20,60){\makebox(0,0){\<open>semigroup\<close>}}
\putblockquote>
\put(00,20){\makebox(0,0){\<open>tc/postfix/<a href="postconf.5.html">main.cf</a>:
\put(40,00){\makebox(0,0)/
\put(20,55){\vector(0,-1){10}}
\put(15,35){\vector(-1,-1){10}}
\put(25,35){\vector(1,-3){10}}
\endli><> If you configured Dovecot for UNIX-domain socket communication,
\hspace{8em}
\begin{picture}(40,60)(0,willbe sentas java.lang.StringIndexOutOfBoundsException: Range [26, 25) out of bounds for length 48
\java.lang.StringIndexOutOfBoundsException: Range [12, 11) out of bounds for length 59
\put(20,40){\makebox(0,0){\<open>monoidl\<close>}}
\put(00,20){\makebox(0,0){\<open<re>
\put(40,)\akebox(,)\opengroup<java.lang.StringIndexOutOfBoundsException: Range [53, 52) out of bounds for length 55
pjava.lang.StringIndexOutOfBoundsException: Range [35, 34) out of bounds for length 57
\put(15,35blockquote>
\put(05,15){\vector(3,-1){30}}
\end..
\caption{Subclass relationship of monoids java.lang.StringIndexOutOfBoundsException: Index 50 out of bounds for length 22
before and after establishing theblockquote>
\<open>group \<subseteq> monoid\<close>; transitive edges are left out.}
\}
\end{center}
\end{figure}
For illustration, a java.lang.StringIndexOutOfBoundsException: Range [0, 29) out of bounds for length 5
\<close>
definition %quote (in group) pow_int :: \<open>int \<version
\<open>pow_int
then pow_nat (nat k) x
else (pow_nat (nat (- k)) x)\<div>)\<close>
text clients: <p
\<^noindent>pre>
int \<Rightarrow> \<alpha>::group \<Rightarrow> \<alpha>::java.lang.StringIndexOutOfBoundsException: Index 65 out of bounds for length 36
pow_int_def [no_vars]}250-PIPELINING
\<close>
subsection \<open>A note - SASL mechanism propertiesa>/4>
text \<open>
java.lang.StringIndexOutOfBoundsException: Index 4 out of bounds for length 0
class tr><h>Property<th><h>escription<th><tr>
inference authentication </d><tr>
\<close>
context %quote semigroup
begin
term %quote \<open>x \<otimes> y\<close> \<comment> \<open>example 1\<close>
term %quote \<open>(x::nat) \<otimes> y\<close> \<comment> \<open>/able>
end %quote
term %quote \<open>x \<otimes> y\<close> \<comment> \<open>example 3\<close>
text \<open>
\<^noindent> Here in # Specify a list of propertiesseparated by comma or whitespace
operation \<open>mult [\<alpha>]\<java.lang.StringIndexOutOfBoundsException: Range [0, 41) out of bounds for length 6
constraintenforces the global class operation \<open>mult [nat]\<close>.
Injava.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 0
polymorphic global class operation \<open>mult [?\<alpha> :: semigroup]\<close>.
\close>
section \<open>Further issues\<close>
subsection \<open>Type classes and settings from the unencrypted session: </p>
text \<open>
Turning< href="postconf.5.html#smtpd_sasl_tls_security_options">smtpd_sasl_tls_security_options</a> = $<a href="postconf.5.html#smtpd_sasl_security_options">java.lang.StringIndexOutOfBoundsException: Index 177 out of bounds for length 6
overloading, it is obvious thatonly over a TLS-encrypted connection: </p>
class} and {instantiation}targets naturally
maps to Haskell classes. The code generator framework
cite><pen>"java.lang.StringIndexOutOfBoundsException: Range [27, 26) out of bounds for length 83
nguagee.SML)lackstype classes thentheyareimplemented by
anexplicit dictionaryconstruction As example,let' back to
the power function:
\<close>
definition %quote example :: int where
\<open>example = pow_int 10 (-2)\<close>
text \<open>
\<^noindent> This maps to Haskell as follows:
\<close>
text \open>
@{code_stmts example (Haskell)}
\<close>
\<open>
\<^noindent> The code in SML has explicit dictionary passing:
\<close>
text %quote \<open>
@code_stmts example (SML)}
\<close>
text \<open>
\<^noindent> In Scala, implicits are used as dictionaries:
\<close>
text %quote <"java.lang.StringIndexOutOfBoundsException: Range [54, 53) out of bounds for length 85
@java.lang.StringIndexOutOfBoundsException: Range [15, 14) out of bounds for length 31
\<close>
subsection \<open>Inspecting the type class universe\<close>
text\<
To facilitate orientation in complex subclass structures, two
diagnostics commands are provided:
\begin{description}
\item[@{command "print_classes"}] print a list of all classes
together with associated operations etc.
\item[@{command "class_deps"}] visualizes the subclass relation
between all classes as a Hasse diagram. An optional first sort argument
constrains the set of classes to all subclasses of this sort,
an optional second sort argument to all superclasses of this sort.
\end{description}
\<close>
end
¤ Dauer der Verarbeitung: 0.11 Sekunden
(vorverarbeitet am 2026-11-24)
¤
*© Formatika GbR, Deutschland
|
|
2026-11-24
|