2

我正在为我的应用程序开发一些 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")

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

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

4

1 回答 1

0

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

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

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

注意:操作已重命名为配方。needoperator 根本无法实现。

于 2018-10-18T08:57:47.240 回答