使用linq查询的自定义类型的F#Linq扩展方法

问题描述 投票:0回答:1

在C#中,我可以通过实现SelectSelectMany的扩展方法,在Linq查询中为自定义类型启用单字合成,例如:

public static Either<L, R2> Select<L, R, R2>(this Either<L, R> @this, Func<R, R2> fn) => @this.Map(fn);
public static Either<L, R2> SelectMany<L, R, R2>(this Either<L, R> @this, Func<R, Either<L, R2>> fn) => @this.FlatMap(fn);
public static Either<L, R2> SelectMany<L, R, R1, R2>(this Either<L, R> @this, Func<R, Either<L, R1>> fn, Func<R, R1, R2> select) => @this.FlatMap(a => fn(a).FlatMap(b => select(a, b).ToEither<L, R2>()));

第三个扩展方法是使Linq查询中的单子式合成与Haskell中的liftM函数类似,例如:

Either<L, C> LiftM2(Either<L, A> m1, Either<L, B> m2, Func<A, B, C> f) {
  return from a in m1
         from b in m2
         select Right(f(a, b));
}

但是,我的问题与通过为自定义Either类型实现扩展方法以在Linq查询中启用单子组合而在F#中实现结果有关。

这是我对Either类型的定义:

type Either<'l, 'r> =
  | Left of 'l
  | Right of 'r 

[我首先为mapflatmap添加了一个函数,其中包括map作为<!>flatmap作为>>==<<的自定义运算符:

[<AutoOpen>]
module Either = 
  let lmap f e =
    match e with
    | Left(l) -> Left(f(l))
    | Right(r) -> Right(r)

  let rmap f e =
    match e with
    | Left(l) -> Left(l)
    | Right(r) -> Right(f(r))

  let map f e = rmap f e
  let inline (<!>) e f = map f e
  let inline (<!) a e = map >> constant

  let lflatmap f e = 
    match e with
    | Left(l) -> f(l)
    | Right(r) -> Right(r)

  let rflatmap f e =
    match e with
    | Left(l) -> Left(l)
    | Right(r) -> f(r)

  let flatmap f e = rflatmap f e
  let inline (>>=) f e = flatmap f e
  let inline (=<<) e f = flatmap f e
  let _return r = Right(r);
  let fail (l : string) = Left(l);

然后我添加了扩展方法实现;正如我从其他示例中获得的一样:

[<Extension>]
type EitherExtensions() =
  [<Extension>]
  static member inline Select(e: Either<'l, 'r>, f: 'r -> 's) = map f e
  static member inline SelectMany(e: Either<'l, 'r>, f: 'r -> Either<'l, 's>) = flatmap f e 
  static member inline SelectMany(e: Either<'l, 'r>, f: 'r -> Either<'l, 's>, select: 'r -> 's -> 't) = (f >>= e) =<< (fun s -> Either.Right(select(e, s)))

问题是当我尝试使用它来实现liftMliftM2,...函数时,似乎没有选择这些扩展方法;相反,它对System.Linq.IQueryable使用扩展方法,而不对Linq使用我的自定义扩展方法,例如SelectMany

  let liftM f m1 = query { 
      for a in m1 do 
      select Right(f(a)) 
    }

liftM的类型解析为:

liftM:
  f: a -> b,
  m1: System.Linq.IQueryable<'a>
-> System.Linq.IQueryable<Either<'c, 'a>>

而不是:

liftM:
  f: a -> b,
  m1: Either<'c, 'a>
-> Either<'c, 'b>

我当然可以使用任何一种模式匹配来实现liftM,例如:

  let liftM2 f m1 m2 =
    match m1, m2 with
    | Right(a), Right(b) -> Right(f(a, b));
    | Left(a), _ -> Left(a)
    | _, Left(b) -> Left(b)
    ...

...或内联单声道组成,例如:

let liftM2 f m1 m2 = m1 =<< (fun a -> m2 =<< (fun b -> Right(f(a, b))))

不过,出于方便和一点知识,我想知道如何实现与C#中的F#相同的结果

这可能吗?

linq f# extension-methods monads
1个回答
0
投票

C#查询语法是基于语法转换的,如您的示例所示,在F#中,每个monad实例都由关联的构建器类表示,该类实现所需的操作,例如seqasyncquery。您需要创建一个实现所需操作的either构建器。对于您的示例,您只需要最少的实现:

type EitherBuilder() = 
        member x.Bind(e, f) = flatmap f e
        member x.Return(value) = _return value
        member x.ReturnFrom(e) = e

let either = new EitherBuilder()

然后您可以使用它来实现liftM

let liftM f m1 = either { 
    let! a = m1 
    return (f a) 
}
© www.soinside.com 2019 - 2024. All rights reserved.