Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: asl@tercom.ru
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Mutual recursion between classes and types in OCaml
Date: Mon, 23 Jun 2003 10:14:23 +0900	[thread overview]
Message-ID: <20030623101423H.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <20030621145534.GA3820@polymorph.dorms.spbu.ru>

From: Anton Lavrik <asl@tercom.ru>

> I suppose it's quite useful to define object types and "ordinary" types, so
> that they could refer to each other. For example,
> 
> 	class c = 
> 	object val v = A 0	(* constructor `A' is not bound *)
> 	end 
> 
> 	[...] (* some syntactic construct like `and' keyword *)
> 
> 	type t = A of int | B of c (* now type `t' and constructor `A' are
> 	defined *)

The solution is a FAQ, you just have to parameterize your datatype
with the type of the class (opening the recursion), and everything
will be fine.
 	
> So why mutual recursion between classes and types is not allowed? Is this
> fundamental or technical problem? I can't find any references denoting that
> problem.

This is technical, but has theoretical implications too.
Technical problem: typing of classes definitions and type definitions
are completely independent, and both of them are rather
complex. Mixing the two is practically hard.
Semantical problem: the handling of constraints in type definitions
and class definitions is different. Types allow polymorphic
recursion, and you must give all the constraints explicitely for
every type (what you write in the .mli is the real type). With
classes, constraints are propagated silently (so they may be only
implicit in the .mli), and there is no polymorphic recursion.
Mixing the two might be confusing, or unsound.

> BTW, what does `-rectypes' ocamlc's option really mean (there are quite a
> few words about it in reference manual)? And how does it concern the
> subject?

This is unrelated. It allows you two build arbitrary recursive types
through type inference
# let rec depth (_,l) = 1 + List.fold_left max 0 (List.map depth l);;
val depth : ('b * 'a list as 'a) -> int = <fun>
#  depth (1,[2,[];3,[]]);;
- : int = 2

---------------------------------------------------------------------------
Jacques Garrigue      Kyoto University     garrigue at kurims.kyoto-u.ac.jp
		<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


      reply	other threads:[~2003-06-23  1:14 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-06-21 14:55 Anton Lavrik
2003-06-23  1:14 ` Jacques Garrigue [this message]

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=20030623101423H.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=asl@tercom.ru \
    --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