Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] subtyping questions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: YAMAGATA yoriyuki <yoriyuki@m...>
Subject: [Caml-list] subtyping questions
Recently I was toying with Unicode data types but now get stuck.  It's
not a "serious" project and if I fail, no one would be harmed.  So
just if you give me a suggestion using your spare time, I would
appreciate greatly.

An appropriate Unicode string representation differs depending on
application.  For example, sometimes arrays of OS-dependent "wch" are
required, while sometimes OS-independent realization is proffered.  So I
define the basic virtual class for Unicode string (called
"Ustorage.t") and want to make functions work on all subclass of
"Ustorage.t".  However, I soon stuck when making equivalent of Buffer
in stdlib.  What I want to do is, making a buffer return its contents
as any given subclass of "Ustorage.t".  First idea is passing a
"constructor", which receives any instant of subclass of "Ustorage.t"
and returns its copy in a desired type, as an argument and applying a
buffer internal to the constructor.  I'd like generalise the type of
the function getting contents from a buffer as
   "(#Ustorage.t -> #Ustorage.t as 'a) -> t -> 'a"
(t is the type of buffers).  However, type checker gives the type
   "t_internal -> #Ustorage.t as 'a -> t -> 'a" 
and doesn't allow such generalisation via module
interface. (t_internal is the type of internal buffer, which I don't
want to publish.)

OK, next I tried to coerce an internal buffer to an instant of
"Ustorage.t" and want to get the type
   "Ustorage.t -> #Ustorage.t as 'a -> t -> 'a".
However, coercion doesn't work, perhaps due to binary methods.

So, what I should do?  Or, what I try to do is too weird to realize?

Here are class interfaces of related objects.  (precisely speaking,
Ustorage.t is parametrised by its "cursor" type.  So the type
expression "Ustorage.t" and "#Ustorage.t" appeared above, are
abbreviation of "Usotrage.cur Ustorage.t" and "#Ustorage.cur
#Ustorage.t".)

In the file ustorage.mli,
class virtual cur :
object ('a)
(* Uchar.t is the type of unicode characters *)
  method virtual get : Uchar.t
  method virtual copy : unit -> 'a
  method virtual next : 'a
  method virtual prev : 'a
  method virtual incr : unit -> unit
  method virtual decr : unit -> unit
  method virtual eq : 'a -> bool
  method virtual leq : 'a -> bool		(*<=*)
  method virtual geq : 'a -> bool		(*>=*)
  method virtual dst : 'a -> int		(*distance*)
end

class virtual ['b] t :
  object ('a)
    constraint 'b = #cur 
    method virtual first : 'b
    method virtual last : 'b
    method virtual len : int
  end

in ubuffer.ml
class t_internal : int  -> int -> int -> string ->
  object ('a)
    inherit [cur_internal] Ustorage.t
    val rep_len	: int	(* number of UCS-characters *)
    val rep_point  : int (* position of the newly added character.*)
    val rep_buf : string ref
    method len : int
    method point : int
    method buf : string
    method clear : unit -> unit
    method reset : unit -> unit 
    method add_char : Uchar.t -> unit
    method add_buffer : 'a -> unit
    method copy : unit -> 'a
    method append : 'a -> 'a
    method first : cur_internal
    method last ; cur_internal
    method resize : int -> unit
    method private put_byte : char -> unit
    method private add_code : int -> unit
  end
and cur_internal : string ref -> int ref -> int ref ->
  object('a)
    inherit Ustorage.cur
    val mutable p : int
    method get : Uchar.t
    method copy : unit -> 'a
    method incr : unit -> unit
    method decr : unit -> unit
    method next : 'a
    method prev :'a
    method eq :'a -> bool
    method leq : 'a -> bool
    method geq : 'a -> bool
    method dst : 'a -> int
    method pos : int
    method storage : stirng ref
    method last_pos : int ref
  end

Sorry for a lengthy mail.
-------------------
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