From: Jim Pryor Date: Tue, 23 Nov 2010 04:15:39 +0000 (-0500) Subject: week9 expand on =/== X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=commitdiff_plain;h=54fb8fb43f80f2e63354c79db8d0c116b2da11f5 week9 expand on =/== Signed-off-by: Jim Pryor --- diff --git a/week9.mdwn b/week9.mdwn index cf598202..f4a2c740 100644 --- a/week9.mdwn +++ b/week9.mdwn @@ -625,7 +625,22 @@ Programming languages tend to provide a bunch of mutation-related capabilities a Terminological note: in OCaml, `=` and `<>` express the qualitative (in)discernibility relations, also expressed in Scheme with `equal?`. In OCaml, `==` and `!=` express the numerical (non)identity relations, also expressed in Scheme with `eq?`. `=` also has other syntactic roles in OCaml, such as in the form `let x = value in ...`. In other languages, like C and Python, `=` is commonly used just for assignment (of either of the sorts we've now seen: `let x = value in ...` or `change x to value in ...`). The symbols `==` and `!=` are commonly used to express qualitative (in)discernibility in these languages. Python expresses numerical (non)identity with `is` and `is not`. What an unattractive mess. Don't get me started on Haskell (qualitative discernibility is `/=`) and Lua (physical (non)identity is `==` and `~=`). - Note that neither of the equality predicates here being considered are the same as the "hyperequals" predicate mentioned above. For example, in the following (fictional) language: + The `=` and `==` predicates in OCaml do not between them cover all the natural cases you may want to test for. For instance, if `ycell` is a reference cell, then `ref !ycell` will always be a numerically distinct reference cell containing the same value. We get this pattern of comparisons: + + ycell == ycell + ycell != ref !ycell + + ycell = ycell + ycell = ref !ycell + + But now what about? + + (0, 1, ycell) ? (0, 1, ycell) + (0, 1. ycell) ? (0, 1. ref !ycell) + + You might expect that there'd be an equality predicate that counts the first pair as equal, but not the second. But there isn't. OCaml's physical identity predicate `==` counts each pair as non-equal, even though the first of them involves the same structure containing only physically identical components. OCaml's structural equality preficate `=` counts both pairs as equal; it's insensitive to the lack of physical identity between `ycell` and `ref !ycell`. + + Additionally, note that none of the equality predicates so far considered is the same as the "hyperequals" predicate mentioned above. For example, in the following (fictional) language: let ycell = ref 1 in let xcell = ref 1