caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] reverse coercions
@ 2016-06-10 18:12 Hendrik Boom
  2016-06-10 18:25 ` dario.teixeira
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Hendrik Boom @ 2016-06-10 18:12 UTC (permalink / raw)
  To: caml-list

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'?

(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?

If so, I'd like to use this in a suite 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] reverse coercions
  2016-06-10 18:12 [Caml-list] reverse coercions Hendrik Boom
@ 2016-06-10 18:25 ` dario.teixeira
  2016-06-10 19:05   ` [Caml-list] octothorpes and backticks? Hendrik Boom
  2016-06-10 18:34 ` [Caml-list] reverse coercions Gerd Stolpmann
       [not found] ` <CAPFanBFa+MJzvt_KYmReuHSzRpD=fT=nEaXB4c7dMwWSL3w7Vg@mail.gmail.com>
  2 siblings, 1 reply; 6+ messages in thread
From: dario.teixeira @ 2016-06-10 18:25 UTC (permalink / raw)
  To: Hendrik Boom, caml-list

Hi,

> (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?

I reckon the "#" syntax may be what you are looking for.
It works with polymorphic variants and objects (types
where row-polymorphism applies).  Small example:

type foo = [ `A | `B | `C ]
type bar = [ `D | `E | `F ]
type foobar = [ foo | bar ]

let f = function
  | #foo -> "Foo"
  | #bar -> "Bar"

Hope this helps!
Best regards,

Dario Teixeira

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] reverse coercions
  2016-06-10 18:12 [Caml-list] reverse coercions Hendrik Boom
  2016-06-10 18:25 ` dario.teixeira
@ 2016-06-10 18:34 ` Gerd Stolpmann
       [not found] ` <CAPFanBFa+MJzvt_KYmReuHSzRpD=fT=nEaXB4c7dMwWSL3w7Vg@mail.gmail.com>
  2 siblings, 0 replies; 6+ messages in thread
From: Gerd Stolpmann @ 2016-06-10 18:34 UTC (permalink / raw)
  To: Hendrik Boom; +Cc: caml-list

[-- 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 --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] reverse coercions
       [not found] ` <CAPFanBFa+MJzvt_KYmReuHSzRpD=fT=nEaXB4c7dMwWSL3w7Vg@mail.gmail.com>
@ 2016-06-10 18:59   ` Hendrik Boom
  0 siblings, 0 replies; 6+ messages in thread
From: Hendrik Boom @ 2016-06-10 18:59 UTC (permalink / raw)
  To: Gabriel Scherer; +Cc: caml-list

On Fri, Jun 10, 2016 at 02:40:29PM -0400, Gabriel Scherer wrote:
> Maybe it would help to have some examples of the use-cases you have in
> mind (pattern-directed rule invocations?) to find which solutions
> could be suitable.

Yes, that would be nice.  However, I'm still at an early stage  of 
figuring out what I want to do, and am not yet that specific.  I'm 
checking what the built-in tools do so I have somme idea which 
approaches to an ill-defined problem might possibly be feasible.
I haven't even got to the point of writing my algoriths in English, 
which I often do before coding.

-- hendrik

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Caml-list] octothorpes and backticks?
  2016-06-10 18:25 ` dario.teixeira
@ 2016-06-10 19:05   ` Hendrik Boom
  2016-06-10 20:04     ` Anthony Tavener
  0 siblings, 1 reply; 6+ messages in thread
From: Hendrik Boom @ 2016-06-10 19:05 UTC (permalink / raw)
  To: dario.teixeira; +Cc: caml-list

On Fri, Jun 10, 2016 at 06:25:57PM +0000, dario.teixeira@yahoo.com wrote:
> Hi,
> 
> > (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?
> 
> I reckon the "#" syntax may be what you are looking for.
> It works with polymorphic variants and objects (types
> where row-polymorphism applies).  Small example:
> 
> type foo = [ `A | `B | `C ]
> type bar = [ `D | `E | `F ]
> type foobar = [ foo | bar ]
> 
> let f = function
>   | #foo -> "Foo"
>   | #bar -> "Bar"

Haven't encountered the 3 syntax or the ` syntax.  Where is it 
docuumented, so I can start to understand?

-- hendrik

> 
> Hope this helps!
> Best regards,
> 
> Dario Teixeira

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] octothorpes and backticks?
  2016-06-10 19:05   ` [Caml-list] octothorpes and backticks? Hendrik Boom
@ 2016-06-10 20:04     ` Anthony Tavener
  0 siblings, 0 replies; 6+ messages in thread
From: Anthony Tavener @ 2016-06-10 20:04 UTC (permalink / raw)
  To: Hendrik Boom; +Cc: dario.teixeira, caml-list

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

Backticks are for Polymorphic Variants...

Documentation is in plain sight... and yet kind of hidden. :) Here's the
Polymorphic Variants section of the manual:
http://caml.inria.fr/pub/docs/manual-ocaml/lablexamples.html#sec46

And at the bottom of the "Advanced Use" subsection it introduces the
abbreviated syntax for matching.

The Polymorphic Variant chapter of Real World OCaml might be useful too,
though I couldn't see any reference to the abbreviated matching with #,
even in the subtyping topic in the Objects chapter.

On Fri, Jun 10, 2016 at 1:05 PM, Hendrik Boom <hendrik@topoi.pooq.com>
wrote:

> On Fri, Jun 10, 2016 at 06:25:57PM +0000, dario.teixeira@yahoo.com wrote:
> > Hi,
> >
> > > (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?
> >
> > I reckon the "#" syntax may be what you are looking for.
> > It works with polymorphic variants and objects (types
> > where row-polymorphism applies).  Small example:
> >
> > type foo = [ `A | `B | `C ]
> > type bar = [ `D | `E | `F ]
> > type foobar = [ foo | bar ]
> >
> > let f = function
> >   | #foo -> "Foo"
> >   | #bar -> "Bar"
>
> Haven't encountered the 3 syntax or the ` syntax.  Where is it
> docuumented, so I can start to understand?
>
> -- hendrik
>
> >
> > Hope this helps!
> > Best regards,
> >
> > Dario Teixeira
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

[-- Attachment #2: Type: text/html, Size: 2761 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-06-10 20:04 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-10 18:12 [Caml-list] reverse coercions 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 ` [Caml-list] reverse coercions Gerd Stolpmann
     [not found] ` <CAPFanBFa+MJzvt_KYmReuHSzRpD=fT=nEaXB4c7dMwWSL3w7Vg@mail.gmail.com>
2016-06-10 18:59   ` Hendrik Boom

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).