From: Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Getting rid of impossible polymorphic variant tags from infered types
Date: Mon, 1 Mar 2004 17:35:32 +0100 [thread overview]
Message-ID: <20040301163532.GA7661@cs.unibo.it> (raw)
In-Reply-To: <4043520E.2040802@bik-gmbh.de>
> type foo = [ `Foo | `Bar | `Fnord ]
> type foo_flat = [ `Foo | `Bar ]
>
> let f' l =
> let f_r (v: foo) (init: foo_flat list) =
> match v with
> | `Fnord -> init
> | _ -> Obj.magic v :: init
> in
> List.fold_right f_r l []
There is no need for Obj.magic:
type foo_flat = [ `Foo | `Bar ]
type foo = [ foo_flat | `Fnord ]
let f_r (v : foo) (init : foo_flat list) =
match v with
`Fnord -> init
| #foo_flat as v -> v :: init
;;
type foo = [ `Bar | `Fnord | `Foo ]
type foo_flat = [ `Bar | `Foo ]
val f_r : foo -> foo_flat list -> foo_flat list
Since the list `Foo | `Bar of variants is written only once,
the code is also reasonably extensible. Of course it is still not
what you want.
If you really need to exclude the `Fnord value at compile time, an
alternative solution is to use phantom types:
type 'a foo = [ `Foo | `Bar | `Fnord ]
let f_r (v : 'a foo) (init : ([> `NoFnord] foo) list) =
match v with
`Fnord -> init
| _ -> v :: init
;;
type 'a foo = [ `Bar | `Fnord | `Foo ]
val f_r : 'a foo -> ([> `NoFnord ] as 'b) foo list -> 'b foo list
The `NoFnord variant explicitly tells that the Fnord value is not allowed.
Notice how the first parameter does not have to satisfy this restriction,
while the second one has to. As usual, you need to use modules to enforce
that a value of type `Foo has type [`NoFnord] foo whereas a value of type
`NoFnord has NOT type [`NoFnord].
Regards,
C.S.C.
--
----------------------------------------------------------------
Real name: Claudio Sacerdoti Coen
PhD Student in Computer Science at University of Bologna
E-mail: sacerdot@cs.unibo.it
http://www.cs.unibo.it/~sacerdot
----------------------------------------------------------------
-------------------
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:[~2004-03-01 16:35 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-03-01 15:09 Florian Hars
2004-03-01 16:35 ` Claudio Sacerdoti Coen [this message]
2004-03-03 10:27 ` Florian Hars
2004-03-03 10:40 ` Claudio Sacerdoti Coen
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=20040301163532.GA7661@cs.unibo.it \
--to=sacerdot@cs.unibo.it \
--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