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 *> return SMT.SuccessRsp
<|> terminal KW_sat *> return (SMT.CheckSatRsp SMT.Sat)
<|> terminal KW_unsat *> return (SMT.CheckSatRsp SMT.Unsat)
<|> terminal KW_unknown *> return (SMT.CheckSatRsp SMT.Unknown)
<|> terminal KW_unsupported *> return 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
<|> return SMT.EchoRsp <*> parseStr
<|> return SMT.GetOptionRsp <*> parseAttrValue
<|> return SMT.GetProofRsp <*> parseSExpr
parseGetAssertionsRsp :: SMTParser SMT.CmdResponse
parseGetAssertionsRsp = return SMT.GetAssertionsRsp <*> many parseTerm
parseGetAssignmentRsp :: SMTParser SMT.CmdResponse
parseGetAssignmentRsp = return SMT.GetAssignmentRsp <*> many parseTValPair
parseTValPair :: SMTParser (SMT.Symbol, Bool)
parseTValPair =
terminal LParen *> return (,) <*> parseSym <*> parseBool <* terminal RParen
parseGetInfoRsp :: SMTParser SMT.CmdResponse
parseGetInfoRsp = return SMT.GetInfoRsp <*> some parseInfoRsp
parseInfoRsp :: SMTParser SMT.InfoRsp
parseInfoRsp
= terminal Colon *> terminal KW_assertion_stack_levels
*> return SMT.AssertionStackLevelsRsp <*> parseNum
<|> terminal Colon *> terminal KW_authors *> return SMT.AuthorsRsp <*> parseStr
<|> terminal Colon *> terminal KW_error_behavior *> return SMT.ErrorBehaviorRsp
<*> parseErrorBehavior
<|> terminal Colon *> terminal KW_name *> return SMT.NameRsp <*> parseStr
<|> terminal Colon *> terminal KW_reason_unknown *> return SMT.ReasonUnknownRsp
<*> parseReasonUnknown
<|> terminal Colon *> terminal KW_version *> return SMT.VersionRsp <*> parseStr
<|> return SMT.AttrRsp <*> parseAttribute
parseErrorBehavior :: SMTParser SMT.ErrorBehavior
parseErrorBehavior
= terminal KW_immediate_exit *> return SMT.ImmediateExit
<|> terminal KW_continued_execution *> return SMT.ContinuedExecution
parseReasonUnknown :: SMTParser SMT.ReasonUnknown
parseReasonUnknown = terminal KW_memout *> return SMT.Memout
<|> terminal KW_incomplete *> return SMT.Incomplete
<|> return SMT.SEReason <*> parseSExpr
parseGetModelRsp :: SMTParser SMT.CmdResponse
parseGetModelRsp =
terminal KW_model *> return SMT.GetModelRsp <*> many parseModelRsp
parseModelRsp :: SMTParser SMT.ModelRsp
parseModelRsp
= terminal LParen *> terminal KW_def_fun *> return SMT.MRFun <*> parseFunDef
<* terminal RParen
<|> terminal LParen *> terminal KW_def_fun_rec *> return SMT.MRFunRec
<*> parseFunDef <* terminal RParen
<|> terminal LParen *> terminal KW_def_funs_rec
*> return ((SMT.MRFunsRec .) . zip) <* terminal LParen
<*> some parseFunDec <* terminal RParen
<* terminal LParen <*> some parseTerm <* terminal RParen
<* terminal RParen
parseFunDef :: SMTParser SMT.FunDef
parseFunDef = return SMT.FunDef <*> parseSym <* terminal LParen
<*> many parseSortedVar <* terminal RParen <*> parseSort <*> parseTerm
parseFunDec :: SMTParser SMT.FunDec
parseFunDec
= terminal LParen *> return SMT.FunDec <*> parseSym <* terminal LParen
<*> many parseSortedVar <* terminal RParen <*> parseSort <* terminal RParen
parseGetValueRsp :: SMTParser SMT.CmdResponse
parseGetValueRsp = return SMT.GetValueRsp <*> some parseValPair
parseErrorRsp :: SMTParser SMT.CmdResponse
parseErrorRsp = terminal KW_error *> return 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 *> return (,) <*> parseTerm <*> parseTerm <* terminal RParen
parseTerm :: SMTParser SMT.Term
parseTerm = SMT.TConst . SMT.Str <$> parseStr <|> (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
= return (SMT.TConst . SMT.Num) <*> parseNum <* terminal RParen
<|> terminal KW_as *> return SMT.TComb <*> (return SMT.As <*> parseSym
<*> parseSort) <*> return [] <* terminal RParen
<|> return SMT.TComb <*> parseQIdent <*> some parseTerm <* terminal RParen
<|> terminal KW_let *> return SMT.Let <* terminal LParen <*> some parseVarBind
<* terminal RParen <*> parseTerm <* terminal RParen
<|> terminal KW_forall *> return SMT.Forall <* terminal LParen
<*> some parseSortedVar <* terminal RParen
<*> parseTerm <* terminal RParen
<|> terminal KW_exists *> return SMT.Exists <* terminal LParen
<*> some parseSortedVar <* terminal RParen
<*> parseTerm <* terminal RParen
<|> terminal Bang *> return SMT.Annot <*> parseTerm <*> some parseAttribute
<* terminal RParen
parseVarBind :: SMTParser (SMT.Symbol, SMT.Term)
parseVarBind =
terminal LParen *> return (,) <*> parseSym <*> parseTerm <* terminal RParen
parseSortedVar :: SMTParser SMT.SortedVar
parseSortedVar =
terminal LParen *> return 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 (return (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 (return 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 (return (SMT.SEKW . SMT.KW) <*> parseSym)) ts
LParen : ts -> (runParser (return 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 *> return 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 (return SMT.SComb <*> parseSym <*> some parseSort
<* terminal RParen)) ts
t : ts -> (runParser (unexpected t)) ts
|