src/Pure/ML-Systems/proper_int.ML
author wenzelm
Fri Apr 12 14:54:14 2013 +0200 (2013-04-12)
changeset 51700 c8f2bad67dbb
parent 50801 b8ff6d1ee56c
child 52277 2bbeab01c0ea
permissions -rw-r--r--
tuned signature;
tuned comments;
     1 (*  Title:      Pure/ML-Systems/proper_int.ML
     2     Author:     Makarius
     3 
     4 SML basis with type int representing proper integers, not machine
     5 words.
     6 *)
     7 
     8 val mk_int = IntInf.fromInt: Int.int -> IntInf.int;
     9 val dest_int = IntInf.toInt: IntInf.int -> Int.int;
    10 
    11 
    12 (* Int *)
    13 
    14 type int = IntInf.int;
    15 
    16 structure IntInf =
    17 struct
    18   open IntInf;
    19   fun fromInt (a: int) = a;
    20   fun toInt (a: int) = a;
    21   val log2 = mk_int o IntInf.log2;
    22   val sign = mk_int o IntInf.sign;
    23 end;
    24 
    25 structure Int = IntInf;
    26 
    27 
    28 (* List *)
    29 
    30 structure List =
    31 struct
    32   open List;
    33   fun length a = mk_int (List.length a);
    34   fun nth (a, b) = List.nth (a, dest_int b);
    35   fun take (a, b) = List.take (a, dest_int b);
    36   fun drop (a, b) = List.drop (a, dest_int b);
    37   fun tabulate (a, b) = List.tabulate (dest_int a, b o mk_int);
    38 end;
    39 
    40 val length = List.length;
    41 
    42 
    43 (* Array *)
    44 
    45 structure Array =
    46 struct
    47   open Array;
    48   val maxLen = mk_int Array.maxLen;
    49   fun array (a, b) = Array.array (dest_int a, b);
    50   fun tabulate (a, b) = Array.tabulate (dest_int a, b o mk_int);
    51   fun length a = mk_int (Array.length a);
    52   fun sub (a, b) = Array.sub (a, dest_int b);
    53   fun update (a, b, c) = Array.update (a, dest_int b, c);
    54   fun copy {src, dst, di} = Array.copy {src = src, dst = dst, di = dest_int di};
    55   fun copyVec {src, dst, di} = Array.copyVec {src = src, dst = dst, di = dest_int di};
    56   fun appi a b = Array.appi (fn (x, y) => a (mk_int x, y)) b;
    57   fun modifyi a b = Array.modifyi (fn (x, y) => a (mk_int x, y)) b;
    58   fun foldli a b c = Array.foldli (fn (x, y, z) => a (mk_int x, y, z)) b c;
    59   fun foldri a b c = Array.foldri (fn (x, y, z) => a (mk_int x, y, z)) b c;
    60   fun findi a b =
    61     (case Array.findi (fn (x, y) => a (mk_int x, y)) b of
    62       NONE => NONE
    63     | SOME (c, d) => SOME (mk_int c, d));
    64 end;
    65 
    66 
    67 (* Vector *)
    68 
    69 structure Vector =
    70 struct
    71   open Vector;
    72   val maxLen = mk_int Vector.maxLen;
    73   fun tabulate (a, b) = Vector.tabulate (dest_int a, b o mk_int);
    74   fun length a = mk_int (Vector.length a);
    75   fun sub (a, b) = Vector.sub (a, dest_int b);
    76   fun update (a, b, c) = Vector.update (a, dest_int b, c);
    77   fun appi a b = Vector.appi (fn (x, y) => a (mk_int x, y)) b;
    78   fun mapi a b = Vector.mapi (fn (x, y) => a (mk_int x, y)) b;
    79   fun foldli a b c = Vector.foldli (fn (x, y, z) => a (mk_int x, y, z)) b c;
    80   fun foldri a b c = Vector.foldri (fn (x, y, z) => a (mk_int x, y, z)) b c;
    81   fun findi a b =
    82     (case Vector.findi (fn (x, y) => a (mk_int x, y)) b of
    83       NONE => NONE
    84     | SOME (c, d) => SOME (mk_int c, d));
    85 end;
    86 
    87 
    88 (* Word8VectorSlice *)
    89 
    90 structure Word8VectorSlice =
    91 struct
    92   open Word8VectorSlice;
    93   val length = mk_int o Word8VectorSlice.length;
    94   fun subslice (a, b, c) = Word8VectorSlice.subslice (a, dest_int b, Option.map dest_int c);
    95 end;
    96 
    97 
    98 (* Char *)
    99 
   100 structure Char =
   101 struct
   102   open Char;
   103   val maxOrd = mk_int Char.maxOrd;
   104   val chr = Char.chr o dest_int;
   105   val ord = mk_int o Char.ord;
   106 end;
   107 
   108 val chr = Char.chr;
   109 val ord = Char.ord;
   110 
   111 
   112 (* String *)
   113 
   114 structure String =
   115 struct
   116   open String;
   117   val maxSize = mk_int String.maxSize;
   118   val size = mk_int o String.size;
   119   fun sub (a, b) = String.sub (a, dest_int b);
   120   fun extract (a, b, c) = String.extract (a, dest_int b, Option.map dest_int c);
   121   fun substring (a, b, c) = String.substring (a, dest_int b, dest_int c);
   122 end;
   123 
   124 val size = String.size;
   125 val substring = String.substring;
   126 
   127 
   128 (* Substring *)
   129 
   130 structure Substring =
   131 struct
   132   open Substring;
   133   fun sub (a, b) = Substring.sub (a, dest_int b);
   134   val size = mk_int o Substring.size;
   135   fun base a = let val (b, c, d) = Substring.base a in (b, mk_int c, mk_int d) end;
   136   fun extract (a, b, c) = Substring.extract (a, dest_int b, Option.map dest_int c);
   137   fun substring (a, b, c) = Substring.substring (a, dest_int b, dest_int c);
   138   fun triml a b = Substring.triml (dest_int a) b;
   139   fun trimr a b = Substring.trimr (dest_int a) b;
   140   fun slice (a, b, c) = Substring.slice (a, dest_int b, Option.map dest_int c);
   141   fun splitAt (a, b) = Substring.splitAt (a, dest_int b);
   142 end;
   143 
   144 
   145 (* StringCvt *)
   146 
   147 structure StringCvt =
   148 struct
   149   open StringCvt;
   150   datatype realfmt = EXACT | FIX of int option | GEN of int option | SCI of int option;
   151   fun realfmt fmt = Real.fmt
   152     (case fmt of
   153       EXACT => StringCvt.EXACT
   154     | FIX NONE => StringCvt.FIX NONE
   155     | FIX (SOME b) => StringCvt.FIX (SOME (dest_int b))
   156     | GEN NONE => StringCvt.GEN NONE
   157     | GEN (SOME b) => StringCvt.GEN (SOME (dest_int b))
   158     | SCI NONE => StringCvt.SCI NONE
   159     | SCI (SOME b) => StringCvt.SCI (SOME (dest_int b)));
   160 end;
   161 
   162 
   163 (* Word *)
   164 
   165 structure Word =
   166 struct
   167   open Word;
   168   val wordSize = mk_int Word.wordSize;
   169   val toInt = Word.toLargeInt;
   170   val toIntX = Word.toLargeIntX;
   171   val fromInt = Word.fromLargeInt;
   172 end;
   173 
   174 structure Word8 =
   175 struct
   176   open Word8;
   177   val wordSize = mk_int Word8.wordSize;
   178   val toInt = Word8.toLargeInt;
   179   val toIntX = Word8.toLargeIntX;
   180   val fromInt = Word8.fromLargeInt;
   181 end;
   182 
   183 structure Word32 =
   184 struct
   185   open Word32;
   186   val wordSize = mk_int Word32.wordSize;
   187   val toInt = Word32.toLargeInt;
   188   val toIntX = Word32.toLargeIntX;
   189   val fromInt = Word32.fromLargeInt;
   190 end;
   191 
   192 structure LargeWord =
   193 struct
   194   open LargeWord;
   195   val wordSize = mk_int LargeWord.wordSize;
   196   val toInt = LargeWord.toLargeInt;
   197   val toIntX = LargeWord.toLargeIntX;
   198   val fromInt = LargeWord.fromLargeInt;
   199 end;
   200 
   201 
   202 (* Real *)
   203 
   204 structure Real =
   205 struct
   206   open Real;
   207   val radix = mk_int Real.radix;
   208   val precision = mk_int Real.precision;
   209   fun sign a = mk_int (Real.sign a);
   210   fun toManExp a = let val {man, exp} = Real.toManExp a in {man = man, exp = mk_int exp} end;
   211   fun fromManExp {man, exp} = Real.fromManExp {man = man, exp = dest_int exp};
   212   val ceil = mk_int o Real.ceil;
   213   val floor = mk_int o Real.floor;
   214   val real = Real.fromInt o dest_int;
   215   val round = mk_int o Real.round;
   216   val trunc = mk_int o Real.trunc;
   217   fun toInt a b = mk_int (Real.toInt a b);
   218   fun fromInt a = Real.fromInt (dest_int a);
   219   val fmt = StringCvt.realfmt;
   220 end;
   221 
   222 val ceil = Real.ceil;
   223 val floor = Real.floor;
   224 val real = Real.real;
   225 val round = Real.round;
   226 val trunc = Real.trunc;
   227 
   228 
   229 (* TextIO *)
   230 
   231 structure TextIO =
   232 struct
   233   open TextIO;
   234   fun inputN (a, b) = TextIO.inputN (a, dest_int b);
   235   fun canInput (a, b) = Option.map mk_int (TextIO.canInput (a, dest_int b));
   236 end;
   237 
   238 
   239 (* BinIO *)
   240 
   241 structure BinIO =
   242 struct
   243   open BinIO;
   244   fun inputN (a, b) = BinIO.inputN (a, dest_int b);
   245   fun canInput (a, b) = Option.map mk_int (BinIO.canInput (a, dest_int b));
   246 end;
   247 
   248 
   249 (* Time *)
   250 
   251 structure Time =
   252 struct
   253   open Time;
   254   fun fmt a b = Time.fmt (dest_int a) b;
   255 end;
   256 
   257 
   258 (* Sockets *)
   259 
   260 structure INetSock =
   261 struct
   262   open INetSock;
   263   fun toAddr (a, b) = INetSock.toAddr (a, dest_int b);
   264   fun fromAddr adr = let val (a, b) = INetSock.fromAddr adr in (a, mk_int b) end;
   265 end;
   266