-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse.scala
357 lines (280 loc) · 10.9 KB
/
parse.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
package KeYmaeraD
//package cohenhormander;
import java.io.BufferedWriter
import java.io.OutputStreamWriter
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.InputStream
import java.io.FileInputStream
import scala.util.parsing.combinator.lexical._
import scala.util.parsing.combinator.syntactical._
import scala.util.parsing.combinator._
class DLLexical extends StdLexical {
override def identChar = letter | elem('_') | elem('\'')
}
class DLParser(ins : String)
extends StdTokenParsers {
type Tokens = StdLexical ; val lexical = new DLLexical
lexical.delimiters ++= List(",", ";",":", "(", ")","[","]","{","}",
"=", "<", ">", ">=", "<=", "/=",
"+","-","*", "/", "^",
"++", ":=", "@", "?", "\'",
"&", "|", "<=>", "==>", "|-", ".", "~", "->"
).iterator
lexical.reserved ++= List("forall", "exists",
"true", "false",
"solution", "invariant"
).iterator
/*
val ins : String = ""
def this(ins1: String) = {
this()
ins = ins1
}
*/
def this(in : InputStream) = {
this({
val br = new BufferedReader(new InputStreamReader(in))
var ins1 = ""
var ln = br.readLine()
while (ln != null){
println( ln)
ins1 = ins1 + ln + "\n"
ln = br.readLine()
}
println("input = " + ins1)
ins1
})
}
def term: Parser[Term] =
prod*("+" ^^^ {(x:Term, y:Term) => Fn("+", List(x,y))}
| "-" ^^^ {(x: Term, y: Term) => Fn("-", List(x,y))})
def prod: Parser[Term] =
factor*("*" ^^^ {(x: Term, y: Term)
=> Fn("*", List(x, y))}
| "/" ^^^ {(x: Term, y: Term)
=> Fn("/", List(x, y))}) |
"-" ~> prod ^^ { x => Fn("-", List(x))}
def factor: Parser[Term] =
atomicTerm ~ "^" ~ numericLit ^^
{case x ~ "^" ~ y =>
Fn("^", List(x,Num(Exact.Integer(Integer.parseInt(y)))))} |
atomicTerm
def function : Parser [Fn] =
(ident <~ "(") ~ ( repsep(term, ",") <~ ")") ^^
{case f ~ xs => Fn(f, xs)}
def atomicTerm : Parser[Term] =
"(" ~> term <~ ")" |
numericLit ^^ (x => Num(Exact.Integer(Integer.parseInt(x)))) |
function |
ident ^^ (x => Var(x))
def pred : Parser[Pred] =
term ~ ("=" | "/=" | "<" | ">" | "<=" | ">=" ) ~ term ^^
{ case t1 ~ r ~ t2 =>
R(r, List(t1,t2))}
def formula00 : Parser[Formula] =
"forall" ~> ident ~ "."~ formula00 ^^
{ case x ~ "." ~ f => Quantifier(Forall, x, Real, f)} |
"exists" ~> ident ~ "."~ formula00 ^^
{ case x ~ "." ~ f => Quantifier(Exists, x, Real, f)} |
"forall" ~> ident ~ ":" ~ ident ~ "." ~ formula00 ^^
{ case x ~ ":" ~ "Real" ~ "." ~ f => Quantifier(Forall, x, Real, f)
case x ~ ":" ~ c ~ "." ~ f => Quantifier(Forall, x, St(c), f)
} |
"exists" ~> ident ~ ":" ~ ident ~ "." ~ formula00 ^^
{ case x ~ ":" ~ "Real" ~ "." ~ f => Quantifier(Exists, x, Real, f)
case x ~ ":" ~ c ~ "." ~ f => Quantifier(Exists, x, St(c), f)
} |
formula0
def formula0 : Parser[Formula] =
formula1*( "<=>" ^^^ {(f1:Formula,f2:Formula) => Binop(Iff,f1,f2)})
// Implication is right-associative.
def formula1 : Parser[Formula] =
rep1sep(formula2, "==>") ^^
((lst) => lst.reduceRight((f1:Formula,f2:Formula) => Binop(Imp,f1,f2)))
def formula2 : Parser[Formula] =
formula3*( "|" ^^^ {(f1:Formula,f2:Formula) => Binop(Or,f1,f2)})
def formula3 : Parser[Formula] =
formula4*( "&" ^^^ {(f1:Formula,f2:Formula) => Binop(And,f1,f2)})
def formula4 : Parser[Formula] =
"~" ~> formula5 ^^ {fm => Not(fm)} |
formula5
def formula5 : Parser[Formula] =
"(" ~> formula00 <~ ")" |
pred ^^ (x => Atom(x)) |
"true" ^^^ True |
"false" ^^^ False |
// XXX doesn't work right for e.g. "[hp] forall x . ..."
("[" ~> hp <~ "]") ~ formula4 ^^ {case a ~ f => Modality(Box,a,f)} |
("<" ~> hp <~ ">") ~ formula4 ^^ {case a ~ f => Modality(Diamond,a,f)}
/* Distinguish between logical variables
* and (unary) function symbols.
*/
def freeVarsAreFns_Term(bndVars : List[String], tm : Term) : Term
= tm match {
case Var(x) if ! (bndVars.contains(x)) =>
Fn(x,Nil)
case Fn(f, ps) =>
val fnew = f //if(bndVars.contains(f) ) xnew else f
Fn(fnew, ps.map(p => freeVarsAreFns_Term(bndVars, p)))
case _ => tm
}
def freeVarsAreFns_HP(bndVars : List[String], hp: HP) : HP = hp match {
case Assign(vs) =>
val vs1 = vs.map(vt => {
val f@Fn(_,_) = freeVarsAreFns_Term(bndVars, vt._1)
(f, freeVarsAreFns_Term(bndVars,vt._2)) })
Assign(vs1)
case AssignAny(f) =>
val f1@Fn(_,_) = freeVarsAreFns_Term(bndVars,f)
AssignAny(f1)
case AssignQuantified(i,c,vs) =>
AssignQuantified(i,c, vs.map(replace_asgn(i::bndVars)))
case AssignAnyQuantified(i,c,f) =>
val f1@Fn(_,_) = freeVarsAreFns_Term(i::bndVars,f)
AssignAnyQuantified(i,c,f1)
case Check(fm) =>
Check(freeVarsAreFns(bndVars,fm))
case Seq(p,q) =>
Seq(freeVarsAreFns_HP(bndVars,p), freeVarsAreFns_HP(bndVars,q))
case Choose(p,q) =>
Choose(freeVarsAreFns_HP(bndVars,p), freeVarsAreFns_HP(bndVars,q))
case Loop(p,fm, inv_hints) =>
Loop(freeVarsAreFns_HP(bndVars,p),
freeVarsAreFns(bndVars,fm),
inv_hints.map(f => freeVarsAreFns(bndVars,f)))
case Evolve(derivs, fm, inv_hints, sols) =>
Evolve(derivs.map( replace_asgn(bndVars)),
freeVarsAreFns(bndVars,fm),
inv_hints.map(f => freeVarsAreFns(bndVars,f)),
sols.map(f => freeVarsAreFns(bndVars,f)))
case EvolveQuantified(i,c,vs,h, sols) =>
// println("in freevars are fns. h = " + h);
EvolveQuantified(i,
c,
vs.map(replace_asgn(i::bndVars)),
freeVarsAreFns(i::bndVars,h),
sols.map(f => freeVarsAreFns(i::bndVars,f)))
}
val replace_asgn: List[String] => ((Fn, Term)) => (Fn, Term) =
bndVars => vt => {
val (v,t) = vt
val t1 = freeVarsAreFns_Term(bndVars,t)
val v1@Fn(_,_) = freeVarsAreFns_Term(bndVars,v)
(v1,t1)
}
def freeVarsAreFns(bndVars : List[String], fm: Formula) : Formula
= fm match {
case True | False => fm
case Atom(R(r,ps)) =>
Atom(R(r, ps.map(p => freeVarsAreFns_Term(bndVars, p))))
case Not(f) => Not(freeVarsAreFns(bndVars, f))
case Binop(c,f1,f2) =>
Binop(c, freeVarsAreFns(bndVars, f1),freeVarsAreFns(bndVars, f2))
case Quantifier(q, v, c, f) =>
Quantifier(q, v, c, freeVarsAreFns(v :: bndVars, f))
case Modality(m, hp, phi) =>
Modality(m,freeVarsAreFns_HP(bndVars, hp), freeVarsAreFns(bndVars, phi))
}
def formula : Parser[Formula] =
formula00 ^^ {fm => freeVarsAreFns(Nil, fm) }
def hp : Parser[HP] =
hp1*(";" ^^^ {(p1,p2) => Seq(p1,p2)})
def hp1 : Parser[HP] =
hp2*("++" ^^^ {(p1,p2) => Choose(p1,p2)})
def hp2 : Parser[HP] =
"(" ~> hp <~ ")" |
"?" ~> formula00 ^^ { x => Check(x)} |
ident <~ ":=" <~ "*" ^^ { x => AssignAny(Fn(x,Nil))} |
function <~ ":=" <~ "*" ^^ { f => AssignAny(f)} |
(ident <~ ":=") ~ term ^^ {case x ~ t => Assign(List((Fn(x,Nil),t)))} |
(function <~ ":=") ~ term ^^ {case f ~ t => Assign(List((f,t)))} |
(("forall" ~> ident <~ ":") ~
ident ~ function <~ ":=") ~ term ^^
{case i ~ c ~ f ~ v => AssignQuantified(i,St(c),List((f,v)))} |
(("forall" ~> ident <~ ":") ~
ident ~ function <~ ":=") ~ "*" ^^
{case i ~ c ~ f ~ "*" => AssignAnyQuantified(i,St(c),f)} |
// { alpha }*
("{" ~> hp <~ "}" <~ "*") ~ annotation("invariant") ^^
{ case x ~ invs => Loop(x, True, invs)} |
// {x' = theta, ...; h}
("{" ~> rep1sep(diffeq, ",") <~ ";") ~
(formula00 <~ "}") ~ annotation("invariant") ~ annotation("solution") ^^
{case dvs ~ f ~ invs ~ sols => Evolve(dvs,f,invs,sols)} |
// forall i : C f(v)' = theta & h
// XXX TODO figure out how to read apostrophes in a sane way
("forall" ~> ident <~ ":") ~ ident ~
("{" ~> rep1sep(qdiffeq, ",") <~ ";") ~ (formula00 <~ "}") ~ annotation("solution") ^^
{ case i ~ c ~ vs ~ h ~ sols =>
EvolveQuantified(i,St(c),vs,h, sols) }
def diffeq : Parser[(Fn,Term)] =
(ident <~ "=") ~ term ^?
{case s ~ tm if s.endsWith("\'")
=> (Fn(s.substring(0,s.length - 1), Nil), tm)}
def qdiffeq : Parser[(Fn,Term)] =
(function ~ ident <~ "=") ~ term ^?
{case f ~ "\'" ~ tm => (f, tm)}
def annotation(name: String) : Parser[List[Formula]] =
"@" ~> keyword(name) ~> "(" ~> repsep(formula00, ",") <~ ")" |
success(Nil)
def sort : Parser[Sort] =
ident ^^ {case "Real" => Real
case s => St(s)}
def functionsort : Parser[(String,(List[Sort],Sort))] =
(ident <~ ":" <~ "(") ~ (repsep(sort,",") <~ ")" <~ "->") ~ sort ^^
{case f ~ args ~ rtn => (f,(args,rtn))}
def functionsorts : Parser[Map[String,(List[Sort],Sort)]] =
"{" ~> repsep(functionsort, ",") <~ "}" ^^
{case fnsrts => scala.collection.immutable.HashMap.empty ++ fnsrts } |
success(scala.collection.immutable.HashMap.empty)
def sequent : Parser[Sequent] =
functionsorts ~ repsep(formula, ",") ~ ("|-" ~> repsep(formula,",")) ^^
{case fns ~ c ~ s => Sequent(fns, c,s)}
def result : Option[Sequent] = {
val ls = new lexical.Scanner(ins);
phrase(sequent)(ls) match {
case Success(r,next) if next.atEnd =>
println("successful parse! ")
println(r)
Some(r)
case Success(r,next) =>
println("parse failure! Leftover input. only parsed: " )
println(r)
None
case f =>
// println("failure!")
println(f)
None
}
}
def fm_result : Option[Formula] = {
// don't infer var / fn distinction
phrase(formula00)(new lexical.Scanner(ins)) match {
case Success(r,next) if next.atEnd =>
Some(r)
case Success(r,next) =>
println("parse failure! Left over input. only parsed: " )
println(r)
None
case f =>
// println("failure!")
println(f)
None
}
}
}
object P {
def openFile(f:String) : InputStream = {
new FileInputStream(f)
}
def parseFormula(f:String) : Formula = {
val dlp = new DLParser(f)
dlp.fm_result match {
case Some(fm) => fm
case None =>
println("could not read a formula from " + f)
False
}
}
}