caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Recursive record initialisation
@ 2018-04-16 20:57 Angelo Corsaro
  2018-04-16 21:07 ` Gabriel Scherer
  0 siblings, 1 reply; 2+ messages in thread
From: Angelo Corsaro @ 2018-04-16 20:57 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 899 bytes --]

Hello,

	I have a question concerning the initialisation of mutually recursive record types. As a trivial example consider the following type declaration:

type atype = { b : btype; }
and btype = { a : atype; }

This is a legal type declaration (it type-checks w/o a blink), but I can’t see how such a type would be initialised? As both “a" and “b” are values there is an apparent bootstrapping issue. But as this is a legal type declaration I guess there is a way around, wondering what the right way is.

Thanks very much in advance for any clarification.

—Angelo
 



—
“Simplicity is the ultimate sophistication." ~ Leonardo da Vinci


-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

[-- Attachment #2: Type: text/html, Size: 2047 bytes --]

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

* Re: [Caml-list] Recursive record initialisation
  2018-04-16 20:57 [Caml-list] Recursive record initialisation Angelo Corsaro
@ 2018-04-16 21:07 ` Gabriel Scherer
  0 siblings, 0 replies; 2+ messages in thread
From: Gabriel Scherer @ 2018-04-16 21:07 UTC (permalink / raw)
  To: Angelo Corsaro; +Cc: caml users

The fact that the declaration is legal does not imply that you can
build useful values at this type. (It may be that trying to reject it
would naturally lead to reject other types that *are* more
interesting, for example.)

In this case you can build a value using value recursion, but it is
not very interesting:

# let rec a = { b }
       and b = { a };;
val a : atype = {b = {a = <cycle>}}
val b : btype = {a = {b = <cycle>}}

( Recursive value definitions:
http://caml.inria.fr/pub/docs/manual-ocaml/extn.html#s%3Aletrecvalues
)

Recursive value definitions are somewhat arcane and rarely used, but
still occasionally useful. One downside is that they prevent a purely
inductive interpretation of datatypes: if a recursive function is
structurally decreasing on one of its argument, there still exists
valid OCaml inputs on which it may not terminate. For example,
consider List.length on (let rec x = 1 :: 2 :: x).

On Mon, Apr 16, 2018 at 11:57 PM, Angelo Corsaro <angelo@icorsaro.net> wrote:
> Hello,
>
> I have a question concerning the initialisation of mutually recursive record
> types. As a trivial example consider the following type declaration:
>
> type atype = { b : btype; }
> and btype = { a : atype; }
>
> This is a legal type declaration (it type-checks w/o a blink), but I can’t
> see how such a type would be initialised? As both “a" and “b” are values
> there is an apparent bootstrapping issue. But as this is a legal type
> declaration I guess there is a way around, wondering what the right way is.
>
> Thanks very much in advance for any clarification.
>
> —Angelo
>
>
>
>
> —
> “Simplicity is the ultimate sophistication." ~ Leonardo da Vinci
>

-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

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

end of thread, other threads:[~2018-04-16 21:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-16 20:57 [Caml-list] Recursive record initialisation Angelo Corsaro
2018-04-16 21:07 ` Gabriel Scherer

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).