r/ocaml•
queus

Some troubles with types

I had a bit of trouble with a type which I used for modeling trees, which can be navigated up, sideways and down, and where nodes can be added left, right and down. Context

I have the following type for nodes:

type node = {
  mutable value : int;
  parent : node option;
  left : node option;
  right : node option;
  mutable children : node list;
}

Pretty straightforward. Now suppose I need to find a node in the children list and do something like

if node = child then

then OCaml crashes with an out of memory error. Well, I guess that is what circular references and structural equality will do for you, but I wasn't expecting it.

So first question is: Is that something which OCaml should handle better or is just the expected behavior.

Another problem is that I'd rather have the field children be node array than a node list, but I how I would I then initialize it?

Even if somehow I would have managed (Obj.magic and such) to create a dummy value of type node it would not be pretty.

Well, I guess this simply cannot be done..

Posted Mar 28, 2016, 9:51 PM UTC

516
[deleted]•

[deleted]

7
queusOP•

Wow! That's lot to digest.

And yes, left and right fields should be mutable. Also unique IDs for nodes are a good idea.

1
jeffsco•

The = operator is trying to compare the whole structure, and for cyclic structures this doesn't terminate. You can use == on mutable structures to compare the identity. I suspect that's what you want.

You can treat OCaml as a standard imperative language if you want. There's nothing (within reason) that "can't be done."

4
aiij•

Call a polymorphic function recursively on a different type.

1
jeffsco•

Until OCaml 3.12 one had to resort to tricks to get polymorphic recursion.

https://blogs.janestreet.com/ensuring-that-a-function-is-polymorphic-in-ocaml-3-12/

(But naturally there are many tricky things that the type system won't let you do, and a few of them are useful. That's not the spirit of the original post, which is talking about making straightforward computations.)

2
aiij•

Cool. I'm not sure if that would work where I wanted it back in the day, but cool nonetheless.

1
Syrak•

If you keep a mutable children field you can start with an empty array, and allocate the final array once you have all the children, saving the trouble of initializing it with dummy values. You don't need Obj.magic to create a dummy node: { value = 0; parent = None; left = None; right = None; children = [||] }.

2
queusOP•

Thanks! Somehow this option escaped me.

Just for fun... since there can be no more than 10 child node I can inline the array and have a child count and fileds

 kid0 : node option; ... kid9: node option

Shouldn't be too terrible :)

1
BluddyCurry•

It will be terrible. Iterating through the children is now a manual, laborious process. You really want an array there.

Also, depending on how much performance matters to you, you might be ok with a list of children after all. Any place where you can use functional data structures will generally simplify your code and more importantly, the number of mistakes you may make with it. Of course, if you need fast random access to children, a list won't do, but perhaps a Map will.

1
queusOP•

Also, depending on how much performance matters to you, you might be ok with a list of children after all.

Well it matters. To a degree. On a 100001 lines input my version runs in 4.5 seconds, while the the time limit is 3 seconds.

But... I've settled to the following node type

type node = {
  mutable value : int;
  mutable index : int;
  parent : node option;
  mutable children : node list;
}

where every node "knows" its position in list and after every deletion/insertion I re-index the children.

The funny thing is that when I change the type of children from node list to node array it became slower, even if I do insertion/deletion by first converting the list to an array and then back.

Well...

1
BluddyCurry•

How is this code compiled? Is it the code submitted on the web form that is profiled, or do you have your own local code that you're testing?

1