CoAlgReasoner.ml 25.4 KB
Newer Older
Thorsten Wißmann's avatar
Thorsten Wißmann committed
1
2
3
4
5
6
7
8
9
10
11
(** A graph-tableau-based decision procedure framework for coalgebraic logics.
    @author Florian Widmann
 *)


open CoAlgMisc

module M = Minisat

type sortTable = CoAlgMisc.sortTable

12
13
14
15
16
17
type nomTable = string -> CoAlgFormula.sort option

type assumptions = CoAlgFormula.sortedFormula list

type input = sortTable * nomTable * assumptions * CoAlgFormula.sortedFormula

Thorsten Wißmann's avatar
Thorsten Wißmann committed
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

(*****************************************************************************)
(*                     Propagation of Satisfiability                         *)
(*****************************************************************************)

let propSatFindSucc setCnstr cset =
  if csetHasDot cset then false
  else
    match graphFindCnstr cset with
      | None -> assert false
      | Some SatC -> true
      | Some (OpenC _) -> setMemCnstr setCnstr cset
      | Some (UnexpandedC _)
      | Some UnsatC -> false

let rec propSat setStates setCores setCnstr = function
  | [] -> ()
  | propElem::tl ->
      let tl1 =
        match propElem with
        | UState (state, _) ->
            if setMemState setStates state then
              let cnstrs = stateGetConstraints state in
              let isStillOpen = cssExists (fun c -> propSatFindSucc setCnstr c) cnstrs in
              if isStillOpen then () else setRemoveState setStates state
            else ();
            tl
        | UCore (core, _) ->
            if setMemCore setCores core then
              let cnstrs = coreGetConstraints core in
              let isStillOpen = cssExists (fun c -> propSatFindSucc setCnstr c) cnstrs in
              if isStillOpen then tl else begin
                setRemoveCore setCores core;
                let cnstrPar = coreGetConstraintParents core in
                List.fold_left (fun acc cset -> (UCnstr cset)::acc) tl cnstrPar
              end
            else tl
        | UCnstr cset ->
            if setMemCnstr setCnstr cset then begin
              setRemoveCnstr setCnstr cset;
              match graphFindCnstr cset with
              | Some (OpenC nodes) ->
                  let prop acc node =
                    match node with
                    | Core core -> (UCore (core, true))::acc
                    | State state -> (UState (state, None))::acc
                  in
                  List.fold_left prop tl nodes
              | _ -> assert false
            end else tl
      in
      propSat setStates setCores setCnstr tl1

let propagateSat () =
  let setStates = setEmptyState () in
  let setCores = setEmptyCore () in
  let setCnstr = setEmptyCnstr () in
  let fktS1 state =
    match stateGetStatus state with
    | Expandable
    | Open -> setAddState setStates state
    | Unsat
    | Sat -> ()
  in
  graphIterStates fktS1;
  let fktC1 core =
    match coreGetStatus core with
    | Expandable
    | Open -> setAddCore setCores core
    | Unsat
    | Sat -> ()
  in
  graphIterCores fktC1;
  let cfkt1 cset cnstr =
    if csetHasDot cset then ()
    else
      match cnstr with
      | OpenC _ -> setAddCnstr setCnstr cset
      | UnsatC
      | SatC
      | UnexpandedC _ -> ()
  in
  graphIterCnstrs cfkt1;
  graphIterStates (fun state -> propSat setStates setCores setCnstr [UState (state, None)]);
  graphIterCores (fun core -> propSat setStates setCores setCnstr [UCore (core, false)]);
  setIterState (fun state -> stateSetStatus state Sat) setStates;
  let setCoreSat core =
    coreSetStatus core Sat;
    if core == graphGetRoot () then raise (CoAlg_finished true) else ()
  in
  setIterCore setCoreSat setCores;
  setIterCnstr (fun cset -> graphReplaceCnstr cset SatC) setCnstr


(*****************************************************************************)
(*                     Propagation of Unsatisfiability                       *)
(*****************************************************************************)

let isConstraintUnsat cset =
  match graphFindCnstr cset with
  | None -> assert false
  | Some UnsatC -> true
  | _ -> false

122
123
124
125
126
127
128
129
130
131
132
133
134
(* look up the formula f in the hash-table fht, while knowing that it
   must be found *)
let mustWork (f: 'a -> 'b option): 'a -> 'b =
    fun x -> match (f x) with
             | Some l -> l
             | None -> assert false

let mustWork2 (f: 'a -> 'b -> 'c option): 'a -> 'b -> 'c =
    fun x -> mustWork (f x)

let fhtMustFind : fht -> localFormula -> M.lit =
    mustWork2 fhtFind
    (* fun fht f ->
Thorsten Wißmann's avatar
Thorsten Wißmann committed
135
136
137
  match fhtFind fht f with
  | Some l -> l
  | None -> assert false
138
  *)
Thorsten Wißmann's avatar
Thorsten Wißmann committed
139

140
141
let lhtMustFind : lht -> M.lit -> localFormula = mustWork2 lhtFind
  (*match lhtFind lht l with
Thorsten Wißmann's avatar
Thorsten Wißmann committed
142
143
  | Some f -> f
  | None -> assert false
144
145
  *)

Thorsten Wißmann's avatar
Thorsten Wißmann committed
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407

let rec propagateUnsat = function
  | [] -> ()
  | propElem::tl ->
      let tl1 =
        match propElem with
        | UState (state, idxopt) -> begin
            match stateGetStatus state with
            | Unsat -> tl
            | Sat -> assert false
            | Open
            | Expandable ->
                let mbs =
                  match idxopt with
                  | None -> stateGetBs state
                  | Some idx ->
                      let (dep, children) = stateGetRule state idx in
                      let getBs core =
                        assert (coreGetStatus core = Unsat);
                        coreGetBs core
                      in
                      dep (List.map getBs children)
                in
                stateSetBs state mbs;
                stateSetStatus state Unsat;
                let prop acc core =
                  let turnsUnsat =
                    match coreGetStatus core with
                    | Open -> List.for_all (fun s -> stateGetStatus s = Unsat) (coreGetChildren core)
                    | Expandable
                    | Unsat
                    | Sat -> false
                  in
                  if turnsUnsat then (UCore (core, false))::acc else acc
                in
                List.fold_left prop tl (stateGetParents state)
          end
        | UCore (core, comesFromCnstr) -> begin
            match coreGetStatus core with
            | Unsat -> tl
            | Sat -> assert false
            | Open
            | Expandable ->
                let mbs =
                  if comesFromCnstr then coreGetBs core
                  else
                    let bs = coreGetBs core in
                    let solver = coreGetSolver core in
                    let fht = coreGetFht core in
                    let lht = lhtInit () in
                    let addLits f acc =
                      let lit = fhtMustFind fht f in
                      lhtAdd lht lit f;
                      lit::acc
                    in
                    let litset = bsetFold addLits bs [] in
                    let addClauses state =
                      let cbs = stateGetBs state in
                      let clause = bsetFold (fun f acc -> (M.neg_lit (fhtMustFind fht f))::acc) cbs [] in
                      let okay = M.add_clause solver clause in
                      assert okay
                    in
                    List.iter addClauses (coreGetChildren core);
                    let isSat = M.invoke_solver solver litset in
                    assert (not isSat);
                    let res = bsetMake () in
                    let confls = M.get_conflict solver in
                    List.iter (fun l -> bsetAdd res (lhtMustFind lht l)) confls;
                    res
                in
                coreSetBs core mbs;
                coreSetStatus core Unsat;
                if core == graphGetRoot () then raise (CoAlg_finished false) else ();
                let prop acc (state, idx) =
                  let turnsUnsat =
                    match stateGetStatus state with
                    | Open
                    | Expandable ->
                        List.for_all (fun c -> coreGetStatus c = Unsat) (snd (stateGetRule state idx))
                    | Unsat
                    | Sat -> false
                  in
                  if turnsUnsat then (UState (state, Some idx))::acc else acc
                in
                let tl2 = List.fold_left prop tl (coreGetParents core) in
                List.fold_left (fun acc cset -> (UCnstr cset)::acc) tl2 (coreGetConstraintParents core)
          end
        | UCnstr cset ->
            match graphFindCnstr cset with
            | None
            | Some SatC -> assert false
            | Some UnsatC -> tl
            | Some (UnexpandedC nodes)
            | Some (OpenC nodes) ->
                graphReplaceCnstr cset UnsatC;
                let prop acc node =
                  match node with
                  | State state ->
                      let turnsUnsat = 
                        match stateGetStatus state with
                        | Expandable
                        | Open -> cssForall isConstraintUnsat (stateGetConstraints state)
                        | Unsat
                        | Sat -> false
                      in
                      if turnsUnsat then (UState (state, None))::acc else acc
                  | Core core ->
                      let turnsUnsat =
                        match coreGetStatus core with
                        | Expandable
                        | Open -> cssForall isConstraintUnsat (coreGetConstraints core)
                        | Unsat
                        | Sat -> false
                      in
                      if turnsUnsat then (UCore (core, true))::acc else acc
                in
                List.fold_left prop tl nodes
      in
      propagateUnsat tl1


(*****************************************************************************)
(*                   Propagation of Nominal Constraints                      *)
(*****************************************************************************)

let extractAts sort bs =
  let ats = csetMake () in
  let procNom nom f =
    match lfGetType sort f with
    | AndF
    | OrF -> ()
    | ConstF
    | AtF -> ()
    | _ -> csetAdd ats (lfGetAt (sort, nom) f)
  in
  let getAts f =
    match lfGetType sort f with
    | AtF -> csetAdd ats (lfToAt sort f)
    | NomF -> bsetIter (fun f1 -> procNom f f1) bs
    | _ -> ()
  in
  bsetIter getAts bs;
  ats

let detConstraintsState state =
  let procRule accR (_, chldrn) =
    let procChldrn accC core =
      if coreGetStatus core = Unsat then accC
      else cssUnion accC (coreGetConstraints core)
    in
    let orset = List.fold_left procChldrn cssEmpty chldrn in
    let procOrset csetO accO =
      let mkUnion csetU accU =
        let cset = csetUnion csetO csetU in
        cssAdd cset accU
      in
      cssFold mkUnion accR accO
    in
    cssFold procOrset orset cssEmpty
  in
  let sort = stateGetSort state in
  let bs = stateGetBs state in
  let ats = extractAts sort bs in
  if stateGetStatus state = Expandable then csetAddDot ats else ();
  List.fold_left procRule (cssSingleton ats) (stateGetRules state)

let detConstraintsCore core =
  let sort = coreGetSort core in
  let bs = coreGetBs core in
  let ats = extractAts sort bs in
  let addCnstr acc state =
    if stateGetStatus state = Unsat then acc
    else
      let csets = stateGetConstraints state in
      (* cssFold (fun c a -> cssAdd (csetUnion c ats) a) csets acc *)
      cssUnion csets acc
  in
  let initInner =
    if coreGetStatus core = Expandable then
      (* let cset = csetCopy ats in *)
      let cset = ats in
      csetAddDot cset;
      cssSingleton cset
    else cssEmpty
  in
  List.fold_left addCnstr initInner (coreGetChildren core)

let rec propNom = function
  | [] -> ()
  | node::tl ->
      let tl1 =
        match node with
        | State state ->
            if stateGetStatus state = Unsat then tl
            else
              let csets = detConstraintsState state in
              let oldcsets = stateGetConstraints state in
              if cssEqual csets oldcsets then tl
              else begin
                stateSetConstraints state csets;
                List.fold_left (fun acc c -> (Core c)::acc) tl (stateGetParents state)
              end
        | Core core ->
            if coreGetStatus core = Unsat then tl
            else
              let csets = detConstraintsCore core in
              let oldcsets = coreGetConstraints core in
              if cssEqual csets oldcsets then tl
              else begin
                coreSetConstraints core csets;
                List.fold_left (fun acc (s, _) -> (State s)::acc) tl (coreGetParents core)
              end
      in
      propNom tl1

let updateConstraints node elemU csets =
  let update cset acc =
    let cnstr =
      match graphFindCnstr cset with
      | Some cn -> cn
      | None ->
          let cn = UnexpandedC [] in
          graphInsertCnstr cset cn;
          queueInsertCnstr cset;
          cn
    in
    match cnstr with
    | UnsatC -> acc
    | UnexpandedC parents ->
        graphReplaceCnstr cset (UnexpandedC (node::parents));
        false
    | OpenC parents ->
        graphReplaceCnstr cset (OpenC (node::parents));
        false
    | SatC -> false
  in
  let isUnsat = cssFold update csets true in
  if isUnsat then propagateUnsat [elemU] else ()

let propagateNominals () =
  let init = cssSingleton (csetMake ()) in
  graphIterStates (fun s -> if stateGetStatus s = Unsat then () else stateSetConstraints s init);
  graphIterCores (fun c -> if coreGetStatus c = Unsat then () else coreSetConstraints c init);
  graphIterStates (fun s -> propNom [State s]);
  graphIterCores (fun c -> propNom [Core c]);
  graphClearCnstr ();
  let fktS state =
    if stateGetStatus state = Unsat then ()
    else updateConstraints (State state) (UState (state, None)) (stateGetConstraints state)
  in
  graphIterStates fktS;
  let fktC core =
    if coreGetStatus core = Unsat then ()
    else updateConstraints (Core core) (UCore (core, true)) (coreGetConstraints core)
  in
  graphIterCores fktC


(*****************************************************************************)
(*                           Node Expansions                                 *)
(*****************************************************************************)

408
409
(* get the literal representing the formula f, possibly expanding the formula hash table fht *)
let getLit sort (fht: fht) (solver:M.solver) (f: localFormula) : M.lit =
Thorsten Wißmann's avatar
Thorsten Wißmann committed
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
  match fhtFind fht f with
  | Some lit -> lit
  | None ->
      let var = M.new_variable solver in
      let lit = M.var_to_lit var true in
      fhtAdd fht f lit;
      let () =
        match lfGetNeg sort f with
        | None -> ()
        | Some nf ->
            let nlit = M.neg_lit lit in
            fhtAdd fht nf nlit;
      in
      lit

425
426
(* creates a new core of specified sort, representing the set of formulas bs (conjunctively) *)
let newCore sort (bs: bset) : core =
Thorsten Wißmann's avatar
Thorsten Wißmann committed
427
428
  let fht = fhtInit () in
  let solver = M.new_solver () in
429
430
431
432
433
434
435
436
  let rec addClauses f : unit =
    (* "internalize" the formula f, by
        a. creating a literal for f
        b. "internalizing" the components of f
        c. encode the relationship between f and
           its components
    *)
    let lf = getLit sort fht solver f in (* a. *)
Thorsten Wißmann's avatar
Thorsten Wißmann committed
437
438
439
440
441
442
443
444
    match lfGetType sort f with
    | OrF ->
        let f1 = lfGetDest1 sort f in
        let f2 = lfGetDest2 sort f in
        addClauses f1;
        addClauses f2;
        let lf1 = fhtMustFind fht f1 in
        let lf2 = fhtMustFind fht f2 in
445
446
447
448
449
        (* c.:
            if "f = f1 v f2", then it is
                f -> f1 v f2, i.e.
                ¬f v f1 v f2
        *)
Thorsten Wißmann's avatar
Thorsten Wißmann committed
450
451
452
453
454
455
456
457
458
        let okay = M.add_clause solver [M.neg_lit lf; lf1; lf2] in
        assert (okay)
    | AndF ->
        let f1 = lfGetDest1 sort f in
        let f2 = lfGetDest2 sort f in
        addClauses f1;
        addClauses f2;
        let lf1 = fhtMustFind fht f1 in
        let lf2 = fhtMustFind fht f2 in
459
460
461
462
463
        (* c.:
            if "f = f1 ∧ f2" then it is
                f -> f1 as well as f -> f2, i.e.
                ¬f v f1 and ¬f v f2.
        *)
Thorsten Wißmann's avatar
Thorsten Wißmann committed
464
465
466
467
468
469
470
471
472
        let okay1 = M.add_clause solver [M.neg_lit lf; lf1] in
        assert (okay1);
        let okay2 = M.add_clause solver [M.neg_lit lf; lf2] in
        assert (okay2)
    | _ -> ()
  in
  bsetIter addClauses bs;
  coreMake sort bs solver fht

473
474
475
(* get another set of formulas whose satisfiability would prove the
   satisfiability of the core *)
let getNextState (core:core) : (sort*bset) option =
Thorsten Wißmann's avatar
Thorsten Wißmann committed
476
477
478
479
480
481
482
483
484
485
486
  let bs = coreGetBs core in
  let fht = coreGetFht core in
  let litset = bsetFold (fun f acc -> (fhtMustFind fht f)::acc) bs [] in
  let solver = coreGetSolver core in
  let isSat = M.invoke_solver solver litset in
  if not isSat then None
  else
    let sort = coreGetSort core in
    let newbs = bsetMake () in
    let rec mkExclClause f acc =
      match lfGetType sort f with
487
      | OrF -> (* OrF f1 f2 := f *)
Thorsten Wißmann's avatar
Thorsten Wißmann committed
488
          let f1 = lfGetDest1 sort f in
489
          let lf1 = fhtMustFind fht f1 in (* the corresponding literal *)
Thorsten Wißmann's avatar
Thorsten Wißmann committed
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
          if M.literal_status solver lf1 = M.LTRUE then mkExclClause f1 acc
          else
            let f2 = lfGetDest2 sort f in
            let lf2 = fhtMustFind fht f2 in
            assert (M.literal_status solver lf2 = M.LTRUE);
            mkExclClause f2 acc
      | AndF ->
          let f1 = lfGetDest1 sort f in
          let lf1 = fhtMustFind fht f1 in
          assert (M.literal_status solver lf1 = M.LTRUE);
          let acc1 = mkExclClause f1 acc in
          let f2 = lfGetDest2 sort f in
          let lf2 = fhtMustFind fht f2 in
          assert (M.literal_status solver lf2 = M.LTRUE);
          mkExclClause f2 acc1
      | _ ->
          bsetAdd newbs f;
          (M.neg_lit (fhtMustFind fht f))::acc
    in
    let clause = bsetFold mkExclClause bs [] in
    let okay = M.add_clause solver clause in
    assert (okay);
    Some (sort, newbs)

let newState sort bs =
  let (func, sl) = !sortTable.(sort) in
  let producer = CoAlgLogics.getExpandingFunctionProducer func in
  let exp = producer sort bs sl in
  stateMake sort bs exp

520
521
(* creates -- if needed -- a new state representing formulas in the bitset bs *)
(* parent specifies the parent "core" for the new state *)
Thorsten Wißmann's avatar
Thorsten Wißmann committed
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
let insertState parent sort bs =
  let child =
    match graphFindState sort bs with
    | None ->
        let s = newState sort bs in
        graphInsertState sort bs s;
        queueInsertState s;
        s
    | Some s -> s
  in
  coreAddChild parent child;
  stateAddParent child parent

let expandCore core =
  match getNextState core with
  | Some (sort, bs) ->
      insertState core sort bs;
      queueInsertCore core
  | None ->
      let isUnsat = List.for_all (fun s -> stateGetStatus s = Unsat) (coreGetChildren core) in
      if isUnsat then propagateUnsat [UCore (core, false)]
      else coreSetStatus core Open


546
(* creates a new core -- if needed -- *)
Thorsten Wißmann's avatar
Thorsten Wißmann committed
547
548
549
550
551
552
553
554
555
let insertCore sort bs =
  match graphFindCore sort bs with
  | None ->
      let c = newCore sort bs in
      graphInsertCore sort bs c;
      queueInsertCore c;
      c
  | Some c -> c

Thorsten Wißmann's avatar
Thorsten Wißmann committed
556
557
558
559
560
561
562
563
    (* tells whether inserting the rule makes it satisfiable:
        Some true -> surely satisfiable
        Some false -> surely unsat
        None -> Not known yet
    *)
let insertRule state dep (chldrn: (sort * bset) list junction) : bool option =
  let (chldrn, junc) = junctionExtract chldrn in
  let insert (sort, bs) = (* checks if the given child node has some sat-state *)
Thorsten Wißmann's avatar
Thorsten Wißmann committed
564
    let bs1 = bsetAddTBox sort bs in
Thorsten Wißmann's avatar
Thorsten Wißmann committed
565
    insertCore sort bs1
Thorsten Wißmann's avatar
Thorsten Wißmann committed
566
  in
Thorsten Wißmann's avatar
Thorsten Wißmann committed
567
568
569
570
571
572
573
574
575
576
577
578
579
  let children : core list = List.map insert chldrn in
  (* get the sat-states of all the children, throw it back into a junction
     (i.e. disjunction or conjunction) and then evaluate it *)
  let satStateChildren =
    let child2satState core = match (coreGetStatus core) with
                              | Unsat       -> Some false
                              | Sat         -> Some true
                              | Expandable  -> None (* unknown state *)
                              | Open        -> None (* unknown state *)
    in
    junctionEvalBoolOption (junc (List.map child2satState children))
  in
  (* find out satisfiability *)
Thorsten Wißmann's avatar
Thorsten Wißmann committed
580
581
  let idx = stateAddRule state dep (List.rev children) in
  List.iter (fun c -> coreAddParent c state idx) children;
582
583
584
585
586
587
588
589
  match satStateChildren with
  | Some false -> (* definitely unsat *)
        propagateUnsat [UState (state, Some idx)];
        Some false
  | Some true ->
        (* TODO: propagateSat ? *)
        Some true
  | None -> None
Thorsten Wißmann's avatar
Thorsten Wißmann committed
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608

    (* tells whether it is satisfiable:
        Some true -> surely satisfiable
        Some false -> surely unsat
        None -> Not known yet
    *)
let rec insertAllRules state : rule list junction -> bool option = function
  | Conjunctive [] -> Some true
  | Disjunctive [] -> Some false
  | Conjunctive ((dep, chldrn)::tl) ->
      let satState = insertRule state dep (Conjunctive chldrn) in
      if satState <> Some false (* if notUnsat*)
        then insertAllRules state (Conjunctive tl)
        else Some false
  | Disjunctive ((dep, chldrn)::tl) ->
      let satState = insertRule state dep (Disjunctive chldrn) in
      if satState <> Some true (* if not Sat *)
        then insertAllRules state (Disjunctive tl)
        else Some true
Thorsten Wißmann's avatar
Thorsten Wißmann committed
609
610

let expandState state =
Thorsten Wißmann's avatar
Thorsten Wißmann committed
611
  let (rules, _) = junctionExtract (stateNextRule state) in
612
  match rules with
Thorsten Wißmann's avatar
Thorsten Wißmann committed
613
  | AllInOne rules ->
Thorsten Wißmann's avatar
Thorsten Wißmann committed
614
615
616
      let (_, junction) = junctionExtract (stateNextRule state) in
      let satState = insertAllRules state (junction rules) in
      if satState <> Some false (* i.e. if not Unsat *) then stateSetStatus state Open
Thorsten Wißmann's avatar
Thorsten Wißmann committed
617
  | NextRule (dep, chldrn) ->
Thorsten Wißmann's avatar
Thorsten Wißmann committed
618
619
620
      let (_, junction) = junctionExtract (stateNextRule state) in
      let satState = insertRule state dep (junction chldrn) in
      if satState <> Some false (* if notUnsat *) then queueInsertState state else ()
Thorsten Wißmann's avatar
Thorsten Wißmann committed
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
  | NoMoreRules -> stateSetStatus state Open


let expandCnstr cset =
  let nht = nhtInit () in
  let mkCores f =
    let (sort, lf) as nom = atFormulaGetNominal f in
    let nomset =
      match nhtFind nht nom with
      | Some ns -> ns
      | None ->
          let cset1 = csetCopy cset in
          csetRemDot cset1;
          let bs = csetAddTBox sort cset1 in
          bsetAdd bs lf;
          nhtAdd nht nom bs;
          bs
    in
    bsetAdd nomset (atFormulaGetSubformula f)
  in
  csetIter cset mkCores;
  let inCores (sort, _) bs (isUns, acc) =
    let core = insertCore sort bs in
    coreAddConstraintParent core cset;
    (coreGetStatus core = Unsat || isUns, core::acc)
  in
  let (isUnsat, children) = nhtFold inCores nht (false, []) in
  if isUnsat then propagateUnsat [UCnstr cset]
  else
    match graphFindCnstr cset with
    | Some (UnexpandedC parents) -> graphReplaceCnstr cset (OpenC parents)
    | _ -> assert false


(*****************************************************************************)
(*                           The Main Loop                                   *)
(*****************************************************************************)

let rec expandNodesLoop () =
  match queueGetElement () with
  | QState state ->
      if stateGetStatus state = Expandable then begin
        expandState state;
        if doNominalPropagation () then begin
          propagateNominals ();
          if doSatPropagation () then propagateSat ()
        end else ()
      end else ();
      expandNodesLoop ()
  | QCore core ->
      if coreGetStatus core = Expandable then begin
        expandCore core;
        if doNominalPropagation () then begin
          propagateNominals ();
          if doSatPropagation () then propagateSat ()
        end else ()
      end else ();
      expandNodesLoop ()
  | QCnstr cset ->
      expandCnstr cset;
      expandNodesLoop ()
  | QEmpty -> ()

let rec buildGraphLoop () =
  expandNodesLoop ();
  propagateNominals ();
  if queueIsEmpty () then () else buildGraphLoop ()
    
(** A graph-tableau-based decision procedure framework for coalgebraic logics.
    @param verbose An optional switch which determines
    whether the procedure shall print some information on the standard output.
    The default is false.
    @param sorts An array mapping each sort (represented as an integer)
    to a functor and a list of sorts which are the arguments of the functor.
    @param nomTable A partial function mapping nominals (represented as strings)
    to their sorts.
    @param tbox A list of sorted formulae.
    @param sf A sorted formula.
    @return True if sf is satisfiable wrt tbox, false otherwise.
 *)
let isSat ?(verbose = false) sorts nomTable tbox sf =
  let start = if verbose then Unix.gettimeofday () else 0. in
  sortTable := Array.copy sorts;
  let (tbox1, sf1, bs) = ppFormulae nomTable tbox sf in
  let sort = fst sf in
  let bs1 = bsetAddTBox sort bs in
  graphInit ();
  queueInit ();
  let root = insertCore sort bs1 in
  graphAddRoot root;
  let sat =
    try
      buildGraphLoop ();
      match coreGetStatus (graphGetRoot ()) with
      | Expandable -> assert false
      | Unsat -> false
      | Sat
      | Open -> true
    with CoAlg_finished res -> res
  in
  if verbose then
    let stop = Unix.gettimeofday () in
    let addup lst = List.fold_left (fun acc sf -> acc + (CoAlgFormula.sizeSortedFormula sf)) 0 lst in
    print_newline ();
    print_endline ("Query: " ^ (CoAlgFormula.exportQuery tbox sf));
    let size = (CoAlgFormula.sizeSortedFormula sf) + (addup tbox) in
    print_endline ("Added Size: " ^ (string_of_int size));
    print_endline ("Negation Normal Form: " ^ (CoAlgFormula.exportQuery tbox1 sf1));
    let nsize = (CoAlgFormula.sizeSortedFormula sf1) + (addup tbox1) in
    print_endline ("Added Size: " ^ (string_of_int nsize));
    print_endline ("Result: Query is " ^ (if not sat then "not " else "") ^ "satisfiable.");
    print_endline ("Time: " ^ (string_of_float (stop -. start)));
    print_endline ("Generated states: " ^ (string_of_int (graphSizeState ())));
    print_endline ("Generated cores: " ^ (string_of_int (graphSizeCore ())));
    print_endline ("Generated constraints: " ^ (string_of_int (graphSizeCnstr ())));
    print_newline ()
  else ();
  sat