Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Formula

Class for representing propositional formulas.

Formulas have a recursive structure - complex formulas have other formulas as parts (i.e., operands). The "base case" of this structure is the atomic proposition. A formulaString for an atomic proposition can be identified syntactically as a single lower-case letter after removing any parentheses and whitespace.

The class is instantiated with a formulaString, which we want to parse into its Operator (simply an enum) and its operands, which ultimately should be Formulas as well. We do an initial parse which gets the main Operator and ParsedInterface.operands in string format. We can then recurse on the operands until we just hit atomic propositions.

We rely on a canonical string representation of a Formula to compare the identity of different Formulas - i.e., the cleansedFormulaString. When the class is instantiated, we parse it in the manner described here and compute and store its "cleansed" representation.

Hierarchy

  • Formula

Index

Constructors

constructor

  • new Formula(formulaString: string): Formula
  • Parameters

    • formulaString: string

      A logical formula in string format.

    Returns Formula

Properties

cleansedFormulaString

cleansedFormulaString: string

The canonical string representation of the formula. It is stripped of whitespace and any extra parens. This can be used for checking equality of two Formulas (see isEqual).

formulaString

formulaString: string

The initial string passed to the constructor.

Optional operands

operands: Formula[]

The Formula's main operands, if it is complex.

Optional operator

operator: Operator

The Formula's main operator, if it is complex.

Accessors

prettifiedFormula

  • get prettifiedFormula(): string
  • Getter that prints the "prettified" version of the current formula. (No extra parens; one single space between operators and operands.)

    Returns string

Methods

evaluateFormulaString

  • Parameters

    Returns boolean

generateTruthTable

  • generateTruthTable(formulaString?: string, partial?: boolean): boolean[][]
  • Parameters

    • Default value formulaString: string = this.cleansedFormulaString
    • Default value partial: boolean = false

    Returns boolean[][]

generateTruthTableHeaders

  • generateTruthTableHeaders(formulaString?: string): string[]
  • Parameters

    • Default value formulaString: string = this.formulaString

    Returns string[]

getAtomicVariables

  • getAtomicVariables(formulaString?: string): string[]
  • Class method invoking the static method Formula.getAtomicVariables.

    Parameters

    • Default value formulaString: string = this.cleansedFormulaString

    Returns string[]

isAtomicString

  • isAtomicString(string?: string): boolean
  • Parameters

    • Default value string: string = this.cleansedFormulaString

    Returns boolean

isEqual

  • isEqual(formula: Formula | string, formula2?: string | Formula): boolean
  • Parameters

    • formula: Formula | string
    • Default value formula2: string | Formula = this as Formula | string

    Returns boolean

isNegation

  • isNegation(formula: Formula | string, formula2?: string | Formula): boolean
  • Parameters

    • formula: Formula | string
    • Default value formula2: string | Formula = this as Formula | string

    Returns boolean

isWFFString

  • isWFFString(formulaString?: string): boolean
  • Parameters

    • Default value formulaString: string = this.formulaString

    Returns boolean

negateFormula

  • Parameters

    • Default value formula: string | Formula = this as Formula | string

    Returns Formula

parseStringBasic

  • Parameters

    • Default value formulaString: string = this.cleansedFormulaString

    Returns ParsedInterface | null

prettyFormula

  • prettyFormula(formulaString?: string): string
  • Parameters

    • Default value formulaString: string = this.cleansedFormulaString

    Returns string

removeWhiteSpace

  • removeWhiteSpace(str?: string): string
  • Parameters

    • Default value str: string = this.cleansedFormulaString

    Returns string

trimOuterParens

  • trimOuterParens(formulaString?: string): string
  • Parameters

    • Default value formulaString: string = this.cleansedFormulaString

    Returns string

Static Private cleanseFormulaString

  • cleanseFormulaString(formula?: string): string | undefined
  • Remove all whitespace and unnecessary parentheses from a formula. This produces a canonical string representation of a formula.

    Parameters

    • Optional formula: string

      the formula to "cleanse"

    Returns string | undefined

    • the cleansed formula

Static evaluateFormulaString

  • Takes a formula (string) and a set of assignments of propositional variables to truth values, and returns true iff the proposition represented by the formula is true under the given assignment. Returns null if the string is non-well-formed. (Cannot evaluate.) Returns undefined if the formula contains atomic variables that do not have an assignment. (Truth value is indeterminate.)

    Parameters

    • formulaString: string

      String representing the proposition to evaluate.

    • assignment: AssignmentInterface

      Assignment of truth values to atomic variables.

    Returns boolean

    • Is the formulaString true under the assignment?

Static findMainBinaryOperatorIndex

  • findMainBinaryOperatorIndex(trimmedFormulaString: string): number
  • Find the index of the main binary operator in a formula string, if it exists, or -1 if there is no main binary operator.

    Parameters

    • trimmedFormulaString: string

      The string to be analyzed. We assume that extra parens have been trimmed.

    Returns number

    • The index of the main operator.

Static generateTruthTable

  • generateTruthTable(formulaString: string, partial?: boolean): boolean[][]
  • Generate a complete truth table with values filled in if partial is true.

    Parameters

    • formulaString: string
    • Default value partial: boolean = false

    Returns boolean[][]

    • Truth table as matrix with values filled in.

Static generateTruthTableHeaders

  • generateTruthTableHeaders(formulaString: string): string[]
  • Generate the headers for the truth table.

    Parameters

    • formulaString: string

      Optional formulaString argument.

    Returns string[]

    • Truth table headers sorted alphabetically and by length.

Static getAtomicVariables

  • getAtomicVariables(formulaString: string): string[]
  • Returns a sorted list of the atomic variables of a formulaString.

    Parameters

    • formulaString: string

      The formula whose variables will be retrieved.

    Returns string[]

    • An array with unique atomic variables (lower case letters),
          sorted alphabetically.

Static isAtomicString

  • isAtomicString(string: string): boolean
  • Returns true iff the formula string represents an atomic proposition.

    Parameters

    • string: string

      The string to test

    Returns boolean

    • Is the input string atomic?

Static isEqual

  • isEqual(formula: Formula | string, formula2: Formula | string): boolean
  • Compares two formulas for equality (same propositions/operators/operands).

    Parameters

    • formula: Formula | string

      Formula to be compared.

    • formula2: Formula | string

      Other formula to be compared (if absent, compares with this formula).

    Returns boolean

    • Are the two formulas identical?

Static isNegation

  • isNegation(formula: Formula | string, formula2: Formula | string): boolean
  • Checks whether a formula is the negation of another.

    Parameters

    • formula: Formula | string

      formula to compare for negation

    • formula2: Formula | string

      formula to compare for negation

    Returns boolean

    • Is formula2 the negation of formula?

Static isWFFString

  • isWFFString(formulaString: string): boolean
  • Returns true iff the formulaString is a well-formed formula (wff).

    Parameters

    • formulaString: string

      The string to be analyzed.

    Returns boolean

    • Does the string represent a wff?

Static negateFormula

  • Generate a new Formula that is the negation of the input formula (or this formula by default).

    Parameters

    • Optional formula: Formula | string

      The formula to be negated.

    Returns Formula

    • The negated formula.

Static parseStringBasic

  • Takes a formula string and returns an object with the main operator and main operands in string form.

    Parameters

    • formulaString: string

      Formula string with extra parens removed.

    Returns ParsedInterface | null

    • Object with operator and operands.

Static prettyFormula

  • prettyFormula(formulaString: string): string
  • Takes a formulaString and returns a pretty, normalized formatting with a single space between arguments and operators.

    Parameters

    • formulaString: string

      The formula to be prettified.

    Returns string

    • Prettified formula.

Static removeWhiteSpace

  • removeWhiteSpace(string: string): string
  • Remove all whitespace from a string.

    Parameters

    • string: string

    Returns string

    • String with whitespace removed.

Static translateEnglishToSymbolic

  • translateEnglishToSymbolic(formulaString: string): string
  • Parameters

    • formulaString: string

    Returns string

Static trimOuterParens

  • trimOuterParens(formulaString: string): string
  • Takes a formula and recursively removes any "extra" leading/trailing parentheses, i.e.: ((p & (q -> r))) => p & (q -> r)

    Parameters

    • formulaString: string

      the string to be trimmed

    Returns string

    • the trimmed string

Generated using TypeDoc