【问题标题】:How to define Yield and For for custom computation operation in F#如何在 F# 中为自定义计算操作定义 Yield 和 For
【发布时间】:2018-10-18 16:57:47
【问题描述】:

我正在为我的应用程序开发一些 DSL,这是我定义计算类型和构建器的方式:

// expression type
type Action<'a,'b> = Action of ('a -> Async<'b>)

let runAction (Action r) ctx = r ctx
let returnF a = Action (fun _ -> async {return a})
let bind m f = Action (fun r -> async {
    let! a = runAction m r in return! runAction (f a) r
    })

let bindA ac f = Action (fun r -> async {
    let! a = ac in return! runAction (f a) r
    })

type ActionBuilder<'x>() =
  member this.Return(c) = returnF c
  member this.Zero()    = returnF ()
  member this.Delay(f)  = bind (returnF ()) f

  // binds both monadic and for async computations
  member this.Bind(m, f) = bind m f
  member this.Bind(m, f) = bindA m f

  member this.Combine(r1, r2) = bind r1 (fun () -> r2)
  member this.For(s:seq<_>, f)  = Action (fun x -> async {
    for i in s do runAction (f i) x |> ignore
    })

  // here's the attempt to implement 'need' operations

  [<CustomOperation("need")>]
  member this.Need(Action a,  targets: string list) =
    Action (fun x ->
      let r = a x
      printfn "need(%A, [%A])" a targets
      r)

   member this.For(a, f)  = bindA a f
   member this.Yield(()) =
    returnF ()

let action = ActionBuilder<string>()

/////////////////////////////////////////////////////////////
// other functions for Action

/// Gets action context
let getCtx = Action (fun ctx -> async {return ctx})


let needFn res = action {
    let! ctx = getCtx
    printfn "need([%A]) in %A" res ctx
  }

生成的代码应该是:

let program1 = fun filename -> action {
  let! a = async {return 123}
  let f = a+1

  // need ["def"; "dd"]
  do! needFn ["def"; "dd"]
  printfn "after need"

  for i in [0..10] do
    do! Async.Sleep (1)
    printfn "i: %A" i

  let! d = async {return f}
  let! ctx = getCtx
  printfn "ctx: %A, %A" ctx f
}

Async.RunSynchronously(runAction (program1 "m.c") "abc")

现在我想通过定义“need”自定义操作来将do! needFn ["def"; "dd"] 语法更改为更好的语法,但会收到编译器的各种抱怨。这是正确的方法还是我误用了计算表达式?

另一个问题是 for 不起作用!在循环体内使用。

【问题讨论】:

    标签: f# computation-expression


    【解决方案1】:

    阅读论文后,通过反复试验的方法,我得出了以下for实现(不需要Yieldbuilder方法):

        let forF (e: seq<_>) prog =
        usingF (e.GetEnumerator()) (fun e ->
            whileF
                (fun () -> e.MoveNext())
                ((fun () -> prog e.Current) |> delayF)
        )
    

    计算表达式构建器的完整源代码可以在the target project 中找到。整个项目是 Fake 构建系统的变体。

    注意:操作已重命名为配方。 need 运算符根本无法实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多