Function parameters:
The propositional formula in which substitution will occur.
The atomic proposition (variable) to be replaced.
The formula or atom to replace the target atom with.
A new formula with all occurrences of the atom replaced by the substitute.
// Replace q with s in (p => (q => r))
const formula: PropFormula = { operator: Operator.Implies, values: [
{ operator: Operator.Var, values: [['p']] },
{ operator: Operator.Implies, values: [
{ operator: Operator.Var, values: [['q']] },
{ operator: Operator.Var, values: [['r']] }
]}
]};
const result = replaceAtomInFormula({ formula, atom: ['q'], substitute: { operator: Operator.Var, values: [['s']] } });
// Result: p => (s => r)
Recursively replaces all occurrences of a specified atom in a formula with a substitute formula or atom.
This function traverses the formula tree and replaces every instance of the target atom with the provided substitute. If the atom is not present in the formula, the original formula is returned unchanged.