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

Questions about reference

Expand Messages
  • Chingfan Tsou
    - Hello, I m new to OCaml. - You may probably think it s a stupid question but I m still very confused about the strange behavior of reference. - According to
    Message 1 of 8 , Jun 19, 2013
      -

      Hello, I'm new to OCaml.
      -

      You may probably think it's a stupid question but I'm still very
      confused about the strange behavior of reference.
      -

      According to the documentation, reference is implemented as a
      single-field mutable record:
      1. type 'a ref = {mutable contents: 'a};;
      2. let (!) r = r.contents;;
      3. let (:=) r newval = r.contents <- newval;;
      -

      I'm not quite sure about what that means so I did some experiments:

      let u = ref 1;;
      let x = ref u;; # int ref ref = {contents = {contents = 1}}
      u := 2;;
      u;; # int ref = {contents = 2}
      x;; # int ref ref = {contents = {contents = 2}}

      -

      So here comes the question: if reference is implemented as a
      single-field mutable record, how could the assignment of u affect the
      contents of x? x has its own record, I don't know why its contents
      changed.
      -

      Could anybody help me with this? Any instructions would be appreciated.
      Thanks!


      [Non-text portions of this message have been removed]
    • Ashish Agarwal
      ... Yes, x has its own record, which is mutable. And which record is it? The one you named u. And you mutated u, so now you see the change in x. [Non-text
      Message 2 of 8 , Jun 19, 2013
        On Wed, Jun 19, 2013 at 1:55 PM, Chingfan Tsou <caojingfan@...> wrote:


        > how could the assignment of u affect the contents of x? x has its own
        > record
        >
        Yes, x has its own record, which is mutable. And which record is it? The
        one you named u. And you mutated u, so now you see the change in x.


        [Non-text portions of this message have been removed]
      • Chingfan Tsou
        Ah, does that mean if the contents is immutable value, it ll save a copy of the value; otherwise, if it s a ref of something(A for example), it won t save a
        Message 3 of 8 , Jun 19, 2013
          Ah, does that mean if the contents is immutable value, it'll save a copy of
          the value; otherwise, if it's a ref of something(A for example), it won't
          save a copy but instead, just save a "relation" so it'll be changed if A is
          changed. Am I right?


          [Non-text portions of this message have been removed]
        • Ashish Agarwal
          Yes, that s conceptually correct. If you know languages like C, you can think of ref as a pointer. When you say let x = ref u , you define x to be a pointer
          Message 4 of 8 , Jun 19, 2013
            Yes, that's conceptually correct. If you know languages like C, you can
            think of ref as a pointer. When you say "let x = ref u", you define x to be
            a pointer to u. Furthermore, you defined u to be a pointer to a memory
            location holding the value 1. Finally, you changed what was in this memory
            location to 2.

            Note that for immutable values, it is correct to logically think you are
            always getting a copy, but the compiler is smart enough to often avoid
            really making a copy. In other words, don't use ref's unnecessarily.
            Program with immutable values by default, and use ref's or other mutable
            data structures only if really needed.

            On Wed, Jun 19, 2013 at 2:13 PM, Chingfan Tsou <caojingfan@...> wrote:

            > **
            >
            >
            > Ah, does that mean if the contents is immutable value, it'll save a copy of
            > the value; otherwise, if it's a ref of something(A for example), it won't
            > save a copy but instead, just save a "relation" so it'll be changed if A is
            > changed. Am I right?
            >
            >
            > [Non-text portions of this message have been removed]
            >
            >
            >


            [Non-text portions of this message have been removed]
          • Lukasz Stafiniak
            ... mutable then the relation can change, otherwise the relation is fixed. [Non-text portions of this message have been removed]
            Message 5 of 8 , Jun 19, 2013
              On Wed, Jun 19, 2013 at 8:13 PM, Chingfan Tsou <caojingfan@...> wrote:

              > **
              >
              >
              > Ah, does that mean if the contents is immutable value, it'll save a copy of
              > the value; otherwise, if it's a ref of something(A for example), it won't
              > save a copy but instead, just save a "relation" so it'll be changed if A is
              > changed. Am I right?
              >
              > It never saves a copy, it is always a relation. If the record field is
              mutable then the "relation" can change, otherwise the "relation" is fixed.


              [Non-text portions of this message have been removed]
            • Chingfan Tsou
              2013/6/20 Ashish Agarwal ... I know languages like C. That s much easier to understand now. Thanks a lot!
              Message 6 of 8 , Jun 19, 2013
                2013/6/20 Ashish Agarwal <agarwal1975@...>
                >
                > Yes, that's conceptually correct. If you know languages like C, you can
                > think of ref as a pointer. When you say "let x = ref u", you define x to be
                > a pointer to u. Furthermore, you defined u to be a pointer to a memory
                > location holding the value 1. Finally, you changed what was in this memory
                > location to 2.
                >
                > Note that for immutable values, it is correct to logically think you are
                > always getting a copy, but the compiler is smart enough to often avoid
                > really making a copy. In other words, don't use ref's unnecessarily.
                > Program with immutable values by default, and use ref's or other mutable
                > data structures only if really needed.

                I know languages like C. That's much easier to understand now. Thanks a lot!
              • Chingfan Tsou
                ... Did you mean the compiler is smart enough to often avoid really making a copy ?
                Message 7 of 8 , Jun 19, 2013
                  > It never saves a copy, it is always a relation. If the record field is
                  > mutable then the "relation" can change, otherwise the "relation" is fixed.

                  Did you mean "the compiler is smart enough to often avoid
                  really making a copy"?
                • Francois Berenger
                  ... OCaml refs are more like C++ references in fact, because we never have null pointers in OCaml. And, that removes a hell of a lot of possible errors. That s
                  Message 8 of 8 , Jun 19, 2013
                    On 06/20/2013 03:37 AM, Ashish Agarwal wrote:
                    > Yes, that's conceptually correct. If you know languages like C, you can
                    > think of ref as a pointer.

                    OCaml refs are more like C++ references in fact, because we never have
                    null pointers in OCaml.
                    And, that removes a hell of a lot of possible errors.
                    That's a shame Go did not get this right.

                    > When you say "let x = ref u", you define x to be
                    > a pointer to u. Furthermore, you defined u to be a pointer to a memory
                    > location holding the value 1. Finally, you changed what was in this memory
                    > location to 2.
                    >
                    > Note that for immutable values, it is correct to logically think you are
                    > always getting a copy, but the compiler is smart enough to often avoid
                    > really making a copy. In other words, don't use ref's unnecessarily.
                    > Program with immutable values by default, and use ref's or other mutable
                    > data structures only if really needed.
                    >
                    > On Wed, Jun 19, 2013 at 2:13 PM, Chingfan Tsou <caojingfan@...> wrote:
                    >
                    >> **
                    >>
                    >>
                    >> Ah, does that mean if the contents is immutable value, it'll save a copy of
                    >> the value; otherwise, if it's a ref of something(A for example), it won't
                    >> save a copy but instead, just save a "relation" so it'll be changed if A is
                    >> changed. Am I right?
                    >>
                    >>
                    >> [Non-text portions of this message have been removed]
                    >>
                    >>
                    >>
                    >
                    >
                    > [Non-text portions of this message have been removed]
                    >
                    >
                    >
                    > ------------------------------------
                    >
                    > Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners
                    > The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
                    > Attachments are banned and you're asked to be polite, avoid flames etc.Yahoo! Groups Links
                    >
                    >
                    >
                  Your message has been successfully submitted and would be delivered to recipients shortly.