src/Doc/Locales/Examples3.thy
changeset 61419 3c3f8b182e4b
parent 58620 7435b6a3f72e
child 61565 352c73a689da
equal deleted inserted replaced
61418:44d9d55b9ef4 61419:3c3f8b182e4b
     1 theory Examples3
     1 theory Examples3
     2 imports Examples
     2 imports Examples
     3 begin
     3 begin
     4 
     4 
     5 subsection {* Third Version: Local Interpretation
     5 subsection \<open>Third Version: Local Interpretation
     6   \label{sec:local-interpretation} *}
     6   \label{sec:local-interpretation}\<close>
     7 
     7 
     8 text {* In the above example, the fact that @{term "op \<le>"} is a partial
     8 text \<open>In the above example, the fact that @{term "op \<le>"} is a partial
     9   order for the integers was used in the second goal to
     9   order for the integers was used in the second goal to
    10   discharge the premise in the definition of @{text "op \<sqsubset>"}.  In
    10   discharge the premise in the definition of @{text "op \<sqsubset>"}.  In
    11   general, proofs of the equations not only may involve definitions
    11   general, proofs of the equations not only may involve definitions
    12   from the interpreted locale but arbitrarily complex arguments in the
    12   from the interpreted locale but arbitrarily complex arguments in the
    13   context of the locale.  Therefore it would be convenient to have the
    13   context of the locale.  Therefore it would be convenient to have the
    14   interpreted locale conclusions temporarily available in the proof.
    14   interpreted locale conclusions temporarily available in the proof.
    15   This can be achieved by a locale interpretation in the proof body.
    15   This can be achieved by a locale interpretation in the proof body.
    16   The command for local interpretations is \isakeyword{interpret}.  We
    16   The command for local interpretations is \isakeyword{interpret}.  We
    17   repeat the example from the previous section to illustrate this. *}
    17   repeat the example from the previous section to illustrate this.\<close>
    18 
    18 
    19   interpretation %visible int: partial_order "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"
    19   interpretation %visible int: partial_order "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"
    20     where "int.less x y = (x < y)"
    20     where "int.less x y = (x < y)"
    21   proof -
    21   proof -
    22     show "partial_order (op \<le> :: int \<Rightarrow> int \<Rightarrow> bool)"
    22     show "partial_order (op \<le> :: int \<Rightarrow> int \<Rightarrow> bool)"
    24     then interpret int: partial_order "op \<le> :: [int, int] \<Rightarrow> bool" .
    24     then interpret int: partial_order "op \<le> :: [int, int] \<Rightarrow> bool" .
    25     show "int.less x y = (x < y)"
    25     show "int.less x y = (x < y)"
    26       unfolding int.less_def by auto
    26       unfolding int.less_def by auto
    27   qed
    27   qed
    28 
    28 
    29 text {* The inner interpretation is immediate from the preceding fact
    29 text \<open>The inner interpretation is immediate from the preceding fact
    30   and proved by assumption (Isar short hand ``.'').  It enriches the
    30   and proved by assumption (Isar short hand ``.'').  It enriches the
    31   local proof context by the theorems
    31   local proof context by the theorems
    32   also obtained in the interpretation from Section~\ref{sec:po-first},
    32   also obtained in the interpretation from Section~\ref{sec:po-first},
    33   and @{text int.less_def} may directly be used to unfold the
    33   and @{text int.less_def} may directly be used to unfold the
    34   definition.  Theorems from the local interpretation disappear after
    34   definition.  Theorems from the local interpretation disappear after
    35   leaving the proof context --- that is, after the succeeding
    35   leaving the proof context --- that is, after the succeeding
    36   \isakeyword{next} or \isakeyword{qed} statement. *}
    36   \isakeyword{next} or \isakeyword{qed} statement.\<close>
    37 
    37 
    38 
    38 
    39 subsection {* Further Interpretations *}
    39 subsection \<open>Further Interpretations\<close>
    40 
    40 
    41 text {* Further interpretations are necessary for
    41 text \<open>Further interpretations are necessary for
    42   the other locales.  In @{text lattice} the operations~@{text \<sqinter>}
    42   the other locales.  In @{text lattice} the operations~@{text \<sqinter>}
    43   and~@{text \<squnion>} are substituted by @{term "min :: int \<Rightarrow> int \<Rightarrow> int"}
    43   and~@{text \<squnion>} are substituted by @{term "min :: int \<Rightarrow> int \<Rightarrow> int"}
    44   and @{term "max :: int \<Rightarrow> int \<Rightarrow> int"}.  The entire proof for the
    44   and @{term "max :: int \<Rightarrow> int \<Rightarrow> int"}.  The entire proof for the
    45   interpretation is reproduced to give an example of a more
    45   interpretation is reproduced to give an example of a more
    46   elaborate interpretation proof.  Note that the equations are named
    46   elaborate interpretation proof.  Note that the equations are named
    47   so they can be used in a later example.  *}
    47   so they can be used in a later example.\<close>
    48 
    48 
    49   interpretation %visible int: lattice "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"
    49   interpretation %visible int: lattice "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"
    50     where int_min_eq: "int.meet x y = min x y"
    50     where int_min_eq: "int.meet x y = min x y"
    51       and int_max_eq: "int.join x y = max x y"
    51       and int_max_eq: "int.join x y = max x y"
    52   proof -
    52   proof -
    53     show "lattice (op \<le> :: int \<Rightarrow> int \<Rightarrow> bool)"
    53     show "lattice (op \<le> :: int \<Rightarrow> int \<Rightarrow> bool)"
    54       txt {* \normalsize We have already shown that this is a partial
    54       txt \<open>\normalsize We have already shown that this is a partial
    55         order, *}
    55         order,\<close>
    56       apply unfold_locales
    56       apply unfold_locales
    57       txt {* \normalsize hence only the lattice axioms remain to be
    57       txt \<open>\normalsize hence only the lattice axioms remain to be
    58         shown.
    58         shown.
    59         @{subgoals [display]}
    59         @{subgoals [display]}
    60         By @{text is_inf} and @{text is_sup}, *}
    60         By @{text is_inf} and @{text is_sup},\<close>
    61       apply (unfold int.is_inf_def int.is_sup_def)
    61       apply (unfold int.is_inf_def int.is_sup_def)
    62       txt {* \normalsize the goals are transformed to these
    62       txt \<open>\normalsize the goals are transformed to these
    63         statements:
    63         statements:
    64         @{subgoals [display]}
    64         @{subgoals [display]}
    65         This is Presburger arithmetic, which can be solved by the
    65         This is Presburger arithmetic, which can be solved by the
    66         method @{text arith}. *}
    66         method @{text arith}.\<close>
    67       by arith+
    67       by arith+
    68     txt {* \normalsize In order to show the equations, we put ourselves
    68     txt \<open>\normalsize In order to show the equations, we put ourselves
    69       in a situation where the lattice theorems can be used in a
    69       in a situation where the lattice theorems can be used in a
    70       convenient way. *}
    70       convenient way.\<close>
    71     then interpret int: lattice "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool" .
    71     then interpret int: lattice "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool" .
    72     show "int.meet x y = min x y"
    72     show "int.meet x y = min x y"
    73       by (bestsimp simp: int.meet_def int.is_inf_def)
    73       by (bestsimp simp: int.meet_def int.is_inf_def)
    74     show "int.join x y = max x y"
    74     show "int.join x y = max x y"
    75       by (bestsimp simp: int.join_def int.is_sup_def)
    75       by (bestsimp simp: int.join_def int.is_sup_def)
    76   qed
    76   qed
    77 
    77 
    78 text {* Next follows that @{text "op \<le>"} is a total order, again for
    78 text \<open>Next follows that @{text "op \<le>"} is a total order, again for
    79   the integers. *}
    79   the integers.\<close>
    80 
    80 
    81   interpretation %visible int: total_order "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"
    81   interpretation %visible int: total_order "op \<le> :: int \<Rightarrow> int \<Rightarrow> bool"
    82     by unfold_locales arith
    82     by unfold_locales arith
    83 
    83 
    84 text {* Theorems that are available in the theory at this point are shown in
    84 text \<open>Theorems that are available in the theory at this point are shown in
    85   Table~\ref{tab:int-lattice}.  Two points are worth noting:
    85   Table~\ref{tab:int-lattice}.  Two points are worth noting:
    86 
    86 
    87 \begin{table}
    87 \begin{table}
    88 \hrule
    88 \hrule
    89 \vspace{2ex}
    89 \vspace{2ex}
   121   given in the interpretation of @{text partial_order}.  The
   121   given in the interpretation of @{text partial_order}.  The
   122   interpretation equations are pushed downwards the hierarchy for
   122   interpretation equations are pushed downwards the hierarchy for
   123   related interpretations --- that is, for interpretations that share
   123   related interpretations --- that is, for interpretations that share
   124   the instances of parameters they have in common.
   124   the instances of parameters they have in common.
   125 \end{itemize}
   125 \end{itemize}
   126   *}
   126 \<close>
   127 
   127 
   128 text {* The interpretations for a locale $n$ within the current
   128 text \<open>The interpretations for a locale $n$ within the current
   129   theory may be inspected with \isakeyword{print\_interps}~$n$.  This
   129   theory may be inspected with \isakeyword{print\_interps}~$n$.  This
   130   prints the list of instances of $n$, for which interpretations exist.
   130   prints the list of instances of $n$, for which interpretations exist.
   131   For example, \isakeyword{print\_interps} @{term partial_order}
   131   For example, \isakeyword{print\_interps} @{term partial_order}
   132   outputs the following:
   132   outputs the following:
   133 \begin{small}
   133 \begin{small}
   142   ``!'' or ``?'' respectively.  Mandatory qualifiers must occur in a
   142   ``!'' or ``?'' respectively.  Mandatory qualifiers must occur in a
   143   name reference while optional ones need not.  Mandatory qualifiers
   143   name reference while optional ones need not.  Mandatory qualifiers
   144   prevent accidental hiding of names, while optional qualifiers can be
   144   prevent accidental hiding of names, while optional qualifiers can be
   145   more convenient to use.  For \isakeyword{interpretation}, the
   145   more convenient to use.  For \isakeyword{interpretation}, the
   146   default is ``!''.
   146   default is ``!''.
   147 *}
   147 \<close>
   148 
   148 
   149 
   149 
   150 section {* Locale Expressions \label{sec:expressions} *}
   150 section \<open>Locale Expressions \label{sec:expressions}\<close>
   151 
   151 
   152 text {*
   152 text \<open>
   153   A map~@{term \<phi>} between partial orders~@{text \<sqsubseteq>} and~@{text \<preceq>}
   153   A map~@{term \<phi>} between partial orders~@{text \<sqsubseteq>} and~@{text \<preceq>}
   154   is called order preserving if @{text "x \<sqsubseteq> y"} implies @{text "\<phi> x \<preceq>
   154   is called order preserving if @{text "x \<sqsubseteq> y"} implies @{text "\<phi> x \<preceq>
   155   \<phi> y"}.  This situation is more complex than those encountered so
   155   \<phi> y"}.  This situation is more complex than those encountered so
   156   far: it involves two partial orders, and it is desirable to use the
   156   far: it involves two partial orders, and it is desirable to use the
   157   existing locale for both.
   157   existing locale for both.
   204   clause.  The \isakeyword{for} clause is also where the syntax of these
   204   clause.  The \isakeyword{for} clause is also where the syntax of these
   205   parameters is declared.
   205   parameters is declared.
   206 
   206 
   207   Two context elements for the map parameter~@{text \<phi>} and the
   207   Two context elements for the map parameter~@{text \<phi>} and the
   208   assumptions that it is order preserving complete the locale
   208   assumptions that it is order preserving complete the locale
   209   declaration. *}
   209   declaration.\<close>
   210 
   210 
   211   locale order_preserving =
   211   locale order_preserving =
   212     le: partial_order le + le': partial_order le'
   212     le: partial_order le + le': partial_order le'
   213       for le (infixl "\<sqsubseteq>" 50) and le' (infixl "\<preceq>" 50) +
   213       for le (infixl "\<sqsubseteq>" 50) and le' (infixl "\<preceq>" 50) +
   214     fixes \<phi>
   214     fixes \<phi>
   215     assumes hom_le: "x \<sqsubseteq> y \<Longrightarrow> \<phi> x \<preceq> \<phi> y"
   215     assumes hom_le: "x \<sqsubseteq> y \<Longrightarrow> \<phi> x \<preceq> \<phi> y"
   216 
   216 
   217 text (in order_preserving) {* Here are examples of theorems that are
   217 text (in order_preserving) \<open>Here are examples of theorems that are
   218   available in the locale:
   218   available in the locale:
   219 
   219 
   220   \hspace*{1em}@{thm [source] hom_le}: @{thm hom_le}
   220   \hspace*{1em}@{thm [source] hom_le}: @{thm hom_le}
   221 
   221 
   222   \hspace*{1em}@{thm [source] le.less_le_trans}: @{thm le.less_le_trans}
   222   \hspace*{1em}@{thm [source] le.less_le_trans}: @{thm le.less_le_trans}
   226   While there is infix syntax for the strict operation associated to
   226   While there is infix syntax for the strict operation associated to
   227   @{term "op \<sqsubseteq>"}, there is none for the strict version of @{term
   227   @{term "op \<sqsubseteq>"}, there is none for the strict version of @{term
   228   "op \<preceq>"}.  The abbreviation @{text less} with its infix syntax is only
   228   "op \<preceq>"}.  The abbreviation @{text less} with its infix syntax is only
   229   available for the original instance it was declared for.  We may
   229   available for the original instance it was declared for.  We may
   230   introduce the abbreviation @{text less'} with infix syntax~@{text \<prec>}
   230   introduce the abbreviation @{text less'} with infix syntax~@{text \<prec>}
   231   with the following declaration: *}
   231   with the following declaration:\<close>
   232 
   232 
   233   abbreviation (in order_preserving)
   233   abbreviation (in order_preserving)
   234     less' (infixl "\<prec>" 50) where "less' \<equiv> partial_order.less le'"
   234     less' (infixl "\<prec>" 50) where "less' \<equiv> partial_order.less le'"
   235 
   235 
   236 text (in order_preserving) {* Now the theorem is displayed nicely as
   236 text (in order_preserving) \<open>Now the theorem is displayed nicely as
   237   @{thm [source]  le'.less_le_trans}:
   237   @{thm [source]  le'.less_le_trans}:
   238   @{thm [display, indent=2] le'.less_le_trans} *}
   238   @{thm [display, indent=2] le'.less_le_trans}\<close>
   239 
   239 
   240 text {* There are short notations for locale expressions.  These are
   240 text \<open>There are short notations for locale expressions.  These are
   241   discussed in the following. *}
   241   discussed in the following.\<close>
   242 
   242 
   243 
   243 
   244 subsection {* Default Instantiations *}
   244 subsection \<open>Default Instantiations\<close>
   245 
   245 
   246 text {* 
   246 text \<open>
   247   It is possible to omit parameter instantiations.  The
   247   It is possible to omit parameter instantiations.  The
   248   instantiation then defaults to the name of
   248   instantiation then defaults to the name of
   249   the parameter itself.  For example, the locale expression @{text
   249   the parameter itself.  For example, the locale expression @{text
   250   partial_order} is short for @{text "partial_order le"}, since the
   250   partial_order} is short for @{text "partial_order le"}, since the
   251   locale's single parameter is~@{text le}.  We took advantage of this
   251   locale's single parameter is~@{text le}.  We took advantage of this
   252   in the \isakeyword{sublocale} declarations of
   252   in the \isakeyword{sublocale} declarations of
   253   Section~\ref{sec:changing-the-hierarchy}. *}
   253   Section~\ref{sec:changing-the-hierarchy}.\<close>
   254 
   254 
   255 
   255 
   256 subsection {* Implicit Parameters \label{sec:implicit-parameters} *}
   256 subsection \<open>Implicit Parameters \label{sec:implicit-parameters}\<close>
   257 
   257 
   258 text {* In a locale expression that occurs within a locale
   258 text \<open>In a locale expression that occurs within a locale
   259   declaration, omitted parameters additionally extend the (possibly
   259   declaration, omitted parameters additionally extend the (possibly
   260   empty) \isakeyword{for} clause.
   260   empty) \isakeyword{for} clause.
   261 
   261 
   262   The \isakeyword{for} clause is a general construct of Isabelle/Isar
   262   The \isakeyword{for} clause is a general construct of Isabelle/Isar
   263   to mark names occurring in the preceding declaration as ``arbitrary
   263   to mark names occurring in the preceding declaration as ``arbitrary
   279   partial_order le \isakeyword{for} le (\isakeyword{infixl} "\(\sqsubseteq\)" 50)\textrm{.}
   279   partial_order le \isakeyword{for} le (\isakeyword{infixl} "\(\sqsubseteq\)" 50)\textrm{.}
   280 \end{alltt}
   280 \end{alltt}
   281 \end{small}
   281 \end{small}
   282   This short hand was used in the locale declarations throughout
   282   This short hand was used in the locale declarations throughout
   283   Section~\ref{sec:import}.
   283   Section~\ref{sec:import}.
   284  *}
   284 \<close>
   285 
   285 
   286 text{*
   286 text\<open>
   287   The following locale declarations provide more examples.  A
   287   The following locale declarations provide more examples.  A
   288   map~@{text \<phi>} is a lattice homomorphism if it preserves meet and
   288   map~@{text \<phi>} is a lattice homomorphism if it preserves meet and
   289   join. *}
   289   join.\<close>
   290 
   290 
   291   locale lattice_hom =
   291   locale lattice_hom =
   292     le: lattice + le': lattice le' for le' (infixl "\<preceq>" 50) +
   292     le: lattice + le': lattice le' for le' (infixl "\<preceq>" 50) +
   293     fixes \<phi>
   293     fixes \<phi>
   294     assumes hom_meet: "\<phi> (x \<sqinter> y) = le'.meet (\<phi> x) (\<phi> y)"
   294     assumes hom_meet: "\<phi> (x \<sqinter> y) = le'.meet (\<phi> x) (\<phi> y)"
   295       and hom_join: "\<phi> (x \<squnion> y) = le'.join (\<phi> x) (\<phi> y)"
   295       and hom_join: "\<phi> (x \<squnion> y) = le'.join (\<phi> x) (\<phi> y)"
   296 
   296 
   297 text {* The parameter instantiation in the first instance of @{term
   297 text \<open>The parameter instantiation in the first instance of @{term
   298   lattice} is omitted.  This causes the parameter~@{text le} to be
   298   lattice} is omitted.  This causes the parameter~@{text le} to be
   299   added to the \isakeyword{for} clause, and the locale has
   299   added to the \isakeyword{for} clause, and the locale has
   300   parameters~@{text le},~@{text le'} and, of course,~@{text \<phi>}.
   300   parameters~@{text le},~@{text le'} and, of course,~@{text \<phi>}.
   301 
   301 
   302   Before turning to the second example, we complete the locale by
   302   Before turning to the second example, we complete the locale by
   303   providing infix syntax for the meet and join operations of the
   303   providing infix syntax for the meet and join operations of the
   304   second lattice.
   304   second lattice.
   305 *}
   305 \<close>
   306 
   306 
   307   context lattice_hom
   307   context lattice_hom
   308   begin
   308   begin
   309   abbreviation meet' (infixl "\<sqinter>''" 50) where "meet' \<equiv> le'.meet"
   309   abbreviation meet' (infixl "\<sqinter>''" 50) where "meet' \<equiv> le'.meet"
   310   abbreviation join' (infixl "\<squnion>''" 50) where "join' \<equiv> le'.join"
   310   abbreviation join' (infixl "\<squnion>''" 50) where "join' \<equiv> le'.join"
   311   end
   311   end
   312 
   312 
   313 text {* The next example makes radical use of the short hand
   313 text \<open>The next example makes radical use of the short hand
   314   facilities.  A homomorphism is an endomorphism if both orders
   314   facilities.  A homomorphism is an endomorphism if both orders
   315   coincide. *}
   315   coincide.\<close>
   316 
   316 
   317   locale lattice_end = lattice_hom _ le
   317   locale lattice_end = lattice_hom _ le
   318 
   318 
   319 text {* The notation~@{text _} enables to omit a parameter in a
   319 text \<open>The notation~@{text _} enables to omit a parameter in a
   320   positional instantiation.  The omitted parameter,~@{text le} becomes
   320   positional instantiation.  The omitted parameter,~@{text le} becomes
   321   the parameter of the declared locale and is, in the following
   321   the parameter of the declared locale and is, in the following
   322   position, used to instantiate the second parameter of @{text
   322   position, used to instantiate the second parameter of @{text
   323   lattice_hom}.  The effect is that of identifying the first in second
   323   lattice_hom}.  The effect is that of identifying the first in second
   324   parameter of the homomorphism locale. *}
   324   parameter of the homomorphism locale.\<close>
   325 
   325 
   326 text {* The inheritance diagram of the situation we have now is shown
   326 text \<open>The inheritance diagram of the situation we have now is shown
   327   in Figure~\ref{fig:hom}, where the dashed line depicts an
   327   in Figure~\ref{fig:hom}, where the dashed line depicts an
   328   interpretation which is introduced below.  Parameter instantiations
   328   interpretation which is introduced below.  Parameter instantiations
   329   are indicated by $\sqsubseteq \mapsto \preceq$ etc.  By looking at
   329   are indicated by $\sqsubseteq \mapsto \preceq$ etc.  By looking at
   330   the inheritance diagram it would seem
   330   the inheritance diagram it would seem
   331   that two identical copies of each of the locales @{text
   331   that two identical copies of each of the locales @{text
   362 \end{center}
   362 \end{center}
   363 \hrule
   363 \hrule
   364 \caption{Hierarchy of Homomorphism Locales.}
   364 \caption{Hierarchy of Homomorphism Locales.}
   365 \label{fig:hom}
   365 \label{fig:hom}
   366 \end{figure}
   366 \end{figure}
   367   *}
   367 \<close>
   368 
   368 
   369 text {* It can be shown easily that a lattice homomorphism is order
   369 text \<open>It can be shown easily that a lattice homomorphism is order
   370   preserving.  As the final example of this section, a locale
   370   preserving.  As the final example of this section, a locale
   371   interpretation is used to assert this: *}
   371   interpretation is used to assert this:\<close>
   372 
   372 
   373   sublocale lattice_hom \<subseteq> order_preserving
   373   sublocale lattice_hom \<subseteq> order_preserving
   374   proof unfold_locales
   374   proof unfold_locales
   375     fix x y
   375     fix x y
   376     assume "x \<sqsubseteq> y"
   376     assume "x \<sqsubseteq> y"
   377     then have "y = (x \<squnion> y)" by (simp add: le.join_connection)
   377     then have "y = (x \<squnion> y)" by (simp add: le.join_connection)
   378     then have "\<phi> y = (\<phi> x \<squnion>' \<phi> y)" by (simp add: hom_join [symmetric])
   378     then have "\<phi> y = (\<phi> x \<squnion>' \<phi> y)" by (simp add: hom_join [symmetric])
   379     then show "\<phi> x \<preceq> \<phi> y" by (simp add: le'.join_connection)
   379     then show "\<phi> x \<preceq> \<phi> y" by (simp add: le'.join_connection)
   380   qed
   380   qed
   381 
   381 
   382 text (in lattice_hom) {*
   382 text (in lattice_hom) \<open>
   383   Theorems and other declarations --- syntax, in particular --- from
   383   Theorems and other declarations --- syntax, in particular --- from
   384   the locale @{text order_preserving} are now active in @{text
   384   the locale @{text order_preserving} are now active in @{text
   385   lattice_hom}, for example
   385   lattice_hom}, for example
   386   @{thm [source] hom_le}:
   386   @{thm [source] hom_le}:
   387   @{thm [display, indent=2] hom_le}
   387   @{thm [display, indent=2] hom_le}
   388   This theorem will be useful in the following section.
   388   This theorem will be useful in the following section.
   389   *}
   389 \<close>
   390 
   390 
   391 
   391 
   392 section {* Conditional Interpretation *}
   392 section \<open>Conditional Interpretation\<close>
   393 
   393 
   394 text {* There are situations where an interpretation is not possible
   394 text \<open>There are situations where an interpretation is not possible
   395   in the general case since the desired property is only valid if
   395   in the general case since the desired property is only valid if
   396   certain conditions are fulfilled.  Take, for example, the function
   396   certain conditions are fulfilled.  Take, for example, the function
   397   @{text "\<lambda>i. n * i"} that scales its argument by a constant factor.
   397   @{text "\<lambda>i. n * i"} that scales its argument by a constant factor.
   398   This function is order preserving (and even a lattice endomorphism)
   398   This function is order preserving (and even a lattice endomorphism)
   399   with respect to @{term "op \<le>"} provided @{text "n \<ge> 0"}.
   399   with respect to @{term "op \<le>"} provided @{text "n \<ge> 0"}.
   405   This is not fully satisfactory either, since potentially
   405   This is not fully satisfactory either, since potentially
   406   interpretations may be required to make interpretations in many
   406   interpretations may be required to make interpretations in many
   407   contexts.  What is
   407   contexts.  What is
   408   required is an interpretation that depends on the condition --- and
   408   required is an interpretation that depends on the condition --- and
   409   this can be done with the \isakeyword{sublocale} command.  For this
   409   this can be done with the \isakeyword{sublocale} command.  For this
   410   purpose, we introduce a locale for the condition. *}
   410   purpose, we introduce a locale for the condition.\<close>
   411 
   411 
   412   locale non_negative =
   412   locale non_negative =
   413     fixes n :: int
   413     fixes n :: int
   414     assumes non_neg: "0 \<le> n"
   414     assumes non_neg: "0 \<le> n"
   415 
   415 
   416 text {* It is again convenient to make the interpretation in an
   416 text \<open>It is again convenient to make the interpretation in an
   417   incremental fashion, first for order preserving maps, the for
   417   incremental fashion, first for order preserving maps, the for
   418   lattice endomorphisms. *}
   418   lattice endomorphisms.\<close>
   419 
   419 
   420   sublocale non_negative \<subseteq>
   420   sublocale non_negative \<subseteq>
   421       order_preserving "op \<le>" "op \<le>" "\<lambda>i. n * i"
   421       order_preserving "op \<le>" "op \<le>" "\<lambda>i. n * i"
   422     using non_neg by unfold_locales (rule mult_left_mono)
   422     using non_neg by unfold_locales (rule mult_left_mono)
   423 
   423 
   424 text {* While the proof of the previous interpretation
   424 text \<open>While the proof of the previous interpretation
   425   is straightforward from monotonicity lemmas for~@{term "op *"}, the
   425   is straightforward from monotonicity lemmas for~@{term "op *"}, the
   426   second proof follows a useful pattern. *}
   426   second proof follows a useful pattern.\<close>
   427 
   427 
   428   sublocale %visible non_negative \<subseteq> lattice_end "op \<le>" "\<lambda>i. n * i"
   428   sublocale %visible non_negative \<subseteq> lattice_end "op \<le>" "\<lambda>i. n * i"
   429   proof (unfold_locales, unfold int_min_eq int_max_eq)
   429   proof (unfold_locales, unfold int_min_eq int_max_eq)
   430     txt {* \normalsize Unfolding the locale predicates \emph{and} the
   430     txt \<open>\normalsize Unfolding the locale predicates \emph{and} the
   431       interpretation equations immediately yields two subgoals that
   431       interpretation equations immediately yields two subgoals that
   432       reflect the core conjecture.
   432       reflect the core conjecture.
   433       @{subgoals [display]}
   433       @{subgoals [display]}
   434       It is now necessary to show, in the context of @{term
   434       It is now necessary to show, in the context of @{term
   435       non_negative}, that multiplication by~@{term n} commutes with
   435       non_negative}, that multiplication by~@{term n} commutes with
   436       @{term min} and @{term max}. *}
   436       @{term min} and @{term max}.\<close>
   437   qed (auto simp: hom_le)
   437   qed (auto simp: hom_le)
   438 
   438 
   439 text (in order_preserving) {* The lemma @{thm [source] hom_le}
   439 text (in order_preserving) \<open>The lemma @{thm [source] hom_le}
   440   simplifies a proof that would have otherwise been lengthy and we may
   440   simplifies a proof that would have otherwise been lengthy and we may
   441   consider making it a default rule for the simplifier: *}
   441   consider making it a default rule for the simplifier:\<close>
   442 
   442 
   443   lemmas (in order_preserving) hom_le [simp]
   443   lemmas (in order_preserving) hom_le [simp]
   444 
   444 
   445 
   445 
   446 subsection {* Avoiding Infinite Chains of Interpretations
   446 subsection \<open>Avoiding Infinite Chains of Interpretations
   447   \label{sec:infinite-chains} *}
   447   \label{sec:infinite-chains}\<close>
   448 
   448 
   449 text {* Similar situations arise frequently in formalisations of
   449 text \<open>Similar situations arise frequently in formalisations of
   450   abstract algebra where it is desirable to express that certain
   450   abstract algebra where it is desirable to express that certain
   451   constructions preserve certain properties.  For example, polynomials
   451   constructions preserve certain properties.  For example, polynomials
   452   over rings are rings, or --- an example from the domain where the
   452   over rings are rings, or --- an example from the domain where the
   453   illustrations of this tutorial are taken from --- a partial order
   453   illustrations of this tutorial are taken from --- a partial order
   454   may be obtained for a function space by point-wise lifting of the
   454   may be obtained for a function space by point-wise lifting of the
   455   partial order of the co-domain.  This corresponds to the following
   455   partial order of the co-domain.  This corresponds to the following
   456   interpretation: *}
   456   interpretation:\<close>
   457 
   457 
   458   sublocale %visible partial_order \<subseteq> f: partial_order "\<lambda>f g. \<forall>x. f x \<sqsubseteq> g x"
   458   sublocale %visible partial_order \<subseteq> f: partial_order "\<lambda>f g. \<forall>x. f x \<sqsubseteq> g x"
   459     oops
   459     oops
   460 
   460 
   461 text {* Unfortunately this is a cyclic interpretation that leads to an
   461 text \<open>Unfortunately this is a cyclic interpretation that leads to an
   462   infinite chain, namely
   462   infinite chain, namely
   463   @{text [display, indent=2] "partial_order \<subseteq> partial_order (\<lambda>f g. \<forall>x. f x \<sqsubseteq> g x) \<subseteq>
   463   @{text [display, indent=2] "partial_order \<subseteq> partial_order (\<lambda>f g. \<forall>x. f x \<sqsubseteq> g x) \<subseteq>
   464   partial_order (\<lambda>f g. \<forall>x y. f x y \<sqsubseteq> g x y) \<subseteq>  \<dots>"}
   464   partial_order (\<lambda>f g. \<forall>x y. f x y \<sqsubseteq> g x y) \<subseteq>  \<dots>"}
   465   and the interpretation is rejected.
   465   and the interpretation is rejected.
   466 
   466 
   467   Instead it is necessary to declare a locale that is logically
   467   Instead it is necessary to declare a locale that is logically
   468   equivalent to @{term partial_order} but serves to collect facts
   468   equivalent to @{term partial_order} but serves to collect facts
   469   about functions spaces where the co-domain is a partial order, and
   469   about functions spaces where the co-domain is a partial order, and
   470   to make the interpretation in its context: *}
   470   to make the interpretation in its context:\<close>
   471 
   471 
   472   locale fun_partial_order = partial_order
   472   locale fun_partial_order = partial_order
   473 
   473 
   474   sublocale fun_partial_order \<subseteq>
   474   sublocale fun_partial_order \<subseteq>
   475       f: partial_order "\<lambda>f g. \<forall>x. f x \<sqsubseteq> g x"
   475       f: partial_order "\<lambda>f g. \<forall>x. f x \<sqsubseteq> g x"
   476     by unfold_locales (fast,rule,fast,blast intro: trans)
   476     by unfold_locales (fast,rule,fast,blast intro: trans)
   477 
   477 
   478 text {* It is quite common in abstract algebra that such a construction
   478 text \<open>It is quite common in abstract algebra that such a construction
   479   maps a hierarchy of algebraic structures (or specifications) to a
   479   maps a hierarchy of algebraic structures (or specifications) to a
   480   related hierarchy.  By means of the same lifting, a function space
   480   related hierarchy.  By means of the same lifting, a function space
   481   is a lattice if its co-domain is a lattice: *}
   481   is a lattice if its co-domain is a lattice:\<close>
   482 
   482 
   483   locale fun_lattice = fun_partial_order + lattice
   483   locale fun_lattice = fun_partial_order + lattice
   484 
   484 
   485   sublocale fun_lattice \<subseteq> f: lattice "\<lambda>f g. \<forall>x. f x \<sqsubseteq> g x"
   485   sublocale fun_lattice \<subseteq> f: lattice "\<lambda>f g. \<forall>x. f x \<sqsubseteq> g x"
   486   proof unfold_locales
   486   proof unfold_locales
   496     then show "\<exists>sup. partial_order.is_sup (\<lambda>f g. \<forall>x. f x \<sqsubseteq> g x) f g sup"
   496     then show "\<exists>sup. partial_order.is_sup (\<lambda>f g. \<forall>x. f x \<sqsubseteq> g x) f g sup"
   497       by fast
   497       by fast
   498   qed
   498   qed
   499 
   499 
   500 
   500 
   501 section {* Further Reading *}
   501 section \<open>Further Reading\<close>
   502 
   502 
   503 text {* More information on locales and their interpretation is
   503 text \<open>More information on locales and their interpretation is
   504   available.  For the locale hierarchy of import and interpretation
   504   available.  For the locale hierarchy of import and interpretation
   505   dependencies see~@{cite Ballarin2006a}; interpretations in theories
   505   dependencies see~@{cite Ballarin2006a}; interpretations in theories
   506   and proofs are covered in~@{cite Ballarin2006b}.  In the latter, I
   506   and proofs are covered in~@{cite Ballarin2006b}.  In the latter, I
   507   show how interpretation in proofs enables to reason about families
   507   show how interpretation in proofs enables to reason about families
   508   of algebraic structures, which cannot be expressed with locales
   508   of algebraic structures, which cannot be expressed with locales
   530   taken from Jacobson's textbook on algebra~@{cite \<open>Chapter~8\<close> Jacobson1985}.
   530   taken from Jacobson's textbook on algebra~@{cite \<open>Chapter~8\<close> Jacobson1985}.
   531 
   531 
   532   The sources of this tutorial, which include all proofs, are
   532   The sources of this tutorial, which include all proofs, are
   533   available with the Isabelle distribution at
   533   available with the Isabelle distribution at
   534   @{url "http://isabelle.in.tum.de"}.
   534   @{url "http://isabelle.in.tum.de"}.
   535   *}
   535 \<close>
   536 
   536 
   537 text {*
   537 text \<open>
   538 \begin{table}
   538 \begin{table}
   539 \hrule
   539 \hrule
   540 \vspace{2ex}
   540 \vspace{2ex}
   541 \begin{center}
   541 \begin{center}
   542 \begin{tabular}{l>$c<$l}
   542 \begin{tabular}{l>$c<$l}
   633 \end{center}
   633 \end{center}
   634 \hrule
   634 \hrule
   635 \caption{Syntax of Locale Commands.}
   635 \caption{Syntax of Locale Commands.}
   636 \label{tab:commands}
   636 \label{tab:commands}
   637 \end{table}
   637 \end{table}
   638   *}
   638 \<close>
   639 
   639 
   640 text {* \textbf{Revision History.}  For the present third revision of
   640 text \<open>\textbf{Revision History.}  For the present third revision of
   641   the tutorial, much of the explanatory text
   641   the tutorial, much of the explanatory text
   642   was rewritten.  Inheritance of interpretation equations is
   642   was rewritten.  Inheritance of interpretation equations is
   643   available with the forthcoming release of Isabelle, which at the
   643   available with the forthcoming release of Isabelle, which at the
   644   time of editing these notes is expected for the end of 2009.
   644   time of editing these notes is expected for the end of 2009.
   645   The second revision accommodates changes introduced by the locales
   645   The second revision accommodates changes introduced by the locales
   646   reimplementation for Isabelle 2009.  Most notably locale expressions
   646   reimplementation for Isabelle 2009.  Most notably locale expressions
   647   have been generalised from renaming to instantiation.  *}
   647   have been generalised from renaming to instantiation.\<close>
   648 
   648 
   649 text {* \textbf{Acknowledgements.}  Alexander Krauss, Tobias Nipkow,
   649 text \<open>\textbf{Acknowledgements.}  Alexander Krauss, Tobias Nipkow,
   650   Randy Pollack, Andreas Schropp, Christian Sternagel and Makarius Wenzel
   650   Randy Pollack, Andreas Schropp, Christian Sternagel and Makarius Wenzel
   651   have made
   651   have made
   652   useful comments on earlier versions of this document.  The section
   652   useful comments on earlier versions of this document.  The section
   653   on conditional interpretation was inspired by a number of e-mail
   653   on conditional interpretation was inspired by a number of e-mail
   654   enquiries the author received from locale users, and which suggested
   654   enquiries the author received from locale users, and which suggested
   655   that this use case is important enough to deserve explicit
   655   that this use case is important enough to deserve explicit
   656   explanation.  The term \emph{conditional interpretation} is due to
   656   explanation.  The term \emph{conditional interpretation} is due to
   657   Larry Paulson. *}
   657   Larry Paulson.\<close>
   658 
   658 
   659 end
   659 end