Esempio n. 1
0
        private Decomposition DecomposeWFF(Biconditional f)
        {
            List <WFF> left  = new List <WFF>();
            List <WFF> right = new List <WFF>();

            left.Add(f.left);
            left.Add(f.right);
            right.Add(f.left.GetNegation());
            right.Add(f.right.GetNegation());
            return(new Decomposition(f, left, right));
        }
Esempio n. 2
0
        private List <WFF> IdentityDecompose(Identity i, Biconditional f)
        {
            List <WFF> left   = IdentityDecompose(i, f.left);
            List <WFF> right  = IdentityDecompose(i, f.right);
            List <WFF> result = new List <WFF>();

            foreach (WFF a in left)
            {
                foreach (WFF b in right)
                {
                    result.Add(new Biconditional(a, b));
                }
            }

            return(result);
        }
Esempio n. 3
0
        internal static void AddTree(this Subproof p, Tree t)
        {
            if (t == null)
            {
                throw new ArgumentNullException();
            }
            if (t.State != TreeState.Closed)
            {
                throw new InvalidOperationException();
            }

            if (t.decomposed == null)
            {
                // Something got a contradiction here, find out what
                List <WFF> all = t.getAllSentences();
                WFF        c1  = null;
                WFF        c2  = null;
                for (int a = 0; a < all.Count; a++)
                {
                    if (all[a] is Negation)
                    {
                        Negation n = (Negation)all[a];
                        if (n.inner is Identity)
                        {
                            Identity i = (Identity)n.inner;
                            if (i.left.Equals(i.right))
                            {
                                c1 = all[a];
                                break;
                            }
                        }
                    }
                    else if (all[a] is Contradiction)
                    {
                        c1 = all[a];
                        break;
                    }

                    bool found = false;
                    for (int b = a + 1; b < all.Count; b++)
                    {
                        if (all[a].Equals(all[b].GetNegation()) || all[b].Equals(all[a].GetNegation()))
                        {
                            c1    = all[a];
                            c2    = all[b];
                            found = true;
                            break;
                        }
                    }

                    if (found)
                    {
                        break;
                    }
                }

                Proof p1 = p.Find(c1);
                if (p1 == null)
                {
                    throw new InvalidOperationException("p1");
                }

                if (c2 == null)
                {
                    p.steps.Add(new ProofLine(new Contradiction(), Rule.ContradictionIntro, p1));
                }
                else
                {
                    Proof p2 = p.Find(c2);
                    if (p2 == null)
                    {
                        throw new InvalidOperationException("p2");
                    }

                    p.steps.Add(new ProofLine(new Contradiction(), Rule.ContradictionIntro, p1, p2));
                }

                return;
            }

            Proof d = p.Find(t.decomposed);

            if (t.decomposed is Biconditional)
            {
                // Add the steps for Equiv
                Biconditional b   = (Biconditional)t.decomposed;
                Disjunction   equ = new Disjunction(
                    new Conjunction(b.left, b.right),
                    new Conjunction(b.left.GetNegation(), b.right.GetNegation()));
                ProofLine equp = new ProofLine(equ, Rule.Equivalence, d);
                p.steps.Add(equp);

                ProofLine plleft = new ProofLine(equ.disjuncts[0], Rule.Assumption);
                Subproof  left   = new Subproof();
                left.parent = p;
                left.assumptions.Add(plleft);
                left.steps.Add(new ProofLine(b.left, Rule.AndElim, plleft));
                left.steps.Add(new ProofLine(b.right, Rule.AndElim, plleft));
                AddTree(left, t.children[0]);
                p.steps.Add(left);

                ProofLine plright = new ProofLine(equ.disjuncts[1], Rule.Assumption);
                Subproof  right   = new Subproof();
                right.parent = p;
                right.assumptions.Add(plright);
                right.steps.Add(new ProofLine(b.left.GetNegation(), Rule.AndElim, plright));
                right.steps.Add(new ProofLine(b.right.GetNegation(), Rule.AndElim, plright));
                AddTree(right, t.children[1]);
                p.steps.Add(right);

                p.steps.Add(new ProofLine(new Contradiction(), Rule.OrElim, equp, left, right));
            }
            else if (t.decomposed is Conditional)
            {
                // Add the steps for Impl
                Conditional c    = (Conditional)t.decomposed;
                Disjunction imp  = new Disjunction(c.antecedent.GetNegation(), c.consequent);
                ProofLine   impp = new ProofLine(imp, Rule.Implication, d);
                p.steps.Add(impp);

                Proof[] steps = new Proof[t.children.Length + 1];
                steps[0] = impp;
                for (int a = 0; a < t.children.Length; a++)
                {
                    steps[a + 1] = new Subproof();
                    Subproof s = (Subproof)steps[a + 1];
                    s.parent = p;
                    s.assumptions.Add(new ProofLine(t.children[a].sentences[0], Rule.Assumption));
                    AddTree(s, t.children[a]);
                    p.steps.Add(s);
                }
                p.steps.Add(new ProofLine(new Contradiction(), Rule.OrElim, steps));
            }
            else if (t.decomposed is Conjunction)
            {
                foreach (WFF f in t.children[0].sentences)
                {
                    p.steps.Add(new ProofLine(f, Rule.AndElim, d));
                }
                AddTree(p, t.children[0]);
            }
            else if (t.decomposed is Disjunction)
            {
                Proof[] steps = new Proof[t.children.Length + 1];
                steps[0] = d;
                for (int a = 0; a < t.children.Length; a++)
                {
                    steps[a + 1] = new Subproof();
                    Subproof s = (Subproof)steps[a + 1];
                    s.parent = p;
                    s.assumptions.Add(new ProofLine(t.children[a].sentences[0], Rule.Assumption));
                    AddTree(s, t.children[a]);
                    p.steps.Add(s);
                }
                p.steps.Add(new ProofLine(new Contradiction(), Rule.OrElim, steps));
            }
            else if (t.decomposed is Existential)
            {
                Subproof sub = new Subproof();
                sub.parent = p;
                sub.assumptions.Add(new ProofLine(t.children[0].sentences[0], Rule.Assumption));
                AddTree(sub, t.children[0]);
                p.steps.Add(sub);
                p.steps.Add(new ProofLine(new Contradiction(), Rule.ExistentialElim, d, sub));
            }
            else if (t.decomposed is Identity)
            {
                // Need to add some way to find the other source
                foreach (WFF f in t.children[0].sentences)
                {
                    p.steps.Add(new ProofLine(f, Rule.EqualsElim, d));
                }
                AddTree(p, t.children[0]);
            }
            else if (t.decomposed is Negation)
            {
                Negation n = (Negation)t.decomposed;
                if (n.inner is Biconditional)
                {
                    // I really hope there is a better way for this one
                    // Add the steps for DeM and Equiv
                    Biconditional b   = (Biconditional)n.inner;
                    ProofLine     equ = new ProofLine(new Negation(new Conjunction(new Disjunction(b.left.GetNegation(), b.right), new Disjunction(b.left, b.right.GetNegation()))), Rule.Equivalence, d);
                    p.steps.Add(equ);

                    ProofLine dem = new ProofLine(new Disjunction(new Negation(new Disjunction(b.left.GetNegation(), b.right)), new Negation(new Disjunction(b.left, b.right.GetNegation()))), Rule.DeMorgans, equ);
                    p.steps.Add(dem);

                    Subproof left = new Subproof();
                    left.parent = p;
                    ProofLine plleft = new ProofLine(new Negation(new Disjunction(b.left.GetNegation(), b.right)), Rule.Assumption);
                    left.assumptions.Add(plleft);
                    ProofLine demleft = new ProofLine(new Conjunction(b.left, b.right.GetNegation()), Rule.DeMorgans, plleft);
                    left.steps.Add(demleft);
                    left.steps.Add(new ProofLine(b.left, Rule.AndElim, demleft));
                    left.steps.Add(new ProofLine(b.right.GetNegation(), Rule.AndElim, demleft));
                    AddTree(left, t.children[1]);
                    p.steps.Add(left);

                    Subproof right = new Subproof();
                    right.parent = p;
                    ProofLine plright = new ProofLine(new Negation(new Disjunction(b.left, b.right.GetNegation())), Rule.Assumption);
                    right.assumptions.Add(plright);
                    ProofLine demright = new ProofLine(new Conjunction(b.left.GetNegation(), b.right), Rule.DeMorgans, plright);
                    right.steps.Add(demright);
                    right.steps.Add(new ProofLine(b.left.GetNegation(), Rule.AndElim, demright));
                    right.steps.Add(new ProofLine(b.right, Rule.AndElim, demright));
                    AddTree(right, t.children[0]);
                    p.steps.Add(right);

                    p.steps.Add(new ProofLine(new Contradiction(), Rule.OrElim, dem, left, right));
                }
                else if (n.inner is Conditional)
                {
                    // Add the steps for DeM and Impl
                    Conditional c   = (Conditional)n.inner;
                    ProofLine   imp = new ProofLine(new Negation(new Disjunction(c.antecedent.GetNegation(), c.consequent)), Rule.Implication, d);
                    p.steps.Add(imp);

                    ProofLine dem = new ProofLine(new Conjunction(c.antecedent, c.consequent.GetNegation()), Rule.DeMorgans, imp);
                    p.steps.Add(dem);

                    p.steps.Add(new ProofLine(c.antecedent, Rule.AndElim, dem));
                    p.steps.Add(new ProofLine(c.consequent.GetNegation(), Rule.AndElim, dem));

                    AddTree(p, t.children[0]);
                }
                else if (n.inner is Conjunction)
                {
                    // Add the steps for DeM
                    Conjunction i   = (Conjunction)n.inner;
                    List <WFF>  neg = new List <WFF>();
                    foreach (WFF f in i.conjuncts)
                    {
                        neg.Add(f.GetNegation());
                    }
                    Disjunction dem  = new Disjunction(neg.ToArray());
                    ProofLine   demp = new ProofLine(dem, Rule.DeMorgans, d);
                    p.steps.Add(demp);

                    Proof[] steps = new Proof[t.children.Length + 1];
                    steps[0] = demp;
                    for (int a = 0; a < t.children.Length; a++)
                    {
                        steps[a + 1] = new Subproof();
                        Subproof s = (Subproof)steps[a + 1];
                        s.parent = p;
                        s.assumptions.Add(new ProofLine(t.children[a].sentences[0], Rule.Assumption));
                        AddTree(s, t.children[a]);
                        p.steps.Add(s);
                    }
                    p.steps.Add(new ProofLine(new Contradiction(), Rule.OrElim, steps));
                }
                else if (n.inner is Disjunction)
                {
                    // Add the steps for DeM
                    Disjunction i   = (Disjunction)n.inner;
                    List <WFF>  neg = new List <WFF>();
                    foreach (WFF f in i.disjuncts)
                    {
                        neg.Add(f.GetNegation());
                    }
                    Conjunction dem = new Conjunction(neg.ToArray());

                    ProofLine demp = new ProofLine(dem, Rule.DeMorgans, d);
                    p.steps.Add(demp);

                    foreach (WFF f in t.children[0].sentences)
                    {
                        p.steps.Add(new ProofLine(f, Rule.AndElim, demp));
                    }
                    AddTree(p, t.children[0]);
                }
                else if (n.inner is Existential)
                {
                    // Add the steps for DeM
                    p.steps.Add(new ProofLine(t.children[0].sentences[0], Rule.DeMorgans, d));
                    AddTree(p, t.children[0]);
                }
                else if (n.inner is Negation)
                {
                    p.steps.Add(new ProofLine(t.children[0].sentences[0], Rule.NotElim, d));
                    AddTree(p, t.children[0]);
                }
                else if (n.inner is Universal)
                {
                    // Add the steps for DeM
                    p.steps.Add(new ProofLine(t.children[0].sentences[0], Rule.DeMorgans, d));
                    AddTree(p, t.children[0]);
                }
            }
            else if (t.decomposed is Universal)
            {
                foreach (WFF f in t.children[0].sentences)
                {
                    p.steps.Add(new ProofLine(f, Rule.UniversalElim, d));
                }
                AddTree(p, t.children[0]);
            }
        }
Esempio n. 4
0
        private Decomposition DecomposeWFF(Negation f)
        {
            if (f.inner is AtomicWFF)
            {
                return(null);
            }
            else if (f.inner is Biconditional)
            {
                Biconditional inner = (Biconditional)f.inner;
                List <WFF>    left  = new List <WFF>();
                List <WFF>    right = new List <WFF>();
                left.Add(inner.left.GetNegation());
                left.Add(inner.right);
                right.Add(inner.left);
                right.Add(inner.right.GetNegation());
                return(new Decomposition(f, left, right));
            }
            else if (f.inner is Conditional)
            {
                Conditional inner = (Conditional)f.inner;
                List <WFF>  left  = new List <WFF>();
                left.Add(inner.antecedent);
                left.Add(inner.consequent.GetNegation());
                return(new Decomposition(f, left));
            }
            else if (f.inner is Conjunction)
            {
                Conjunction  inner     = (Conjunction)f.inner;
                List <WFF>[] conjuncts = new List <WFF> [inner.arity];
                for (int a = 0; a < inner.arity; a++)
                {
                    conjuncts[a] = new List <WFF>();
                    conjuncts[a].Add(inner.conjuncts[a].GetNegation());
                }

                return(new Decomposition(f, conjuncts.ToArray()));
            }
            else if (f.inner is Disjunction)
            {
                Disjunction inner     = (Disjunction)f.inner;
                List <WFF>  disjuncts = new List <WFF>();
                foreach (WFF d in inner.disjuncts)
                {
                    disjuncts.Add(d.GetNegation());
                }
                return(new Decomposition(f, disjuncts));
            }
            else if (f.inner is Existential)
            {
                Existential inner = (Existential)f.inner;
                List <WFF>  dec   = new List <WFF>();
                dec.Add(new Universal(inner.variable, inner.scope.GetNegation()));
                return(new Decomposition(f, dec));
            }
            else if (f.inner is Negation)
            {
                Negation   inner = (Negation)f.inner;
                List <WFF> dec   = new List <WFF>();
                dec.Add(inner.inner);
                return(new Decomposition(f, dec));
            }
            else if (f.inner is Universal)
            {
                Universal  inner = (Universal)f.inner;
                List <WFF> dec   = new List <WFF>();
                dec.Add(new Existential(inner.variable, inner.scope.GetNegation()));
                return(new Decomposition(f, dec));
            }

            throw new NotImplementedException(f.inner.GetType().ToString());
        }
Esempio n. 5
0
        private Node CreateABinaryTree(ref string formula, Node node)
        {
            if (node == null)
            {
                switch (formula[0])
                {
                case '~':
                    formula = formula.Remove(0, 1);
                    node    = new Negation(null);
                    break;

                case '>':
                    formula = formula.Remove(0, 1);
                    node    = new Implication(null, null);
                    break;

                case '=':
                    formula = formula.Remove(0, 1);
                    node    = new Biconditional(null, null);
                    break;

                case '&':
                    formula = formula.Remove(0, 1);
                    node    = new Conjunction(null, null);
                    break;

                case '|':
                    formula = formula.Remove(0, 1);
                    node    = new Disjunction(null, null);
                    break;

                case '%':
                    formula = formula.Remove(0, 1);
                    formula = formula.Insert(0, "&");
                    node    = new Negation(null);
                    break;

                case '(':
                    formula = formula.Remove(0, 1);
                    return(this.CreateABinaryTree(ref formula, node));

                case ',':
                    formula = formula.Remove(0, 1);
                    return(this.CreateABinaryTree(ref formula, node));

                case ')':
                    formula = formula.Remove(0, 1);
                    return(this.CreateABinaryTree(ref formula, node));

                default:
                    node = new Leaf(formula[0]);
                    if (!leaves.Exists(e => e == node.Value))
                    {
                        this.leaves.Add(node.Value);
                    }
                    formula = formula.Remove(0, 1);
                    break;
                }
                if (!(node is Leaf))
                {
                    node.leftChild = CreateABinaryTree(ref formula, node.leftChild);
                }

                if (!(node is Negation) && !(node is Leaf))
                {
                    node.rightChild = CreateABinaryTree(ref formula, node.rightChild);
                }

                return(node);
            }
            else
            {
                return(node);
            }
        }