src/Pure/basis.ML
author paulson
Tue, 22 Jul 1997 11:14:18 +0200
changeset 3538 ed9de44032e0
parent 3244 71b760618f30
child 5021 235f8508d440
permissions -rw-r--r--
Removal of the tactical STATE
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
3244
71b760618f30 Basis library version of type "option" now resides in its own structure Option
paulson
parents: 3047
diff changeset
    21
71b760618f30 Basis library version of type "option" now resides in its own structure Option
paulson
parents: 3047
diff changeset
    22
structure Option =
2862
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    23
  struct
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    24
  exception Option
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    25
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    26
  datatype 'a option = NONE | SOME of 'a
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    27
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    28
  fun getOpt (SOME v, _) = v
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    29
    | getOpt (NONE,   a) = a
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    30
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    31
  fun isSome (SOME _) = true 
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    32
    | isSome NONE     = false
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    33
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    34
  fun valOf (SOME v) = v
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    35
    | valOf NONE     = raise Option
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    36
  end;
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    37
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    38
2217
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    39
structure Int =
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    40
  struct
2230
275a5a699ff7 Structure Bool and value Int.toString needed to replace makestring calls
paulson
parents: 2217
diff changeset
    41
  fun toString (i: int) = makestring i;
2217
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    42
  fun max (x, y) = if x < y then y else x : int;
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    43
  fun min (x, y) = if x < y then x else y : int;
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    44
  end;
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
    45
2265
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    46
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    47
structure List =
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    48
  struct
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    49
  exception Empty
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    50
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    51
  fun last []      = raise Empty
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    52
    | last [x]     = x
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    53
    | last (x::xs) = last xs;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    54
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    55
  fun nth (xs, n) =
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    56
      let fun h []      _ = raise Subscript
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    57
	    | 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
    58
      in if n<0 then raise Subscript else h xs n end;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    59
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    60
  fun drop (xs, n) =
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    61
      let fun h xs      0 = xs
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    62
	    | h []      n = raise Subscript
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    63
	    | h (x::xs) n = h xs (n-1)
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    64
      in if n<0 then raise Subscript else h xs n end;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    65
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    66
  fun take (xs, n) =
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    67
      let fun h xs      0 = []
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    68
	    | h []      n = raise Subscript
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    69
	    | h (x::xs) n = x :: h xs (n-1)
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    70
      in if n<0 then raise Subscript else h xs n end;
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 concat []      = []
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    73
    | concat (l::ls) = l @ concat ls;
2862
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    74
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    75
  fun mapPartial f []      = []
3f38cbd57d47 Now declares Basis Library version of type option
paulson
parents: 2470
diff changeset
    76
    | mapPartial f (x::xs) = 
3244
71b760618f30 Basis library version of type "option" now resides in its own structure Option
paulson
parents: 3047
diff changeset
    77
         (case f x of Option.NONE   => mapPartial f xs
71b760618f30 Basis library version of type "option" now resides in its own structure Option
paulson
parents: 3047
diff changeset
    78
                    | Option.SOME y => y :: mapPartial f xs);
2884
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    79
3244
71b760618f30 Basis library version of type "option" now resides in its own structure Option
paulson
parents: 3047
diff changeset
    80
  fun find _ []        = Option.NONE
71b760618f30 Basis library version of type "option" now resides in its own structure Option
paulson
parents: 3047
diff changeset
    81
    | find p (x :: xs) = if p x then Option.SOME x else find p xs;
2884
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    82
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    83
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    84
  (*copy the list preserving elements that satisfy the predicate*)
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    85
  fun filter p [] = []
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    86
    | filter p (x :: xs) = if p x then x :: filter p xs else filter p xs;
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    87
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    88
  (*Partition list into elements that satisfy predicate and those that don't.
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    89
    Preserves order of elements in both lists.*)
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    90
  fun partition (p: 'a->bool) (ys: 'a list) : ('a list * 'a list) =
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    91
      let fun part ([], answer) = answer
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    92
	    | part (x::xs, (ys, ns)) = if p(x)
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    93
	      then  part (xs, (x::ys, ns))
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    94
	      else  part (xs, (ys, x::ns))
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    95
      in  part (rev ys, ([], []))  end;
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
    96
2265
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    97
  end;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    98
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
    99
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   100
structure ListPair =
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   101
  struct
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   102
  fun zip ([], [])      = []
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   103
    | zip (x::xs,y::ys) = (x,y) :: zip(xs,ys);
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   104
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   105
  fun unzip [] = ([],[])
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   106
    | unzip((x,y)::pairs) =
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   107
	  let val (xs,ys) = unzip pairs
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   108
	  in  (x::xs, y::ys)  end;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   109
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   110
  fun map f ([], [])      = []
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   111
    | 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
   112
2884
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
   113
  fun exists p =
2265
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   114
    let fun boolf ([], [])      = false
2884
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
   115
	  | boolf (x::xs,y::ys) = p(x,y) orelse boolf (xs,ys)
2265
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   116
    in boolf end;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   117
2884
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
   118
  fun all p =
2265
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   119
    let fun boolf ([], [])      = true
2884
4f2a4c27f9b5 More List and ListPair utilities
paulson
parents: 2862
diff changeset
   120
	  | boolf (x::xs,y::ys) = p(x,y) andalso boolf (xs,ys)
2265
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   121
    in boolf end;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   122
  end;
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   123
3123fef88dce Addition of structures List and ListPair
paulson
parents: 2230
diff changeset
   124
2217
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   125
structure TextIO =
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   126
  struct
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   127
  type instream = instream
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   128
  and  outstream = outstream
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   129
  exception Io of {name: string, function: string, cause: exn}
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   130
  val stdIn 	= std_in
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   131
  val stdOut 	= std_out
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   132
  val openIn 	= open_in
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   133
  val openAppend = open_append
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   134
  val openOut 	= open_out
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   135
  val closeIn 	= close_in
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   136
  val closeOut 	= close_out
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   137
  val inputN 	= input
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   138
  val inputAll  = fn is => inputN (is, 999999)
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   139
  val inputLine = input_line
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   140
  val endOfStream = end_of_stream
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   141
  val output 	= output
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   142
  val flushOut 	= flush_out
411f4683feb6 Basis library emulation for old ML compilers
paulson
parents:
diff changeset
   143
  end;
2470
273580d5c040 A definition of "print", unfortunately overridden by each "open PolyML"
paulson
parents: 2402
diff changeset
   144
273580d5c040 A definition of "print", unfortunately overridden by each "open PolyML"
paulson
parents: 2402
diff changeset
   145
273580d5c040 A definition of "print", unfortunately overridden by each "open PolyML"
paulson
parents: 2402
diff changeset
   146
fun print s = (output (std_out, s); flush_out std_out);