src/HOL/Code_Numeral.thy
author wenzelm
Sat, 09 Jul 2011 21:53:27 +0200
changeset 43721 fad8634cee62
parent 39818 ff9e9d5ac171
child 44821 a92f65e174cf
permissions -rw-r--r--
echo prover input via raw_messages, for improved protocol tracing;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
     1
(* Author: Florian Haftmann, TU Muenchen *)
24999
haftmann
parents:
diff changeset
     2
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
     3
header {* Type of target language numerals *}
24999
haftmann
parents:
diff changeset
     4
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
     5
theory Code_Numeral
33318
ddd97d9dfbfb moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents: 33296
diff changeset
     6
imports Nat_Numeral Nat_Transfer Divides
24999
haftmann
parents:
diff changeset
     7
begin
haftmann
parents:
diff changeset
     8
haftmann
parents:
diff changeset
     9
text {*
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    10
  Code numerals are isomorphic to HOL @{typ nat} but
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    11
  mapped to target-language builtin numerals.
24999
haftmann
parents:
diff changeset
    12
*}
haftmann
parents:
diff changeset
    13
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    14
subsection {* Datatype of target language numerals *}
24999
haftmann
parents:
diff changeset
    15
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    16
typedef (open) code_numeral = "UNIV \<Colon> nat set"
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    17
  morphisms nat_of of_nat by rule
24999
haftmann
parents:
diff changeset
    18
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    19
lemma of_nat_nat_of [simp]:
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    20
  "of_nat (nat_of k) = k"
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    21
  by (rule nat_of_inverse)
24999
haftmann
parents:
diff changeset
    22
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    23
lemma nat_of_of_nat [simp]:
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    24
  "nat_of (of_nat n) = n"
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    25
  by (rule of_nat_inverse) (rule UNIV_I)
24999
haftmann
parents:
diff changeset
    26
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
    27
lemma [measure_function]:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    28
  "is_measure nat_of" by (rule is_measure_trivial)
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
    29
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    30
lemma code_numeral:
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    31
  "(\<And>n\<Colon>code_numeral. PROP P n) \<equiv> (\<And>n\<Colon>nat. PROP P (of_nat n))"
24999
haftmann
parents:
diff changeset
    32
proof
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    33
  fix n :: nat
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    34
  assume "\<And>n\<Colon>code_numeral. PROP P n"
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    35
  then show "PROP P (of_nat n)" .
24999
haftmann
parents:
diff changeset
    36
next
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    37
  fix n :: code_numeral
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    38
  assume "\<And>n\<Colon>nat. PROP P (of_nat n)"
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    39
  then have "PROP P (of_nat (nat_of n))" .
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
    40
  then show "PROP P n" by simp
24999
haftmann
parents:
diff changeset
    41
qed
haftmann
parents:
diff changeset
    42
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    43
lemma code_numeral_case:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    44
  assumes "\<And>n. k = of_nat n \<Longrightarrow> P"
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    45
  shows P
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    46
  by (rule assms [of "nat_of k"]) simp
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    47
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    48
lemma code_numeral_induct_raw:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    49
  assumes "\<And>n. P (of_nat n)"
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    50
  shows "P k"
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    51
proof -
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    52
  from assms have "P (of_nat (nat_of k))" .
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    53
  then show ?thesis by simp
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    54
qed
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    55
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    56
lemma nat_of_inject [simp]:
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    57
  "nat_of k = nat_of l \<longleftrightarrow> k = l"
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    58
  by (rule nat_of_inject)
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    59
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    60
lemma of_nat_inject [simp]:
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    61
  "of_nat n = of_nat m \<longleftrightarrow> n = m"
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    62
  by (rule of_nat_inject) (rule UNIV_I)+
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    63
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    64
instantiation code_numeral :: zero
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    65
begin
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    66
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28351
diff changeset
    67
definition [simp, code del]:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    68
  "0 = of_nat 0"
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    69
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    70
instance ..
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    71
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    72
end
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    73
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    74
definition [simp]:
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    75
  "Suc_code_numeral k = of_nat (Suc (nat_of k))"
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    76
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    77
rep_datatype "0 \<Colon> code_numeral" Suc_code_numeral
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    78
proof -
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    79
  fix P :: "code_numeral \<Rightarrow> bool"
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    80
  fix k :: code_numeral
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    81
  assume "P 0" then have init: "P (of_nat 0)" by simp
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    82
  assume "\<And>k. P k \<Longrightarrow> P (Suc_code_numeral k)"
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    83
    then have "\<And>n. P (of_nat n) \<Longrightarrow> P (Suc_code_numeral (of_nat n))" .
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    84
    then have step: "\<And>n. P (of_nat n) \<Longrightarrow> P (of_nat (Suc n))" by simp
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
    85
  from init step have "P (of_nat (nat_of k))"
34915
7894c7dab132 Adapted to changes in induct method.
berghofe
parents: 33340
diff changeset
    86
    by (induct ("nat_of k")) simp_all
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    87
  then show "P k" by simp
27104
791607529f6d rep_datatype command now takes list of constructors as input arguments
haftmann
parents: 26304
diff changeset
    88
qed simp_all
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    89
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    90
declare code_numeral_case [case_names nat, cases type: code_numeral]
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    91
declare code_numeral.induct [case_names nat, induct type: code_numeral]
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    92
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    93
lemma code_numeral_decr [termination_simp]:
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    94
  "k \<noteq> of_nat 0 \<Longrightarrow> nat_of k - Suc 0 < nat_of k"
30245
e67f42ac1157 consequent rewrite of index_size, size [index] to nat_of; support pseudo-primrec sepcifications with fun
haftmann
parents: 29823
diff changeset
    95
  by (cases k) simp
e67f42ac1157 consequent rewrite of index_size, size [index] to nat_of; support pseudo-primrec sepcifications with fun
haftmann
parents: 29823
diff changeset
    96
e67f42ac1157 consequent rewrite of index_size, size [index] to nat_of; support pseudo-primrec sepcifications with fun
haftmann
parents: 29823
diff changeset
    97
lemma [simp, code]:
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
    98
  "code_numeral_size = nat_of"
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
    99
proof (rule ext)
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   100
  fix k
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   101
  have "code_numeral_size k = nat_size (nat_of k)"
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   102
    by (induct k rule: code_numeral.induct) (simp_all del: zero_code_numeral_def Suc_code_numeral_def, simp_all)
34915
7894c7dab132 Adapted to changes in induct method.
berghofe
parents: 33340
diff changeset
   103
  also have "nat_size (nat_of k) = nat_of k" by (induct ("nat_of k")) simp_all
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   104
  finally show "code_numeral_size k = nat_of k" .
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   105
qed
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   106
30245
e67f42ac1157 consequent rewrite of index_size, size [index] to nat_of; support pseudo-primrec sepcifications with fun
haftmann
parents: 29823
diff changeset
   107
lemma [simp, code]:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   108
  "size = nat_of"
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   109
proof (rule ext)
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   110
  fix k
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   111
  show "size k = nat_of k"
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   112
  by (induct k) (simp_all del: zero_code_numeral_def Suc_code_numeral_def, simp_all)
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   113
qed
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   114
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   115
lemmas [code del] = code_numeral.recs code_numeral.cases
30245
e67f42ac1157 consequent rewrite of index_size, size [index] to nat_of; support pseudo-primrec sepcifications with fun
haftmann
parents: 29823
diff changeset
   116
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28351
diff changeset
   117
lemma [code]:
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37958
diff changeset
   118
  "HOL.equal k l \<longleftrightarrow> HOL.equal (nat_of k) (nat_of l)"
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37958
diff changeset
   119
  by (cases k, cases l) (simp add: equal)
24999
haftmann
parents:
diff changeset
   120
28351
abfc66969d1f non left-linear equations for nbe
haftmann
parents: 28346
diff changeset
   121
lemma [code nbe]:
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37958
diff changeset
   122
  "HOL.equal (k::code_numeral) k \<longleftrightarrow> True"
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37958
diff changeset
   123
  by (rule equal_refl)
28351
abfc66969d1f non left-linear equations for nbe
haftmann
parents: 28346
diff changeset
   124
24999
haftmann
parents:
diff changeset
   125
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   126
subsection {* Code numerals as datatype of ints *}
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   127
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   128
instantiation code_numeral :: number
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   129
begin
24999
haftmann
parents:
diff changeset
   130
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   131
definition
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   132
  "number_of = of_nat o nat"
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   133
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   134
instance ..
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   135
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   136
end
24999
haftmann
parents:
diff changeset
   137
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   138
lemma nat_of_number [simp]:
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   139
  "nat_of (number_of k) = number_of k"
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   140
  by (simp add: number_of_code_numeral_def nat_number_of_def number_of_is_id)
26264
89e25cc8da7a yet another useful lemma
haftmann
parents: 26140
diff changeset
   141
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   142
code_datatype "number_of \<Colon> int \<Rightarrow> code_numeral"
24999
haftmann
parents:
diff changeset
   143
haftmann
parents:
diff changeset
   144
haftmann
parents:
diff changeset
   145
subsection {* Basic arithmetic *}
haftmann
parents:
diff changeset
   146
35028
108662d50512 more consistent naming of type classes involving orderings (and lattices) -- c.f. NEWS
haftmann
parents: 34944
diff changeset
   147
instantiation code_numeral :: "{minus, linordered_semidom, semiring_div, linorder}"
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   148
begin
24999
haftmann
parents:
diff changeset
   149
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28351
diff changeset
   150
definition [simp, code del]:
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   151
  "(1\<Colon>code_numeral) = of_nat 1"
24999
haftmann
parents:
diff changeset
   152
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28351
diff changeset
   153
definition [simp, code del]:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   154
  "n + m = of_nat (nat_of n + nat_of m)"
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   155
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28351
diff changeset
   156
definition [simp, code del]:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   157
  "n - m = of_nat (nat_of n - nat_of m)"
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   158
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28351
diff changeset
   159
definition [simp, code del]:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   160
  "n * m = of_nat (nat_of n * nat_of m)"
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   161
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28351
diff changeset
   162
definition [simp, code del]:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   163
  "n div m = of_nat (nat_of n div nat_of m)"
24999
haftmann
parents:
diff changeset
   164
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28351
diff changeset
   165
definition [simp, code del]:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   166
  "n mod m = of_nat (nat_of n mod nat_of m)"
24999
haftmann
parents:
diff changeset
   167
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28351
diff changeset
   168
definition [simp, code del]:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   169
  "n \<le> m \<longleftrightarrow> nat_of n \<le> nat_of m"
24999
haftmann
parents:
diff changeset
   170
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28351
diff changeset
   171
definition [simp, code del]:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   172
  "n < m \<longleftrightarrow> nat_of n < nat_of m"
24999
haftmann
parents:
diff changeset
   173
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   174
instance proof
33340
a165b97f3658 moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents: 33318
diff changeset
   175
qed (auto simp add: code_numeral left_distrib intro: mult_commute)
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   176
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   177
end
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   178
32069
6d28bbd33e2c prefer code_inline over code_unfold; use code_unfold_post where appropriate
haftmann
parents: 31998
diff changeset
   179
lemma zero_code_numeral_code [code, code_unfold]:
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   180
  "(0\<Colon>code_numeral) = Numeral0"
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   181
  by (simp add: number_of_code_numeral_def Pls_def)
31998
2c7a24f74db9 code attributes use common underscore convention
haftmann
parents: 31377
diff changeset
   182
lemma [code_post]: "Numeral0 = (0\<Colon>code_numeral)"
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   183
  using zero_code_numeral_code ..
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   184
32069
6d28bbd33e2c prefer code_inline over code_unfold; use code_unfold_post where appropriate
haftmann
parents: 31998
diff changeset
   185
lemma one_code_numeral_code [code, code_unfold]:
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   186
  "(1\<Colon>code_numeral) = Numeral1"
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   187
  by (simp add: number_of_code_numeral_def Pls_def Bit1_def)
31998
2c7a24f74db9 code attributes use common underscore convention
haftmann
parents: 31377
diff changeset
   188
lemma [code_post]: "Numeral1 = (1\<Colon>code_numeral)"
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   189
  using one_code_numeral_code ..
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   190
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   191
lemma plus_code_numeral_code [code nbe]:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   192
  "of_nat n + of_nat m = of_nat (n + m)"
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   193
  by simp
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   194
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   195
definition subtract_code_numeral :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" where
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   196
  [simp, code del]: "subtract_code_numeral = op -"
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   197
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   198
lemma subtract_code_numeral_code [code nbe]:
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   199
  "subtract_code_numeral (of_nat n) (of_nat m) = of_nat (n - m)"
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   200
  by simp
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   201
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   202
lemma minus_code_numeral_code [code]:
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   203
  "n - m = subtract_code_numeral n m"
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   204
  by simp
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   205
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   206
lemma times_code_numeral_code [code nbe]:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   207
  "of_nat n * of_nat m = of_nat (n * m)"
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   208
  by simp
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   209
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   210
lemma less_eq_code_numeral_code [code nbe]:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   211
  "of_nat n \<le> of_nat m \<longleftrightarrow> n \<le> m"
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   212
  by simp
24999
haftmann
parents:
diff changeset
   213
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   214
lemma less_code_numeral_code [code nbe]:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   215
  "of_nat n < of_nat m \<longleftrightarrow> n < m"
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   216
  by simp
24999
haftmann
parents:
diff changeset
   217
31266
55e70b6d812e added lemma about 0 - 1
haftmann
parents: 31205
diff changeset
   218
lemma code_numeral_zero_minus_one:
55e70b6d812e added lemma about 0 - 1
haftmann
parents: 31205
diff changeset
   219
  "(0::code_numeral) - 1 = 0"
55e70b6d812e added lemma about 0 - 1
haftmann
parents: 31205
diff changeset
   220
  by simp
55e70b6d812e added lemma about 0 - 1
haftmann
parents: 31205
diff changeset
   221
55e70b6d812e added lemma about 0 - 1
haftmann
parents: 31205
diff changeset
   222
lemma Suc_code_numeral_minus_one:
55e70b6d812e added lemma about 0 - 1
haftmann
parents: 31205
diff changeset
   223
  "Suc_code_numeral n - 1 = n"
55e70b6d812e added lemma about 0 - 1
haftmann
parents: 31205
diff changeset
   224
  by simp
26140
e45375135052 Zero/Suc recursion combinator for type index
haftmann
parents: 26086
diff changeset
   225
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   226
lemma of_nat_code [code]:
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   227
  "of_nat = Nat.of_nat"
25918
haftmann
parents: 25767
diff changeset
   228
proof
haftmann
parents: 25767
diff changeset
   229
  fix n :: nat
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   230
  have "Nat.of_nat n = of_nat n"
25918
haftmann
parents: 25767
diff changeset
   231
    by (induct n) simp_all
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   232
  then show "of_nat n = Nat.of_nat n"
25918
haftmann
parents: 25767
diff changeset
   233
    by (rule sym)
haftmann
parents: 25767
diff changeset
   234
qed
haftmann
parents: 25767
diff changeset
   235
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   236
lemma code_numeral_not_eq_zero: "i \<noteq> of_nat 0 \<longleftrightarrow> i \<ge> 1"
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   237
  by (cases i) auto
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   238
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   239
definition nat_of_aux :: "code_numeral \<Rightarrow> nat \<Rightarrow> nat" where
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   240
  "nat_of_aux i n = nat_of i + n"
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   241
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   242
lemma nat_of_aux_code [code]:
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   243
  "nat_of_aux i n = (if i = 0 then n else nat_of_aux (i - 1) (Suc n))"
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   244
  by (auto simp add: nat_of_aux_def code_numeral_not_eq_zero)
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   245
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   246
lemma nat_of_code [code]:
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   247
  "nat_of i = nat_of_aux i 0"
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 28708
diff changeset
   248
  by (simp add: nat_of_aux_def)
25918
haftmann
parents: 25767
diff changeset
   249
35687
564a49e8be44 tuned whitespace
haftmann
parents: 35028
diff changeset
   250
definition div_mod_code_numeral :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral \<times> code_numeral" where
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   251
  [code del]: "div_mod_code_numeral n m = (n div m, n mod m)"
26009
b6a64fe38634 treating division by zero properly
haftmann
parents: 25967
diff changeset
   252
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28351
diff changeset
   253
lemma [code]:
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   254
  "div_mod_code_numeral n m = (if m = 0 then (0, n) else (n div m, n mod m))"
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   255
  unfolding div_mod_code_numeral_def by auto
26009
b6a64fe38634 treating division by zero properly
haftmann
parents: 25967
diff changeset
   256
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28351
diff changeset
   257
lemma [code]:
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   258
  "n div m = fst (div_mod_code_numeral n m)"
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   259
  unfolding div_mod_code_numeral_def by simp
26009
b6a64fe38634 treating division by zero properly
haftmann
parents: 25967
diff changeset
   260
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28351
diff changeset
   261
lemma [code]:
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   262
  "n mod m = snd (div_mod_code_numeral n m)"
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   263
  unfolding div_mod_code_numeral_def by simp
26009
b6a64fe38634 treating division by zero properly
haftmann
parents: 25967
diff changeset
   264
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   265
definition int_of :: "code_numeral \<Rightarrow> int" where
31192
a324d214009c added Code_Index.int_of operation
haftmann
parents: 31186
diff changeset
   266
  "int_of = Nat.of_nat o nat_of"
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   267
31192
a324d214009c added Code_Index.int_of operation
haftmann
parents: 31186
diff changeset
   268
lemma int_of_code [code]:
a324d214009c added Code_Index.int_of operation
haftmann
parents: 31186
diff changeset
   269
  "int_of k = (if k = 0 then 0
a324d214009c added Code_Index.int_of operation
haftmann
parents: 31186
diff changeset
   270
    else (if k mod 2 = 0 then 2 * int_of (k div 2) else 2 * int_of (k div 2) + 1))"
33340
a165b97f3658 moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents: 33318
diff changeset
   271
proof -
a165b97f3658 moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents: 33318
diff changeset
   272
  have "(nat_of k div 2) * 2 + nat_of k mod 2 = nat_of k" 
a165b97f3658 moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents: 33318
diff changeset
   273
    by (rule mod_div_equality)
a165b97f3658 moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents: 33318
diff changeset
   274
  then have "int ((nat_of k div 2) * 2 + nat_of k mod 2) = int (nat_of k)" 
a165b97f3658 moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents: 33318
diff changeset
   275
    by simp
a165b97f3658 moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents: 33318
diff changeset
   276
  then have "int (nat_of k) = int (nat_of k div 2) * 2 + int (nat_of k mod 2)" 
a165b97f3658 moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents: 33318
diff changeset
   277
    unfolding int_mult zadd_int [symmetric] by simp
a165b97f3658 moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents: 33318
diff changeset
   278
  then show ?thesis by (auto simp add: int_of_def mult_ac)
a165b97f3658 moved Nat_Transfer before Divides; distributed Nat_Transfer setup accordingly
haftmann
parents: 33318
diff changeset
   279
qed
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   280
36176
3fe7e97ccca8 replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents: 36049
diff changeset
   281
hide_const (open) of_nat nat_of int_of
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   282
36049
0ce5b7a5c2fd adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
bulwahn
parents: 35687
diff changeset
   283
subsubsection {* Lazy Evaluation of an indexed function *}
0ce5b7a5c2fd adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
bulwahn
parents: 35687
diff changeset
   284
0ce5b7a5c2fd adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
bulwahn
parents: 35687
diff changeset
   285
function iterate_upto :: "(code_numeral => 'a) => code_numeral => code_numeral => 'a Predicate.pred"
0ce5b7a5c2fd adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
bulwahn
parents: 35687
diff changeset
   286
where
0ce5b7a5c2fd adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
bulwahn
parents: 35687
diff changeset
   287
  "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))"
0ce5b7a5c2fd adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
bulwahn
parents: 35687
diff changeset
   288
by pat_completeness auto
0ce5b7a5c2fd adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
bulwahn
parents: 35687
diff changeset
   289
0ce5b7a5c2fd adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
bulwahn
parents: 35687
diff changeset
   290
termination by (relation "measure (%(f, n, m). Code_Numeral.nat_of (m + 1 - n))") auto
0ce5b7a5c2fd adding iterate_upto interface in compilations and iterate_upto functions in Isabelle theories for arithmetic setup of the predicate compiler
bulwahn
parents: 35687
diff changeset
   291
36176
3fe7e97ccca8 replaced generic 'hide' command by more conventional 'hide_class', 'hide_type', 'hide_const', 'hide_fact' -- frees some popular keywords;
wenzelm
parents: 36049
diff changeset
   292
hide_const (open) iterate_upto
28708
a1a436f09ec6 explicit check for pattern discipline before code translation
haftmann
parents: 28562
diff changeset
   293
28228
7ebe8dc06cbb evaluation using code generator
haftmann
parents: 28042
diff changeset
   294
subsection {* Code generator setup *}
24999
haftmann
parents:
diff changeset
   295
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   296
text {* Implementation of code numerals by bounded integers *}
25767
852bce03412a index now a copy of nat rather than int
haftmann
parents: 25691
diff changeset
   297
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   298
code_type code_numeral
24999
haftmann
parents:
diff changeset
   299
  (SML "int")
31377
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31266
diff changeset
   300
  (OCaml "Big'_int.big'_int")
37947
844977c7abeb avoid unreliable Haskell Int type
haftmann
parents: 36176
diff changeset
   301
  (Haskell "Integer")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   302
  (Scala "BigInt")
24999
haftmann
parents:
diff changeset
   303
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37958
diff changeset
   304
code_instance code_numeral :: equal
24999
haftmann
parents:
diff changeset
   305
  (Haskell -)
haftmann
parents:
diff changeset
   306
haftmann
parents:
diff changeset
   307
setup {*
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   308
  Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   309
    false Code_Printer.literal_naive_numeral "SML"
37947
844977c7abeb avoid unreliable Haskell Int type
haftmann
parents: 36176
diff changeset
   310
  #> fold (Numeral.add_code @{const_name number_code_numeral_inst.number_of_code_numeral}
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   311
    false Code_Printer.literal_numeral) ["OCaml", "Haskell", "Scala"]
24999
haftmann
parents:
diff changeset
   312
*}
haftmann
parents:
diff changeset
   313
25918
haftmann
parents: 25767
diff changeset
   314
code_reserved SML Int int
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   315
code_reserved Eval Integer
24999
haftmann
parents:
diff changeset
   316
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   317
code_const "op + \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   318
  (SML "Int.+/ ((_),/ (_))")
31377
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31266
diff changeset
   319
  (OCaml "Big'_int.add'_big'_int")
24999
haftmann
parents:
diff changeset
   320
  (Haskell infixl 6 "+")
34886
873c31d9f10d some syntax setup for Scala
haftmann
parents: 33340
diff changeset
   321
  (Scala infixl 7 "+")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   322
  (Eval infixl 8 "+")
24999
haftmann
parents:
diff changeset
   323
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   324
code_const "subtract_code_numeral \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
25918
haftmann
parents: 25767
diff changeset
   325
  (SML "Int.max/ (_/ -/ _,/ 0 : int)")
31377
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31266
diff changeset
   326
  (OCaml "Big'_int.max'_big'_int/ (Big'_int.sub'_big'_int/ _/ _)/ Big'_int.zero'_big'_int")
37947
844977c7abeb avoid unreliable Haskell Int type
haftmann
parents: 36176
diff changeset
   327
  (Haskell "max/ (_/ -/ _)/ (0 :: Integer)")
34886
873c31d9f10d some syntax setup for Scala
haftmann
parents: 33340
diff changeset
   328
  (Scala "!(_/ -/ _).max(0)")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   329
  (Eval "Integer.max/ (_/ -/ _)/ 0")
24999
haftmann
parents:
diff changeset
   330
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   331
code_const "op * \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   332
  (SML "Int.*/ ((_),/ (_))")
31377
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31266
diff changeset
   333
  (OCaml "Big'_int.mult'_big'_int")
24999
haftmann
parents:
diff changeset
   334
  (Haskell infixl 7 "*")
34886
873c31d9f10d some syntax setup for Scala
haftmann
parents: 33340
diff changeset
   335
  (Scala infixl 8 "*")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   336
  (Eval infixl 8 "*")
24999
haftmann
parents:
diff changeset
   337
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   338
code_const div_mod_code_numeral
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   339
  (SML "!(fn n => fn m =>/ if m = 0/ then (0, n) else/ (Int.div (n, m), Int.mod (n, m)))")
34898
62d70417f8ce allow individual printing of numerals during code serialization
haftmann
parents: 34886
diff changeset
   340
  (OCaml "Big'_int.quomod'_big'_int/ (Big'_int.abs'_big'_int _)/ (Big'_int.abs'_big'_int _)")
26009
b6a64fe38634 treating division by zero properly
haftmann
parents: 25967
diff changeset
   341
  (Haskell "divMod")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   342
  (Scala "!((k: BigInt) => (l: BigInt) =>/ if (l == 0)/ (BigInt(0), k) else/ (k.abs '/% l.abs))")
39818
ff9e9d5ac171 use module integer for Eval
haftmann
parents: 39272
diff changeset
   343
  (Eval "!(fn n => fn m =>/ if m = 0/ then (0, n) else/ (Integer.div'_mod n m))")
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   344
38857
97775f3e8722 renamed class/constant eq to equal; tuned some instantiations
haftmann
parents: 37958
diff changeset
   345
code_const "HOL.equal \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
24999
haftmann
parents:
diff changeset
   346
  (SML "!((_ : Int.int) = _)")
31377
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31266
diff changeset
   347
  (OCaml "Big'_int.eq'_big'_int")
39272
0b61951d2682 Haskell == is infix, not infixl
haftmann
parents: 38857
diff changeset
   348
  (Haskell infix 4 "==")
34886
873c31d9f10d some syntax setup for Scala
haftmann
parents: 33340
diff changeset
   349
  (Scala infixl 5 "==")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   350
  (Eval "!((_ : int) = _)")
24999
haftmann
parents:
diff changeset
   351
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   352
code_const "op \<le> \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   353
  (SML "Int.<=/ ((_),/ (_))")
31377
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31266
diff changeset
   354
  (OCaml "Big'_int.le'_big'_int")
24999
haftmann
parents:
diff changeset
   355
  (Haskell infix 4 "<=")
34898
62d70417f8ce allow individual printing of numerals during code serialization
haftmann
parents: 34886
diff changeset
   356
  (Scala infixl 4 "<=")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   357
  (Eval infixl 6 "<=")
24999
haftmann
parents:
diff changeset
   358
31205
98370b26c2ce String.literal replaces message_string, code_numeral replaces (code_)index
haftmann
parents: 31203
diff changeset
   359
code_const "op < \<Colon> code_numeral \<Rightarrow> code_numeral \<Rightarrow> bool"
25928
042e877d9841 tuned code setup
haftmann
parents: 25918
diff changeset
   360
  (SML "Int.</ ((_),/ (_))")
31377
a48f9ef9de15 OCaml builtin intergers are elusive; avoid
haftmann
parents: 31266
diff changeset
   361
  (OCaml "Big'_int.lt'_big'_int")
24999
haftmann
parents:
diff changeset
   362
  (Haskell infix 4 "<")
34898
62d70417f8ce allow individual printing of numerals during code serialization
haftmann
parents: 34886
diff changeset
   363
  (Scala infixl 4 "<")
37958
9728342bcd56 another refinement chapter in the neverending numeral story
haftmann
parents: 37947
diff changeset
   364
  (Eval infixl 6 "<")
24999
haftmann
parents:
diff changeset
   365
haftmann
parents:
diff changeset
   366
end