Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] expose an open-ended reasoning function that proves all inferences #15

Open
bddap opened this issue Mar 17, 2021 · 2 comments

Comments

@bddap
Copy link
Contributor

bddap commented Mar 17, 2021

The current function signature for infer() is:

pub fn infer<Unbound: Ord + Clone, Bound: Ord + Clone>(
    premises: &[[Bound; 4]],
    rules: &[Rule<Unbound, Bound>],
) -> Vec<[Bound; 4]>;

I can imagine an alternate version being useful:

pub fn infer2<Unbound: Ord + Clone, Bound: Ord + Clone>(
    premises: &[[Bound; 4]],
    rules: &[Rule<Unbound, Bound>],
) -> Vec<RuleApplication<Bound>>;
validate(rules, infer2(premises, rules)).unwrap() == infer(premises, rules)
@bddap
Copy link
Contributor Author

bddap commented Mar 17, 2021

Fyi, as of now the current version of infer() in development.

@bddap
Copy link
Contributor Author

bddap commented Mar 17, 2021

Potential implementation of `infer2()` (not tested)
use crate::common::{forward, vertices, LowRuleApplication};
use crate::reasoner::{Quad, Reasoner};
use crate::rule::{LowRule, Rule};
use crate::translator::Translator;
use crate::RuleApplication;
use alloc::collections::BTreeSet;

/// Make all possible inferences.
pub fn infer2<Unbound: Ord + Clone, Bound: Ord + Clone>(
    premises: &[[Bound; 4]],
    rules: &[Rule<Unbound, Bound>],
) -> Vec<RuleApplication<Bound>> {
    let tran: Translator<Bound> = vertices(premises, rules).cloned().collect();
    let lpremises: Vec<Quad> = premises
        .iter()
        .map(|quad| forward(&tran, quad).unwrap())
        .collect();
    let lrules: Vec<LowRule> = rules
        .iter()
        .cloned()
        .map(|rule: Rule<Unbound, Bound>| -> LowRule { rule.lower(&tran).map_err(|_| ()).unwrap() })
        .collect();
    low_infer(&lpremises, &lrules)
        .iter()
        .enumerate()
        .map(|(i, lra)| lra.raise(&rules[i], &tran))
        .collect()
}

/// A version of infer that operates on lowered input an returns output in lowered form.
fn low_infer2(premises: &[Quad], rules: &[LowRule]) -> Vec<LowRuleApplication> {
    let mut rs = Reasoner::default();
    for prem in premises {
        rs.insert(prem.clone());
    }

    let mut arguments: Vec<LowRuleApplication> = Default::default();

    loop {
        let mut to_add = BTreeSet::<Quad>::new();
        for (rule_index, rr) in rules.iter().enumerate() {
            rs.apply(&mut rr.if_all.clone(), &mut rr.inst.clone(), &mut |inst| {
                let mut novel = false;

                let ins = inst.as_ref();
                for implied in &rr.then {
                    let new_quad = [
                        ins[&implied.s.0],
                        ins[&implied.p.0],
                        ins[&implied.o.0],
                        ins[&implied.g.0],
                    ]
                    .into();
                    if !rs.contains(&new_quad) {
                        novel |= to_add.insert(new_quad);
                    }
                }

                if novel {
                    arguments.push(LowRuleApplication {
                        rule_index,
                        instantiations: ins.clone(),
                    });
                }
            });
        }
        if to_add.is_empty() {
            break;
        }
        for new in to_add.into_iter() {
            rs.insert(new);
        }
    }

    debug_assert!(
        {
            let mut args = arguments.clone();
            args.sort_unstable();
            args.windows(2).all(|w| w[0] != w[1])
        },
        "results of inference should not contain duplicates"
    );

    arguments
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant