internal static Func<T, T> Variable<T>(Func<T, Func<Func<T, T>, T>> g, Func<T, T> h) => x => g(x)(y => h(y));
internal static Func<T, T> Variable<T>(Func<T, Func<Func<T, T>, T>> g, Func<T, T> h) => x => g(x)(z => h(z));
internal static Func<int, int> increment => x => x+1;
// increment(n) wird zu n+1
public static partial class FuncExtensions
{
public static Func<T, TResult2> Ring<T, TResult1, TResult2>(
this Func<TResult1, TResult2> function2, Func<T, TResult1> function1) =>
value => function2(function1(value));
}
public delegate T Unit<T>(T value);
public static partial class Functions<T>
{
public static readonly Unit<T> I = x => x;
}
let True t f = t
let False t f = f
public delegate Func<dynamic, dynamic> Boolean(object @true);
public static partial class ChurchBoolean
{
public static readonly Boolean True = @true => @false => @true;
public static readonly Boolean False = @true => @false => @false;
public static partial class ChurchBoolean
{
public static readonly Func<Boolean, Func<Boolean, Boolean>> And = a => b => (Boolean)a(b)(False);
// substituting False function
public static Func<Boolean, Func<Boolean, Boolean>>
And = a => b => a(b)(new Boolean(@true => @false => @false));
}
public static partial class BooleanExtensions
{
public static Boolean And(this Boolean a, Boolean b) => ChurchBoolean.And(a)(b);
}
public static Func<Boolean, Func<Boolean, Boolean>> Or = a => b => a(True)(b);
public static Boolean Or(this Boolean a, Boolean b) => ChurchBoolean.Or(a)(b);
public static Func<Boolean, Boolean> Not = boolean => boolean(False)(True);
public static Boolean Not(this Boolean a) => ChurchBoolean.Not(a);
public static Func<Boolean, Func<Boolean, Boolean>>
Xor = a => b => a(Not(b))(b);
public static Boolean Xor(this Boolean a, Boolean b) => ChurchBoolean.Xor(a)(b);
public static partial class ChurchEncoding
{
// System.Boolean structure to Boolean function.
public static Boolean Church(this bool boolean) => boolean ? True : False;
// Boolean function to System.Boolean structure.
public static bool Unchurch(this Boolean boolean) => boolean(true)(false);
}
public static readonly Func<Boolean, Func<dynamic, Func<dynamic, dynamic>>> EagerIf = condition => then => @else => condition (then) (@else);
public static readonly Func<Boolean, Func<Func<Unit<dynamic>, dynamic>, Func<Func<Unit<dynamic>, dynamic>, dynamic>>> If = condition => thenFactory => elseFactory => condition (thenFactory) (elseFactory)(Functions<dynamic>.Id);