Question

I have seen somewhere (sorry, I can't find the a reference) this operator composition:

(>>)(>>)

where (>>): (('a -> 'b) -> ('b -> 'c) -> 'a -> 'c) - (>>) is the function composition operator.

I find simpler examples are easy to understand. For example (>>)f, where f: i -> i. (>>)(i -> i) becomes (i -> 't) -> i -> 't. This is because ('a -> 'b) is curried away, 'b is inferred to be i and 't remains a generic type.

I do not fully understand (>>)(>>):

The use

What would (>>)(>>) and (<<)(<<) used for?

Why it is necessary to make the argument explicit?

> (>>)(>>);;

  (>>)(>>);;
  -^^^^^^

C:\Users\...\Temp\stdin(3,2): error FS0030: Value restriction. The value 'it' has been inferred to have generic type
    val it : (((('_a -> '_b) -> '_c -> '_b) -> '_d) -> ('_c -> '_a) -> '_d)    
Either make the arguments to 'it' explicit or, if you do not intend for it to be generic, add a type annotation.

As suggested by the error message:

> let strangeFun arg = (>>)(>>) arg;;

val strangeFun : arg:((('a -> 'b) -> 'c -> 'b) -> 'd) -> (('c -> 'a) -> 'd)
Was it helpful?

Solution

There have been several explanations of value restriction around here in the past; here is one I wrote that explains why it is necessary. For completeness I'll copy here the example code that would be incorrect if the value restriction was removed:

let f : 'a -> 'a option =
    let r = ref None
    fun x ->
        let old = !r
        r := Some x
        old

f 3           // r := Some 3; returns None : int option
f "t"         // r := Some "t"; returns Some 3 : string option!!!

As far as what (>>)(>>) would be used for, I admit I don't know. There is however a similar-looking but useful function, which is (<<) << (<<) (better known in Haskell as (.).(.)), which returns a similar composition operator whose second and returned functions can take two arguments instead of one.

let (<<<<) f = ((<<) << (<<)) f;;

// val ( <<<< ) : f:('a -> 'b) -> (('c -> 'd -> 'a) -> 'c -> 'd -> 'b)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top