Version française
Home     About     Download     Resources     Contact us    
Browse thread
type of ==
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] type of ==
On Monday 18 April 2005 13:11, Andreas Rossberg wrote:
> Either when 'a and 'b happen to be instantiated to the same type,

Yes, I realised this just after posting (thanks Diego!).

> or when the representation happens to be the same, e.g. 0 == false.

Assuming this is for an OCaml-only program, it sounds as though the "other" 
possible types 'a and 'b should be put into a single variant type. Then you 
can use "'a -> 'a -> bool" with "'a = your variant type". Does that make any 
sense? :-)

You'd have to indirect the physical equality once though, e.g.:

# type ('a, 'b) a = A of 'a | B of 'b;;
type ('a, 'b) a = A of 'a | B of 'b
# let compare_a x y = match x, y with
    A x, A y -> x == y
  | B x, B y -> x == y
  | _ -> false;;
val compare_a : ('a, 'b) a -> ('a, 'b) a -> bool = <fun>

A good excuse not to do this would be when you're writing a veneer between 
OCaml and C in OCaml.

> The latter also provides a good argument against making physical equality
> too polymorphic. It would break abstraction, much worse than it does
> already. In particular, a program's meaning could depend on implementation
> details (like false being represented by 0) in very questionable ways.

Yes, although this is already the case when applying "=" to abstract types 
(implementation of the type in that case, rather than of the language 
itself).

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists