Version française
Home     About     Download     Resources     Contact us    
Browse thread
polymorphic variants (2) + magic
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: David Chemouil <David.Chemouil@e...>
Subject: polymorphic variants (2) + magic


Considering my two previous posts ("polymorphic variants" and
"Obj.magic"), I'd like to show how I had to merge them to have my
application work.

Indeed, I don't know yet completely the type 'argument', but I already
know some of its constructors. Then I use polymorphic constructors when
I need them and, when I'm able to do it, I will generate the 'argument'
type. 

The problem is that there are already functions which manipulate the
'argument' type. So, if I try to use them on an already known
polymorphic constructor, it doesn't work, because the constructor is not
known to belong to 'argument', as 'argument' isn't defined. 

My problem is then to be able to apply a function, of which the domain
is a yet undefined type, to a polymorphic constructor. My idea was to
have my already known polymorphic constructors considered as weak type
variables. Here is a simple example of the situation (I consider `A as
an already known constructor):

# type argument;;
type argument
# let id : argument -> argument = function x -> x;;
val id : argument -> argument = <fun>
# let x = `A;;
val x : [> `A] = `A
# let y = ! (ref `A);;
val y : _[> `A] = `A
# let z = Obj.magic `A;;
val z : '_a = <poly>
# id x;;
Characters 3-4:
This expression has type [> `A] but is here used with type argument
# id y;;
Characters 3-4:
This expression has type [> `A] but is here used with type argument
# id z;;
- : argument = <abstr>
# z;;
- : argument = <abstr>

As the example shows, the only solution for me is to use Obj.magic. Then
I'm sure everything will be well-typed. But is it completely correct to
do like this? 


-- 
David Chemouil [mailto:chemouil@enseeiht.fr] [mobile: 06 84 16 26 65]

Laboratoire d'informatique et de mathématiques appliquées (IRIT-INPT)

"Je vous ai fait trop faibles pour sortir du gouffre, parce que 
 je vous ai fait assez forts pour n'y point tomber" -- Rousseau