caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] checking for same ctor type?
@ 2001-06-28  2:40 Chris Hecker
  2001-06-28  2:55 ` Brian Rogoff
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Chris Hecker @ 2001-06-28  2:40 UTC (permalink / raw)
  To: caml-list


What's the right way to check if two variants have the same ctor, but
not necessarily the same values in the ctor parms?

For example, I want to check if both data_types are Strings:

type data_type =
    String of string
  | Float of float

let a = String "foo"
let b = String "bar"
let c = Float 2.0

let ctor_equal a b = ???

ctor_equal a b = true
ctor_equal a c = false

I can think of a couple definitions of ctor_equal, one using
Hashtbl.hash_param 1 1 and one using Obj.tag (which both appear to be
doing the same thing).  Both of these seem a bit cheesy and
implementation dependent.

Is there a better way?

Chris

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] checking for same ctor type?
  2001-06-28  2:40 [Caml-list] checking for same ctor type? Chris Hecker
@ 2001-06-28  2:55 ` Brian Rogoff
       [not found] ` <Pine.BSF.4.21.0106271950090.28760-100000@shell5.ba.best.co m>
  2001-06-29 13:14 ` Chris Quinn
  2 siblings, 0 replies; 4+ messages in thread
From: Brian Rogoff @ 2001-06-28  2:55 UTC (permalink / raw)
  To: Chris Hecker; +Cc: caml-list

On Wed, 27 Jun 2001, Chris Hecker wrote:
> What's the right way to check if two variants have the same ctor, but
> not necessarily the same values in the ctor parms?
> 
> For example, I want to check if both data_types are Strings:
> 
> type data_type =
>     String of string
>   | Float of float
> 
> let a = String "foo"
> let b = String "bar"
> let c = Float 2.0
> 
> let ctor_equal a b = ???

let ctor_equal a b = 
  match a,b with 
  String(_),String(_) | Float(_),Float(_) -> true | _,_ -> false

> ctor_equal a b = true
> ctor_equal a c = false
> 
> I can think of a couple definitions of ctor_equal, one using
> Hashtbl.hash_param 1 1 and one using Obj.tag (which both appear to be
> doing the same thing).  Both of these seem a bit cheesy and
> implementation dependent.

Yes, but there's only one implementation, right? 

> Is there a better way?

Beauty is in the eye of the beholder. 

-- Brian


-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] checking for same ctor type?
       [not found] ` <Pine.BSF.4.21.0106271950090.28760-100000@shell5.ba.best.co m>
@ 2001-06-28  3:06   ` Chris Hecker
  0 siblings, 0 replies; 4+ messages in thread
From: Chris Hecker @ 2001-06-28  3:06 UTC (permalink / raw)
  To: Brian Rogoff; +Cc: caml-list


>let ctor_equal a b = 
>  match a,b with 
>  String(_),String(_) | Float(_),Float(_) -> true | _,_ -> false

Right, I should have added "without writing out all the cases".  :)

> Yes, but there's only one implementation, right? 

Unless they never update it again, then "implementation dependent" is usually a bad thing, in my book (and the ocaml folks shouldn't want people depending on implementation details either, since it makes their lives miserable when it comes time to update stuff).

Chris

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

* Re: [Caml-list] checking for same ctor type?
  2001-06-28  2:40 [Caml-list] checking for same ctor type? Chris Hecker
  2001-06-28  2:55 ` Brian Rogoff
       [not found] ` <Pine.BSF.4.21.0106271950090.28760-100000@shell5.ba.best.co m>
@ 2001-06-29 13:14 ` Chris Quinn
  2 siblings, 0 replies; 4+ messages in thread
From: Chris Quinn @ 2001-06-29 13:14 UTC (permalink / raw)
  Cc: caml-list

Pervasives.compare is fine for nullary constructors or where you don't mind the performance hit of the function recursing into the structure of the arguments.

A more efficient equality predicate that is really just a cut down version of [compare]:

module Kludge =
  struct
    let cmp (a: 't) (b: 't) =
      let a' = Obj.repr a and b' = Obj.repr b in
      match Obj.is_block a', Obj.is_block b' with
      | true,true ->
          Obj.tag a' = Obj.tag b'
      | false,false ->
          a' = b'
      | _ -> false
  end

It of course relies on the physical representation of sum types.
It has the merit of working on a type of any number of constructors, but the mis-merit of also operating over functions for instance!
To constrain such a comparison to sum types would require it to be built into the compiler.

Chris Q.
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


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

end of thread, other threads:[~2001-06-29 13:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-06-28  2:40 [Caml-list] checking for same ctor type? Chris Hecker
2001-06-28  2:55 ` Brian Rogoff
     [not found] ` <Pine.BSF.4.21.0106271950090.28760-100000@shell5.ba.best.co m>
2001-06-28  3:06   ` Chris Hecker
2001-06-29 13:14 ` Chris Quinn

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