caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] recursive type declaration
@ 2002-05-31 21:56 dengping zhu
  2002-06-01  8:26 ` Jacques Garrigue
  0 siblings, 1 reply; 3+ messages in thread
From: dengping zhu @ 2002-05-31 21:56 UTC (permalink / raw)
  To: Ocaml

Hi, all, I have to raise another question about recursive type in Ocaml.

Now I want to define a type and a class, both of which are recursive
defined as follows:
----------------
type ty1 =
   | Int of int
   | String of string
   | Class of typeClass

class virtual typeClass =
   object
      method virtual typeCon : ty1
   end;;
-----------------
However, it does not work. 
I can use another way to solve it by define ty1 as 'a ty1. Besides this
way, is there any method to solve it? What's more, what's the common way
to define such types?

Thanks,
Dengping



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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] recursive type declaration
  2002-05-31 21:56 [Caml-list] recursive type declaration dengping zhu
@ 2002-06-01  8:26 ` Jacques Garrigue
  2002-06-02  9:08   ` William Lovas
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Garrigue @ 2002-06-01  8:26 UTC (permalink / raw)
  To: zhudp; +Cc: caml-list

From: dengping zhu <zhudp@cs.bu.edu>
> Now I want to define a type and a class, both of which are recursive
> defined as follows:
> ----------------
> type ty1 =
>    | Int of int
>    | String of string
>    | Class of typeClass
> 
> class virtual typeClass =
>    object
>       method virtual typeCon : ty1
>    end;;
> -----------------
> However, it does not work. 
> I can use another way to solve it by define ty1 as 'a ty1. Besides this
> way, is there any method to solve it? What's more, what's the common way
> to define such types?

I believe this is a FAQ.

Just parameterize one one of the two.
For instance:

type 'tc ty1 =
   | Int of int
   | String of string
   | Class of 'tc

class virtual typeClass =
   object
      method virtual typeCon : typeClass ty1
   end;;

Arguably, this is a misfeature of the language that you cannot define
the two together, but then recursive class definitions and type
definitions do not handle constraints in the same way, so this would
be hard to mix them properly.

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] recursive type declaration
  2002-06-01  8:26 ` Jacques Garrigue
@ 2002-06-02  9:08   ` William Lovas
  0 siblings, 0 replies; 3+ messages in thread
From: William Lovas @ 2002-06-02  9:08 UTC (permalink / raw)
  To: caml-list

On Sat, Jun 01, 2002 at 05:26:11PM +0900, Jacques Garrigue wrote:
> From: dengping zhu <zhudp@cs.bu.edu>
> > I can use another way to solve it by define ty1 as 'a ty1. Besides this
> > way, is there any method to solve it? What's more, what's the common way
> > to define such types?
> 
> I believe this is a FAQ.
> 
> Just parameterize one one of the two.
> For instance:
> 
> type 'tc ty1 =
>    | Int of int
>    | String of string
>    | Class of 'tc
> 
> class virtual typeClass =
>    object
>       method virtual typeCon : typeClass ty1
>    end;;

Incidentally, if you really wanted an alternative, you could explicitly
specify the methods you need in ty1's definition, since (i think) class
types are just an abbreviation for objects containing all the necessary
methods.  i.e.:

    type ty1 =
      | Int of int
      | String of string
      | Class of < typeCon : ty1 >

    class virtual typeClass =
      object
        method virtual typeCon : ty1
      end

This is inferior to parametrization in at least two ways:

    1. It most likely fails to capture the intended generalization, making
    the whole thing more error prone, and
    
    2. With a larger example, it could be quite cumbersome (related to 1., 
    in a way).

It's a possibility, but it seems to have a pretty high ugly potential...

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-06-02  9:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-05-31 21:56 [Caml-list] recursive type declaration dengping zhu
2002-06-01  8:26 ` Jacques Garrigue
2002-06-02  9:08   ` William Lovas

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