-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathBool.hs
228 lines (214 loc) · 5.69 KB
/
Bool.hs
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
{-# LANGUAGE PatternSynonyms #-}
{- |
Copyright : (c) Runtime Verification, 2024
License : BSD-3-Clause
-}
module Booster.Pattern.Bool (
foldAndBool,
isBottom,
negateBool,
splitBoolPredicates,
splitAndBools,
-- patterns
pattern TrueBool,
pattern FalseBool,
pattern NotBool,
pattern AndBool,
pattern EqualsInt,
pattern EqualsBool,
pattern LtInt,
pattern NEqualsInt,
pattern EqualsK,
pattern NEqualsK,
pattern SetIn,
) where
import Data.ByteString.Char8 (ByteString)
import Booster.Definition.Attributes.Base (
FunctionType (..),
SMTType (SMTHook),
SymbolAttributes (SymbolAttributes),
SymbolType (Function),
pattern CanBeEvaluated,
pattern IsNotAssoc,
pattern IsNotIdem,
pattern IsNotMacroOrAlias,
)
import Booster.Pattern.Base (
Pattern,
Predicate (..),
Symbol (Symbol),
Term,
constraints,
pattern DomainValue,
pattern SortBool,
pattern SortInt,
pattern SortK,
pattern SortKItem,
pattern SortSet,
pattern SymbolApplication,
)
import Booster.Pattern.Util (isConcrete)
import Booster.SMT.Base (SExpr (Atom), SMTId (..))
pattern HookedTotalFunction :: ByteString -> SymbolAttributes
pattern HookedTotalFunction hook =
SymbolAttributes
(Function Total)
IsNotIdem
IsNotAssoc
IsNotMacroOrAlias
CanBeEvaluated
Nothing
Nothing
(Just hook)
pattern HookedTotalFunctionWithSMT :: ByteString -> ByteString -> SymbolAttributes
pattern HookedTotalFunctionWithSMT hook smt =
SymbolAttributes
(Function Total)
IsNotIdem
IsNotAssoc
IsNotMacroOrAlias
CanBeEvaluated
Nothing
(Just (SMTHook (Atom (SMTId smt))))
(Just hook)
pattern AndBool :: Term -> Term -> Term
pattern AndBool l r =
SymbolApplication
( Symbol
"Lbl'Unds'andBool'Unds'"
[]
[SortBool, SortBool]
SortBool
(HookedTotalFunctionWithSMT "BOOL.and" "and")
)
[]
[l, r]
pattern NotBool :: Term -> Term
pattern NotBool t =
SymbolApplication
( Symbol
"LblnotBool'Unds'"
[]
[SortBool]
SortBool
(HookedTotalFunctionWithSMT "BOOL.not" "not")
)
[]
[t]
pattern
EqualsInt
, EqualsBool
, LtInt
, NEqualsInt
, EqualsK
, NEqualsK
, SetIn ::
Term -> Term -> Term
pattern EqualsInt a b =
SymbolApplication
( Symbol
"Lbl'UndsEqlsEqls'Int'Unds'"
[]
[SortInt, SortInt]
SortBool
(HookedTotalFunctionWithSMT "INT.eq" "=")
)
[]
[a, b]
pattern LtInt a b =
SymbolApplication
( Symbol
"Lbl'Unds-LT-'Int'Unds'"
[]
[SortInt, SortInt]
SortBool
(HookedTotalFunctionWithSMT "INT.lt" "<")
)
[]
[a, b]
pattern EqualsBool a b =
SymbolApplication
( Symbol
"Lbl'UndsEqlsEqls'Bool'Unds'"
[]
[SortBool, SortBool]
SortBool
(HookedTotalFunctionWithSMT "BOOL.eq" "=")
)
[]
[a, b]
pattern NEqualsInt a b =
SymbolApplication
( Symbol
"Lbl'UndsEqlsSlshEqls'Int'Unds'"
[]
[SortInt, SortInt]
SortBool
(HookedTotalFunctionWithSMT "INT.ne" "distinct")
)
[]
[a, b]
pattern EqualsK a b =
SymbolApplication
( Symbol
"Lbl'UndsEqlsEqls'K'Unds'"
[]
[SortK, SortK]
SortBool
(HookedTotalFunctionWithSMT "KEQUAL.eq" "=")
)
[]
[a, b]
pattern SetIn a b =
SymbolApplication
( Symbol
"LblSet'Coln'in"
[]
[SortKItem, SortSet]
SortBool
(HookedTotalFunction "SET.in")
)
[]
[a, b]
pattern NEqualsK a b =
SymbolApplication
( Symbol
"Lbl'UndsEqlsSlshEqls'K'Unds'"
[]
[SortK, SortK]
SortBool
(HookedTotalFunctionWithSMT "KEQUAL.ne" "distinct")
)
[]
[a, b]
pattern TrueBool, FalseBool :: Term
pattern TrueBool = DomainValue SortBool "true"
pattern FalseBool = DomainValue SortBool "false"
negateBool :: Term -> Term
negateBool TrueBool = FalseBool
negateBool FalseBool = TrueBool
negateBool x = NotBool x
foldAndBool :: [Term] -> Term
foldAndBool [] = TrueBool
foldAndBool [x] = x
foldAndBool (FalseBool : _) = FalseBool
foldAndBool (TrueBool : xs) = foldAndBool xs
foldAndBool (x : xs) = AndBool x $ foldAndBool xs
isBottom :: Pattern -> Bool
isBottom = (Predicate FalseBool `elem`) . constraints
{- | We want to break apart predicates of type `Y1 andBool ... Yn` apart, in case some of the `Y`s are abstract
which prevents the original expression from being fed to the LLVM simplifyBool function
-}
splitBoolPredicates :: Predicate -> [Predicate]
splitBoolPredicates p@(Predicate t)
| isConcrete t = [p]
| otherwise = case t of
AndBool l r -> concatMap (splitBoolPredicates . Predicate) [l, r]
_other -> [p]
{- | Break apart a predicate composed of top-level Y1 andBool ... Yn
(not considering whether any of the subterms is concrete).
-}
splitAndBools :: Predicate -> [Predicate]
splitAndBools p@(Predicate t)
| AndBool l r <- t = concatMap (splitAndBools . Predicate) [l, r]
| otherwise = [p]