doc-src/Locales/Locales/Examples1.thy
changeset 27063 d1d35284542f
child 27077 f64166dd92f0
equal deleted inserted replaced
27062:9681f347b6f5 27063:d1d35284542f
       
     1 (*<*)
       
     2 theory Examples1
       
     3 imports Examples
       
     4 begin
       
     5 
       
     6 (*>*)
       
     7 
       
     8 section {* Use of Locales in Theories and Proofs *}
       
     9 
       
    10 text {* Locales enable to prove theorems abstractly, relative to
       
    11   sets of assumptions.  These theorems can then be used in other
       
    12   contexts where the assumptions themselves, or
       
    13   instances of the assumptions, are theorems.  This form of theorem
       
    14   reuse is called \emph{interpretation}.
       
    15 
       
    16   The changes of the locale
       
    17   hierarchy from the previous sections are examples of
       
    18   interpretations.  The command \isakeyword{interpretation} $l_1
       
    19   \subseteq l_2$ is said to \emph{interpret} locale $l_2$ in the
       
    20   context of $l_1$.  It causes all theorems of $l_2$ to be made
       
    21   available in $l_1$.  The interpretation is \emph{dynamic}: not only
       
    22   theorems already present in $l_2$ are available in $l_1$.  Theorems
       
    23   that will be added to $l_2$ in future will automatically be
       
    24   propagated to $l_1$.
       
    25 
       
    26   Locales can also be interpreted in the contexts of theories and
       
    27   structured proofs.  These interpretations are dynamic, too.
       
    28   Theorems added to locales will be propagated to theories.
       
    29   In this section the interpretation in
       
    30   theories is illustrated; interpretation in proofs is analogous.
       
    31   As an example consider, the type of natural numbers @{typ nat}.  The
       
    32   order relation @{text \<le>} is a total order over @{typ nat},
       
    33   divisibility @{text dvd} is a distributive lattice.
       
    34 
       
    35   We start with the
       
    36   interpretation that @{text \<le>} is a partial order.  The facilities of
       
    37   the interpretation command are explored in three versions.
       
    38   *}
       
    39 
       
    40 
       
    41 subsection {* First Version: Replacement of Parameters Only *}
       
    42 
       
    43 text {*
       
    44 \label{sec:po-first}
       
    45 
       
    46   In the most basic form, interpretation just replaces the locale
       
    47   parameters by terms.  The following command interprets the locale
       
    48   @{text partial_order} in the global context of the theory.  The
       
    49   parameter @{term le} is replaced by @{term "op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool"}. *} 
       
    50 
       
    51   interpretation %visible nat: partial_order ["op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool"]
       
    52 txt {* The locale name is succeeded by a \emph{parameter
       
    53   instantiation}.  In general, this is a list of terms, which refer to
       
    54   the parameters in the order of declaration in the locale.  The
       
    55   locale name is preceded by an optional \emph{interpretation prefix},
       
    56   which is used to qualify names from the locale in the global
       
    57   context.
       
    58 
       
    59   The command creates the goal @{subgoals [display]} which can be shown
       
    60   easily:%
       
    61 \footnote{Note that @{text op} binds tighter than functions
       
    62   application: parentheses around @{text "op \<le>"} are not necessary.}
       
    63  *}
       
    64     by unfold_locales auto
       
    65 
       
    66 text {*  Now theorems from the locale are available in the theory,
       
    67   interpreted for natural numbers, for example @{thm [source]
       
    68   nat.trans}: @{thm [display, indent=2] nat.trans}
       
    69 
       
    70   In order to avoid unwanted hiding of theorems, interpretation
       
    71   accepts a qualifier, @{text nat} in the example, which is applied to
       
    72   all names processed by the
       
    73   interpretation.  If present the qualifier is mandatory --- that is,
       
    74   the above theorem cannot be referred to simply as @{text trans}.
       
    75   *}
       
    76 
       
    77 
       
    78 subsection {* Second Version: Replacement of Definitions *}
       
    79 
       
    80 text {* The above interpretation also creates the theorem
       
    81   @{thm [source] nat.less_le_trans}: @{thm [display, indent=2]
       
    82   nat.less_le_trans}
       
    83   Here, @{term "partial_order.less (op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool)"}
       
    84   represents the strict order, although @{text "<"} is defined on
       
    85   @{typ nat}.  Interpretation enables to map concepts
       
    86   introduced in locales through definitions to the corresponding
       
    87   concepts of the theory.%
       
    88 \footnote{This applies not only to \isakeyword{definition} but also to
       
    89   \isakeyword{inductive}, \isakeyword{fun} and \isakeyword{function}.} 
       
    90   *}
       
    91 
       
    92 (*<*)
       
    93 end
       
    94 (*>*)