.. Copyright (C) 2001-2012 NLTK Project
.. For license information, see LICENSE.TXT

======================
Nonmonotonic Reasoning
======================

    >>> from nltk import *
    >>> from nltk.inference.nonmonotonic import *
    >>> from nltk.sem import logic
    >>> logic._counter._value = 0
    >>> lp = logic.LogicParser()
    
------------------------
Closed Domain Assumption
------------------------

The only entities in the domain are those found in the assumptions or goal.  
If the domain only contains "A" and "B", then the expression "exists x.P(x)" can
be replaced with "P(A) | P(B)" and an expression "all x.P(x)" can be replaced
with "P(A) & P(B)". 

    >>> p1 = lp.parse(r'all x.(man(x) -> mortal(x))')
    >>> p2 = lp.parse(r'man(Socrates)')
    >>> c = lp.parse(r'mortal(Socrates)')
    >>> prover = Prover9Command(c, [p1,p2])
    >>> prover.prove()
    True
    >>> cdp = ClosedDomainProver(prover)
    >>> for a in cdp.assumptions(): print a
    (man(Socrates) -> mortal(Socrates))
    man(Socrates)
    >>> cdp.prove()
    True

    >>> p1 = lp.parse(r'exists x.walk(x)')
    >>> p2 = lp.parse(r'man(Socrates)')
    >>> c = lp.parse(r'walk(Socrates)')
    >>> prover = Prover9Command(c, [p1,p2])
    >>> prover.prove()
    False
    >>> cdp = ClosedDomainProver(prover)
    >>> for a in cdp.assumptions(): print a
    walk(Socrates)
    man(Socrates)
    >>> cdp.prove()
    True

    >>> p1 = lp.parse(r'exists x.walk(x)')
    >>> p2 = lp.parse(r'man(Socrates)')
    >>> p3 = lp.parse(r'-walk(Bill)')
    >>> c = lp.parse(r'walk(Socrates)')
    >>> prover = Prover9Command(c, [p1,p2,p3])
    >>> prover.prove()
    False
    >>> cdp = ClosedDomainProver(prover)
    >>> for a in cdp.assumptions(): print a
    (walk(Socrates) | walk(Bill))
    man(Socrates)
    -walk(Bill)
    >>> cdp.prove()
    True

    >>> p1 = lp.parse(r'walk(Socrates)')
    >>> p2 = lp.parse(r'walk(Bill)')
    >>> c = lp.parse(r'all x.walk(x)')
    >>> prover = Prover9Command(c, [p1,p2])
    >>> prover.prove()
    False
    >>> cdp = ClosedDomainProver(prover)
    >>> for a in cdp.assumptions(): print a
    walk(Socrates)
    walk(Bill)
    >>> print cdp.goal()
    (walk(Socrates) & walk(Bill))
    >>> cdp.prove()
    True

    >>> p1 = lp.parse(r'girl(mary)')
    >>> p2 = lp.parse(r'dog(rover)')
    >>> p3 = lp.parse(r'all x.(girl(x) -> -dog(x))')
    >>> p4 = lp.parse(r'all x.(dog(x) -> -girl(x))')
    >>> p5 = lp.parse(r'chase(mary, rover)')
    >>> c = lp.parse(r'exists y.(dog(y) & all x.(girl(x) -> chase(x,y)))')
    >>> prover = Prover9Command(c, [p1,p2,p3,p4,p5])
    >>> print prover.prove()
    False
    >>> cdp = ClosedDomainProver(prover)
    >>> for a in cdp.assumptions(): print a
    girl(mary)
    dog(rover)
    ((girl(rover) -> -dog(rover)) & (girl(mary) -> -dog(mary)))
    ((dog(rover) -> -girl(rover)) & (dog(mary) -> -girl(mary)))
    chase(mary,rover)
    >>> print cdp.goal()
    ((dog(rover) & (girl(rover) -> chase(rover,rover)) & (girl(mary) -> chase(mary,rover))) | (dog(mary) & (girl(rover) -> chase(rover,mary)) & (girl(mary) -> chase(mary,mary))))
    >>> print cdp.prove()
    True

-----------------------
Unique Names Assumption
-----------------------

No two entities in the domain represent the same entity unless it can be 
explicitly proven that they do.  Therefore, if the domain contains "A" and "B", 
then add the assumption "-(A = B)" if it is not the case that 
"<assumptions> \|- (A = B)".

    >>> p1 = lp.parse(r'man(Socrates)')
    >>> p2 = lp.parse(r'man(Bill)')
    >>> c = lp.parse(r'exists x.exists y.-(x = y)')
    >>> prover = Prover9Command(c, [p1,p2])
    >>> prover.prove()
    False
    >>> unp = UniqueNamesProver(prover)
    >>> for a in unp.assumptions(): print a
    man(Socrates)
    man(Bill)
    -(Socrates = Bill)
    >>> unp.prove()
    True

    >>> p1 = lp.parse(r'all x.(walk(x) -> (x = Socrates))')
    >>> p2 = lp.parse(r'Bill = William')
    >>> p3 = lp.parse(r'Bill = Billy')
    >>> c = lp.parse(r'-walk(William)')
    >>> prover = Prover9Command(c, [p1,p2,p3])
    >>> prover.prove()
    False
    >>> unp = UniqueNamesProver(prover)
    >>> for a in unp.assumptions(): print a
    all x.(walk(x) -> (x = Socrates))
    (Bill = William)
    (Bill = Billy)
    -(William = Socrates)
    -(Billy = Socrates)
    -(Socrates = Bill)
    >>> unp.prove()
    True

-----------------------
Closed World Assumption
-----------------------

The only entities that have certain properties are those that is it stated
have the properties.  We accomplish this assumption by "completing" predicates.

If the assumptions contain "P(A)", then "all x.(P(x) -> (x=A))" is the completion 
of "P".  If the assumptions contain "all x.(ostrich(x) -> bird(x))", then 
"all x.(bird(x) -> ostrich(x))" is the completion of "bird".  If the 
assumptions don't contain anything that are "P", then "all x.-P(x)" is the 
completion of "P". 

    >>> p1 = lp.parse(r'walk(Socrates)')
    >>> p2 = lp.parse(r'-(Socrates = Bill)')
    >>> c = lp.parse(r'-walk(Bill)')
    >>> prover = Prover9Command(c, [p1,p2])
    >>> prover.prove()
    False
    >>> cwp = ClosedWorldProver(prover)
    >>> for a in cwp.assumptions(): print a
    walk(Socrates)
    -(Socrates = Bill)
    all z1.(walk(z1) -> (z1 = Socrates))
    >>> cwp.prove()
    True

    >>> p1 = lp.parse(r'see(Socrates, John)')
    >>> p2 = lp.parse(r'see(John, Mary)')
    >>> p3 = lp.parse(r'-(Socrates = John)')
    >>> p4 = lp.parse(r'-(John = Mary)')
    >>> c = lp.parse(r'-see(Socrates, Mary)')
    >>> prover = Prover9Command(c, [p1,p2,p3,p4])
    >>> prover.prove()
    False
    >>> cwp = ClosedWorldProver(prover)
    >>> for a in cwp.assumptions(): print a
    see(Socrates,John)
    see(John,Mary)
    -(Socrates = John)
    -(John = Mary)
    all z3 z4.(see(z3,z4) -> (((z3 = Socrates) & (z4 = John)) | ((z3 = John) & (z4 = Mary))))
    >>> cwp.prove()
    True

    >>> p1 = lp.parse(r'all x.(ostrich(x) -> bird(x))')
    >>> p2 = lp.parse(r'bird(Tweety)')
    >>> p3 = lp.parse(r'-ostrich(Sam)')
    >>> p4 = lp.parse(r'Sam != Tweety')
    >>> c = lp.parse(r'-bird(Sam)')
    >>> prover = Prover9Command(c, [p1,p2,p3,p4])
    >>> prover.prove()
    False
    >>> cwp = ClosedWorldProver(prover)
    >>> for a in cwp.assumptions(): print a
    all x.(ostrich(x) -> bird(x))
    bird(Tweety)
    -ostrich(Sam)
    -(Sam = Tweety)
    all z7.-ostrich(z7)
    all z8.(bird(z8) -> ((z8 = Tweety) | ostrich(z8)))
    >>> print cwp.prove()
    True

-----------------------
Multi-Decorator Example
-----------------------

Decorators can be nested to utilize multiple assumptions.

    >>> p1 = lp.parse(r'see(Socrates, John)')
    >>> p2 = lp.parse(r'see(John, Mary)')
    >>> c = lp.parse(r'-see(Socrates, Mary)')
    >>> prover = Prover9Command(c, [p1,p2])
    >>> print prover.prove()
    False
    >>> cmd = ClosedDomainProver(UniqueNamesProver(ClosedWorldProver(prover)))
    >>> print cmd.prove()
    True

-----------------
Default Reasoning
-----------------
    >>> logic._counter._value = 0
    >>> premises = []

define the taxonomy
    >>> premises.append(lp.parse(r'all x.(elephant(x)        -> animal(x))'))
    >>> premises.append(lp.parse(r'all x.(bird(x)            -> animal(x))'))
    >>> premises.append(lp.parse(r'all x.(dove(x)            -> bird(x))'))
    >>> premises.append(lp.parse(r'all x.(ostrich(x)         -> bird(x))'))
    >>> premises.append(lp.parse(r'all x.(flying_ostrich(x)  -> ostrich(x))'))

default the properties using abnormalities
    >>> premises.append(lp.parse(r'all x.((animal(x)  & -Ab1(x)) -> -fly(x))')) #normal animals don't fly
    >>> premises.append(lp.parse(r'all x.((bird(x)    & -Ab2(x)) -> fly(x))'))  #normal birds fly
    >>> premises.append(lp.parse(r'all x.((ostrich(x) & -Ab3(x)) -> -fly(x))')) #normal ostriches don't fly
    
specify abnormal entities
    >>> premises.append(lp.parse(r'all x.(bird(x)           -> Ab1(x))')) #flight
    >>> premises.append(lp.parse(r'all x.(ostrich(x)        -> Ab2(x))')) #non-flying bird
    >>> premises.append(lp.parse(r'all x.(flying_ostrich(x) -> Ab3(x))')) #flying ostrich

define entities
    >>> premises.append(lp.parse(r'elephant(el)'))
    >>> premises.append(lp.parse(r'dove(do)'))
    >>> premises.append(lp.parse(r'ostrich(os)'))
    
print the augmented assumptions list
    >>> prover = Prover9Command(None, premises)
    >>> command = UniqueNamesProver(ClosedWorldProver(prover))
    >>> for a in command.assumptions(): print a
    all x.(elephant(x) -> animal(x))
    all x.(bird(x) -> animal(x))
    all x.(dove(x) -> bird(x))
    all x.(ostrich(x) -> bird(x))
    all x.(flying_ostrich(x) -> ostrich(x))
    all x.((animal(x) & -Ab1(x)) -> -fly(x))
    all x.((bird(x) & -Ab2(x)) -> fly(x))
    all x.((ostrich(x) & -Ab3(x)) -> -fly(x))
    all x.(bird(x) -> Ab1(x))
    all x.(ostrich(x) -> Ab2(x))
    all x.(flying_ostrich(x) -> Ab3(x))
    elephant(el)
    dove(do)
    ostrich(os)
    all z1.(animal(z1) -> (elephant(z1) | bird(z1)))
    all z2.(Ab1(z2) -> bird(z2))
    all z3.(bird(z3) -> (dove(z3) | ostrich(z3)))
    all z4.(dove(z4) -> (z4 = do))
    all z5.(Ab2(z5) -> ostrich(z5))
    all z6.(Ab3(z6) -> flying_ostrich(z6))
    all z7.(ostrich(z7) -> ((z7 = os) | flying_ostrich(z7)))
    all z8.-flying_ostrich(z8)
    all z9.(elephant(z9) -> (z9 = el))
    -(el = os)
    -(el = do)
    -(os = do)

    >>> UniqueNamesProver(ClosedWorldProver(Prover9Command(lp.parse('-fly(el)'), premises))).prove()
    True
    >>> UniqueNamesProver(ClosedWorldProver(Prover9Command(lp.parse('fly(do)'), premises))).prove()
    True
    >>> UniqueNamesProver(ClosedWorldProver(Prover9Command(lp.parse('-fly(os)'), premises))).prove()
    True
