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