create a generic modal logical system with linq in csharp

To create a generic modal logical system with LINQ in C#, you can start with defining the basic data structures for modal logic. This would typically include classes for ModalFormulas, Worlds or States, and Transitions.

main.cs
public class ModalFormula<T> // generic ModalFormula class
{
    public string Name { get; set; }
    public Func<T, bool> Predicate { get; set; } 
} 

public class State<T> // generic State class
{
    public List<State<T>> Outgoing { get; set; }
    public List<State<T>> Incoming { get; set; }
    public T Value { get; set; }
} 

public class Transition<T> // generic Transition class
{
    public State<T> From { get; set; }
    public State<T> To { get; set; }
} 
470 chars
19 lines

Next, you can define a class for the Modal System that contains all the necessary methods for performing modal logic operations.

main.cs
public class ModalSystem<T> // generic ModalSystem class
{
    public List<ModalFormula<T>> ModalFormulas { get; set; } 
    public List<State<T>> States { get; set; } 
    public List<Transition<T>> Transitions { get; set; }

    public IEnumerable<State<T>> GetStatesSatisfyingFormula(ModalFormula<T> formula)
    {
        return States.Where(s => formula.Predicate(s.Value)); 
    }

    public bool IsSatisfiable(ModalFormula<T> formula)
    {
        return States.Any(s => formula.Predicate(s.Value)); 
    }

    public bool IsValidTransition(State<T> from, State<T> to)
    {
        return Transitions.Any(t => t.From == from && t.To == to); 
    }

    public IEnumerable<State<T>> GetPossibleNextStates(State<T> from)
    {
        return Transitions.Where(t => t.From == from).Select(t => t.To); 
    }
}
818 chars
27 lines

With these basic classes and methods in place, you can now create instances of your ModalSystem and start performing modal logic operations. For example, you can create a new System with a set of states, formulas, and transitions like this:

main.cs
var system = new ModalSystem<int>
{
    States = new List<State<int>>
    {
        new State<int> { Value = 0 },
        new State<int> { Value = 1 },
        new State<int> { Value = 2 }
    },

    Transitions = new List<Transition<int>>
    {
        new Transition<int> { From = States[0], To = States[1] },
        new Transition<int> { From = States[1], To = States[2] }
    },

    ModalFormulas = new List<ModalFormula<int>>
    {
        new ModalFormula<int> { Name = "Even", Predicate = x => x % 2 == 0 }
    }
};

// Check if formula "Even" is satisfiable in any state
var isSatisfiable = system.IsSatisfiable(system.ModalFormulas[0]); 

// Get all states that satisfy formula "Even"
var satisfyingStates = system.GetStatesSatisfyingFormula(system.ModalFormulas[0]); 

// Check if a transition from state[1] to state[2] is valid in this system
var isValidTransition = system.IsValidTransition(system.States[1], system.States[2]); 

// Get all possible next states from state[1]
var possibleNextStates = system.GetPossibleNextStates(system.States[1]); 
1065 chars
33 lines

gistlibby LogSnag