Why does OCaml modify the type of a value on first use when it contains a universal? -


this question has answer here:

why ocaml change type of value of first use when contains universal? example, if define church encoding tuples, have:

# let pair x y z = z x y;; val pair : 'a -> 'b -> ('a -> 'b -> 'c) -> 'c = <fun> # let first p = p (fun x y-> x);; val first : (('a -> 'b -> 'a) -> 'c) -> 'c = <fun> # let second p = p (fun x y -> y);; val second : (('a -> 'b -> 'b) -> 'c) -> 'c = <fun> # let foo = pair 1.2 "bob";; val foo : (float -> string -> '_a) -> '_a = <fun> # first foo;; - : float = 1.2 # foo;; - : (float -> string -> float) -> float = <fun> # second foo;; error: expression has type (float -> string -> float) -> float        expression expected of type          (float -> string -> string) -> 'a        type float not compatible type string  # let foo = pair 1.2 "bob";; val foo : (float -> string -> '_a) -> '_a = <fun> # second foo;; - : string = "bob" # foo;; - : (float -> string -> string) -> string = <fun> # first foo;; error: expression has type (float -> string -> string) -> string        expression expected of type          (float -> string -> float) -> 'a        type string not compatible type float  

basically, foo has type val foo : (float -> string -> '_a) -> '_a = <fun>, changes first time project out either first or second element. why happen?

this called weak polymorphic type. lots of questions asked , answered this. feel free use search facilities or read ocaml faq. in short, due value restriction, comes play when have partial application or mutable values. in former case can strengthen type called eta-expression (in layman terms, substituting partial application normal function invocation). in latter case, nothing can made.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -