caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: YAMAGATA yoriyuki <yoriyuki@ms.u-tokyo.ac.jp>
To: caml-list@inria.fr
Subject: [Caml-list] subtyping questions
Date: Sun, 24 Feb 2002 01:10:22 +0900	[thread overview]
Message-ID: <20020224011022E.yoriyuki@ms.u-tokyo.ac.jp> (raw)

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


                 reply	other threads:[~2002-02-23 16:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020224011022E.yoriyuki@ms.u-tokyo.ac.jp \
    --to=yoriyuki@ms.u-tokyo.ac.jp \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).