Version française
Home     About     Download     Resources     Contact us    
Browse thread
Type problem... possible design problem
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: chris.danx <chris.danx@n...>
Subject: Re: [Caml-list] Type problem... possible design problem
Richard Jones wrote:
> On Sat, Dec 18, 2004 at 02:28:09PM +0000, chris.danx wrote:
> 
>>My concern with variants is sharing subgraphs in graphs, will they be 
>>shared or copied?
> 
> A tree structure like this should be stored shared:
> 
>   type 'a tree = Leaf of 'a | Node of 'a tree list
> 
> You can prove this; for instance:
> 
> # let n = ref 1;;
> val n : int ref = {contents = 1}
> # let t = Leaf n;;  
> val t : int ref tree = Leaf {contents = 1}
> # let tree = Node [t; t];;
> val tree : int ref tree = Node [Leaf {contents = 1}; Leaf {contents = 1}]
> # n := 2;;
> - : unit = ()
> # tree;;
> - : int ref tree = Node [Leaf {contents = 2}; Leaf {contents = 2}]

Hmm... this looks like it shows two subtrees hold the same reference but 
it does not to me look like it proves the structure is shared, only that 
references work as expected.

Perhaps I can use

type 'a sceneGraph = Leaf of 'a | IntNode of 'a * 'a sceneGraph list | 
SharedNode of 'a sceneGraph ref

to achieve what I want.

Chris