39348
|
1 |
(* ========================================================================= *)
|
|
2 |
(* FIRST ORDER LOGIC TERMS *)
|
39349
|
3 |
(* Copyright (c) 2001 Joe Hurd, distributed under the BSD License *)
|
39348
|
4 |
(* ========================================================================= *)
|
|
5 |
|
|
6 |
structure Term :> Term =
|
|
7 |
struct
|
|
8 |
|
|
9 |
open Useful;
|
|
10 |
|
|
11 |
(* ------------------------------------------------------------------------- *)
|
|
12 |
(* A type of first order logic terms. *)
|
|
13 |
(* ------------------------------------------------------------------------- *)
|
|
14 |
|
|
15 |
type var = Name.name;
|
|
16 |
|
|
17 |
type functionName = Name.name;
|
|
18 |
|
|
19 |
type function = functionName * int;
|
|
20 |
|
|
21 |
type const = functionName;
|
|
22 |
|
|
23 |
datatype term =
|
|
24 |
Var of Name.name
|
|
25 |
| Fn of Name.name * term list;
|
|
26 |
|
|
27 |
(* ------------------------------------------------------------------------- *)
|
|
28 |
(* Constructors and destructors. *)
|
|
29 |
(* ------------------------------------------------------------------------- *)
|
|
30 |
|
|
31 |
(* Variables *)
|
|
32 |
|
|
33 |
fun destVar (Var v) = v
|
|
34 |
| destVar (Fn _) = raise Error "destVar";
|
|
35 |
|
|
36 |
val isVar = can destVar;
|
|
37 |
|
|
38 |
fun equalVar v (Var v') = Name.equal v v'
|
|
39 |
| equalVar _ _ = false;
|
|
40 |
|
|
41 |
(* Functions *)
|
|
42 |
|
|
43 |
fun destFn (Fn f) = f
|
|
44 |
| destFn (Var _) = raise Error "destFn";
|
|
45 |
|
|
46 |
val isFn = can destFn;
|
|
47 |
|
|
48 |
fun fnName tm = fst (destFn tm);
|
|
49 |
|
|
50 |
fun fnArguments tm = snd (destFn tm);
|
|
51 |
|
|
52 |
fun fnArity tm = length (fnArguments tm);
|
|
53 |
|
|
54 |
fun fnFunction tm = (fnName tm, fnArity tm);
|
|
55 |
|
|
56 |
local
|
|
57 |
fun func fs [] = fs
|
|
58 |
| func fs (Var _ :: tms) = func fs tms
|
|
59 |
| func fs (Fn (n,l) :: tms) =
|
|
60 |
func (NameAritySet.add fs (n, length l)) (l @ tms);
|
|
61 |
in
|
|
62 |
fun functions tm = func NameAritySet.empty [tm];
|
|
63 |
end;
|
|
64 |
|
|
65 |
local
|
|
66 |
fun func fs [] = fs
|
|
67 |
| func fs (Var _ :: tms) = func fs tms
|
|
68 |
| func fs (Fn (n,l) :: tms) = func (NameSet.add fs n) (l @ tms);
|
|
69 |
in
|
|
70 |
fun functionNames tm = func NameSet.empty [tm];
|
|
71 |
end;
|
|
72 |
|
|
73 |
(* Constants *)
|
|
74 |
|
|
75 |
fun mkConst c = (Fn (c, []));
|
|
76 |
|
|
77 |
fun destConst (Fn (c, [])) = c
|
|
78 |
| destConst _ = raise Error "destConst";
|
|
79 |
|
|
80 |
val isConst = can destConst;
|
|
81 |
|
|
82 |
(* Binary functions *)
|
|
83 |
|
|
84 |
fun mkBinop f (a,b) = Fn (f,[a,b]);
|
|
85 |
|
|
86 |
fun destBinop f (Fn (x,[a,b])) =
|
|
87 |
if Name.equal x f then (a,b) else raise Error "Term.destBinop: wrong binop"
|
|
88 |
| destBinop _ _ = raise Error "Term.destBinop: not a binop";
|
|
89 |
|
|
90 |
fun isBinop f = can (destBinop f);
|
|
91 |
|
|
92 |
(* ------------------------------------------------------------------------- *)
|
|
93 |
(* The size of a term in symbols. *)
|
|
94 |
(* ------------------------------------------------------------------------- *)
|
|
95 |
|
|
96 |
val VAR_SYMBOLS = 1;
|
|
97 |
|
|
98 |
val FN_SYMBOLS = 1;
|
|
99 |
|
|
100 |
local
|
|
101 |
fun sz n [] = n
|
|
102 |
| sz n (Var _ :: tms) = sz (n + VAR_SYMBOLS) tms
|
|
103 |
| sz n (Fn (func,args) :: tms) = sz (n + FN_SYMBOLS) (args @ tms);
|
|
104 |
in
|
|
105 |
fun symbols tm = sz 0 [tm];
|
|
106 |
end;
|
|
107 |
|
|
108 |
(* ------------------------------------------------------------------------- *)
|
|
109 |
(* A total comparison function for terms. *)
|
|
110 |
(* ------------------------------------------------------------------------- *)
|
|
111 |
|
|
112 |
local
|
|
113 |
fun cmp [] [] = EQUAL
|
|
114 |
| cmp (tm1 :: tms1) (tm2 :: tms2) =
|
|
115 |
let
|
|
116 |
val tm1_tm2 = (tm1,tm2)
|
|
117 |
in
|
|
118 |
if Portable.pointerEqual tm1_tm2 then cmp tms1 tms2
|
|
119 |
else
|
|
120 |
case tm1_tm2 of
|
|
121 |
(Var v1, Var v2) =>
|
|
122 |
(case Name.compare (v1,v2) of
|
|
123 |
LESS => LESS
|
|
124 |
| EQUAL => cmp tms1 tms2
|
|
125 |
| GREATER => GREATER)
|
|
126 |
| (Var _, Fn _) => LESS
|
|
127 |
| (Fn _, Var _) => GREATER
|
|
128 |
| (Fn (f1,a1), Fn (f2,a2)) =>
|
|
129 |
(case Name.compare (f1,f2) of
|
|
130 |
LESS => LESS
|
|
131 |
| EQUAL =>
|
|
132 |
(case Int.compare (length a1, length a2) of
|
|
133 |
LESS => LESS
|
|
134 |
| EQUAL => cmp (a1 @ tms1) (a2 @ tms2)
|
|
135 |
| GREATER => GREATER)
|
|
136 |
| GREATER => GREATER)
|
|
137 |
end
|
|
138 |
| cmp _ _ = raise Bug "Term.compare";
|
|
139 |
in
|
|
140 |
fun compare (tm1,tm2) = cmp [tm1] [tm2];
|
|
141 |
end;
|
|
142 |
|
|
143 |
fun equal tm1 tm2 = compare (tm1,tm2) = EQUAL;
|
|
144 |
|
|
145 |
(* ------------------------------------------------------------------------- *)
|
|
146 |
(* Subterms. *)
|
|
147 |
(* ------------------------------------------------------------------------- *)
|
|
148 |
|
|
149 |
type path = int list;
|
|
150 |
|
|
151 |
fun subterm tm [] = tm
|
|
152 |
| subterm (Var _) (_ :: _) = raise Error "Term.subterm: Var"
|
|
153 |
| subterm (Fn (_,tms)) (h :: t) =
|
|
154 |
if h >= length tms then raise Error "Term.replace: Fn"
|
|
155 |
else subterm (List.nth (tms,h)) t;
|
|
156 |
|
|
157 |
local
|
|
158 |
fun subtms [] acc = acc
|
|
159 |
| subtms ((path,tm) :: rest) acc =
|
|
160 |
let
|
|
161 |
fun f (n,arg) = (n :: path, arg)
|
|
162 |
|
|
163 |
val acc = (rev path, tm) :: acc
|
|
164 |
in
|
|
165 |
case tm of
|
|
166 |
Var _ => subtms rest acc
|
|
167 |
| Fn (_,args) => subtms (map f (enumerate args) @ rest) acc
|
|
168 |
end;
|
|
169 |
in
|
|
170 |
fun subterms tm = subtms [([],tm)] [];
|
|
171 |
end;
|
|
172 |
|
|
173 |
fun replace tm ([],res) = if equal res tm then tm else res
|
|
174 |
| replace tm (h :: t, res) =
|
|
175 |
case tm of
|
|
176 |
Var _ => raise Error "Term.replace: Var"
|
|
177 |
| Fn (func,tms) =>
|
|
178 |
if h >= length tms then raise Error "Term.replace: Fn"
|
|
179 |
else
|
|
180 |
let
|
|
181 |
val arg = List.nth (tms,h)
|
|
182 |
val arg' = replace arg (t,res)
|
|
183 |
in
|
|
184 |
if Portable.pointerEqual (arg',arg) then tm
|
|
185 |
else Fn (func, updateNth (h,arg') tms)
|
|
186 |
end;
|
|
187 |
|
|
188 |
fun find pred =
|
|
189 |
let
|
|
190 |
fun search [] = NONE
|
|
191 |
| search ((path,tm) :: rest) =
|
|
192 |
if pred tm then SOME (rev path)
|
|
193 |
else
|
|
194 |
case tm of
|
|
195 |
Var _ => search rest
|
|
196 |
| Fn (_,a) =>
|
|
197 |
let
|
|
198 |
val subtms = map (fn (i,t) => (i :: path, t)) (enumerate a)
|
|
199 |
in
|
|
200 |
search (subtms @ rest)
|
|
201 |
end
|
|
202 |
in
|
|
203 |
fn tm => search [([],tm)]
|
|
204 |
end;
|
|
205 |
|
|
206 |
val ppPath = Print.ppList Print.ppInt;
|
|
207 |
|
|
208 |
val pathToString = Print.toString ppPath;
|
|
209 |
|
|
210 |
(* ------------------------------------------------------------------------- *)
|
|
211 |
(* Free variables. *)
|
|
212 |
(* ------------------------------------------------------------------------- *)
|
|
213 |
|
|
214 |
local
|
|
215 |
fun free _ [] = false
|
|
216 |
| free v (Var w :: tms) = Name.equal v w orelse free v tms
|
|
217 |
| free v (Fn (_,args) :: tms) = free v (args @ tms);
|
|
218 |
in
|
|
219 |
fun freeIn v tm = free v [tm];
|
|
220 |
end;
|
|
221 |
|
|
222 |
local
|
|
223 |
fun free vs [] = vs
|
|
224 |
| free vs (Var v :: tms) = free (NameSet.add vs v) tms
|
|
225 |
| free vs (Fn (_,args) :: tms) = free vs (args @ tms);
|
|
226 |
in
|
|
227 |
val freeVarsList = free NameSet.empty;
|
|
228 |
|
|
229 |
fun freeVars tm = freeVarsList [tm];
|
|
230 |
end;
|
|
231 |
|
|
232 |
(* ------------------------------------------------------------------------- *)
|
|
233 |
(* Fresh variables. *)
|
|
234 |
(* ------------------------------------------------------------------------- *)
|
|
235 |
|
|
236 |
fun newVar () = Var (Name.newName ());
|
|
237 |
|
|
238 |
fun newVars n = map Var (Name.newNames n);
|
|
239 |
|
|
240 |
local
|
|
241 |
fun avoidAcceptable avoid n = not (NameSet.member n avoid);
|
|
242 |
in
|
|
243 |
fun variantPrime avoid = Name.variantPrime (avoidAcceptable avoid);
|
|
244 |
|
|
245 |
fun variantNum avoid = Name.variantNum (avoidAcceptable avoid);
|
|
246 |
end;
|
|
247 |
|
|
248 |
(* ------------------------------------------------------------------------- *)
|
|
249 |
(* Special support for terms with type annotations. *)
|
|
250 |
(* ------------------------------------------------------------------------- *)
|
|
251 |
|
|
252 |
val hasTypeFunctionName = Name.fromString ":";
|
|
253 |
|
|
254 |
val hasTypeFunction = (hasTypeFunctionName,2);
|
|
255 |
|
|
256 |
fun destFnHasType ((f,a) : functionName * term list) =
|
|
257 |
if not (Name.equal f hasTypeFunctionName) then
|
|
258 |
raise Error "Term.destFnHasType"
|
|
259 |
else
|
|
260 |
case a of
|
|
261 |
[tm,ty] => (tm,ty)
|
|
262 |
| _ => raise Error "Term.destFnHasType";
|
|
263 |
|
|
264 |
val isFnHasType = can destFnHasType;
|
|
265 |
|
|
266 |
fun isTypedVar tm =
|
|
267 |
case tm of
|
|
268 |
Var _ => true
|
|
269 |
| Fn func =>
|
|
270 |
case total destFnHasType func of
|
|
271 |
SOME (Var _, _) => true
|
|
272 |
| _ => false;
|
|
273 |
|
|
274 |
local
|
|
275 |
fun sz n [] = n
|
|
276 |
| sz n (tm :: tms) =
|
|
277 |
case tm of
|
|
278 |
Var _ => sz (n + 1) tms
|
|
279 |
| Fn func =>
|
|
280 |
case total destFnHasType func of
|
|
281 |
SOME (tm,_) => sz n (tm :: tms)
|
|
282 |
| NONE =>
|
|
283 |
let
|
|
284 |
val (_,a) = func
|
|
285 |
in
|
|
286 |
sz (n + 1) (a @ tms)
|
|
287 |
end;
|
|
288 |
in
|
|
289 |
fun typedSymbols tm = sz 0 [tm];
|
|
290 |
end;
|
|
291 |
|
|
292 |
local
|
|
293 |
fun subtms [] acc = acc
|
|
294 |
| subtms ((path,tm) :: rest) acc =
|
|
295 |
case tm of
|
|
296 |
Var _ => subtms rest acc
|
|
297 |
| Fn func =>
|
|
298 |
case total destFnHasType func of
|
|
299 |
SOME (t,_) =>
|
|
300 |
(case t of
|
|
301 |
Var _ => subtms rest acc
|
|
302 |
| Fn _ =>
|
|
303 |
let
|
|
304 |
val acc = (rev path, tm) :: acc
|
|
305 |
val rest = (0 :: path, t) :: rest
|
|
306 |
in
|
|
307 |
subtms rest acc
|
|
308 |
end)
|
|
309 |
| NONE =>
|
|
310 |
let
|
|
311 |
fun f (n,arg) = (n :: path, arg)
|
|
312 |
|
|
313 |
val (_,args) = func
|
|
314 |
|
|
315 |
val acc = (rev path, tm) :: acc
|
|
316 |
|
|
317 |
val rest = map f (enumerate args) @ rest
|
|
318 |
in
|
|
319 |
subtms rest acc
|
|
320 |
end;
|
|
321 |
in
|
|
322 |
fun nonVarTypedSubterms tm = subtms [([],tm)] [];
|
|
323 |
end;
|
|
324 |
|
|
325 |
(* ------------------------------------------------------------------------- *)
|
|
326 |
(* Special support for terms with an explicit function application operator. *)
|
|
327 |
(* ------------------------------------------------------------------------- *)
|
|
328 |
|
|
329 |
val appName = Name.fromString ".";
|
|
330 |
|
|
331 |
fun mkFnApp (fTm,aTm) = (appName, [fTm,aTm]);
|
|
332 |
|
|
333 |
fun mkApp f_a = Fn (mkFnApp f_a);
|
|
334 |
|
|
335 |
fun destFnApp ((f,a) : Name.name * term list) =
|
|
336 |
if not (Name.equal f appName) then raise Error "Term.destFnApp"
|
|
337 |
else
|
|
338 |
case a of
|
|
339 |
[fTm,aTm] => (fTm,aTm)
|
|
340 |
| _ => raise Error "Term.destFnApp";
|
|
341 |
|
|
342 |
val isFnApp = can destFnApp;
|
|
343 |
|
|
344 |
fun destApp tm =
|
|
345 |
case tm of
|
|
346 |
Var _ => raise Error "Term.destApp"
|
|
347 |
| Fn func => destFnApp func;
|
|
348 |
|
|
349 |
val isApp = can destApp;
|
|
350 |
|
|
351 |
fun listMkApp (f,l) = foldl mkApp f l;
|
|
352 |
|
|
353 |
local
|
|
354 |
fun strip tms tm =
|
|
355 |
case total destApp tm of
|
|
356 |
SOME (f,a) => strip (a :: tms) f
|
|
357 |
| NONE => (tm,tms);
|
|
358 |
in
|
|
359 |
fun stripApp tm = strip [] tm;
|
|
360 |
end;
|
|
361 |
|
|
362 |
(* ------------------------------------------------------------------------- *)
|
|
363 |
(* Parsing and pretty printing. *)
|
|
364 |
(* ------------------------------------------------------------------------- *)
|
|
365 |
|
|
366 |
(* Operators parsed and printed infix *)
|
|
367 |
|
|
368 |
val infixes =
|
|
369 |
(ref o Print.Infixes)
|
|
370 |
[(* ML symbols *)
|
|
371 |
{token = " / ", precedence = 7, leftAssoc = true},
|
|
372 |
{token = " div ", precedence = 7, leftAssoc = true},
|
|
373 |
{token = " mod ", precedence = 7, leftAssoc = true},
|
|
374 |
{token = " * ", precedence = 7, leftAssoc = true},
|
|
375 |
{token = " + ", precedence = 6, leftAssoc = true},
|
|
376 |
{token = " - ", precedence = 6, leftAssoc = true},
|
|
377 |
{token = " ^ ", precedence = 6, leftAssoc = true},
|
|
378 |
{token = " @ ", precedence = 5, leftAssoc = false},
|
|
379 |
{token = " :: ", precedence = 5, leftAssoc = false},
|
|
380 |
{token = " = ", precedence = 4, leftAssoc = true},
|
|
381 |
{token = " <> ", precedence = 4, leftAssoc = true},
|
|
382 |
{token = " <= ", precedence = 4, leftAssoc = true},
|
|
383 |
{token = " < ", precedence = 4, leftAssoc = true},
|
|
384 |
{token = " >= ", precedence = 4, leftAssoc = true},
|
|
385 |
{token = " > ", precedence = 4, leftAssoc = true},
|
|
386 |
{token = " o ", precedence = 3, leftAssoc = true},
|
|
387 |
{token = " -> ", precedence = 2, leftAssoc = false}, (* inferred prec *)
|
|
388 |
{token = " : ", precedence = 1, leftAssoc = false}, (* inferred prec *)
|
|
389 |
{token = ", ", precedence = 0, leftAssoc = false}, (* inferred prec *)
|
|
390 |
|
|
391 |
(* Logical connectives *)
|
|
392 |
{token = " /\\ ", precedence = ~1, leftAssoc = false},
|
|
393 |
{token = " \\/ ", precedence = ~2, leftAssoc = false},
|
|
394 |
{token = " ==> ", precedence = ~3, leftAssoc = false},
|
|
395 |
{token = " <=> ", precedence = ~4, leftAssoc = false},
|
|
396 |
|
|
397 |
(* Other symbols *)
|
|
398 |
{token = " . ", precedence = 9, leftAssoc = true}, (* function app *)
|
|
399 |
{token = " ** ", precedence = 8, leftAssoc = true},
|
|
400 |
{token = " ++ ", precedence = 6, leftAssoc = true},
|
|
401 |
{token = " -- ", precedence = 6, leftAssoc = true},
|
|
402 |
{token = " == ", precedence = 4, leftAssoc = true}];
|
|
403 |
|
|
404 |
(* The negation symbol *)
|
|
405 |
|
|
406 |
val negation : string ref = ref "~";
|
|
407 |
|
|
408 |
(* Binder symbols *)
|
|
409 |
|
|
410 |
val binders : string list ref = ref ["\\","!","?","?!"];
|
|
411 |
|
|
412 |
(* Bracket symbols *)
|
|
413 |
|
|
414 |
val brackets : (string * string) list ref = ref [("[","]"),("{","}")];
|
|
415 |
|
|
416 |
(* Pretty printing *)
|
|
417 |
|
|
418 |
fun pp inputTerm =
|
|
419 |
let
|
|
420 |
val quants = !binders
|
|
421 |
and iOps = !infixes
|
|
422 |
and neg = !negation
|
|
423 |
and bracks = !brackets
|
|
424 |
|
|
425 |
val bracks = map (fn (b1,b2) => (b1 ^ b2, b1, b2)) bracks
|
|
426 |
|
|
427 |
val bTokens = map #2 bracks @ map #3 bracks
|
|
428 |
|
|
429 |
val iTokens = Print.tokensInfixes iOps
|
|
430 |
|
|
431 |
fun destI tm =
|
|
432 |
case tm of
|
|
433 |
Fn (f,[a,b]) =>
|
|
434 |
let
|
|
435 |
val f = Name.toString f
|
|
436 |
in
|
|
437 |
if StringSet.member f iTokens then SOME (f,a,b) else NONE
|
|
438 |
end
|
|
439 |
| _ => NONE
|
|
440 |
|
|
441 |
val iPrinter = Print.ppInfixes iOps destI
|
|
442 |
|
|
443 |
val specialTokens =
|
|
444 |
StringSet.addList iTokens (neg :: quants @ ["$","(",")"] @ bTokens)
|
|
445 |
|
|
446 |
fun vName bv s = StringSet.member s bv
|
|
447 |
|
|
448 |
fun checkVarName bv n =
|
|
449 |
let
|
|
450 |
val s = Name.toString n
|
|
451 |
in
|
|
452 |
if vName bv s then s else "$" ^ s
|
|
453 |
end
|
|
454 |
|
|
455 |
fun varName bv = Print.ppMap (checkVarName bv) Print.ppString
|
|
456 |
|
|
457 |
fun checkFunctionName bv n =
|
|
458 |
let
|
|
459 |
val s = Name.toString n
|
|
460 |
in
|
|
461 |
if StringSet.member s specialTokens orelse vName bv s then
|
|
462 |
"(" ^ s ^ ")"
|
|
463 |
else
|
|
464 |
s
|
|
465 |
end
|
|
466 |
|
|
467 |
fun functionName bv = Print.ppMap (checkFunctionName bv) Print.ppString
|
|
468 |
|
|
469 |
fun isI tm = Option.isSome (destI tm)
|
|
470 |
|
|
471 |
fun stripNeg tm =
|
|
472 |
case tm of
|
|
473 |
Fn (f,[a]) =>
|
|
474 |
if Name.toString f <> neg then (0,tm)
|
|
475 |
else let val (n,tm) = stripNeg a in (n + 1, tm) end
|
|
476 |
| _ => (0,tm)
|
|
477 |
|
|
478 |
val destQuant =
|
|
479 |
let
|
|
480 |
fun dest q (Fn (q', [Var v, body])) =
|
|
481 |
if Name.toString q' <> q then NONE
|
|
482 |
else
|
|
483 |
(case dest q body of
|
|
484 |
NONE => SOME (q,v,[],body)
|
|
485 |
| SOME (_,v',vs,body) => SOME (q, v, v' :: vs, body))
|
|
486 |
| dest _ _ = NONE
|
|
487 |
in
|
|
488 |
fn tm => Useful.first (fn q => dest q tm) quants
|
|
489 |
end
|
|
490 |
|
|
491 |
fun isQuant tm = Option.isSome (destQuant tm)
|
|
492 |
|
|
493 |
fun destBrack (Fn (b,[tm])) =
|
|
494 |
let
|
|
495 |
val s = Name.toString b
|
|
496 |
in
|
|
497 |
case List.find (fn (n,_,_) => n = s) bracks of
|
|
498 |
NONE => NONE
|
|
499 |
| SOME (_,b1,b2) => SOME (b1,tm,b2)
|
|
500 |
end
|
|
501 |
| destBrack _ = NONE
|
|
502 |
|
|
503 |
fun isBrack tm = Option.isSome (destBrack tm)
|
|
504 |
|
|
505 |
fun functionArgument bv tm =
|
|
506 |
Print.sequence
|
|
507 |
(Print.addBreak 1)
|
|
508 |
(if isBrack tm then customBracket bv tm
|
|
509 |
else if isVar tm orelse isConst tm then basic bv tm
|
|
510 |
else bracket bv tm)
|
|
511 |
|
|
512 |
and basic bv (Var v) = varName bv v
|
|
513 |
| basic bv (Fn (f,args)) =
|
|
514 |
Print.blockProgram Print.Inconsistent 2
|
|
515 |
(functionName bv f :: map (functionArgument bv) args)
|
|
516 |
|
|
517 |
and customBracket bv tm =
|
|
518 |
case destBrack tm of
|
|
519 |
SOME (b1,tm,b2) => Print.ppBracket b1 b2 (term bv) tm
|
|
520 |
| NONE => basic bv tm
|
|
521 |
|
|
522 |
and innerQuant bv tm =
|
|
523 |
case destQuant tm of
|
|
524 |
NONE => term bv tm
|
|
525 |
| SOME (q,v,vs,tm) =>
|
|
526 |
let
|
|
527 |
val bv = StringSet.addList bv (map Name.toString (v :: vs))
|
|
528 |
in
|
|
529 |
Print.program
|
|
530 |
[Print.addString q,
|
|
531 |
varName bv v,
|
|
532 |
Print.program
|
|
533 |
(map (Print.sequence (Print.addBreak 1) o varName bv) vs),
|
|
534 |
Print.addString ".",
|
|
535 |
Print.addBreak 1,
|
|
536 |
innerQuant bv tm]
|
|
537 |
end
|
|
538 |
|
|
539 |
and quantifier bv tm =
|
|
540 |
if not (isQuant tm) then customBracket bv tm
|
|
541 |
else Print.block Print.Inconsistent 2 (innerQuant bv tm)
|
|
542 |
|
|
543 |
and molecule bv (tm,r) =
|
|
544 |
let
|
|
545 |
val (n,tm) = stripNeg tm
|
|
546 |
in
|
|
547 |
Print.blockProgram Print.Inconsistent n
|
|
548 |
[Print.duplicate n (Print.addString neg),
|
|
549 |
if isI tm orelse (r andalso isQuant tm) then bracket bv tm
|
|
550 |
else quantifier bv tm]
|
|
551 |
end
|
|
552 |
|
|
553 |
and term bv tm = iPrinter (molecule bv) (tm,false)
|
|
554 |
|
|
555 |
and bracket bv tm = Print.ppBracket "(" ")" (term bv) tm
|
|
556 |
in
|
|
557 |
term StringSet.empty
|
|
558 |
end inputTerm;
|
|
559 |
|
|
560 |
val toString = Print.toString pp;
|
|
561 |
|
|
562 |
(* Parsing *)
|
|
563 |
|
|
564 |
local
|
|
565 |
open Parse;
|
|
566 |
|
|
567 |
infixr 9 >>++
|
|
568 |
infixr 8 ++
|
|
569 |
infixr 7 >>
|
|
570 |
infixr 6 ||
|
|
571 |
|
|
572 |
val isAlphaNum =
|
|
573 |
let
|
|
574 |
val alphaNumChars = explode "_'"
|
|
575 |
in
|
|
576 |
fn c => mem c alphaNumChars orelse Char.isAlphaNum c
|
|
577 |
end;
|
|
578 |
|
|
579 |
local
|
|
580 |
val alphaNumToken = atLeastOne (some isAlphaNum) >> implode;
|
|
581 |
|
|
582 |
val symbolToken =
|
|
583 |
let
|
|
584 |
fun isNeg c = str c = !negation
|
|
585 |
|
|
586 |
val symbolChars = explode "<>=-*+/\\?@|!$%&#^:;~"
|
|
587 |
|
|
588 |
fun isSymbol c = mem c symbolChars
|
|
589 |
|
|
590 |
fun isNonNegSymbol c = not (isNeg c) andalso isSymbol c
|
|
591 |
in
|
|
592 |
some isNeg >> str ||
|
|
593 |
(some isNonNegSymbol ++ many (some isSymbol)) >> (implode o op::)
|
|
594 |
end;
|
|
595 |
|
|
596 |
val punctToken =
|
|
597 |
let
|
|
598 |
val punctChars = explode "()[]{}.,"
|
|
599 |
|
|
600 |
fun isPunct c = mem c punctChars
|
|
601 |
in
|
|
602 |
some isPunct >> str
|
|
603 |
end;
|
|
604 |
|
|
605 |
val lexToken = alphaNumToken || symbolToken || punctToken;
|
|
606 |
|
|
607 |
val space = many (some Char.isSpace);
|
|
608 |
in
|
|
609 |
val lexer = (space ++ lexToken ++ space) >> (fn (_,(tok,_)) => tok);
|
|
610 |
end;
|
|
611 |
|
|
612 |
fun termParser inputStream =
|
|
613 |
let
|
|
614 |
val quants = !binders
|
|
615 |
and iOps = !infixes
|
|
616 |
and neg = !negation
|
|
617 |
and bracks = ("(",")") :: !brackets
|
|
618 |
|
|
619 |
val bracks = map (fn (b1,b2) => (b1 ^ b2, b1, b2)) bracks
|
|
620 |
|
|
621 |
val bTokens = map #2 bracks @ map #3 bracks
|
|
622 |
|
|
623 |
fun possibleVarName "" = false
|
|
624 |
| possibleVarName s = isAlphaNum (String.sub (s,0))
|
|
625 |
|
|
626 |
fun vName bv s = StringSet.member s bv
|
|
627 |
|
|
628 |
val iTokens = Print.tokensInfixes iOps
|
|
629 |
|
|
630 |
val iParser =
|
|
631 |
parseInfixes iOps (fn (f,a,b) => Fn (Name.fromString f, [a,b]))
|
|
632 |
|
|
633 |
val specialTokens =
|
|
634 |
StringSet.addList iTokens (neg :: quants @ ["$"] @ bTokens)
|
|
635 |
|
|
636 |
fun varName bv =
|
|
637 |
some (vName bv) ||
|
|
638 |
(some (Useful.equal "$") ++ some possibleVarName) >> snd
|
|
639 |
|
|
640 |
fun fName bv s =
|
|
641 |
not (StringSet.member s specialTokens) andalso not (vName bv s)
|
|
642 |
|
|
643 |
fun functionName bv =
|
|
644 |
some (fName bv) ||
|
|
645 |
(some (Useful.equal "(") ++ any ++ some (Useful.equal ")")) >>
|
|
646 |
(fn (_,(s,_)) => s)
|
|
647 |
|
|
648 |
fun basic bv tokens =
|
|
649 |
let
|
|
650 |
val var = varName bv >> (Var o Name.fromString)
|
|
651 |
|
|
652 |
val const =
|
|
653 |
functionName bv >> (fn f => Fn (Name.fromString f, []))
|
|
654 |
|
|
655 |
fun bracket (ab,a,b) =
|
|
656 |
(some (Useful.equal a) ++ term bv ++ some (Useful.equal b)) >>
|
|
657 |
(fn (_,(tm,_)) =>
|
|
658 |
if ab = "()" then tm else Fn (Name.fromString ab, [tm]))
|
|
659 |
|
|
660 |
fun quantifier q =
|
|
661 |
let
|
|
662 |
fun bind (v,t) =
|
|
663 |
Fn (Name.fromString q, [Var (Name.fromString v), t])
|
|
664 |
in
|
|
665 |
(some (Useful.equal q) ++
|
|
666 |
atLeastOne (some possibleVarName) ++
|
|
667 |
some (Useful.equal ".")) >>++
|
|
668 |
(fn (_,(vs,_)) =>
|
|
669 |
term (StringSet.addList bv vs) >>
|
|
670 |
(fn body => foldr bind body vs))
|
|
671 |
end
|
|
672 |
in
|
|
673 |
var ||
|
|
674 |
const ||
|
|
675 |
first (map bracket bracks) ||
|
|
676 |
first (map quantifier quants)
|
|
677 |
end tokens
|
|
678 |
|
|
679 |
and molecule bv tokens =
|
|
680 |
let
|
|
681 |
val negations = many (some (Useful.equal neg)) >> length
|
|
682 |
|
|
683 |
val function =
|
|
684 |
(functionName bv ++ many (basic bv)) >>
|
|
685 |
(fn (f,args) => Fn (Name.fromString f, args)) ||
|
|
686 |
basic bv
|
|
687 |
in
|
|
688 |
(negations ++ function) >>
|
|
689 |
(fn (n,tm) => funpow n (fn t => Fn (Name.fromString neg, [t])) tm)
|
|
690 |
end tokens
|
|
691 |
|
|
692 |
and term bv tokens = iParser (molecule bv) tokens
|
|
693 |
in
|
|
694 |
term StringSet.empty
|
|
695 |
end inputStream;
|
|
696 |
in
|
|
697 |
fun fromString input =
|
|
698 |
let
|
|
699 |
val chars = Stream.fromList (explode input)
|
|
700 |
|
|
701 |
val tokens = everything (lexer >> singleton) chars
|
|
702 |
|
|
703 |
val terms = everything (termParser >> singleton) tokens
|
|
704 |
in
|
|
705 |
case Stream.toList terms of
|
|
706 |
[tm] => tm
|
|
707 |
| _ => raise Error "Term.fromString"
|
|
708 |
end;
|
|
709 |
end;
|
|
710 |
|
|
711 |
local
|
|
712 |
val antiquotedTermToString = Print.toString (Print.ppBracket "(" ")" pp);
|
|
713 |
in
|
|
714 |
val parse = Parse.parseQuotation antiquotedTermToString fromString;
|
|
715 |
end;
|
|
716 |
|
|
717 |
end
|
|
718 |
|
|
719 |
structure TermOrdered =
|
|
720 |
struct type t = Term.term val compare = Term.compare end
|
|
721 |
|
|
722 |
structure TermMap = KeyMap (TermOrdered);
|
|
723 |
|
|
724 |
structure TermSet = ElementSet (TermMap);
|