src/HOL/Code_Numeral.thy
changeset 46547 d1dcb91a512e
parent 46028 9f113cdf3d66
child 46635 cde737f9c911
     1.1 --- a/src/HOL/Code_Numeral.thy	Sat Feb 18 10:35:45 2012 +0100
     1.2 +++ b/src/HOL/Code_Numeral.thy	Mon Feb 20 12:37:17 2012 +0100
     1.3 @@ -71,17 +71,17 @@
     1.4  
     1.5  end
     1.6  
     1.7 -definition [simp]:
     1.8 -  "Suc_code_numeral k = of_nat (Suc (nat_of k))"
     1.9 +definition Suc where [simp]:
    1.10 +  "Suc k = of_nat (Nat.Suc (nat_of k))"
    1.11  
    1.12 -rep_datatype "0 \<Colon> code_numeral" Suc_code_numeral
    1.13 +rep_datatype "0 \<Colon> code_numeral" Suc
    1.14  proof -
    1.15    fix P :: "code_numeral \<Rightarrow> bool"
    1.16    fix k :: code_numeral
    1.17    assume "P 0" then have init: "P (of_nat 0)" by simp
    1.18 -  assume "\<And>k. P k \<Longrightarrow> P (Suc_code_numeral k)"
    1.19 -    then have "\<And>n. P (of_nat n) \<Longrightarrow> P (Suc_code_numeral (of_nat n))" .
    1.20 -    then have step: "\<And>n. P (of_nat n) \<Longrightarrow> P (of_nat (Suc n))" by simp
    1.21 +  assume "\<And>k. P k \<Longrightarrow> P (Suc k)"
    1.22 +    then have "\<And>n. P (of_nat n) \<Longrightarrow> P (Suc (of_nat n))" .
    1.23 +    then have step: "\<And>n. P (of_nat n) \<Longrightarrow> P (of_nat (Nat.Suc n))" by simp
    1.24    from init step have "P (of_nat (nat_of k))"
    1.25      by (induct ("nat_of k")) simp_all
    1.26    then show "P k" by simp
    1.27 @@ -91,7 +91,7 @@
    1.28  declare code_numeral.induct [case_names nat, induct type: code_numeral]
    1.29  
    1.30  lemma code_numeral_decr [termination_simp]:
    1.31 -  "k \<noteq> of_nat 0 \<Longrightarrow> nat_of k - Suc 0 < nat_of k"
    1.32 +  "k \<noteq> of_nat 0 \<Longrightarrow> nat_of k - Nat.Suc 0 < nat_of k"
    1.33    by (cases k) simp
    1.34  
    1.35  lemma [simp, code]:
    1.36 @@ -99,7 +99,7 @@
    1.37  proof (rule ext)
    1.38    fix k
    1.39    have "code_numeral_size k = nat_size (nat_of k)"
    1.40 -    by (induct k rule: code_numeral.induct) (simp_all del: zero_code_numeral_def Suc_code_numeral_def, simp_all)
    1.41 +    by (induct k rule: code_numeral.induct) (simp_all del: zero_code_numeral_def Suc_def, simp_all)
    1.42    also have "nat_size (nat_of k) = nat_of k" by (induct ("nat_of k")) simp_all
    1.43    finally show "code_numeral_size k = nat_of k" .
    1.44  qed
    1.45 @@ -109,7 +109,7 @@
    1.46  proof (rule ext)
    1.47    fix k
    1.48    show "size k = nat_of k"
    1.49 -  by (induct k) (simp_all del: zero_code_numeral_def Suc_code_numeral_def, simp_all)
    1.50 +  by (induct k) (simp_all del: zero_code_numeral_def Suc_def, simp_all)
    1.51  qed
    1.52  
    1.53  lemmas [code del] = code_numeral.recs code_numeral.cases
    1.54 @@ -194,15 +194,15 @@
    1.55    "of_nat n + of_nat m = of_nat (n + m)"
    1.56    by simp
    1.57  
    1.58 -definition subtract_code_numeral :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" where
    1.59 -  [simp, code del]: "subtract_code_numeral = op -"
    1.60 +definition subtract :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" where
    1.61 +  [simp]: "subtract = minus"
    1.62  
    1.63 -lemma subtract_code_numeral_code [code nbe]:
    1.64 -  "subtract_code_numeral (of_nat n) (of_nat m) = of_nat (n - m)"
    1.65 +lemma subtract_code [code nbe]:
    1.66 +  "subtract (of_nat n) (of_nat m) = of_nat (n - m)"
    1.67    by simp
    1.68  
    1.69  lemma minus_code_numeral_code [code]:
    1.70 -  "n - m = subtract_code_numeral n m"
    1.71 +  "minus = subtract"
    1.72    by simp
    1.73  
    1.74  lemma times_code_numeral_code [code nbe]:
    1.75 @@ -222,7 +222,7 @@
    1.76    by simp
    1.77  
    1.78  lemma Suc_code_numeral_minus_one:
    1.79 -  "Suc_code_numeral n - 1 = n"
    1.80 +  "Suc n - 1 = n"
    1.81    by simp
    1.82  
    1.83  lemma of_nat_code [code]:
    1.84 @@ -242,27 +242,27 @@
    1.85    "nat_of_aux i n = nat_of i + n"
    1.86  
    1.87  lemma nat_of_aux_code [code]:
    1.88 -  "nat_of_aux i n = (if i = 0 then n else nat_of_aux (i - 1) (Suc n))"
    1.89 +  "nat_of_aux i n = (if i = 0 then n else nat_of_aux (i - 1) (Nat.Suc n))"
    1.90    by (auto simp add: nat_of_aux_def code_numeral_not_eq_zero)
    1.91  
    1.92  lemma nat_of_code [code]:
    1.93    "nat_of i = nat_of_aux i 0"
    1.94    by (simp add: nat_of_aux_def)
    1.95  
    1.96 -definition div_mod_code_numeral :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral \<times> code_numeral" where
    1.97 -  [code del]: "div_mod_code_numeral n m = (n div m, n mod m)"
    1.98 +definition div_mod :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral \<times> code_numeral" where
    1.99 +  [code del]: "div_mod n m = (n div m, n mod m)"
   1.100  
   1.101  lemma [code]:
   1.102 -  "div_mod_code_numeral n m = (if m = 0 then (0, n) else (n div m, n mod m))"
   1.103 -  unfolding div_mod_code_numeral_def by auto
   1.104 +  "div_mod n m = (if m = 0 then (0, n) else (n div m, n mod m))"
   1.105 +  unfolding div_mod_def by auto
   1.106  
   1.107  lemma [code]:
   1.108 -  "n div m = fst (div_mod_code_numeral n m)"
   1.109 -  unfolding div_mod_code_numeral_def by simp
   1.110 +  "n div m = fst (div_mod n m)"
   1.111 +  unfolding div_mod_def by simp
   1.112  
   1.113  lemma [code]:
   1.114 -  "n mod m = snd (div_mod_code_numeral n m)"
   1.115 -  unfolding div_mod_code_numeral_def by simp
   1.116 +  "n mod m = snd (div_mod n m)"
   1.117 +  unfolding div_mod_def by simp
   1.118  
   1.119  definition int_of :: "code_numeral \<Rightarrow> int" where
   1.120    "int_of = Nat.of_nat o nat_of"
   1.121 @@ -280,18 +280,20 @@
   1.122    then show ?thesis by (auto simp add: int_of_def mult_ac)
   1.123  qed
   1.124  
   1.125 -hide_const (open) of_nat nat_of int_of
   1.126  
   1.127 -subsubsection {* Lazy Evaluation of an indexed function *}
   1.128 +text {* Lazy Evaluation of an indexed function *}
   1.129  
   1.130 -function iterate_upto :: "(code_numeral => 'a) => code_numeral => code_numeral => 'a Predicate.pred"
   1.131 +function iterate_upto :: "(code_numeral \<Rightarrow> 'a) \<Rightarrow> code_numeral \<Rightarrow> code_numeral \<Rightarrow> 'a Predicate.pred"
   1.132  where
   1.133 -  "iterate_upto f n m = Predicate.Seq (%u. if n > m then Predicate.Empty else Predicate.Insert (f n) (iterate_upto f (n + 1) m))"
   1.134 +  "iterate_upto f n m =
   1.135 +    Predicate.Seq (%u. if n > m then Predicate.Empty
   1.136 +     else Predicate.Insert (f n) (iterate_upto f (n + 1) m))"
   1.137  by pat_completeness auto
   1.138  
   1.139  termination by (relation "measure (%(f, n, m). Code_Numeral.nat_of (m + 1 - n))") auto
   1.140  
   1.141 -hide_const (open) iterate_upto
   1.142 +hide_const (open) of_nat nat_of Suc  subtract int_of iterate_upto
   1.143 +
   1.144  
   1.145  subsection {* Code generator setup *}
   1.146  
   1.147 @@ -316,28 +318,28 @@
   1.148  code_reserved SML Int int
   1.149  code_reserved Eval Integer
   1.150  
   1.151 -code_const "op + \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   1.152 +code_const "plus \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   1.153    (SML "Int.+/ ((_),/ (_))")
   1.154    (OCaml "Big'_int.add'_big'_int")
   1.155    (Haskell infixl 6 "+")
   1.156    (Scala infixl 7 "+")
   1.157    (Eval infixl 8 "+")
   1.158  
   1.159 -code_const "subtract_code_numeral \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   1.160 +code_const "Code_Numeral.subtract \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   1.161    (SML "Int.max/ (_/ -/ _,/ 0 : int)")
   1.162    (OCaml "Big'_int.max'_big'_int/ (Big'_int.sub'_big'_int/ _/ _)/ Big'_int.zero'_big'_int")
   1.163    (Haskell "max/ (_/ -/ _)/ (0 :: Integer)")
   1.164    (Scala "!(_/ -/ _).max(0)")
   1.165    (Eval "Integer.max/ (_/ -/ _)/ 0")
   1.166  
   1.167 -code_const "op * \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   1.168 +code_const "times \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   1.169    (SML "Int.*/ ((_),/ (_))")
   1.170    (OCaml "Big'_int.mult'_big'_int")
   1.171    (Haskell infixl 7 "*")
   1.172    (Scala infixl 8 "*")
   1.173    (Eval infixl 8 "*")
   1.174  
   1.175 -code_const div_mod_code_numeral
   1.176 +code_const Code_Numeral.div_mod
   1.177    (SML "!(fn n => fn m =>/ if m = 0/ then (0, n) else/ (Int.div (n, m), Int.mod (n, m)))")
   1.178    (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
   1.179    (Haskell "divMod")
   1.180 @@ -351,18 +353,27 @@
   1.181    (Scala infixl 5 "==")
   1.182    (Eval "!((_ : int) = _)")
   1.183  
   1.184 -code_const "op \<le> \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
   1.185 +code_const "less_eq \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
   1.186    (SML "Int.<=/ ((_),/ (_))")
   1.187    (OCaml "Big'_int.le'_big'_int")
   1.188    (Haskell infix 4 "<=")
   1.189    (Scala infixl 4 "<=")
   1.190    (Eval infixl 6 "<=")
   1.191  
   1.192 -code_const "op < \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
   1.193 +code_const "less \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
   1.194    (SML "Int.</ ((_),/ (_))")
   1.195    (OCaml "Big'_int.lt'_big'_int")
   1.196    (Haskell infix 4 "<")
   1.197    (Scala infixl 4 "<")
   1.198    (Eval infixl 6 "<")
   1.199  
   1.200 +code_modulename SML
   1.201 +  Code_Numeral Arith
   1.202 +
   1.203 +code_modulename OCaml
   1.204 +  Code_Numeral Arith
   1.205 +
   1.206 +code_modulename Haskell
   1.207 +  Code_Numeral Arith
   1.208 +
   1.209  end