src/Pure/POLY.ML
author clasohm
Mon, 15 Jan 1996 14:47:56 +0100
changeset 1436 88b73ad6b0da
parent 1434 834da5152421
child 1437 2ebbc23d49fa
permissions -rw-r--r--
fixed bug in file_info
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     1
(*  Title:      Pure/POLY
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
    Copyright   1991  University of Cambridge
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
Compatibility file for Poly/ML (AHL release 1.88)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     9
open PolyML ExtendedIO;
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
(*A conditional timing function: applies f to () and, if the flag is true,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    13
  prints its runtime.*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    15
fun cond_timeit flag f =
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    16
  if flag then
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
    let val before = System.processtime()
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
        val result = f()
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
        val both = real(System.processtime() - before) / 10.0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    20
    in  output(std_out, "Process time (incl GC): "^
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
                         makestring both ^ " secs\n");
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
        result
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    23
    end
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    24
  else f();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    26
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    27
(*Save function: in Poly/ML, ignores filename and commits to present file*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
fun xML filename banner = commit();
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    30
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    31
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
(*Interface for toplevel pretty printers, see also Pure/install_pp.ML*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    33
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    34
fun make_pp _ pprint (str, blk, brk, en) obj =
19
929ad32d63fc Pure/ROOT.ML
wenzelm
parents: 0
diff changeset
    35
  pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
929ad32d63fc Pure/ROOT.ML
wenzelm
parents: 0
diff changeset
    36
    fn () => brk (99999, 0), en);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    37
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    38
66
1b14cd923772 /NJ,POLY/delete_file: new
lcp
parents: 58
diff changeset
    39
(*** File handling ***)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
100
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    41
local
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    42
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    43
(*These are functions from library.ML *)
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    44
fun take_suffix _ [] = ([], [])
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    45
  | take_suffix pred (x :: xs) =
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    46
      (case take_suffix pred xs of
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    47
         ([], sffx) => if pred x then ([], x :: sffx)
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    48
                                 else ([x], sffx)
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    49
       | (prfx, sffx) => (x :: prfx, sffx));
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    50
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    51
fun apr (f,y) x = f(x,y);
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    52
396
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
    53
fun seq (proc: 'a -> unit) : 'a list -> unit =
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
    54
  let fun seqf [] = ()
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
    55
        | seqf (x :: xs) = (proc x; seqf xs)
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
    56
  in seqf end;
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
    57
100
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    58
in
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    59
58
b30802dfbe80 file_info now returns a string that does not contain the path/filename
clasohm
parents: 19
diff changeset
    60
(*Get a string containing the time of last modification, attributes, owner
b30802dfbe80 file_info now returns a string that does not contain the path/filename
clasohm
parents: 19
diff changeset
    61
  and size of file (but not the path) *)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
fun file_info "" = ""
1436
88b73ad6b0da fixed bug in file_info
clasohm
parents: 1434
diff changeset
    63
  | file_info name =
88b73ad6b0da fixed bug in file_info
clasohm
parents: 1434
diff changeset
    64
    let fun radixpand num : int list =
88b73ad6b0da fixed bug in file_info
clasohm
parents: 1434
diff changeset
    65
          let fun radix (n, tail) =
88b73ad6b0da fixed bug in file_info
clasohm
parents: 1434
diff changeset
    66
            if n < 10 then n :: tail
88b73ad6b0da fixed bug in file_info
clasohm
parents: 1434
diff changeset
    67
            else radix (n div 10, (n mod 10) :: tail)
88b73ad6b0da fixed bug in file_info
clasohm
parents: 1434
diff changeset
    68
          in radix (num, []) end;
88b73ad6b0da fixed bug in file_info
clasohm
parents: 1434
diff changeset
    69
88b73ad6b0da fixed bug in file_info
clasohm
parents: 1434
diff changeset
    70
        fun radixstring num =
88b73ad6b0da fixed bug in file_info
clasohm
parents: 1434
diff changeset
    71
          let val offset = ord "0";
88b73ad6b0da fixed bug in file_info
clasohm
parents: 1434
diff changeset
    72
              fun chrof n = chr (offset + n);
88b73ad6b0da fixed bug in file_info
clasohm
parents: 1434
diff changeset
    73
          in implode (map chrof (radixpand num)) end;
88b73ad6b0da fixed bug in file_info
clasohm
parents: 1434
diff changeset
    74
     in radixstring (System.filedate name) end;
66
1b14cd923772 /NJ,POLY/delete_file: new
lcp
parents: 58
diff changeset
    75
73
075db6ac7f2f delete_file now has type string -> unit in both NJ and POLY,
clasohm
parents: 66
diff changeset
    76
(*Delete a file *)
075db6ac7f2f delete_file now has type string -> unit in both NJ and POLY,
clasohm
parents: 66
diff changeset
    77
fun delete_file name =
100
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    78
  let val (is, os) = ExtendedIO.execute ("rm " ^ name)
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    79
  in close_in is;
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    80
     close_out os
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    81
  end;
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    82
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    83
(*Get pathname of current working directory *)
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    84
fun pwd () =
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    85
  let val (is, os) = ExtendedIO.execute ("pwd")
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    86
      val (path, _) = take_suffix (apr(op=,"\n"))
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    87
                       (explode (ExtendedIO.input_line is)) (*remove newline *)
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    88
  in close_in is;
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    89
     close_out os;
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    90
     implode path
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    91
  end;
e95b98536b3d fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents: 73
diff changeset
    92
378
85ff48546a05 added use_string: string -> unit to execute ML commands passed in a string
clasohm
parents: 100
diff changeset
    93
85ff48546a05 added use_string: string -> unit to execute ML commands passed in a string
clasohm
parents: 100
diff changeset
    94
(*** ML command execution ***)
85ff48546a05 added use_string: string -> unit to execute ML commands passed in a string
clasohm
parents: 100
diff changeset
    95
396
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
    96
val use_string =
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
    97
  let fun exec command =
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
    98
    let val firstLine = ref true;
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
    99
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
   100
        fun getLine () =
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
   101
          if !firstLine
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
   102
          then (firstLine := false; command)
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
   103
          else raise Io "use_string: buffer exhausted"
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
   104
    in PolyML.compiler (getLine, fn s => output (std_out, s)) () end
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
   105
  in seq exec end;
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
   106
18c9c28d0f7e changed use_string's type to string list -> unit because POLY can only
clasohm
parents: 378
diff changeset
   107
end;
1289
2edd7a39d92a added "execute"
clasohm
parents: 396
diff changeset
   108
2edd7a39d92a added "execute"
clasohm
parents: 396
diff changeset
   109
2edd7a39d92a added "execute"
clasohm
parents: 396
diff changeset
   110
(*** System command execution ***)
2edd7a39d92a added "execute"
clasohm
parents: 396
diff changeset
   111
2edd7a39d92a added "execute"
clasohm
parents: 396
diff changeset
   112
(*Execute an Unix command which doesn't take any input from stdin and
2edd7a39d92a added "execute"
clasohm
parents: 396
diff changeset
   113
  sends its output to stdout.*)
2edd7a39d92a added "execute"
clasohm
parents: 396
diff changeset
   114
fun execute command =
2edd7a39d92a added "execute"
clasohm
parents: 396
diff changeset
   115
  let val (is, os) =  ExtendedIO.execute command;
2edd7a39d92a added "execute"
clasohm
parents: 396
diff changeset
   116
      val result = input (is, 999999);
2edd7a39d92a added "execute"
clasohm
parents: 396
diff changeset
   117
  in close_out os;
2edd7a39d92a added "execute"
clasohm
parents: 396
diff changeset
   118
     close_in is;
2edd7a39d92a added "execute"
clasohm
parents: 396
diff changeset
   119
     result
2edd7a39d92a added "execute"
clasohm
parents: 396
diff changeset
   120
  end;