src/HOL/Library/EfficientNat.thy
changeset 20839 ed49d8709959
parent 20713 823967ef47f1
child 20951 868120282837
equal deleted inserted replaced
20838:e115ea078a30 20839:ed49d8709959
    26 
    26 
    27 definition
    27 definition
    28   nat_of_int :: "int \<Rightarrow> nat"
    28   nat_of_int :: "int \<Rightarrow> nat"
    29   "k \<ge> 0 \<Longrightarrow> nat_of_int k = nat k"
    29   "k \<ge> 0 \<Longrightarrow> nat_of_int k = nat k"
    30 
    30 
       
    31 lemma nat_of_int_int:
       
    32   "nat_of_int (int n) = n"
       
    33   using zero_zle_int nat_of_int_def by simp
       
    34 
    31 code_constname
    35 code_constname
    32   nat_of_int "IntDef.nat_of_int"
    36   nat_of_int "IntDef.nat_of_int"
    33 
    37 
    34 text {*
    38 text {*
    35   Case analysis on natural numbers is rephrased using a conditional
    39   Case analysis on natural numbers is rephrased using a conditional
    36   expression:
    40   expression:
    37 *}
    41 *}
    38 
    42 
    39 lemma [code unfold]: "nat_case \<equiv> (\<lambda>f g n. if n = 0 then f else g (n - 1))"
    43 lemma [code unfold, code noinline]: "nat_case \<equiv> (\<lambda>f g n. if n = 0 then f else g (n - 1))"
    40 proof -
    44 proof -
    41   have rewrite: "\<And>f g n. nat_case f g n = (if n = 0 then f else g (n - 1))"
    45   have rewrite: "\<And>f g n. nat_case f g n = (if n = 0 then f else g (n - 1))"
    42   proof -
    46   proof -
    43     fix f g n
    47     fix f g n
    44     show "nat_case f g n = (if n = 0 then f else g (n - 1))"
    48     show "nat_case f g n = (if n = 0 then f else g (n - 1))"
    45       by (cases n) simp_all
    49       by (cases n) simp_all
    46   qed
    50   qed
    47   show "nat_case \<equiv> (\<lambda>f g n. if n = 0 then f else g (n - 1))"
    51   show "nat_case \<equiv> (\<lambda>f g n. if n = 0 then f else g (n - 1))"
    48     by (rule eq_reflection ext rewrite)+ 
    52     by (rule eq_reflection ext rewrite)+ 
    49 qed
    53 qed
       
    54 
       
    55 lemma [code inline]:
       
    56   "nat_case f g n = (if OperationalEquality.eq n 0 then f else g (nat_of_int (int n - 1)))"
       
    57   by (cases n) (simp_all add: eq_def nat_of_int_int)
    50 
    58 
    51 text {*
    59 text {*
    52   Most standard arithmetic functions on natural numbers are implemented
    60   Most standard arithmetic functions on natural numbers are implemented
    53   using their counterparts on the integers:
    61   using their counterparts on the integers:
    54 *}
    62 *}
   118 
   126 
   119 text {*
   127 text {*
   120   @{typ nat} is no longer a datatype but embedded into the integers.
   128   @{typ nat} is no longer a datatype but embedded into the integers.
   121 *}
   129 *}
   122 
   130 
       
   131 code_const "0::nat"
       
   132   (SML target_atom "(0 : IntInf.int)")
       
   133   (Haskell target_atom "0")
       
   134 
       
   135 code_const "Suc"
       
   136   (SML "IntInf.op + (__ + 1)")
       
   137   (Haskell "(__ + 1)")
       
   138 
   123 setup {*
   139 setup {*
   124   CodegenData.del_datatype "nat"
   140   CodegenData.del_datatype "nat"
   125 *}
   141 *}
   126 
   142 
   127 types_code
   143 types_code