Exception 什么是一元错误?

Exception 什么是一元错误?,exception,error-handling,monads,Exception,Error Handling,Monads,我读过关于一元错误作为返回值错误和异常的替代方法的文章。有人能解释并举例说明一元错误在命令式伪语言中是如何工作的吗(请不要提供函数示例)。基本思想: 输出成功或错误失败的概念被编码到类型中 此类型的值(或函数)可以组成此类型的另一个值。可以组合具有不同成功类型的值,但它们必须具有相同的失败类型 一旦输出错误,其余的执行将被短路以传播错误 下面是C#中的一个工作示例: //一个被歧视的联合,它可以是一个类型为恐怖的错误, //或类型为TResult的成功结果。 //IsError指示哪个字段

我读过关于一元错误作为返回值错误和异常的替代方法的文章。有人能解释并举例说明一元错误在命令式伪语言中是如何工作的吗(请不要提供函数示例)。

基本思想:

  • 输出成功或错误失败的概念被编码到类型中
  • 此类型的值(或函数)可以组成此类型的另一个值。可以组合具有不同成功类型的值,但它们必须具有相同的失败类型
  • 一旦输出错误,其余的执行将被短路以传播错误
下面是C#中的一个工作示例:


//一个被歧视的联合,它可以是一个类型为恐怖的错误,
//或类型为TResult的成功结果。
//IsError指示哪个字段具有有效值。
班级投掷
{
公共图书馆;
公共恐怖错误;
公共结果;
//成功创建TResult类型的新reslt。
公共静态抛出成功(TResult结果)
{
抛出t=新抛出();
t、 IsError=错误;
t、 结果=结果;
返回t;
}
//生成一个新的类型为恐怖的错误。
公共静态抛出失败(恐怖错误)
{
抛出t=新抛出();
t、 IsError=真;
t、 错误=错误;
返回t;
}
//组成。
公投绑定(
函数(f)
{
如果(IsError)
{
//如果这是一个错误,那么我们可以短路评估
返回抛出。失败(错误);
}
//否则,将此结果转发到下一次计算。
返回f(结果);
}
}
课堂测试
{
//数字/演示
私有静态抛出Div(双num,双denom)
{
如果(denom==0)
返回抛出。失败(“除以零”);
return-Throws.Success(num/denom);
}
//让用户输入一个double。
私有静态抛出ReadDouble(字符串名称)
{
编写(“{0}:”,名称);
字符串输入=Console.ReadLine();
双重结果;
如果(!double.TryParse(输入,输出结果))
return Throws.Fail(string.Format(“无法解析{0}”,name));
返回投掷。成功(结果);
}
//读两个双精度数并将其除以得出结果。
私有静态抛出交互()
{
返回ReadDouble(“分子”).Bind(num=>
ReadDouble(“分母”).Bind(分母=>
Div(num,denom));
}
公共静态void TestLoop()
{
while(true)
{
//运行一个计算,要求用户输入两个数字,
//将它们分开,然后打印出结果。
抛出t=交互();
//注意返回类型是如何强制您处理
//如果要获取该值,则出错。
if(t.IsError)
{
WriteLine(“错误:{0}”,t.Error);
}
其他的
{
WriteLine(“成功:{0}”,t.Result);
}
}
}
}

“可以组合具有不同成功类型的值,但它们都必须具有相同的失败类型。”这听起来不对——那么一元类型是什么?我想说的是,抛出可以与抛出组合,但抛出和抛出不能。
// A discrimated union that can be either an error of type TError, 
// or a successful result of type TResult. 
// IsError indicates which field has a valid value.
class Throws<TError, TResult>
{
    public bool IsError;
    public TError Error;
    public TResult Result;

    // Make a new successful reslt of type TResult.
    public static Throws<TError, TResult> Success(TResult result)
    {
        Throws<TError, TResult> t = new Throws<TError, TResult>();
        t.IsError = false;
        t.Result = result;
        return t;
    }

    // Make a new error of type TError.
    public static Throws<TError, TResult> Fail(TError error)
    {
        Throws<TError, TResult> t = new Throws<TError, TResult>();
        t.IsError = true;
        t.Error = error;
        return t;
    }

    // Composition.
    public Throws<TError, TResultB> Bind<TResultB>(
              Func<TResult, Throws<TError, TResultB>> f)
    {
        if (IsError)
        {
            // If this is an error, then we can short circuit the evaluation
            return Throws<TError, TResultB>.Fail(Error);
        }

        // Otherwise, forward this result to the next computation.
        return f(Result);
    }
}

class Test
{
    // num / demom
    private static Throws<string, double> Div(double num, double denom)
    {
        if (denom == 0)
            return Throws<string, double>.Fail("divide by zero");

        return Throws<string, double>.Success(num / denom);
    }

    // Have the user enter a double.
    private static Throws<string, double> ReadDouble(string name)
    {
        Console.Write("{0}: ", name);

        string input = Console.ReadLine();
        double result;
        if (!double.TryParse(input, out result))
            return Throws<string, double>.Fail(string.Format("can't parse {0}", name));

        return Throws<string, double>.Success(result);
    }

    // Read two doubles and divide them to produce the result.
    private static Throws<string, double> Interact()
    {
        return ReadDouble("numerator").Bind(num => 
               ReadDouble("denominator").Bind(denom => 
               Div(num, denom)));
    }

    public static void TestLoop()
    {
        while (true)
        {
            // Run a computation that asks the user for two numbers,
            // divides them and then prints out the result.
            Throws<string, double> t = Interact();

            // Notice how the return type forces you to address the
            // error if you want to get to the value.
            if (t.IsError)
            {
                Console.WriteLine("Error: {0}", t.Error);
            }
            else
            {
                Console.WriteLine("Success: {0}", t.Result);
            }
        }
    }
}