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
|
module Language.SMTLIB.Goodies where
import Language.SMTLIB.Types
infixl 7 *%
infixl 6 +%, -%
infix 4 =%, /=%, <%, >%, <=%, >=%
declVars :: [(Symbol, Sort)] -> [Command]
declVars = map (uncurry DeclareConst)
tint :: Int -> Term
tint = TConst . Num
tfloat :: Float -> Term
tfloat = TConst . Dec
tchar :: Char -> Term
tchar = TConst . Str . (: [])
tvar :: Int -> Term
tvar vi = tcomb (var2SMT vi) []
var :: String -> Term
var str = tcomb str []
tcomb :: Ident -> [Term] -> Term
tcomb i ts = TComb (Id i) ts
qtcomb :: QIdent -> [Term] -> Term
qtcomb qi ts = TComb qi ts
forAll :: [Int] -> [Sort] -> Term -> Term
forAll vs ss t = case vs of
[] -> t
_ -> Forall (zipWith SV (map var2SMT vs) ss) t
tneg :: Term -> Term
tneg t = tcomb "-" [t]
tabs :: Term -> Term
tabs t = tcomb "abs" [t]
(+%) :: Term -> Term -> Term
t1 +% t2 = tcomb "+" [t1, t2]
(-%) :: Term -> Term -> Term
t1 -% t2 = tcomb "-" [t1, t2]
(*%) :: Term -> Term -> Term
t1 *% t2 = tcomb "*" [t1, t2]
(/%) :: Term -> Term -> Term
t1 /% t2 = tcomb "/" [t1, t2]
true :: Term
true = qtcomb (As "true" boolSort) []
false :: Term
false = qtcomb (As "false" boolSort) []
(=%) :: Term -> Term -> Term
t1 =% t2 = tcomb "=" [t1, t2]
(/=%) :: Term -> Term -> Term
t1 /=% t2 = tcomb "not" [tcomb "=" [t1, t2]]
(<%) :: Term -> Term -> Term
t1 <% t2 = tcomb "<" [t1, t2]
(<=%) :: Term -> Term -> Term
t1 <=% t2 = tcomb "<=" [t1, t2]
(>%) :: Term -> Term -> Term
t1 >% t2 = tcomb ">" [t1, t2]
(>=%) :: Term -> Term -> Term
t1 >=% t2 = tcomb ">=" [t1, t2]
tand :: [Term] -> Term
tand = tcomb "and"
tor :: [Term] -> Term
tor = tcomb "or"
(==>) :: Term -> Term -> Term
t1 ==> t2 = tcomb "=>" [t1, t2]
tnot :: Term -> Term
tnot t = tcomb "not" [t]
instance Num Term where
t1 + t2 = t1 +% t2
t1 - t2 = t1 -% t2
t1 * t2 = t1 *% t2
negate = tneg
abs = tabs
fromInt = tint
instance Fractional Term where
t1 / t2 = t1 /% t2
fromFloat = tfloat
scomb :: Ident -> [Sort] -> Sort
scomb i ss = SComb i ss
orderingSort :: Sort
orderingSort = scomb "Ordering" []
boolSort :: Sort
boolSort = scomb "Bool" []
intSort :: Sort
intSort = scomb "Int" []
floatSort :: Sort
floatSort = scomb "Real" []
funSC :: [Sort] -> Sort
funSC = scomb "Fun"
nop :: Command
nop = echo ""
assert :: [Term] -> Command
assert ts = case ts of
[] -> nop
[t] -> Assert t
_ -> Assert $ tand ts
unqual :: QIdent -> Ident
unqual (Id i) = i
unqual (As i _) = i
isDeclData :: Command -> Bool
isDeclData cmd = case cmd of
DeclareDatatype _ _ -> True
DeclareDatatypes _ -> True
_ -> False
isEcho :: Command -> Bool
isEcho cmd = case cmd of
Echo _ -> True
_ -> False
echo :: String -> Command
echo str = Echo ('@' : str)
comment :: String -> Command
= Comment
var2SMT :: Int -> Symbol
var2SMT vi = 'x' : show (abs vi)
|