From: Vincent Aravantinos <vincent.aravantinos@yahoo.fr>
To: Arnaud Spiwack <Arnaud.Spiwack@lix.polytechnique.fr>
Cc: Alain Frisch <alain@frisch.fr>,
Jonathan T Bryant <jtbryant@valdosta.edu>,
caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Type issue
Date: Fri, 23 Nov 2007 14:44:05 +0100 [thread overview]
Message-ID: <27F8BDCA-043D-48EA-9CB6-5EBC65E9829F@yahoo.fr> (raw)
In-Reply-To: <4746D7BA.6060508@lix.polytechnique.fr>
Le 23 nov. 07 à 14:38, Arnaud Spiwack a écrit :
> Alain Frisch a écrit :
>> Jonathan T Bryant wrote:
>>> List,
>>>
>>> I don't understand the following typing:
>>>
>>> # type 'a t = Cond of bool t * 'a t * 'a t | Value of 'a;;
>>> type 'a t = Cond of bool t * 'a t * 'a t | Value of 'a
>>>
>>> # let rec f t = match t with
>>> Cond (c,t,e) -> if f c then f t else f e
>>> | Value x -> x
>>> ;;
>>> val f : bool t -> bool = <fun>
>>
>> The type system does not infer polymorphic recursion: the type of
>> a recursive function cannot be more general than any of its
>> occurences within its body.
>>
>> You can get around this limitation in various ways. E.g., with
>> recursive modules:
> My personal favorite, without modules :
>
> # type 'a t = Cond of bool t * 'a t * 'a t | Value of 'a;;
>
> let f_gen branch next t = match t with
> Cond (c,t,e) -> if branch c then next t else next e
> | Value x -> x
> ;;
>
> let rec f_deep t = f_gen f_deep f_deep t;;
>
> let rec f t = f_gen f_deep f t;;
>
>
> type 'a t = Cond of bool t * 'a t * 'a t | Value of 'a
> val f_gen : (bool t -> bool) -> ('a t -> 'a) -> 'a t -> 'a = <fun>
> val f_deep : bool t -> bool = <fun>
> val f : 'a t -> 'a = <fun>
>
> The pattern is rather generic (here we can do a bit better by
> replacing "next" by a recursive call to f_gen actually) :
> - you first write a generic version of your function where
> "recursive calls" are taken as arguments
> - you write a certain number of type-specialized function which are
> intended to be used as initial "recursive calls".
> They are themselves really recursive
> - you write your final function by using the type-specialized ones
> as "recursive calls"
>
> Notice that the use of "recursive calls" in the above is justified
> since all these functions have precisely the same semantics (and
> almost the same behaviour once compiled). But if someone has a
> better vocabulary to describe this practice, I'd gladly adopt it,
> as I'm not really satisfied with it. (I use "continuations" as
> well, but it still not quite what we intend).
This is just wonderful !
Thanks,
V.
next prev parent reply other threads:[~2007-11-23 13:44 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-11-23 4:01 Jonathan T Bryant
2007-11-23 8:26 ` [Caml-list] " Andrej Bauer
2007-11-23 9:27 ` Oliver Bandel
2007-11-23 8:30 ` Florian Weimer
2007-11-23 9:14 ` Alain Frisch
2007-11-23 13:38 ` Arnaud Spiwack
2007-11-23 13:44 ` Vincent Aravantinos [this message]
2007-11-23 9:33 ` Oliver Bandel
2007-11-23 12:33 ` Angela Zhu
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=27F8BDCA-043D-48EA-9CB6-5EBC65E9829F@yahoo.fr \
--to=vincent.aravantinos@yahoo.fr \
--cc=Arnaud.Spiwack@lix.polytechnique.fr \
--cc=alain@frisch.fr \
--cc=caml-list@yquem.inria.fr \
--cc=jtbryant@valdosta.edu \
/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