caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Cyclic data structures: internal representation
@ 2012-11-12  6:44 Jean-Baptiste Jeannin
  2012-11-12  8:55 ` Basile Starynkevitch
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Jean-Baptiste Jeannin @ 2012-11-12  6:44 UTC (permalink / raw)
  To: caml-list

Good morning,

One can create cyclic data structures in OCaml, using the keyword let rec. For example:

  # let rec l0 = 1 :: 2 :: l0;;
  var l0 : int list =
    [1; 2; 1; 2; 1; 2; 1; 2; 1; 2; 1; 2; 1; 2; 1; 2; 1; 2; ...]

I am wondering what the internal representation (in the AST) of such a data structure is. It could be a cyclic data structure on the AST itself, but I doubt it because it would make it difficult to manipulate. Another solution I see is keeping a data structure with free variables inside (or references, for that matter) and binding these free variables in some other place. l0 would then bounded directly to  [1 ; 2 ; l0 ] in the AST. Any idea what the internals of OCaml do?

Related questions:
- is there any easy way to check if a list is cyclic or not? The only way I see is to go down the list, checking at every step if this particular sublist has already been seen. But it's rather heavy.
- the documentation on the = sign (http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html) mentions that "Equality between cyclic data structures may not terminate." It seems to terminate if one or the other of the data structures is not cyclic. Does it ever terminate when both data sstructures are cyclic, or does it always loop?
  # let l1 = [1; 2] in l0 = l1;;
  - : bool = false
  # l0 = l0;;
  (* doesn't finish *)

Thank you,
Jean-Baptiste Jeannin

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2012-11-12 11:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-12  6:44 [Caml-list] Cyclic data structures: internal representation Jean-Baptiste Jeannin
2012-11-12  8:55 ` Basile Starynkevitch
2012-11-12  9:43 ` Gabriel Scherer
2012-11-12 11:24 ` Dmitry Grebeniuk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).