6

我环顾四周,努力寻找答案;我确信有一个明显的答案,但我似乎无法找到它;或者我遇到了与计算表达式一起使用时无法通过的引用限制。

基本上,我想使用计算 F# 工作流来使用如下定义的引用 lambda。尝试将这些工作流程组合在一起时会出现问题。理想情况下,我想使用 let 组合 Workflow<'Env, 'Result> 实例!句法。我有点幼稚的尝试如下:

type Workflow<'Env, 'Result> = Expr<'Env -> 'Result>
type WorkflowSource<'Env, 'Result> = 'Env -> 'Result

type WorkflowBuilder() = 
    member x.Bind
        (workflow: WorkflowSource<'Env, 'OldResult>,
         selector: 'OldResult -> WorkflowSource<'Env, 'NewResult>) : WorkflowSource<'Env, 'NewResult> =
         (fun env -> (selector (workflow env) env))
    member x.Bind
        (workflow: Workflow<'Env, 'OldResult>,
         selector: 'OldResult -> WorkflowSource<'Env, 'NewResult>) 
         : Workflow<'Env, 'NewResult> =
         <@ (fun env -> (selector ((%workflow) env) env)) @>
    // This bind is where the trouble is
    member x.Bind
        (workflow: WorkflowSource<'Env, 'OldResult>,
         selector: 'OldResult -> Workflow<'Env, 'NewResult>) 
         : Workflow<'Env, 'NewResult> =
         <@ fun env -> 
                let newResultWorkflow = %(selector (workflow env))
                newResultWorkflow env @>
    member __.Return(x) = fun env -> x
    member __.ReturnFrom(x : WorkflowSource<_, _>) = x
    member __.Quote(x : Expr<WorkflowSource<_, _>>) : Workflow<_, _> = x

let workflow = new WorkflowBuilder()

第三个绑定成员给了我编译器错误:“变量“env”被绑定在引号中,但在切片表达式中使用”这有点道理。问题是我如何绕过它。我已将上述定义为尝试使以下简单案例起作用的尝试。

let getNumber (env: EnvironmentContext) = (new Random()).Next()

let workflow1 = workflow {
    let! randomNumber = getNumber
    let customValue = randomNumber * 10
    return (globalId * customValue)
}

// From expression to non expression bind case
let workflow2a = workflow {
    let! workflow1 = workflow1
    let! randomNumber = getNumber
    return (randomNumber + workflow1)
}

// From non-expression to expression bind case
let workflow2 = workflow {
    let! randomNumber = getNumber
    let! workflow1 = workflow1
    return (randomNumber + workflow1)
}

只是想知道我想要实现的目标是可能的还是我做错了什么?在最终引用的表达式中捕获用户函数时,是否可以使上述简单案例起作用?

编辑:考虑到 Tomas 的回答,我也尝试过不使用 WorkflowSource 类型。没有运气仍然有错误:System.InvalidOperationException:Microsoft.FSharp.Core.ExtraTopLevelOperators.SpliceExpression[T](FSharpExpr`1 表达式) 不允许第一类使用“%”或“%%”

type WorkflowBuilder() = 
    member x.Bind
        (workflow: Workflow<'Env, 'OldResult>,
         selector: 'OldResult -> Workflow<'Env, 'NewResult>) 
         : Workflow<'Env, 'NewResult> =
         fun env -> <@ %(selector (%(workflow env)) env) @>
    member __.Return(x) = fun Env -> <@ x @>
    member __.ReturnFrom(x: Workflow<_, _>) = x
    member __.Quote(expr: Expr<Workflow<'Env, 'Result>>) = expr
    // This run method fails
    member __.Run(x : Expr<Workflow<'Env, 'Result>>) : Workflow<'Env, 'Result> = fun (env: Expr<'Env>) -> <@ %((%x) env) @>

let workflow = new WorkflowBuilder()

// Env of type int for testing
let getRandomNumber (kernel: Expr<int>) = <@ (new Random()).Next() @> 

let workflow1 = workflow {
    let! randomNumber = getRandomNumber
    let otherValue = 2
    let! randomNumber2 = getRandomNumber
    return randomNumber + otherValue + randomNumber2
}
// This fails due to quotation slicing issue
workflow1 <@ 0 @>
4

1 回答 1

2

这只是一个想法的粗略草图,但我认为如果您不将工作流表示为带引号的函数,而是将其表示为采用带引号的环境并返回带引号的结果的函数,您可以更进一步:

type Workflow<'Env, 'Result> = Expr<'Env> -> Expr<'Result>

然后你当然可以实现所有的绑定:

member x.Bind
    (workflow: WorkflowSource<'Env, 'OldResult>,
     selector: 'OldResult -> WorkflowSource<'Env, 'NewResult>) : WorkflowSource<'Env, 'NewResult> =
     (fun env -> (selector (workflow env) env))
member x.Bind
    (workflow: Workflow<'Env, 'OldResult>,
     selector: 'OldResult -> WorkflowSource<'Env, 'NewResult>) 
     : Workflow<'Env, 'NewResult> =
     fun env -> <@ selector %(workflow env) %env @>

// This bind is where the trouble is
member x.Bind
    (workflow: WorkflowSource<'Env, 'OldResult>,
     selector: 'OldResult -> Workflow<'Env, 'NewResult>) 
     : Workflow<'Env, 'NewResult> =
     fun env -> <@ %(selector (workflow %env) env) @>

也就是说,我认为这并不是您所需要的全部 - 编译器似乎忽略了 中的代码Quote,所以即使我们添加变成 的引号WorkflowSourceWorkflow您仍然会因为存在Expr<WorkflowSource<_>>值而出现错误 - 但我认为 bind 的另一个重载可能解决那个。

member __.Quote(x : Expr<WorkflowSource<_, _>>) : Workflow<_, _> = 
  fun env -> <@ (%x) %env @>
于 2015-09-15T12:49:24.247 回答