value-restriction

Does Scala have a value restriction like ML, if not then why?

半世苍凉 提交于 2020-01-14 03:35:16
问题 Here’s my thoughts on the question. Can anyone confirm, deny, or elaborate? I wrote: Scala doesn’t unify covariant List[A] with a GLB ⊤ assigned to List[Int] , bcz afaics in subtyping “biunification” the direction of assignment matters. Thus None must have type Option[⊥] (i.e. Option[Nothing] ), ditto Nil type List[Nothing] which can’t accept assignment from an Option[Int] or List[Int] respectively. So the value restriction problem originates from directionless unification and global

F# Split Function

两盒软妹~` 提交于 2019-12-23 10:14:24
问题 I'm building a merge sort function and my split method is giving me a value restriction error. I'm using 2 accumulating parameters, the 2 lists resulting from the split, that I package into a tuple in the end for the return. However I'm getting a value restriction error and I can't figure out what the problem is. Does anyone have any ideas? let split lst = let a = [] let b = [] let ctr = 0 let rec helper (lst,l1,l2,ctr) = match lst with | [] -> [] | x::xs -> if ctr%2 = 0 then helper(xs, x::l1

F# compiler error FS0030, problems with the Value Restriction

雨燕双飞 提交于 2019-12-19 09:37:58
问题 I've read the blurb at StrangeLights, I've read the passage from Expert F# (page 119), but I can't see how they apply to my code: For my tests, I want to check equality between floats, with a bit of tolerance. I'm converting everything to units of measure, but I want to be able to be 'generic': let toleq (e:float<_>) a b = (abs ( a - b ) ) < e I can then use this to check equality on different 'types' of float, or curry it to make a custom one: toleqm = toleq 1.0e-10<m> But I get the

F#: arithmetic operator and loss of polymorphism (value restriction?)

与世无争的帅哥 提交于 2019-12-11 20:42:23
问题 This code doesn't compile: let f = fun x y -> x <<< y // bit shift let g = fun x y -> x <<< y [<EntryPoint>] let main _ = printfn "%d" <| f 1 10 printfn "%d" <| f 1L 10 // error printfn "%d" <| g 1L 10 0 (7,21): error FS0001: This expression was expected to have type int but here has type int64 http://ideone.com/qktsOb I guess the unifier fixed the type parameters associated with f and g upon seeing their first occurrences. What governs this process? I think this is very similar to "value

Why does value restriction happen with MergeSort function?

落爺英雄遲暮 提交于 2019-12-11 09:32:07
问题 I have a very simple MergeSort implementation on List. /// Divide the list into (almost) equal halves let rec split = function | [] -> [], [] | [x] -> [x], [] | x1::x2::xs -> let xs1, xs2 = split xs x1::xs1, x2::xs2 /// Merge two sorted lists let rec merge xs ys = match xs, ys with | [], _ -> ys | _, [] -> xs | x::xs', y::ys' when x <= y -> x::merge xs' ys | _, y::ys' -> y::merge xs ys' let rec mergeSort = function | [] -> [] | xs -> let xs1, xs2 = split xs merge (mergeSort xs1) (mergeSort

Why `id id` is not a value in OCaml?

南楼画角 提交于 2019-12-11 02:55:14
问题 I am still trying to understand the value restriction in OCaml and I was reading through Wright's paper. And in it states (fun x -> x) (fun y -> y) is not a syntactic value while it is also stating lambda expression should be a value. I am a bit confused here, isn't id id in its essence also a lambda expression? What really counts as a syntactic value in OCaml? I also tried it in utop and found these: utop # let x = let x = (fun y -> y) (fun z -> z) in x ;; val x : '_a -> '_a = <fun> Here id

Does Scala have a value restriction like ML, if not then why?

强颜欢笑 提交于 2019-12-06 21:35:33
Here’s my thoughts on the question. Can anyone confirm, deny, or elaborate? I wrote : Scala doesn’t unify covariant List[A] with a GLB ⊤ assigned to List[Int] , bcz afaics in subtyping “biunification” the direction of assignment matters. Thus None must have type Option[⊥] (i.e. Option[Nothing] ), ditto Nil type List[Nothing] which can’t accept assignment from an Option[Int] or List[Int] respectively. So the value restriction problem originates from directionless unification and global biunification was thought to be undecidable until the recent research linked above. You may wish to view the

F# compiler error FS0030, problems with the Value Restriction

北战南征 提交于 2019-12-01 09:05:24
I've read the blurb at StrangeLights , I've read the passage from Expert F# (page 119), but I can't see how they apply to my code: For my tests, I want to check equality between floats, with a bit of tolerance. I'm converting everything to units of measure, but I want to be able to be 'generic': let toleq (e:float<_>) a b = (abs ( a - b ) ) < e I can then use this to check equality on different 'types' of float, or curry it to make a custom one: toleqm = toleq 1.0e-10<m> But I get the following message: Type inference has inferred the signature val toleq : float<'u> -> float<'u> -> float<'u> -

Keeping partially applied function generic

梦想与她 提交于 2019-11-29 10:21:27
Is it possible to partially apply a function such as bprintf and prevent it from being restricted based on its initial use? I'd like to do the following: let builder = new System.Text.StringBuilder() let append = Printf.bprintf builder append "%i" 10 append "%s" string_value you can add explicit format argument let builder = new System.Text.StringBuilder() let append format = Printf.bprintf builder format append "%i" 10 append "%s" "1" The aspect of F# that's causing this is called value restriction . You can see that if you enter just the two let declarations to F# Interactive (so that the

F# value restriction in empty list

狂风中的少年 提交于 2019-11-28 08:08:01
问题 I have a F# function: let removeEven (listToGoUnder : _ list) = let rec listRec list x = match list with | [] -> [] | head::tail when (x%2 = 0) -> head :: listRec (tail) (x+1) | head::tail -> listRec (tail) (x+1) listRec listToGoUnder 0 It removes all elements at an even index in a list. It works if I give the list some imput, like removeEven ['1';'2';'3'] I get ['1';'3'] which I am supposed to. But when I insert a empty list as parameter, I get this error: stdin(78,1): error FS0030: Value