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

Function executed more than once in recursion?

Expand Messages
  • hmf@inescporto.pt
    Hello, I have what seems a strange result. Maybe I am overlooking something obvious. I am using the CuDD library via Ocaml in the following function: let rec
    Message 1 of 3 , Aug 1, 2008
    • 0 Attachment
      Hello,

      I have what seems a strange result. Maybe I am overlooking
      something obvious. I am using the CuDD library via Ocaml in
      the following function:

      let rec create_vars = function
      | [] -> []
      | h::t -> begin
      match h with
      | Var(i,_) -> Printf.printf "New var = %d\n" i ;
      (*(Bdd.newvar_at_level man i) :: create_vars t*)
      let v = Bdd.newvar_at_level man i
      in v :: create_vars t
      | _ -> raise_source_pos_exception "Expected a variable." pos
      end

      The function "Bdd.newvar_at_level man i" holds "state"
      in that it increments the number of variables in the BDD.
      When I use:
      (Bdd.newvar_at_level man i) :: create_vars t
      or
      Bdd.newvar_at_level man i :: create_vars t

      Two additional variables in a list of three are created.
      If I use:
      let v = Bdd.newvar_at_level man i
      in v :: create_vars t
      then the number of variables is correct.

      I therefore conclude that in the first 2 version
      the function is executed a second time, supposedly
      when recursion is terminating.

      What am I doing wrong here?

      TIA,
      Hugo F.
    • Martin Jambon
      ... Without looking deeply at your code, I suspect that your problem is in: expr1 :: expr2 You can t assume that expr1 will be evaluated before expr2. Always
      Message 2 of 3 , Aug 1, 2008
      • 0 Attachment
        On Fri, 1 Aug 2008, hmf@... wrote:

        > Hello,
        >
        > I have what seems a strange result. Maybe I am overlooking
        > something obvious. I am using the CuDD library via Ocaml in
        > the following function:
        >
        > let rec create_vars = function
        > | [] -> []
        > | h::t -> begin
        > match h with
        > | Var(i,_) -> Printf.printf "New var = %d\n" i ;
        > (*(Bdd.newvar_at_level man i) :: create_vars t*)
        > let v = Bdd.newvar_at_level man i
        > in v :: create_vars t
        > | _ -> raise_source_pos_exception "Expected a variable." pos
        > end
        >
        > The function "Bdd.newvar_at_level man i" holds "state"
        > in that it increments the number of variables in the BDD.
        > When I use:
        > (Bdd.newvar_at_level man i) :: create_vars t
        > or
        > Bdd.newvar_at_level man i :: create_vars t
        >
        > Two additional variables in a list of three are created.
        > If I use:
        > let v = Bdd.newvar_at_level man i
        > in v :: create_vars t
        > then the number of variables is correct.
        >
        > I therefore conclude that in the first 2 version
        > the function is executed a second time, supposedly
        > when recursion is terminating.
        >
        > What am I doing wrong here?

        Without looking deeply at your code, I suspect that your problem is in:

        expr1 :: expr2

        You can't assume that expr1 will be evaluated before expr2.
        Always do:

        let x = expr1 in
        x :: expr2

        And sorry if it's not the cause of your problem...


        Martin

        --
        http://mjambon.com/
      • hmf@inescporto.pt
        Martin, ... .. ... Which I did in fact. ... That s it! I was already thinking of race-conditions and what not. I was not understanding why the similar
        Message 3 of 3 , Aug 1, 2008
        • 0 Attachment
          Martin,

          Martin Jambon wrote:
          > On Fri, 1 Aug 2008, hmf@... wrote:
          >
          >> Hello,
          >>
          ..
          >
          > Without looking deeply at your code, I suspect that your problem is in:
          >
          > expr1 :: expr2
          >
          > You can't assume that expr1 will be evaluated before expr2.

          Which I did in fact.

          > Always do:
          >
          > let x = expr1 in
          > x :: expr2
          >
          > And sorry if it's not the cause of your problem...
          >

          That's it! I was already thinking of race-conditions and what not.

          I was not understanding why the similar functions:
          Bdd.ithvar man i in
          Bdd.newvar_at_level man i in
          Bdd.newvar man in

          which create variables did not generate the same results.
          The problem is "Bdd.newvar_at_level man i" shifts the variables
          for a given level "i" to make room for new ones. I had assumed
          I was going through the levels in order, hence expected no
          "shifting".

          A quick test shows that a call "Bdd.newvar_at_level man i"
          with "i" in inverse order generates those extra variables.

          Merci beaucoup,
          Hugo F.


          >
          > Martin
          >
          > --
          > http://mjambon.com/
          >
          > ------------------------------------
          >
          > Archives up to December 31, 2007 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.