caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: peng.zang@gmail.com
Cc: caml-list@yquem.inria.fr, Goswin von Brederlow <goswin-v-b@web.de>
Subject: Re: [Caml-list] Bug? Constraints get ignored in methods
Date: Wed, 01 Apr 2009 05:25:15 +0200	[thread overview]
Message-ID: <87vdppdpic.fsf@frosties.localdomain> (raw)
In-Reply-To: <200903312124.38637.peng.zang@gmail.com> (Peng Zang's message of "Tue, 31 Mar 2009 21:24:36 -0400")

Peng Zang <peng.zang@gmail.com> writes:

> Hi,
>
> Here's an example of how constraints are specified for polymorphic methods.  
> In this example I define a list type which can compare to anything that is 
> foldable.
>
>   class type ['a] foldable = object
>     method foldl : 'z. ('z -> 'a -> 'z) -> 'z -> 'z
>   end
>
>   class type ['a] mylist = object
>     inherit ['a] foldable
>     method compare : 'z. ('a #foldable as 'z) -> int
>   end
>
> Direct application to your example would not work:
>
>   # class virtual base = object
>       method virtual setnext : 'a. (#base as 'a) option -> unit
>     end
>   Error: This type scheme cannot quantify 'a : it escapes this scope.
>   #
>
> OCaml does not allow the recursive reference when the method is polymorphic.  
> One option is to just deal with coercions or a function that does it for you:
>
>   class virtual base = object
>     method virtual setnext : base option -> unit
>   end
>
>   let callsetnext (obj:#base) (n:#base option) = 
>     obj#setnext (n :> base option)

That is the part I wanted to clean up / simplify. Doesn't look nice if
some methods are called with # and others need the wrapper function.

> Another option is to factor out the basic operations you need like the in list 
> example.  I didn't make the list compare method work with other lists, I made 
> it more general to work with anything that is foldable.  This avoids the 
> recursive reference because foldable is defined ahead of time.

That was what I did except with virtual methods where the implementation is
type specific.

> Cheers,
>
> Peng

So something like this:

class type linked = object val mutable next : #linked option end
class type base_type = object
  inherit linked
  method set_next : 'a. (#linked as 'a) option -> unit
end

That could actually work. I hope. Thanks.

MfG
        Goswin


  reply	other threads:[~2009-04-01  3:25 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-31 22:05 Goswin von Brederlow
2009-03-31 23:01 ` [Caml-list] " Martin Jambon
2009-03-31 23:12   ` Martin Jambon
2009-03-31 23:52   ` Goswin von Brederlow
2009-04-01  0:08   ` Goswin von Brederlow
2009-04-01 11:41     ` Martin Jambon
2009-04-01 15:57       ` Goswin von Brederlow
2009-04-01 18:45         ` Martin Jambon
2009-04-01  1:24 ` Peng Zang
2009-04-01  3:25   ` Goswin von Brederlow [this message]
2009-04-02  8:39 ` Jacques GARRIGUE
2009-04-03 20:53   ` Goswin von Brederlow
2009-04-06  4:30     ` Jacques Garrigue

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=87vdppdpic.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --cc=caml-list@yquem.inria.fr \
    --cc=peng.zang@gmail.com \
    /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).