doc-src/Locales/Locales/Examples3.thy
changeset 30826 a53f4872400e
parent 30782 38e477e8524f
child 31748 530596c997da
equal deleted inserted replaced
30783:275577cefaa8 30826:a53f4872400e
     1 theory Examples3
     1 theory Examples3
     2 imports Examples
     2 imports Examples
     3 begin
     3 begin
     4 
       
     5 subsection {* Third Version: Local Interpretation *}
     4 subsection {* Third Version: Local Interpretation *}
     6 
     5 
     7 text {* In the above example, the fact that @{text \<le>} is a partial
     6 text {* In the above example, the fact that @{text \<le>} is a partial
     8   order for the natural numbers was used in the proof of the
     7   order for the natural numbers was used in the proof of the
     9   second goal.  In general, proofs of the equations may involve
     8   second goal.  In general, proofs of the equations may involve
    22   then interpret nat: partial_order "op \<le> :: [nat, nat] \<Rightarrow> bool" .
    21   then interpret nat: partial_order "op \<le> :: [nat, nat] \<Rightarrow> bool" .
    23   show "partial_order.less op \<le> (x::nat) y = (x < y)"
    22   show "partial_order.less op \<le> (x::nat) y = (x < y)"
    24     unfolding nat.less_def by auto
    23     unfolding nat.less_def by auto
    25 qed
    24 qed
    26 
    25 
    27 text {* The inner interpretation does not require an
    26 text {* The inner interpretation does not require an elaborate new
    28   elaborate new proof, it is immediate from the preceeding fact and
    27   proof, it is immediate from the preceding fact and proved with
    29   proved with ``.''.  Strict qualifiers are normally not necessary for
    28   ``.''.  It enriches the local proof context by the very theorems
    30   interpretations inside proofs, since these have only limited scope.
    29   also obtained in the interpretation from Section~\ref{sec:po-first},
    31 
    30   and @{text nat.less_def} may directly be used to unfold the
    32   The above interpretation enriches the local proof context by
    31   definition.  Theorems from the local interpretation disappear after
    33   the very theorems also obtained in the interpretation from
    32   leaving the proof context --- that is, after the closing
    34   Section~\ref{sec:po-first}, and @{text nat.less_def} may directly be
    33   \isakeyword{qed} --- and are then replaced by those with the desired
    35   used to unfold the definition.  Theorems from the local
    34   substitutions of the strict order.  *}
    36   interpretation disappear after leaving the proof context --- that
       
    37   is, after the closing \isakeyword{qed} --- and are
       
    38   then replaced by those with the desired substitutions of the strict
       
    39   order.  *}
       
    40 
    35 
    41 
    36 
    42 subsection {* Further Interpretations *}
    37 subsection {* Further Interpretations *}
    43 
    38 
    44 text {* Further interpretations are necessary to reuse theorems from
    39 text {* Further interpretations are necessary to reuse theorems from
    61     apply (unfold nat.is_inf_def nat.is_sup_def)
    56     apply (unfold nat.is_inf_def nat.is_sup_def)
    62     txt {* the goals become @{subgoals [display]} which can be solved
    57     txt {* the goals become @{subgoals [display]} which can be solved
    63       by Presburger arithmetic. *}
    58       by Presburger arithmetic. *}
    64     by arith+
    59     by arith+
    65   txt {* For the first of the equations, we refer to the theorem
    60   txt {* For the first of the equations, we refer to the theorem
    66   generated in the previous interpretation. *}
    61   shown in the previous interpretation. *}
    67   show "partial_order.less op \<le> (x::nat) y = (x < y)"
    62   show "partial_order.less op \<le> (x::nat) y = (x < y)"
    68     by (rule nat_less_eq)
    63     by (rule nat_less_eq)
    69   txt {* In order to show the remaining equations, we put ourselves in a
    64   txt {* In order to show the remaining equations, we put ourselves in a
    70     situation where the lattice theorems can be used in a convenient way. *}
    65     situation where the lattice theorems can be used in a convenient way. *}
    71   from lattice interpret nat: lattice "op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool" .
    66   from lattice interpret nat: lattice "op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool" .
    73     by (bestsimp simp: nat.meet_def nat.is_inf_def)
    68     by (bestsimp simp: nat.meet_def nat.is_inf_def)
    74   show "lattice.join op \<le> (x::nat) y = max x y"
    69   show "lattice.join op \<le> (x::nat) y = max x y"
    75     by (bestsimp simp: nat.join_def nat.is_sup_def)
    70     by (bestsimp simp: nat.join_def nat.is_sup_def)
    76 qed
    71 qed
    77 
    72 
    78 text {* The interpretation that the relation @{text \<le>} is a total
    73 text {* Next follows that @{text \<le>} is a total order. *}
    79   order follows next. *}
       
    80 
    74 
    81 interpretation %visible nat: total_order "op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool"
    75 interpretation %visible nat: total_order "op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool"
    82   where "partial_order.less op \<le> (x::nat) y = (x < y)"
    76   where "partial_order.less op \<le> (x::nat) y = (x < y)"
    83     and "lattice.meet op \<le> (x::nat) y = min x y"
    77     and "lattice.meet op \<le> (x::nat) y = min x y"
    84     and "lattice.join op \<le> (x::nat) y = max x y"
    78     and "lattice.join op \<le> (x::nat) y = max x y"
    85 proof -
    79 proof -
    86   show "total_order (op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool)" by unfold_locales arith
    80   show "total_order (op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool)"
       
    81     by unfold_locales arith
    87 qed (rule nat_less_eq nat_meet_eq nat_join_eq)+
    82 qed (rule nat_less_eq nat_meet_eq nat_join_eq)+
    88 
    83 
    89 text {* Note that since the locale hierarchy reflects that total
    84 text {* Since the locale hierarchy reflects that total
    90   orders are distributive lattices, an explicit interpretation of
    85   orders are distributive lattices, an explicit interpretation of
    91   distributive lattices for the order relation on natural numbers is
    86   distributive lattices for the order relation on natural numbers is
    92   only necessary for mapping the definitions to the right operators on
    87   only necessary for mapping the definitions to the right operators on
    93   @{typ nat}. *}
    88   @{typ nat}. *}
    94 
    89 
   128 text {* Divisibility on the natural numbers is a distributive lattice
   123 text {* Divisibility on the natural numbers is a distributive lattice
   129   but not a total order.  Interpretation again proceeds
   124   but not a total order.  Interpretation again proceeds
   130   incrementally. *}
   125   incrementally. *}
   131 
   126 
   132 interpretation nat_dvd: partial_order "op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool"
   127 interpretation nat_dvd: partial_order "op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool"
   133   where nat_dvd_less_eq: "partial_order.less op dvd (x::nat) y = (x dvd y \<and> x \<noteq> y)"
   128   where nat_dvd_less_eq:
       
   129     "partial_order.less op dvd (x::nat) y = (x dvd y \<and> x \<noteq> y)"
   134 proof -
   130 proof -
   135   show "partial_order (op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool)"
   131   show "partial_order (op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool)"
   136     by unfold_locales (auto simp: dvd_def)
   132     by unfold_locales (auto simp: dvd_def)
   137   then interpret nat_dvd: partial_order "op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool" .
   133   then interpret nat_dvd: partial_order "op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool" .
   138   show "partial_order.less op dvd (x::nat) y = (x dvd y \<and> x \<noteq> y)"
   134   show "partial_order.less op dvd (x::nat) y = (x dvd y \<and> x \<noteq> y)"
   174     apply (unfold nat_dvd.is_sup_def)
   170     apply (unfold nat_dvd.is_sup_def)
   175     by (auto intro: lcm_dvd1 lcm_dvd2 lcm_least)
   171     by (auto intro: lcm_dvd1 lcm_dvd2 lcm_least)
   176 qed
   172 qed
   177 
   173 
   178 text {* Equations @{thm [source] nat_dvd_meet_eq} and @{thm [source]
   174 text {* Equations @{thm [source] nat_dvd_meet_eq} and @{thm [source]
   179   nat_dvd_join_eq} are named since they are handy in the proof of
   175   nat_dvd_join_eq} are used in the main part the subsequent
   180   the subsequent interpretation. *}
   176   interpretation. *}
   181 
   177 
   182 (*
   178 (*
   183 definition
   179 definition
   184   is_lcm :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool" where
   180   is_lcm :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> bool" where
   185   "is_lcm p m n \<longleftrightarrow> m dvd p \<and> n dvd p \<and>
   181   "is_lcm p m n \<longleftrightarrow> m dvd p \<and> n dvd p \<and>
   236 \label{tab:nat-dvd-lattice}
   232 \label{tab:nat-dvd-lattice}
   237 \end{table}
   233 \end{table}
   238   *}
   234   *}
   239 
   235 
   240 text {*
   236 text {*
   241   The full syntax of the interpretation commands is shown in
   237   The syntax of the interpretation commands is shown in
   242   Table~\ref{tab:commands}.  The grammar refers to
   238   Table~\ref{tab:commands}.  The grammar refers to
   243   \textit{expression}, which stands for a \emph{locale} expression.
   239   \textit{expression}, which stands for a \emph{locale} expression.
   244   Locale expressions are discussed in the following section.
   240   Locale expressions are discussed in the following section.
   245   *}
   241   *}
   246 
   242 
   255   existing locale for both.
   251   existing locale for both.
   256 
   252 
   257   Inspecting the grammar of locale commands in
   253   Inspecting the grammar of locale commands in
   258   Table~\ref{tab:commands} reveals that the import of a locale can be
   254   Table~\ref{tab:commands} reveals that the import of a locale can be
   259   more than just a single locale.  In general, the import is a
   255   more than just a single locale.  In general, the import is a
   260   \emph{locale expression}.  These enable to combine locales
   256   \emph{locale expression}, which enables to combine locales
   261   and instantiate parameters.  A locale expression is a sequence of
   257   and instantiate parameters.  A locale expression is a sequence of
   262   locale \emph{instances} followed by an optional \isakeyword{for}
   258   locale \emph{instances} followed by an optional \isakeyword{for}
   263   clause.  Each instance consists of a locale reference, which may be
   259   clause.  Each instance consists of a locale reference, which may be
   264   preceded by a qualifer and succeeded by instantiations of the
   260   preceded by a qualifer and succeeded by instantiations of the
   265   parameters of that locale.  Instantiations may be either positional
   261   parameters of that locale.  Instantiations may be either positional
   266   or through explicit parameter argument pairs.
   262   or through explicit mappings of parameters to arguments.
   267 
   263 
   268   Using a locale expression, a locale for order
   264   Using a locale expression, a locale for order
   269   preserving maps can be declared in the following way.  *}
   265   preserving maps can be declared in the following way.  *}
   270 
   266 
   271   locale order_preserving =
   267   locale order_preserving =
   273       for le (infixl "\<sqsubseteq>" 50) and le' (infixl "\<preceq>" 50) +
   269       for le (infixl "\<sqsubseteq>" 50) and le' (infixl "\<preceq>" 50) +
   274     fixes \<phi> :: "'a \<Rightarrow> 'b"
   270     fixes \<phi> :: "'a \<Rightarrow> 'b"
   275     assumes hom_le: "x \<sqsubseteq> y \<Longrightarrow> \<phi> x \<preceq> \<phi> y"
   271     assumes hom_le: "x \<sqsubseteq> y \<Longrightarrow> \<phi> x \<preceq> \<phi> y"
   276 
   272 
   277 text {* The second and third line contain the expression --- two
   273 text {* The second and third line contain the expression --- two
   278   instances of the partial order locale with instantiations @{text le}
   274   instances of the partial order locale where the parameter is
       
   275   instantiated to @{text le}
   279   and @{text le'}, respectively.  The \isakeyword{for} clause consists
   276   and @{text le'}, respectively.  The \isakeyword{for} clause consists
   280   of parameter declarations and is similar to the context element
   277   of parameter declarations and is similar to the context element
   281   \isakeyword{fixes}.  The notable difference is that the
   278   \isakeyword{fixes}.  The notable difference is that the
   282   \isakeyword{for} clause is part of the expression, and only
   279   \isakeyword{for} clause is part of the expression, and only
   283   parameters defined in the expression may occur in its instances.
   280   parameters defined in the expression may occur in its instances.
   284 
   281 
   285   Instances are \emph{morphisms} on locales.  Their effect on the
   282   Instances define \emph{morphisms} on locales.  Their effect on the
   286   parameters is naturally lifted to terms, propositions and theorems,
   283   parameters is lifted to terms, propositions and theorems in the
       
   284   canonical way,
   287   and thus to the assumptions and conclusions of a locale.  The
   285   and thus to the assumptions and conclusions of a locale.  The
   288   assumption of a locale expression is the conjunction of the
   286   assumption of a locale expression is the conjunction of the
   289   assumptions of the instances.  The conclusions of a sequence of
   287   assumptions of the instances.  The conclusions of a sequence of
   290   instances are obtained by appending the conclusions of the
   288   instances are obtained by appending the conclusions of the
   291   instances in the order of the sequence.
   289   instances in the order of the sequence.
   311   are qualified by @{text le'}.  For example, @{thm [source]
   309   are qualified by @{text le'}.  For example, @{thm [source]
   312   le'.less_le_trans}: @{thm [display, indent=2] le'.less_le_trans} *}
   310   le'.less_le_trans}: @{thm [display, indent=2] le'.less_le_trans} *}
   313 
   311 
   314 end %invisible
   312 end %invisible
   315 
   313 
   316 text {* This example reveals that there is no infix syntax for the strict
   314 text {* This example reveals that there is no infix syntax for the
   317   version of @{text \<preceq>}!  This can be declared through an abbreviation.
   315   strict operation associated with @{text \<preceq>}.  This can be declared
   318   *}
   316   through an abbreviation.  *}
   319 
   317 
   320   abbreviation (in order_preserving)
   318   abbreviation (in order_preserving)
   321     less' (infixl "\<prec>" 50) where "less' \<equiv> partial_order.less le'"
   319     less' (infixl "\<prec>" 50) where "less' \<equiv> partial_order.less le'"
   322 
   320 
   323 text (in order_preserving) {* Now the theorem is displayed nicely as
   321 text (in order_preserving) {* Now the theorem is displayed nicely as
   324   @{thm le'.less_le_trans}.  *}
   322   @{thm [source]  le'.less_le_trans}:
   325 
   323   @{thm [display, indent=2] le'.less_le_trans} *}
   326 text {* Qualifiers not only apply to theorem names, but also to names
   324 
   327   introduced by definitions and abbreviations.  In locale
   325 text (in order_preserving)  {* Qualifiers not only apply to theorem names, but also to names
   328   @{text partial_order} the full name of the strict order of @{text \<sqsubseteq>} is
   326   introduced by definitions and abbreviations.  For example, in @{text
   329   @{text le.less} and therefore @{text le'.less} is the full name of
   327   partial_order} the name @{text less} abbreviates @{term
   330   the strict order of @{text \<preceq>}.  Hence, the equation in the
   328   "partial_order.less le"}.  Therefore, in @{text order_preserving}
   331   abbreviation above could have been also written as @{text "less' \<equiv>
   329   the qualified name @{text le.less} abbreviates @{term
       
   330   "partial_order.less le"} and @{text le'.less} abbreviates @{term
       
   331   "partial_order.less le'"}.  Hence, the equation in the abbreviation
       
   332   above could have been written more concisely as @{text "less' \<equiv>
   332   le'.less"}. *}
   333   le'.less"}. *}
   333 
   334 
   334 text {* Readers may find the declaration of locale @{text
   335 text {* Readers may find the declaration of locale @{text
   335   order_preserving} a little awkward, because the declaration and
   336   order_preserving} a little awkward, because the declaration and
   336   concrete syntax for @{text le} from @{text partial_order} are
   337   concrete syntax for @{text le} from @{text partial_order} are
   340   provided for it.  In positional instantiations, a parameter position
   341   provided for it.  In positional instantiations, a parameter position
   341   may be skipped with an underscore, and it is allowed to give fewer
   342   may be skipped with an underscore, and it is allowed to give fewer
   342   instantiation terms than the instantiated locale's number of
   343   instantiation terms than the instantiated locale's number of
   343   parameters.  In named instantiations, instantiation pairs for
   344   parameters.  In named instantiations, instantiation pairs for
   344   certain parameters may simply be omitted.  Untouched parameters are
   345   certain parameters may simply be omitted.  Untouched parameters are
   345   declared by the locale expression and with their concrete syntax by
   346   implicitly declared by the locale expression and with their concrete
   346   implicitly adding them to the beginning of the \isakeyword{for}
   347   syntax.  In the sequence of parameters, they appear before the
   347   clause.
   348   parameters from the \isakeyword{for} clause.
   348 
   349 
   349   The following locales illustrate this.  A map @{text \<phi>} is a
   350   The following locales illustrate this.  A map @{text \<phi>} is a
   350   lattice homomorphism if it preserves meet and join. *}
   351   lattice homomorphism if it preserves meet and join. *}
   351 
   352 
   352   locale lattice_hom =
   353   locale lattice_hom =
   353     le: lattice + le': lattice le' for le' (infixl "\<preceq>" 50) +
   354     le: lattice + le': lattice le' for le' (infixl "\<preceq>" 50) +
   354     fixes \<phi>
   355     fixes \<phi>
   355     assumes hom_meet:
   356     assumes hom_meet: "\<phi> (x \<sqinter> y) = le'.meet (\<phi> x) (\<phi> y)"
   356 	"\<phi> (x \<sqinter> y) = le'.meet (\<phi> x) (\<phi> y)"
   357       and hom_join: "\<phi> (x \<squnion> y) = le'.join (\<phi> x) (\<phi> y)"
   357       and hom_join:
       
   358 	"\<phi> (x \<squnion> y) = le'.join (\<phi> x) (\<phi> y)"
       
   359 
   358 
   360   abbreviation (in lattice_hom)
   359   abbreviation (in lattice_hom)
   361     meet' (infixl "\<sqinter>''" 50) where "meet' \<equiv> le'.meet"
   360     meet' (infixl "\<sqinter>''" 50) where "meet' \<equiv> le'.meet"
   362   abbreviation (in lattice_hom)
   361   abbreviation (in lattice_hom)
   363     join' (infixl "\<squnion>''" 50) where "join' \<equiv> le'.join"
   362     join' (infixl "\<squnion>''" 50) where "join' \<equiv> le'.join"
   365 text {* A homomorphism is an endomorphism if both orders coincide. *}
   364 text {* A homomorphism is an endomorphism if both orders coincide. *}
   366 
   365 
   367   locale lattice_end = lattice_hom _ le
   366   locale lattice_end = lattice_hom _ le
   368 
   367 
   369 text {* In this declaration, the first parameter of @{text
   368 text {* In this declaration, the first parameter of @{text
   370   lattice_hom}, @{text le}, is untouched and then used to instantiate
   369   lattice_hom}, @{text le}, is untouched and is then used to instantiate
   371   the second parameter.  Its concrete syntax is preserved. *}
   370   the second parameter.  Its concrete syntax is preserved. *}
   372 
   371 
   373 
   372 
   374 text {* The inheritance diagram of the situation we have now is shown
   373 text {* The inheritance diagram of the situation we have now is shown
   375   in Figure~\ref{fig:hom}, where the dashed line depicts an
   374   in Figure~\ref{fig:hom}, where the dashed line depicts an
   428 
   427 
   429 text (in lattice_hom) {*
   428 text (in lattice_hom) {*
   430   Theorems and other declarations --- syntax, in particular --- from
   429   Theorems and other declarations --- syntax, in particular --- from
   431   the locale @{text order_preserving} are now active in @{text
   430   the locale @{text order_preserving} are now active in @{text
   432   lattice_hom}, for example
   431   lattice_hom}, for example
   433 
   432   @{thm [source] hom_le}:
   434   @{thm [source] le'.less_le_trans}:
   433   @{thm [display, indent=2] hom_le}
   435   @{thm le'.less_le_trans}
       
   436   *}
   434   *}
   437 
   435 
   438 
   436 
   439 
   437 
   440 section {* Further Reading *}
   438 section {* Further Reading *}
   557   & \textbf{print\_locale} [ ``\textbf{!}'' ] \textit{locale} \\
   555   & \textbf{print\_locale} [ ``\textbf{!}'' ] \textit{locale} \\
   558   & | & \textbf{print\_locales} 
   556   & | & \textbf{print\_locales} 
   559 \end{tabular}
   557 \end{tabular}
   560 \end{center}
   558 \end{center}
   561 \hrule
   559 \hrule
   562 \caption{Syntax of Locale Commands (abridged).}
   560 \caption{Syntax of Locale Commands.}
   563 \label{tab:commands}
   561 \label{tab:commands}
   564 \end{table}
   562 \end{table}
   565   *}
   563   *}
   566 
   564 
   567 text {* \textbf{Acknowledgements.}  Alexander Krauss, Tobias Nipkow,
   565 text {* \textbf{Acknowledgements.}  Alexander Krauss, Tobias Nipkow,