caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Rogoff <bpr@best.com>
To: caml-list@inria.fr
Subject: Polymorphic variants question
Date: Tue, 25 Apr 2000 17:25:09 -0700 (PDT)	[thread overview]
Message-ID: <Pine.BSF.4.21.0004251643180.11908-100000@shell5.ba.best.com> (raw)

Hi,

A long time ago John Prevost posted a nice trick to get a kind of 
downcasting in OCaml

class type ['a] super =
  object
    method downcast : 'a
  end;;

class type ['a] a =
  object
    inherit ['a] super
    method do_a : int
  end;;

class type ['a] b =
  object
    inherit ['a] super
    method do_b : float
  end;;

type objsum = A of objsum a | B of objsum b;; 

Which have types 

#   class type ['a] super = object method downcast : 'a end
#   class type ['a] a = object method do_a : int method downcast : 'a end
#   class type ['a] b = object method do_b : float method downcast : 'a
end
#   type objsum = A of objsum a | B of objsum b

And to show that it works, he gave the following test code:

class test_a =
    object (s)
      method downcast = A (s :> objsum a)
      method do_a = 1
    end;;
  class test_b =
    object (s)
      method downcast = B (s :> objsum b)
      method do_b = 1.0
    end;;

#  class test_a : object method do_a : int method downcast : objsum end
#  class test_b : object method do_b : float method downcast : objsum end

and so on, testing that this meets assumptions. 

If we replace the variant with a polymorphic variant. 
type objsum = [`A of objsum a | `B of objsum b];;

and then we do the same thing as before the type checker complains. 

# class test_a =
    object (s)
      method downcast = `A (s :> objsum a)
      method do_a = 1
    end;;
# Characters 6-103:
# Some type variables are unbound in this type:
# class test_a : object method do_a : int method downcast : #objsum[>`A]
end
# The method downcast has type #objsum[>`A] where 'a is unbound

This is a good error message, and it tells me what to do to fix the
problem right away: I add an annotation to constrain that "#objsum[>`A]" 

# class test_a =
  object (s)
    method downcast : objsum = `A (s :> objsum a)
    method do_a = 1
  end;;
  class test_b =
    object (s)
      method downcast : objsum = `B (s :> objsum b)
      method do_b = 1.0
    end;;

# class test_a : object method do_a : int method downcast : objsum end
# class test_b : object method do_b : float method downcast : objsum end

Now, in order to avoid surprises in the future, can someone tell me why I
should have expected that type "#objsum[>`A]" to be computed, and hence known
that I would need to constrain the type? It makes sense, but I don't yet
have a good mental model for the typing of polymorphic variants
which would have allowed me to write the correct version immediately. How 
do the experts go about mentally inferring the right types in cases like
this?

-- Brian







             reply	other threads:[~2000-04-26  6:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-04-26  0:25 Brian Rogoff [this message]
2000-04-26  9:34 ` Jacques Garrigue
2006-09-01 17:31 David Allsopp
2006-09-01 18:33 ` Chris King

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=Pine.BSF.4.21.0004251643180.11908-100000@shell5.ba.best.com \
    --to=bpr@best.com \
    --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).