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
172
votes
1 answer

Sneaking lenses and CPS past the value restriction

I'm encoding a form of van Laarhoven lenses in OCaml, but am having difficulty due to the value restriction. The relevant code is as follows: module Optic : sig type (-'s, +'t, +'a, -'b) t val lens : ('s -> 'a) -> ('s -> 'b -> 't) -> ('s, 't,…
J. Abrahamson
  • 72,246
  • 9
  • 135
  • 180
22
votes
3 answers

Understanding F# Value Restriction Errors

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: Why, for example, this gives me a Value Restriction error (Taken from this…
Dave Berk
  • 1,591
  • 2
  • 15
  • 17
7
votes
1 answer

F# value restriction in empty list

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)…
user1090614
  • 2,575
  • 6
  • 22
  • 27
7
votes
3 answers

Keeping partially applied function generic

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"…
Daniel
  • 47,404
  • 11
  • 101
  • 179
7
votes
3 answers

Weak Polymorphism in OCaml

I am a little confused about weak polymorphism in OCaml. Please see the following snippet, where I define a function remember: let remember x = let cache = ref None in match !cache with | Some y -> y | None -> cache := Some x;…
7
votes
2 answers

OCaml unexpected type mismatch in tuples

I'm trying to write a function, that takes an integer and a triplet and returns an element of the triplet at the given position (exercise 5.3 from Hickey's book). Triplet should be able to contain elements of different types. I thought, that if I…
5
votes
1 answer

F# value restriction

I have read all treads about value restriction in F#, but I still not understand it. I have the following code: type tree<'a> = | Nil | Node of (tree<'a> * 'a * tree<'a>) let rec flatten = function | Nil -> [] | Node ( Nil, b, Nil…
877
  • 187
  • 1
  • 7
5
votes
1 answer

F# Split Function

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…
David Tamrazov
  • 567
  • 1
  • 5
  • 16
5
votes
2 answers

Value polymorphism and "generating an exception"

Per The Definition of Standard ML (Revised): The idea is that dynamic evaluation of a non-expansive expression will neither generate an exception nor extend the domain of the memory, while the evaluation of an expansive expression might. [§4.7,…
ruakh
  • 175,680
  • 26
  • 273
  • 307
4
votes
2 answers

How do I declare a generic parameter in F#?

Given the following code: let DisplayImpl logger data = data |> Seq.iter logger printfn "" let Working = DisplayImpl (printfn "%O") [1;2;3] DisplayImpl (printfn "%O") ["a";"b";"c"] let NotWorking display = display (printfn…
Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
4
votes
1 answer

How do I avoid the Value Restriction error when the argument is an empty list?

Some functions in the List module fail when the argument is an empty list. List.rev is an example. The problem is the dreaded Value Restriction. I met the same problem while trying to define a function that returns a list with all but the last…
Soldalma
  • 4,636
  • 3
  • 25
  • 38
4
votes
1 answer

F# value restriction for seq but not list?

Value restriction error: let myFn (s : string) (args : obj seq) = () let myOtherFn = myFn "" No value restriction error: let myFn (s : string) (args : obj list) = () let myOtherFn = myFn "" Why?
MiloDC
  • 2,373
  • 1
  • 16
  • 25
4
votes
3 answers

ocaml formatters and value restriction

EDIT: I'm sorry everyone, I thought my small examle was complete, turns out it's not. I made a new one that really should be! As soon as I use a formatter as parameter to Scanf or Printf functions the formatter type gets bound to a in- or…
Bladt
  • 303
  • 3
  • 11
3
votes
3 answers

Value restriction when there are no generic parameters

I get the value restriction error on let makeElem in the following code: let elemCreator (doc: XmlDocument) = fun name (value: obj) -> let elem = doc.CreateElement(name) match value with | :? seq<#XmlNode> as childs -> …
Dmitrii Lobanov
  • 4,897
  • 1
  • 33
  • 50
3
votes
2 answers

Getting rid of value restriction errors

in OCaml Objective Caml version 3.11.0 # let rec last l= match l with [] -> failwith("Empty list") |a::[] -> a |a::r -> last r;; val last : 'a list -> 'a = # last [];; Exception: Failure "Empty list". In F# >let rec last l =…
Johan Buret
  • 2,614
  • 24
  • 32
1
2 3