caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jean-Baptiste Jeannin <jeannin@cs.cornell.edu>
To: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: [Caml-list] Cyclic data structures: internal representation
Date: Mon, 12 Nov 2012 01:44:55 -0500	[thread overview]
Message-ID: <A7FC6DE86AF33142A1FA4CE04EEBF4F18596A9DD62@gobo.cs.cornell.edu> (raw)

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

             reply	other threads:[~2012-11-12  6:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-12  6:44 Jean-Baptiste Jeannin [this message]
2012-11-12  8:55 ` Basile Starynkevitch
2012-11-12  9:43 ` Gabriel Scherer
2012-11-12 11:24 ` Dmitry Grebeniuk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=A7FC6DE86AF33142A1FA4CE04EEBF4F18596A9DD62@gobo.cs.cornell.edu \
    --to=jeannin@cs.cornell.edu \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).