508 The locale package does not attempt to remove subsumed |
508 The locale package does not attempt to remove subsumed |
509 interpretations. |
509 interpretations. |
510 \end{warn} |
510 \end{warn} |
511 |
511 |
512 |
512 |
513 \subsection{Type classes}\label{sec:class} |
513 \subsection{Classes}\label{sec:class} |
514 |
514 |
515 A type class is a special case of a locale, with some additional |
515 A class is a peculiarity of a locale with \emph{exactly one} type variable. |
516 infrastructure (notably a link to type-inference). Type classes |
516 Beyond the underlying locale, a corresponding type class is established which |
517 consist of a locale with \emph{exactly one} type variable and an |
517 is interpreted logically as axiomatic type class \cite{Wenzel:1997:TPHOL} |
518 corresponding axclass. \cite{isabelle-classes} gives a substantial |
518 whose logical content are the assumptions of the locale. Thus, classes provide |
519 introduction on type classes. |
519 the full generality of locales combined with the commodity of type classes |
|
520 (notably type-inference). See \cite{isabelle-classes} for a short tutorial. |
520 |
521 |
521 \indexisarcmd{instance}\indexisarcmd{class}\indexisarcmd{print-classes} |
522 \indexisarcmd{instance}\indexisarcmd{class}\indexisarcmd{print-classes} |
522 \begin{matharray}{rcl} |
523 \begin{matharray}{rcl} |
523 \isarcmd{class} & : & \isartrans{theory}{local{\dsh}theory} \\ |
524 \isarcmd{class} & : & \isartrans{theory}{local{\dsh}theory} \\ |
|
525 \isarcmd{instantiation} & : & \isartrans{theory}{local{\dsh}theory} \\ |
|
526 \isarcmd{instance} & : & \isartrans{local{\dsh}theory}{local{\dsh}theory} \\ |
524 \isarcmd{subclass} & : & \isartrans{local{\dsh}theory}{local{\dsh}theory} \\ |
527 \isarcmd{subclass} & : & \isartrans{local{\dsh}theory}{local{\dsh}theory} \\ |
525 \isarcmd{instance} & : & \isartrans{theory}{proof(prove)} \\ |
|
526 \isarcmd{print_classes}^* & : & \isarkeep{theory~|~proof} \\ |
528 \isarcmd{print_classes}^* & : & \isarkeep{theory~|~proof} \\ |
527 \end{matharray} |
529 intro_classes & : & \isarmeth |
528 |
530 \end{matharray} |
529 \begin{rail} |
531 |
530 'class' name '=' ((superclassexpr '+' (contextelem+)) | superclassexpr | (contextelem+)) 'begin'? |
532 \begin{rail} |
|
533 'class' name '=' ((superclassexpr '+' (contextelem+)) | superclassexpr | (contextelem+)) \\ |
|
534 'begin'? |
|
535 ; |
|
536 'instantiation' (nameref + 'and') '::' arity 'begin' |
|
537 ; |
|
538 'instance' |
531 ; |
539 ; |
532 'subclass' target? nameref |
540 'subclass' target? nameref |
533 ; |
541 ; |
534 'instance' (nameref '::' arity + 'and') (axmdecl prop +)? |
|
535 ; |
|
536 'print\_classes' |
542 'print\_classes' |
537 ; |
543 ; |
538 |
544 |
539 superclassexpr: nameref | (nameref '+' superclassexpr) |
545 superclassexpr: nameref | (nameref '+' superclassexpr) |
540 ; |
546 ; |
541 \end{rail} |
547 \end{rail} |
542 |
548 |
543 \begin{descr} |
549 \begin{descr} |
544 |
550 |
545 \item [$\CLASS~c = superclasses~+~body$] defines a new class $c$, |
551 \item [$\CLASS~c = superclasses~+~body$] defines a new class $c$, |
546 inheriting from $superclasses$. Simultaneously, a locale |
552 inheriting from $superclasses$. This introduces a locale $c$ |
547 named $c$ is introduced, inheriting from the locales |
553 inheriting from all the locales $superclasses$. Correspondingly, |
548 corresponding to $superclasses$; also, an axclass |
554 a type class $c$, inheriting from type classes $superclasses$. |
549 named $c$, inheriting from the axclasses corresponding to |
555 $\FIXESNAME$ in $body$ are lifted to the global theory level |
550 $superclasses$. $\FIXESNAME$ in $body$ are lifted |
556 (\emph{class operations} $\vec f$ of class $c$), |
551 to the theory toplevel, constraining |
557 mapping the local type parameter $\alpha$ to a schematic |
552 the free type variable to sort $c$ and stripping local syntax. |
558 type variable $?\alpha::c$ |
553 $\ASSUMESNAME$ in $body$ are also lifted, |
559 $\ASSUMESNAME$ in $body$ are also lifted, mapping each local parameter |
554 constraining |
560 $f::\tau [\alpha]$ to its corresponding global constant |
555 the free type variable to sort $c$. |
561 $f::\tau [?\alpha::c]$. |
556 |
562 |
557 \item [$\INSTANCE~~t :: (\vec s)s \vec{defs}$] |
563 \item [$\INSTANTIATION~\vec t~::~(\vec s)~s~\isarkeyword{begin}$] |
558 sets up a goal stating type arities. The proof would usually |
564 opens a theory target (cf.\S\ref{sec:target}) which allows to specify |
559 proceed by $intro_classes$, and then establish the characteristic theorems |
565 class operations $\vec f$ corresponding to sort $s$ at particular |
560 of the type classes involved. |
566 type instances $\vec{\alpha::s}~t$ for each $t$ in $\vec t$. |
561 The $defs$, if given, must correspond to the class parameters |
567 An $\INSTANCE$ command in the target body sets up a goal stating |
562 involved in the $arities$ and are introduced in the theory |
568 the type arities given after the $\INSTANTIATION$ keyword. |
563 before proof. |
569 The possibility to give a list of type constructors with same arity |
564 After finishing the proof, the theory will be |
570 nicely corresponds to mutual recursive type definitions in Isabelle/HOL. |
565 augmented by a type signature declaration corresponding to the |
571 The target is concluded by an $\isarkeyword{end}$ keyword. |
566 resulting theorems. |
572 |
567 This $\isarcmd{instance}$ command is actually an extension |
573 \item [$\INSTANCE$] in an instantiation target body sets up a goal stating |
568 of primitive axclass $\isarcmd{instance}$ (see \ref{sec:axclass}). |
574 the type arities claimed at the opening $\INSTANTIATION$ keyword. |
569 |
575 The proof would usually proceed by $intro_classes$, and then establish the |
|
576 characteristic theorems of the type classes involved. |
|
577 After finishing the proof, the background theory will be |
|
578 augmented by the proven type arities. |
|
579 |
570 \item [$\SUBCLASS~c$] in a class context for class $d$ |
580 \item [$\SUBCLASS~c$] in a class context for class $d$ |
571 sets up a goal stating that class $c$ is logically |
581 sets up a goal stating that class $c$ is logically |
572 contained in class $d$. After finishing the proof, class $d$ is proven |
582 contained in class $d$. After finishing the proof, class $d$ is proven |
573 to be subclass $c$ and the locale $c$ is interpreted into $d$ simultaneously. |
583 to be subclass $c$ and the locale $c$ is interpreted into $d$ simultaneously. |
574 |
584 |
575 \item [$\isarkeyword{print_classes}$] prints all classes |
585 \item [$\isarkeyword{print_classes}$] prints all classes |
576 in the current theory. |
586 in the current theory. |
577 |
587 |
578 \end{descr} |
588 \item [$intro_classes$] repeatedly expands all class introduction rules of |
|
589 this theory. Note that this method usually needs not be named explicitly, |
|
590 as it is already included in the default proof step (of $\PROOFNAME$ etc.). |
|
591 In particular, instantiation of trivial (syntactic) classes may be performed |
|
592 by a single ``$\DDOT$'' proof step. |
|
593 |
|
594 \end{descr} |
|
595 |
|
596 |
|
597 \subsubsection{Class target} |
|
598 |
|
599 A named context may refer to a locale (cf.~\S\ref{sec:target}). If this |
|
600 locale is also a class $c$, beside the common locale target behaviour |
|
601 the following occurs: |
|
602 |
|
603 \begin{itemize} |
|
604 \item Local constant declarations $g [\alpha]$ referring to the local type |
|
605 parameter $\alpha$ and local parameters $\vec f [\alpha]$ are accompagnied |
|
606 by theory-level constants $g [?\alpha::c]$ referring to theory-level |
|
607 class operations $\vec f [?\alpha::c]$ |
|
608 \item Local theorem bindings are lifted as are assumptions. |
|
609 \end{itemize} |
579 |
610 |
580 |
611 |
581 \subsection{Axiomatic type classes}\label{sec:axclass} |
612 \subsection{Axiomatic type classes}\label{sec:axclass} |
582 |
613 |
583 \indexisarcmd{axclass}\indexisarmeth{intro-classes} |
614 \indexisarcmd{axclass}\indexisarmeth{intro-classes} |
584 \begin{matharray}{rcl} |
615 \begin{matharray}{rcl} |
585 \isarcmd{axclass} & : & \isartrans{theory}{theory} \\ |
616 \isarcmd{axclass} & : & \isartrans{theory}{theory} \\ |
586 \isarcmd{instance} & : & \isartrans{theory}{proof(prove)} \\ |
617 \isarcmd{instance} & : & \isartrans{theory}{proof(prove)} \\ |
587 intro_classes & : & \isarmeth \\ |
618 \end{matharray} |
588 \end{matharray} |
619 |
589 |
620 Axiomatic type classes are Isabelle/Pure's primitive \emph{definitional} interface |
590 Axiomatic type classes are provided by Isabelle/Pure as a \emph{definitional} |
621 to type classes. For practical applications, you should consider using classes |
591 interface to type classes (cf.~\S\ref{sec:classes}). Thus any object logic |
622 (cf.~\S\ref{sec:classes}) which provide a convenient user interface. |
592 may make use of this light-weight mechanism of abstract theories |
|
593 \cite{Wenzel:1997:TPHOL}. There is also a tutorial on using axiomatic type |
|
594 classes in Isabelle \cite{isabelle-axclass} that is part of the standard |
|
595 Isabelle documentation. |
|
596 |
623 |
597 \begin{rail} |
624 \begin{rail} |
598 'axclass' classdecl (axmdecl prop +) |
625 'axclass' classdecl (axmdecl prop +) |
599 ; |
626 ; |
600 'instance' (nameref ('<' | subseteq) nameref | nameref '::' arity) |
627 'instance' (nameref ('<' | subseteq) nameref | nameref '::' arity) |