src/HOL/Nat.thy
changeset 30971 7fbebf75b3ef
parent 30966 55104c664185
child 30975 b2fa60d56735
equal deleted inserted replaced
30970:3fe2e418a071 30971:7fbebf75b3ef
  1164 end
  1164 end
  1165 
  1165 
  1166 
  1166 
  1167 subsection {* Natural operation of natural numbers on functions *}
  1167 subsection {* Natural operation of natural numbers on functions *}
  1168 
  1168 
  1169 text {* @{text "f o^ n = f o ... o f"}, the n-fold composition of @{text f} *}
  1169 text {*
       
  1170   We use the same logical constant for the power operations on
       
  1171   functions and relations, in order to share the same syntax.
       
  1172 *}
       
  1173 
       
  1174 consts compow :: "nat \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b)"
       
  1175 
       
  1176 abbreviation compower :: "('a \<Rightarrow> 'b) \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'b" (infixr "^^" 80) where
       
  1177   "f ^^ n \<equiv> compow n f"
       
  1178 
       
  1179 notation (latex output)
       
  1180   compower ("(_\<^bsup>_\<^esup>)" [1000] 1000)
       
  1181 
       
  1182 notation (HTML output)
       
  1183   compower ("(_\<^bsup>_\<^esup>)" [1000] 1000)
       
  1184 
       
  1185 text {* @{text "f ^^ n = f o ... o f"}, the n-fold composition of @{text f} *}
       
  1186 
       
  1187 overloading
       
  1188   funpow == "compow :: nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a)"
       
  1189 begin
  1170 
  1190 
  1171 primrec funpow :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a" where
  1191 primrec funpow :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a" where
  1172     "funpow 0 f = id"
  1192     "funpow 0 f = id"
  1173   | "funpow (Suc n) f = f o funpow n f"
  1193   | "funpow (Suc n) f = f o funpow n f"
  1174 
  1194 
  1175 abbreviation funpower :: "('a \<Rightarrow> 'a) \<Rightarrow> nat \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "o^" 80) where
  1195 end
  1176   "f o^ n \<equiv> funpow n f"
  1196 
  1177 
  1197 text {* for code generation *}
  1178 notation (latex output)
  1198 
  1179   funpower ("(_\<^bsup>_\<^esup>)" [1000] 1000)
  1199 definition funpow :: "nat \<Rightarrow> ('a \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'a" where
  1180 
  1200   funpow_code_def [code post]: "funpow = compow"
  1181 notation (HTML output)
  1201 
  1182   funpower ("(_\<^bsup>_\<^esup>)" [1000] 1000)
  1202 lemmas [code inline] = funpow_code_def [symmetric]
       
  1203 
       
  1204 lemma [code]:
       
  1205   "funpow 0 f = id"
       
  1206   "funpow (Suc n) f = f o funpow n f"
       
  1207   unfolding funpow_code_def by simp_all
       
  1208 
       
  1209 definition "foo = id ^^ (1 + 1)"
  1183 
  1210 
  1184 lemma funpow_add:
  1211 lemma funpow_add:
  1185   "f o^ (m + n) = f o^ m \<circ> f o^ n"
  1212   "f ^^ (m + n) = f ^^ m \<circ> f ^^ n"
  1186   by (induct m) simp_all
  1213   by (induct m) simp_all
  1187 
  1214 
  1188 lemma funpow_swap1:
  1215 lemma funpow_swap1:
  1189   "f ((f o^ n) x) = (f o^ n) (f x)"
  1216   "f ((f ^^ n) x) = (f ^^ n) (f x)"
  1190 proof -
  1217 proof -
  1191   have "f ((f o^ n) x) = (f o^ (n + 1)) x" by simp
  1218   have "f ((f ^^ n) x) = (f ^^ (n + 1)) x" by simp
  1192   also have "\<dots>  = (f o^ n o f o^ 1) x" by (simp only: funpow_add)
  1219   also have "\<dots>  = (f ^^ n o f ^^ 1) x" by (simp only: funpow_add)
  1193   also have "\<dots> = (f o^ n) (f x)" by simp
  1220   also have "\<dots> = (f ^^ n) (f x)" by simp
  1194   finally show ?thesis .
  1221   finally show ?thesis .
  1195 qed
  1222 qed
  1196 
  1223 
  1197 
  1224 
  1198 subsection {* Embedding of the Naturals into any
  1225 subsection {* Embedding of the Naturals into any