caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Type constraints
@ 2004-12-06 19:55 Jim Farrand
  2004-12-07  7:12 ` [Caml-list] " Alain Frisch
  0 siblings, 1 reply; 27+ messages in thread
From: Jim Farrand @ 2004-12-06 19:55 UTC (permalink / raw)
  To: caml-list

Hi,

I am writing a camlp4 extension to implement dynamic types in O'Caml[1].
I am doing this by generating run-time type representations of values.
When a value is "made dynamic" I tag it with type information.  Then
when it is later "made static" I check the tagged type information
against the type the value is being cast to.

(* x is a dynamic value with type Dynamic.t *)
value x = (10 => dynamic int) ; 

(* y has type int. *)
value y = (x => static int) ;

(* y has type string *)
value z = (x => static string) ;
(* (But as x doesn't have an int, this raises a Type_error exception) *)

When generating the code for the first example, I have to check that x
really has type int, so that the run-time type information I output
matches the actual type..  I do this by introducing a type constraint.

(x : int)

This works fine for monomorphic value, but I now want to extend to
polymorphic values.  The idea is that if you have a value, which has a
polymorphic type, eg.

value x = ((fun x -> x) => dynamic 'a -> 'a) ;

The problem is that the type constraint generated,

((fun x -> x) : 'a -> 'a)

does not reject values with a less general type.  This is fine when the
type given matches the function, as it does above, but causes me
problems if the type given is less general.

For example,

value x = ((fun x -> x) => dynamic 'a -> int) ;

yields

((fun x -> x) : 'a -> int)

which is happily compiled by ocaml (I want to reject it).

(To understand why this is a problem, consider the code

value y = (x => static 'a -> int) ;

My extension will accept this as the type matches the type given when
the value was made dynamic.  I can now do

value z = y "foo" ; (** Ooops! Just cast a string to an int!! *)

How can I achieve this?

It occurs to me that if I declared a record with a polymorphic type

{ foo : ! 'a . 'a -> 'a }

then values with the intended type are accepted

value t = { foo = id } 

wherease values with a less general type are not

value t = { foo = (id : 'a -> int) } ; (** Type error *)

It seems logical to me that I should be able to use similar syntax in
type constraints, eg. (id : ! 'a . 'a -> 'a), and I think that if I
could, I could use this to solve my problems.

        Objective Caml version 3.09+dev3 (2004-10-06)

# #load "camlp4r.cma" ;;

        Camlp4 Parsing version 3.09+dev3 (2004-10-06)

# value id x = x ;
value id : 'a -> 'a = <fun>
# (id : ! 'a . 'a -> 'a) ;
This expression has type 'a -> 'a but is here used with type 'b. 'b ->
'b

Why is this?

Thanks in advance, and sorry for such a long post.
Jim

[1] I know that there are all sorts of reasons why I shouldn't be doing
this.  I'm not arguing that it's a good idea, but it's certainly
interesting.
-- 
Jim Farrand


^ permalink raw reply	[flat|nested] 27+ messages in thread
* Type constraints
@ 1997-05-29 18:56 Ernesto Posse
  1997-05-30  7:41 ` Pascal Brisset
                   ` (4 more replies)
  0 siblings, 5 replies; 27+ messages in thread
From: Ernesto Posse @ 1997-05-29 18:56 UTC (permalink / raw)
  To: Caml List

[My apologies for not including a French Version]

Hello. I have the folowing problem. I need to define some interrelated
types as follows:

type 'a node = {x: 'a; y: t1}
and t1 = A | B of t1*t1
and t2 = C | D of (string * t2) node

The interpreter prints the inferred type:

type 'a node = { x: 'a; y: t1 } constraint 'a = string * t2
type t1 = | A | B of t1 * t1
type t2 = | C | D of (string * t2) node

Now, if I add another constructor:

type 'a node = {x: 'a; y: t1}
and t1 = A | B of t1*t1
and t2 = C | D of (string * t2) node | E of bool node

I obtain this message:

Characters 98-102:
This type parameter bool should be an instance of type string * t2

A solution to this would be something like:

type ('a,'b) node = {x: 'a; y: 'b}
and t1 = A | B of t1*t1
and t2 = C | D of (string * t2, t1) node | E of (bool, t1) node

This works, but I am not sure to understand the type clash in the former
example. Why did the type synthetizer infer that constraint?

Thank you for any insights on this.

-- 
Ernesto Posse
Ingeniero de Sistemas y Computacion
(Systems and Computing Engineer)
Universidad de los Andes
Santafe de Bogota
Colombia
e-mail: mposada@impsat.net.co





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

end of thread, other threads:[~2004-12-09  4:53 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-06 19:55 Type constraints Jim Farrand
2004-12-07  7:12 ` [Caml-list] " Alain Frisch
2004-12-07 13:43   ` Damien Doligez
2004-12-07 14:57     ` Andreas Rossberg
2004-12-07 17:44       ` Damien Doligez
2004-12-07 18:08         ` Alain Frisch
2004-12-07 21:04           ` Damien Doligez
2004-12-07 21:43             ` Alain Frisch
2004-12-08  3:30               ` nakata keiko
     [not found]                 ` <8002B033-4906-11D9-8195-000D9345235C@inria.fr>
2004-12-09  0:56                   ` nakata keiko
2004-12-09  1:27                     ` Jacques Garrigue
2004-12-08 10:53               ` Damien Doligez
2004-12-08 12:39                 ` Alain Frisch
2004-12-08 14:23                   ` Jacques Garrigue
2004-12-09  3:07                     ` skaller
2004-12-09  4:53                       ` Jacques Garrigue
2004-12-08 16:10                 ` Xavier Leroy
2004-12-07 18:13         ` William Lovas
2004-12-08  0:27           ` Jacques Garrigue
2004-12-07 18:41         ` Boris Yakobowski
2004-12-07 19:38   ` Jim Farrand
  -- strict thread matches above, loose matches on Subject: below --
1997-05-29 18:56 Ernesto Posse
1997-05-30  7:41 ` Pascal Brisset
1997-05-30  9:24 ` Didier Rousseau
1997-05-30 10:01 ` Vale'rie Me'nissier-Morain
1997-05-30 11:09 ` Wolfgang Lux
1997-05-30 12:17 ` Jerome Vouillon

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