author | blanchet |
Mon, 21 May 2012 10:39:31 +0200 | |
changeset 47944 | e6b51fab96f7 |
parent 47527 | b0a7d235b23b |
child 53384 | 63034189f9ec |
permissions | -rw-r--r-- |
46844 | 1 |
(* Title: HOL/TPTP/TPTP_Parser/tptp_problem_name.ML |
2 |
Author: Nik Sultana, Cambridge University Computer Laboratory |
|
3 |
||
4 |
Scans a TPTP problem name. Naming convention is described |
|
5 |
http://www.cs.miami.edu/~tptp/TPTP/TR/TPTPTR.shtml#Problem and Axiomatization Naming |
|
6 |
*) |
|
7 |
||
8 |
signature TPTP_PROBLEM_NAME = |
|
9 |
sig |
|
10 |
datatype suffix = |
|
11 |
Problem of |
|
12 |
((*version*)int * |
|
13 |
(*size parameter*)int option) * |
|
14 |
(*extension*)string |
|
15 |
| Axiom of |
|
16 |
(*specialisation*)int * |
|
17 |
(*extension*)string |
|
18 |
||
19 |
type tptp_problem_name = |
|
20 |
{prob_domain : string, |
|
21 |
prob_number : int, |
|
22 |
prob_form : TPTP_Syntax.language, |
|
23 |
suffix : suffix} |
|
24 |
||
25 |
datatype problem_name = |
|
26 |
Standard of tptp_problem_name |
|
27 |
| Nonstandard of string |
|
28 |
||
29 |
exception TPTP_PROBLEM_NAME of string |
|
30 |
||
31 |
val parse_problem_name : string -> problem_name |
|
32 |
val mangle_problem_name : problem_name -> string |
|
33 |
end |
|
34 |
||
35 |
structure TPTP_Problem_Name: TPTP_PROBLEM_NAME = |
|
36 |
struct |
|
37 |
||
38 |
(*some basic tokens*) |
|
39 |
val numerics = map Char.chr (48 upto 57) (*0..9*) |
|
40 |
val alphabetics = |
|
41 |
map Char.chr (65 upto 90) @ (*A..Z*) |
|
42 |
map Char.chr (97 upto 122) (*a..z*) |
|
43 |
(*TPTP formula forms*) |
|
44 |
val forms = [#"^", #"_", #"=", #"+", #"-"] |
|
45 |
||
46 |
(*lift a list of characters into a scanner combinator matching any one of the |
|
47 |
characters in that list.*) |
|
48 |
fun lift l = |
|
49 |
(map (Char.toString #> ($$)) l, Scan.fail) |
|
50 |
|-> fold (fn x => fn y => x || y) |
|
51 |
||
52 |
(*combinators for parsing letters and numbers*) |
|
53 |
val alpha = lift alphabetics |
|
54 |
val numer = lift numerics |
|
55 |
||
56 |
datatype suffix = |
|
57 |
Problem of |
|
58 |
((*version*)int * |
|
47520 | 59 |
(*size parameter*)int option) * |
46844 | 60 |
(*extension*)string |
61 |
| Axiom of |
|
62 |
(*specialisation*)int * |
|
63 |
(*extension*)string |
|
64 |
||
65 |
val to_int = Int.fromString #> the |
|
66 |
val rm_ending = Scan.this_string "rm" |
|
67 |
val ax_ending = |
|
68 |
((numer >> to_int) --| |
|
69 |
$$ "." -- (Scan.this_string "eq" || Scan.this_string "ax" || rm_ending)) |
|
70 |
>> Axiom |
|
71 |
val prob_ending = $$ "p" || $$ "g" || rm_ending |
|
72 |
val prob_suffix = |
|
73 |
((numer >> to_int) -- |
|
74 |
Scan.option ($$ "." |-- numer ^^ numer ^^ numer >> to_int) --| $$ "." |
|
75 |
-- prob_ending) |
|
76 |
>> Problem |
|
77 |
||
78 |
type tptp_problem_name = |
|
79 |
{prob_domain : string, |
|
80 |
prob_number : int, |
|
81 |
prob_form : TPTP_Syntax.language, |
|
82 |
suffix : suffix} |
|
83 |
||
84 |
datatype problem_name = |
|
85 |
Standard of tptp_problem_name |
|
86 |
| Nonstandard of string |
|
87 |
||
88 |
exception TPTP_PROBLEM_NAME of string |
|
89 |
||
90 |
fun parse_problem_name str' : problem_name = |
|
91 |
let |
|
92 |
val str = Symbol.explode str' |
|
93 |
(*NOTE there's an ambiguity in the spec: there's no way of knowing if a |
|
94 |
file ending in "rm" used to be "ax" or "p". Here we default to "p".*) |
|
47527
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
95 |
val (parsed_name, rest) = |
47520 | 96 |
Scan.finite Symbol.stopper |
47527
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
97 |
(((alpha ^^ alpha ^^ alpha) -- |
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
98 |
(numer ^^ numer ^^ numer >> to_int) -- |
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
99 |
lift forms -- (prob_suffix || ax_ending)) >> SOME |
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
100 |
|| Scan.succeed NONE) str |
46844 | 101 |
|
102 |
fun parse_form str = |
|
103 |
case str of |
|
104 |
"^" => TPTP_Syntax.THF |
|
105 |
| "_" => TPTP_Syntax.TFF |
|
106 |
| "=" => TPTP_Syntax.TFF_with_arithmetic |
|
107 |
| "+" => TPTP_Syntax.FOF |
|
108 |
| "-" => TPTP_Syntax.CNF |
|
109 |
| _ => raise TPTP_PROBLEM_NAME ("Unknown TPTP form: " ^ str) |
|
110 |
in |
|
47527
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
111 |
if not (null rest) orelse is_none parsed_name then Nonstandard str' |
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
112 |
else |
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
113 |
let |
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
114 |
val (((prob_domain, prob_number), prob_form), suffix) = |
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
115 |
the parsed_name |
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
116 |
in |
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
117 |
Standard |
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
118 |
{prob_domain = prob_domain, |
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
119 |
prob_number = prob_number, |
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
120 |
prob_form = parse_form prob_form, |
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
121 |
suffix = suffix} |
b0a7d235b23b
tidied exception-handling relating to tptp problem names;
sultana
parents:
47520
diff
changeset
|
122 |
end |
46844 | 123 |
end |
124 |
||
125 |
(*Produces an ASCII encoding of a TPTP problem-file name.*) |
|
126 |
fun mangle_problem_name (prob : problem_name) : string = |
|
127 |
case prob of |
|
47520 | 128 |
Standard tptp_prob => |
129 |
let |
|
130 |
val prob_form = |
|
131 |
case #prob_form tptp_prob of |
|
132 |
TPTP_Syntax.THF => "_thf_" |
|
133 |
| TPTP_Syntax.TFF => "_tff_" |
|
134 |
| TPTP_Syntax.TFF_with_arithmetic => "_thfwa_" |
|
135 |
| TPTP_Syntax.FOF => "_fof_" |
|
136 |
| TPTP_Syntax.CNF => "_cnf_" |
|
137 |
val suffix = |
|
138 |
case #suffix tptp_prob of |
|
139 |
Problem ((version, size), extension) => |
|
140 |
Int.toString version ^ "_" ^ |
|
141 |
(if is_some size then Int.toString (the size) ^ "_" else "") ^ |
|
142 |
extension |
|
143 |
| Axiom (specialisation, extension) => |
|
144 |
Int.toString specialisation ^ "_" ^ extension |
|
145 |
in |
|
146 |
#prob_domain tptp_prob ^ |
|
147 |
Int.toString (#prob_number tptp_prob) ^ |
|
148 |
prob_form ^ |
|
149 |
suffix |
|
150 |
end |
|
151 |
| Nonstandard str => str |
|
46844 | 152 |
|
153 |
end |