doc-src/AxClass/Group/Product.thy
changeset 30105 37f47ea6fed1
parent 30104 b094999e1d33
parent 30101 5c6efec476ae
child 30106 351fc2f8493d
equal deleted inserted replaced
30104:b094999e1d33 30105:37f47ea6fed1
     1 
       
     2 header {* Syntactic classes *}
       
     3 
       
     4 theory Product imports Main begin
       
     5 
       
     6 text {*
       
     7   \medskip\noindent There is still a feature of Isabelle's type system
       
     8   left that we have not yet discussed.  When declaring polymorphic
       
     9   constants @{text "c \<Colon> \<sigma>"}, the type variables occurring in @{text \<sigma>}
       
    10   may be constrained by type classes (or even general sorts) in an
       
    11   arbitrary way.  Note that by default, in Isabelle/HOL the
       
    12   declaration @{text "\<odot> \<Colon> 'a \<Rightarrow> 'a \<Rightarrow> 'a"} is actually an abbreviation
       
    13   for @{text "\<odot> \<Colon> 'a\<Colon>type \<Rightarrow> 'a \<Rightarrow> 'a"} Since class @{text type} is the
       
    14   universal class of HOL, this is not really a constraint at all.
       
    15 
       
    16  The @{text product} class below provides a less degenerate example of
       
    17  syntactic type classes.
       
    18 *}
       
    19 
       
    20 axclass
       
    21   product \<subseteq> type
       
    22 consts
       
    23   product :: "'a\<Colon>product \<Rightarrow> 'a \<Rightarrow> 'a"    (infixl "\<odot>" 70)
       
    24 
       
    25 text {*
       
    26   Here class @{text product} is defined as subclass of @{text type}
       
    27   without any additional axioms.  This effects in logical equivalence
       
    28   of @{text product} and @{text type}, as is reflected by the trivial
       
    29   introduction rule generated for this definition.
       
    30 
       
    31   \medskip So what is the difference of declaring @{text "\<odot> \<Colon>
       
    32   'a\<Colon>product \<Rightarrow> 'a \<Rightarrow> 'a"} vs.\ declaring @{text "\<odot> \<Colon> 'a\<Colon>type \<Rightarrow> 'a \<Rightarrow>
       
    33   'a"} anyway?  In this particular case where @{text "product \<equiv>
       
    34   type"}, it should be obvious that both declarations are the same
       
    35   from the logic's point of view.  It even makes the most sense to
       
    36   remove sort constraints from constant declarations, as far as the
       
    37   purely logical meaning is concerned \cite{Wenzel:1997:TPHOL}.
       
    38 
       
    39   On the other hand there are syntactic differences, of course.
       
    40  Constants @{text \<odot>} on some type @{text \<tau>} are rejected by the
       
    41  type-checker, unless the arity @{text "\<tau> \<Colon> product"} is part of the
       
    42  type signature.  In our example, this arity may be always added when
       
    43  required by means of an \isakeyword{instance} with the default proof
       
    44  (double-dot).
       
    45 
       
    46   \medskip Thus, we may observe the following discipline of using
       
    47   syntactic classes.  Overloaded polymorphic constants have their type
       
    48   arguments restricted to an associated (logically trivial) class
       
    49   @{text c}.  Only immediately before \emph{specifying} these
       
    50   constants on a certain type @{text \<tau>} do we instantiate @{text "\<tau> \<Colon>
       
    51   c"}.
       
    52 
       
    53   This is done for class @{text product} and type @{typ bool} as
       
    54   follows.
       
    55 *}
       
    56 
       
    57 instance bool :: product ..
       
    58 defs (overloaded)
       
    59   product_bool_def: "x \<odot> y \<equiv> x \<and> y"
       
    60 
       
    61 text {*
       
    62  The definition @{text prod_bool_def} becomes syntactically
       
    63  well-formed only after the arity @{text "bool \<Colon> product"} is made
       
    64  known to the type checker.
       
    65 
       
    66  \medskip It is very important to see that above \isakeyword{defs} are
       
    67  not directly connected with \isakeyword{instance} at all!  We were
       
    68  just following our convention to specify @{text \<odot>} on @{typ bool}
       
    69  after having instantiated @{text "bool \<Colon> product"}.  Isabelle does
       
    70  not require these definitions, which is in contrast to programming
       
    71  languages like Haskell \cite{haskell-report}.
       
    72 
       
    73  \medskip While Isabelle type classes and those of Haskell are almost
       
    74  the same as far as type-checking and type inference are concerned,
       
    75  there are important semantic differences.  Haskell classes require
       
    76  their instances to \emph{provide operations} of certain \emph{names}.
       
    77  Therefore, its \texttt{instance} has a \texttt{where} part that tells
       
    78  the system what these ``member functions'' should be.
       
    79 
       
    80  This style of \texttt{instance} would not make much sense in
       
    81  Isabelle's meta-logic, because there is no internal notion of
       
    82  ``providing operations'' or even ``names of functions''.
       
    83 *}
       
    84 
       
    85 end