src/HOL/Tools/res_lib.ML
author nipkow
Fri, 05 Aug 2005 12:20:30 +0200
changeset 17022 b257300c3a9c
parent 16954 82d0a25c5a1d
child 17305 6cef3aedd661
permissions -rw-r--r--
added Brian Hufmann's finite instances
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15604
6fb06b768f67 code reformatted
webertj
parents: 15347
diff changeset
     1
(*  Title:      HOL/Tools/res_lib.ML
6fb06b768f67 code reformatted
webertj
parents: 15347
diff changeset
     2
    Author:     Jia Meng, Cambridge University Computer Laboratory
6fb06b768f67 code reformatted
webertj
parents: 15347
diff changeset
     3
    ID:         $Id$
6fb06b768f67 code reformatted
webertj
parents: 15347
diff changeset
     4
    Copyright   2004 University of Cambridge
15347
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
     5
15604
6fb06b768f67 code reformatted
webertj
parents: 15347
diff changeset
     6
Some auxiliary functions frequently used by files in this directory.
15347
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
     7
*)
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
     8
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
     9
signature RES_LIB =
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    10
sig
16515
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    11
val flat_noDup : ''a list list -> ''a list
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    12
val helper_path : string -> string -> string
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    13
val list2str_sep : string -> string list -> string
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    14
val list_to_string : string list -> string
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    15
val list_to_string' : string list -> string
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    16
val no_rep_add : ''a -> ''a list -> ''a list
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    17
val no_rep_app : ''a list -> ''a list -> ''a list
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    18
val pair_ins : 'a -> 'b list -> ('a * 'b) list
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    19
val rm_rep : ''a list -> ''a list
16954
82d0a25c5a1d new function trim_ends
paulson
parents: 16902
diff changeset
    20
val trim_ends : string -> string
16515
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    21
val write_strs : TextIO.outstream -> TextIO.vector list -> unit
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    22
val writeln_strs : TextIO.outstream -> TextIO.vector list -> unit
15347
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    23
end;
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    24
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    25
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    26
structure ResLib : RES_LIB =
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    27
struct
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    28
16954
82d0a25c5a1d new function trim_ends
paulson
parents: 16902
diff changeset
    29
(*Remove both ends from the string (presumably quotation marks?)*)
82d0a25c5a1d new function trim_ends
paulson
parents: 16902
diff changeset
    30
fun trim_ends s = String.substring(s,1,String.size s - 2);
82d0a25c5a1d new function trim_ends
paulson
parents: 16902
diff changeset
    31
82d0a25c5a1d new function trim_ends
paulson
parents: 16902
diff changeset
    32
16515
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    33
(* convert a list of strings into one single string; surrounded by brackets *)
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    34
fun list_to_string strings =
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    35
let
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    36
	fun str_of [s]      = s
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    37
	  | str_of (s1::ss) = s1 ^ "," ^ (str_of ss)
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    38
	  | str_of _        = ""
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    39
in
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    40
	"(" ^ str_of strings ^ ")"
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    41
end;
15347
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    42
16515
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    43
fun list_to_string' strings =
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    44
let
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    45
	fun str_of [s]      = s
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    46
	  | str_of (s1::ss) = s1 ^ "," ^ (str_of ss)
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    47
	  | str_of _        = ""
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    48
in
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    49
	"[" ^ str_of strings ^ "]"
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    50
end;
15347
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    51
16515
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    52
(* remove some chars (not allowed by TPTP format) from a string *)
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    53
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    54
fun no_rep_add x []     = [x]
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    55
  | no_rep_add x (y::z) = if x=y then y::z else y::(no_rep_add x z);
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    56
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    57
fun no_rep_app l1 []     = l1
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    58
  | no_rep_app l1 (x::y) = no_rep_app (no_rep_add x l1) y;
15347
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    59
16515
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    60
fun rm_rep []     = []
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    61
  | rm_rep (x::y) = if x mem y then rm_rep y else x::(rm_rep y);
15347
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    62
16515
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    63
fun flat_noDup []     = []
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    64
  | flat_noDup (x::y) = no_rep_app x (flat_noDup y);
15347
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    65
16515
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    66
fun list2str_sep delim []      = delim
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    67
  | list2str_sep delim (s::ss) = (s ^ delim) ^ (list2str_sep delim ss);
15347
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    68
16515
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    69
fun write_strs _   []      = ()
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    70
  | write_strs out (s::ss) = (TextIO.output (out, s); write_strs out ss);
15347
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    71
16515
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    72
fun writeln_strs _   []      = ()
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    73
  | writeln_strs out (s::ss) = (TextIO.output (out, s); TextIO.output (out, "\n"); writeln_strs out ss);
15347
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    74
16515
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    75
(* pair the first argument with each element in the second input list *)
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    76
fun pair_ins x []      = []
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    77
  | pair_ins x (y::ys) = (x, y) :: (pair_ins x ys);
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    78
  
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    79
(*Return the path to a "helper" like SPASS or tptp2X, first checking that
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    80
  it exists. --lcp *)  
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    81
fun helper_path evar base =
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    82
  case getenv evar of
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    83
      "" => error  ("Isabelle environment variable " ^ evar ^ " not defined")
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    84
    | home => 
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    85
        let val path = home ^ "/" ^ base
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    86
        in  if File.exists (File.unpack_platform_path path) then path 
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    87
	    else error ("Could not find the file " ^ path)
7896ea4f3a87 VAMPIRE_HOME, helper_path and various stylistic tweaks
paulson
parents: 15774
diff changeset
    88
	end;  
15347
14585bc8fa09 resolution package tools by Jia Meng
paulson
parents:
diff changeset
    89
15604
6fb06b768f67 code reformatted
webertj
parents: 15347
diff changeset
    90
end;