src/Pure/basis.ML
author wenzelm
Fri, 28 Feb 1997 16:38:55 +0100
changeset 2692 484ec6ca0c50
parent 2470 273580d5c040
child 2862 3f38cbd57d47
permissions -rw-r--r--
added Syntax/token_trans.ML;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2402
b3d273ce5601 fixed comments;
wenzelm
parents: 2265
diff changeset
     1
(*  Title:      Pure/basis.ML
2217
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
     2
    ID:         $Id$
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
     4
    Copyright   1993  University of Cambridge
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
     5
2402
b3d273ce5601 fixed comments;
wenzelm
parents: 2265
diff changeset
     6
Basis Library emulation.
2217
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
     7
2402
b3d273ce5601 fixed comments;
wenzelm
parents: 2265
diff changeset
     8
Needed for Poly/ML and Standard ML of New Jersey version 0.93 to 1.08.
2217
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
     9
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    10
Full compatibility cannot be obtained using a file: what about char constants?
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    11
*)
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    12
2265
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    13
exception Subscript;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    14
2230
275a5a699ff7 Structure Bool and value Int.toString needed to replace makestring calls
paulson
parents: 2217
diff changeset
    15
structure Bool =
275a5a699ff7 Structure Bool and value Int.toString needed to replace makestring calls
paulson
parents: 2217
diff changeset
    16
  struct
275a5a699ff7 Structure Bool and value Int.toString needed to replace makestring calls
paulson
parents: 2217
diff changeset
    17
  fun toString true  = "true"
275a5a699ff7 Structure Bool and value Int.toString needed to replace makestring calls
paulson
parents: 2217
diff changeset
    18
    | toString false = "false"
275a5a699ff7 Structure Bool and value Int.toString needed to replace makestring calls
paulson
parents: 2217
diff changeset
    19
  end;
275a5a699ff7 Structure Bool and value Int.toString needed to replace makestring calls
paulson
parents: 2217
diff changeset
    20
2217
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    21
structure Int =
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    22
  struct
2230
275a5a699ff7 Structure Bool and value Int.toString needed to replace makestring calls
paulson
parents: 2217
diff changeset
    23
  fun toString (i: int) = makestring i;
2217
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    24
  fun max (x, y) = if x < y then y else x : int;
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    25
  fun min (x, y) = if x < y then x else y : int;
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    26
  end;
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    27
2265
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    28
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    29
structure List =
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    30
  struct
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    31
  exception Empty
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    32
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    33
  fun last []      = raise Empty
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    34
    | last [x]     = x
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    35
    | last (x::xs) = last xs;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    36
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    37
  fun nth (xs, n) =
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    38
      let fun h []      _ = raise Subscript
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    39
	    | h (x::xs) n = if n=0 then x else h xs (n-1)
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    40
      in if n<0 then raise Subscript else h xs n end;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    41
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    42
  fun drop (xs, n) =
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    43
      let fun h xs      0 = xs
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    44
	    | h []      n = raise Subscript
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    45
	    | h (x::xs) n = h xs (n-1)
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    46
      in if n<0 then raise Subscript else h xs n end;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    47
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    48
  fun take (xs, n) =
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    49
      let fun h xs      0 = []
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    50
	    | h []      n = raise Subscript
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    51
	    | h (x::xs) n = x :: h xs (n-1)
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    52
      in if n<0 then raise Subscript else h xs n end;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    53
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    54
  fun concat []      = []
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    55
    | concat (l::ls) = l @ concat ls;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    56
  end;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    57
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    58
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    59
structure ListPair =
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    60
  struct
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    61
  fun zip ([], [])      = []
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    62
    | zip (x::xs,y::ys) = (x,y) :: zip(xs,ys);
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    63
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    64
  fun unzip [] = ([],[])
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    65
    | unzip((x,y)::pairs) =
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    66
	  let val (xs,ys) = unzip pairs
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    67
	  in  (x::xs, y::ys)  end;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    68
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    69
  fun map f ([], [])      = []
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    70
    | map f (x::xs,y::ys) = f(x,y) :: map f (xs,ys);
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    71
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    72
  fun exists pred =
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    73
    let fun boolf ([], [])      = false
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    74
	  | boolf (x::xs,y::ys) = pred(x,y) orelse boolf (xs,ys)
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    75
    in boolf end;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    76
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    77
  fun all pred =
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    78
    let fun boolf ([], [])      = true
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    79
	  | boolf (x::xs,y::ys) = pred(x,y) andalso boolf (xs,ys)
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    80
    in boolf end;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    81
  end;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    82
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    83
2217
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    84
structure TextIO =
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    85
  struct
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    86
  type instream = instream
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    87
  and  outstream = outstream
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    88
  exception Io of {name: string, function: string, cause: exn}
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    89
  val stdIn 	= std_in
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    90
  val stdOut 	= std_out
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    91
  val openIn 	= open_in
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    92
  val openAppend = open_append
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    93
  val openOut 	= open_out
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    94
  val closeIn 	= close_in
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    95
  val closeOut 	= close_out
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    96
  val inputN 	= input
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    97
  val inputAll  = fn is => inputN (is, 999999)
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    98
  val inputLine = input_line
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    99
  val endOfStream = end_of_stream
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   100
  val output 	= output
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   101
  val flushOut 	= flush_out
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   102
  end;
2470
273580d5c040 A definition of "print", unfortunately overridden by each "open PolyML"
paulson
parents: 2402
diff changeset
   103
273580d5c040 A definition of "print", unfortunately overridden by each "open PolyML"
paulson
parents: 2402
diff changeset
   104
273580d5c040 A definition of "print", unfortunately overridden by each "open PolyML"
paulson
parents: 2402
diff changeset
   105
fun print s = (output (std_out, s); flush_out std_out);