src/HOL/Library/ML_Int.thy
author haftmann
Fri Aug 24 14:14:20 2007 +0200 (2007-08-24)
changeset 24423 ae9cd0e92423
parent 24354 0fdabe28f0e6
child 24700 291665d063e4
permissions -rw-r--r--
overloaded definitions accompanied by explicit constants
haftmann@23859
     1
(*  ID:         $Id$
haftmann@23859
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@23859
     3
*)
haftmann@23859
     4
haftmann@23859
     5
header {* Built-in integers for ML *}
haftmann@23859
     6
haftmann@23859
     7
theory ML_Int
haftmann@23859
     8
imports List
haftmann@23859
     9
begin
haftmann@23859
    10
haftmann@23859
    11
subsection {* Datatype of built-in integers *}
haftmann@23859
    12
haftmann@23859
    13
datatype ml_int = ml_int_of_int int
haftmann@23859
    14
haftmann@23859
    15
lemmas [code func del] = ml_int.recs ml_int.cases
haftmann@23859
    16
haftmann@23859
    17
fun
haftmann@23859
    18
  int_of_ml_int :: "ml_int \<Rightarrow> int"
haftmann@23859
    19
where
haftmann@23859
    20
  "int_of_ml_int (ml_int_of_int k) = k"
haftmann@23859
    21
lemmas [code func del] = int_of_ml_int.simps
haftmann@23859
    22
haftmann@23859
    23
lemma ml_int_id [simp]:
haftmann@23859
    24
  "ml_int_of_int (int_of_ml_int k) = k"
haftmann@23859
    25
  by (cases k) simp_all
haftmann@23859
    26
haftmann@23859
    27
lemma ml_int:
haftmann@23859
    28
  "(\<And>k\<Colon>ml_int. PROP P k) \<equiv> (\<And>k\<Colon>int. PROP P (ml_int_of_int k))"
haftmann@23859
    29
proof
haftmann@23859
    30
  fix k :: int
haftmann@23859
    31
  assume "\<And>k\<Colon>ml_int. PROP P k"
haftmann@23859
    32
  then show "PROP P (ml_int_of_int k)" .
haftmann@23859
    33
next
haftmann@23859
    34
  fix k :: ml_int
haftmann@23859
    35
  assume "\<And>k\<Colon>int. PROP P (ml_int_of_int k)"
haftmann@23859
    36
  then have "PROP P (ml_int_of_int (int_of_ml_int k))" .
haftmann@23859
    37
  then show "PROP P k" by simp
haftmann@23859
    38
qed
haftmann@23859
    39
haftmann@23859
    40
lemma [code func]: "size (k\<Colon>ml_int) = 0"
haftmann@23859
    41
  by (cases k) simp_all
haftmann@23859
    42
haftmann@23859
    43
haftmann@23859
    44
subsection {* Built-in integers as datatype on numerals *}
haftmann@23859
    45
haftmann@23859
    46
instance ml_int :: number
haftmann@23859
    47
  "number_of \<equiv> ml_int_of_int" ..
haftmann@23859
    48
haftmann@23859
    49
lemmas [code inline] = number_of_ml_int_def [symmetric]
haftmann@23859
    50
haftmann@23859
    51
code_datatype "number_of \<Colon> int \<Rightarrow> ml_int"
haftmann@23859
    52
haftmann@23859
    53
lemma number_of_ml_int_id [simp]:
haftmann@23859
    54
  "number_of (int_of_ml_int k) = k"
haftmann@23859
    55
  unfolding number_of_ml_int_def by simp
haftmann@23859
    56
haftmann@23859
    57
haftmann@23859
    58
subsection {* Basic arithmetic *}
haftmann@23859
    59
haftmann@23859
    60
instance ml_int :: zero
haftmann@23859
    61
  [simp]: "0 \<equiv> ml_int_of_int 0" ..
haftmann@23859
    62
lemmas [code func del] = zero_ml_int_def
haftmann@23859
    63
haftmann@23859
    64
instance ml_int :: one
haftmann@23859
    65
  [simp]: "1 \<equiv> ml_int_of_int 1" ..
haftmann@23859
    66
lemmas [code func del] = one_ml_int_def
haftmann@23859
    67
haftmann@23859
    68
instance ml_int :: plus
haftmann@23859
    69
  [simp]: "k + l \<equiv> ml_int_of_int (int_of_ml_int k + int_of_ml_int l)" ..
haftmann@23859
    70
lemmas [code func del] = plus_ml_int_def
haftmann@23859
    71
lemma plus_ml_int_code [code func]:
haftmann@23859
    72
  "ml_int_of_int k + ml_int_of_int l = ml_int_of_int (k + l)"
haftmann@23859
    73
  unfolding plus_ml_int_def by simp
haftmann@23859
    74
haftmann@23859
    75
instance ml_int :: minus
haftmann@23859
    76
  [simp]: "- k \<equiv> ml_int_of_int (- int_of_ml_int k)"
haftmann@23859
    77
  [simp]: "k - l \<equiv> ml_int_of_int (int_of_ml_int k - int_of_ml_int l)" ..
haftmann@23859
    78
lemmas [code func del] = uminus_ml_int_def minus_ml_int_def
haftmann@23859
    79
lemma uminus_ml_int_code [code func]:
haftmann@23859
    80
  "- ml_int_of_int k \<equiv> ml_int_of_int (- k)"
haftmann@23859
    81
  unfolding uminus_ml_int_def by simp
haftmann@23859
    82
lemma minus_ml_int_code [code func]:
haftmann@23859
    83
  "ml_int_of_int k - ml_int_of_int l = ml_int_of_int (k - l)"
haftmann@23859
    84
  unfolding minus_ml_int_def by simp
haftmann@23859
    85
haftmann@23859
    86
instance ml_int :: times
haftmann@23859
    87
  [simp]: "k * l \<equiv> ml_int_of_int (int_of_ml_int k * int_of_ml_int l)" ..
haftmann@23859
    88
lemmas [code func del] = times_ml_int_def
haftmann@23859
    89
lemma times_ml_int_code [code func]:
haftmann@23859
    90
  "ml_int_of_int k * ml_int_of_int l = ml_int_of_int (k * l)"
haftmann@23859
    91
  unfolding times_ml_int_def by simp
haftmann@23859
    92
haftmann@23859
    93
instance ml_int :: ord
haftmann@23859
    94
  [simp]: "k \<le> l \<equiv> int_of_ml_int k \<le> int_of_ml_int l"
haftmann@23859
    95
  [simp]: "k < l \<equiv> int_of_ml_int k < int_of_ml_int l" ..
haftmann@23859
    96
lemmas [code func del] = less_eq_ml_int_def less_ml_int_def
haftmann@23859
    97
lemma less_eq_ml_int_code [code func]:
haftmann@23859
    98
  "ml_int_of_int k \<le> ml_int_of_int l \<longleftrightarrow> k \<le> l"
haftmann@23859
    99
  unfolding less_eq_ml_int_def by simp
haftmann@23859
   100
lemma less_ml_int_code [code func]:
haftmann@23859
   101
  "ml_int_of_int k < ml_int_of_int l \<longleftrightarrow> k < l"
haftmann@23859
   102
  unfolding less_ml_int_def by simp
haftmann@23859
   103
haftmann@23859
   104
instance ml_int :: ring_1
haftmann@23859
   105
  by default (auto simp add: left_distrib right_distrib)
haftmann@23859
   106
haftmann@23859
   107
lemma of_nat_ml_int: "of_nat n = ml_int_of_int (of_nat n)"
haftmann@23859
   108
proof (induct n)
haftmann@23859
   109
  case 0 show ?case by simp
haftmann@23859
   110
next
haftmann@23859
   111
  case (Suc n)
huffman@24354
   112
  then have "int_of_ml_int (ml_int_of_int (int n))
haftmann@23859
   113
    = int_of_ml_int (of_nat n)" by simp
huffman@24354
   114
  then have "int n = int_of_ml_int (of_nat n)" by simp
haftmann@23859
   115
  then show ?case by simp
haftmann@23859
   116
qed
haftmann@23859
   117
haftmann@23859
   118
instance ml_int :: number_ring
haftmann@23859
   119
  by default
haftmann@23859
   120
    (simp_all add: left_distrib number_of_ml_int_def of_int_of_nat of_nat_ml_int)
haftmann@23859
   121
haftmann@23859
   122
lemma zero_ml_int_code [code inline, code func]:
haftmann@23859
   123
  "(0\<Colon>ml_int) = Numeral0"
haftmann@23859
   124
  by simp
haftmann@23859
   125
haftmann@23859
   126
lemma one_ml_int_code [code inline, code func]:
haftmann@23859
   127
  "(1\<Colon>ml_int) = Numeral1"
haftmann@23859
   128
  by simp
haftmann@23859
   129
huffman@24354
   130
instance ml_int :: abs
haftmann@23859
   131
  "\<bar>k\<bar> \<equiv> if k < 0 then -k else k" ..
haftmann@23859
   132
haftmann@23859
   133
haftmann@23859
   134
subsection {* Conversion to @{typ nat} *}
haftmann@23859
   135
haftmann@23859
   136
definition
haftmann@23859
   137
  nat_of_ml_int :: "ml_int \<Rightarrow> nat"
haftmann@23859
   138
where
haftmann@23859
   139
  "nat_of_ml_int = nat o int_of_ml_int"
haftmann@23859
   140
haftmann@23859
   141
definition
haftmann@23859
   142
  nat_of_ml_int_aux :: "ml_int \<Rightarrow> nat \<Rightarrow> nat" where
haftmann@23859
   143
  "nat_of_ml_int_aux i n = nat_of_ml_int i + n"
haftmann@23859
   144
haftmann@23859
   145
lemma nat_of_ml_int_aux_code [code]:
haftmann@23859
   146
  "nat_of_ml_int_aux i n = (if i \<le> 0 then n else nat_of_ml_int_aux (i - 1) (Suc n))"
haftmann@23859
   147
  by (auto simp add: nat_of_ml_int_aux_def nat_of_ml_int_def)
haftmann@23859
   148
haftmann@23859
   149
lemma nat_of_ml_int_code [code]:
haftmann@23859
   150
  "nat_of_ml_int i = nat_of_ml_int_aux i 0"
haftmann@23859
   151
  by (simp add: nat_of_ml_int_aux_def)
haftmann@23859
   152
haftmann@23859
   153
haftmann@23859
   154
subsection {* ML interface *}
haftmann@23859
   155
haftmann@23859
   156
ML {*
haftmann@23859
   157
structure ML_Int =
haftmann@23859
   158
struct
haftmann@23859
   159
haftmann@23859
   160
fun mk k = @{term ml_int_of_int} $ HOLogic.mk_number @{typ ml_int} k;
haftmann@23859
   161
haftmann@23859
   162
end;
haftmann@23859
   163
*}
haftmann@23859
   164
haftmann@23859
   165
haftmann@23859
   166
subsection {* Code serialization *}
haftmann@23859
   167
haftmann@23859
   168
code_type ml_int
haftmann@23859
   169
  (SML "int")
haftmann@23859
   170
haftmann@23859
   171
setup {*
haftmann@24219
   172
  CodeTarget.add_pretty_numeral "SML" false
haftmann@24423
   173
    @{const_name ml_int_of_int}
haftmann@23859
   174
    @{const_name Numeral.B0} @{const_name Numeral.B1}
haftmann@23859
   175
    @{const_name Numeral.Pls} @{const_name Numeral.Min}
haftmann@23859
   176
    @{const_name Numeral.Bit}
haftmann@23859
   177
*}
haftmann@23859
   178
haftmann@23859
   179
code_reserved SML int
haftmann@23859
   180
haftmann@23859
   181
code_const "op + \<Colon> ml_int \<Rightarrow> ml_int \<Rightarrow> ml_int"
haftmann@23859
   182
  (SML "Int.+ ((_), (_))")
haftmann@23859
   183
haftmann@23859
   184
code_const "uminus \<Colon> ml_int \<Rightarrow> ml_int"
haftmann@23859
   185
  (SML "Int.~")
haftmann@23859
   186
haftmann@23859
   187
code_const "op - \<Colon> ml_int \<Rightarrow> ml_int \<Rightarrow> ml_int"
haftmann@23859
   188
  (SML "Int.- ((_), (_))")
haftmann@23859
   189
haftmann@23859
   190
code_const "op * \<Colon> ml_int \<Rightarrow> ml_int \<Rightarrow> ml_int"
haftmann@23859
   191
  (SML "Int.* ((_), (_))")
haftmann@23859
   192
haftmann@23859
   193
code_const "op = \<Colon> ml_int \<Rightarrow> ml_int \<Rightarrow> bool"
haftmann@23859
   194
  (SML "!((_ : Int.int) = _)")
haftmann@23859
   195
haftmann@23859
   196
code_const "op \<le> \<Colon> ml_int \<Rightarrow> ml_int \<Rightarrow> bool"
haftmann@23859
   197
  (SML "Int.<= ((_), (_))")
haftmann@23859
   198
haftmann@23859
   199
code_const "op < \<Colon> ml_int \<Rightarrow> ml_int \<Rightarrow> bool"
haftmann@23859
   200
  (SML "Int.< ((_), (_))")
haftmann@23859
   201
haftmann@23859
   202
end
haftmann@23859
   203
haftmann@23859
   204