OCaml 多态递归错误
OCaml polymorphic recursion errors
给定以下类型:
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)}
我从编译器中得到一个错误:
Error: This expression has type b#1 stack
but an expression was expected of type 'a stack
The type constructor b#1 would escape its scope
在这行代码中:
| 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
在没有 运行 进入模糊错误消息的情况下花了一段时间,该错误消息不可避免地通过使用一些帮助程序类型来纠正,但我似乎无法弄清楚如何使用帮手,或者如果我试图用我的类型做一些愚蠢的事情。
消除这个错误的正确方法是什么?
我认为这些功能有些不连贯。添加一些注释并删除不相关的分支给出:
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
| _ -> ()
其中 "underlined" 变量是错误消息的主题:
Error: This expression has type s but an expression was expected of type t
如果第一次通过 step
对 (OnErrorStack : unit stack)
进行操作,然后第二次通过 step
对 (AndThenStack : int stack)
进行操作,应该会发生什么?
换句话说,如果 loop
的参数类似于:
{ root = Success ();
stack = OnErrorStack ((fun () -> Success 3),
AndThenStack ((fun x -> Success (float_of_int x)),
(NoStack : float stack))) }
虽然 (value : unit)
将与第一个 step
兼容,但在我看来,没有任何东西可以保证它与第二个 step
的兼容性,后者更依赖于存在的价值在 OnErrorStack
中输入(反例中的 int
)。
需要将第二个变量添加到任务类型定义中以表达单独的成功和失败值。这是完整的解决方案:
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)}
给定以下类型:
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)}
我从编译器中得到一个错误:
Error: This expression has type b#1 stack but an expression was expected of type 'a stack The type constructor b#1 would escape its scope
在这行代码中:
| 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
在没有 运行 进入模糊错误消息的情况下花了一段时间,该错误消息不可避免地通过使用一些帮助程序类型来纠正,但我似乎无法弄清楚如何使用帮手,或者如果我试图用我的类型做一些愚蠢的事情。
消除这个错误的正确方法是什么?
我认为这些功能有些不连贯。添加一些注释并删除不相关的分支给出:
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
| _ -> ()
其中 "underlined" 变量是错误消息的主题:
Error: This expression has type s but an expression was expected of type t
如果第一次通过 step
对 (OnErrorStack : unit stack)
进行操作,然后第二次通过 step
对 (AndThenStack : int stack)
进行操作,应该会发生什么?
换句话说,如果 loop
的参数类似于:
{ root = Success ();
stack = OnErrorStack ((fun () -> Success 3),
AndThenStack ((fun x -> Success (float_of_int x)),
(NoStack : float stack))) }
虽然 (value : unit)
将与第一个 step
兼容,但在我看来,没有任何东西可以保证它与第二个 step
的兼容性,后者更依赖于存在的价值在 OnErrorStack
中输入(反例中的 int
)。
需要将第二个变量添加到任务类型定义中以表达单独的成功和失败值。这是完整的解决方案:
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)}