caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pierre Weis <pierre.weis@inria.fr>
To: zhudp@cs.bu.edu (dengping zhu)
Cc: garrigue@kurims.kyoto-u.ac.jp, caml-list@inria.fr
Subject: Re: [Caml-list] about Obj.magic
Date: Sat, 1 Jun 2002 19:20:30 +0200 (MET DST)	[thread overview]
Message-ID: <200206011720.TAA16388@pauillac.inria.fr> (raw)
In-Reply-To: <Pine.SOL.4.20.0206011102220.14306-100000@csa.bu.edu> from dengping zhu at "Jun 1, 102 12:00:26 pm"

> Hi,Jacques,
> 
> Thanks for your help! You are right. I solved it by following your
> suggestion. The problem is exactly what you said: ty is abstarct. After
> I added the equation ty = ty1 -> ty2 in the signature where ty was
> defined, I got rid of 'Obj.magic'.
> 
> Now I am wondering why Ocaml compiler makes the type declaration
> abstract unless you specify like : 
>     type ty = ty1 -> ty2
> in the signature, since all the functions are concrete. I took it for
> granted that the type (ty) must be concrete. 

If you need it to be concrete export it as concrete, i.e. with a
definition.

> I always met some type problems in Ocaml. The common case is as follows:
> Suppose I define a module 'Test' (test.ml)
> ----------------------
> module Test : TEST =
>   struct
>      type ty =
>           | Int of int
> 	  | String of string
> 	  | Float of float
>   end;;
> ------------------------------
> and define the signature as :
> -----------------------
> module type TEST =
>   sig
>     type ty
>   end;;
> -----------------------

You exported ty as abstract again! If you want to use ty as concrete
export its definition as in

module type TEST =
   sig
     type ty =
        | Int of int
        | String of string
        | Float of float
   end;;

> Suppose that type ty is very common among all my modules and I want to
> reuse it as following:
> 
> let f x =
>   match x with
>   | Int _ -> 1
>   | String _ -> 2
>   | Float _ -> 3
> 
> where, I want x to has type ty.
> 
> In this case, the only method I can find is to open the module 'Test'.


Yes it's a good way to use the Module Test. Alternatively you can
qualify stuff from the module by prefixing identifiers with the name
of the module they belong to.

let f = function
  | Test.Int _ -> 1
  | Test.String _ -> 2
  | Test.Float _ -> 3

> Actually, 'open' is not a good way which I tried to avoid. Because I
> don't know what the expenses of 'open' is.

open cost nothing in terms of runtime execution: it is just a compiler
directive that avoid writing Test. all over the place, if Test is used
everywhere.

For more extensive discussion about ``open'' have a look at the
programming guide lines for Caml

http://pauillac.inria.fr/caml/pgl-eng.html

> Another reason is if I open a few modules, function overwrite will
> happen,

Not overwrite but masquerading, which far less complex and error prone.

> and it is difficult for me to find out the origins of all the
> imported functions.
>  Dengping

You should use emacs tags for that (assuming not every function in
your appli is named f or g) ...

Regards,

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/


-------------------
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


  reply	other threads:[~2002-06-01 17:21 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-05-30 21:09 dengping zhu
2002-05-30 22:38 ` Jacques Garrigue
2002-05-31  4:00 ` Daniel de Rauglaudre
2002-05-31  5:37   ` Oleg
2002-05-31  6:17     ` Daniel de Rauglaudre
     [not found]       ` <p05100300b91cea02dbf5@[192.168.1.22]>
2002-05-31  9:50         ` [Caml-list] SML->OCaml (was: about Obj.magic) Daniel de Rauglaudre
2002-05-31 15:39   ` [Caml-list] about Obj.magic dengping zhu
2002-05-31 16:28     ` John D. Barnett
2002-06-01  8:37     ` Jacques Garrigue
2002-06-01 16:00       ` dengping zhu
2002-06-01 17:20         ` Pierre Weis [this message]
2002-05-31  9:17 ` Warp
2002-05-31 13:58   ` Jacques Garrigue
2002-05-31 14:06     ` Warp
2002-05-31 18:21       ` John Max Skaller
2002-06-01 17:42         ` Pierre Weis
2002-06-02 15:15           ` John Max Skaller
     [not found]           ` <D37FA3E3-763B-11D6-BE8F-0003938819CE@inria.fr>
2002-06-04 10:22             ` Pierre Weis
2002-05-31 14:10     ` Remi VANICAT
2002-05-31 16:19     ` Christophe TROESTLER
2002-05-31 18:06       ` Olivier Andrieu
2002-05-31 22:03         ` Christophe TROESTLER

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=200206011720.TAA16388@pauillac.inria.fr \
    --to=pierre.weis@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=garrigue@kurims.kyoto-u.ac.jp \
    --cc=zhudp@cs.bu.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;
as well as URLs for NNTP newsgroup(s).