From: oleg@pobox.com
To: garrigue@math.nagoya-u.ac.jp
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Comparing two things of any two types, in pure
Date: Wed, 13 Sep 2006 02:14:35 -0700 (PDT) [thread overview]
Message-ID: <20060913091435.537F4AC04@Adric.metnet.fnmoc.navy.mil> (raw)
In-Reply-To: <20060909.161349.09849160.garrigue@math.nagoya-u.ac.jp>
Hello!
> > It seems however that open polymorphic variants are ideal for the
> > task. We start with
> >
> > let myeq (x : [>]) (y : [>]) = false;;
> [...]
> > We can add another clause, for int->bool functions:
> > let myeq x y = match (x,y) with
> > (`T'int2bool (x : int->bool), `T'int2bool y) -> x == y
> > | _ -> myeq x y;;
> [...]
> > Let us extend myeq once again:
> > let myeq x y = match (x,y) with
> > (`T'int2bool'2bool (x : (int->bool)->bool),
> > `T'int2bool'2bool y) -> x == y
> > | _ -> myeq x y;;
> While such a solution is appealing, there are drawbacks.
> 1) You have to add a new case for each new function type, rather than
> each type constructor.
> 2) In the open case, there is no static protection against mistyping a
> variant constructor. But you can check it dynamically...
> This will return true only if x's type is handled by myeq.
> 2) Polymorphic variant typing does not always play well with
> modularity. ... For this reason, exceptions may be better for
> extension across modules.
The drawback #1 is a feature! The intent was to play around the
similarity between such polymorphic variants and dependent sums. The
tag like `T'int2bool almost certainly has a run-time representation --
that's what makes (run-time) matching possible. Let's call the type of
these run-time values typeRep. Therefore, the value
`T'int2bool (x : int->bool)
might be thought of as
let tag = (repr `T'int2bool) : typerep in (tag, x : reflect(tag))
or, in a more familiar notation,
Sigma (tag: typeRep) x : reflect(tag)
where reflect is a function from terms to types. It should be a function:
identical tags correspond to identical types. Then the myeq function
might be thought of as
let myeq x y = open x with (tag1,xv : reflect(tag1)) in
open y with (tag2,yv : reflect(tag2)) in
tag1 = tag2 && (==) [reflect(tag1)] xv yv
where [t] is a type application. Then one may dream of the ways to
generate these tags a bit more systematically (actually, there is: the
compiler has the way to represent types, and in MetaOCaml, these types
can be reified to run-time values).
next prev parent reply other threads:[~2006-09-13 9:18 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-09-09 3:11 Comparing two things of any two types, in pure OCaml oleg
2006-09-09 7:13 ` [Caml-list] " Jacques Garrigue
2006-09-13 9:14 ` oleg [this message]
2006-09-16 18:53 ` Improper generic equality in Caml (Rossberg's SML vs Caml) Diego Olivier Fernandez Pons
2006-09-16 20:58 ` [Caml-list] " Jacques Garrigue
2006-09-17 5:08 ` rossberg
2006-09-17 9:45 ` skaller
2006-09-17 12:43 ` rossberg
2006-09-14 4:36 ` [Caml-list] Comparing two things of any two types, in pure OCaml brogoff
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=20060913091435.537F4AC04@Adric.metnet.fnmoc.navy.mil \
--to=oleg@pobox.com \
--cc=caml-list@inria.fr \
--cc=garrigue@math.nagoya-u.ac.jp \
/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).