2
$\begingroup$

I am trying to write an algorithm that constructs only well-formed formulas in PN. I have some list of symbols for binary connectives, unary connectives, and propositional variables (trying to make program robust for any length).

Here Polish Notation mentions an algorithm to test whether or not it is well-formed. It is much easier to make it so it is well-formed and not to have to test it later for computational purposes. It isn't difficult to make all possible permutations and then test. When looking EKKCprCqrKCpsCqsCApqKrs on the provide link, it seemed that I could "smoosh" the words EKKCCKCCCAK and prqrpsqspqrs at all possible places by creating a bunch of insertion sights, but not sure if this is always correct or can get every possible combination. I am teaching myself these things and having a difficult time thinking how to approach this. I am judging the answer based on explanation and also efficiency to generating wff's.

  • 1
    Generating ? Start with $p$ and you have only two possibilitiey : $p$ and $Np$. hen add $q$ and now you have the four "basic" : $p, Np, q, Nq$ plus all the combinations with the two (or more ?) binary connectives. I think that it is not hard to write the code, but the number depends on the initial stock of letters : $p,q,\ldots$. In the formal specifications of the syntax they are countable many : $p_0,p_1, \ldots$2017-02-03
  • 1
    A recursive algorithm for this is pretty simply to produce. Try starting with that and see if you can “flatten” it.2017-02-03
  • 0
    Are you interested in enumerating wwfs, or in generating random wwfs?2017-02-03
  • 0
    I am interested in enumeration.2017-02-03
  • 0
    For a given finite set of letters? Is the program ever supposed to stop (in practice)?2017-02-03
  • 0
    Yes. I am currently working with a finite set of letters, specifically Presburger arithmetic, but am trying to come up with an algorithm that works with any length. That way I can use it to teach myself more as I read/practice with other systems. I can easily come up with all possible combinations from the alphabets, but they don't necessarily make sense.2017-02-03
  • 0
    I kinda already did it for infix notation, it is much easier. I just took out pairs that don't make sense like ")0" or "01"(no multiplication in Presburger) at each wff of length n.. The nice thing about getting rid of the parenthesis is I don't have to deal with as many possible combinations, but I can no longer ignore pairs and the such.2017-02-03

1 Answers 1

1

Here's one solution in Python that generates formulae up to a certain depth. To keep clutter to a minimum, no arguments are passed to the script, but adding such parameters and passing in signature and depth should be easy. This version seems to work with both Python 2.7 and 3.5.

""" Enumerates wffs in Polish notation up to prescribed depth."""
from __future__ import print_function
from itertools import product

def generate(depth):
    if depth == 1:
        return variables
    else:
        wfflist = []
        for letter, arity in symbols:
            extensions = [[letter]]
            if arity > 0:
                extensions.extend([generate(depth-1)] * arity)
                wfflist.extend([list(w) for w in product(*extensions)])
            else:
                wfflist.extend(extensions)
        return wfflist

functions = ['C', 'K', 'A', 'E', 'D', 'N']
arities   = [ 2,   2,   2,   2,   2,   1 ]
variables = ['p', 'q', 'r'];
symbols = list(zip(functions + variables, arities + [0] * len(variables)));

for wff in generate(3): print(wff)