ocaml - Why is this weakly polymorphic -
i starting larn ocaml , trying practices:
# allow left x y = x;; val left : 'a -> 'b -> 'a = <fun> # allow first = list.fold_right left;; val first : '_a list -> '_a -> '_a = <fun>
why first
weakly polymorphic instead of polymorphic?
this value restriction. first
not value, it's function application.
to polymorphic version, utilize eta expansion:
# allow left x y = x;; val left : 'a -> 'b -> 'a = <fun> # allow first b = list.fold_right left b;; val first : 'a list -> 'a -> 'a = <fun>
as @ivg points out, commonly asked ocaml question.
update
here's function application that's unsafe generalize:
# allow f x = ref x;; val f : 'a -> 'a ref = <fun> # f [];; - : '_a list ref = {contents = []}
if pretend result has type 'a list ref
can create code go wrong (i tried it).
here's partial application that's unsafe generalize:
# allow g x = allow z = ref x in fun () -> z;; val g : 'a -> unit -> 'a ref = <fun> # g [];; - : unit -> '_a list ref = <fun>
if pretend result has type unit -> 'a list ref
can create code go wrong (i tried it).
ocaml
No comments:
Post a Comment