X-Git-Url: http://lambda.jimpryor.net/git/gitweb.cgi?p=lambda.git;a=blobdiff_plain;f=week9.mdwn;h=639298304e2690dbf49644462aaca54ea86036e3;hp=b58a87f14a0a1325c5d8fb079728c35928209b50;hb=afbd2e27f7d3f0c904041a44bed00502f3a0f688;hpb=6c5c9ad0e2612ab4dabdfd70f0496817ff0cb41a diff --git a/week9.mdwn b/week9.mdwn index b58a87f1..63929830 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