caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Etienne Miret <etienne.miret@ens-lyon.fr>
To: Denis Bueno <dbueno@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Type error
Date: Sun, 15 Oct 2006 13:26:21 +0200	[thread overview]
Message-ID: <1B6343A2-A779-4BB7-8A5E-64DE51562035@ens-lyon.fr> (raw)
In-Reply-To: <6dbd4d000610142029m92d7005v65e95f031e7eae9b@mail.gmail.com>

On oct 15 2006 at 05:29, Denis Bueno wrote:
> ,----
> | val of_array: ?getter : ('b -> 'a) -> 'b array -> 'a t
> |   (** [of_array xs] returns a set containing the elements in  
> [xs]. *)
> `----
>
> I get a type error when trying to compile this. I don't know how to
> fix it, or even if it's possible without wrecking my nice of_array
> signature.

Your very nice of_array signature... I'm afraid you're signature is  
awful. Basically you're trying to get a 'a -> 'b type, which is  
impossible without using Obj.magic, and awful anyway. See, if you do

# let my_array = [| (1,'a') ; (2,'b') |];;
val my_array : (int * char) array = [|(1, 'a'); (2, 'b')|]
# let set = of_array ~getter:snd my_array;;
val set : char list = ['a'; 'b']

This is okay, but if you decide not to give the optional argument:

# let set2 = of_array my_array

Then what type should be inferred for set2? of course the real type  
is (int * char) t, but with the signature you gave for of_array you  
cannot infer it, since they're is no way to know that the default  
value for the optional argument has the type 'a -> 'a.

By implementing of_array with Obj.magic, you get:

# let set2 = of_array my_array;;
val set2 : 'a list = [<poly>; <poly>]

and then you can do funny (and ugly) things:

# add 4 set2;;
- : int list = [4; 354636; 354630]
# add 'c' set2;;
- : char list = ['c'; '\
36'; '\
30']
# add (3,'c') set2;;
- : (int * char) list = [(1, 'a'); (2, 'b'); (3, 'c')]

I implemented of_array this way:
# let of_array ?(getter = fun x -> Obj.magic x) xs =
   Array.fold_right (fun x set -> add (getter x) set) xs empty;;
   val of_array : ?getter:('a -> 'b) -> 'a array -> 'b list = <fun>

Regards,

Etienne


  parent reply	other threads:[~2006-10-15 11:25 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-10-15  3:29 Denis Bueno
2006-10-15  3:40 ` [Caml-list] " Jonathan Roewen
2006-10-15  3:48 ` skaller
2006-10-15  3:54 ` Jonathan Roewen
2006-10-15 11:26 ` Etienne Miret [this message]
2006-10-15 23:37 ` Jacques Garrigue
  -- strict thread matches above, loose matches on Subject: below --
2008-04-01  2:27 type error Jacques Le Normand
2008-04-01  2:42 ` [Caml-list] " Jacques Garrigue
2008-03-23 22:01 Jacques Le Normand
2008-03-23 22:19 ` [Caml-list] " Martin Jambon
2005-04-02  1:29 Type error Jon Harrop
2005-04-02  7:50 ` [Caml-list] " Jacques Garrigue

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=1B6343A2-A779-4BB7-8A5E-64DE51562035@ens-lyon.fr \
    --to=etienne.miret@ens-lyon.fr \
    --cc=caml-list@inria.fr \
    --cc=dbueno@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).