Questions tagged [value-restriction]

In functional programming, in particular the ML programming language family, the value restriction means that declarations are only polymorphically generalized if they are syntactic values (also called non-expansive).

In functional programming, in particular the ML programming language family, the value restriction means that declarations are only polymorphically generalized if they are syntactic values (also called non-expansive). The value restriction prevents reference cells from holding values of different types and preserves type safety.

Some interesting notes on value restriction

http://en.wikipedia.org/wiki/Value_restriction

For F#

Automatic Generalization
Subtyping, overloading and inline functions
Defining the value restriction
Imperative programming necessitates the value restriction

41 questions
3
votes
1 answer

why does wrapping functions in iife cause weak types?

I'm trying to figure out a way to hide certain helper functions and related stuff from the user of a module and thought using an IIFE would work, yet it fails because a type variable cannot be generalized? I think I've boiled it down to the most…
hesxenon
  • 501
  • 3
  • 12
3
votes
2 answers

Set the values out of the defined set to a given value (f.e. NaN) for a column in pandas data frame

Having a defined set of valid values, all the pandas data frame column values out of it should be set to a given value, f.e. NaN. The values contained in the set and data frame can be assumed to be of numerical type. Having the following valid…
Krzysztof Słowiński
  • 6,239
  • 8
  • 44
  • 62
3
votes
1 answer

F# compiler error FS0030, problems with the Value Restriction

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…
Benjol
  • 63,995
  • 54
  • 186
  • 268
2
votes
2 answers

Yet Another Value Restriction Question

In the following code Seq.generateUnique is constrained to be of type ((Assembly -> seq) -> seq -> seq). open System open System.Collections.Generic open System.Reflection module Seq = let generateUnique = let…
Daniel
  • 47,404
  • 11
  • 101
  • 179
2
votes
1 answer

How can I curry a function with optional parameters that generates Js.t objects in ReasionML/BuckleScript?

I have the following function [@bs.obj] external route: ( ~_method: string, ~path: string, ~action: list(string) => unit, ~options: Js.t({..})=?, unit ) => _ = ""; Since functions can be partially applied, I expect to be…
fhdhsni
  • 1,529
  • 1
  • 13
  • 21
2
votes
1 answer

Unable to type polymorphic [%bs.raw function

1) Is there a way to type this? 2) Anyone able to expound on these error messages? let identity1: 'a => 'a = [%bs.raw {| function(value) { return value } |}]; /* Line 2, 11: The type of this expression, '_a -> '_a, contains type variables…
M. Walker
  • 603
  • 4
  • 14
2
votes
3 answers

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

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…
Shelby Moore III
  • 6,063
  • 1
  • 33
  • 36
2
votes
2 answers

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

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…
2
votes
1 answer

Monads and value restriction in ML

The value restriction in ML prevents type generalization in contexts where it could break type safety. The core issue seems to arise from combining sequenced mutation and polymorphic types, as for instance in this OCaml code: let x = ref [];; (*…
max
  • 1,048
  • 10
  • 20
2
votes
1 answer

Why this OCaml code does not subject to the value restriction

I don't understand that the function (my_path_mapper) doesn't subject to the value restriction. # let rec my_map ~f l = match l with [] -> [] | h::t -> f h::my_map f t;; val my_map : f:('a -> 'b) -> 'a list -> 'b list = # let…
2
votes
1 answer

Why this weakly polymorphic type?

module type M = sig type ('k, 'v) t val foo : 'k -> ('k, 'v) t end module M : M = struct type ('k, 'v) t = ('k * 'v) list let foo k = [] end In this little example, why would M.foo 123 have a weakly polymorphic type, (int, '_a) M.t)?
Alan O'Donnell
  • 1,276
  • 9
  • 17
2
votes
1 answer

define type before use

According to the MLton documentation: Standard ML requires types to be defined before they are used. [link] Not all implementations enforce this requirement (for example, SML/NJ doesn't), but the above-linked page makes a good case for why it…
ruakh
  • 175,680
  • 26
  • 273
  • 307
1
vote
1 answer

Why is a value with an explicit type annotation generalized here?

I'm getting a VR error on a let binding on module scope saying one of its parameters is a generic, but I don't know why that parameter is generic in the first place. This is the code: let private asJsonResponse (responseSource: _ Task) = fun…
Timo
  • 9,269
  • 2
  • 28
  • 58
1
vote
1 answer

Why type inference algorithm confuses because of 'Fun.flip Option.bind'?

The common signature of functions declarations in a module is when the last parameter has a type of the main state (Module.t). Like it is in the 'List' module. This form opens the ability to use '|>' operator like: [1;2;3] |> List.filter ((>)2) …
Valentyn Zakharenko
  • 2,710
  • 1
  • 21
  • 47
1
vote
1 answer

Value restriction - The value has been inferred to have generic type

Give the following definition let fn (id: int) (_:string) = id I can create a partially applied function let fnPartial = fn 1 However changing the type of _ to a non sealed type like IEnumerable let fn (id: int) (_:IEnumerable) = id Causes a…
kimsagro
  • 15,513
  • 17
  • 54
  • 69