In [1]:
import sys
sys.path.append('../..')

import ddq.fol.lang as lang

In [2]:
# Print tree helper function

def print_tree(root, children_func=iter, name_func=str):
    """Pretty print a tree, in the style of gnu tree"""
    
    # Inspired by https://stackoverflow.com/questions/9727673
    
    # prefix components:
    space =  '    '
    branch = '│   '
    # pointers:
    tee =    '├── '
    last =   '└── '
    
    def tree(dir_path, children_func, name_func, prefix: str=''):
        """A recursive generator, given a tree
        will yield a visual tree structure line by line
        with each line prefixed by the same characters
        """    
        contents = children_func(dir_path)
        # contents each get pointers that are ├── with a final └── :
        pointers = [tee] * (len(contents) - 1) + [last]
        for pointer, path in zip(pointers, contents):
            yield prefix + str(pointer) + name_func(path)
            if len(children_func(path)): # extend the prefix and recurse:
                extension = branch if pointer == tee else space 
                # i.e. space because last, └── , above so no more |
                yield from tree(path, children_func, name_func, prefix=prefix+extension)

    for line in tree(root, children_func, name_func):
        print(line)

In [3]:
# Print the language's symbol hierarchy

def print_symbol_hierarchy(root_class):
    """Print the hierarchy and include a canonical example for each class"""
    print_tree(root_class, 
               lambda cls: cls.__subclasses__(), 
               lambda cls: cls.__name__ 
                           + " [ " + cls.canonical_instance().symbol_type() 
                           + ": " + cls.canonical_instance().expression()
                           + " ]")

print_symbol_hierarchy(lang.Symbol)


└── PrimitiveSymbol [ primitive symbol: ? ]
    ├── ImproperSymbol [ improper symbol: ? ]
    │   ├── LeftParenSymbol [ left parenthesis: ( ]
    │   ├── RightParenSymbol [ right parenthesis: ) ]
    │   ├── ConjSymbol [ conjunction: ^ ]
    │   ├── DisjSymbol [ disjunction: v ]
    │   ├── NegSymbol [ negation: ~ ]
    │   ├── ImplSymbol [ material implication: => ]
    │   ├── EquivSymbol [ material equivalence: == ]
    │   ├── UniversalSymbol [ universal quantifier: ∀ ]
    │   └── ExistentialSymbol [ existential quantifier: ∃ ]
    ├── IndividualSymbol [ individual: ? ]
    │   ├── Constant [ constant: c ]
    │   └── Variable [ variable: ? ]
    │       ├── IndividualVariable [ individual variable: p ]
    │       └── PropositionalVariable [ propositional variable: X ]
    ├── Function [ function: f ]
    └── Predicate [ predicate: P ]

Note

What about adding our own differentiation between Specific and Arbitrary indiv var, maybe not as part of the language though :(, but then, as part of what?


In [4]:
# Print the language's Term hierarchy

def print_symbol_hierarchy(root_class):
    """Print the hierarchy and include a canonical example for each class"""
    print_tree(root_class, 
               lambda cls: cls.__subclasses__(), 
               lambda cls: cls.__name__
                      + " [ " + cls.canonical_instance().expression()
                      + " ]")

print_symbol_hierarchy(lang.Term)


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-dd4dda22915b> in <module>
      9                       + " ]")
     10 
---> 11 print_symbol_hierarchy(lang.Term)

<ipython-input-4-dd4dda22915b> in print_symbol_hierarchy(root_class)
      5     print_tree(root_class, 
      6                lambda cls: cls.__subclasses__(),
----> 7                lambda cls: cls.__name__
      8                       + " [ " + cls.canonical_instance().expression()
      9                       + " ]")

<ipython-input-2-a2e1335c1f47> in print_tree(root, children_func, name_func)
     28                 yield from tree(path, children_func, name_func, prefix=prefix+extension)
     29 
---> 30     for line in tree(root, children_func, name_func):
     31         print(line)

<ipython-input-2-a2e1335c1f47> in tree(dir_path, children_func, name_func, prefix)
     22         pointers = [tee] * (len(contents) - 1) + [last]
     23         for pointer, path in zip(pointers, contents):
---> 24             yield prefix + str(pointer) + name_func(path)
     25             if len(children_func(path)): # extend the prefix and recurse:
     26                 extension = branch if pointer == tee else space

<ipython-input-4-dd4dda22915b> in <lambda>(cls)
      7                lambda cls: cls.__name__
      8                       + " [ " + cls.canonical_instance().expression()
----> 9                       + " ]")
     10 
     11 print_symbol_hierarchy(lang.Term)

AttributeError: 'Term' object has no attribute 'expression'

In [4]:
# Print the language's Wff hierarchy

def print_symbol_hierarchy(root_class):
    """Print the hierarchy and include a canonical example for each class"""
    print_tree(root_class, 
               lambda cls: cls.__subclasses__(), 
               lambda cls: cls.__name__
                      + " [ " + cls.canonical_instance().expression()
                      + " ]")

print_symbol_hierarchy(lang.Wff)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-f6d10dcea2e0> in <module>
      9                       + " ]")
     10 
---> 11 print_symbol_hierarchy(lang.Wff)

<ipython-input-4-f6d10dcea2e0> in print_symbol_hierarchy(root_class)
      5     print_tree(root_class, 
      6                lambda cls: cls.__subclasses__(),
----> 7                lambda cls: cls.__name__
      8                       + " [ " + cls.canonical_instance().expression()
      9                       + " ]")

<ipython-input-2-a2e1335c1f47> in print_tree(root, children_func, name_func)
     28                 yield from tree(path, children_func, name_func, prefix=prefix+extension)
     29 
---> 30     for line in tree(root, children_func, name_func):
     31         print(line)

<ipython-input-2-a2e1335c1f47> in tree(dir_path, children_func, name_func, prefix)
     22         pointers = [tee] * (len(contents) - 1) + [last]
     23         for pointer, path in zip(pointers, contents):
---> 24             yield prefix + str(pointer) + name_func(path)
     25             if len(children_func(path)): # extend the prefix and recurse:
     26                 extension = branch if pointer == tee else space

<ipython-input-4-f6d10dcea2e0> in <lambda>(cls)
      7                lambda cls: cls.__name__
      8                       + " [ " + cls.canonical_instance().expression()
----> 9                       + " ]")
     10 
     11 print_symbol_hierarchy(lang.Wff)

~/github/connect/ddq/fol/lang.py in canonical_instance()
    316     @staticmethod
    317     def canonical_instance() -> "PropVarWff":
--> 318         return PropVarWff([PropositionalVariable.canonical_instance()])
    319 
    320 

~/github/connect/ddq/fol/lang.py in __init__(self, var)
    300 class PropVarWff(Wff):
    301     def __init__(self, var: PropositionalVariable):
--> 302         super.__init__([var])
    303 
    304     def is_atomic(self) -> bool:

TypeError: descriptor '__init__' requires a 'super' object but received a 'list'

In [ ]: