From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] ANN: pretty-printing, type-safe marshalling, dynamic typing for free.
Date: Fri, 20 Jul 2007 07:24:18 +0100 [thread overview]
Message-ID: <200707200724.18839.jon@ffconsultancy.com> (raw)
In-Reply-To: <469EAE60.5060302@ed.ac.uk>
On Thursday 19 July 2007 01:20:48 you wrote:
> Sure! I suppose "SML-style equality" can be understood to include
> various things:
>
> (1) an equality predicate that tests for physical equality at mutable
> types and structural equality at immutable types.
>
> SOME 3 = SOME 3
> => true
>
> SOME (ref 3) = SOME (ref 3)
> => false
I forgot about this. I prefer OCaml's approach (physical and structural)
actually. I use physical equality a lot as an optimization and I think that
makes sense in an impure FPL.
> (2) a compile-time error if you try to use equality at a type for
> which it's not defined (an abstract or functional type)
>
> - op o = op o;;
> stdIn:4.1-4.12 Error: operator and operand don't agree
> [equality type required]
This is what I was thinking of.
> (3) a means to write functions that are polymorphic over equality
> types:
>
> - fn x => x = x;;
> val it = fn : ''a -> bool
and this.
> With `deriving' you get (1) and (2). You can simulate (3) using
> functors if you squint a bit. In all cases you have to be explicit
> about types, i.e. you have to write
>
> Eq.eq<int ref option> (Some (ref 3)) (Some (ref 3))
>
> rather than (say)
>
> Eq.eq (Some (ref 3)) (Some (ref 3))
>
> Specifying types is perhaps a bit of a nuisance, but to make up for it
> you get a bit more flexibility: equality can be customized at
> particular types.
Ah, I did not realise you had to add type annotations everywhere by hand. I
was rather hoping you could spot existing incorrect applications like:
lazy 3 = lazy 3
This is simply because I want to apply it to an existing code base.
> I hope this helps a bit. The documentation on the website gives more
> examples. There'll also be a paper out soon which should explain
> things in more depth.
That's great, thank you. This is a beautiful piece of work but I think what
I'm after requires a different approach.
I'm not quite sure how it could be done, short of altering the type system in
the OCaml compiler. Maybe by adding a phantom type variable to every type,
but I think that would require higher-order types:
val ( = ) : [> `eq] 'a -> [> `eq] 'a -> bool
F# takes the Haskell approach of carrying an equality function in a dictionary
with every type. That is a burden but it is probably a preferable solution
overall (you just override the equality type when necessary). Some
inconsistencies remain though, as you don't want to create a new list type
every time you use a different comparison function, so Set assumes the
equality from the dictionary whereas List.sort still uses an explicitly
specified total order.
I can't believe how often I fall for this stupid bug. Even my first attempt at
writing a GUI Sudoku solver for the OCaml Journal made the classic mistake of
applying = to a pair of Maps. It would be very nice indeed if OCaml would
catch such errors...
--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists/?e
prev parent reply other threads:[~2007-07-20 7:44 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-06-20 0:14 Jeremy Yallop
2007-06-20 8:01 ` [Caml-list] " Nicolas Pouillard
[not found] ` <AB56B84F-A45E-433B-B419-2B49F5D92043@gmail.com>
2007-06-20 10:27 ` Jeremy Yallop
2007-06-20 18:28 ` Stefan Monnier
2007-06-20 19:38 ` [Caml-list] " Jeremy Yallop
2007-06-20 21:06 ` Eric Cooper
2007-06-21 7:37 ` Nicolas Pouillard
2007-06-22 0:05 ` Jeremy Yallop
2007-07-15 4:03 ` [Caml-list] " Jon Harrop
2007-07-17 3:05 ` Jon Harrop
2007-07-19 0:20 ` Jeremy Yallop
2007-07-20 6:24 ` Jon Harrop [this message]
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=200707200724.18839.jon@ffconsultancy.com \
--to=jon@ffconsultancy.com \
--cc=caml-list@yquem.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