rify 0.7.1

RDF reasoner that operates on RIF-like conjunctive rules. Outputs a machine readable proof of some claim which can be cheaply verified.
Documentation
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
use crate::common::forward;
use crate::common::vertices;
use crate::common::LowRuleApplication;
use crate::reasoner::{Quad, Reasoner};
use crate::rule::{Entity, LowRule, Rule};
use crate::translator::Translator;
use alloc::collections::{BTreeMap, BTreeSet};
use core::fmt::{Debug, Display};

/// Locate a proof of some composite claims given the provided premises and rules.
///
/// ```
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use rify::{
///     prove,
///     Entity::{Unbound, Bound},
///     Rule, RuleApplication,
/// };
///
/// // (?a, is, awesome) ∧ (?a, score, ?s) -> (?a score, awesome)
/// let awesome_score_axiom = Rule::create(
///     vec![
///         // if someone is awesome
///         [Unbound("a"), Bound("is"), Bound("awesome"), Bound("default_graph")],
///         // and they have some score
///         [Unbound("a"), Bound("score"), Unbound("s"), Bound("default_graph")],
///     ],
///     vec![
///         // then they must have an awesome score
///         [Unbound("a"), Bound("score"), Bound("awesome"), Bound("default_graph")]
///     ],
/// )?;
///
/// assert_eq!(
///     prove::<&str, &str>(
///         // list of already known facts (premises)
///         &[
///             ["you", "score", "unspecified", "default_graph"],
///             ["you", "is", "awesome", "default_graph"]
///         ],
///         // the thing we want to prove
///         &[["you", "score", "awesome", "default_graph"]],
///         // ordered list of logical rules
///         &[awesome_score_axiom]
///     )?,
///     &[
///         // the desired statement is be proven by instatiating the `awesome_score_axiom`
///         // (you is awesome) ∧ (you score unspecified) -> (you score awesome)
///         RuleApplication {
///             rule_index: 0,
///             instantiations: vec!["you", "unspecified"]
///         }
///     ]
/// );
/// # Ok(())
/// # }
/// ```
pub fn prove<'a, Unbound: Ord + Clone, Bound: Ord + Clone>(
    premises: &'a [[Bound; 4]],
    to_prove: &'a [[Bound; 4]],
    rules: &'a [Rule<Unbound, Bound>],
) -> Result<Vec<RuleApplication<Bound>>, CantProve> {
    let tran: Translator<Bound> = vertices(premises, rules).cloned().collect();
    let lpremises: Vec<Quad> = premises
        .iter()
        .map(|spog| forward(&tran, spog).unwrap())
        .collect();
    let lto_prove: Vec<Quad> = to_prove
        .iter()
        .map(|spog| forward(&tran, spog))
        .collect::<Option<_>>()
        .ok_or(CantProve::NovelName)?;
    let lrules: Vec<LowRule> = rules
        .iter()
        .cloned()
        .map(|rule: Rule<Unbound, Bound>| -> LowRule { rule.lower(&tran).map_err(|_| ()).unwrap() })
        .collect();

    let lproof = low_prove(&lpremises, &lto_prove, &lrules)?;

    // convert to proof
    Ok(lproof
        .iter()
        .map(|rra: &LowRuleApplication| -> RuleApplication<Bound> {
            rra.raise(&rules[rra.rule_index], &tran)
        })
        .collect())
}

fn low_prove(
    premises: &[Quad],
    to_prove: &[Quad],
    rules: &[LowRule],
) -> Result<Vec<LowRuleApplication>, CantProve> {
    let mut rs = Reasoner::default();
    // statement (Quad) is proved by applying rule (LowRuleApplication)
    let mut arguments: BTreeMap<Quad, LowRuleApplication> = BTreeMap::new();
    let mut to_add: BTreeSet<Quad> = premises.iter().cloned().collect();

    for (rule_index, rule) in rules.iter().enumerate() {
        if rule.if_all.is_empty() {
            for implied in &rule.then {
                let new_quad = implied.clone().local_to_global(&rule.inst).unwrap();
                if to_add.insert(new_quad.clone()) {
                    arguments.insert(
                        new_quad,
                        LowRuleApplication {
                            rule_index,
                            instantiations: Default::default(),
                        },
                    );
                }
            }
        }
    }
    let mut rules2: Vec<(usize, LowRule)> = rules
        .iter()
        .cloned()
        .enumerate()
        .filter(|(_index, rule)| !rule.if_all.is_empty())
        .collect();

    // reason
    while !to_add.is_empty() && !to_prove.iter().all(|tp| rs.contains(tp)) {
        let mut adding_now = BTreeSet::<Quad>::new();
        core::mem::swap(&mut adding_now, &mut to_add);
        for fact in &adding_now {
            rs.insert(fact.clone());
            for (
                rule_index,
                LowRule {
                    ref mut if_all,
                    then,
                    ref mut inst,
                },
            ) in rules2.iter_mut()
            {
                rs.apply_related(fact.clone(), if_all, inst, &mut |inst| {
                    for implied in then.iter().cloned() {
                        let new_quad = implied.local_to_global(inst).unwrap();
                        if !rs.contains(&new_quad) && !adding_now.contains(&new_quad) {
                            arguments.entry(new_quad.clone()).or_insert_with(|| {
                                LowRuleApplication {
                                    rule_index: *rule_index,
                                    instantiations: inst.inner().clone(),
                                }
                            });
                            to_add.insert(new_quad);
                        }
                    }
                });
            }
        }
    }

    if !to_prove.iter().all(|tp| rs.contains(tp)) {
        return Err(CantProve::ExhaustedSearchSpace);
    }

    let mut ret: Vec<LowRuleApplication> = Vec::new();
    for claim in to_prove {
        recall_proof(claim, &mut arguments, rules, &mut ret);
    }
    Ok(ret)
}

// this function is not tail recursive
/// As this function populates the output. It removes corresponding arguments from the input.
/// The reason being that a single argument does not need to be proved twice. Once is is
/// proved, it can be treated as a premise.
fn recall_proof(
    // the globally scoped quad for which to find arguments
    to_prove: &Quad,
    arguments: &mut BTreeMap<Quad, LowRuleApplication>,
    rules: &[LowRule],
    outp: &mut Vec<LowRuleApplication>,
) {
    let to_global_scope = |rra: &LowRuleApplication, locally_scoped: usize| -> usize {
        let concrete = rules[rra.rule_index].inst.get(locally_scoped);
        let found = rra
            .instantiations
            .get(locally_scoped)
            .and_then(|o| o.as_ref());
        if let (Some(c), Some(f)) = (concrete, found) {
            debug_assert_eq!(c, f);
        }
        *concrete.or(found).unwrap()
    };

    if let Some(application) = arguments.remove(to_prove) {
        // for every required sub-statement, recurse
        for quad in &rules[application.rule_index].if_all {
            let Quad { s, p, o, g } = quad;
            recall_proof(
                &[
                    to_global_scope(&application, s.0),
                    to_global_scope(&application, p.0),
                    to_global_scope(&application, o.0),
                    to_global_scope(&application, g.0),
                ]
                .into(),
                arguments,
                rules,
                outp,
            );
        }
        // push the application onto output and return
        outp.push(application);
    }
}

#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum CantProve {
    /// Entire search space was exhausted. The requested proof does not exists.
    ExhaustedSearchSpace,
    /// One of the entities in to_prove was never mentioned in the provided premises or
    /// rules. The requested proof does not exists.
    NovelName,
}

impl Display for CantProve {
    fn fmt(&self, fmtr: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
        Debug::fmt(self, fmtr)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for CantProve {}

#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
/// An element of a deductive proof. Proofs can be transmitted and later validatated as long as the
/// validator assumes the same rule list as the prover.
///
/// Unbound variables are bound to the values in `instantiations`. They are bound in order of
/// initial appearance.
///
/// Given the rule:
///
/// ```customlang
/// ifall
///   [?z ex:foo ?x DG]
/// then
///   [?x ex:bar ?z DG]
/// ```
///
/// and the RuleApplication:
///
/// ```customlang
/// RuleApplication {
///   rule_index: 0,
///   instantiations: vec!["foer", "bary"],
/// }
/// ```
///
/// The rule application represents the deductive proof:
///
/// ```customlang
/// [foer ex:foo bary DG]
/// therefore
/// [bary ex:bar foer DG]
/// ```
pub struct RuleApplication<Bound> {
    /// The index of the rule in the implicitly associated rule list.
    pub rule_index: usize,
    /// Bindings for unbound names in the rule in order of appearance.
    pub instantiations: Vec<Bound>,
}

impl<Bound: Clone> RuleApplication<Bound> {
    pub(crate) fn assumptions_when_applied<'a, Unbound: Ord + Clone>(
        &'a self,
        rule: &'a Rule<Unbound, Bound>,
    ) -> Result<impl Iterator<Item = [Bound; 4]> + 'a, BadRuleApplication> {
        self.bind_claims(rule, rule.if_all())
    }

    pub(crate) fn implications_when_applied<'a, Unbound: Ord + Clone>(
        &'a self,
        rule: &'a Rule<Unbound, Bound>,
    ) -> Result<impl Iterator<Item = [Bound; 4]> + 'a, BadRuleApplication> {
        self.bind_claims(rule, rule.then())
    }

    /// claims must be either if_all or then from rule
    fn bind_claims<'a, Unbound: Ord + Clone>(
        &'a self,
        rule: &'a Rule<Unbound, Bound>,
        claims: &'a [[Entity<Unbound, Bound>; 4]],
    ) -> Result<impl Iterator<Item = [Bound; 4]> + 'a, BadRuleApplication> {
        let cannon: BTreeMap<&Unbound, usize> = rule
            .cononical_unbound()
            .enumerate()
            .map(|(ub, n)| (n, ub))
            .collect();
        if cannon.len() != self.instantiations.len() {
            return Err(BadRuleApplication);
        }
        Ok(claims
            .iter()
            .cloned()
            .map(move |claim| bind_claim(claim, &cannon, &self.instantiations)))
    }
}

/// Panics
///
/// panics if an unbound entity is not registered in map
/// panics if the canonical index of unbound (according to map) is too large to index instantiations
fn bind_claim<Unbound: Ord, Bound: Clone>(
    [s, p, o, g]: [Entity<Unbound, Bound>; 4],
    map: &BTreeMap<&Unbound, usize>,
    instantiations: &[Bound],
) -> [Bound; 4] {
    [
        bind_entity(s, map, instantiations),
        bind_entity(p, map, instantiations),
        bind_entity(o, map, instantiations),
        bind_entity(g, map, instantiations),
    ]
}

/// Panics
///
/// panics if an unbound entity is not registered in map
/// panics if the canonical index of unbound (according to map) is too large to index instantiations
fn bind_entity<Unbound: Ord, Bound: Clone>(
    e: Entity<Unbound, Bound>,
    map: &BTreeMap<&Unbound, usize>,
    instantiations: &[Bound],
) -> Bound {
    match e {
        Entity::Unbound(a) => instantiations[map[&a]].clone(),
        Entity::Bound(e) => e,
    }
}

/// The Rule being applied expects a different number of name bindings.
#[derive(Debug)]
pub struct BadRuleApplication;

#[cfg(test)]
mod test {
    use super::*;
    use crate::common::decl_rules;
    use crate::common::inc;
    use crate::rule::Entity::{Bound as B, Unbound as U};
    use crate::validate::validate;
    use crate::validate::Valid;

    #[test]
    fn novel_name() {
        assert_eq!(
            prove::<&str, &str>(&[], &[["andrew", "score", "awesome", "default_graph"]], &[])
                .unwrap_err(),
            CantProve::NovelName
        );
    }

    #[test]
    fn search_space_exhausted() {
        let err = prove::<&str, &str>(
            &[
                ["score", "score", "score", "default_graph"],
                ["andrew", "andrew", "andrew", "default_graph"],
                ["awesome", "awesome", "awesome", "default_graph"],
            ],
            &[["andrew", "score", "awesome", "default_graph"]],
            &[],
        )
        .unwrap_err();
        assert_eq!(err, CantProve::ExhaustedSearchSpace);
        let err = prove::<&str, &str>(
            &[
                ["score", "score", "score", "default_graph"],
                ["andrew", "andrew", "andrew", "default_graph"],
                ["awesome", "awesome", "awesome", "default_graph"],
                ["backflip", "backflip", "backflip", "default_graph"],
                ["ability", "ability", "ability", "default_graph"],
            ],
            &[["andrew", "score", "awesome", "default_graph"]],
            &[
                Rule::create(vec![], vec![]).unwrap(),
                Rule::create(
                    vec![[U("a"), B("ability"), B("backflip"), U("g")]],
                    vec![[U("a"), B("score"), B("awesome"), U("g")]],
                )
                .unwrap(),
            ],
        )
        .unwrap_err();
        assert_eq!(err, CantProve::ExhaustedSearchSpace);
    }

    #[test]
    fn prove_already_stated() {
        assert_eq!(
            prove::<&str, &str>(
                &[["doggo", "score", "11", "default_graph"]],
                &[["doggo", "score", "11", "default_graph"]],
                &[]
            )
            .unwrap(),
            Vec::new()
        );
    }

    #[test]
    /// generate a single step proof
    fn prove_single_step() {
        // (?boi, is, awesome) ∧ (?boi, score, ?s) -> (?boi score, awesome)
        let awesome_score_axiom = Rule::create(
            vec![
                [U("boi"), B("is"), B("awesome"), U("g")], // if someone is awesome
                [U("boi"), B("score"), U("s"), U("g")],    // and they have some score
            ],
            vec![[U("boi"), B("score"), B("awesome"), U("g")]], // then they must have an awesome score
        )
        .unwrap();
        assert_eq!(
            prove::<&str, &str>(
                &[
                    ["you", "score", "unspecified", "default_graph"],
                    ["you", "is", "awesome", "default_graph"]
                ],
                &[["you", "score", "awesome", "default_graph"]],
                &[awesome_score_axiom]
            )
            .unwrap(),
            vec![
                // (you is awesome) ∧ (you score unspecified) -> (you score awesome)
                RuleApplication {
                    rule_index: 0,
                    instantiations: vec!["you", "default_graph", "unspecified"]
                }
            ]
        );
    }

    #[test]
    /// rules with a single unbound graphname cannot be applied accross graphnames
    fn graph_separation() {
        let awesome_score_axiom = Rule::create(
            vec![
                [U("boi"), B("is"), B("awesome"), U("g")],
                [U("boi"), B("score"), U("s"), U("g")],
            ],
            vec![[U("boi"), B("score"), B("awesome"), U("g")]],
        )
        .unwrap();

        prove::<&str, &str>(
            &[
                ["you", "score", "unspecified", "default_graph"],
                ["you", "is", "awesome", "default_graph"],
            ],
            &[["you", "score", "awesome", "default_graph"]],
            &[awesome_score_axiom.clone()],
        )
        .unwrap();
        assert_eq!(
            prove(
                &[
                    ["you", "score", "unspecified", "default_graph"],
                    ["you", "is", "awesome", "other_graph"]
                ],
                &[["you", "score", "awesome", "default_graph"]],
                &[awesome_score_axiom.clone()]
            )
            .unwrap_err(),
            CantProve::ExhaustedSearchSpace
        );
        assert_eq!(
            prove(
                &[
                    ["you", "score", "unspecified", "default_graph"],
                    ["you", "is", "awesome", "other_graph"]
                ],
                &[["you", "score", "awesome", "other_graph"]],
                &[awesome_score_axiom.clone()]
            )
            .unwrap_err(),
            CantProve::ExhaustedSearchSpace
        );
        assert_eq!(
            prove(
                &[
                    ["you", "score", "unspecified", "default_graph"],
                    ["you", "is", "awesome", "default_graph"],
                    // to prevent NovelName error:
                    ["other_graph", "other_graph", "other_graph", "other_graph"],
                ],
                &[["you", "score", "awesome", "other_graph"]],
                &[awesome_score_axiom.clone()]
            )
            .unwrap_err(),
            CantProve::ExhaustedSearchSpace
        );
    }

    #[test]
    fn prove_multi_step() {
        // Rules:
        // (andrew claims ?c) ∧ (?c subject ?s) ∧ (?c property ?p) ∧ (?c object ?o) -> (?s ?p ?o)
        // (?person_a is awesome) ∧ (?person_a friendswith ?person_b) -> (?person_b is awesome)
        // (?person_a friendswith ?person_b) -> (?person_b friendswith ?person_a)

        // Facts:
        // (soyoung friendswith nick)
        // (nick friendswith elina)
        // (elina friendswith sam)
        // (sam friendswith fausto)
        // (fausto friendswith lovesh)
        // (andrew claims _:claim1)
        // (_:claim1 subject lovesh)
        // (_:claim1 property is)
        // (_:claim1 object awesome)

        // Composite Claims:
        // (soyoung is awesome)
        // (nick is awesome)

        // the following rules operate only on the defualt graph
        let rules: Vec<Rule<&str, &str>> = {
            let ru: &[[&[[Entity<&str, &str>; 4]]; 2]] = &[
                [
                    &[
                        [B("andrew"), B("claims"), U("c"), B("default_graph")],
                        [U("c"), B("subject"), U("s"), B("default_graph")],
                        [U("c"), B("property"), U("p"), B("default_graph")],
                        [U("c"), B("object"), U("o"), B("default_graph")],
                    ],
                    &[[U("s"), U("p"), U("o"), B("default_graph")]],
                ],
                [
                    &[
                        [U("person_a"), B("is"), B("awesome"), B("default_graph")],
                        [
                            U("person_a"),
                            B("friendswith"),
                            U("person_b"),
                            B("default_graph"),
                        ],
                    ],
                    &[[U("person_b"), B("is"), B("awesome"), B("default_graph")]],
                ],
                [
                    &[[
                        U("person_a"),
                        B("friendswith"),
                        U("person_b"),
                        B("default_graph"),
                    ]],
                    &[[
                        U("person_b"),
                        B("friendswith"),
                        U("person_a"),
                        B("default_graph"),
                    ]],
                ],
            ];
            ru.iter()
                .map(|[ifa, then]| Rule::create(ifa.to_vec(), then.to_vec()).unwrap())
                .collect()
        };
        let facts: &[[&str; 4]] = &[
            ["soyoung", "friendswith", "nick", "default_graph"],
            ["nick", "friendswith", "elina", "default_graph"],
            ["elina", "friendswith", "sam", "default_graph"],
            ["sam", "friendswith", "fausto", "default_graph"],
            ["fausto", "friendswith", "lovesh", "default_graph"],
            ["andrew", "claims", "_:claim1", "default_graph"],
            ["_:claim1", "subject", "lovesh", "default_graph"],
            ["_:claim1", "property", "is", "default_graph"],
            ["_:claim1", "object", "awesome", "default_graph"],
        ];
        let composite_claims: &[[&str; 4]] = &[
            ["soyoung", "is", "awesome", "default_graph"],
            ["nick", "is", "awesome", "default_graph"],
        ];
        let expected_proof: Vec<RuleApplication<&str>> = {
            let ep: &[(usize, &[&str])] = &[
                (0, &["_:claim1", "lovesh", "is", "awesome"]),
                (2, &["fausto", "lovesh"]),
                (1, &["lovesh", "fausto"]),
                (2, &["sam", "fausto"]),
                (1, &["fausto", "sam"]),
                (2, &["elina", "sam"]),
                (1, &["sam", "elina"]),
                (2, &["nick", "elina"]),
                (1, &["elina", "nick"]),
                (2, &["soyoung", "nick"]),
                (1, &["nick", "soyoung"]),
            ];
            ep.iter()
                .map(|(rule_index, inst)| RuleApplication {
                    rule_index: *rule_index,
                    instantiations: inst.to_vec(),
                })
                .collect()
        };
        let proof = prove::<&str, &str>(facts, composite_claims, &rules).unwrap();
        assert!(
            proof.len() <= expected_proof.len(),
            "if this assertion fails, the generated proof length is longer than it was previously"
        );
        assert_eq!(
            proof, expected_proof,
            "if this assertion fails the proof may still be valid but the order of the proof may \
             have changed"
        );
        let Valid { assumed, implied } = validate(&rules, &proof).unwrap();
        for claim in composite_claims {
            assert!(implied.contains(claim));
            assert!(
                !facts.contains(claim),
                "all theorems are expected to be composite for this particular problem"
            );
        }
        for assumption in &assumed {
            assert!(
                assumed.contains(assumption),
                "This problem was expected to use all porvided assumptions."
            );
        }
    }

    #[test]
    fn ancestry_high_prove_and_verify() {
        let mut next_uniq = 0u32;
        let parent = inc(&mut next_uniq);
        let ancestor = inc(&mut next_uniq);
        let default_graph = inc(&mut next_uniq);
        let nodes: Vec<u32> = (0usize..10).map(|_| inc(&mut next_uniq)).collect();
        let facts: Vec<[u32; 4]> = nodes
            .iter()
            .zip(nodes.iter().cycle().skip(1))
            .map(|(a, b)| [*a, parent, *b, default_graph])
            .collect();
        let rules = decl_rules(&[
            [
                &[[U("a"), B(parent), U("b"), B(default_graph)]],
                &[[U("a"), B(ancestor), U("b"), B(default_graph)]],
            ],
            [
                &[
                    [U("a"), B(ancestor), U("b"), B(default_graph)],
                    [U("b"), B(ancestor), U("c"), B(default_graph)],
                ],
                &[[U("a"), B(ancestor), U("c"), B(default_graph)]],
            ],
        ]);
        let composite_claims = vec![
            [nodes[0], ancestor, *nodes.last().unwrap(), default_graph],
            [*nodes.last().unwrap(), ancestor, nodes[0], default_graph],
            [nodes[0], ancestor, nodes[0], default_graph],
            // (first node, parent, second node) is a premise
            [nodes[0], parent, nodes[1], default_graph],
        ];
        let proof = prove::<&str, u32>(&facts, &composite_claims, &rules).unwrap();
        let Valid { assumed, implied } = validate::<&str, u32>(&rules, &proof).unwrap();
        assert_eq!(
            &assumed,
            &facts.iter().cloned().collect(),
            "all supplied premises are expected to be used for this proof"
        );
        assert!(!facts.contains(&composite_claims[0]));
        assert!(!facts.contains(&composite_claims[1]));
        assert!(!facts.contains(&composite_claims[2]));
        assert!(facts.contains(&composite_claims[3]));
        for claim in composite_claims {
            assert!(implied.contains(&claim) ^ facts.contains(&claim));
        }
        for fact in facts {
            assert!(!implied.contains(&fact));
        }
    }

    #[test]
    fn no_proof_is_generated_for_facts() {
        let facts: Vec<[&str; 4]> = vec![
            ["tacos", "are", "tasty", "default_graph"],
            ["nachos", "are", "tasty", "default_graph"],
            ["nachos", "are", "food", "default_graph"],
        ];
        let rules = decl_rules::<&str, &str>(&[[
            &[[B("nachos"), B("are"), B("tasty"), B("default_graph")]],
            &[[B("nachos"), B("are"), B("food"), B("default_graph")]],
        ]]);
        let composite_claims = vec![["nachos", "are", "food", "default_graph"]];
        let proof = prove::<&str, &str>(&facts, &composite_claims, &rules).unwrap();
        assert_eq!(&proof, &vec![]);
    }

    #[test]
    fn unconditional_rule() {
        let facts: Vec<[&str; 4]> = vec![];
        let rules = decl_rules::<&str, &str>(&[[
            &[],
            &[[B("nachos"), B("are"), B("food"), B("default_graph")]],
        ]]);
        let composite_claims = vec![["nachos", "are", "food", "default_graph"]];
        let proof = prove::<&str, &str>(&facts, &composite_claims, &rules).unwrap();
        assert_eq!(
            &proof,
            &[RuleApplication {
                rule_index: 0,
                instantiations: vec![]
            }]
        );
    }
}