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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
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
//! Provides a framework and interfaces to various components that are used for implementing the
//! Chase. It also implements entrypoints for running the Chase.
//!
//! ## Background
//! Razor implements a variant of [the Chase] algorithm to construct models for first-order theories with
//! equality. The Chase operates on *[geometric theories]*, theories that contain a syntactic
//! variation of first-order formulae which we refer to as the Geometric Normal Form (GNF). Formulae
//! in GNF have the following shape:
//!
//! A<sub>1</sub> ∧ ... ∧ A<sub>m</sub> →
//! (∃ x<sub>11</sub>, ..., x<sub>1j<sub>1</sub></sub> . A<sub>11</sub> ∧ ... ∧ A<sub>1n<sub>1</sub></sub>) </br>
//! > >
//! ∨ (∃ x<sub>21</sub>, ..., x<sub>2j<sub>2</sub></sub> . A<sub>21</sub> ∧ ... ∧ A<sub>2n<sub>2</sub></sub>) </br>
//! > >
//! ∨ ... </br>
//! > >
//! ∨ (∃ x<sub>i1</sub>, ..., x<sub>ij<sub>i</sub></sub> . A<sub>i1</sub> ∧ ... ∧ A<sub>in<sub>i</sub></sub>)
//!
//! where A<sub>k</sub>s are (positive) atomic formulae (possibly including equality) and free
//! variables are assumed to be universally qualified over the entire formula.
//!
//! In the context of a [run of the Chase], we refer to formulae in the their GNF as
//! [*sequents*][sequent]. The premise (left side) and the consequence (right side) of the
//! implication are respectively said to be the *body* and the *head* of the sequent.
//!
//! ### Satisfiability of Geometric Theories
//! It turns out that every first-order theory can be transformed to a geometric theory that is
//! *equisatisfiable* to the original theory via [standard syntactic manipulation].
//! In fact, for every model *N* of the original theory, there exists a model *M* of the geometric
//! theory such that there is a homomorphism from *M* to *N*. This is an important result that
//! enables Razor to utilize the Chase to construct homomorphically minimal models of a given
//! first-order theory.
//!
//! In the context of a model-finding application, the models that the Chase produces are desirable
//! since they contain minimum amount of information, thus they induce minimal distraction.
//! As a direct consequence of semi-decidability of satisfiability in first-order logic
//! (see [Gödel's incompleteness theorems][godel]), satisfiability of geometric theories is
//! semi-decidable as well.
//!
//! **Note**: A comprehensive discussion on the properties of the models that are constructed by
//! the Chase is out of the scope of this document.
//!
//! [the Chase]: https://en.wikipedia.org/wiki/Chase_(algorithm)
//! [geometric theories]: https://www.cs.bham.ac.uk/~sjv/GLiCS.pdf
//! [run of the Chase]: ./fn.solve_all.html
//! [sequent]: ./trait.SequentTrait.html
//! [standard syntactic manipulation]: ../formula/syntax/enum.Formula.html#method.gnf
//! [godel]: https://en.wikipedia.org/wiki/Gödel%27s_incompleteness_theorems
//!
//! ## The Chase
//! Given a geometric theory and starting with an empty model, a run of Chase consists of repeated
//! applications of [chase-step]s by which the model is augmented with *necessary* pieces of
//! information until there is a contradiction or the model satisfies the theory. Within
//! Razor's implementation, instances of any type that implements [ModelTrait] can serve as a
//! first-order model. Also, inspired by [Steven Vickers][vickers], we refer to the units of
//! information that augment models as [observation]s.
//!
//! [chase-step]: index.html#chase-step
//! [vickers]: https://www.cs.bham.ac.uk/~sjv/GeoZ.pdf
//!
//! ### Chase Step
//! Given a geometric theory and an existing model, a chase-step proceeds as follows:
//!
//! 1. A sequent from the theory is selected to be evaluated against the model. Razor uses an
//! instance of [StrategyTrait] to select the sequent.
//!
//! 2. The selected sequent is evaluated against the model: given an assignment from the free
//! variables of the sequent to the elements of the model, if the body of the sequent is true and
//! its head is not true in the model, new observations are added to the model to make the
//! sequent's head true. Instances of any type that implements [EvaluatorTrait] may be used to
//! evaluate the sequent in the model.
//!
//! 2.1. If the sequent is headless, meaning its consequence is falsehood (an empty disjunction),
//! the Chase fails on the given model.
//!
//! 2.2. If the head of the sequent contains more than one disjunct (with at least one
//! non-trivial disjunction), the Chase branches and satisfies each disjunct independently on clones
//! of the model. Razor uses instances of [SchedulerTrait] to schedule various branches of the Chase
//! for future Chase steps.
//!
//! 2.3. If no sequent can be found such that its body and head are respectively true and false
//! in the model, the model already satisfies the theory and will be returned as an output of the
//! Chase.
//!
//! [model]: ./trait.ModelTrait.html
//! [observation]: ./enum.Observation.html
//! [ModelTrait]: ./trait.ModelTrait.html
//! [StrategyTrait]: ./trait.StrategyTrait.html
//! [EvaluatorTrait]: ./trait.EvaluatorTrait.html
//! [SchedulerTrait]: ./trait.SchedulerTrait.html
//!
//! ### Termination
//! As a result of semi-decidability of geometric theories, it can be shown if a geometric theory
//! is unsatisfiable, a run of the Chase on the theory always terminates, although it may take
//! a very very long time.
//! However, when the theory is satisfiable, a run of the Chase may not terminate, producing
//! infinitely large models and/or infinitely many models that satisfy the theory. Nevertheless,
//! in practice, Razor can *bound* the size of models created by the Chase to guarantee termination.
//! Razor uses instances of types that implement [BounderTrait] to implement various strategies to
//! cap the size of search space for models.
//!
//! [BounderTrait]: ./trait.BounderTrait.html
//!
//! ## Implementation
//! The primary motivation for implementing Razor is to study the Chase and improve its performance
//! for practical applications. The flexible (but somehow complex) design of Razor allows for
//! experimenting with various data structures to represent [models] and [sequents], [evaluating]
//! sequents in models using a variety of algorithms, testing different ideas for [scheduling]
//! branches of the Chase and devising various [strategies] for selecting sequents. Also, because of
//! theoretical and non-theoretical consequences of non-termination of the Chase, Razor is going to
//! explore a variety of ideas for limiting the search space by [bounding] the size of models:
//!
//! Interesting combinations of these various options are constantly benchmarked and the
//! configuration with the best average performance is used by the Rusty Razor application.
//!
//! [models]: ./trait.ModelTrait.html
//! [sequents]: ./trait.SequentTrait.html
//! [evaluating]: ./trait.EvaluatorTrait.html
//! [scheduling]: ./trait.SchedulerTrait.html
//! [strategies]: ./trait.StrategyTrait.html
//! [bounding]: ./trait.BounderTrait.html
//!
use *;
use fmt;
use tracing;
use Either;
/// Is a symbol to represent elements of first-order models. An element is identified by an index.
;
/// Is the trait for special kind of variable-free terms that are used to witness existence of
/// model elements. These terms are used as provenance information for models to describe *why*
/// elements exist or facts are true in models.
/// Relations are semantic counterparts of predicates and are used to store [`Fact`]s in models.
///
/// **Note**: `Rel` is the counterpart of [`Pred`] at a semantic level.
///
/// [`Fact`]: ./enum.Observation.html#variant.Fact
/// [`Pred`]: ../formula/syntax/struct.Pred.html
;
/// Represents positive units of information by which [`Model`]s are constructed. Once a `Model` is
/// augmented by an observation, the observation remains true in the model.
///
/// [`Model`]: ./trait.ModelTrait.html
/// Is the trait for various implementations of first-order models that are constructed by the
/// Chase.
/// Is the trait for types that represents a [geometric sequent][sequent] in the
/// context of an implementation of the Chase.
///
/// [sequent]: ./index.html#background
/// Strategy is the trait of algorithms for choosing sequents in the context of an implementation
/// of the Chase. Strategy instances provide the next sequent to be evaluated by the Chase.
///
/// **Note**: See [here] for more information about how strategy instances are used.
///
/// [here]: ./index.html#implementation
/// Bounder is the trait of algorithms for [bounding] the size of models generated by the Chase.
///
/// [bounding]: ./index.html#termination
/// Is the trait of algorithms that evaluate an implementation of [SequentTrait] in an
/// implementation of [ModelTrait] within a [chase-step].
///
/// [SequentTrait]: ./trait.SequentTrait.html
/// [ModelTrait]: ./trait.ModelTrait.html
/// [chase-step]: ./index.html#chase-step
/// Is result of [evaluating] a model in a [chase-step].
///
/// [evaluating]: ./trait.EvaluatorTrait.html
/// [chase-step]: ./index.html#chase-step
/// Is the trait of algorithms for scheduling various branches of the Chase. A branch of the Chase
/// is represented with a [model] together with a [strategy] for scheduling sequents to be
/// evaluated in the model.
///
/// **Note**: See [here] for more information about scheduling branches of the Chase.
///
/// [model]: ./trait.ModelTrait.html
/// [strategy]: ./trait.StrategyTrait.html
/// [here]: ./index.html#implementation
/// Given a [`scheduler`], an [`evaluator`] and possibly a [`bounder`], runs an implementation
/// independent run of [the Chase] and returns *all* resulting models. The return value is empty if
/// the theory is not satisfiable.
///
/// [`scheduler`]: ./trait.SchedulerTrait.html
/// [`evaluator`]: ./trait.EvaluatorTrait.html
/// [`bounder`]: ./trait.BounderTrait.html
/// [the Chase]: ./index.html#the-chase
///
/// ```rust
/// use razor_fol::syntax::Theory;
/// use razor_chase::chase::{
/// SchedulerTrait, StrategyTrait, chase_all,
/// r#impl::basic,
/// strategy::Linear,
/// scheduler::FIFO,
/// bounder::DomainSize,
/// };
///
/// // parse the theory:
/// let theory: Theory = r#"
/// exists x . P(x);
/// P(x) implies Q(x) | R(x);
/// R(x) -> exists y . Q(x, y);
/// "#.parse().unwrap();
///
/// let geometric_theory = theory.gnf(); // convert the theory to geometric
///
/// // create sequents for the geometric theory:
/// let sequents: Vec<basic::Sequent> = geometric_theory
/// .formulae
/// .iter()
/// .map(|f| f.into())
/// .collect();
///
/// let evaluator = basic::Evaluator {};
/// let strategy = Linear::new(sequents.iter().collect()); // use the `Linear` strategy
/// let mut scheduler = FIFO::new(); // schedule branches in first-in-first-out manner
///
/// // run unbounded model-finding (note that the Chase terminates on the given theory):
/// let bounder: Option<&DomainSize> = None;
/// scheduler.add(basic::Model::new(), strategy); // schedule the initial (empty) model
/// let models = chase_all(&mut scheduler, &evaluator, bounder);
///
/// assert_eq!(2, models.len()); // two models are found
/// ```
/// Given a [`scheduler`], an [`evaluator`], possibly a [`bounder`] and a `consumer` closure, runs
/// an implementation independent [chase-step]. Satisfying models of the theory that are produced
/// by the `chase-step` will be consumed by the `consumer`. In contrast, `incomplete_consumer`
/// (if provided) consumes incomplete non-models of the theory that are rejected by the bounder.
///
/// [`scheduler`]: ./trait.SchedulerTrait.html
/// [`evaluator`]: ./trait.EvaluatorTrait.html
/// [`bounder`]: ./trait.BounderTrait.html
/// [chase-step]: ./index.html#chase-step
///
/// ```rust
/// use razor_fol::syntax::Theory;
/// use razor_chase::chase::{
/// SchedulerTrait, StrategyTrait, chase_step,
/// r#impl::basic,
/// strategy::Linear,
/// scheduler::FIFO,
/// bounder::DomainSize,
/// };
///
/// // parse the theory:
/// let theory: Theory = r#"
/// exists x . P(x);
/// P(x) implies Q(x) | R(x);
/// R(x) -> exists y . Q(x, y);
/// "#.parse().unwrap();
///
/// let geometric_theory = theory.gnf(); // convert the theory to geometric
///
/// // create sequents for the geometric theory:
/// let sequents: Vec<basic::Sequent> = geometric_theory
/// .formulae
/// .iter()
/// .map(|f| f.into())
/// .collect();
///
/// let evaluator = basic::Evaluator {};
/// let strategy = Linear::new(sequents.iter().collect()); // use the `Linear` strategy
/// let mut scheduler = FIFO::new(); // schedule branches in first-in-first-out manner
///
/// // run unbounded model-finding (note that the Chase terminates on the given theory):
/// let bounder: Option<&DomainSize> = None;
/// scheduler.add(basic::Model::new(), strategy); // schedule the initial (empty) model
///
/// let mut counter = 0; // a counter to count the number of models
/// while !scheduler.empty() {
/// let models = chase_step(
/// &mut scheduler,
/// &evaluator,
/// bounder,
/// |m| {counter += 1}, // the closure counts the models found
/// |_|{}, // ignore incomplete non-models
/// );
/// }
///
/// assert_eq!(2, counter); // two models are found
/// ```
//// Tests -------------------------------------