caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Allowing many types
@ 2002-02-08  2:29 Ryan Tarpine
  2002-02-08 16:37 ` Ceri Storey
  0 siblings, 1 reply; 7+ messages in thread
From: Ryan Tarpine @ 2002-02-08  2:29 UTC (permalink / raw)
  To: caml-list

  Recently, I've been playing with writing my own language.  With some good 
books, and inspiration from existing languages, it's not that hard, really, 
even for a beginner  :)  The only problem I've found is setting the possible 
data types, the so-called "primitives" of the language.  Currently, I have 
something like this:

type Primitive =
| PInt of int
| PString of string
| PFloat of float
   ...

  The problem is, I know that I will need new types in the future besides 
these.  It's an object-oriented language, and I would like to add classes 
that manage things like Tk.  Every object has a variable of type Primitive 
that it stores its value in.  Is it possible to somehow allow other types, 
like Tk's widgets, without changing the definition of Primitive to include 
them and recompiling everything?

Thanks in advance,

Ryan Tarpine, rtarpine@hotmail.com
"To err is human, to compute divine.  Trust your computer but not its 
programmer."
  - Morris Kingston

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Allowing many types
  2002-02-08  2:29 [Caml-list] Allowing many types Ryan Tarpine
@ 2002-02-08 16:37 ` Ceri Storey
  2002-02-08 16:53   ` Remi VANICAT
  0 siblings, 1 reply; 7+ messages in thread
From: Ceri Storey @ 2002-02-08 16:37 UTC (permalink / raw)
  To: Ryan Tarpine; +Cc: caml-list

On Thu, Feb 07, 2002 at 09:29:44PM -0500, Ryan Tarpine wrote:
> type Primitive =
> | PInt of int
> | PString of string
> | PFloat of float
>  ...

>  The problem is, I know that I will need new types in the future besides 
> these.  It's an object-oriented language, and I would like to add classes 
> that manage things like Tk.  Every object has a variable of type Primitive 
> that it stores its value in.  Is it possible to somehow allow other types, 
> like Tk's widgets, without changing the definition of Primitive to include 
> them and recompiling everything?

I've been trying to do something similar with something i'm writing, and
the only thing i could come up with is to store data in an object, and
then subclass it. The problem here is that once the object has been
coerced to the general type, it cannot be coerced back again. ie: if you
subclass the object to say, impliment integers, then it's not possible
for it to be coerced back to the integer type so you can retreive / act
on the value. (withoug using Obj.magic, that is)

If anyone has any good ideas, I'd be happy to hear about them too.
-- 
Ceri Storey <cez@pkl.net> http://pkl.net/~cez/
vi(1)! postfix(7)! pie(5)!
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Allowing many types
  2002-02-08 16:37 ` Ceri Storey
@ 2002-02-08 16:53   ` Remi VANICAT
  2002-02-10 17:42     ` Ceri Storey
  0 siblings, 1 reply; 7+ messages in thread
From: Remi VANICAT @ 2002-02-08 16:53 UTC (permalink / raw)
  To: caml-list

Ceri Storey <cez@pkl.net> writes:

> I've been trying to do something similar with something i'm writing, and
> the only thing i could come up with is to store data in an object, and
> then subclass it. The problem here is that once the object has been
> coerced to the general type, it cannot be coerced back again. ie: if you
> subclass the object to say, impliment integers, then it's not possible
> for it to be coerced back to the integer type so you can retreive / act
> on the value. (withoug using Obj.magic, that is)
> 
> If anyone has any good ideas, I'd be happy to hear about them too.

polymorphic variant me be the answer, because you can get back the
type :

let fn1 (`Int x) = float_of_int x
let fn2 (`Float x) = x

let fn x =
  match x with
  | `Int _ as i -> fn1 i
  | `Float _ as f -> fn2 f
  | _ -> raise (Invalid_argument "fn")

(be aware that

let fn x =
  match x with
  | `Int _ as i -> fn1 x
  | `Float _ as f -> fn2 x
  | _ -> raise (Invalid_argument "fn")

doesn't work)

extensible function from camlp4 library may be also useful, but
I've not try it yet.

-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Allowing many types
  2002-02-08 16:53   ` Remi VANICAT
@ 2002-02-10 17:42     ` Ceri Storey
  0 siblings, 0 replies; 7+ messages in thread
From: Ceri Storey @ 2002-02-10 17:42 UTC (permalink / raw)
  To: Remi VANICAT; +Cc: caml-list

On Fri, Feb 08, 2002 at 05:53:41PM +0100, Remi VANICAT wrote:
> polymorphic variant me be the answer, because you can get back the
> type :
[...]

The problem i've found, is that It's now impossible to explicitly do
type annotations, which is a problem when you're creating.. say, a
Hashtable. 

Aside from that, it looks like it'll work well, thanks.

-- 
Ceri Storey <cez@pkl.net> http://pkl.net/~cez/
vi(1)! postfix(7)! pie(5)!
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Allowing many types
  2002-02-11 10:53 ` Ceri Storey
@ 2002-02-11 11:48   ` Remi VANICAT
  0 siblings, 0 replies; 7+ messages in thread
From: Remi VANICAT @ 2002-02-11 11:48 UTC (permalink / raw)
  To: caml-list

Ceri Storey <cez@pkl.net> writes:

> On Sun, Feb 10, 2002 at 08:43:20PM -0500, Ryan Tarpine wrote:
> > Is there a type that means "any polymorphic variant"?  Trying to set
> >  type primitive = [ `PTest ]
> > forces only `PTest to be the value of object_data.  How can I allow 
> > anything to be stored there?
> 
> According to the output of the toplevel, then the type [> `Foo | `Bar ]
> should be usable, but alas, it is not allowable in type specifictions,
> giving "Unbound type parameter [..]". 
> 
> I've also tried just using plain tuples for this, but then lots of types
> become ungeneralisable, eg in the trivial example:
> # List.map (fun x -> x) [`X; `Y; `Z];;
> - : _[> `X | `Y | `Z] list = [`X; `Y; `Z]
> 
> So say, declaring such a variable in a complied module is impossible
> (AFAIK). 

there is a hidden type variable :

type 'a t = 'a constraint 'a = [> `Foo | `Bar ]

work...


-- 
Rémi Vanicat
vanicat@labri.u-bordeaux.fr
http://dept-info.labri.u-bordeaux.fr/~vanicat
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Allowing many types
  2002-02-11  1:43 Ryan Tarpine
@ 2002-02-11 10:53 ` Ceri Storey
  2002-02-11 11:48   ` Remi VANICAT
  0 siblings, 1 reply; 7+ messages in thread
From: Ceri Storey @ 2002-02-11 10:53 UTC (permalink / raw)
  To: Ryan Tarpine; +Cc: caml-list

On Sun, Feb 10, 2002 at 08:43:20PM -0500, Ryan Tarpine wrote:
> Is there a type that means "any polymorphic variant"?  Trying to set
>  type primitive = [ `PTest ]
> forces only `PTest to be the value of object_data.  How can I allow 
> anything to be stored there?

According to the output of the toplevel, then the type [> `Foo | `Bar ]
should be usable, but alas, it is not allowable in type specifictions,
giving "Unbound type parameter [..]". 

I've also tried just using plain tuples for this, but then lots of types
become ungeneralisable, eg in the trivial example:
# List.map (fun x -> x) [`X; `Y; `Z];;
- : _[> `X | `Y | `Z] list = [`X; `Y; `Z]

So say, declaring such a variable in a complied module is impossible
(AFAIK). 

Thanks...
-- 
Ceri Storey <cez@pkl.net> http://pkl.net/~cez/
vi(1)! postfix(7)! pie(5)!
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] Allowing many types
@ 2002-02-11  1:43 Ryan Tarpine
  2002-02-11 10:53 ` Ceri Storey
  0 siblings, 1 reply; 7+ messages in thread
From: Ryan Tarpine @ 2002-02-11  1:43 UTC (permalink / raw)
  To: caml-list

>polymorphic variant me be the answer, because you can get back the
>type :
>...

I'm probably missing something obvious, but how do you store a polymorphic 
variant in a field in a record?  Before, when I just used a normal variant 
type I called "primitive", all I did was
  type my_object = { object_data : primitive; (* etc. *) }

Is there a type that means "any polymorphic variant"?  Trying to set
  type primitive = [ `PTest ]
forces only `PTest to be the value of object_data.  How can I allow anything 
to be stored there?

Thanks,

Ryan Tarpine, rtarpine@hotmail.com
"To err is human, to compute divine.  Trust your computer but not its 
programmer."
  - Morris Kingston

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

end of thread, other threads:[~2002-02-11 11:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-08  2:29 [Caml-list] Allowing many types Ryan Tarpine
2002-02-08 16:37 ` Ceri Storey
2002-02-08 16:53   ` Remi VANICAT
2002-02-10 17:42     ` Ceri Storey
2002-02-11  1:43 Ryan Tarpine
2002-02-11 10:53 ` Ceri Storey
2002-02-11 11:48   ` Remi VANICAT

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