forked from regb/scala-smtlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TailPrinter.scala
541 lines (475 loc) · 20.7 KB
/
TailPrinter.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package smtlib
package printer
import common.LinkedList
import parser.Commands._
import parser.CommandsResponses._
import parser.Terms._
import java.io.Writer
object TailPrinter extends Printer with TerminalTreesPrinter {
override val name: String = "tail-printer"
private type Action = () => Unit
override def printScript(script: Script, writer: Writer): Unit = {
script.commands.foreach(cmd => {
printCommand(cmd, writer)
writer.write("\n")
})
}
override def printCommand(command: Command, writer: Writer): Unit = {
val actionsBuffer = new LinkedList[Action]
printCommand(command, writer, actionsBuffer)
doActions(actionsBuffer)
}
private def printCommand(command: Command, writer: Writer, actions: LinkedList[Action]): Unit = command match {
case Assert(term) => {
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => printTerm(term, writer, actions))
actions.prepend(() => writer.write("(assert "))
}
case CheckSat() => {
actions.prepend(() => writer.write("(check-sat)\n"))
}
case CheckSatAssuming(props) => {
actions.prepend(() => writer.write(')'))
actions.prepend(() => printNary(writer, props, printPropLit _, "(", " ", ")", actions))
actions.prepend(() => writer.write("(check-sat-assuming "))
}
case DeclareConst(name, sort) => {
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => printSort(sort, writer))
actions.prepend(() => writer.write(' '))
actions.prepend(() => printSymbol(name, writer))
actions.prepend(() => writer.write("(declare-const "))
}
case DeclareFun(name, paramSorts, returnSort) => {
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => printSort(returnSort, writer, actions))
actions.prepend(() => printNary(writer, paramSorts, (s: Sort, writer: Writer) => printSort(s, writer, actions), " (", " ", ") ", actions))
actions.prepend(() => printSymbol(name, writer))
actions.prepend(() => writer.write("(declare-fun "))
}
case DeclareSort(name, arity) => {
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => writer.write(arity.toString))
actions.prepend(() => writer.write(" "))
actions.prepend(() => printSymbol(name, writer))
actions.prepend(() => writer.write("(declare-sort "))
}
case DefineFun(FunDef(name, sortedVars, returnSort, body)) => {
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => printTerm(body, writer, actions))
actions.prepend(() => writer.write(" "))
actions.prepend(() => printSort(returnSort, writer, actions))
actions.prepend(() => printNary(writer, sortedVars, (v: SortedVar, w: Writer) => printSortedVar(v, w, actions), " (", " ", ") ", actions))
actions.prepend(() => printSymbol(name, writer))
actions.prepend(() => writer.write("(define-fun "))
}
case DefineFunRec(FunDef(name, sortedVars, returnSort, body)) => {
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => printTerm(body, writer, actions))
actions.prepend(() => writer.write(" "))
actions.prepend(() => printSort(returnSort, writer, actions))
actions.prepend(() => printNary(writer, sortedVars, (v: SortedVar, w: Writer) => printSortedVar(v, w, actions), " (", " ", ") ", actions))
actions.prepend(() => printSymbol(name, writer))
actions.prepend(() => writer.write("(define-fun-rec "))
}
case DefineFunsRec(funDecs, bodies) => {
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => printNary(writer, bodies, (t: Term, w: Writer) => printTerm(t, w, actions), "(", " ", ")", actions))
actions.prepend(() => printNary(writer, funDecs, (fd: FunDec, w: Writer) => printFunDec(fd, w, actions), "(", " ", ")", actions))
actions.prepend(() => writer.write("(define-funs-rec "))
}
case DefineSort(name, params, sort) => {
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => printSort(sort, writer, actions))
actions.prepend(() => printNary(writer, params, printSymbol," (", " ", ") ", actions))
actions.prepend(() => printSymbol(name, writer))
actions.prepend(() => writer.write("(define-sort "))
}
case Echo(value) => {
actions.prepend(() => writer.write(")"))
actions.prepend(() => printConstant(value, writer))
actions.prepend(() => writer.write("(echo "))
}
case Exit() => {
actions.prepend(() => writer.write("(exit)\n"))
}
case GetAssertions() => {
actions.prepend(() => writer.write("(get-assertions)\n"))
}
case GetAssignment() => {
actions.prepend(() => writer.write("(get-assignment)\n"))
}
case GetInfo(flag) => {
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => printInfoFlag(flag, writer))
actions.prepend(() => writer.write("(get-info "))
}
case GetModel() => {
actions.prepend(() => writer.write("(get-model)\n"))
}
case GetOption(SKeyword(key)) => {
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => writer.write(key))
actions.prepend(() => writer.write("(get-option :"))
}
case GetProof() => {
actions.prepend(() => writer.write("(get-proof)\n"))
}
case GetUnsatAssumptions() => {
actions.prepend(() => writer.write("(get-unsat-assumptions)\n"))
}
case GetUnsatCore() => {
actions.prepend(() => writer.write("(get-unsat-core)\n"))
}
case GetValue(t, ts) => {
actions.prepend(() => printNary(writer, t +: ts, (t: Term, w: Writer) => printTerm(t, w, actions), "(", " ", "))", actions))
actions.prepend(() => writer.write("(get-value "))
}
case Pop(n) => {
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => writer.write(n.toString))
actions.prepend(() => writer.write("(pop "))
}
case Push(n) => {
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => writer.write(n.toString))
actions.prepend(() => writer.write("(push "))
}
case Reset() => {
writer.write("(reset)\n")
}
case ResetAssertions() => {
writer.write("(reset-assertions)\n")
}
case SetInfo(attribute) => {
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => printAttribute(attribute, writer))
actions.prepend(() => writer.write("(set-info "))
}
case SetLogic(logic) => {
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => printLogic(logic, writer))
actions.prepend(() => writer.write("(set-logic "))
}
case SetOption(option) => {
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => printOption(option, writer))
actions.prepend(() => writer.write("(set-option "))
}
case DeclareDatatypes(datatypes) => {
writer.write("(declare-datatypes () (")
datatypes.foreach{ case (name, constructors) => {
writer.write(" (")
printSymbol(name, writer)
constructors.foreach{
case Constructor(sym, Seq()) => {
writer.write(" (")
writer.write(sym.name)
writer.write(")")
}
case Constructor(sym, fields) => {
writer.write(" (")
writer.write(sym.name)
fields.foreach{ case (field, sort) => {
writer.write(" (")
writer.write(field.name)
writer.write(" ")
printSort(sort, writer)
writer.write(")")
}}
writer.write(")")
}
}
writer.write(") ")
}}
writer.write("))\n")
}
}
override def printCommandResponse(response: CommandResponse, writer: Writer): Unit = {
val actionsBuffer = new LinkedList[Action]
printCommandResponse(response, writer, actionsBuffer)
doActions(actionsBuffer)
}
private def printCommandResponse(response: CommandResponse, writer: Writer, actions: LinkedList[Action]): Unit = response match {
case Success =>
actions.prepend(() => writer.write("success\n"))
case Unsupported =>
actions.prepend(() => writer.write("unsupported\n"))
case Error(msg) =>
actions.prepend(() => writer.write(")\n"))
actions.prepend(() => writer.write('"'))
actions.prepend(() => writer.write(msg))
actions.prepend(() => writer.write('"'))
actions.prepend(() => writer.write("(error "))
case CheckSatStatus(SatStatus) =>
actions.prepend(() => writer.write("sat\n"))
case CheckSatStatus(UnsatStatus) =>
actions.prepend(() => writer.write("unsat\n"))
case CheckSatStatus(UnknownStatus) =>
actions.prepend(() => writer.write("unknown\n"))
case EchoResponseSuccess(value) =>
actions.prepend(() => printString(value, writer))
case GetAssertionsResponseSuccess(assertions) =>
printNary(writer, assertions, printTerm, "(", " ", " )", actions)
case GetAssignmentResponseSuccess(valuationPairs) => {
def printValuationPair(pair: (SSymbol, Boolean), writer: Writer): Unit = {
actions.prepend(() => writer.write(')'))
actions.prepend(() => writer.write(pair._2.toString))
actions.prepend(() => writer.write(' '))
actions.prepend(() => printSymbol(pair._1, writer))
actions.prepend(() => writer.write('('))
}
printNary(writer, valuationPairs, printValuationPair, "(", " ", ")", actions)
}
case GetInfoResponseSuccess(response, responses) => {
printNary(writer, response +: responses,
(ir: InfoResponse, w: Writer) => actions.prepend(() => printInfoResponse(ir, w, actions)),
"(", " ", ")", actions)
}
case GetModelResponseSuccess(exprs) => {
def printGetModelResponseEntry(expr: SExpr, writer: Writer): Unit =
printSExpr(expr, writer, actions)
actions.prepend(() => writer.write(')'))
actions.prepend(() => printNary(writer, exprs, printGetModelResponseEntry, "", "\n", "", actions))
actions.prepend(() => writer.write("(model \n"))
}
case GetOptionResponseSuccess(av) => {
printSExpr(av, writer, actions)
}
case GetProofResponseSuccess(proof) => {
actions.prepend(() => printSExpr(proof, writer, actions))
}
case GetUnsatAssumptionsResponseSuccess(symbols) => {
printNary(writer, symbols, printSExpr, "(", " ", ")", actions)
}
case GetUnsatCoreResponseSuccess(symbols) => {
printNary(writer, symbols, printSExpr, "(", " ", ")", actions)
}
case GetValueResponseSuccess(valuationPairs) => {
def printValuationPair(pair: (Term, Term), writer: Writer): Unit = {
actions.prepend(() => writer.write(')'))
actions.prepend(() => printTerm(pair._2, writer, actions))
actions.prepend(() => writer.write(' '))
actions.prepend(() => printTerm(pair._1, writer, actions))
actions.prepend(() => writer.write('('))
}
printNary(writer, valuationPairs, printValuationPair, "(", " ", ")", actions)
}
}
override def printSort(sort: Sort, writer: Writer): Unit = {
val actionsBuffer = new LinkedList[Action]
printSort(sort, writer, actionsBuffer)
doActions(actionsBuffer)
}
private def printSort(sort: Sort, writer: Writer, actions: LinkedList[Action]): Unit = {
val id = sort.id
if(sort.subSorts.isEmpty)
actions.prepend(() => printId(id, writer))
else {
actions.prepend(() => printNary(writer, sort.subSorts, (s: Sort, w: Writer) => printSort(s, w, actions), " ", " ", ")", actions))
actions.prepend(() => printId(id, writer))
actions.prepend(() => writer.write("("))
}
}
override def printTerm(term: Term, writer: Writer): Unit = {
val actionsBuffer = new LinkedList[Action]
printTerm(term, writer, actionsBuffer)
doActions(actionsBuffer)
}
private def printTerm(term: Term, writer: Writer, actions: LinkedList[Action]): Unit = term match {
case Let(vb, vbs, t) =>
actions.prepend(() => writer.write(")"))
actions.prepend(() => printTerm(t, writer, actions))
actions.prepend(() => printNary(writer, vbs, (v: VarBinding, w: Writer) => printVarBinding(v, w, actions), "", " ", ") ", actions))
actions.prepend(() => printVarBinding(vb, writer, actions))
actions.prepend(() => writer.write("(let ("))
case Forall(sortedVar, sortedVars, t) =>
actions.prepend(() => writer.write(")"))
actions.prepend(() => printTerm(t, writer, actions))
actions.prepend(() => printNary(writer, sortedVars, (v: SortedVar, w: Writer) => printSortedVar(v, w, actions), "", " ", ") ", actions))
actions.prepend(() => printSortedVar(sortedVar, writer, actions))
actions.prepend(() => writer.write("(forall ("))
case Exists(sortedVar, sortedVars, t) =>
actions.prepend(() => writer.write(")"))
actions.prepend(() => printTerm(t, writer, actions))
actions.prepend(() => printNary(writer, sortedVars, (v: SortedVar, w: Writer) => printSortedVar(v, w, actions), "", " ", ") ", actions))
actions.prepend(() => printSortedVar(sortedVar, writer, actions))
actions.prepend(() => writer.write("(exists ("))
case FunctionApplication(fun, ts) =>
if (ts.nonEmpty) {
actions.prepend(() => printNary(writer, ts, (t: Term, w: Writer) => printTerm(t, w, actions), " ", " ", ")", actions))
actions.prepend(() => printQualifiedId(fun, writer, actions))
actions.prepend(() => writer.write("("))
} else {
actions.prepend(() => printQualifiedId(fun, writer, actions))
}
case AnnotatedTerm(term, attr, attrs) => {
actions.prepend(() => printNary(writer, attr +: attrs, printAttribute, " ", " ", ")", actions))
actions.prepend(() => printTerm(term, writer, actions))
actions.prepend(() => writer.write("(! "))
}
case id@QualifiedIdentifier(_, _) =>
actions.prepend(() => printQualifiedId(id, writer, actions))
case (c: Constant) =>
actions.prepend(() => printConstant(c, writer))
}
private def printQualifiedId(qi: QualifiedIdentifier, writer: Writer, actions: LinkedList[Action]): Unit = qi.sort match {
case None =>
actions.prepend(() => printId(qi.id, writer))
case Some(sort) =>
actions.prepend(() => writer.write(")"))
actions.prepend(() => printSort(sort, writer, actions))
actions.prepend(() => writer.write(" "))
actions.prepend(() => printId(qi.id, writer))
actions.prepend(() => writer.write("(as "))
}
private def printVarBinding(vb: VarBinding, writer: Writer, actions: LinkedList[Action]): Unit = {
actions.prepend(() => writer.write(')'))
actions.prepend(() => printTerm(vb.term, writer, actions))
actions.prepend(() => writer.write(' '))
actions.prepend(() => printSymbol(vb.name, writer))
actions.prepend(() => writer.write('('))
}
private def printSortedVar(sv: SortedVar, writer: Writer, actions: LinkedList[Action]): Unit = {
actions.prepend(() => writer.write(')'))
actions.prepend(() => printSort(sv.sort, writer, actions))
actions.prepend(() => writer.write(' '))
actions.prepend(() => printSymbol(sv.name, writer))
actions.prepend(() => writer.write('('))
}
private def printInfoResponse(infoResponse: InfoResponse, writer: Writer, actions: LinkedList[Action]): Unit = infoResponse match {
case AssertionStackLevelsInfoResponse(level) =>
actions.prepend(() => writer.write(level.toString))
actions.prepend(() => writer.write(":assertion-stack-levels "))
case AuthorsInfoResponse(authors) =>
actions.prepend(() => printString(authors, writer))
actions.prepend(() => writer.write(":authors "))
case ErrorBehaviorInfoResponse(ImmediateExitErrorBehavior) =>
actions.prepend(() => writer.write(":error-behavior immediate-exit"))
case ErrorBehaviorInfoResponse(ContinuedExecutionErrorBehavior) =>
actions.prepend(() => writer.write(":error-behavior continued-execution"))
case NameInfoResponse(name) =>
actions.prepend(() => writer.write('"'))
actions.prepend(() => writer.write(name))
actions.prepend(() => writer.write(":name \""))
case ReasonUnknownInfoResponse(TimeoutReasonUnknown) =>
actions.prepend(() => writer.write(":reason-unknown timeout"))
case ReasonUnknownInfoResponse(MemoutReasonUnknown) =>
actions.prepend(() => writer.write(":reason-unknown memout"))
case ReasonUnknownInfoResponse(IncompleteReasonUnknown) =>
actions.prepend(() => writer.write(":reason-unknown incomplete"))
case VersionInfoResponse(version) =>
actions.prepend(() => writer.write('"'))
actions.prepend(() => writer.write(version))
actions.prepend(() => writer.write(":version \""))
case AttributeInfoResponse(attribute) =>
actions.prepend(() => printAttribute(attribute, writer))
}
def printAttribute(attribute: Attribute, writer: Writer): Unit = {
printKeyword(attribute.keyword, writer)
attribute.value match {
case Some(value) =>
writer.write(" ")
printSExpr(value, writer)
case None => ()
}
}
override def printSExpr(sexpr: SExpr, writer: Writer): Unit = {
val actionsBuffer = new LinkedList[Action]
printSExpr(sexpr, writer, actionsBuffer)
doActions(actionsBuffer)
}
private def printSExpr(sexpr: SExpr, writer: Writer, actions: LinkedList[Action]): Unit = sexpr match {
case (cmd: Command) => printCommand(cmd, writer, actions)
case (res: CommandResponse) => printCommandResponse(res, writer, actions)
case (term: Term) => printTerm(term, writer, actions)
case SList(es) =>
printNary(writer, es, printSExpr, "(", " ", ")", actions)
case SKeyword(key) =>
writer.write(":")
writer.write(key)
case s@SSymbol(_) =>
printSymbol(s, writer)
}
private def printOption(option: SMTOption, writer: Writer): Unit = option match {
case DiagnosticOutputChannel(value) =>
writer.write(":diagnostic-output-channel ")
writer.write('"')
writer.write(value)
writer.write('"')
case GlobalDeclarations(value) =>
writer.write(":global-declarations ")
writer.write(value.toString)
case PrintSuccess(value) =>
writer.write(":print-success ")
writer.write(value.toString)
case InteractiveMode(value) =>
writer.write(":interactive-mode ")
writer.write(value.toString)
case ProduceAssertions(value) =>
writer.write(":produce-assertions ")
writer.write(value.toString)
case ProduceAssignments(value) =>
writer.write(":produce-assignments ")
writer.write(value.toString)
case ProduceModels(value) =>
writer.write(":produce-models ")
writer.write(value.toString)
case ProduceProofs(value) =>
writer.write(":produce-proofs ")
writer.write(value.toString)
case ProduceUnsatAssumptions(value) =>
writer.write(":produce-unsat-assumptions ")
writer.write(value.toString)
case ProduceUnsatCores(value) =>
writer.write(":produce-unsat-cores ")
writer.write(value.toString)
case RegularOutputChannel(value) =>
writer.write(":regular-output-channel ")
writer.write('"')
writer.write(value)
writer.write('"')
case RandomSeed(num) =>
writer.write(":random-seed ")
writer.write(num.toString)
case ReproducibleResourceLimit(num) =>
writer.write(":reproducible-resource-limit ")
writer.write(num.toString)
case Verbosity(num) =>
writer.write(":verbosity ")
writer.write(num.toString)
case AttributeOption(attribute) =>
printAttribute(attribute, writer)
}
protected def printFunDec(funDec: FunDec, writer: Writer, actions: LinkedList[Action]): Unit = {
actions.prepend(() => writer.write(')'))
actions.prepend(() => printSort(funDec.returnSort, writer, actions))
actions.prepend(() => printNary(writer, funDec.params, (v: SortedVar, w: Writer) => printSortedVar(v, w, actions), " (", " ", ") ", actions))
actions.prepend(() => printSymbol(funDec.name, writer))
actions.prepend(() => writer.write('('))
}
private def printNary[A](
writer: Writer, as: Seq[A], printer: (A, Writer) => Unit,
pre: String, op: String, post: String, actions: LinkedList[Action]): Unit = {
val newActions = new scala.collection.mutable.ListBuffer[Action]()
newActions.append(() => writer.write(pre))
var c = 0
val sz = as.size
as.foreach(a => {
newActions.append(() => printer(a, writer))
c += 1
if(c < sz)
newActions.append(() => writer.write(op))
})
newActions.append(() => writer.write(post))
newActions.toList.reverse.foreach(action =>
actions.prepend(action)
)
}
private def doActions(actions: LinkedList[Action]): Unit = {
while(!actions.isEmpty) {
val action = actions.pop()
action()
}
}
}