了解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:
-
为什么,例如,这给了我一个值限制错误(取自但是没有:
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)
推荐答案
编辑
更好/最近的信息在这里:保持部分应用的函数为通用
Better/recent info is here: Keeping partially applied function generic
(以下为原始内容)
我认为,在这里务实的事情不是试图太深入地了解这一点,而是要了解一些超越VR并继续进行工作的一般策略.这是个解决问题"的答案,但是我不确定花时间了解F#类型系统的内在含义是有意义的(F2类型系统在各个发行版之间会不断发生细微变化).
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.
我要提倡的两个主要策略是这些.首先,如果要使用函数类型(带有箭头->"的类型)定义值,请通过执行
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".)
这是我上面讨论的语法功能"问题; 'let'编译成字段/属性,与函数不同的是,这些字段/属性不能通用.因此,如果您希望它是通用的,则使其成为一个函数. (另请参见
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屋!
-