caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Hendrik Boom <hendrik@topoi.pooq.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] reverse coercions
Date: Fri, 10 Jun 2016 20:34:17 +0200	[thread overview]
Message-ID: <1465583657.32584.23.camel@e130.lan.sumadev.de> (raw)
In-Reply-To: <20160610181222.GA27973@topoi.pooq.com>

[-- Attachment #1: Type: text/plain, Size: 2168 bytes --]

Am Freitag, den 10.06.2016, 14:12 -0400 schrieb Hendrik Boom:
> In http://caml.inria.fr/pub/docs/u3-ocaml/ocaml-objects.html it says:
> 
> reverse coercions from a supertype to a supertype are never possible in 
> OCaml.
> 
> (a) Might one of these 'supertype's  actually be a 'subtype'?

The second should be a subtype.

> (b) Is there any way to do a run-time type-test on a value of a 
> specific statically known supertype to test whether it is actually a 
> value of a known specified subtype, and if so to proceed to use it as 
> that subtype?

This is not possible (essentially because subtyping is not bound back to
the class hierarchy (a subclass is not necessarily a subtype), and there
is no runtime representation of the types).

To some extent you can emulate what you want with open variants, e.g.
(untested):

type leaftype = ..

class type supertype =
  object
     ...
     method leaftype : leaftype
  end

class type subtype = ...

type leaftype +=
  | Subtype of subtype

class subclass : subtype =
  object(self)
    inherit supertype
    method leaftype = Leaftype self
    ...
  end

For every subclass, define a new variant for leaftype. Then, the test is

let leafobj =
  match obj#leaftype with
    | Subtype x -> x
    | _ -> failwith "coercion failed"


Gerd

> If so, I'd like to use this in a site of pattern-directed rule 
> invocations.
> 
> If not, I'd like to know if there are any well-known efficient ways 
> around this.  Of course I can effectively Godel-encode my data 
> structures using lists, and interpret all my patterns and the 
> application types I woyd have rather expressed as OCaml types, but I'm 
> looking for something more elegant, statically checked,  and 
> efficient.
> 
> -- hendrik
> 

-- 
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany    gerd@gerd-stolpmann.de
My OCaml site:          http://www.camlcity.org
Contact details:        http://www.camlcity.org/contact.html
Company homepage:       http://www.gerd-stolpmann.de
------------------------------------------------------------


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

  parent reply	other threads:[~2016-06-10 18:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-10 18:12 Hendrik Boom
2016-06-10 18:25 ` dario.teixeira
2016-06-10 19:05   ` [Caml-list] octothorpes and backticks? Hendrik Boom
2016-06-10 20:04     ` Anthony Tavener
2016-06-10 18:34 ` Gerd Stolpmann [this message]
     [not found] ` <CAPFanBFa+MJzvt_KYmReuHSzRpD=fT=nEaXB4c7dMwWSL3w7Vg@mail.gmail.com>
2016-06-10 18:59   ` [Caml-list] reverse coercions Hendrik Boom

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=1465583657.32584.23.camel@e130.lan.sumadev.de \
    --to=info@gerd-stolpmann.de \
    --cc=caml-list@inria.fr \
    --cc=hendrik@topoi.pooq.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).