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"]を定義して構文をより適切なものに変更したいと考えていますが、コンパイラからさまざまな苦情が寄せられています。それは正しいアプローチですか、それとも計算式を誤用していますか?

もう 1 つの問題は、 for が機能しないことです。ループ本体内で使用されます。

4

1 に答える 1