Loading ...
Sorry, an error occurred while loading the content.

2745Re: "ocaml_beginners"::[] Re: 'ocaml_beginners'::[] Execution semantics

Expand Messages
  • Nicolas Cannasse
    Nov 4, 2004
      > results in f not being fully polymorphic, but
      > val example1 : 'a -> 'b -> 'c -> 'a * 'b * 'c = <fun>
      > let f = fun x -> example1 1 2 x;;
      > val f : 'a -> int * int * 'a = <fun>
      >
      > Does. It seems to me that I should be able to return a (fun x- >
      > blah), and have it be as polymorphic as possible. (not should in the
      > 'right thing' sense, but should in the 'actual way it works' sense,
      > based on my very poor knowledge of ocaml types and '_a).

      The monomorphic types '_a and others are needed in order to ensure type
      safety.
      for example :

      let id x = x
      let f = ref id

      > f will be '_a -> '_a ref
      if it's type was 'a -> 'a , that means we can put any unifiable function
      into the reference f and call it later with another type :

      f := (fun x -> x + 1) // would unify since int -> int is unifiable with
      'a -> 'a
      !f "hello" // would works since !f would still be 'a -> 'a, causing a typing
      error since we're actually executing above function

      So it's needed to introduce monomorphic variables, they are really
      "temporary" polymorphic variables until they're unified. And this limitation
      is only required for values " let x = " since polymorphic functions does
      not cause any problem.

      Nicolas Cannasse
    • Show all 26 messages in this topic