0

给定以下类型:

type _ task =
| Success : 'a -> 'a task
| Fail : 'a -> 'a task
| Binding : (('a task -> unit) -> unit) -> 'a task
| AndThen : ('a -> 'b task) * 'a task -> 'b task
| OnError : ('a -> 'b task) * 'a task -> 'b task

type _ stack =
| NoStack : 'a stack
| AndThenStack : ('a -> 'b task) * 'b stack -> 'a stack
| OnErrorStack : ('a -> 'b task) * 'b stack -> 'a stack

type 'a process = 
{ root: 'a task 
; stack: 'a stack 
}

let rec loop : 'a. 'a process -> unit = fun proc ->
match proc.root with
| Success value -> 
    let rec step = function
    | NoStack -> ()
    | AndThenStack (callback, rest) -> loop {proc with root = callback value; stack = rest }
    | OnErrorStack (_callback, rest) -> step rest  <-- ERROR HERE
    in
    step proc.stack
| Fail value -> 
    let rec step = function
    | NoStack -> ()
    | AndThenStack (_callback, rest) -> step rest
    | OnErrorStack (callback, rest) -> loop {proc with root = callback value; stack = rest }
    in
    step proc.stack
| Binding callback -> callback (fun task -> loop {proc with root = task} )
| AndThen (callback, task) -> loop {root = task; stack = AndThenStack (callback, proc.stack)}
| OnError (callback, task) -> loop {root = task; stack = OnErrorStack (callback, proc.stack)}

我从编译器得到一个错误:

错误:此表达式具有类型 b#1 堆栈,但预期的表达式类型为 'a 堆栈类型构造函数 b#1 将逃脱其范围

在这行代码中:

| Success value -> 
    let rec step = function
    | NoStack -> ()
    | AndThenStack (callback, rest) -> loop {proc with root = callback value; stack = rest }
    | OnErrorStack (_callback, rest) -> step rest  <-- ERROR HERE
    in
    step proc.stack

需要一段时间才能做到这一点,而不会遇到通过使用一些帮助类型不可避免地纠正的晦涩错误消息,但我似乎无法弄清楚如何使用助手纠正这个问题,或者我是否正在尝试对我的类型做一些愚蠢的事情。

消除此错误的正确方法是什么?

4

2 回答 2

0

我认为这些功能有些不连贯。添加一些注释并删除不相关的分支给出:

let rec loop (type s) (proc : s process) =
  match proc.root with
  | Success value -> 
      let rec step (type t) (x : t stack) =
        match x with
        | NoStack -> ()
        | AndThenStack (callback, rest) ->
            loop {proc with root = callback value; stack = rest }
                                          (*^^^^^*)
        | OnErrorStack (callback, rest) -> step rest
      in
      step proc.stack
  | _ -> ()

其中“带下划线”的变量是错误消息的主题:

错误:此表达式的类型为 s,但预期的表达式为 t 类型

如果第一次通过step对 an进行操作(OnErrorStack : unit stack),然后第二次通过step对 an进行操作,会发生(AndThenStack : int stack)什么?

换句话说,如果 to 的参数loop类似于:

{ root = Success ();
  stack = OnErrorStack ((fun () -> Success 3),
                        AndThenStack ((fun x -> Success (float_of_int x)),
                                      (NoStack : float stack))) }

虽然(value : unit)将与 first 兼容step,但在我看来,没有什么能保证它与 second 的兼容性step,而是作用于OnErrorStack(int在反例中) 中存在类型的值。

于 2018-05-29T18:10:02.007 回答
0

需要将第二个变量添加到任务类型定义中以表示单独的成功和失败值。这是完整的解决方案:

type (_,_) task =
| Success : 'a -> ('a,_) task
| Fail : 'x -> (_,'x) task
| Binding : ((('a,'x) task -> unit) -> unit) -> ('a,'x) task
| AndThen : ('a -> ('b,'x) task) * ('a,'x) task -> ('b,'x) task
| OnError : ('x -> ('a,'y) task) * ('a,'x) task -> ('a,'y) task

type (_,_) stack =
| NoStack : (_,_) stack
| AndThenStack : ('a -> ('b,'x) task) * ('b,'x) stack -> ('a,'x) stack
| OnErrorStack : ('x -> ('a,'y) task) * ('a,'y) stack -> ('a,'x) stack

type ('a,'x) process = 
{ root: ('a,'x) task 
; stack: ('a,'x) stack 
}

let rec loop : type a x. (a, x) process -> unit = fun proc ->
match proc.root with
| Success value -> 
    let rec step : 'x. (a, 'x) stack -> unit = function
    | NoStack -> ()
    | AndThenStack (callback, rest) -> loop {root = callback value; stack = rest }
    | OnErrorStack (_callback, rest) -> step rest
    in
    step proc.stack
| Fail value -> 
    let rec step : 'a. ('a, x) stack -> unit = function
    | NoStack -> ()
    | AndThenStack (_callback, rest) -> step rest
    | OnErrorStack (callback, rest) -> loop {root = callback value; stack = rest }
    in
    step proc.stack
| Binding callback -> callback (fun task -> loop {proc with root = task})
| AndThen (callback, task) -> loop {root = task; stack = AndThenStack (callback, proc.stack)}
| OnError (callback, task) -> loop {root = task; stack = OnErrorStack (callback, proc.stack)}
于 2018-05-29T18:38:47.830 回答