序列运算符和错误术语

时间:2013-04-08 13:26:56

标签: c# lambda lambda-calculus

我试图表达序列运算符

g*f = λz.(g z = error) -> error, (f o g)z

,其中

f o g = λz.f(g z)

使用委托在C#

中构造lambda表达式
public delegate Lambda Lambda(Lambda x);

问题是我不知道如何编码错误术语。 有没有办法表达它像真假术语或教会数字?

1 个答案:

答案 0 :(得分:0)

你的意思是你想要一个类型,或者说“错误”或“结果是......”?

那只不过是Maybe - Monad,我会通过一对布尔和实际结果对其进行编码:

// you already have/know these:
Lambda lTrue = ...
Lambda lFalse = ...
Lambda not = ...
Lambda if = ...  // = Identity for usual church encoding of booleans

// "Pair" type
Lambda makePair = a => b => (f => f(a)(b));
Lambda getFst = pair => pair (a => b => a);
Lambda getSnd = pair => pair (a => b => b);

// "Error" type/monad
Lambda constError = makePair(lFalse)(null);
Lambda returnResult = result => makePair(lTrue)(result);

Lambda isError = error => not(getFst(error));
Lambda isResult = error => getFst(error);     // = getFst
Lambda getResult = error => getSnd(error);    // = getSnd

Lambda bindError = error => op => if(isResult(error))
                                      (op(getResult(error)))
                                      (constError);

现在假设您的两个方法fg不仅返回Error类型(=错误或结果) - 正式f,g :: T -> Error U - 您的序列运算符看起来像这(我假设您的参数顺序与o - 运算符)不同并非偶然:

Lambda sequence = g => f => (z => bindError(g(z))(f))