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
|
module Language.SMTLIB.Parser where
import ParserComb
import Language.SMTLIB.Scanner
import qualified Language.SMTLIB.Types as SMT
type SMTParser a = Parser Token a
parseCmdRsps :: String -> Either String [SMT.CmdResponse]
parseCmdRsps str = case (runParser (many parseResponse) . scan) str of
Left msg -> Left msg
Right ([], rsps) -> Right rsps
Right (ts, _) -> Left $ "Incomplete parse: " ++ show ts
parseResponse :: SMTParser SMT.CmdResponse
parseResponse = terminal KW_success *> yield SMT.SuccessRsp
<|> terminal KW_sat *> yield (SMT.CheckSatRsp SMT.Sat)
<|> terminal KW_unsat *> yield (SMT.CheckSatRsp SMT.Unsat)
<|> terminal KW_unknown *> yield (SMT.CheckSatRsp SMT.Unknown)
<|> terminal KW_unsupported *> yield SMT.UnsupportedRsp
<|> terminal LParen *> parseErrorRsp <* terminal RParen
<|> terminal LParen *> parseGetValueRsp <* terminal RParen
<|> terminal LParen *> parseGetModelRsp <* terminal RParen
<|> terminal LParen *> parseGetAssertionsRsp <* terminal RParen
<|> terminal LParen *> parseGetAssignmentRsp <* terminal RParen
<|> terminal LParen *> parseGetInfoRsp <* terminal RParen
<|> yield SMT.EchoRsp <*> parseStr
<|> yield SMT.GetOptionRsp <*> parseAttrValue
<|> yield SMT.GetProofRsp <*> parseSExpr
parseGetAssertionsRsp :: SMTParser SMT.CmdResponse
parseGetAssertionsRsp = yield SMT.GetAssertionsRsp <*> many parseTerm
parseGetAssignmentRsp :: SMTParser SMT.CmdResponse
parseGetAssignmentRsp = yield SMT.GetAssignmentRsp <*> many parseTValPair
parseTValPair :: SMTParser (SMT.Symbol, Bool)
parseTValPair =
terminal LParen *> yield (,) <*> parseSym <*> parseBool <* terminal RParen
parseGetInfoRsp :: SMTParser SMT.CmdResponse
parseGetInfoRsp = yield SMT.GetInfoRsp <*> some parseInfoRsp
parseInfoRsp :: SMTParser SMT.InfoRsp
parseInfoRsp
= terminal Colon *> terminal KW_assertion_stack_levels
*> yield SMT.AssertionStackLevelsRsp <*> parseNum
<|> terminal Colon *> terminal KW_authors *> yield SMT.AuthorsRsp <*> parseStr
<|> terminal Colon *> terminal KW_error_behavior *> yield SMT.ErrorBehaviorRsp
<*> parseErrorBehavior
<|> terminal Colon *> terminal KW_name *> yield SMT.NameRsp <*> parseStr
<|> terminal Colon *> terminal KW_reason_unknown *> yield SMT.ReasonUnknownRsp
<*> parseReasonUnknown
<|> terminal Colon *> terminal KW_version *> yield SMT.VersionRsp <*> parseStr
<|> yield SMT.AttrRsp <*> parseAttribute
parseErrorBehavior :: SMTParser SMT.ErrorBehavior
parseErrorBehavior
= terminal KW_immediate_exit *> yield SMT.ImmediateExit
<|> terminal KW_continued_execution *> yield SMT.ContinuedExecution
parseReasonUnknown :: SMTParser SMT.ReasonUnknown
parseReasonUnknown = terminal KW_memout *> yield SMT.Memout
<|> terminal KW_incomplete *> yield SMT.Incomplete
<|> yield SMT.SEReason <*> parseSExpr
parseGetModelRsp :: SMTParser SMT.CmdResponse
parseGetModelRsp =
terminal KW_model *> yield SMT.GetModelRsp <*> many parseModelRsp
parseModelRsp :: SMTParser SMT.ModelRsp
parseModelRsp
= terminal LParen *> terminal KW_def_fun *> yield SMT.MRFun <*> parseFunDef
<* terminal RParen
<|> terminal LParen *> terminal KW_def_fun_rec *> yield SMT.MRFunRec
<*> parseFunDef <* terminal RParen
<|> terminal LParen *> terminal KW_def_funs_rec
*> yield ((SMT.MRFunsRec .) . zip) <* terminal LParen
<*> some parseFunDec <* terminal RParen
<* terminal LParen <*> some parseTerm <* terminal RParen
<* terminal RParen
parseFunDef :: SMTParser SMT.FunDef
parseFunDef = yield SMT.FunDef <*> parseSym <* terminal LParen
<*> many parseSortedVar <* terminal RParen <*> parseSort <*> parseTerm
parseFunDec :: SMTParser SMT.FunDec
parseFunDec
= terminal LParen *> yield SMT.FunDec <*> parseSym <* terminal LParen
<*> many parseSortedVar <* terminal RParen <*> parseSort <* terminal RParen
parseGetValueRsp :: SMTParser SMT.CmdResponse
parseGetValueRsp = yield SMT.GetValueRsp <*> some parseValPair
parseErrorRsp :: SMTParser SMT.CmdResponse
parseErrorRsp = terminal KW_error *> yield SMT.ErrorRsp <*> parseStr
parseBool :: SMTParser Bool
parseBool = Parser $ \tokens -> case tokens of
[] -> (runParser eof) tokens
BVal b : ts -> Right (ts, b)
t : ts -> (runParser (unexpected t)) ts
parseNum :: SMTParser SMT.Numeral
parseNum = Parser $ \tokens -> case tokens of
[] -> (runParser eof) tokens
Num n : ts -> Right (ts, n)
t : ts -> (runParser (unexpected t)) ts
parseStr :: SMTParser String
parseStr = Parser $ \tokens -> case tokens of
[] -> runParser eof tokens
DQuote : ts -> let (toks, _:rs) = span (/= DQuote) ts
in Right (rs, unwords (map toStr toks))
t : ts -> runParser (unexpected t) ts
where
toStr tok = case tok of
Num n -> show n
Id s -> s
BVal b -> show b
Colon -> ":"
Semi -> ";"
Comma -> ","
Bang -> "!"
Underscore -> "_"
LParen -> "("
RParen -> ")"
t -> show t
parseSym :: SMTParser SMT.Symbol
parseSym = Parser $ \tokens -> case tokens of
[] -> (runParser eof) tokens
Id s : ts -> Right (ts, s)
t : ts -> (runParser (unexpected t)) ts
parseValPair :: SMTParser SMT.ValuationPair
parseValPair =
terminal LParen *> yield (,) <*> parseTerm <*> parseTerm <* terminal RParen
parseTerm :: SMTParser SMT.Term
parseTerm = Parser $ \tokens -> case tokens of
[] -> (runParser eof) tokens
Num n : ts -> Right (ts, SMT.TConst (SMT.Num n))
BVal b : ts -> let s = case b of
True -> "true"
False -> "false"
in Right (ts, SMT.TComb (SMT.Id s) [])
Id s : ts -> Right (ts, SMT.TComb (SMT.Id s) [])
LParen : ts -> (runParser parseParenTerm) ts
t : ts -> (runParser (unexpected t)) ts
parseParenTerm :: SMTParser SMT.Term
parseParenTerm
= yield (SMT.TConst . SMT.Num) <*> parseNum <* terminal RParen
<|> terminal KW_as *> yield SMT.TComb <*> (yield SMT.As <*> parseSym
<*> parseSort) <*> yield [] <* terminal RParen
<|> yield SMT.TComb <*> parseQIdent <*> some parseTerm <* terminal RParen
<|> terminal KW_let *> yield SMT.Let <* terminal LParen <*> some parseVarBind
<* terminal RParen <*> parseTerm <* terminal RParen
<|> terminal KW_forall *> yield SMT.Forall <* terminal LParen
<*> some parseSortedVar <* terminal RParen
<*> parseTerm <* terminal RParen
<|> terminal KW_exists *> yield SMT.Exists <* terminal LParen
<*> some parseSortedVar <* terminal RParen
<*> parseTerm <* terminal RParen
<|> terminal Bang *> yield SMT.Annot <*> parseTerm <*> some parseAttribute
<* terminal RParen
parseVarBind :: SMTParser (SMT.Symbol, SMT.Term)
parseVarBind =
terminal LParen *> yield (,) <*> parseSym <*> parseTerm <* terminal RParen
parseSortedVar :: SMTParser SMT.SortedVar
parseSortedVar =
terminal LParen *> yield SMT.SV <*> parseSym <*> parseSort <* terminal RParen
parseAttribute :: SMTParser SMT.Attribute
parseAttribute = Parser $ \tokens -> case tokens of
[] -> (runParser eof) tokens
Colon : (Id s : (RParen : ts)) -> Right (RParen : ts, SMT.AKW (SMT.KW s))
Colon : (Id s : ts) -> (runParser (yield (SMT.AVal (SMT.KW s))
<*> parseAttrValue)) ts
t : ts -> (runParser (unexpected t)) ts
parseAttrValue :: SMTParser SMT.AttrValue
parseAttrValue = Parser $ \tokens -> case tokens of
[] -> (runParser eof) tokens
Num n : ts -> Right (ts, SMT.AVConst (SMT.Num n))
Id s : ts -> Right (ts, SMT.AVSym s)
LParen : ts -> (runParser (yield SMT.AVSExpr <*> many parseSExpr
<* terminal RParen)) ts
t : ts -> (runParser (unexpected t)) ts
parseSExpr :: SMTParser SMT.SExpr
parseSExpr = Parser $ \tokens -> case tokens of
[] -> (runParser eof) tokens
Num n : ts -> Right (ts, SMT.SEConst (SMT.Num n))
Id s : ts -> Right (ts, SMT.SESym s)
Colon : ts -> (runParser (yield (SMT.SEKW . SMT.KW) <*> parseSym)) ts
LParen : ts -> (runParser (yield SMT.SEList <*> many parseSExpr
<* terminal RParen)) ts
t : ts -> (runParser (unexpected t)) ts
parseQIdent :: SMTParser SMT.QIdent
parseQIdent = Parser $ \tokens -> case tokens of
[] -> (runParser eof) tokens
Id s : ts -> Right (ts, SMT.Id s)
LParen : ts -> (runParser (terminal KW_as *> yield SMT.As <*> parseSym
<*> parseSort <* terminal RParen)) ts
t : ts -> (runParser (unexpected t)) ts
parseSort :: SMTParser SMT.Sort
parseSort = Parser $ \tokens -> case tokens of
[] -> (runParser eof) tokens
Id s : ts -> Right (ts, SMT.SComb s [])
LParen : ts -> (runParser (yield SMT.SComb <*> parseSym <*> some parseSort
<* terminal RParen)) ts
t : ts -> (runParser (unexpected t)) ts
|