Does != have meaning in OCaml?
Asked Answered
C

5

68

It seems to be an equivalency 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 does "odp" != "odp" evaluate to true? What is it actually doing? Shouldn't it generate a type error?

Comply answered 11/9, 2009 at 18:50 Comment(0)
P
103

you have experienced the difference between structural and physical equality.

<> is to = (structural equality) as != is to == (physical equality)

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

is false because each is instantiated in different memory locations, doing:

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

Most of the time you'll want to use = and <>.

edit about when structural and physical equality are equivalent:

You can use the what_is_it function and find out all the types that would be equal both structurally and physically. As mentioned in the comments below, and in the linked article, characters, integers, unit, empty list, and some instances of variant types will have this property.

Peerage answered 11/9, 2009 at 18:54 Comment(0)
G
18

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

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

The == operator is a "physical equality". When you type "a" == "a", you compare two different instances of strings that happen to look alike, so the operator returns false. While having one instance makes it return true:

# let str = "a"
  in str == str ;;
- : bool = true
# let str = "a"
  in str != str ;;
- : bool = false
Gothicism answered 11/9, 2009 at 19:0 Comment(0)
H
15

A quick explanation about == and != in OCaml in addition to all the correct answers that have already been provided:

1/ == and != expose 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) are physically equal because t.(0) contains a pointer to the same block that x is pointing to. This is what basic knowledge of the implementation 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 are seeing here are the results of an otherwise useful optimization involving floats.

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

If you are writing an equality function on binary trees

let equal t1 t2 =
  match ...

checking t1 and t2 for physical equality is a quick way to detect that they are obviously structurally equal, without even having to recurse and read them. That is:

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

And if you keep in mind that in OCaml the “boolean or” operator is “lazy”,

let equal t1 t1 =
  (t1 == t2) ||
  match ...
Hellkite answered 13/9, 2009 at 9:24 Comment(0)
K
2

They are like two "Tom"s in your class! Because:

In this case, "odp" = "odp" because they are TWO strings with SAME VALUE!!

So they are not == because they are TWO different strings store in different (Memory) location

They are = because they have the identical string value.

One more step deeper, "odp" is anonymous variable. And two anonymous variable leads to this Two strings.

For your convenience:

# "odp" = "odp";; 
- : bool = true 
# "odp" != "odp";; 
- : bool = true 
# "odp" <> "odp";; 
- : bool = false
Kong answered 25/6, 2013 at 9:40 Comment(0)
C
1

ints are the only type where physical and structural equality are the same, because ints are the only type that is unboxed

Cetinje answered 11/9, 2009 at 19:6 Comment(3)
ints and chars are the only types... FTFYPeerage
To shorten this list up, it really should say anything integer like, as it also includes empty lists and unitPeerage
...and anything that has been hash consed. That is done precisely so you can replace structural equality with physical equality to improve performance.Samons

© 2022 - 2024 — McMap. All rights reserved.