Does! = Make sense in OCaml?

This seems to be an equivalence comparison for some types, but not strings.

# 3 != 3;; - : bool = false # 3 != 2;; - : bool = true 

This is as expected.

 # "odp" = "odp";; - : bool = true # "odp" != "odp";; - : bool = true # "odp" <> "odp";; - : bool = false 

Why is "odp" != "odp" evaluated to true ? What is this actually doing? Shouldn't a type error be generated?

+50
ocaml
Sep 11 '09 at 18:50
source share
5 answers

you have experienced the difference between structural and physical equality.

<> matches = (structural equality) since != matters == (physical equality)

 "odg" = "odg" (* true *) "odg" == "odg" (* false *) 

is false because each instance is created in different memory cells, doing:

 let v = "odg" v == v (* true *) v = v (* true *) 

In most cases, you will want to use = and <> .

change when structural and physical equality are equivalent:

You can use the what_is_it function and find out all types that are the same both structurally and physically. As mentioned in the comments below, and in a related article, this property will have characters, integers, one, an empty list, and some instances of variant types.

+75
Sep 11 '09 at 18:54
source share

The opposite for the != Operator is the == operator, not = .

 # "a" != "a" ;; - : bool = true # "a" == "a" ;; - : bool = false 

The == operator is "physical equality." When you enter "a" == "a" , you are comparing two different instances of strings that look the same, so the statement returns false . Although one instance returns true:

 # let str = "a" in str == str ;; - : bool = true # let str = "a" in str != str ;; - : bool = false 
+14
Sep 11 '09 at 19:00
source share

A brief description of == and != In OCaml in addition to all the correct answers that have already been provided:

1 / == and != Reveal implementation details that you really don't want to know about. Example:

 # let x = Some [] ;; val x : 'a list option = Some [] # let t = Array.create 1 x ;; val t : '_a list option array = [|Some []|] # x == t.(0) ;; - : bool = true 

So far, so good: x and t.(0) physically equal, because t.(0) contains a pointer to the same block that x points to. This is what basic implementation knowledge dictates. BUT:

 # let x = 1.125 ;; val x : float = 1.125 # let t = Array.create 1 x ;; val t : float array = [|1.125|] # x == t.(0) ;; - : bool = false 

What you see here are the results of a useful optimization involving floats.

2 / On the other hand, there is a safe way to use == , and it is a quick but incomplete way to check for structural equality.

If you write an equality function on binary trees

 let equal t1 t2 = match ... 

checking t1 and t2 for physical equality is a quick way to find that they are obviously structurally equal, without even requiring a second review and reading. I.e:

 let equal t1 t2 = if t1 == t2 then true else match ... 

And if you remember that in OCaml the "logical or" operator is "lazy",

 let equal t1 t1 = (t1 == t2) || match ... 
+12
Sep 13 '09 at 9:24
source share

They look like two "Toms" in your class! Because:

In this case, "odp" = "odp" because it is TWO strong> with SAME VALUE !!

So they are not == because they are TWO strong> stored in different lines different (memory) location

They are = because they have the same string value .

One step deeper, "odp" is an anonymous variable. And two anonymous variables lead to these lines of Two .

For your comfort:

 # "odp" = "odp";; - : bool = true # "odp" != "odp";; - : bool = true # "odp" <> "odp";; - : bool = false 
+2
Jun 25 '13 at 9:40
source share

ints is the only type where physical and structural equality are the same, since ints is the only type that is unpacked

0
Sep 11 '09 at 19:06
source share



All Articles