Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jerome Vouillon <vouillon@clipper.ens.fr>
To: Caml List <caml-list@inria.fr>
Subject: Re: types recursifs ...
Date: Thu, 15 May 1997 19:30:52 +0200 (MET DST)	[thread overview]
Message-ID: <Pine.SUN.3.95.970515185203.22011A-100000@nave> (raw)
In-Reply-To: <33797A31.4765@lri.fr>



On Wed, 14 May 1997, Emmanuel Engel wrote:

> ~> ocaml
>         Objective Caml version 1.05
> #  type 'a t = Leaf of 'a | Node of ('b t as 'b) * ('c t as 'c);;
> type 'a t = | Leaf of 'a | Node of 'a * 'a constraint 'a = 'a t
[...]
> # let rec l = l :: l;;
> val l : 'a list as 'a =
[...]
> 
> L'interet de tels types me m'apparais pas de facon immediate. Je ne vois
> pas de facon de definir des valeurs avec un tel type qui ne soient pas
> un cas terminal (ie la liste vide par exemple) ou un terme infini; les 
> contraintes du langage impose alors que le terme presente une regularite 
> certaine: cela ne peut etre qu'une definition syntaxique et pas le
> resultat d'un (vrai) calcul.

Voici un exemple.

  # type 'a t = | A | B of 'a;;
  type 'a t = | A | B of 'a
  # let x = B(1, B (2, A));;
  x : (int * (int * 'a t) t) t = B (1, B (2, A))

Il n'y a pas de recursion dans le type infere pour x. Mais on peut
donner a x un type recursif.

  # (x : (int * 'a) t as 'a);;
  - : (int * 'a) t as 'a = B (1, B (2, A))

Une fonction iterant sur de telles listes aura un type recursif.

  # let rec iter f = function A -> () | B (x, l) -> f x; iter f l;;
  val iter : ('a -> 'b) -> (('a * 'c) t as 'c) -> unit = <fun>

En fait, on definit plutot t par

  # type 'a t = | A | B of 'a * 'a t;;

On n'a plus alors besoin de type (explicitement) recursif pour typer
les examples precedents. La recursion est masquee par le constructeur
de type B.

  # let x = B(1, B (2, A));;
  val x : int t = B (1, B (2, A))
  # let rec iter f = function A -> () | B (x, l) -> f x; iter f l;;
  val iter : ('a -> 'b) -> 'a t -> unit = <fun>

En pratique, lorsque le type infere' pour une expression est recursif,
c'est qu'il y a en fait une erreur dans la definition de cette
expression. Ce genre de resultat etant tres deconcertant la premiere
fois que l'on tombe dessus, la recursion ne sera a priori acceptee
dans la prochaine release de ocaml que la` ou` elle est vraiment
necessaire (c'est-a-dire lorsqu'elle traverse un objet; par exemple : 
<x : 'a> as 'a).  Il est de toute facon toujours possible de masquer
la recursion en utilisant des types construits (voir le mail de
Christian Boos).

-- Jerome Vouillon






  reply	other threads:[~1997-05-15 18:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-04-28  3:07 Sven LUTHER
1997-04-29 14:22 ` Didier Rousseau
1997-04-30  2:50   ` Sven LUTHER
1997-04-30 12:42     ` Didier Rousseau
1997-05-14  6:39       ` Sven LUTHER
1997-05-14  8:39         ` Emmanuel Engel
1997-05-15 17:30           ` Jerome Vouillon [this message]
1997-05-14 15:00         ` [LONG] " Christian Boos

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=Pine.SUN.3.95.970515185203.22011A-100000@nave \
    --to=vouillon@clipper.ens.fr \
    --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