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: kahl@diogenes.informatik.unibw-muenchen.de
Cc: caml-list@inria.fr
Subject: Re: Recursive types in OCaml/OLabl-1.06
Date: Thu, 27 Nov 1997 14:44:43 +0900	[thread overview]
Message-ID: <19971127144443U.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: Your message of "26 Nov 1997 13:06:33 -0000" <19971126130633.1554.qmail@diogenes.informatik.unibw-muenchen.de>

From: Wolfram Kahl <kahl@diogenes.informatik.unibw-muenchen.de>

> Recapitulating an argument that already has been
> discussed to a certain degree in the last few days,
> I would like to write something like:
> 
> @O@<rectest1.mli@>==@{@-
> type x = x option
> @}

[ lots of material erased: cf. the original message ]
 
> @$@<rectest1 output@>@Z==@{@-
> File "rectest1.mli", line 1, characters 4-17:
> The type abbreviation x is cyclic
> @}
> 
> Obviously it is not a good idea to write
> 
> @O@<rectest2.mli@>==@{@-
> type x = None | Some of x
> @}
> 
> which is accepted, but gives bad name clashes
> with the original constructors of @{option@}.

I'm sorry to give an O'Labl specific answer in the caml-list, but this
program is supposed to be an O'Labl one.

In fact, using O'Labl there is another solution to this problem.
You can define your own option type using polymorphic variants:

	type 'a opt = [None Some('a)]

And since the rule for O'Labl is that a cyclic abbreviation should go
through an object OR a polymorphic variant,

	type nat = nat opt

will be accepted, and is exactly equivalent to,

	type nat = [None Some(nat)]

Since these are polymorphic variants, there is no problem of name clash:
you may define the same tag in as many types as you want.

Last, if you care about speed, remember that as long as you have only
a small number of variant tags by type, polymorphic variants are just
as efficient as standard ones when you use the native code compiler.

Another remark, related with this discussion, is that O'Labl 1.05 was
already more restrictive than O'Caml 1.05 about recursive types. In
particular ('a option as 'a) was not a valid type to infer.

Here is an O'Labl 1.05 example:

# type nat = nat option;;
type nat = nat option
# fun x -> (x = Some x);;
This expression has type 'a option but is here used with type 'a
# fun (x : nat) -> (x = Some x);;
- : nat -> bool = <fun>

As you can see, such recursive types may be used, but not inferred.
The second and third line would have been equivalently OK with O'Caml.
This may seem not very clean, since you need a type annotation in
order to type the function, and I suppose this is why it is not
allowed in O'Caml 1.06.

Regards,

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





      reply	other threads:[~1997-11-27  7:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-11-26 13:06 Wolfram Kahl
1997-11-27  5:44 ` 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=19971127144443U.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=kahl@diogenes.informatik.unibw-muenchen.de \
    /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