From: Olivier Andrieu <andrieu@ijm.jussieu.fr>
To: Christophe Raffalli <christophe.raffalli@univ-savoie.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Bug ? + Mutable list in OCaml 3.07beta2... using type inference to infer mutability
Date: Tue, 9 Sep 2003 11:28:29 +0200 [thread overview]
Message-ID: <16221.40253.994424.109094@karryall.dnsalias.org> (raw)
In-Reply-To: <3F5D7629.7060901@univ-savoie.fr>
Christophe Raffalli [Tuesday 9 September 2003] :
> Here is an implementation of mutable list where we use the typing of
> polymorphic variant (but no polymorphic variant) to infer if a program
> can mute a list !
>
> And even better, you can program the tail_rec version of map using
> set_cdr and have the type inference telling you that it does not mute
> its argument ... but I think this is a bug ? (if it is not, how to get
> the same type for append ?)
no I don't think it's a bug :
> let append l l' = match l with
> Nil -> l'
> | Cons(x,l) ->
> let acc0 = Cons(x,Nil) in
> let rec fn acc = function
> Nil -> set_cdr acc l'
> | Cons(x,l) ->
> let acc' = Cons(x,Nil) in
> set_cdr acc acc';
> fn acc' l
> in fn acc0 l; acc0
here, the set_cdr acc l' in the second Nil causes l' to have type (_,
[>`MutCdr]) since it comes as second argument of set_cdr.
I think the problem is with your set_cdr function. The second argument
shouldn't have the same 'mute parameter than the first one since the
second argument is not mutated.
val set_cdr : ('a, [> `MuteCdr ]) mlist -> ('a, 'b) mlist -> unit
With this type, append has the "right" type. The appendq type can come
right too but the function must be modified a bit :
,----
| let rec appendq l l' = match l with
| | Nil -> l'
| | Cons(_,_) ->
| let rec fn l = match l with
| Nil -> assert false
| | Cons(_, Nil) ->
| set_cdr l l' ; l
| | Cons(_, l) ->
| fn l
| in
| fn l
`----
Anyway, as usual with phantom types, they don't enfore much until the
type representation is abstracted.
And if you had abstracted the mlist definition with module constraint,
you wouldn't be able to come up with these types : for instance, the
return type of map would be ('b, [> `MuteCdr ]) mlist since you return
a cell that was set_cdr'ed (and the type of the second argument of append
would be ('b, [> `MuteCdr ]) mlist again).
--
Olivier
-------------------
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
next prev parent reply other threads:[~2003-09-09 9:28 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-09-04 19:21 [Caml-list] to Caml maillist birthday :) Valery A.Khamenya
2003-09-09 6:41 ` [Caml-list] Bug ? + Mutable list in OCaml 3.07beta2... using type inference to infer mutability Christophe Raffalli
2003-09-09 9:28 ` Olivier Andrieu [this message]
2003-09-09 13:03 ` Christophe Raffalli
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=16221.40253.994424.109094@karryall.dnsalias.org \
--to=andrieu@ijm.jussieu.fr \
--cc=caml-list@inria.fr \
--cc=christophe.raffalli@univ-savoie.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