了解 F# 值限制错误 [英] Understanding F# Value Restriction Errors
问题描述
我不明白 F# 中的值限制是如何工作的.我也阅读了 wiki 中的解释作为 MSDN 文档.我不明白的是:
I don't understand how the Value Restriction in F# works. I've read the explanation in the wiki as well as the MSDN documentation. What I don't understand is:
为什么,例如,这会给我一个值限制错误(取自 this 问题):
let toleq (e:float<_>) a b = (abs ( a - b ) ) < e
但事实并非如此:
let toleq e (a:float<_>) b = (abs ( a - b ) ) < e
这是概括的好吧...
This is generalized all right...
let is_bigger a b = a < b
但这不是(它被指定为 int):
but this isn't (it is specified as int):
let add a b = a + b
为什么带有隐式参数的函数会产生值限制:
Why functions with implicit parameters generate Value Restriction:
这个:
let item_count = List.fold (fun acc _ -> 1 + acc) 0
对比:
let item_count l = List.fold (fun acc _ -> 1 + acc) 0 l
(请注意,如果我在代码片段中使用此函数,VR 错误将消失,但该函数将指定为我使用它的类型,我希望它被泛化)
(Mind you, if I do use this function in a code fragment the VR error will be gone, but then the function will be specified to the type I used it for, and I want it to be generalized)
它是如何工作的?
(我使用的是最新的 F#,v1.9.6.16)
(I'm using the latest F#, v1.9.6.16)
推荐答案
EDIT
更好/最近的信息在这里:保持部分应用的函数通用
Better/recent info is here: Keeping partially applied function generic
(以下原文)
我认为这里务实的事情不是试图太深入地理解这一点,而是要了解一些通用策略来克服 VR 并继续您的工作.这是一个有点逃避"的答案,但我不确定在这里花时间了解 F# 类型系统的内部结构(它在不同版本之间继续以微小的方式发生变化)是否有意义.
I think a pragmatic thing here is not to try to understand this too deeply, but rather to know a couple general strategies to get past the VR and move on with your work. It's a bit of a 'cop out' answer, but I'm not sure it makes sense to spend time understanding the intracacies of the F# type system (which continues to change in minor ways from release to release) here.
我提倡的两个主要策略是这些.首先,如果您使用函数类型(带有箭头->"的类型)定义一个值,那么通过执行 eta-conversion:
The two main strategies I would advocate are these. First, if you're defining a value with a function type (type with an arrow '->'), then ensure it is a syntactic function by doing eta-conversion:
// function that looks like a value, problem
let tupleList = List.map (fun x -> x,x)
// make it a syntactic function by adding argument to both sides
let tupleList l = List.map (fun x -> x,x) l
其次,如果您仍然遇到 VR/泛化问题,请指定整个类型签名以表达您想要的内容(然后在 F# 允许的情况下退避"):
Second, if you still encounter VR/generalizing problems, then specify the entire type signature to say what you want (and then 'back off' as F# allows):
// below has a problem...
let toleq (e:float<_>) a b = (abs ( a - b ) ) < e
// so be fully explicit, get it working...
let toleq<[<Measure>]'u> (e:float<'u>) (a:float<'u>) (b:float<'u>) : bool =
(abs ( a - b ) ) < e
// then can experiment with removing annotations one-by-one...
let toleq<[<Measure>]'u> e (a:float<'u>) b = (abs ( a - b ) ) < e
我认为这两种策略是最好的务实建议.也就是说,这是我尝试回答您的具体问题.
I think those two strategies are the best pragmatic advice. That said, here's my attempt to answer your specific questions.
我不知道.
I don't know.
'>' 是一个完全通用的函数 ('a -> 'a -> bool),适用于所有类型,因此 is_bigger 是通用的.另一方面,+"是一个内联"函数,它适用于少数原始类型和特定类别的其他类型;它只能在其他内联"函数中泛化,否则必须将其固定为特定类型(或将默认为int").(即席多态的内联"方法是 F# 中的数学运算符如何克服类型类"的缺失.)
'>' is a fully generic function ('a -> 'a -> bool) which works for all types, and thus is_bigger generalizes. On the other-hand, '+' is an 'inline' function which works on a handful of primitive types and a certain class of other types; it can only be generalized inside other 'inline' functions, otherwise it must be pinned down to a specific type (or will default to 'int'). (The 'inline' method of ad-hoc polymorphism is how the mathematical operators in F# overcome the lack of "type classes".)
这就是我上面讨论的句法功能"问题;'让我们编译成字段/属性,与函数不同,它们不能是通用的.因此,如果您希望它是通用的,请将其设为函数.(另见 这个问题是这条规则的另一个例外.)
This is the 'syntactic function' issue I discussed above; 'let's compile down into fields/properties which, unlike functions, cannot be generic. So if you want it to be generic, make it a function. (See also this question for another exception to this rule.)
这篇关于了解 F# 值限制错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!