create all modal logic axioms in csharp

Here is an implementation of all the modal logic axioms in C#:

main.cs
public abstract class ModalFormula
{
    public abstract bool Evaluate(KripkeModel model, World world);

    public ModalFormula Not => new NotFormula(this);

    public ModalFormula And(ModalFormula other) => new AndFormula(this, other);

    public ModalFormula Or(ModalFormula other) => new OrFormula(this, other);

    public ModalFormula Implies(ModalFormula other) => new ImpliesFormula(this, other);

    public ModalFormula Box => new BoxFormula(this);

    public ModalFormula Diamond => new DiamondFormula(this);
}

public class PropositionFormula : ModalFormula
{
    private readonly string _name;

    public PropositionFormula(string name)
    {
        _name = name;
    }

    public override bool Evaluate(KripkeModel model, World world)
    {
        return model.GetPropositionValue(_name, world);
    }
}

public class AndFormula : ModalFormula
{
    private readonly ModalFormula _left;
    private readonly ModalFormula _right;

    public AndFormula(ModalFormula left, ModalFormula right)
    {
        _left = left;
        _right = right;
    }

    public override bool Evaluate(KripkeModel model, World world)
    {
        return _left.Evaluate(model, world) && _right.Evaluate(model, world);
    }
}

public class OrFormula : ModalFormula
{
    private readonly ModalFormula _left;
    private readonly ModalFormula _right;

    public OrFormula(ModalFormula left, ModalFormula right)
    {
        _left = left;
        _right = right;
    }

    public override bool Evaluate(KripkeModel model, World world)
    {
        return _left.Evaluate(model, world) || _right.Evaluate(model, world);
    }
}

public class NotFormula : ModalFormula
{
    private readonly ModalFormula _formula;

    public NotFormula(ModalFormula formula)
    {
        _formula = formula;
    }

    public override bool Evaluate(KripkeModel model, World world)
    {
        return !_formula.Evaluate(model, world);
    }
}

public class ImpliesFormula : ModalFormula
{
    private readonly ModalFormula _left;
    private readonly ModalFormula _right;

    public ImpliesFormula(ModalFormula left, ModalFormula right)
    {
        _left = left;
        _right = right;
    }

    public override bool Evaluate(KripkeModel model, World world)
    {
        return !_left.Evaluate(model, world) || _right.Evaluate(model, world);
    }
}

public class BoxFormula : ModalFormula
{
    private readonly ModalFormula _formula;

    public BoxFormula(ModalFormula formula)
    {
        _formula = formula;
    }

    public override bool Evaluate(KripkeModel model, World world)
    {
        return model.GetAccessibleWorlds(world).All(x => _formula.Evaluate(model, x));
    }
}

public class DiamondFormula : ModalFormula
{
    private readonly ModalFormula _formula;

    public DiamondFormula(ModalFormula formula)
    {
        _formula = formula;
    }

    public override bool Evaluate(KripkeModel model, World world)
    {
        return model.GetAccessibleWorlds(world).Any(x => _formula.Evaluate(model, x));
    }
}
3031 chars
128 lines

These classes implement the basic modal logic operations: propositions, negation, conjunction, disjunction, implication, necessity (box), and possibility (diamond). They work with Kripke models, which are structures that represent possible worlds and their relationships.

To use these formulas, you can create instances of PropositionFormula representing your atomic propositions, and then combine them using the logic operations. For example, (p1.And(p2)).Implies(new PropositionFormula("p3")) represents the formula "(p1 AND p2) -> p3".

You can then evaluate the formulas using a KripkeModel and a World object. The model describes the relationships between worlds, and the World object represents the current world. The Evaluate method returns true if the formula holds in the current world of the model.

gistlibby LogSnag