added computing oracle support for HOL and numerals
authorobua
Mon Jul 09 17:38:40 2007 +0200 (2007-07-09)
changeset 236649c486517354a
parent 23663 84b5c89b8b49
child 23665 825bea0266db
added computing oracle support for HOL and numerals
src/HOL/Tools/ComputeHOL.ML
src/HOL/Tools/ComputeHOL.thy
src/HOL/Tools/ComputeNumeral.thy
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Tools/ComputeHOL.ML	Mon Jul 09 17:38:40 2007 +0200
     1.3 @@ -0,0 +1,54 @@
     1.4 +signature ComputeHOL =
     1.5 +sig
     1.6 +  val prep_thms : thm list -> thm list
     1.7 +  val to_meta_eq : thm -> thm
     1.8 +  val to_hol_eq : thm -> thm
     1.9 +  val symmetric : thm -> thm 
    1.10 +  val trans : thm -> thm -> thm
    1.11 +end
    1.12 +
    1.13 +structure ComputeHOL : ComputeHOL =
    1.14 +struct
    1.15 +
    1.16 +fun all_prems_conv ci ct = Conv.prems_conv (Logic.count_prems (term_of ct)) ci ct
    1.17 +
    1.18 +local
    1.19 +fun lhs_of eq = fst (Thm.dest_equals (cprop_of eq));
    1.20 +in
    1.21 +fun rewrite_conv [] ct = raise CTERM ("rewrite_conv", [])
    1.22 +  | rewrite_conv (eq :: eqs) ct =
    1.23 +      Thm.instantiate (Thm.match (lhs_of eq, ct)) eq
    1.24 +      handle Pattern.MATCH => rewrite_conv eqs ct;
    1.25 +end
    1.26 +
    1.27 +val convert_conditions = Conv.fconv_rule (all_prems_conv (fn _ => Conv.else_conv (rewrite_conv [@{thm "Trueprop_eq_eq"}], Conv.all_conv)))
    1.28 +
    1.29 +val eq_th = @{thm "HOL.eq_reflection"}
    1.30 +val meta_eq_trivial = @{thm "ComputeHOL.meta_eq_trivial"}
    1.31 +val bool_to_true = @{thm "ComputeHOL.bool_to_true"}
    1.32 +
    1.33 +fun to_meta_eq th = eq_th OF [th] handle THM _ => meta_eq_trivial OF [th] handle THM _ => bool_to_true OF [th]
    1.34 +
    1.35 +fun to_hol_eq th = @{thm "meta_eq_imp_eq"} OF [th] handle THM _ => @{thm "eq_trivial"} OF [th] 
    1.36 +
    1.37 +fun prep_thms ths = map (convert_conditions o to_meta_eq) ths
    1.38 +
    1.39 +local
    1.40 +    val sym_HOL = @{thm "HOL.sym"}
    1.41 +    val sym_Pure = @{thm "ProtoPure.symmetric"}
    1.42 +in
    1.43 +  fun symmetric th = ((sym_HOL OF [th]) handle THM _ => (sym_Pure OF [th]))
    1.44 +end
    1.45 +
    1.46 +local
    1.47 +    val trans_HOL = @{thm "HOL.trans"}
    1.48 +    val trans_HOL_1 = @{thm "ComputeHOL.transmeta_1"}
    1.49 +    val trans_HOL_2 = @{thm "ComputeHOL.transmeta_2"}
    1.50 +    val trans_HOL_3 = @{thm "ComputeHOL.transmeta_3"}
    1.51 +    fun tr [] th1 th2 = trans_HOL OF [th1, th2]
    1.52 +      | tr (t::ts) th1 th2 = (t OF [th1, th2] handle THM _ => tr ts th1 th2) 
    1.53 +in
    1.54 +  fun trans th1 th2 = tr [trans_HOL, trans_HOL_1, trans_HOL_2, trans_HOL_3] th1 th2
    1.55 +end
    1.56 +
    1.57 +end
    1.58 \ No newline at end of file
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/HOL/Tools/ComputeHOL.thy	Mon Jul 09 17:38:40 2007 +0200
     2.3 @@ -0,0 +1,141 @@
     2.4 +theory ComputeHOL 
     2.5 +imports Main "~~/src/Tools/Compute_Oracle/Compute_Oracle"
     2.6 +begin
     2.7 +
     2.8 +lemma Trueprop_eq_eq: "Trueprop X == (X == True)" by (simp add: atomize_eq)
     2.9 +lemma meta_eq_trivial: "x == y \<Longrightarrow> x == y" by simp
    2.10 +lemma meta_eq_imp_eq: "x == y \<Longrightarrow> x = y" by auto
    2.11 +lemma eq_trivial: "x = y \<Longrightarrow> x = y" by auto
    2.12 +lemma bool_to_true: "x :: bool \<Longrightarrow> x == True"  by simp
    2.13 +lemma transmeta_1: "x = y \<Longrightarrow> y == z \<Longrightarrow> x = z" by simp
    2.14 +lemma transmeta_2: "x == y \<Longrightarrow> y = z \<Longrightarrow> x = z" by simp
    2.15 +lemma transmeta_3: "x == y \<Longrightarrow> y == z \<Longrightarrow> x = z" by simp
    2.16 +
    2.17 +
    2.18 +(**** compute_if ****)
    2.19 +
    2.20 +lemma If_True: "If True = (\<lambda> x y. x)" by ((rule ext)+,auto)
    2.21 +lemma If_False: "If False = (\<lambda> x y. y)" by ((rule ext)+, auto)
    2.22 +
    2.23 +lemmas compute_if = If_True If_False
    2.24 +
    2.25 +(**** compute_bool ****)
    2.26 +
    2.27 +lemma bool1: "(\<not> True) = False"  by blast
    2.28 +lemma bool2: "(\<not> False) = True"  by blast
    2.29 +lemma bool3: "(P \<and> True) = P" by blast
    2.30 +lemma bool4: "(True \<and> P) = P" by blast
    2.31 +lemma bool5: "(P \<and> False) = False" by blast
    2.32 +lemma bool6: "(False \<and> P) = False" by blast
    2.33 +lemma bool7: "(P \<or> True) = True" by blast
    2.34 +lemma bool8: "(True \<or> P) = True" by blast
    2.35 +lemma bool9: "(P \<or> False) = P" by blast
    2.36 +lemma bool10: "(False \<or> P) = P" by blast
    2.37 +lemma bool11: "(True \<longrightarrow> P) = P" by blast
    2.38 +lemma bool12: "(P \<longrightarrow> True) = True" by blast
    2.39 +lemma bool13: "(True \<longrightarrow> P) = P" by blast
    2.40 +lemma bool14: "(P \<longrightarrow> False) = (\<not> P)" by blast
    2.41 +lemma bool15: "(False \<longrightarrow> P) = True" by blast
    2.42 +lemma bool16: "(False = False) = True" by blast
    2.43 +lemma bool17: "(True = True) = True" by blast
    2.44 +lemma bool18: "(False = True) = False" by blast
    2.45 +lemma bool19: "(True = False) = False" by blast
    2.46 +
    2.47 +lemmas compute_bool = bool1 bool2 bool3 bool4 bool5 bool6 bool7 bool8 bool9 bool10 bool11 bool12 bool13 bool14 bool15 bool16 bool17 bool18 bool19
    2.48 +
    2.49 +
    2.50 +(*** compute_pair ***)
    2.51 +
    2.52 +lemma compute_fst: "fst (x,y) = x" by simp
    2.53 +lemma compute_snd: "snd (x,y) = y" by simp
    2.54 +lemma compute_pair_eq: "((a, b) = (c, d)) = (a = c \<and> b = d)" by auto
    2.55 +
    2.56 +lemma prod_case_simp: "prod_case f (x,y) = f x y" by simp
    2.57 +
    2.58 +lemmas compute_pair = compute_fst compute_snd compute_pair_eq prod_case_simp
    2.59 +
    2.60 +(*** compute_option ***)
    2.61 +
    2.62 +lemma compute_the: "the (Some x) = x" by simp
    2.63 +lemma compute_None_Some_eq: "(None = Some x) = False" by auto
    2.64 +lemma compute_Some_None_eq: "(Some x = None) = False" by auto
    2.65 +lemma compute_None_None_eq: "(None = None) = True" by auto
    2.66 +lemma compute_Some_Some_eq: "(Some x = Some y) = (x = y)" by auto
    2.67 +
    2.68 +definition
    2.69 +   option_case_compute :: "'b option \<Rightarrow> 'a \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'a"
    2.70 +where
    2.71 +   "option_case_compute opt a f = option_case a f opt"
    2.72 +
    2.73 +lemma option_case_compute: "option_case = (\<lambda> a f opt. option_case_compute opt a f)"
    2.74 +  by (simp add: option_case_compute_def)
    2.75 +
    2.76 +lemma option_case_compute_None: "option_case_compute None = (\<lambda> a f. a)"
    2.77 +  apply (rule ext)+
    2.78 +  apply (simp add: option_case_compute_def)
    2.79 +  done
    2.80 +
    2.81 +lemma option_case_compute_Some: "option_case_compute (Some x) = (\<lambda> a f. f x)"
    2.82 +  apply (rule ext)+
    2.83 +  apply (simp add: option_case_compute_def)
    2.84 +  done
    2.85 +
    2.86 +lemmas compute_option_case = option_case_compute option_case_compute_None option_case_compute_Some
    2.87 +
    2.88 +lemmas compute_option = compute_the compute_None_Some_eq compute_Some_None_eq compute_None_None_eq compute_Some_Some_eq compute_option_case
    2.89 +
    2.90 +(**** compute_list_length ****)
    2.91 +
    2.92 +lemma length_cons:"length (x#xs) = 1 + (length xs)"
    2.93 +  by simp
    2.94 +
    2.95 +lemma length_nil: "length [] = 0"
    2.96 +  by simp
    2.97 +
    2.98 +lemmas compute_list_length = length_nil length_cons
    2.99 +
   2.100 +(*** compute_list_case ***)
   2.101 +
   2.102 +definition
   2.103 +  list_case_compute :: "'b list \<Rightarrow> 'a \<Rightarrow> ('b \<Rightarrow> 'b list \<Rightarrow> 'a) \<Rightarrow> 'a"
   2.104 +where
   2.105 +  "list_case_compute l a f = list_case a f l"
   2.106 +
   2.107 +lemma list_case_compute: "list_case = (\<lambda> (a::'a) f (l::'b list). list_case_compute l a f)"
   2.108 +  apply (rule ext)+
   2.109 +  apply (simp add: list_case_compute_def)
   2.110 +  done
   2.111 +
   2.112 +lemma list_case_compute_empty: "list_case_compute ([]::'b list) = (\<lambda> (a::'a) f. a)"
   2.113 +  apply (rule ext)+
   2.114 +  apply (simp add: list_case_compute_def)
   2.115 +  done
   2.116 +
   2.117 +lemma list_case_compute_cons: "list_case_compute (u#v) = (\<lambda> (a::'a) f. (f (u::'b) v))"
   2.118 +  apply (rule ext)+
   2.119 +  apply (simp add: list_case_compute_def)
   2.120 +  done
   2.121 +
   2.122 +lemmas compute_list_case = list_case_compute list_case_compute_empty list_case_compute_cons
   2.123 +
   2.124 +(*** compute_list_nth ***)
   2.125 +(* Of course, you will need computation with nats for this to work \<dots> *)
   2.126 +
   2.127 +lemma compute_list_nth: "((x#xs) ! n) = (if n = 0 then x else (xs ! (n - 1)))"
   2.128 +  by (cases n, auto)
   2.129 +  
   2.130 +(*** compute_list ***)
   2.131 +
   2.132 +lemmas compute_list = compute_list_case compute_list_length compute_list_nth
   2.133 +
   2.134 +(*** compute_let ***)
   2.135 +
   2.136 +lemmas compute_let = Let_def
   2.137 +
   2.138 +(***********************)
   2.139 +(* Everything together *)
   2.140 +(***********************)
   2.141 +
   2.142 +lemmas compute_hol = compute_if compute_bool compute_pair compute_option compute_list compute_let
   2.143 +
   2.144 +end
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/HOL/Tools/ComputeNumeral.thy	Mon Jul 09 17:38:40 2007 +0200
     3.3 @@ -0,0 +1,250 @@
     3.4 +theory ComputeNumeral
     3.5 +imports ComputeHOL Float
     3.6 +begin
     3.7 +
     3.8 +(* normalization of bit strings *)
     3.9 +lemmas bitnorm = Pls_0_eq Min_1_eq
    3.10 +
    3.11 +(* neg for bit strings *)
    3.12 +lemma neg1: "neg Numeral.Pls = False" by (simp add: Numeral.Pls_def)
    3.13 +lemma neg2: "neg Numeral.Min = True" apply (subst Numeral.Min_def) by auto
    3.14 +lemma neg3: "neg (x BIT Numeral.B0) = neg x" apply (simp add: neg_def) apply (subst Bit_def) by auto
    3.15 +lemma neg4: "neg (x BIT Numeral.B1) = neg x" apply (simp add: neg_def) apply (subst Bit_def) by auto  
    3.16 +lemmas bitneg = neg1 neg2 neg3 neg4
    3.17 +
    3.18 +(* iszero for bit strings *)
    3.19 +lemma iszero1: "iszero Numeral.Pls = True" by (simp add: Numeral.Pls_def iszero_def)
    3.20 +lemma iszero2: "iszero Numeral.Min = False" apply (subst Numeral.Min_def) apply (subst iszero_def) by simp
    3.21 +lemma iszero3: "iszero (x BIT Numeral.B0) = iszero x" apply (subst Numeral.Bit_def) apply (subst iszero_def)+ by auto
    3.22 +lemma iszero4: "iszero (x BIT Numeral.B1) = False" apply (subst Numeral.Bit_def) apply (subst iszero_def)+  apply simp by arith
    3.23 +lemmas bitiszero = iszero1 iszero2 iszero3 iszero4
    3.24 +
    3.25 +(* lezero for bit strings *)
    3.26 +constdefs
    3.27 +  "lezero x == (x \<le> 0)"
    3.28 +lemma lezero1: "lezero Numeral.Pls = True" unfolding Numeral.Pls_def lezero_def by auto
    3.29 +lemma lezero2: "lezero Numeral.Min = True" unfolding Numeral.Min_def lezero_def by auto
    3.30 +lemma lezero3: "lezero (x BIT Numeral.B0) = lezero x" unfolding Numeral.Bit_def lezero_def by auto
    3.31 +lemma lezero4: "lezero (x BIT Numeral.B1) = neg x" unfolding Numeral.Bit_def lezero_def neg_def by auto
    3.32 +lemmas bitlezero = lezero1 lezero2 lezero3 lezero4
    3.33 +
    3.34 +(* equality for bit strings *)
    3.35 +lemma biteq1: "(Numeral.Pls = Numeral.Pls) = True" by auto
    3.36 +lemma biteq2: "(Numeral.Min = Numeral.Min) = True" by auto
    3.37 +lemma biteq3: "(Numeral.Pls = Numeral.Min) = False" unfolding Pls_def Min_def by auto
    3.38 +lemma biteq4: "(Numeral.Min = Numeral.Pls) = False" unfolding Pls_def Min_def by auto
    3.39 +lemma biteq5: "(x BIT Numeral.B0 = y BIT Numeral.B0) = (x = y)" unfolding Bit_def by auto
    3.40 +lemma biteq6: "(x BIT Numeral.B1 = y BIT Numeral.B1) = (x = y)" unfolding Bit_def by auto
    3.41 +lemma biteq7: "(x BIT Numeral.B0 = y BIT Numeral.B1) = False" unfolding Bit_def by (simp, arith) 
    3.42 +lemma biteq8: "(x BIT Numeral.B1 = y BIT Numeral.B0) = False" unfolding Bit_def by (simp, arith)
    3.43 +lemma biteq9: "(Numeral.Pls = x BIT Numeral.B0) = (Numeral.Pls = x)" unfolding Bit_def Pls_def by auto
    3.44 +lemma biteq10: "(Numeral.Pls = x BIT Numeral.B1) = False" unfolding Bit_def Pls_def by (simp, arith) 
    3.45 +lemma biteq11: "(Numeral.Min = x BIT Numeral.B0) = False" unfolding Bit_def Min_def by (simp, arith)
    3.46 +lemma biteq12: "(Numeral.Min = x BIT Numeral.B1) = (Numeral.Min = x)" unfolding Bit_def Min_def by auto
    3.47 +lemma biteq13: "(x BIT Numeral.B0 = Numeral.Pls) = (x = Numeral.Pls)" unfolding Bit_def Pls_def by auto
    3.48 +lemma biteq14: "(x BIT Numeral.B1 = Numeral.Pls) = False" unfolding Bit_def Pls_def by (simp, arith)
    3.49 +lemma biteq15: "(x BIT Numeral.B0 = Numeral.Min) = False" unfolding Bit_def Pls_def Min_def by (simp, arith)
    3.50 +lemma biteq16: "(x BIT Numeral.B1 = Numeral.Min) = (x = Numeral.Min)" unfolding Bit_def Min_def by (simp, arith)
    3.51 +lemmas biteq = biteq1 biteq2 biteq3 biteq4 biteq5 biteq6 biteq7 biteq8 biteq9 biteq10 biteq11 biteq12 biteq13 biteq14 biteq15 biteq16
    3.52 +
    3.53 +(* x < y for bit strings *)
    3.54 +lemma bitless1: "(Numeral.Pls < Numeral.Min) = False" unfolding Pls_def Min_def by auto
    3.55 +lemma bitless2: "(Numeral.Pls < Numeral.Pls) = False" by auto
    3.56 +lemma bitless3: "(Numeral.Min < Numeral.Pls) = True" unfolding Pls_def Min_def by auto
    3.57 +lemma bitless4: "(Numeral.Min < Numeral.Min) = False" unfolding Pls_def Min_def by auto
    3.58 +lemma bitless5: "(x BIT Numeral.B0 < y BIT Numeral.B0) = (x < y)" unfolding Bit_def by auto
    3.59 +lemma bitless6: "(x BIT Numeral.B1 < y BIT Numeral.B1) = (x < y)" unfolding Bit_def by auto
    3.60 +lemma bitless7: "(x BIT Numeral.B0 < y BIT Numeral.B1) = (x \<le> y)" unfolding Bit_def by auto
    3.61 +lemma bitless8: "(x BIT Numeral.B1 < y BIT Numeral.B0) = (x < y)" unfolding Bit_def by auto
    3.62 +lemma bitless9: "(Numeral.Pls < x BIT Numeral.B0) = (Numeral.Pls < x)" unfolding Bit_def Pls_def by auto
    3.63 +lemma bitless10: "(Numeral.Pls < x BIT Numeral.B1) = (Numeral.Pls \<le> x)" unfolding Bit_def Pls_def by auto
    3.64 +lemma bitless11: "(Numeral.Min < x BIT Numeral.B0) = (Numeral.Pls \<le> x)" unfolding Bit_def Pls_def Min_def by auto
    3.65 +lemma bitless12: "(Numeral.Min < x BIT Numeral.B1) = (Numeral.Min < x)" unfolding Bit_def Min_def by auto
    3.66 +lemma bitless13: "(x BIT Numeral.B0 < Numeral.Pls) = (x < Numeral.Pls)" unfolding Bit_def Pls_def by auto
    3.67 +lemma bitless14: "(x BIT Numeral.B1 < Numeral.Pls) = (x < Numeral.Pls)" unfolding Bit_def Pls_def by auto
    3.68 +lemma bitless15: "(x BIT Numeral.B0 < Numeral.Min) = (x < Numeral.Pls)" unfolding Bit_def Pls_def Min_def by auto
    3.69 +lemma bitless16: "(x BIT Numeral.B1 < Numeral.Min) = (x < Numeral.Min)" unfolding Bit_def Min_def by auto
    3.70 +lemmas bitless = bitless1 bitless2 bitless3 bitless4 bitless5 bitless6 bitless7 bitless8 
    3.71 +                 bitless9 bitless10 bitless11 bitless12 bitless13 bitless14 bitless15 bitless16
    3.72 +
    3.73 +(* x \<le> y for bit strings *)
    3.74 +lemma bitle1: "(Numeral.Pls \<le> Numeral.Min) = False" unfolding Pls_def Min_def by auto
    3.75 +lemma bitle2: "(Numeral.Pls \<le> Numeral.Pls) = True" by auto
    3.76 +lemma bitle3: "(Numeral.Min \<le> Numeral.Pls) = True" unfolding Pls_def Min_def by auto
    3.77 +lemma bitle4: "(Numeral.Min \<le> Numeral.Min) = True" unfolding Pls_def Min_def by auto
    3.78 +lemma bitle5: "(x BIT Numeral.B0 \<le> y BIT Numeral.B0) = (x \<le> y)" unfolding Bit_def by auto
    3.79 +lemma bitle6: "(x BIT Numeral.B1 \<le> y BIT Numeral.B1) = (x \<le> y)" unfolding Bit_def by auto
    3.80 +lemma bitle7: "(x BIT Numeral.B0 \<le> y BIT Numeral.B1) = (x \<le> y)" unfolding Bit_def by auto
    3.81 +lemma bitle8: "(x BIT Numeral.B1 \<le> y BIT Numeral.B0) = (x < y)" unfolding Bit_def by auto
    3.82 +lemma bitle9: "(Numeral.Pls \<le> x BIT Numeral.B0) = (Numeral.Pls \<le> x)" unfolding Bit_def Pls_def by auto
    3.83 +lemma bitle10: "(Numeral.Pls \<le> x BIT Numeral.B1) = (Numeral.Pls \<le> x)" unfolding Bit_def Pls_def by auto
    3.84 +lemma bitle11: "(Numeral.Min \<le> x BIT Numeral.B0) = (Numeral.Pls \<le> x)" unfolding Bit_def Pls_def Min_def by auto
    3.85 +lemma bitle12: "(Numeral.Min \<le> x BIT Numeral.B1) = (Numeral.Min \<le> x)" unfolding Bit_def Min_def by auto
    3.86 +lemma bitle13: "(x BIT Numeral.B0 \<le> Numeral.Pls) = (x \<le> Numeral.Pls)" unfolding Bit_def Pls_def by auto
    3.87 +lemma bitle14: "(x BIT Numeral.B1 \<le> Numeral.Pls) = (x < Numeral.Pls)" unfolding Bit_def Pls_def by auto
    3.88 +lemma bitle15: "(x BIT Numeral.B0 \<le> Numeral.Min) = (x < Numeral.Pls)" unfolding Bit_def Pls_def Min_def by auto
    3.89 +lemma bitle16: "(x BIT Numeral.B1 \<le> Numeral.Min) = (x \<le> Numeral.Min)" unfolding Bit_def Min_def by auto
    3.90 +lemmas bitle = bitle1 bitle2 bitle3 bitle4 bitle5 bitle6 bitle7 bitle8 
    3.91 +                 bitle9 bitle10 bitle11 bitle12 bitle13 bitle14 bitle15 bitle16
    3.92 +
    3.93 +(* succ for bit strings *)
    3.94 +lemmas bitsucc = succ_Pls succ_Min succ_1 succ_0
    3.95 +
    3.96 +(* pred for bit strings *)
    3.97 +lemmas bitpred = pred_Pls pred_Min pred_1 pred_0
    3.98 +
    3.99 +(* unary minus for bit strings *)
   3.100 +lemmas bituminus = minus_Pls minus_Min minus_1 minus_0 
   3.101 +
   3.102 +(* addition for bit strings *)
   3.103 +lemmas bitadd = add_Pls add_Pls_right add_Min add_Min_right add_BIT_11 add_BIT_10 add_BIT_0[where b="Numeral.B0"] add_BIT_0[where b="Numeral.B1"]
   3.104 +
   3.105 +(* multiplication for bit strings *) 
   3.106 +lemma mult_Pls_right: "x * Numeral.Pls = Numeral.Pls" by (simp add: Pls_def)
   3.107 +lemma mult_Min_right: "x * Numeral.Min = - x" by (subst mult_commute, simp add: mult_Min)
   3.108 +lemma multb0x: "(x BIT Numeral.B0) * y = (x * y) BIT Numeral.B0" unfolding Bit_def by simp
   3.109 +lemma multxb0: "x * (y BIT Numeral.B0) = (x * y) BIT Numeral.B0" unfolding Bit_def by simp
   3.110 +lemma multb1: "(x BIT Numeral.B1) * (y BIT Numeral.B1) = (((x * y) BIT Numeral.B0) + x + y) BIT Numeral.B1"
   3.111 +  unfolding Bit_def by (simp add: ring_simps)
   3.112 +lemmas bitmul = mult_Pls mult_Min mult_Pls_right mult_Min_right multb0x multxb0 multb1
   3.113 +
   3.114 +lemmas bitarith = bitnorm bitiszero bitneg bitlezero biteq bitless bitle bitsucc bitpred bituminus bitadd bitmul 
   3.115 +
   3.116 +constdefs 
   3.117 +  "nat_norm_number_of (x::nat) == x"
   3.118 +
   3.119 +lemma nat_norm_number_of: "nat_norm_number_of (number_of w) = (if lezero w then 0 else number_of w)"
   3.120 +  apply (simp add: nat_norm_number_of_def)
   3.121 +  unfolding lezero_def iszero_def neg_def
   3.122 +  apply (simp add: number_of_is_id)
   3.123 +  done
   3.124 +
   3.125 +(* Normalization of nat literals *)
   3.126 +lemma natnorm0: "(0::nat) = number_of (Numeral.Pls)" by auto
   3.127 +lemma natnorm1: "(1 :: nat) = number_of (Numeral.Pls BIT Numeral.B1)"  by auto 
   3.128 +lemmas natnorm = natnorm0 natnorm1 nat_norm_number_of
   3.129 +
   3.130 +(* Suc *)
   3.131 +lemma natsuc: "Suc (number_of x) = (if neg x then 1 else number_of (Numeral.succ x))" by (auto simp add: number_of_is_id)
   3.132 +
   3.133 +(* Addition for nat *)
   3.134 +lemma natadd: "number_of x + ((number_of y)::nat) = (if neg x then (number_of y) else (if neg y then number_of x else (number_of (x + y))))"
   3.135 +  by (auto simp add: number_of_is_id)
   3.136 +
   3.137 +(* Subtraction for nat *)
   3.138 +lemma natsub: "(number_of x) - ((number_of y)::nat) = 
   3.139 +  (if neg x then 0 else (if neg y then number_of x else (nat_norm_number_of (number_of (x + (- y))))))"
   3.140 +  unfolding nat_norm_number_of
   3.141 +  by (auto simp add: number_of_is_id neg_def lezero_def iszero_def Let_def nat_number_of_def)
   3.142 +
   3.143 +(* Multiplication for nat *)
   3.144 +lemma natmul: "(number_of x) * ((number_of y)::nat) = 
   3.145 +  (if neg x then 0 else (if neg y then 0 else number_of (x * y)))"
   3.146 +  apply (auto simp add: number_of_is_id neg_def iszero_def)
   3.147 +  apply (case_tac "x > 0")
   3.148 +  apply auto
   3.149 +  apply (simp add: mult_strict_left_mono[where a=y and b=0 and c=x, simplified])
   3.150 +  done
   3.151 +
   3.152 +lemma nateq: "(((number_of x)::nat) = (number_of y)) = ((lezero x \<and> lezero y) \<or> (x = y))"
   3.153 +  by (auto simp add: iszero_def lezero_def neg_def number_of_is_id)
   3.154 +
   3.155 +lemma natless: "(((number_of x)::nat) < (number_of y)) = ((x < y) \<and> (\<not> (lezero y)))"
   3.156 +  by (auto simp add: number_of_is_id neg_def lezero_def)
   3.157 +
   3.158 +lemma natle: "(((number_of x)::nat) \<le> (number_of y)) = (y < x \<longrightarrow> lezero x)"
   3.159 +  by (auto simp add: number_of_is_id lezero_def nat_number_of_def)
   3.160 +
   3.161 +fun natfac :: "nat \<Rightarrow> nat"
   3.162 +where
   3.163 +   "natfac n = (if n = 0 then 1 else n * (natfac (n - 1)))"
   3.164 +
   3.165 +lemmas compute_natarith = bitarith natnorm natsuc natadd natsub natmul nateq natless natle natfac.simps
   3.166 +
   3.167 +lemma number_eq: "(((number_of x)::'a::{number_ring, ordered_idom}) = (number_of y)) = (x = y)"
   3.168 +  unfolding number_of_eq
   3.169 +  apply simp
   3.170 +  done
   3.171 +
   3.172 +lemma number_le: "(((number_of x)::'a::{number_ring, ordered_idom}) \<le>  (number_of y)) = (x \<le> y)"
   3.173 +  unfolding number_of_eq
   3.174 +  apply simp
   3.175 +  done
   3.176 +
   3.177 +lemma number_less: "(((number_of x)::'a::{number_ring, ordered_idom}) <  (number_of y)) = (x < y)"
   3.178 +  unfolding number_of_eq 
   3.179 +  apply simp
   3.180 +  done
   3.181 +
   3.182 +lemma number_diff: "((number_of x)::'a::{number_ring, ordered_idom}) - number_of y = number_of (x + (- y))"
   3.183 +  apply (subst diff_number_of_eq)
   3.184 +  apply simp
   3.185 +  done
   3.186 +
   3.187 +lemmas number_norm = number_of_Pls[symmetric] numeral_1_eq_1[symmetric]
   3.188 +
   3.189 +lemmas compute_numberarith = number_of_minus[symmetric] number_of_add[symmetric] number_diff number_of_mult[symmetric] number_norm number_eq number_le number_less
   3.190 +
   3.191 +lemma compute_real_of_nat_number_of: "real ((number_of v)::nat) = (if neg v then 0 else number_of v)"
   3.192 +  by (simp only: real_of_nat_number_of number_of_is_id)
   3.193 +
   3.194 +lemma compute_nat_of_int_number_of: "nat ((number_of v)::int) = (number_of v)"
   3.195 +  by simp
   3.196 +
   3.197 +lemmas compute_num_conversions = compute_real_of_nat_number_of compute_nat_of_int_number_of real_number_of
   3.198 +
   3.199 +lemmas zpowerarith = zpower_number_of_even
   3.200 +  zpower_number_of_odd[simplified zero_eq_Numeral0_nring one_eq_Numeral1_nring]
   3.201 +  zpower_Pls zpower_Min
   3.202 +
   3.203 +(* div, mod *)
   3.204 +
   3.205 +lemma adjust: "adjust b (q, r) = (if 0 \<le> r - b then (2 * q + 1, r - b) else (2 * q, r))"
   3.206 +  by (auto simp only: adjust_def)
   3.207 +
   3.208 +lemma negateSnd: "negateSnd (q, r) = (q, -r)" 
   3.209 +  by (auto simp only: negateSnd_def)
   3.210 +
   3.211 +lemma divAlg: "divAlg (a, b) = (if 0\<le>a then
   3.212 +                  if 0\<le>b then posDivAlg a b
   3.213 +                  else if a=0 then (0, 0)
   3.214 +                       else negateSnd (negDivAlg (-a) (-b))
   3.215 +               else 
   3.216 +                  if 0<b then negDivAlg a b
   3.217 +                  else negateSnd (posDivAlg (-a) (-b)))"
   3.218 +  by (auto simp only: divAlg_def)
   3.219 +
   3.220 +lemmas compute_div_mod = div_def mod_def divAlg adjust negateSnd posDivAlg.simps negDivAlg.simps
   3.221 +
   3.222 +
   3.223 +
   3.224 +(* collecting all the theorems *)
   3.225 +
   3.226 +lemma even_Pls: "even (Numeral.Pls) = True"
   3.227 +  apply (unfold Pls_def even_def)
   3.228 +  by simp
   3.229 +
   3.230 +lemma even_Min: "even (Numeral.Min) = False"
   3.231 +  apply (unfold Min_def even_def)
   3.232 +  by simp
   3.233 +
   3.234 +lemma even_B0: "even (x BIT Numeral.B0) = True"
   3.235 +  apply (unfold Bit_def)
   3.236 +  by simp
   3.237 +
   3.238 +lemma even_B1: "even (x BIT Numeral.B1) = False"
   3.239 +  apply (unfold Bit_def)
   3.240 +  by simp
   3.241 +
   3.242 +lemma even_number_of: "even ((number_of w)::int) = even w"
   3.243 +  by (simp only: number_of_is_id)
   3.244 +
   3.245 +lemmas compute_even = even_Pls even_Min even_B0 even_B1 even_number_of
   3.246 +
   3.247 +lemmas compute_numeral = compute_if compute_let compute_pair compute_bool 
   3.248 +                         compute_natarith compute_numberarith max_def min_def compute_num_conversions zpowerarith compute_div_mod compute_even
   3.249 +
   3.250 +end
   3.251 +
   3.252 +
   3.253 +