create s5 modal logic in csharp

To create a modal logic system in C#, we need to define its syntax and semantics. Here's an example of how we can create a simple S5 modal logic system:

main.cs
public abstract class Formula
{
    public abstract bool Evaluate(Model model);
}

public class Atom : Formula
{
    private string name;

    public Atom(string name)
    {
        this.name = name;
    }

    public override bool Evaluate(Model model)
    {
        return model.Check(this.name);
    }
}

public class Not : Formula
{
    private Formula formula;

    public Not(Formula formula)
    {
        this.formula = formula;
    }

    public override bool Evaluate(Model model)
    {
        return !this.formula.Evaluate(model);
    }
}

public class And : Formula
{
    private Formula left;
    private Formula right;

    public And(Formula left, Formula right)
    {
        this.left = left;
        this.right = right;
    }

    public override bool Evaluate(Model model)
    {
        return this.left.Evaluate(model) && this.right.Evaluate(model);
    }
}

public class Or : Formula
{
    private Formula left;
    private Formula right;

    public Or(Formula left, Formula right)
    {
        this.left = left;
        this.right = right;
    }

    public override bool Evaluate(Model model)
    {
        return this.left.Evaluate(model) || this.right.Evaluate(model);
    }
}

public class Box : Formula
{
    private string agent;
    private Formula formula;

    public Box(string agent, Formula formula)
    {
        this.agent = agent;
        this.formula = formula;
    }

    public override bool Evaluate(Model model)
    {
        foreach (State s in model.States)
        {
            if (model.Relation(s, this.agent).All(x => this.formula.Evaluate(x)))
            {
                if (!this.formula.Evaluate(s))
                {
                    return false;
                }
            }
        }

        return true;
    }
}

public class Diamond : Formula
{
    private string agent;
    private Formula formula;

    public Diamond(string agent, Formula formula)
    {
        this.agent = agent;
        this.formula = formula;
    }

    public override bool Evaluate(Model model)
    {
        foreach (State s in model.States)
        {
            if (model.Relation(s, this.agent).Any(x => this.formula.Evaluate(x)))
            {
                if (this.formula.Evaluate(s))
                {
                    return true;
                }
            }
        }

        return false;
    }
}

public class Model
{
    public List<State> States { get; set; }
    public List<string> Agents { get; set; }
    public Func<State, string, List<State>> Relation { get; set; }
    public State CurrentState { get; set; }

    public bool Check(string name)
    {
        return this.CurrentState.Atoms.Contains(name);
    }
}

public class State
{
    public List<string> Atoms { get; set; }

    public State()
    {
        this.Atoms = new List<string>();
    }
}
2834 chars
148 lines

In this system, Atom represents a primitive proposition, Not, And, and Or represent negation, conjunction, and disjunction, respectively. Box and Diamond represent the modal operators □ and ◊. Finally, Model represents a Kripke model, and State represents a possible world in the model.

To use this system, we need to create a Kripke model, specify the current state of the model, and evaluate a given formula:

main.cs
Model model = new Model();

State s0 = new State() // the initial state of the model
{
    Atoms = new List<string>
    {
        "a",
        "b"
    }
};

State s1 = new State() // a state reachable from s0 via the "r" agent
{
    Atoms = new List<string>
    {
        "a",
        "c"
    }
};

model.States = new List<State>
{
    s0,
    s1
};

model.Agents = new List<string>
{
    "r"
};

model.Relation = (state, agent) =>
{
    if (agent == "r" && state == s0)
    {
        return new List<State> { s1 };
    }
    else
    {
        return new List<State>();
    }
};

model.CurrentState = s0;

Formula formula = new Box("r", new Atom("c")); // □r(c)

bool result = formula.Evaluate(model); // true

Console.WriteLine(result);
739 chars
51 lines

This will evaluate the formula □r(c) in the Kripke model, starting from the initial state s0, and return true if the formula holds, and false otherwise.

gistlibby LogSnag