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

Re: "ocaml_beginners"::[] Possible Y warning and/or compilation error when using references

Expand Messages
  • William D. Neumann
    ... Well, that s not what you re doing. What let _ = s_v = ref b in blah does is check to see if s_v is equal to ref b (i.e. if it is a reference that
    Message 1 of 8 , Nov 3, 2005
    • 0 Attachment
      On Thu, 3 Nov 2005, Hugo Ferreira wrote:

      >> Also, I wonder what's the purpose of "let _ = s_v = ref b" ? It does
      >> nothing: is this just there for the demo ?
      >
      > This is my attempt not to "shadow" s_v. I figured this is equivalent to:
      > (s_v = ref b)

      Well, that's not what you're doing. What "let _ = s_v = ref b in blah"
      does is check to see if s_v is equal to ref b (i.e. if it is a reference
      that contains something = to b), then throws away the result and performs
      the code in blah. It's equivalent to defining a function like:

      # let f x y =
      x = y;;
      val f : 'a -> 'a -> bool = <fun>

      and saying ignore (f s_v (ref b)); blah...

      Note that this is more or less like saying (s_v = ref b); minus one
      warning:

      # let b = 100;;
      val b : int = 100
      # let s_v = ref b;;
      val s_v : int ref = {contents = 100}
      # s_v = ref b;;
      - : bool = true
      # (s_v = ref b); print_endline "yo!";;
      Characters 0-13:
      Warning S: this expression should have type unit.
      (s_v = ref b); print_endline "yo!";;
      ^^^^^^^^^^^^^
      yo!
      - : unit = ()

      > Not what I am looking for. In the above we are just setting s_b's value
      > to v's value (see * in example below). I want s_b to be the same
      > reference as "ref v" (see example below).

      Then you have to say "let s_b = ref v in ...".

      Of course, I still don't see you actually using s_b or s_v anywhere in
      that code snippet...

      > The following is an example of the effect I want:
      > <snip...>

      Wait a second... what you showed in this example is not what you said
      earlier. There, you said "s_b is a reference and v is a value", but in
      your ref example, you are working with two refs (which are values
      themselves, but nevermind). So, what are you trying to do? Do you want:

      a) s_b and v are both references to the same cell, so that when you update
      one, the other is dragged along? If so do what you showed in the snipped
      example, i.e. let s_b = v in ...

      b) s_b and v are both references to equal values, but to different cells,
      so they can be changed independently? If so, use let s_b = !v in...

      c) s_b is a refrence and v is a value, and you want s_b to hold the
      current binding of v? If so, use let s_b = ref v in ... Note, however,
      that changing the binding of v won't affect the contents of s_b, e.g.
      # let v = 100;;
      val v : int = 100
      # let s_b = ref v;;
      val s_b : int ref = {contents = 100}
      # let v = 200;;
      val v : int = 200
      # s_b;;
      - : int ref = {contents = 100}
      # s_b := 9;;
      - : unit = ()
      # s_b;;
      - : int ref = {contents = 9}
      # v;;
      - : int = 200

      If you want the above, but you want s_b to track the rebindings of v...
      well, you're S.O.L. OCaml don't play that way.

      William D. Neumann

      ---

      "There's just so many extra children, we could just feed the
      children to these tigers. We don't need them, we're not doing
      anything with them.

      Tigers are noble and sleek; children are loud and messy."

      -- Neko Case

      Life is unfair. Kill yourself or get over it.
      -- Black Box Recorder
    • William D. Neumann
      ... Whoops! That should be let s_b = ref !v in ... Or if you re trying to update the s_b from back in the match case (i.e. Root(_,_,lb,nb,s_b)), then a simple
      Message 2 of 8 , Nov 3, 2005
      • 0 Attachment
        On Thu, 3 Nov 2005, William D. Neumann wrote:

        > b) s_b and v are both references to equal values, but to different cells,
        > so they can be changed independently? If so, use let s_b = !v in...

        Whoops! That should be
        let s_b = ref !v in ...

        Or if you're trying to update the s_b from back in the match case (i.e.
        Root(_,_,lb,nb,s_b)), then a simple
        s_b := !v;
        will do.

        William D. Neumann

        ---

        "There's just so many extra children, we could just feed the
        children to these tigers. We don't need them, we're not doing
        anything with them.

        Tigers are noble and sleek; children are loud and messy."

        -- Neko Case

        Life is unfair. Kill yourself or get over it.
        -- Black Box Recorder
      • Hugo Ferreira
        Hi William, ... snip.... Unbelievable, I had this figured out while doing some testing in top-level. Lets ignore this ok? ;-) ... I have a tree. Each node has
        Message 3 of 8 , Nov 3, 2005
        • 0 Attachment
          Hi William,

          On Thursday 03 November 2005 15:14, William D. Neumann wrote:
          > On Thu, 3 Nov 2005, Hugo Ferreira wrote:
          > >> Also, I wonder what's the purpose of "let _ = s_v = ref b" ? It does
          > >> nothing: is this just there for the demo ?
          > >
          > > This is my attempt not to "shadow" s_v. I figured this is equivalent to:
          > > (s_v = ref b)
          >
          > Well, that's not what you're doing. What "let _ = s_v = ref b in blah"
          > does is check to see if s_v is equal to ref b (i.e. if it is a reference
          > that contains something = to b), then throws away the result and performs
          > the code in blah. It's equivalent to defining a function like:
          >
          > # let f x y =
          > x = y;;
          > val f : 'a -> 'a -> bool = <fun>
          >
          snip....

          Unbelievable, I had this figured out while doing some testing in top-level.
          Lets ignore this ok? ;-)

          >
          > Of course, I still don't see you actually using s_b or s_v anywhere in
          > that code snippet...
          >

          I have a tree. Each node has a list of children. These children have
          references to the parents (lets call that reference "p"). These children
          however may be moved from one parent to another. Initially I would traverse
          the list of children and set the parent reference "p" one at a time.

          Now I wanted to use a single reference that points to the parent. All children
          use this reference, call it reference "p". Now when I change the children's
          parent pointer I need only change "p" once to a p'.

          > > The following is an example of the effect I want:
          > > <snip...>
          >
          > Wait a second... what you showed in this example is not what you said
          > earlier. There, you said "s_b is a reference and v is a value", but in
          > your ref example, you are working with two refs (which are values
          > themselves, but nevermind). So, what are you trying to do? Do you want:
          >
          snip....
          >
          > c) s_b is a refrence and v is a value, and you want s_b to hold the
          > current binding of v? If so, use let s_b = ref v in ...

          This is the idea.

          > Note, however, that changing the binding of v won't affect the contents of
          > s_b, e.g.
          > # let v = 100;;
          > val v : int = 100
          > # let s_b = ref v;;
          > val s_b : int ref = {contents = 100}
          > # let v = 200;;
          > val v : int = 200
          > # s_b;;
          > - : int ref = {contents = 100}
          > # s_b := 9;;
          > - : unit = ()
          > # s_b;;
          > - : int ref = {contents = 9}
          > # v;;
          > - : int = 200
          >
          > If you want the above, but you want s_b to track the rebindings of v...

          yes.

          > well, you're S.O.L. OCaml don't play that way.
          >

          I see. Back to the drawing board. 8-(

          Thanks for the correction,

          Hugo Ferreira.


          > William D. Neumann
          >
          > ---
          >
          > "There's just so many extra children, we could just feed the
          > children to these tigers. We don't need them, we're not doing
          > anything with them.
          >
          > Tigers are noble and sleek; children are loud and messy."
          >
          > -- Neko Case
          >
          > Life is unfair. Kill yourself or get over it.
          > -- Black Box Recorder
          >
          >
          >
          > Archives up to August 22, 2005 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
          >
          >
          >
        • Richard Jones
          ... Another possibility is to make each node into a struct and use a mutable field for the parent reference. This thread might also be helpful:
          Message 4 of 8 , Nov 3, 2005
          • 0 Attachment
            On Thu, Nov 03, 2005 at 04:28:26PM +0000, Hugo Ferreira wrote:
            > Now I wanted to use a single reference that points to the
            > parent. All children use this reference, call it reference "p". Now
            > when I change the children's parent pointer I need only change "p"
            > once to a p'.

            Another possibility is to make each node into a struct and use a
            mutable field for the parent reference.

            This thread might also be helpful:

            http://groups.yahoo.com/group/ocaml_beginners/message/2711

            Rich.

            --
            Richard Jones, CTO Merjis Ltd.
            Merjis - web marketing and technology - http://merjis.com
            Team Notepad - intranets and extranets for business - http://team-notepad.com
          • Hugo Ferreira
            Hi, After some time experimenting with top-level I have finally figured out what I need. My objective is to implement an algorithm that is imperative in style.
            Message 5 of 8 , Nov 4, 2005
            • 0 Attachment
              Hi,

              After some time experimenting with top-level I have finally
              figured out what I need. My objective is to implement an
              algorithm that is imperative in style. I am using a tree-like
              structure that uses references as pointers to parent nodes. The
              session in top-level shown below is essentially what I need:

              # type node = Empty | Node of node ref ;;
              type node = Empty | Node of node ref
              # let p = ref Empty ;;
              val p : node ref = {contents = Empty}
              # let r1 = Node(p);;
              val r1 : node = Node {contents = Empty}
              # p := r1 ;; <---- (0)
              - : unit = ()
              # p;;
              - : node ref =
              {contents =
              Node
              {contents =
              Node
              {contents =
              Node
              {contents =
              Node
              {contents =
              Node
              {contents =
              Node


              Note however that even though the structure seems correct
              (i.e.: I have a node that points to a parent, in this case
              itself) this is not what I need. As William pointed out any
              changes to "r1" will not be reflected in "p" because (0) is
              basically copying a value into the space allocated for
              reference "p".

              What I need is to have "reference p" not contain the value
              of r1 but the reference of r1. In other words I need
              references of references 8-{.

              A simple example is shown below:

              # let x = ref 100;;
              val x : int ref = {contents = 100}
              # let y = ref x;;
              val y : int ref ref = {contents = {contents = 100}}
              # let z = ref x;;
              val z : int ref ref = {contents = {contents = 100}}
              # x := 200;;
              - : unit = ()
              # z;;
              - : int ref ref = {contents = {contents = 200}}
              # y;;
              - : int ref ref = {contents = {contents = 200}}
              # let x' = ref 5 ;;
              val x' : int ref = {contents = 5}
              # y := x' ;;
              - : unit = ()
              # z := x' ;;
              - : unit = ()
              # x;;
              - : int ref = {contents = 200}
              # x';;
              - : int ref = {contents = 5}
              # y;;
              - : int ref ref = {contents = {contents = 5}}
              # z;;
              - : int ref ref = {contents = {contents = 5}}

              Needless to say I now understand Jon's advice to "avoid such
              structures and stick to the immutable". Of course this is easier
              said than done... how do I implement algorithms that are
              essentially imperative as functional?

              Any comments?

              TIA,
              Hugo Ferreira.


              On Thursday 03 November 2005 15:32, William D. Neumann wrote:
              > On Thu, 3 Nov 2005, William D. Neumann wrote:
              > > b) s_b and v are both references to equal values, but to different cells,
              > > so they can be changed independently? If so, use let s_b = !v in...
              >
              > Whoops! That should be
              > let s_b = ref !v in ...
              >
              > Or if you're trying to update the s_b from back in the match case (i.e.
              > Root(_,_,lb,nb,s_b)), then a simple
              > s_b := !v;
              > will do.
              >
              > William D. Neumann
              >
              > ---
              >
              > "There's just so many extra children, we could just feed the
              > children to these tigers. We don't need them, we're not doing
              > anything with them.
              >
              > Tigers are noble and sleek; children are loud and messy."
              >
              > -- Neko Case
              >
              > Life is unfair. Kill yourself or get over it.
              > -- Black Box Recorder
              >
              >
              >
              > Archives up to August 22, 2005 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
              >
              >
              >
            • Hugo Ferreira
              Hi Richard, ... Basically that is what I am doing. Only I sue the ref and := to manipulate the value. ... I went through the thread. What struck me is that
              Message 6 of 8 , Nov 4, 2005
              • 0 Attachment
                Hi Richard,

                On Thursday 03 November 2005 16:50, Richard Jones wrote:
                > On Thu, Nov 03, 2005 at 04:28:26PM +0000, Hugo Ferreira wrote:
                > > Now I wanted to use a single reference that points to the
                > > parent. All children use this reference, call it reference "p". Now
                > > when I change the children's parent pointer I need only change "p"
                > > once to a p'.
                >
                > Another possibility is to make each node into a struct and use a
                > mutable field for the parent reference.
                >

                Basically that is what I am doing. Only I sue the "ref" and ":=" to manipulate
                the value.

                > This thread might also be helpful:
                >
                > http://groups.yahoo.com/group/ocaml_beginners/message/2711

                I went through the thread. What struck me is that the structure
                does not explicitly use mutable references but resorts to the use
                of a hash table.

                Maybe I should consider such a solution. Nevertheless I think I have
                identified the real issue that has been bugging me (see my latest response).

                Thanks for the feedback,
                Hugo Ferreira.


                >
                > Rich.
              Your message has been successfully submitted and would be delivered to recipients shortly.