forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TokenParserBoolExpr.fs
307 lines (262 loc) · 9.29 KB
/
TokenParserBoolExpr.fs
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
module Fantomas.TokenParserBoolExpr
#nowarn "40"
[<RequireQualifiedAccess>]
type BoolExpr =
| Ident of string
| And of BoolExpr * BoolExpr
| Or of BoolExpr * BoolExpr
| Not of BoolExpr
module BoolExpr =
let rec map f e =
match f e with
| Some x -> x
| None ->
match e with
| BoolExpr.Not e -> BoolExpr.Not(map f e)
| BoolExpr.And(e1, e2) -> BoolExpr.And(map f e1, map f e2)
| BoolExpr.Or(e1, e2) -> BoolExpr.Or(map f e1, map f e2)
| _ -> e
let rec forall f e =
f e && match e with
| BoolExpr.Not e -> forall f e
| BoolExpr.And(e1, e2)
| BoolExpr.Or(e1, e2) -> forall f e1 && forall f e2
| _ -> true
let normalizeCNF expr =
let mapUntilNotChanged mapFunctions expr =
let oneStep e = mapFunctions |> Seq.fold (fun e f -> map f e) e
expr
|> Seq.unfold (fun e ->
let e' = oneStep e
if e = e' then None
else Some(e', e'))
|> Seq.tryLast
|> Option.defaultValue expr
let doubleNegative =
function
| BoolExpr.Not(BoolExpr.Not(e)) -> Some e
| _ -> None
let deMorgan =
function
| BoolExpr.Not(BoolExpr.And(e1, e2)) -> Some(BoolExpr.Or(BoolExpr.Not e1, BoolExpr.Not e2))
| BoolExpr.Not(BoolExpr.Or(e1, e2)) -> Some(BoolExpr.And(BoolExpr.Not e1, BoolExpr.Not e2))
| _ -> None
let expandOr =
function
| BoolExpr.Or(e1, BoolExpr.And(e2, e3))
| BoolExpr.Or(BoolExpr.And(e2, e3), e1) -> Some(BoolExpr.And(BoolExpr.Or(e1, e2), BoolExpr.Or(e1, e3)))
| _ -> None
expr |> mapUntilNotChanged [ doubleNegative; deMorgan; expandOr ]
type Literal =
| Positive of string
| Negative of string
type SATSolveResult =
| Satisfiable of string list
| Unsatisfiable
| Unconclusive
// result: list of AND-connected terms; term - OR-connected Literals
let toFlatCNF expr =
let e = expr |> normalizeCNF
let rec toAndList =
function
| BoolExpr.And(e1, e2) -> toAndList e1 @ toAndList e2
| e -> [ e ]
let rec toOrList =
function
| BoolExpr.Or(e1, e2) -> toOrList e1 @ toOrList e2
| e -> [ e ]
let splitByNeg xs =
xs
|> List.map (function
| BoolExpr.Not(BoolExpr.Ident x) -> Negative x
| BoolExpr.Ident x -> Positive x
| _ -> failwithf "Expr not in CNF: %A" e)
|> set
e
|> toAndList
|> List.map (toOrList >> splitByNeg)
let eval cnf vals =
let vals = set vals
let evalTerm s =
Set.intersect vals s
|> Set.isEmpty
|> not
cnf |> List.forall evalTerm
let trySolveSAT maxSteps cnf =
let allLiterals =
cnf
|> Seq.collect id
|> Seq.toList
let groupedLiterals ls =
ls
|> Seq.groupBy (function
| Positive x
| Negative x -> x)
|> Seq.map (fun (key, g) ->
key,
g
|> Seq.distinct
|> Seq.toList)
|> Seq.toList
let enforcedLit =
cnf
|> List.filter (fun t -> Set.count t = 1)
|> List.collect Set.toList
if groupedLiterals enforcedLit |> Seq.exists (fun (_, g) -> List.length g > 1) then Unsatisfiable
else
let (singletons, toSolve) =
groupedLiterals allLiterals |> List.partition (fun (_, g) -> List.length g = 1)
let singletonsLit = singletons |> List.collect snd
let solvedSet = set (enforcedLit @ singletonsLit)
let toSolve =
toSolve
|> List.filter
(fun (k, _) -> not (Set.contains (Positive k) solvedSet || Set.contains (Negative k) solvedSet))
let rec genCombinations groups =
seq {
match groups with
| [] -> yield []
| g :: gs -> yield! genCombinations gs |> Seq.collect (fun l -> g |> Seq.map (fun x -> x :: l))
}
let solve vals groups =
let combinations = genCombinations (groups |> List.map snd)
combinations
|> Seq.mapi (fun i comb ->
if i > maxSteps then Some Unconclusive
else
let vals = vals @ comb
if eval cnf vals then
Some
(Satisfiable
(vals
|> List.choose (function
| Positive x -> Some x
| _ -> None)))
else None)
|> Seq.tryPick id
|> Option.defaultValue Unsatisfiable
solve (Seq.toList solvedSet) toSolve
let mergeBoolExprs maxSolveSteps exprs =
let solve e =
e
|> toFlatCNF
|> trySolveSAT maxSolveSteps
|> function
| Satisfiable x -> Some x
| _ -> None
let pairSolve e1 e2 =
BoolExpr.And(e1, e2) |> solve //|> Dbg.tee (fun r -> printfn "%A: %A" (BoolExpr.And(e1, e2)) r)
let allPairs xs =
xs
|> Seq.mapi (fun i x ->
xs
|> Seq.mapi (fun j y ->
if i < j then Some(x, y)
else None))
|> Seq.collect id
|> Seq.choose id
let exprs = exprs |> List.map (fun x -> x, None)
let rec f exprs =
let exprsIndexed = exprs |> Seq.mapi (fun i x -> i, x)
match exprsIndexed
|> allPairs
|> Seq.tryPick
(fun ((i, (x, _)), (j, (y, _))) -> pairSolve x y |> Option.map (fun r -> (i, x), (j, y), r)) with
| None -> exprs
| Some((i, x), (j, y), r) ->
f
((exprsIndexed
|> Seq.filter (fun (k, _) -> i <> k && j <> k)
|> Seq.map snd
|> Seq.toList)
@ [ BoolExpr.And(x, y), Some r ])
let r =
f exprs
|> List.choose (fun (e, r) ->
r
|> Option.orElseWith (fun () -> solve e)
|> Option.map (fun x -> e, x))
r
let solveDefinesForExpr maxSolveSteps e =
e
|> toFlatCNF
|> trySolveSAT maxSolveSteps
|> function
| Satisfiable x -> Some x
| _ -> None
module BoolExprParser =
let (|Eq|_|) x y =
if x = y then Some()
else None
let (|TakeUntil|_|) x xs =
match List.takeWhile ((<>) x) xs with
| y when y = xs -> None
| y -> Some(y, List.skipWhile ((<>) x) xs)
let (|ListSurround|_|) before after xs =
let rec f d acc xs =
match xs with
| _ when d < 0 -> None
| Eq before :: rest -> f (d + 1) (before :: acc) rest
| [ Eq after ] when d = 1 -> List.rev acc |> Some
| Eq after :: rest -> f (d - 1) (after :: acc) rest
| x :: rest -> f d (x :: acc) rest
| _ -> None
match xs with
| Eq before :: rest -> f 1 [] rest
| _ -> None
let (|ListSplit|_|) split xs =
match xs with
| TakeUntil split (x1, (_ :: x2)) -> Some(x1, x2)
| _ -> None
let (|ListSplitPick|_|) split f xs =
let rec loop prev xs =
seq {
match xs with
| TakeUntil split (x1, (_ :: x2)) ->
yield (prev @ x1, x2)
yield! loop (prev @ x1 @ [ split ]) x2
| _ -> ()
}
loop [] xs |> Seq.tryPick f
let rec (|SubExpr|_|) =
function
| ListSurround "(" ")" (ExprPat e) -> Some e
| _ -> None
and (|AndExpr|_|) =
let chooser =
function
| (ExprPat e1, ExprPat e2) -> Some(e1, e2)
| _ -> None
function
| ListSplitPick "&&" chooser (e1, e2) -> Some(BoolExpr.And(e1, e2))
| _ -> None
and (|OrExpr|_|) =
let chooser =
function
| (ExprPat e1, ExprPat e2) -> Some(e1, e2)
| _ -> None
function
| ListSplitPick "||" chooser (e1, e2) -> Some(BoolExpr.Or(e1, e2))
| _ -> None
and (|NotSubExpr|_|) =
function
| "!" :: SubExpr e -> Some(BoolExpr.Not e)
| _ -> None
and (|NotIdentExpr|_|) =
function
| "!" :: [x] -> Some(BoolExpr.Not(BoolExpr.Ident x))
| _ -> None
and (|ExprPat|_|) =
function
| NotSubExpr e
| SubExpr e
| AndExpr e
| OrExpr e
| NotIdentExpr e -> Some e
| [x] -> BoolExpr.Ident x |> Some
| _ -> None
let parse =
function
| [] -> None
| ExprPat e -> Some e
| s -> failwithf "Fail to parse bool expr in #if: %A" s