C#中的Monad变形金刚

时间:2013-12-03 14:03:57

标签: c# functional-programming monads monad-transformers higher-kinded-types

我正在C#中使用monad变换器 我想知道我提供的以下代码是否表明我理解了这一点 我对此很新,所以任何反馈/意见都非常受欢迎 这个例子只是用于将一个monad包装在验证monad中。

using System;
using NUnit.Framework;

namespace Monads
{
    public static class MaybeExtensions
    {
        public static IMaybe<T> ToMaybe<T>(this T value)
        {
            if (value == null)
                return new None<T>();

            return new Just<T>(value);
        }
    }

    public interface IMaybe<T>
    {
        IMaybe<U> Select<U>(Func<T, U> f);

        IMaybe<U> SelectMany<U>(Func<T, IMaybe<U>> f);

        U Fold<U>(Func<U> error, Func<T, U> success);
    }

    public class Just<T> : IMaybe<T>
    {
        public Just(T value)
        {
            this.value = value;

        }

        public IMaybe<U> Select<U>(Func<T, U> f)
        {
            return f(value).ToMaybe();
        }

        public IMaybe<U> SelectMany<U>(Func<T, IMaybe<U>> f)
        {
            return f(value);
        }

        public U Fold<U>(Func<U> error, Func<T, U> success)
        {
            return success(value);
        }

        public IValidation<U, T> ToValidationT<U>()
        {
            return new ValidationMaybeT<U, T>(this, default(U));
        }

        private readonly T value;
    }

    public class None<T> : IMaybe<T>
    {
        public IMaybe<U> Select<U>(Func<T, U> f)
        {
            return new None<U>();
        }

        public IMaybe<U> SelectMany<U>(Func<T, IMaybe<U>> f)
        {
            return new None<U>();
        }

        public U Fold<U>(Func<U> error, Func<T, U> success)
        {
            return error();
        }

        public IValidation<U, T> ToValidationT<U>(U exceptionalValue)
        {
            return new ValidationMaybeT<U, T>(this, exceptionalValue);
        }
    }

    public class Customer
    {
        public Customer(string name)
        {
            Name = name;
        }

        public string Name { get; set; }
    }

    public interface IValidation<T, U>
    {
        IValidation<T, V> Select<V>(Func<U, V> f);

        IValidation<T, V> SelectMany<V>(Func<U, IValidation<T, V>> f);
    }

    public class ValidationError<T, U> : IValidation<T, U>
    {
        public ValidationError(T error)
        {
            Error = error;
        }

        public IValidation<T, V> Select<V>(Func<U, V> f)
        {
            return new ValidationError<T, V>(Error);
        }

        public IValidation<T, V> SelectMany<V>(Func<U, IValidation<T, V>> f)
        {
            return new ValidationError<T, V>(Error);
        }

        public T Error { get; private set; }
    }

    public class ValidationSuccess<T, U> : IValidation<T, U>
    {
        public ValidationSuccess(U value)
        {
            Result = value;
        }

        public IValidation<T, V> Select<V>(Func<U, V> f)
        {
            return new ValidationSuccess<T, V>(f(Result));
        }

        public IValidation<T, V> SelectMany<V>(Func<U, IValidation<T, V>> f)
        {
            return f(Result);
        }

        public U Result { get; private set; }
    }

    public class ValidationMaybeT<T, U> : IValidation<T, U>
    {
        public ValidationMaybeT(IMaybe<U> value, T error)
        {
            Value = value;
            Error = error;
        }

        public IValidation<T, V> Select<V>(Func<U, V> f)
        {
            return Value.Fold<IValidation<T, V>>(() => new ValidationError<T, V>(Error), s => new ValidationSuccess<T, V>(f(s)));
        }

        ValidationError<T, V> SelectManyError<V>()
        {
            return new ValidationError<T, V>(Error);
        }

        public IValidation<T, V> SelectMany<V>(Func<U, IValidation<T, V>> f)
        {
            return Value.Fold(() => SelectManyError<V>(), s => f(s));
        }

        public IMaybe<U> Value { get; private set; }

        public T Error { get; private set; }
    }

    public interface ICustomerRepository
    {
        IValidation<Exception, Customer> GetById(int id);
    }

    public class CustomerRepository : ICustomerRepository
    {
        public IValidation<Exception, Customer> GetById(int id)
        {

            if (id < 0)
                return new None<Customer>().ToValidationT<Exception>(new Exception("Customer Id less than zero"));

            return new Just<Customer>(new Customer("Structerre")).ToValidationT<Exception>();
        }
    }

    public interface ICustomerService
    {
        void Delete(int id);
    }

    public class CustomerService : ICustomerService
    {
        public CustomerService(ICustomerRepository customerRepository)
        {
            this.customerRepository = customerRepository;

        }

        public void Delete(int id)
        {
            customerRepository.GetById(id)
                .SelectMany(x => SendEmail(x).SelectMany(y => LogResult(y)));


        }

        public IValidation<Exception, Customer> LogResult(Customer c)
        {
            Console.WriteLine("Deleting: " + c.Name);
            return new ValidationSuccess<Exception, Customer>(c);
            //return new ValidationError<Exception, Customer>(new Exception("Unable write log"));
        }

        private IValidation<Exception, Customer> SendEmail(Customer c)
        {
            Console.WriteLine("Emailing: " + c.Name);
            return new ValidationSuccess<Exception, Customer>(c);
        }

        ICustomerRepository customerRepository;
    }

    [TestFixture]
    public class MonadTests
    {
        [Test]
        public void Testing_With_Maybe_Monad()
        {
            new CustomerService(new CustomerRepository()).Delete(-1);
        }
    }
}

另一个较小的子问题是,如果C#具有更高的kinded类型,我可以只实现此类一次(ValidationT)并且它适用于所有其他包装的monad,或者这是不正确的?

1 个答案:

答案 0 :(得分:4)

几乎是最快的答案。您的ValidationMaybeT正在存储Maybe的值,而真正的monad变换器将具有MaybeValidation monad的行为,并且可以修改默认行为如果需要,包裹的monad。

这是一种非常手动的方式,我不一定会推荐它,它很快就会非常混乱。 C#缺乏更高级别的多态性会让你抓住每个机会。

我管理的最近的(即使那时它不是一个合适的monad变换器系统)也在我的库中:Language-Ext

项目中有13个monad(Option,Map,Lst,Either,Try,Reader等),我为所有这些实现了一组标准功能:

Sum      
Count    
Bind     
Exists   
Filter   
Fold     
ForAll   
Iter     
Map      
Select
SeletMany
Where
Lift

这些函数在函数式编程中最有用,几乎可以让你做任何需要的操作。

因此,在所有monad实现这些标准函数的情况下,它们将成为更高级的类型。并不是编译器知道这一点,它们只是同一组的一部分&#39;。

然后我编写了一个T4模板来生成变换器函数作为扩展方法(它们有一个T后缀),用于“高等级”类型的monad和function的每个组合。 / p>

例如:

var list = List(Some(1),None,Some(2),None,Some(3));
var total = list.SumT();

上面的代码会产生6SumT的定义是:

int SumT(Lst<Option<int>> self) => 
    self.Map( s => s.Sum() ).Sum();
例如,

FilterT也适用于内部monad:

var list = List(Some(1),None,Some(2),None,Some(3));
list = list.FilterT(x => x > 2);

所以扩展方法路线非常好。而不是创建新类型,请使用:

IValidation<IMaybe<T>>

然后为Maybe

提供IValidation<IMaybe<T>>扩展方法

您可以执行我所做的操作并从标准集自动生成,也可以手动编写。然后,它会使您的MaybeValidation实施保持清洁,并将定制的变压器功能分开。

如果您有兴趣,这是我用来生成变换器方法的T4模板(说实话,它非常摇摇欲坠):LanguageExt.Core/HKT.tt

这是生成的代码:LanguageExt.Core/HKT.cs

在我完成上述HKT之前,我采用了与您尝试的方法类似的方法,我有一个名为TryOption<T>的monad,它是TryOption。但是对于新的HKT,我现在可以写Try<Option<T>>。最初的实施是here

无论如何,我希望有所帮助!