簡體   English   中英

什么是monadic錯誤?

[英]What are monadic errors?

我讀到monadic錯誤是返回值錯誤和異常的替代方法。 有人可以解釋並舉例說明monadic錯誤如何在命令式偽語言中起作用(請不要使用功能示例)。

基本理念:

  • 輸出成功或失敗失敗的概念被編碼為一種類型。
  • 此類型的值(或函數)可以組成此類型的另一個值。 可以組合具有不同成功類型的值,但它們必須具有相同的失敗類型。
  • 一旦輸出錯誤,執行的其余部分就會短路以傳播錯誤。

這是C#中的一個工作示例:


// 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);
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM