src/Pure/ML-Systems/proper_int.ML
author berghofe
Mon Jan 21 14:18:49 2008 +0100 (2008-01-21)
changeset 25939 ddea202704b4
parent 24604 d5c5d2e13fbf
child 28490 40c3f900c457
permissions -rw-r--r--
Removed Logic.auto_rename.
wenzelm@24140
     1
(*  Title:      Pure/ML-Systems/proper_int.ML
wenzelm@24140
     2
    ID:         $Id$
wenzelm@24140
     3
    Author:     Makarius
wenzelm@24140
     4
wenzelm@24140
     5
SML basis with type int representing proper integers, not machine
wenzelm@24140
     6
words.
wenzelm@24140
     7
*)
wenzelm@24140
     8
wenzelm@24597
     9
val ml_system_fix_ints = true;
wenzelm@24597
    10
wenzelm@24140
    11
val mk_int = IntInf.fromInt: Int.int -> IntInf.int;
wenzelm@24140
    12
val dest_int = IntInf.toInt: IntInf.int -> Int.int;
wenzelm@24140
    13
wenzelm@24140
    14
wenzelm@24140
    15
(* Int *)
wenzelm@24140
    16
wenzelm@24140
    17
structure OrigInt = Int;
wenzelm@24140
    18
structure OrigIntInf = IntInf;
wenzelm@24140
    19
type int = IntInf.int;
wenzelm@24140
    20
wenzelm@24140
    21
structure IntInf =
wenzelm@24140
    22
struct
wenzelm@24140
    23
  open IntInf;
wenzelm@24140
    24
  fun fromInt (a: int) = a;
wenzelm@24140
    25
  fun toInt (a: int) = a;
wenzelm@24140
    26
  val log2 = mk_int o IntInf.log2;
wenzelm@24140
    27
  val sign = mk_int o IntInf.sign;
wenzelm@24140
    28
end;
wenzelm@24140
    29
wenzelm@24140
    30
structure Int = IntInf;
wenzelm@24140
    31
wenzelm@24140
    32
wenzelm@24140
    33
(* List *)
wenzelm@24140
    34
wenzelm@24140
    35
structure List =
wenzelm@24140
    36
struct
wenzelm@24140
    37
  open List;
wenzelm@24140
    38
  fun length a = mk_int (List.length a);
wenzelm@24140
    39
  fun nth (a, b) = List.nth (a, dest_int b);
wenzelm@24140
    40
  fun take (a, b) = List.take (a, dest_int b);
wenzelm@24140
    41
  fun drop (a, b) = List.drop (a, dest_int b);
wenzelm@24140
    42
  fun tabulate (a, b) = List.tabulate (dest_int a, b o mk_int);
wenzelm@24140
    43
end;
wenzelm@24140
    44
wenzelm@24140
    45
val length = List.length;
wenzelm@24140
    46
wenzelm@24140
    47
wenzelm@24140
    48
(* Array *)
wenzelm@24140
    49
wenzelm@24140
    50
structure Array =
wenzelm@24140
    51
struct
wenzelm@24140
    52
  open Array;
wenzelm@24140
    53
  val maxLen = mk_int Array.maxLen;
wenzelm@24140
    54
  fun array (a, b) = Array.array (dest_int a, b);
wenzelm@24140
    55
  fun tabulate (a, b) = Array.tabulate (dest_int a, b o mk_int);
wenzelm@24140
    56
  fun length a = mk_int (Array.length a);
wenzelm@24140
    57
  fun sub (a, b) = Array.sub (a, dest_int b);
wenzelm@24140
    58
  fun update (a, b, c) = Array.update (a, dest_int b, c);
wenzelm@24140
    59
  fun copy {src, dst, di} = Array.copy {src = src, dst = dst, di = dest_int di};
wenzelm@24140
    60
  fun copyVec {src, dst, di} = Array.copyVec {src = src, dst = dst, di = dest_int di};
wenzelm@24140
    61
  fun appi a b = Array.appi (fn (x, y) => a (mk_int x, y)) b;
wenzelm@24140
    62
  fun modifyi a b = Array.modifyi (fn (x, y) => a (mk_int x, y)) b;
wenzelm@24140
    63
  fun foldli a b c = Array.foldli (fn (x, y, z) => a (mk_int x, y, z)) b c;
wenzelm@24140
    64
  fun foldri a b c = Array.foldri (fn (x, y, z) => a (mk_int x, y, z)) b c;
wenzelm@24140
    65
  fun findi a b =
wenzelm@24140
    66
    (case Array.findi (fn (x, y) => a (mk_int x, y)) b of
wenzelm@24140
    67
      NONE => NONE
wenzelm@24140
    68
    | SOME (c, d) => SOME (mk_int c, d));
wenzelm@24140
    69
end;
wenzelm@24140
    70
wenzelm@24140
    71
wenzelm@24140
    72
(* Vector *)
wenzelm@24140
    73
wenzelm@24140
    74
structure Vector =
wenzelm@24140
    75
struct
wenzelm@24140
    76
  open Vector;
wenzelm@24140
    77
  val maxLen = mk_int Vector.maxLen;
wenzelm@24140
    78
  fun tabulate (a, b) = Vector.tabulate (dest_int a, b o mk_int);
wenzelm@24140
    79
  fun length a = mk_int (Vector.length a);
wenzelm@24140
    80
  fun sub (a, b) = Vector.sub (a, dest_int b);
wenzelm@24140
    81
  fun update (a, b, c) = Vector.update (a, dest_int b, c);
wenzelm@24140
    82
  fun appi a b = Vector.appi (fn (x, y) => a (mk_int x, y)) b;
wenzelm@24140
    83
  fun mapi a b = Vector.mapi (fn (x, y) => a (mk_int x, y)) b;
wenzelm@24140
    84
  fun foldli a b c = Vector.foldli (fn (x, y, z) => a (mk_int x, y, z)) b c;
wenzelm@24140
    85
  fun foldri a b c = Vector.foldri (fn (x, y, z) => a (mk_int x, y, z)) b c;
wenzelm@24140
    86
  fun findi a b =
wenzelm@24140
    87
    (case Vector.findi (fn (x, y) => a (mk_int x, y)) b of
wenzelm@24140
    88
      NONE => NONE
wenzelm@24140
    89
    | SOME (c, d) => SOME (mk_int c, d));
wenzelm@24140
    90
end;
wenzelm@24140
    91
wenzelm@24140
    92
wenzelm@24140
    93
(* Char *)
wenzelm@24140
    94
wenzelm@24140
    95
structure Char =
wenzelm@24140
    96
struct
wenzelm@24140
    97
  open Char;
wenzelm@24140
    98
  val maxOrd = mk_int Char.maxOrd;
wenzelm@24140
    99
  val chr = Char.chr o dest_int;
wenzelm@24140
   100
  val ord = mk_int o Char.ord;
wenzelm@24140
   101
end;
wenzelm@24140
   102
wenzelm@24140
   103
val chr = Char.chr;
wenzelm@24140
   104
val ord = Char.ord;
wenzelm@24140
   105
wenzelm@24140
   106
wenzelm@24140
   107
(* String *)
wenzelm@24140
   108
wenzelm@24140
   109
structure String =
wenzelm@24140
   110
struct
wenzelm@24140
   111
  open String;
wenzelm@24140
   112
  val maxSize = mk_int String.maxSize;
wenzelm@24140
   113
  val size = mk_int o String.size;
wenzelm@24140
   114
  fun sub (a, b) = String.sub (a, dest_int b);
wenzelm@24140
   115
  fun extract (a, b, c) = String.extract (a, dest_int b, Option.map dest_int c);
wenzelm@24140
   116
  fun substring (a, b, c) = String.substring (a, dest_int b, dest_int c);
wenzelm@24140
   117
end;
wenzelm@24140
   118
wenzelm@24140
   119
val size = String.size;
wenzelm@24140
   120
val substring = String.substring;
wenzelm@24140
   121
wenzelm@24140
   122
wenzelm@24140
   123
(* Substring *)
wenzelm@24140
   124
wenzelm@24140
   125
structure Substring =
wenzelm@24140
   126
struct
wenzelm@24140
   127
  open Substring;
wenzelm@24140
   128
  fun sub (a, b) = Substring.sub (a, dest_int b);
wenzelm@24140
   129
  val size = mk_int o Substring.size;
wenzelm@24140
   130
  fun base a = let val (b, c, d) = Substring.base a in (b, mk_int c, mk_int d) end;
wenzelm@24140
   131
  fun extract (a, b, c) = Substring.extract (a, dest_int b, Option.map dest_int c);
wenzelm@24140
   132
  fun substring (a, b, c) = Substring.substring (a, dest_int b, dest_int c);
wenzelm@24140
   133
  fun triml a b = Substring.triml (dest_int a) b;
wenzelm@24140
   134
  fun trimr a b = Substring.trimr (dest_int a) b;
wenzelm@24140
   135
  fun slice (a, b, c) = Substring.slice (a, dest_int b, Option.map dest_int c);
wenzelm@24140
   136
  fun splitAt (a, b) = Substring.splitAt (a, dest_int b);
wenzelm@24140
   137
end;
wenzelm@24140
   138
wenzelm@24140
   139
wenzelm@24140
   140
(* Word *)
wenzelm@24140
   141
wenzelm@24140
   142
structure Word =
wenzelm@24140
   143
struct
wenzelm@24140
   144
  open Word;
wenzelm@24140
   145
  val wordSize = mk_int Word.wordSize;
wenzelm@24140
   146
  val toInt = mk_int o Word.toInt;
wenzelm@24140
   147
  val toIntX = mk_int o Word.toIntX;
wenzelm@24140
   148
  val fromInt = Word.fromInt o dest_int;
wenzelm@24140
   149
end;
wenzelm@24140
   150
wenzelm@24140
   151
wenzelm@24140
   152
(* Real *)
wenzelm@24140
   153
wenzelm@24140
   154
structure Real =
wenzelm@24140
   155
struct
wenzelm@24140
   156
  open Real;
wenzelm@24140
   157
  val radix = mk_int Real.radix;
wenzelm@24140
   158
  val precision = mk_int Real.precision;
wenzelm@24140
   159
  fun sign a = mk_int (Real.sign a);
wenzelm@24140
   160
  fun toManExp a = let val {man, exp} = Real.toManExp a in {man = man, exp = mk_int exp} end;
wenzelm@24140
   161
  fun fromManExp {man, exp} = Real.fromManExp {man = man, exp = dest_int exp};
wenzelm@24140
   162
  val ceil = mk_int o Real.ceil;
wenzelm@24140
   163
  val floor = mk_int o Real.floor;
wenzelm@24140
   164
  val real = Real.fromInt o dest_int;
wenzelm@24140
   165
  val round = mk_int o Real.round;
wenzelm@24140
   166
  val trunc = mk_int o Real.trunc;
wenzelm@24140
   167
  fun toInt a b = mk_int (Real.toInt a b);
wenzelm@24140
   168
  fun fromInt a = Real.fromInt (dest_int a);
wenzelm@24140
   169
end;
wenzelm@24140
   170
wenzelm@24140
   171
val ceil = Real.ceil;
wenzelm@24140
   172
val floor = Real.floor;
wenzelm@24140
   173
val real = Real.real;
wenzelm@24140
   174
val round = Real.round;
wenzelm@24140
   175
val trunc = Real.trunc;
wenzelm@24140
   176
wenzelm@24140
   177
wenzelm@24140
   178
(* TextIO *)
wenzelm@24140
   179
wenzelm@24140
   180
structure TextIO =
wenzelm@24140
   181
struct
wenzelm@24140
   182
  open TextIO;
wenzelm@24140
   183
  fun inputN (a, b) = TextIO.inputN (a, dest_int b);
wenzelm@24140
   184
  fun canInput (a, b) = Option.map mk_int (TextIO.canInput (a, dest_int b));
wenzelm@24140
   185
end;
wenzelm@24140
   186
wenzelm@24140
   187
wenzelm@24140
   188
(* Time *)
wenzelm@24140
   189
wenzelm@24140
   190
structure Time =
wenzelm@24140
   191
struct
wenzelm@24140
   192
  open Time;
wenzelm@24140
   193
  fun fmt a b = Time.fmt (dest_int a) b;
wenzelm@24140
   194
end;
wenzelm@24604
   195
wenzelm@24604
   196
wenzelm@24604
   197
(* Posix *)
wenzelm@24604
   198
wenzelm@24604
   199
structure Posix =
wenzelm@24604
   200
struct
wenzelm@24604
   201
  open Posix;
wenzelm@24604
   202
  structure IO =
wenzelm@24604
   203
  struct
wenzelm@24604
   204
    open IO;
wenzelm@24604
   205
    fun mkTextWriter {appendMode, chunkSize, fd, initBlkMode, name} =
wenzelm@24604
   206
      IO.mkTextWriter {appendMode = appendMode, chunkSize = dest_int chunkSize,
wenzelm@24604
   207
        fd = fd, initBlkMode = initBlkMode, name = name};
wenzelm@24604
   208
  end;
wenzelm@24604
   209
end;
wenzelm@24604
   210