0

So say we have loaded a function F that take in/out a set of args and returns a result. How to check at runtime if this F does not act on anything other than args members and functions? Meaning no Console.Writeline, Singletons (or other stuff not presented in args). Is it possible with CodeContracts library or some other solution?

Say we know that [Pure] attribute was presented in the function definition. This sucks for many cases when we have a lambda, yet at least it would be something

Why I do not see how [Pure] can help - this code compiles:

class Test {
    public struct Message {
        public string Data;
    }

    public struct Package {
        public int Size;
    }

    [Pure]
    public static List<Package> Decomposse(Message m) {
        Console.WriteLine("rrrr"); // This sould not happen
        var mtu = 1400;
        Package p = new Package{Size = mtu};
        return Enumerable.Repeat(p, m.Data.Length / mtu).ToList();
    }
}

And I want to eliminate (or at least detect that function calls stuff like Console.WriteLine("rrrr"))

DuckQueen
  • 772
  • 10
  • 62
  • 134
  • I guess you'd have to analyse the method body IL to see if it mutates anything. This would probably be easier to do with code analysis (not sure if a tool exists for this) than it would be to do it after compilation. – ProgrammingLlama Mar 13 '20 at 00:25
  • "*How to check if a function has no side-effects (is pure) at runtime?*" - With great difficulty... That's of course if its not decorated with the `[Pure]` attribute, though to prove it is quite another thing. – TheGeneral Mar 13 '20 at 00:32
  • @MichaelRandall: say we had `[Pure]` attribute on that function - how we check from the caller side? – DuckQueen Mar 13 '20 at 00:57
  • Can you load the function as an `Expression`? That would be easier to validate, then compile. But still not trivial... – Jeremy Lakeman Mar 13 '20 at 01:46
  • @JeremyLakeman: I thougth about using [expressive](https://github.com/bijington/expressive/wiki) yet they do not provide any simple ways to bring class types into expression... may be I will end up with Roslyn – DuckQueen Mar 13 '20 at 01:51
  • De-compiling is hard.... – Jeremy Lakeman Mar 13 '20 at 04:02

1 Answers1

1

It doesn't matter if the function has inputs or a result. Too many things can happen in a code body, e.g. instantiated object constructors. The problem is the modern language.

What about safe API calls which just retrieve data like DateTime.Now()? Are you going to build a list of API calls which mutate state and keep it updated for the rest of us over time, for all applications in your organization or on earth? Are you going to document what processes the compiler inlines? Then by reducing this approach to absurdity, can we accept it is not feasible?

My architecture models machines which should only change "Product" data points, but even I admit this is an unenforceable rule. I have other rules as well to try to enforce determinism. However, these modules must make API calls at some point to do the meaningful work already organized in APIs today. Otherwise we would rewrite them all.

    class Machine1Product
    {
        public Cvar<int> Y { get; set; }
    }
    class Machine1 : Producer<Machine1Product>, IMachine
    {
        public Cvar<int> X { get; set; }
        public void M()
        {
            // work which changes only product data points (Y)
        }
    }

Until a minimalist language is developed for functions, there is no observing or preventing side effects.

RBJ
  • 128
  • 6
  • Yes - I want function to be `[Pure]` in a sense that it can call its arguments members, math functions, and not to be capable to call on anything else – DuckQueen Mar 13 '20 at 15:07