doc-src/Locales/Locales/Examples3.thy
changeset 32804 ca430e6aee1c
parent 31960 1984af09eb41
child 32981 0114e04a0d64
equal deleted inserted replaced
32803:fc02b4b9eccc 32804:ca430e6aee1c
    12   conveniently in the context through an auxiliary local interpretation (keyword
    12   conveniently in the context through an auxiliary local interpretation (keyword
    13   \isakeyword{interpret}).  This interpretation is inside the proof of the global
    13   \isakeyword{interpret}).  This interpretation is inside the proof of the global
    14   interpretation.  The third revision of the example illustrates this.  *}
    14   interpretation.  The third revision of the example illustrates this.  *}
    15 
    15 
    16 interpretation %visible nat: partial_order "op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool"
    16 interpretation %visible nat: partial_order "op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool"
    17   where nat_less_eq: "partial_order.less op \<le> (x::nat) y = (x < y)"
    17   where "partial_order.less op \<le> (x::nat) y = (x < y)"
    18 proof -
    18 proof -
    19   show "partial_order (op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool)"
    19   show "partial_order (op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool)"
    20     by unfold_locales auto
    20     by unfold_locales auto
    21   then interpret nat: partial_order "op \<le> :: [nat, nat] \<Rightarrow> bool" .
    21   then interpret nat: partial_order "op \<le> :: [nat, nat] \<Rightarrow> bool" .
    22   show "partial_order.less op \<le> (x::nat) y = (x < y)"
    22   show "partial_order.less op \<le> (x::nat) y = (x < y)"
    42   @{term "max :: nat \<Rightarrow> nat \<Rightarrow> nat"}.  The entire proof for the
    42   @{term "max :: nat \<Rightarrow> nat \<Rightarrow> nat"}.  The entire proof for the
    43   interpretation is reproduced in order to give an example of a more
    43   interpretation is reproduced in order to give an example of a more
    44   elaborate interpretation proof.  *}
    44   elaborate interpretation proof.  *}
    45 
    45 
    46 interpretation %visible nat: lattice "op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool"
    46 interpretation %visible nat: lattice "op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool"
    47   where "partial_order.less op \<le> (x::nat) y = (x < y)"
    47   where "lattice.meet op \<le> (x::nat) y = min x y"
    48     and nat_meet_eq: "lattice.meet op \<le> (x::nat) y = min x y"
    48     and "lattice.join op \<le> (x::nat) y = max x y"
    49     and nat_join_eq: "lattice.join op \<le> (x::nat) y = max x y"
       
    50 proof -
    49 proof -
    51   show lattice: "lattice (op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool)"
    50   show "lattice (op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool)"
    52     txt {* We have already shown that this is a partial order, *}
    51     txt {* We have already shown that this is a partial order, *}
    53     apply unfold_locales
    52     apply unfold_locales
    54     txt {* hence only the lattice axioms remain to be shown: @{subgoals
    53     txt {* hence only the lattice axioms remain to be shown: @{subgoals
    55       [display]}  After unfolding @{text is_inf} and @{text is_sup}, *}
    54       [display]}  After unfolding @{text is_inf} and @{text is_sup}, *}
    56     apply (unfold nat.is_inf_def nat.is_sup_def)
    55     apply (unfold nat.is_inf_def nat.is_sup_def)
    57     txt {* the goals become @{subgoals [display]} which can be solved
    56     txt {* the goals become @{subgoals [display]} which can be solved
    58       by Presburger arithmetic. *}
    57       by Presburger arithmetic. *}
    59     by arith+
    58     by arith+
    60   txt {* For the first of the equations, we refer to the theorem
    59   txt {* In order to show the equations, we put ourselves in a
    61   shown in the previous interpretation. *}
       
    62   show "partial_order.less op \<le> (x::nat) y = (x < y)"
       
    63     by (rule nat_less_eq)
       
    64   txt {* In order to show the remaining equations, we put ourselves in a
       
    65     situation where the lattice theorems can be used in a convenient way. *}
    60     situation where the lattice theorems can be used in a convenient way. *}
    66   from lattice interpret nat: lattice "op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool" .
    61   then interpret nat: lattice "op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool" .
    67   show "lattice.meet op \<le> (x::nat) y = min x y"
    62   show "lattice.meet op \<le> (x::nat) y = min x y"
    68     by (bestsimp simp: nat.meet_def nat.is_inf_def)
    63     by (bestsimp simp: nat.meet_def nat.is_inf_def)
    69   show "lattice.join op \<le> (x::nat) y = max x y"
    64   show "lattice.join op \<le> (x::nat) y = max x y"
    70     by (bestsimp simp: nat.join_def nat.is_sup_def)
    65     by (bestsimp simp: nat.join_def nat.is_sup_def)
    71 qed
    66 qed
    72 
    67 
    73 text {* Next follows that @{text \<le>} is a total order. *}
    68 text {* Next follows that @{text \<le>} is a total order. *}
    74 
    69 
    75 interpretation %visible nat: total_order "op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool"
    70 interpretation %visible nat: total_order "op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool"
    76   where "partial_order.less op \<le> (x::nat) y = (x < y)"
    71   by unfold_locales arith
    77     and "lattice.meet op \<le> (x::nat) y = min x y"
       
    78     and "lattice.join op \<le> (x::nat) y = max x y"
       
    79 proof -
       
    80   show "total_order (op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool)"
       
    81     by unfold_locales arith
       
    82 qed (rule nat_less_eq nat_meet_eq nat_join_eq)+
       
    83 
       
    84 text {* Since the locale hierarchy reflects that total
       
    85   orders are distributive lattices, an explicit interpretation of
       
    86   distributive lattices for the order relation on natural numbers is
       
    87   only necessary for mapping the definitions to the right operators on
       
    88   @{typ nat}. *}
       
    89 
       
    90 interpretation %visible nat: distrib_lattice "op \<le> :: nat \<Rightarrow> nat \<Rightarrow> bool"
       
    91   where "partial_order.less op \<le> (x::nat) y = (x < y)"
       
    92     and "lattice.meet op \<le> (x::nat) y = min x y"
       
    93     and "lattice.join op \<le> (x::nat) y = max x y"
       
    94   by unfold_locales [1] (rule nat_less_eq nat_meet_eq nat_join_eq)+
       
    95 
    72 
    96 text {* Theorems that are available in the theory at this point are shown in
    73 text {* Theorems that are available in the theory at this point are shown in
    97   Table~\ref{tab:nat-lattice}.
    74   Table~\ref{tab:nat-lattice}.
    98 
    75 
    99 \begin{table}
    76 \begin{table}
   113 \end{center}
    90 \end{center}
   114 \hrule
    91 \hrule
   115 \caption{Interpreted theorems for @{text \<le>} on the natural numbers.}
    92 \caption{Interpreted theorems for @{text \<le>} on the natural numbers.}
   116 \label{tab:nat-lattice}
    93 \label{tab:nat-lattice}
   117 \end{table}
    94 \end{table}
       
    95 
       
    96   Note that since the locale hierarchy reflects that total orders are
       
    97   distributive lattices, an explicit interpretation of distributive
       
    98   lattices for the order relation on natural numbers is not neccessary.
       
    99 
       
   100   Why not push this idea further and just give the last interpretation
       
   101   as a single interpretation instead of the sequence of three?  The
       
   102   reasons for this are twofold:
       
   103 \begin{itemize}
       
   104 \item
       
   105   Often it is easier to work in an incremental fashion, because later
       
   106   interpretations require theorems provided by earlier
       
   107   interpretations.
       
   108 \item
       
   109   Assume that a definition is made in some locale $l_1$, and that $l_2$
       
   110   imports $l_1$.  Let an equation for the definition be
       
   111   proved in an interpretation of $l_2$.  The equation will be unfolded
       
   112   in interpretations of theorems added to $l_2$ or below in the import
       
   113   hierarchy, but not for theorems added above $l_2$.
       
   114   Hence, an equation interpreting a definition should always be given in
       
   115   an interpretation of the locale where the definition is made, not in
       
   116   an interpretation of a locale further down the hierarchy.
       
   117 \end{itemize}
   118   *}
   118   *}
   119 
   119 
   120 
   120 
   121 subsection {* Lattice @{text "dvd"} on @{typ nat} *}
   121 subsection {* Lattice @{text "dvd"} on @{typ nat} *}
   122 
   122 
   123 text {* Divisibility on the natural numbers is a distributive lattice
   123 text {* Divisibility on the natural numbers is a distributive lattice
   124   but not a total order.  Interpretation again proceeds
   124   but not a total order.  Interpretation again proceeds
   125   incrementally. *}
   125   incrementally. *}
   126 
   126 
   127 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"
   128   where nat_dvd_less_eq:
   128   where "partial_order.less op dvd (x::nat) y = (x dvd y \<and> x \<noteq> y)"
   129     "partial_order.less op dvd (x::nat) y = (x dvd y \<and> x \<noteq> y)"
       
   130 proof -
   129 proof -
   131   show "partial_order (op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool)"
   130   show "partial_order (op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool)"
   132     by unfold_locales (auto simp: dvd_def)
   131     by unfold_locales (auto simp: dvd_def)
   133   then interpret nat_dvd: partial_order "op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool" .
   132   then interpret nat_dvd: partial_order "op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool" .
   134   show "partial_order.less op dvd (x::nat) y = (x dvd y \<and> x \<noteq> y)"
   133   show "partial_order.less op dvd (x::nat) y = (x dvd y \<and> x \<noteq> y)"
   140 text {* Note that in Isabelle/HOL there is no symbol for strict
   139 text {* Note that in Isabelle/HOL there is no symbol for strict
   141   divisibility.  Instead, interpretation substitutes @{term "x dvd y \<and>
   140   divisibility.  Instead, interpretation substitutes @{term "x dvd y \<and>
   142   x \<noteq> y"}.  *}
   141   x \<noteq> y"}.  *}
   143 
   142 
   144 interpretation nat_dvd: lattice "op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool"
   143 interpretation nat_dvd: lattice "op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool"
   145   where "partial_order.less op dvd (x::nat) y = (x dvd y \<and> x \<noteq> y)"
   144   where nat_dvd_meet_eq: "lattice.meet (op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool) = gcd"
   146     and nat_dvd_meet_eq: "lattice.meet (op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool) = gcd"
       
   147     and nat_dvd_join_eq: "lattice.join (op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool) = lcm"
   145     and nat_dvd_join_eq: "lattice.join (op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool) = lcm"
   148 proof -
   146 proof -
   149   show "lattice (op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool)"
   147   show "lattice (op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool)"
   150     apply unfold_locales
   148     apply unfold_locales
   151     apply (unfold nat_dvd.is_inf_def nat_dvd.is_sup_def)
   149     apply (unfold nat_dvd.is_inf_def nat_dvd.is_sup_def)
   153     apply auto [1]
   151     apply auto [1]
   154     apply (rule_tac x = "lcm x y" in exI)
   152     apply (rule_tac x = "lcm x y" in exI)
   155     apply (auto intro: lcm_least_nat)
   153     apply (auto intro: lcm_least_nat)
   156     done
   154     done
   157   then interpret nat_dvd: lattice "op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool" .
   155   then interpret nat_dvd: lattice "op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool" .
   158   show "partial_order.less op dvd (x::nat) y = (x dvd y \<and> x \<noteq> y)"
       
   159     by (rule nat_dvd_less_eq)
       
   160   show "lattice.meet (op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool) = gcd"
   156   show "lattice.meet (op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool) = gcd"
   161     apply (auto simp add: expand_fun_eq)
   157     apply (auto simp add: expand_fun_eq)
   162     apply (unfold nat_dvd.meet_def)
   158     apply (unfold nat_dvd.meet_def)
   163     apply (rule the_equality)
   159     apply (rule the_equality)
   164     apply (unfold nat_dvd.is_inf_def)
   160     apply (unfold nat_dvd.is_inf_def)
   196 lemma %invisible gcd_lcm_distr:
   192 lemma %invisible gcd_lcm_distr:
   197   "gcd x (lcm y z) = lcm (gcd x y) (gcd x z)" sorry
   193   "gcd x (lcm y z) = lcm (gcd x y) (gcd x z)" sorry
   198 
   194 
   199 interpretation %visible nat_dvd:
   195 interpretation %visible nat_dvd:
   200   distrib_lattice "op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool"
   196   distrib_lattice "op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool"
   201   where "partial_order.less op dvd (x::nat) y = (x dvd y \<and> x \<noteq> y)"
   197   apply unfold_locales
   202     and "lattice.meet (op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool) = gcd"
   198   txt {* @{subgoals [display]} *}
   203     and "lattice.join (op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool) = lcm"
   199   apply (unfold nat_dvd_meet_eq nat_dvd_join_eq)
   204 proof -
   200   txt {* @{subgoals [display]} *}
   205   show "distrib_lattice (op dvd :: nat \<Rightarrow> nat \<Rightarrow> bool)"
   201   apply (rule gcd_lcm_distr) done
   206     apply unfold_locales
       
   207     txt {* @{subgoals [display]} *}
       
   208     apply (unfold nat_dvd_meet_eq nat_dvd_join_eq)
       
   209     txt {* @{subgoals [display]} *}
       
   210     apply (rule gcd_lcm_distr)
       
   211     done
       
   212 qed (rule nat_dvd_less_eq nat_dvd_meet_eq nat_dvd_join_eq)+
       
   213 
   202 
   214 
   203 
   215 text {* Theorems that are available in the theory after these
   204 text {* Theorems that are available in the theory after these
   216   interpretations are shown in Table~\ref{tab:nat-dvd-lattice}.
   205   interpretations are shown in Table~\ref{tab:nat-dvd-lattice}.
   217 
   206