What's the difference between these two functions?

Jackson Tale

I have two functions:

let rev_flatten l = 
  List.fold_left (fun acc x -> List.fold_left (fun acc y -> y::acc) acc x) [] l

The type is val rev_flatten : 'a list list -> 'a list = <fun>

and

let rev_flatten = 
  List.fold_left (fun acc x -> List.fold_left (fun acc y -> y::acc) acc x) []

The type is val rev_flatten : '_a list list -> '_a list = <fun>


I think it is the same functions, at least the same functionality, but why they have two different types? Why the second has the element type of _a? What is it?

Pavel Zaichenkov

A type variable with underscore as a prefix tells us that the variable is weakly polymorphic. A weakly polymorphic variable can be used only with one type, however a compiler can't deduce the exact type, so the type variable is mark with underscore.

When you provide an argument for the first time, a variable will no longer be polymorphic and will be able to accept arguments of a single type only.

Usually, a function is not generalized, but marked as weakly polymorphic if it might contain mutable state. In your example this is probably the case, because type system doesn't know if List.fold_left is pure or impure function.

Edit: Why avoiding partial application (eta expansion) allows function (even impure) to be polymorphic?

Let's define a function that have an internal counter that is incremented and printed out every time the function is called. Among this, it takes a function as the argument and applies it after increasing the counter:

let count f =
  let inc = ref 0 in
  (fun x -> inc := !inc + 1; print_int !inc; f x);;

This function is polymorphic: ('a -> 'b) -> 'a -> 'b.

Next, let's define two more functions. A weekly polymorphic:

let max' = count max;;
val max' : '_a -> '_a -> '_a = <fun>

and a polymorphic one:

let max'' x = count max x;;
val max'' : 'a -> 'a -> 'a = <fun>

Now notice what is printed when we execute these functions:

max' 1 2;;  (* prints 1 *)
max' 1 2;;  (* prints 2 *)
max' 1 2;;  (* prints 3 *)
max'' 1 2;; (* prints 1 *)
max'' 1 2;; (* prints 1 *)
max'' 1 2;; (* prints 1 *)

So the function that we designed as weekly polymorphic has a persistent mutable state inside that allows to use the counter as expected, while the polymorphic function is stateless and is reconstructed with every call, although we wanted to have a mutable variable inside.

This is the reason for a compiler to prefer a weakly polymorphic function that can be used with any single type instead of supporting full-fledged polymorphism.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What's the difference between these two functions

From Dev

What's the difference between these two functions in JavaScript?

From Dev

What's the difference between these two ways to declare functions?

From Dev

What is the difference between these two functions in Javascript?

From Dev

What is the difference and issues between these two clojure functions?

From Dev

What is the difference between these two recursive functions?

From Dev

What is the difference between these two recursive ocaml functions?

From Dev

What is the difference between these two ruby functions?

From Dev

What exactly is the difference between these two javascript functions?

From Dev

What is the difference between these two recursive functions?

From Dev

What is the difference and issues between these two clojure functions?

From Dev

What is the difference between these two ruby functions?

From Dev

What is the difference between scope of these two functions

From Dev

What are all the difference between these two Javascript functions?

From Dev

What's the difference between these two async methods?

From Dev

What's the difference between these two function expressions?

From Dev

What's the difference between these two lines?

From Dev

What's the difference between these two new syntaxes?

From Dev

What's the difference between these two Chronometer implementations?

From Dev

What's the difference between these two statements in Javascript?

From Dev

what's the difference between these two in C?

From Dev

What's the difference between these two pieces of code?

From Dev

What's the difference between these two implementation?

From Dev

what's the difference between these two solr queries?

From Dev

What's the difference between these two pieces of code?

From Dev

what's the difference between these two in C?

From Dev

What's the difference between these two statements in Javascript?

From Dev

What's the difference between these two dd commands?

From Dev

what's the difference between these two pointer code

Related Related

  1. 1

    What's the difference between these two functions

  2. 2

    What's the difference between these two functions in JavaScript?

  3. 3

    What's the difference between these two ways to declare functions?

  4. 4

    What is the difference between these two functions in Javascript?

  5. 5

    What is the difference and issues between these two clojure functions?

  6. 6

    What is the difference between these two recursive functions?

  7. 7

    What is the difference between these two recursive ocaml functions?

  8. 8

    What is the difference between these two ruby functions?

  9. 9

    What exactly is the difference between these two javascript functions?

  10. 10

    What is the difference between these two recursive functions?

  11. 11

    What is the difference and issues between these two clojure functions?

  12. 12

    What is the difference between these two ruby functions?

  13. 13

    What is the difference between scope of these two functions

  14. 14

    What are all the difference between these two Javascript functions?

  15. 15

    What's the difference between these two async methods?

  16. 16

    What's the difference between these two function expressions?

  17. 17

    What's the difference between these two lines?

  18. 18

    What's the difference between these two new syntaxes?

  19. 19

    What's the difference between these two Chronometer implementations?

  20. 20

    What's the difference between these two statements in Javascript?

  21. 21

    what's the difference between these two in C?

  22. 22

    What's the difference between these two pieces of code?

  23. 23

    What's the difference between these two implementation?

  24. 24

    what's the difference between these two solr queries?

  25. 25

    What's the difference between these two pieces of code?

  26. 26

    what's the difference between these two in C?

  27. 27

    What's the difference between these two statements in Javascript?

  28. 28

    What's the difference between these two dd commands?

  29. 29

    what's the difference between these two pointer code

HotTag

Archive